當(dāng)前位置:首頁 >  站長 >  編程技術(shù) >  正文

git log根據(jù)特定條件查詢?nèi)罩静⒔y(tǒng)計修改的代碼行數(shù)

 2020-10-30 14:59  來源: 腳本之家   我來投稿 撤稿糾錯

  域名預(yù)訂/競價,好“米”不錯過

這篇文章主要介紹了git log根據(jù)特定條件查詢?nèi)罩静⒔y(tǒng)計修改的代碼行數(shù),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下。

前言

隨著年齡的增長和知識的積累,最近常常有種豁然開朗的感覺,或者對一個已經(jīng)存在的事物突然有了新的認識,比如統(tǒng)計這個詞很早就接觸了,從沒考慮過它是什么意思,而這篇總結(jié)的題目中用了統(tǒng)計一詞,第一感覺應(yīng)該是匯總、記錄的意思,后來去查了詞條定義,也確實就是類似的解釋,從沒有刻意去學(xué)這個詞的含義,但是在每天的生活中已經(jīng)潛移默化地歸納、總結(jié)出來了。

想要統(tǒng)計就得有數(shù)據(jù)源,而 git log 命令恰恰就能提供這個數(shù)據(jù)源,git log 本身就是一個顯示日志的命令,日志記錄的是代碼庫變化的數(shù)據(jù),類似于描述代碼庫變化的 “史書”,想要描述歷史就需要大量的數(shù)據(jù)支撐,想要統(tǒng)計修改的代碼行數(shù),只要我們從歷史記錄中找到需要計算的部分就可以了。

git log

在統(tǒng)計之前我們需要先整理數(shù)據(jù),雜亂無章的數(shù)據(jù)不是不能統(tǒng)計,只是計算起來更加的麻煩,所以在統(tǒng)計前需要先將數(shù)據(jù)規(guī)范化,所以我們需要先學(xué)習(xí)一下 git log 的相關(guān)操作。

我們以 redis 開源庫為例,切換到 6.0 分支,提交記錄定位到 7bf665f125a4771db095c83a7ad6ed46692cd314,以此為數(shù)據(jù)源,學(xué)習(xí)一下git log 的常用的查詢方法,其實使用不同的條件查詢就是整理、歸類數(shù)據(jù)的過程。

git log 的用法多種多樣,我們主要關(guān)心兩個大類,分別是條件篩選和顯示格式。

條件篩選

git log 條件篩選的選項非常多,使用條件篩選的選項會影響顯示的提交記錄的范圍,查找到想要顯示的提交記錄。

查詢最近幾條log

使用 -number 參數(shù)可以查詢最近幾條提交提交記錄:

$ git log -3
commit 7bf665f125a4771db095c83a7ad6ed46692cd314 (HEAD -> 6.0, tag: 6.0.6, origin/6.0)
Author: Oran Agra <oran@redislabs.com>
Date: Sun Jul 19 14:00:20 2020 +0300

Redis 6.0.6.

commit a5696bdf4f2687ab45f633ccb7cdc4ee9c2f957d
Author: Oran Agra <oran@redislabs.com>
Date: Sun Jul 19 15:33:21 2020 +0300

Run daily CI on PRs to release a branch

commit e15528bf1da1f1232fd08801ad382c915be94662
Author: Itamar Haber <itamar@redislabs.com>
Date: Thu Jul 16 21:31:36 2020 +0300

Adds SHA256SUM to redis-stable tarball upload

(cherry picked from commit 5df0a64d30e7815c0a4a75a80f165fdee0bd1db6)

查詢指定作者提交

使用 --author 參數(shù)可以查詢指定作者的提交記錄:

Albert@DESKTOP-6746UC3 MINGW64 /d/data/maingit/redis (6.0)
$ git log -2 --author='Oran Agra'
commit 7bf665f125a4771db095c83a7ad6ed46692cd314 (HEAD -> 6.0, tag: 6.0.6, origin/6.0)
Author: Oran Agra <oran@redislabs.com>
Date: Sun Jul 19 14:00:20 2020 +0300

Redis 6.0.6.

