4 # gitdiffbinstat - gets a git diff --shortstat-like output for changed binary files
5 # Copyright (C) 2012 Matthias Krüger
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 1, or (at your option)
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
21 # in order to avoid conflicts when starting the script twice, get unix time + nanoseconds and
22 # append them to the diffstat file
24 timestamp
=`date +%s%N`
27 diffstat
=/tmp
/gitdiffstat.
$timestamp
29 # If no argument is given, print this how-to.
30 if [ -z "$obj" ] ; then
31 echo "Usage: \"gitdiffbinstat [<commit/branch/tag>]\""
32 echo "Make sure to be in a git repo!"
36 # Check if we are actually in a git repo
37 gitcheck
=`git tag |& grep -o "fatal:\ Not\ a\ git\ repository\ "`
38 if [ "$gitcheck" ] ; then
39 echo "fatal: Not a git repository!"
40 echo "Make sure to be in a git repo!"
45 curbranch
=`git branch | awk '/^\*\ /' | sed -e 's/\*\ //'`
46 # get current rev hash
47 curcommit
=`git rev-parse HEAD`
48 # print what we compare in git diff
49 echo " $obj -> $curbranch"
50 echo " $obj -> $curcommit"
53 # get the actuall diff we process
54 git
diff $obj --stat .
/ |
awk '/>/' > $diffstat
57 # generate our own git diff --shortstat
58 gst
=`git diff $obj --shortstat ./`
59 gstins
=`echo $gst | awk '{print $4}'`
60 gstdels
=`echo $gst | awk '{print $6}'`
61 gstchval
=`expr $gstins - $gstdels`
62 gstchfiles
=`echo $gst | awk '{print $1}'`
64 if [ "$gstchfiles" == 1 ] ; then
69 # say file/files correctly if we have only 0, one or several files
70 if [ "$gstchval" -gt 0 ] ; then
71 echo -e " $gstchfiles $gstfiles changed, \e[033;32m$gstins \e[0minsertions(\e[033;32m+\e[0m), \e[033;31m$gstdels\e[0m deletions(\e[033;31m-\e[0m) (\e[033;32m+$gstchval lines\e[0m)"
73 echo -e " $gstchfiles $gstfiles changed, \e[033;32m$gstins \e[0minsertions(\e[033;32m+\e[0m), \e[033;31m$gstdels\e[0m deletions(\e[033;31m-\e[0m) (\e[033;31m$gstchval lines\e[0m)"
76 # If there are no changes, exit
77 checksum
=`cat $diffstat | wc -l`
78 if [ $checksum == 0 ] ; then
79 echo " No binary files changed, exiting..."
86 # size (b) of all bin files of obj
87 old
=`awk '{ sum+=$4} END {print sum}' $diffstat`
88 # size (b) of all bin files of curbranch/curcommit
89 new
=`awk '{ sum+=$6} END {print sum}' $diffstat`
90 # amount of changed files
91 files
=`wc -l $diffstat | awk '{print $1}'`
96 # "Bin 0 -> x bytes" : file has been added
97 newfiles
=`awk '/Bin\ 0/' $diffstat | wc -l`
98 # "Bin x -> 0 bytes" : file has been deleted
99 delfiles
=`awk '/->\ 0\ bytes/' $diffstat | wc -l`
100 # all files - new files - deleted files = modified files
101 binchval
=`expr $newfiles - $delfiles`
102 chfiles
=`expr $files - $binchval`
104 if [ "$binchval" -gt 0 ] ; then
105 echo -e " Binary files: $chfiles modified, \e[033;32m$newfiles\e[0m added, \e[033;31m$delfiles\e[0m deleted. (\e[033;32m+$binchval files\e[0m)"
107 echo -e " Binary files: $chfiles modified, \e[033;32m$newfiles\e[0m added, \e[033;31m$delfiles\e[0m deleted. (\e[033;31m$binchval files\e[0m)"
111 # find out how many bytes bigger/smaller the repo got
112 changeval
=`calc -p "$new-$old"`
113 # print the result in different colors :)
115 # say file/files correctly if we have only 0, one or several files
116 if [ "$files" == 1 ] ; then
122 if [ "$changeval" -gt 0 ] ; then
123 echo -e " $files binary $somefiles changed, \e[033;31m$old bytes\e[0m -> \e[033;32m$new bytes\e[0m (\e[033;32m+$changeval bytes\e[0m)"
125 changeval1
=`echo "$changeval" | sed s/-//`
126 echo -e " $files binary $somefiles changed, \e[033;31m$old bytes\e[0m -> \e[033;32m$new bytes\e[0m (\e[033;31m-$changeval1 bytes\e[0m)"
129 # rather ugly foo to get nicer output numbers
132 ((newkilo
=$new/1024))
133 if [ ! "$newkilo" == 0 ] ; then
136 ((newmega
=$newkilo/1024))
137 if [ ! "$newmega" == 0 ] ; then
140 ((newgiga
=$newmega/1024))
141 if [ ! "$newgiga" == 0 ] ; then
151 ((oldkilo
=$old/1024))
152 if [ ! "$oldkilo" == 0 ] ; then
155 ((oldmega
=$oldkilo/1024))
156 if [ ! "$oldmega" == 0 ] ; then
159 ((oldgiga
=$oldmega/1024))
160 if [ ! "$oldgiga" == 0 ] ; then
168 if [ "$changeval" -gt 0 ] ; then
170 changevalval
=$changeval
171 ((changevalkilo
=$changeval/1024))
172 if [ ! "$changevalkilo" == 0 ] ; then
174 changevalval
=$changevalkilo
175 ((changevalmega
=$changevalkilo/1024))
176 if [ ! "$changevalmega" == 0 ] ; then
178 changevalval
=$changevalmega
179 ((changevalgiga
=$changevalmega/1024))
180 if [ ! "$changevalgiga" == 0 ] ; then
182 changevalval
=$changvalgiga
186 echo -e " $files binary $somefiles changed, \e[033;31m$oldval $oldunit\e[0m -> \e[033;32m$newval$newunit\e[0m (\e[033;32m+$changevalval$changevalunit\e[0m)"
189 changeval
=`echo "$changeval" | sed s/-//`
191 changevalval
=$changeval
192 ((changevalkilo
=$changeval/1024))
193 if [ ! "$changevalkilo" == 0 ] ; then
195 changevalval
=$changevalkilo
196 ((changevalmega
=$changevalkilo/1024))
197 if [ ! "$changevalmega" == 0 ] ; then
199 changevalval
=$changevalmega
200 ((changevalgiga
=$changevalmega/1024))
201 if [ ! "$changevalgiga" == 0 ] ; then
203 changevalval
=$changvalgiga
207 echo -e " $files binary $somefiles changed, \e[033;31m$oldval $oldunit\e[0m -> \e[033;32m$newval $newunit\e[0m (\e[033;31m-$changevalval $changevalunit\e[0m)"
210 # remove the diffstat, we don't need it anymore