gitlog根据特定条件查询⽇志并统计修改的代码⾏数
前⾔
随着年龄的增长和知识的积累,最近常常有种豁然开朗的感觉,或者对⼀个已经存在的事物突然有了新的认识,⽐如统计这个词很早就接触了,从没考虑过它是什么意思,⽽这篇总结的题⽬中⽤了统计⼀词,第⼀感觉应该是汇总、记录的意思,后来去查了词条定义,也确实就是类似的解释,从没有刻意去学这个词的含义,但是在每天的⽣活中已经潜移默化地归纳、总结出来了。
想要统计就得有数据源,⽽git log命令恰恰就能提供这个数据源,git log本⾝就是⼀个显⽰⽇志的命令,⽇志记录的是代码库变化的数据,类似于描述代码库变化的 “史书”,想要描述历史就需要⼤量的数据⽀撑,想要统计修改的代码⾏数,只要我们从历史记录中到需要计算的部分就可以了。
git log
在统计之前我们需要先整理数据,杂乱⽆章的数据不是不能统计,只是计算起来更加的⿇烦,所以在统计前需要先将数据规范化,所以我们需要先学习⼀下git log的相关操作。
我们以redis开源库为例,切换到6.0分⽀,提交记录定位到7bf665f125a4771db095c83a7ad6ed46692cd314,以此为数据源,学习⼀下git log的常⽤的查询⽅法,
其实使⽤不同的条件查询就是整理、归类数据的过程。
git log的⽤法多种多样,我们主要关⼼两个⼤类,分别是条件筛选和显⽰格式。
条件筛选
git log条件筛选的选项⾮常多,使⽤条件筛选的选项会影响显⽰的提交记录的范围,查到想要显⽰的提交记录。
查询最近⼏条log
使⽤-number参数可以查询最近⼏条提交提交记录:
$ git log -3
commit 7bf665f125a4771db095c83a7ad6ed46692cd314 (HEAD -> 6.0, tag: 6.0.6, origin/6.0)
Author: Oran Agra <oran@redislabs>
Date: Sun Jul 19 14:00:20 2020 +0300
Redis 6.0.6.
commit a5696bdf4f2687ab45f633ccb7cdc4ee9c2f957d
Author: Oran Agra <oran@redislabs>
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>
Date: Thu Jul 16 21:31:36 2020 +0300
Adds SHA256SUM to redis-stable tarball upload
(cherry picked from commit 5df0a64d30e7815c0a4a75a80f165fdee0bd1db6)
查询指定作者提交
使⽤--author参数可以查询指定作者的提交记录:
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>
Date: Sun Jul 19 14:00:20 2020 +0300
Redis 6.0.6.
commit a5696bdf4f2687ab45f633ccb7cdc4ee9c2f957d
Author: Oran Agra <oran@redislabs>
Date: Sun Jul 19 15:33:21 2020 +0300
Run daily CI on PRs to release a branch
查询指定时间段的⽇志
这个可选参数⽐较多,⽐如--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>
Date: Tue Mar 31 17:56:04 2020 +0200
Redis 6.0-RC3.
commit ef1b1f01a84e969ea368e7fdbaf0d10615743269
Author: antirez <antirez@gmail>
Date: Tue Mar 31 17:41:23 2020 +0200
cast raxSize() to avoid warning with format spec.
恰好逮到了原作者的提交~
查询1年前的提交记录
$ git log -2 --ar.ago
commit 86aade9a024c3582665903d0cc0c5692c6677cfd
Merge: 89ad0ca56 3bfcae247
Author: Salvatore Sanfilippo <antirez@gmail>
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>
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.
查询包含指定描述内容的提交记录
这⾥⽤可以使⽤--grep参数,可以过滤出包含指定内容的提交记录,这⾥指的是在 commit 描述中筛选符合条件的提交,⽐如查提交描述中包含client 的提交记录:
$ git log -2 --grep='client'
commit 0f75036c07db48dfcf605e090216a4447edc38fc
Author: Wen Hui <wen.hui.ware@gmail>
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>
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默认查的是当前分⽀的提交记录,如果想查询其他分⽀的记录直接在命令后⾯加上分⽀名字就⾏,⽐如查询arm分⽀上的提交记录:$ git log -2 arm
commit 7329cc39818a05c168e7d1e791afb03c089f1933 (origin/arm, arm)
Author: Salvatore Sanfilippo <antirez@gmail>
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>
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、标签等拥有⼏乎相同的含义,所以在很多场景下可以扩展互换,⽐如git log后⾯加上分⽀名就可以查询指定分⽀的提交记录,如果加上 commit 就会查询这个 commit 之前的提交记录,如果加上标签,就可以查询这个标签之前的提交记录,⽐如我们加⼀个commit 试试:
$ git log -2 7329cc39818a05c168e7d1e791afb03c089f1933
commit 7329cc39818a05c168e7d1e791afb03c089f1933 (origin/arm, arm)
Author: Salvatore Sanfilippo <antirez@gmail>
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>
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.mit2,需要注意的是这样查询出来的提交记录列表中不包含 commit1,其实列举出的就是 commit1 之后⼜做了哪些修改提交。
$ git log e15528bf1da1f1232fd08801ad382c915be94662..7bf665f125a4771db095c83a7ad6ed46692cd314
commit 7bf665f125a4771db095c83a7ad6ed46692cd314 (HEAD -> 6.0, tag: 6.0.6, origin/6.0)
Author: Oran Agra <oran@redislabs>
Date: Sun Jul 19 14:00:20 2020 +0300
Redis 6.0.6.
commit a5696bdf4f2687ab45f633ccb7cdc4ee9c2f957d
Author: Oran Agra <oran@redislabs>
Date: Sun Jul 19 15:33:21 2020 +0300
Run daily CI on PRs to release a branch
这个特性有⼀个应⽤就是在 merge 分⽀之前可以查询究竟会 merge 哪些记录,常见的⽤法⽐如git log feature..dev就是列举出 feature 分⽀合并到 dev 分⽀将要合并的提交记录有哪些。
$ git log 6.0..unstable
commit 324e22accf457edc996971bc97f5474349cd7c4c (unstable)
Author: antirez <antirez@gmail>
Date: Fri Dec 20 12:29:02 2019 +0100
Fix ip and missing mode in RM_GetClusterNodeInfo().
查询指定⽂件的提交记录
查询指定⽂件的提交记录⼀般直接在git log命令后⾯跟上⽂件名就可以,但是为了避免和分⽀名产⽣分歧,所以通常在⽂件名前⾯加上--⽤来区分,--这个标识符就是⽤来防⽌混淆的,放在--前⾯的是分⽀名,放在后⾯的是⽂件名,相同的作⽤不仅仅在git log命令中,在其他命令⽐如git checkout中也有相同的⽤法。
$ git log -2 -- f
commit 7a536c2912be1fd9f62b26b7022a00644c88ef8b
Author: Yossi Gottlieb <ply.github>
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>
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除了可以筛选提交记录,还可以控制显⽰格式,普通不加参数,会显⽰作者、邮件、提交描述信息、⽇期等信息。
通过添
$ git log -1
commit 7bf665f125a4771db095c83a7ad6ed46692cd314 (HEAD -> 6.0, tag: 6.0.6, origin/6.0)
Author: Oran Agra <oran@redislabs>
Date: Sun Jul 19 14:00:20 2020 +0300
Redis 6.0.6.
加参数可以控制和改变显⽰格式,下⾯来看⼏条常见的
显⽰单⾏信息
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)
显⽰每条记录中⽂件修改的具体⾏数和⾏体统计
使⽤--stat参数就可以显⽰每条记录的中修改⽂件的具体⾏数和⾏数统计
$ git log -2 --stat
commit 7bf665f125a4771db095c83a7ad6ed46692cd314 (HEAD -> 6.0, tag: 6.0.6, origin/6.0)
Author: Oran Agra <oran@redislabs>
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>
Date: Sun Jul 19 15:33:21 2020 +0300
Run daily CI on PRs to release a branch
.github/l | 6 ++++--
1 file changed, 4 insertions(+),
2 deletions(-)
显⽰每条提交记录中⽂件的增加⾏数和删除⾏数
使⽤--numstat参数会把--stat参数中合并显⽰的修改⾏数拆分成增加⾏数和删除⾏数
$ git log -2 --numstat
git常用指令commit 7bf665f125a4771db095c83a7ad6ed46692cd314 (HEAD -> 6.0, tag: 6.0.6, origin/6.0)
Author: Oran Agra <oran@redislabs>
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>
Date: Sun Jul 19 15:33:21 2020 +0300
Run daily CI on PRs to release a branch
4 2 .github/l
依次罗列各提交记录中每个⽂件中增加的⾏数和删除的⾏数
要想达到这个⽬的需要⽤到--prety=tformat: --numstat参数,这样的显⽰格式便于统计
$ git log -2 --pretty=tformat: --numstat
245 0 00-RELEASENOTES
2 2 src/help.h
1 1 src/version.h
4 2 .github/l
统计修改的代码⾏数
有了前⾯的铺垫,想要统⼀修改的⾏数就容易了,只要配合awk⼯具就可以完成统计了
$ $ 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
还可以统计两个分⽀相差的代码⾏数
$ 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
到这⾥可以发现前⾯的知识都可以⽤上,前⾯筛选的参数变了,得到的结果就变了,我们可以根据需求来调整不同的参数
总结
git log就是⼀部代码库记录的“史书”,对于曾经所做的修改可以做到有史可查
git log的选项参数可以分为筛选参数和格式参数,筛选参数可以选择记录范围,格式参数可以控制显⽰样式
统计就是按照⼀定规律来将数据进⾏汇总,在进⾏汇总前需要将数据进⾏整理,这样汇总的⼯作才会更加顺利
到此这篇关于git log根据特定条件查询⽇志并统计修改的代码⾏数的⽂章就介绍到这了,更多相关git log统计修改代码⾏数内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。