commit a5696bdf4f2687ab45f633ccb7cdc4ee9c2f957d
Author: Oran Agra <oran@redislabs.com>
Date: Sun Jul 19 15:33:21 2020 +0300

Run daily CI on PRs to release a branch

查詢指定時間段的日志

這個可選參數(shù)比較多,比如 --since、--until、--before、--after 等等,從意思很容易分辨怎么使用:

查詢2020-01-01到2020-04-01的提交記錄

$ git log -2 --after=2020-01-01 --before=2020-04-01
commit 957e917a84ac9979f18145a4d0b53386f5ce4fd9 (tag: 6.0-rc3)
Author: antirez <antirez@gmail.com>
Date: Tue Mar 31 17:56:04 2020 +0200

Redis 6.0-RC3.

commit ef1b1f01a84e969ea368e7fdbaf0d10615743269
Author: antirez <antirez@gmail.com>
Date: Tue Mar 31 17:41:23 2020 +0200

cast raxSize() to avoid warning with format spec.

恰好逮到了原作者的提交~

查詢1年前的提交記錄

$ git log -2 --until=1.year.ago
commit 86aade9a024c3582665903d0cc0c5692c6677cfd
Merge: 89ad0ca56 3bfcae247
Author: Salvatore Sanfilippo <antirez@gmail.com>
Date: Thu Sep 5 13:30:26 2019 +0200

Merge pull request #6364 from oranagra/fix_module_aux_when

Fix to module aux data rdb format for backwards compatibility with old check-rdb

commit 3bfcae247a1c51788940bd4d2f32751ead451e42
Author: Oran Agra <oran@redislabs.com>
Date: Thu Sep 5 14:11:37 2019 +0300

Fix to module aux data rdb format for backwards compatibility with old check-rdb

When implementing the code that saves and loads these aux fields we used rdb
format that was added for that in redis 5.0, but then we added the 'when' field
which meant that the old redis-check-rdb won't be able to skip these.
this fix adds an opcode as if that 'when' is part of the module data.

查詢包含指定描述內(nèi)容的提交記錄

這里用可以使用 --grep 參數(shù),可以過濾出包含指定內(nèi)容的提交記錄,這里指的是在 commit 描述中篩選符合條件的提交,比如查找提交描述中包含 client 的提交記錄:

$ git log -2 --grep='client'
commit 0f75036c07db48dfcf605e090216a4447edc38fc
Author: Wen Hui <wen.hui.ware@gmail.com>
Date: Wed Jul 15 05:38:47 2020 -0400

