Git:显示两次提交之间的文件总大小差异?

Mat*_*ens 73 git filesize git-diff

是否可以显示两次提交之间的文件总大小差异?就像是:

$ git file-size-diff 7f3219 bad418 # I wish this worked :)
-1234 bytes
Run Code Online (Sandbox Code Playgroud)

我试过了:

$ git diff --patch-with-stat
Run Code Online (Sandbox Code Playgroud)

这显示了diff中每个二进制文件的文件大小差异 - 但不是文本文件,而不是文件总大小差异.

有任何想法吗?

pat*_*yts 82

git cat-file -s将输出git中对象的大小(以字节为单位).git diff-tree可以告诉你一棵树和另一棵树之间的差异.

将它们放在一个名为git-file-size-diff位于PATH某处的脚本中将使您能够调用git file-size-diff <tree-ish> <tree-ish>.我们可以尝试以下内容:

#!/bin/bash
USAGE='[--cached] [<rev-list-options>...]

Show file size changes between two commits or the index and a commit.'

. "$(git --exec-path)/git-sh-setup"
args=$(git rev-parse --sq "$@")
[ -n "$args" ] || usage
cmd="diff-tree -r"
[[ $args =~ "--cached" ]] && cmd="diff-index"
eval "git $cmd $args" | {
  total=0
  while read A B C D M P
  do
    case $M in
      M) bytes=$(( $(git cat-file -s $D) - $(git cat-file -s $C) )) ;;
      A) bytes=$(git cat-file -s $D) ;;
      D) bytes=-$(git cat-file -s $C) ;;
      *)
        echo >&2 warning: unhandled mode $M in \"$A $B $C $D $M $P\"
        continue
        ;;
    esac
    total=$(( $total + $bytes ))
    printf '%d\t%s\n' $bytes "$P"
  done
  echo total $total
}
Run Code Online (Sandbox Code Playgroud)

在使用中,它看起来像如下:

$ git file-size-diff HEAD~850..HEAD~845
-234   Documentation/RelNotes/1.7.7.txt
112    Documentation/git.txt
-4     GIT-VERSION-GEN
43     builtin/grep.c
42     diff-lib.c
594    git-rebase--interactive.sh
381    t/t3404-rebase-interactive.sh
114    t/test-lib.sh
743    tree-walk.c
28     tree-walk.h
67     unpack-trees.c
28     unpack-trees.h
total 1914
Run Code Online (Sandbox Code Playgroud)

通过使用git-rev-parse它应该接受指定提交范围的所有常用方法.

编辑:更新以记录累计总数.请注意,bash在子shell中运行while读取,因此额外的花括号可以避免在子shell退出时丢失总数.

编辑:通过使用--cached参数调用git diff-index代替而添加了对索引与另一个树的比较的支持git diff-tree.例如:

$ git file-size-diff --cached master
-570    Makefile
-134    git-gui.sh
-1  lib/browser.tcl
931 lib/commit.tcl
18  lib/index.tcl
total 244
Run Code Online (Sandbox Code Playgroud)

  • 如果在不是git存储库的目录中运行此命令,它会执行基本检查,例如生成合理的消息.它还可以帮助抽象出一些平台差异.虽然大多是习惯.编写git脚本时 - 首先引入git-sh-setup文件. (3认同)
  • @mr5 HEAD~850 是 HEAD 之前的 850 次提交。这只是提交的另一种表示法,是的,您可以使用特定的提交 ID 或标签或任何可以解析为提交的内容。该脚本使用 `git rev-parse`,因此请参阅 git-rev-parse 文档中的手册部分“指定修订版”以获取完整详细信息。(https://git-scm.com/docs/git-rev-parse) (2认同)

Ada*_*ruk 20

你可以管出输出

git show some-ref:some-path-to-file | wc -c
git show some-other-ref:some-path-to-file | wc -c
Run Code Online (Sandbox Code Playgroud)

并比较2个数字.

  • +1这非常适合快速检查版本之间文件的大小差异.但是如何使用它来获得两次提交之间的总文件差异?我想看看两个引用之间在项目范围内添加/删除了多少字节. (6认同)
  • 你可以跳过`| wc -c` 如果你使用 `cat-file -s` 而不是 `show` (3认同)