Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: show total file size difference between two commits?

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?

like image 451
Mathias Bynens Avatar asked Jun 01 '12 05:06

Mathias Bynens


People also ask

How do I find the difference between two commits in git?

To see the changes between two commits, you can use git diff ID1.. ID2 , where ID1 and ID2 identify the two commits you're interested in, and the connector .. is a pair of dots. For example, git diff abc123.. def456 shows the differences between the commits abc123 and def456 , while git diff HEAD~1..

How can you tell the difference between commits?

The git diff command displays the differences between files in two commits or between a commit and your current repository. You can see what text has been added to, removed from, and changed in a file. By default, the git diff command displays any uncommitted changes to your repository.

How do I see file size in git?

You can use either git ls-tree -r -l <revision> <path> to get the blob size at given revision, e.g. The blob size in this example is '16067'.

How to show diff between commits in Git?

How to show diff between commits in Git? To show the difference between commits, you use git diff. There are two ways of finding the differences: Using HEAD pointer. Using commit-SHAs.

How do I compare changes between two Git branches?

You can use HEAD to compare it with the latest commit, or a branch name to compare with the tip of a different branch. git diff [<options>] [--merge-base] <commit> <commit> [--] [<path>…. ] This is to view the changes between two arbitrary <commit>.

How do I view changes in Git git diff?

git diff [<options>] <commit> [--] [<path>… ] This form is to view the changes you have in your working tree relative to the named <commit>. You can use HEAD to compare it with the latest commit, or a branch name to compare with the tip of a different branch.

How do I see the version of a file in Git?

To show the difference between some version of a file in a given commit and the local HEAD version you can specify the commit you want to compare against: git diff 27fa75e myfile.txt. Or if you want to see the version between two separate commits: git diff 27fa75e ada9b57 myfile.txt.


1 Answers

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 
like image 196
patthoyts Avatar answered Oct 14 '22 18:10

patthoyts