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

问题描述 投票:0回答:6

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

$ git file-size-diff 7f3219 bad418 # I wish this worked :)
-1234 bytes

我已经尝试过:

$ git diff --patch-with-stat

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

有什么想法吗?

git filesize git-diff
6个回答
109
投票

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

将其放入位于 PATH 上某个位置的名为

git-file-size-diff
的脚本中,您将能够调用
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.'

SUBDIRECTORY_OK=1
. "$(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
}

在使用中,如下所示:

$ 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

通过使用

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

编辑:更新以记录累计总数。请注意,bash 在子 shell 中运行 while read,因此需要附加花括号以避免在子 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

编辑:将脚本标记为能够在 git 存储库的子目录中运行。


28
投票

您可以通过管道输出

git show some-ref:some-path-to-file | wc -c
git show some-other-ref:some-path-to-file | wc -c

并比较这两个数字。


4
投票

扩展matthiaskrgr的答案https://github.com/matthiaskrgr/gitdiffbinstat可以像其他脚本一样使用:

gitdiffbinstat.sh HEAD..HEAD~4

我觉得它真的很好用,比这里发布的其他任何东西都要快得多。示例输出:

$ gitdiffbinstat.sh HEAD~6..HEAD~7
 HEAD~6..HEAD~7
 704a8b56161d8c69bfaf0c3e6be27a68f27453a6..40a8563d082143d81e622c675de1ea46db706f22
 Recursively getting stat for path "./c/data/gitrepo" from repo root......
 105 files changed in total
  3 text files changed, 16 insertions(+), 16 deletions(-) => [±0 lines]
  102 binary files changed 40374331 b (38 Mb) -> 39000258 b (37 Mb) => [-1374073 b (-1 Mb)]
   0 binary files added, 3 binary files removed, 99 binary files modified => [-3 files]
    0 b  added in new files, 777588 b (759 kb) removed => [-777588 b (-759 kb)]
    file modifications: 39596743 b (37 Mb) -> 39000258 b (37 Mb) => [-596485 b (-582 kb)]
    / ==>  [-1374073 b (-1 Mb)]

输出目录与 ./c/data... 很时髦,因为 /c 实际上是文件系统根。


3
投票

我制作了一个 bash 脚本来根据实际文件/内容大小来比较分支/提交等。 它可以在 https://github.com/matthiaskrgr/gitdiffbinstat 找到,并且还可以检测文件重命名。


2
投票

对脚本的评论:git-file-size-diff,由 patthoyts 建议。该脚本非常有用,但是我发现了两个问题:

  1. 当有人更改文件的权限时,git 在 case 语句中返回另一种类型:

    T) echo >&2 "Skipping change of type"
    continue ;;
    
  2. 如果 sha-1 值不再存在(由于某种原因),脚本就会崩溃。在获取文件大小之前,您需要验证 sha:

    $(git cat-file -e $D)
     if [ "$?" = 1 ]; then continue; fi

完整的案例陈述将如下所示:

case $M in
      M) $(git cat-file -e $D)
         if [ "$?" = 1 ]; then continue; fi
         $(git cat-file -e $C)
         if [ "$?" = 1 ]; then continue; fi
         bytes=$(( $(git cat-file -s $D) - $(git cat-file -s $C) )) ;;
      A) $(git cat-file -e $D)
         if [ "$?" = 1 ]; then continue; fi
         bytes=$(git cat-file -s $D) ;;
      D) $(git cat-file -e $C)
         if [ "$?" = 1 ]; then continue; fi
         bytes=-$(git cat-file -s $C) ;;
      T) echo >&2 "Skipping change of type"
         continue ;;
      *)
        echo >&2 warning: unhandled mode $M in \"$A $B $C $D $M $P\"
        continue
        ;;
    esac

0
投票

如果您对大概的答案感到满意,您可以使用以下命令获取餐巾纸背面的数据大小:

git archive <COMMIT> | wc -c

报告的大小将是提交中所有数据加上一些 tar 元数据的字节数。由于

tar
本身(
git archive
的默认值)不进行压缩,因此报告的数字在某种程度上具有可比性。

如果您的目的是查找添加了 1GB 日志文件的一次提交,那么这种方法就完全足够了。

© www.soinside.com 2019 - 2024. All rights reserved.