dimid
dimid

Reputation: 7659

Get file size of a git commit without a tmp file

I've answered this question, suggesting using git hooks, but frankly, I'M not fully satisfied with the second part, since creating a temporary file just to get the size of the commit looks redundant and ugly. Is there a way to get the size directly? Assume the standard git implementation.

As suggested by @Cyrus, git diff --cached | wc -c is a good approximation, but it's encoding dependent.

I also tried process substitution ls -l <(git diff --cached) | awk '{print $5}' but that returns 0.

Edit: Based on @torek's answer I came up with these two solutions:

git ls-files --stage | cut -d' ' -f2 | xargs -L 1 git cat-file -s | awk '{s+=$1} END {print s}'

git diff --cached | awk -F. '/index/ {print $3}' | xargs -L 1 git cat-file -s | awk '{s+=$1} END {print s}'

The latter also handles new files.

Upvotes: 3

Views: 115

Answers (2)

torek
torek

Reputation: 489818

To get the size (in bytes) of a git object, use git cat-file -s revspec. For instance:

$ git cat-file -s HEAD:Makefile
950

Beware that this will show you the object size of a tree:

$ git cat-file -t HEAD:
tree
$ git cat-file -s HEAD:
546

For a large collection of IDs you might want to use git cat-file --batch-check (which, since git 1.8.4, now takes a format argument to select what information to print; see the documentation).

Upvotes: 2

user3159253
user3159253

Reputation: 17455

I think that git diff --cached --numstat is basically the main source of the information. For me it's not completely clear what restrictions you'd like to put on a commit but to summarize number of insertions and deletions you could use something like this:

git diff --numstat --cached | awk '
   BEGIN { insertions=0; deletions=0; }
   { insertions+=$1; deletions+=$2; }
   END { printf("inserttions=%d, deletions=%d\n", insertions, deletions); }'

Upvotes: 1

Related Questions