19

I have a big horrible pile of code and I am setting it up in version control.

I would like a command I can run on Linux to give me the total size of the files that would be committed and pushed if I ran git add -A && git commit -am 'initial commit'

The total size is needed, also a break down by folder would be handy.

I will then use this to build up my ignores so that I can get the repo to a realistic size before I push it up

6 Answers 6

14

I think I have answered my own question:

for f in `git status --porcelain | sed 's#^...##'`; do du -cs $f | head -n 1; done | sort -nr;  echo "TOTAL:"; du -cs .

However I'm open to any better ideas or useful tricks. My current output is 13GB :)


The above command is basically there, it gives me the total line by line from git status but doesn't give me the total sum. I'm currently getting the total of all files at the end which is not correct. I tried some use of bc but couldn't get it to work

0
7

I adapted the answer of edmondscommerce by adding a simple awk statement which sums the output of the for loop and prints the sum (divided by 1024*1024 to convert to Mb)

for f in `git status --porcelain | sed 's#^...##'`; do du -cs $f | head -n 1; done | sort -nr  | awk ' {tot = tot+$1; print } END{ printf("%.2fMb\n",tot/(1024*1024)) }' 

Note that --porcelain prints pathnames relative to the root of the git repos. So, if you do this in a subdirectory the du statement will not be able to find the files..

(whoppa; my first answer in SoF, may the force be with it)

0
3

I've used a modified version of this, because I had files with spaces in them which made it crash. I was also unsure about the size calculations and removed a useless head:

git status --porcelain | sed 's/^...//;s/^"//;s/"$//' | while read path; do
    du -bs "$path" ;
done | sort -n | awk ' {tot = tot+$1; print } END { printf("%.2fMB\n",tot/(1024*1024)) }'

I prefer to use while as it's slightly safer than for: it can still do nasty things with files that have newlines in them so I wish there was a to pass null-separate files yet still be able to grep for the status, but I couldn't find a nice way for that.

3

Since version 2.11, git provides a handy "count-objects" command :

git count-objects -H

If this is not enough, I would recommend git-sizer from github : https://github.com/github/git-sizer

git-sizer --verbose

Detailed usage here : https://github.com/github/git-sizer/#usage

0

Since you're just adding everything, I don't see any reason to go via Git. Just use the ordinary Unix tools: du, find, &c.

1
  • 2
    The reason is that I will be ignoring things and then running this command to see what I have brought it down to Commented Feb 17, 2014 at 15:44
0

I wanted a smarter git add also but for more incremental reasons. I came up with this fish function:

function gitadd
    set size (git status --short | grep -v '^ D' | awk '{print substr($0,4)}' | xargs -n 1 du -b | awk '{print $1}' | sum)
    if test $size -lt 1048576 # 1MiB
        git add .
    else
        git status --short | grep -v '^ D' | awk '{print substr($0,4)}' | xargs -n 1 du -h
        git diff --stat --relative
        return 1
    end
end

function sum
    set accumulator 0
    while read -l line
        set accumulator (math $accumulator+$line)
    end
    echo $accumulator
end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.