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)
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个数字.