Git:显示两个提交之间的文件总大小差异? [英] Git: show total file size difference between two commits?
问题描述
是否可以显示两个提交之间的文件总大小差异?例如:
$ git file-size-diff 7f3219 bad418#我希望这个工作:)
-1234字节
我试过了:
$ git diff --patch-with-stat
每个二进制文件在diff中的文件大小差异 - 但不是文本文件,而不是文件大小总和差异。
任何想法?
git cat-file -s
git中的一个对象的字节。 git diff-tree
可以告诉你一棵树与另一棵树之间的区别。
将这些放在一个名为 git-file-size-diff
的脚本中,它将位于PATH的某处能够调用 git file-size-diff< tree-ish> <树肥胖型>
。我们可以尝试如下所示:
$ b
#!/ bin / bash
USAGE ='[--cached] [< rev-list-options> ...]
在两次提交或索引与提交之间显示文件大小变化。'
。 $(git --exec-path)/ git-sh-setup
args = $(git rev-parse --sq$ @)
[-n$ args] | |用法
cmd =diff-tree -r
[[$ args =〜--cached]]&& cmd =diff-index
evalgit $ cmd $ args| {
total = 0
同时阅读ABCDMP
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警告:未处理模式$ M在\$ A $ B $ C $ D $ M $ P\
继续
;;
esac
total = $(($ total + $ bytes))
printf'%d\t%s\'$ 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内建/ 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中读取时运行,因此额外的大括号可以避免在子shell退出时丢失总数。
编辑:增加了对比索引的支持通过使用 - cached
参数来调用 git diff-index
而不是 git diff-tree
。例如:
$ git file-size-diff - 缓存大师
-570 Makefile
-134 git-gui.sh
-1 lib / browser.tcl
931 lib / commit.tcl
18 lib / index.tcl
total 244
Is it possible to show the total file size difference between two commits? Something like:
$ git file-size-diff 7f3219 bad418 # I wish this worked :)
-1234 bytes
I’ve tried:
$ git diff --patch-with-stat
And that shows the file size difference for each binary file in the diff — but not for text files, and not the total file size difference.
Any ideas?
git cat-file -s
will output the size in bytes of an object in git. git diff-tree
can tell you the differences between one tree and another.
Putting this together into a script called git-file-size-diff
located somewhere on your PATH will give you the ability to call git file-size-diff <tree-ish> <tree-ish>
. We can try something like the following:
#!/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
}
In use this looks like the following:
$ 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
By using git-rev-parse
it should accept all the usual ways of specifying commit ranges.
EDIT: updated to record the cumulative total. Note that bash runs the while read in a subshell, hence the additional curly braces to avoid losing the total when the subshell exits.
EDIT: added support for comparing the index against another tree-ish by using a --cached
argument to call git diff-index
instead of git diff-tree
. eg:
$ 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:显示两个提交之间的文件总大小差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!