如何告诉 `git log --[num|short]stat` 计算空行数

发布于 2024-11-30 16:52:20 字数 979 浏览 0 评论 0原文

一般问题

在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

辞旧 2024-12-07 16:52:20

上下文

对于同一更改,有多个不同(有效)的补丁。主要区别在于上下文行的使用。统一差异通常在每次更改之前和之后使用三行上下文。在内部 git (有时)使用零行上下文,这可能会导致不同的更改行。

第一种解决方案:外部工具

正如 @karl-bielefeldt 已经描述的那样,可以将 git show 的结果通过管道传输到 grep -Pc '^\+(?!\+)' 或grep -Pc '^-(?!-)'。有一个工具 diffstat 可以做到这一点:

$git show c21ad76 -- contrib/hadoop-store-builder/src/java/voldemort/store/readonly/mr/HadoopStoreBuilderReducer.java | diffstat
 HadoopStoreBuilderReducer.java |   57 +++++++++++++++++++++--------------------
 1 file changed, 30 insertions(+), 27 deletions(-)

第二种解决方案:为补丁使用不同的上下文

可以配置 git show 的输出补丁。使用选项“-Ux”可以指定 xa 上下文。

$git show -U0 c21ad76 -- contrib/hadoop-store-builder/src/java/voldemort/store/readonly/mr/HadoopStoreBuilderReducer.java | diffstat
 HadoopStoreBuilderReducer.java |   47 +++++++++++++++++++++--------------------
 1 file changed, 25 insertions(+), 22 deletions(-)

这与内部 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 into grep -Pc '^\+(?!\+)' or grep -Pc '^-(?!-)'. There is the tool diffstat which does exactly that:

$git show c21ad76 -- contrib/hadoop-store-builder/src/java/voldemort/store/readonly/mr/HadoopStoreBuilderReducer.java | diffstat
 HadoopStoreBuilderReducer.java |   57 +++++++++++++++++++++--------------------
 1 file changed, 30 insertions(+), 27 deletions(-)

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.

$git show -U0 c21ad76 -- contrib/hadoop-store-builder/src/java/voldemort/store/readonly/mr/HadoopStoreBuilderReducer.java | diffstat
 HadoopStoreBuilderReducer.java |   47 +++++++++++++++++++++--------------------
 1 file changed, 25 insertions(+), 22 deletions(-)

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.

二手情话 2024-12-07 16:52:20

我数了数,删除了 27 个,添加了 30 个。我不知道是否有办法使用内部 git 来做到这一点,但这对我有用:

git show c21ad76 -- contrib/hadoop-store-builder/src/java/voldemort/store/readonly/mr/HadoopStoreBuilderReducer.java | grep -Pc '^\+(?!\+)'
git show c21ad76 -- contrib/hadoop-store-builder/src/java/voldemort/store/readonly/mr/HadoopStoreBuilderReducer.java | grep -Pc '^-(?!-)'

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:

git show c21ad76 -- contrib/hadoop-store-builder/src/java/voldemort/store/readonly/mr/HadoopStoreBuilderReducer.java | grep -Pc '^\+(?!\+)'
git show c21ad76 -- contrib/hadoop-store-builder/src/java/voldemort/store/readonly/mr/HadoopStoreBuilderReducer.java | grep -Pc '^-(?!-)'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文