Tuesday, June 10, 2014

Git - see what added so much space to the repo

Here's a good way to see what added space to the git repo recently.
$ git log --all --pretty='%h' --since='{2014-06-06}' | xargs -n1 sh size.sh
total: 240 0.0MB for 6df23f5
total: 88709989 84.6MB for f66712d
total: 26473 0.0MB for c397131
total: 462 0.0MB for 7c78e1c

And here is the size.sh file:
#!/bin/bash

if [[ -z "$1" ]] || [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]] ; then
    echo "Get change of size introduced by individual commits"
    echo "Usage : $(basename "$0") <commit> ..."
    echo "Example: git log --all --pretty='%h' --since='{2014-06-06}' | xargs bash size.sh"
    exit 1
fi

while [[ "$1" ]]; do 
  commit="$1"
  shift
  total=0
  git diff-tree -r $commit^1 $commit | {
    while read -r sperms dperms oldobj obj flag filename; do
      case $flag in
        A) bytes=`git cat-file -s $obj`
           ;;
        D) bytes=-`git cat-file -s $oldobj`
           ;;
        M) bytes=$(( `git cat-file -s $obj` - `git cat-file -s $oldobj` ))
           ;;
        *)
          echo "ERROR: unknown flag \"$flag\" in in commit \"$commit\": $sperms $dperms $oldobj $obj $flag $filename"
          continue
          ;;
      esac
      total=$(( total + bytes ))
    done
    total_mb=`echo $total | awk '{printf "%.1f",$1/1024/1024}'`
    echo "total: $total ${total_mb}MB for $commit"
  }
done

credits to for the idea:
http://stackoverflow.com/a/10847242/520567