correct error msg for num connections reaching maxclients in cluster mode (#7444)


(cherry picked from commit d85af4d6f5fbe9cb9787b81583627cd74b47f838)

commit f89f50dbd06247677b8cb3927cbb88c1b5384061
Author: Oran Agra <oran@redislabs.com>
Date: Tue Jul 14 20:21:59 2020 +0300

diskless master disconnect replicas when rdb child failed (#7518)

in case the rdb child failed, crashed or terminated unexpectedly redis
would have marked the replica clients with repl_put_online_on_ack and
then kill them only after a minute when no ack was received.

it would not stream anything to these connections, so the only effect of
this bug is a delay of 1 minute in the replicas attempt to re-connect.

(cherry picked from commit a176cb56a3c0235adddde33fcbaee2369a5af73e)

查找指定分支的提交記錄

使用 git log 默認查找的是當(dāng)前分支的提交記錄,如果想查詢其他分支的記錄直接在命令后面加上分支名字就行,比如查詢 arm 分支上的提交記錄:

$ git log -2 arm
commit 7329cc39818a05c168e7d1e791afb03c089f1933 (origin/arm, arm)
Author: Salvatore Sanfilippo <antirez@gmail.com>
Date: Sun Feb 19 15:07:08 2017 +0000

ARM: Avoid fast path for BITOP.

GCC will produce certain unaligned multi load-store instructions
that will be trapped by the Linux kernel since ARM v6 cannot
handle them with unaligned addresses. Better to use the slower
but safer implementation instead of generating the exception which
should be anyway very slow.

commit 4e9cf4cc7ed4b732fc4bb592f19ceb41d132954e
Author: Salvatore Sanfilippo <antirez@gmail.com>
Date: Sun Feb 19 15:02:37 2017 +0000

ARM: Use libc malloc by default.

I'm not sure how much test Jemalloc gets on ARM, moreover
compiling Redis with Jemalloc support in not very powerful
devices, like most ARMs people will build Redis on, is extremely
slow. It is possible to enable Jemalloc build anyway if needed
by using "make MALLOC=jemalloc".

其實在 git 體系中,分支名、commit、標(biāo)簽等擁有幾乎相同的含義,所以在很多場景下可以擴展互換,比如 git log 后面加上分支名就可以查詢指定分支的提交記錄,如果加上 commit 就會查詢這個 commit 之前的提交記錄,如果加上標(biāo)簽,就可以查詢這個標(biāo)簽之前的提交記錄,比如我們加一個 commit 試試:

$ git log -2 7329cc39818a05c168e7d1e791afb03c089f1933
commit 7329cc39818a05c168e7d1e791afb03c089f1933 (origin/arm, arm)
Author: Salvatore Sanfilippo <antirez@gmail.com>
Date: Sun Feb 19 15:07:08 2017 +0000

ARM: Avoid fast path for BITOP.

GCC will produce certain unaligned multi load-store instructions
that will be trapped by the Linux kernel since ARM v6 cannot
handle them with unaligned addresses. Better to use the slower
but safer implementation instead of generating the exception which
should be anyway very slow.

commit 4e9cf4cc7ed4b732fc4bb592f19ceb41d132954e
Author: Salvatore Sanfilippo <antirez@gmail.com>
Date: Sun Feb 19 15:02:37 2017 +0000

ARM: Use libc malloc by default.

I'm not sure how much test Jemalloc gets on ARM, moreover
compiling Redis with Jemalloc support in not very powerful
devices, like most ARMs people will build Redis on, is extremely
slow. It is possible to enable Jemalloc build anyway if needed
by using "make MALLOC=jemalloc".

其實在 git 體系中,分支名、commit、標(biāo)簽等擁有幾乎相同的含義,所以在很多場景下可以擴展互換,比如 git log 后面加上分支名就可以查詢指定分支的提交記錄,如果加上 commit 就會查詢這個 commit 之前的提交記錄,如果加上標(biāo)簽,就可以查詢這個標(biāo)簽之前的提交記錄,比如我們加一個 commit 試試:

$ git log -2 7329cc39818a05c168e7d1e791afb03c089f1933
commit 7329cc39818a05c168e7d1e791afb03c089f1933 (origin/arm, arm)
Author: Salvatore Sanfilippo <antirez@gmail.com>
Date: Sun Feb 19 15:07:08 2017 +0000

ARM: Avoid fast path for BITOP.

GCC will produce certain unaligned multi load-store instructions
that will be trapped by the Linux kernel since ARM v6 cannot
handle them with unaligned addresses. Better to use the slower
but safer implementation instead of generating the exception which
should be anyway very slow.

commit 4e9cf4cc7ed4b732fc4bb592f19ceb41d132954e
Author: Salvatore Sanfilippo <antirez@gmail.com>
Date: Sun Feb 19 15:02:37 2017 +0000

ARM: Use libc malloc by default.

I'm not sure how much test Jemalloc gets on ARM, moreover
compiling Redis with Jemalloc support in not very powerful
devices, like most ARMs people will build Redis on, is extremely
slow. It is possible to enable Jemalloc build anyway if needed
by using "make MALLOC=jemalloc".

因為 commit id 就是之前的 arm 分支最新的記錄,所以這個命令等價于 git log -2 arm

查詢指定 commit 之間的提交記錄

如果想查詢兩個 commit 之前的提交記錄,可以將兩個 commit id 依次放在命令后面并用 .. 連接就可以了,格式為 git log commit1..commit2,需要注意的是這樣查詢出來的提交記錄列表中不包含 commit1,其實列舉出的就是 commit1 之后又做了哪些修改提交。

$ git log e15528bf1da1f1232fd08801ad382c915be94662..7bf665f125a4771db095c83a7ad6ed46692cd314
commit 7bf665f125a4771db095c83a7ad6ed46692cd314 (HEAD -> 6.0, tag: 6.0.6, origin/6.0)
Author: Oran Agra <oran@redislabs.com>
Date: Sun Jul 19 14:00:20 2020 +0300

Redis 6.0.6.

commit a5696bdf4f2687ab45f633ccb7cdc4ee9c2f957d
Author: Oran Agra <oran@redislabs.com>
Date: Sun Jul 19 15:33:21 2020 +0300

Run daily CI on PRs to release a branch

這個特性有一個應(yīng)用就是在 merge 分支之前可以查詢究竟會 merge 哪些記錄,常見的用法比如 git log feature..dev 就是列舉出 feature 分支合并到 dev 分支將要合并的提交記錄有哪些。

$ git log 6.0..unstable
commit 324e22accf457edc996971bc97f5474349cd7c4c (unstable)
Author: antirez <antirez@gmail.com>
Date: Fri Dec 20 12:29:02 2019 +0100

Fix ip and missing mode in RM_GetClusterNodeInfo().

查詢指定文件的提交記錄

查詢指定文件的提交記錄一般直接在 git log 命令后面跟上文件名就可以,但是為了避免和分支名產(chǎn)生分歧,所以通常在文件名前面加上 -- 用來區(qū)分,-- 這個標(biāo)識符就是用來防止混淆的,放在 -- 前面的是分支名,放在后面的是文件名,相同的作用不僅僅在 git log 命令中,在其他命令比如 git checkout 中也有相同的用法。

$ git log -2 -- redis.conf
commit 7a536c2912be1fd9f62b26b7022a00644c88ef8b
Author: Yossi Gottlieb <yossigo@users.noreply.github.com>
Date: Fri Jul 10 11:33:47 2020 +0300

TLS: Session caching configuration support. (#7420)

* TLS: Session caching configuration support.
* TLS: Remove redundant config initialization.

(cherry picked from commit 3e6f2b1a45176ac3d81b95cb6025f30d7aaa1393)

commit 8312aa27d47c0befcf69eb74d0a5dc19745ffd32
Author: antirez <antirez@gmail.com>
Date: Mon Jun 22 11:21:21 2020 +0200

Clarify maxclients and cluster in conf. Remove myself too.

(cherry picked from commit 59fd178014c7cca1b0c668b30ab0d991dd3030f3)

顯示格式

git log 除了可以篩選提交記錄,還可以控制顯示格式,普通不加參數(shù),會顯示作者、郵件、提交描述信息、日期等信息。

通過添

$ git log -1
commit 7bf665f125a4771db095c83a7ad6ed46692cd314 (HEAD -> 6.0, tag: 6.0.6, origin/6.0)
Author: Oran Agra <oran@redislabs.com>
Date: Sun Jul 19 14:00:20 2020 +0300

Redis 6.0.6.

加參數(shù)可以控制和改變顯示格式,下面來看幾條常見的

顯示單行信息

git log 默認會顯示多行信息,使用 --oneline 后每條提交記錄只顯示一行信息,可以在一屏幕中查看到更多的信息

$ git log -10 --oneline
7bf665f12 (HEAD -> 6.0, tag: 6.0.6, origin/6.0) Redis 6.0.6.
a5696bdf4 Run daily CI on PRs to release a branch
e15528bf1 Adds SHA256SUM to redis-stable tarball upload
e28aa99af Support passing stack allocated module strings to moduleCreateArgvFromUserFormat (#7528)
305143004 Send null for invalidate on flush (#7469)
29b20fd52 Notify systemd on sentinel startup (#7168)
5b3668121 Add registers dump support for Apple silicon (#7453)
0f75036c0 correct error msg for num connections reaching maxclients in cluster mode (#7444)
b1a01fda9 Fix command help for unexpected options (#7476)
83f55f61a Refactor RM_KeyType() by using macro. (#7486)

顯示每條記錄中文件修改的具體行數(shù)和行體統(tǒng)計

使用 --stat 參數(shù)就可以顯示每條記錄的中修改文件的具體行數(shù)和行數(shù)統(tǒng)計

$ git log -2 --stat
commit 7bf665f125a4771db095c83a7ad6ed46692cd314 (HEAD -> 6.0, tag: 6.0.6, origin/6.0)
Author: Oran Agra <oran@redislabs.com>
Date: Sun Jul 19 14:00:20 2020 +0300

Redis 6.0.6.

00-RELEASENOTES | 245 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
src/help.h | 4 +-
src/version.h | 2 +-
3 files changed, 248 insertions(+), 3 deletions(-)

commit a5696bdf4f2687ab45f633ccb7cdc4ee9c2f957d
Author: Oran Agra <oran@redislabs.com>
Date: Sun Jul 19 15:33:21 2020 +0300

Run daily CI on PRs to release a branch

.github/workflows/daily.yml | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)

顯示每條提交記錄中文件的增加行數(shù)和刪除行數(shù)

使用 --numstat 參數(shù)會把 --stat 參數(shù)中合并顯示的修改行數(shù)拆分成增加行數(shù)和刪除行數(shù)

$ git log -2 --numstat
commit 7bf665f125a4771db095c83a7ad6ed46692cd314 (HEAD -> 6.0, tag: 6.0.6, origin/6.0)
Author: Oran Agra <oran@redislabs.com>
Date: Sun Jul 19 14:00:20 2020 +0300

Redis 6.0.6.

245 0 00-RELEASENOTES
2 2 src/help.h
1 1 src/version.h

commit a5696bdf4f2687ab45f633ccb7cdc4ee9c2f957d
Author: Oran Agra <oran@redislabs.com>
Date: Sun Jul 19 15:33:21 2020 +0300

Run daily CI on PRs to release a branch

4 2 .github/workflows/daily.yml

依次羅列各提交記錄中每個文件中增加的行數(shù)和刪除的行數(shù)

要想達到這個目的需要用到 --prety=tformat: --numstat 參數(shù),這樣的顯示格式便于統(tǒng)計

$ git log -2 --pretty=tformat: --numstat
245 0 00-RELEASENOTES
2 2 src/help.h
1 1 src/version.h
4 2 .github/workflows/daily.yml

統(tǒng)計修改的代碼行數(shù)

有了前面的鋪墊,想要統(tǒng)一修改的行數(shù)就容易了,只要配合 awk 工具就可以完成統(tǒng)計了

$ $ git log -2 --pretty=tformat: --numstat | awk '{adds += $1; subs += $2; diffs += $1 - $2} END {printf "added lines: %s removed lines: %s, diff lines: %s\n", adds, subs, diffs}'
added lines: 252 removed lines: 5, diff lines: 247

還可以統(tǒng)計兩個分支相差的代碼行數(shù)

$ git log 6.0..unstable --pretty=tformat: --numstat | awk '{adds += $1; subs += $2; diffs += $1 - $2} END {printf "added lines: %s removed lines: %s, diff lines: %s\n", adds, subs, diffs}'
added lines: 5 removed lines: 2, diff lines: 3


到這里可以發(fā)現(xiàn)前面的知識都可以用上,前面篩選的參數(shù)變了,得到的結(jié)果就變了,我們可以根據(jù)需求來調(diào)整不同的參數(shù)

總結(jié)

git log 就是一部代碼庫記錄的“史書”,對于曾經(jīng)所做的修改可以做到有史可查

git log 的選項參數(shù)可以分為篩選參數(shù)和格式參數(shù),篩選參數(shù)可以選擇記錄范圍,格式參數(shù)可以控制顯示樣式

統(tǒng)計就是按照一定規(guī)律來將數(shù)據(jù)進行匯總,在進行匯總前需要將數(shù)據(jù)進行整理,這樣匯總的工作才會更加順利

到此這篇關(guān)于git log根據(jù)特定條件查詢?nèi)罩静⒔y(tǒng)計修改的代碼行數(shù)的文章就介紹到這了,更多相關(guān)git log統(tǒng)計修改代碼行數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

文章轉(zhuǎn)自腳本之家,原文鏈接:https://www.jb51.net/article/195348.htm

申請創(chuàng)業(yè)報道,分享創(chuàng)業(yè)好點子。點擊此處,共同探討創(chuàng)業(yè)新機遇!

相關(guān)文章

熱門排行

信息推薦