首页 > 解决方案 > 即使对于零值,如何强制 diffstat 始终显示插入、删除和修改的数量?

问题描述

我正在diffstat合并以查看我做了多少插入、删除修改,如下所示:

git show 526162eed6 --first-parent --unified=0 | diffstat -m

这列出了所有文件并在最后给出了摘要:

a/b/c | 10 ++++++++++
a/b/d |  5 +++++
...
10 files changed, 50 insertions(+), 10 modification(!)

但是,即使它们为零,我也希望查看所有值:

10 files changed, 50 insertions(+), 0 deletions(-), 10 modifications(!)

我怎样才能做到这一点?我目前的解决方法是通过输出 CSV... | diffstat -mt并通过手动添加列awk。有没有更简单的方法?

标签: gitdiff

解决方案


我找不到做你想做的事的选项。diffstat是一种产生人类可读输出的工具,不适合机器使用。

如果您绝对必须解析/按摩其输出,则可以使用非常肮脏的 hack(不推荐,随时可能中断)。定义外壳函数:

stats() {
  read -r stat
  echo "$stat" | grep -o '[0-9]\+ file' | grep -o '[0-9]\+' || echo '0'
  echo 'files changed,' # does not match original output for 1 file
  echo "$stat" | grep -o '[0-9]\+ ins' | grep -o '[0-9]\+' || echo '0'
  echo 'insertions(+),'
  echo "$stat" | grep -o '[0-9]\+ del' | grep -o '[0-9]\+' || echo '0'
  echo 'deletions(-),'
  echo "$stat" | grep -o '[0-9]\+ mod' | grep -o '[0-9]\+' || echo '0'
  echo 'modifications(!)'
}
diffstats() {
  diffstat -sm | stats | paste -sd ' '
}

接着:

git diff | diffstats

推荐阅读