'File size difference between two commits in git
So, I know it's possible to get the file size difference outputted as a positive or negative delta value between two commits using git diff command.
Although I am interested in seeing the negative and the positive difference of the files in the commits. Because let's say that in one file 400 bytes worth of code were removed and 405 added, then the delta will be outputted as positive 5 and will not reflect the size of change that I'm interested in.
Instead of: (+)5 filename
I want: -400 (+)405 filename
Anyone has a clue how to solve this? Very grateful for any guidance.
Solution 1:[1]
This doesn't fit as a comment, but is mostly intended as one.
Suppose that the output from git diff
is:
diff --git a/somefile b/somefile
index 1bb1e3a..f7c29ea 100644
--- a/somefile
+++ b/somefile
@@ -1,3 +1,3 @@
this file has
-several lines
+a few lines
of text.
One line is changed. That one line went from 14 bytes (including final newline) to twelve bytes (including newline) long. But lines\n
matched on the one changed line, so in fact, we really only removed one word, several
: 7 bytes; and added two words, a few
: 5 bytes.
What number(s) would you like for this diff? Is it -14 +12, or is it -2, or is it -7 +5 (which is of course also -2)?
In any case, to get the result you want, you will need to extract the two commits in question (or at least all the files in those commits that have changed) and compute the numbers yourself.
Solution 2:[2]
We can produce exactly what your asking for, by extracting the lines of each modified file and then counting them; by using a combination of 'git diff' and 'uniq -c', with some other typical linux cli commands.
## Assuming the currently checked out branch is used for compare
## sh, bash, or ksh
git diff --name-status branchB | while read flag filename; do
var_counts=$( git diff branchB -- $filename | grep -e '^[-+]' | sed 's|\([-+]\).*|\1|g' | sort | uniq -c | sed 's| ||g' )
echo $filename $var_counts;
done
Produces the output of:
name_of_file.xxx 15- 10+
another_file.zzz 12- 13+
Explanation / break down of the commands:
# produces a list of modified file
git diff --name-status branchB
# OUTPUT:
M name_of_file.xxx
M another_file.zzz
# walk the list of modified files, putting the results into 2 variables, $flag and $filename.
| while read flag filename; do
done
# obtain modified lines from the modified file, having '-' or '+' as the first character
# git diff branchB -- $filename | grep -e '^[-+]'
# OUTPUT:
- asdf asdf asdf asdf
+ qwert qwert qwert qwert
# extract out the first character from each modified line, the '+' and '-'.
| sed 's|\([-+]\).*|\1|g'
# Count the number of '+' and '-' characters:
| sort | uniq -c
# OUTPUT:
15 -
10 -
# Remove the whitespace from the 'uniq -c', for any additional parsing:
| sed 's| ||g'
# OUTPUT:
15- 10+
# produce the final output
echo $filename $var_count
# FINAL OUTPUT:
name_of_file.xxx 15- 10+
another_file.zzz 12- 13+
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | torek |
Solution 2 | J Jorgenson |