如何告诉 `git log --[num|short]stat` 计算空行数
一般问题
在使用 git log --stat 时,如何告诉 git 它也应该计算 diff 中的空行?
代码示例
git clone https://github.com/voldemort/voldemort.git
cd voldemort
git log --numstat -n 1 c21ad76 contrib/hadoop-store-builder/src/java/voldemort/store/readonly/mr/HadoopStoreBuilderReducer.java
git show c21ad76 -- contrib/hadoop-store-builder/src/java/voldemort/store/readonly/mr/HadoopStoreBuilderReducer.java
更多详细信息
在给定的示例 git log --numstat
声明提交 c21ad76
中,该文件 HadoopStoreBuilderReducer.java
已添加 25 和删除了22行。如果您仔细查看该文件的 diff 输出 (git show
),您会发现实际上添加了 30 个 和删除了 25 个 > 行,这使得它的不同之处在于添加了 5 行和删除了 3 行。仔细观察,添加的行块中有 5 个空行,删除的行块中有 4 个空行。
此行为与 git log --shortstat 或 git log --stat 相同。
在我看来,块内的所有空行都不被git log --numstat
计数。
如何使用 git
计算每次提交添加和删除的行数包括空白行?
General question
How can I tell git
, that it should also count empty lines in a diff, when using git log --stat
?
Code example
git clone https://github.com/voldemort/voldemort.git
cd voldemort
git log --numstat -n 1 c21ad76 contrib/hadoop-store-builder/src/java/voldemort/store/readonly/mr/HadoopStoreBuilderReducer.java
git show c21ad76 -- contrib/hadoop-store-builder/src/java/voldemort/store/readonly/mr/HadoopStoreBuilderReducer.java
More details
In the given example git log --numstat
claims for commit c21ad76
, that file HadoopStoreBuilderReducer.java
has 25 added and 22 removed lines . If you have a closer look at the diff output (git show
) of that file you can see, that there are actually 30 added and 25 removed lines, which make it different by 5 added and 3 deleted lines. At an even closer look, there are 5 empty lines inside the added lines hunk and 4 empty lines in the deleted lines hunk.
This behavior is the same with git log --shortstat
or git log --stat
.
It appears to me, that all empty lines, which are inside an hunk are not counted by git log --numstat
.
How can I calculate with git
the number of added and removed lines per commit including blank lines?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
上下文
对于同一更改,有多个不同(有效)的补丁。主要区别在于上下文行的使用。统一差异通常在每次更改之前和之后使用三行上下文。在内部 git (有时)使用零行上下文,这可能会导致不同的更改行。
第一种解决方案:外部工具
正如 @karl-bielefeldt 已经描述的那样,可以将 git show 的结果通过管道传输到 grep -Pc '^\+(?!\+)' 或
grep -Pc '^-(?!-)'
。有一个工具diffstat
可以做到这一点:第二种解决方案:为补丁使用不同的上下文
可以配置 git show 的输出补丁。使用选项“-Ux”可以指定 xa 上下文。
这与内部 git log --numstat 匹配,因为它使用 0 上下文来计算统计数据。请注意,此行为将在 git 版本 1.7.7 中更改。 numstat 使用 3 行上下文。
Context
There are several different (valid) patches for the same change. The main difference is the use of context lines. Unified diff usually uses three lines of context before and after each change. Internally git (sometimes) uses zero lines of context which can result to different changed lines.
First solution: external tool
As @karl-bielefeldt already described, one can pipe the result of
git show
intogrep -Pc '^\+(?!\+)'
orgrep -Pc '^-(?!-)'
. There is the tooldiffstat
which does exactly that:Second solution: use different contexts for the patch
The output patch of git show can be configured. With the option "-Ux" for x a context can be specified.
This matches the internal
git log --numstat
because it uses a 0 context for calculating the stat. Note that this behaviour is about to change in git version 1.7.7. With that numstat uses 3 lines of context.我数了数,删除了 27 个,添加了 30 个。我不知道是否有办法使用内部 git 来做到这一点,但这对我有用:
I count 27 removed and 30 added. I don't know if there's a way to do it with internal git, but this worked for me: