简体   繁体   中英

How to get the folder size for the whole git history

I have some folder in git with different files (little images for example). Every change/insertion of images are stored in git history forever. If that folder grows too much, for example it will be over 50mb, it can make some problems to all users of that git repo.

For some reasons I cant use git-lfs because it will make huge problems.

So the question is: how to count size of all files in selected directory in all commits in the history? Or some ideas how to achieve it?

As @Mehrdad mentions in the answer for Find size of git repo , the following command can be used to find the size of the git repo :

git rev-list --objects --all | git cat-file --batch-check="%(objectsize) %(rest)" | cut -d" " -f1 | paste -s -d + - | bc

rev-list takes path as an argument. So, to find the size of a directory (ex : foo/bar ) in the git repo ( across all commits ), you can run :

git rev-list --objects --all -- foo/bar | git cat-file --batch-check="%(objectsize) %(rest)" | cut -d" " -f1 | paste -s -d + - | bc 

This will however return size in bytes. You could pipe the output to numfmt util for more human readable format.

git rev-list --objects --all -- <relative-path-to-directory> | git cat-file --batch-check="%(objectsize) %(rest)" | cut -d" " -f1 | paste -s -d + - | bc | numfmt --to=si

Note: --all includes all revisions, you can specify a branch or tag name instead.

I got result using git log and git show

RESULT=0
OUTP=$(git log --pretty=tformat:%H -- path/to/data | while read hash; do
    git show --stat --name-only $hash | grep "^path/to/data" | while read filename; do
        if [ ! -z "$filename" ]; then
            git show "$hash:$filename" | grep "^size " | while read filesizeString; do
                filesize=${filesizeString#"size "}
                echo $filesize
            done
        fi
    done
done)

echo "FINISHED"

while IFS= read -r line; do
    RESULT=$((RESULT += line))
done <<< "$OUTP"
RESULT_MB=$((RESULT/1024/1024))
echo "Directory takes $RESULT_MB mb in git repository"

But I dont know why that way shows different size than @Saurabh P Bhandari's

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM