Search code examples
git

How to prevent a Git repository growing above a maximum size?


There is a repository where you need to control its size and in case of exceeding the limit - block any changes. How to implement this?


Solution

  • .git/hooks/pre-receive

    #!/bin/bash
    
    # size limit 2(GB)
    sizelimit_gb=2
    
    reposize_kb=`git count-objects -v | grep 'size-pack' | sed  's/.*\(size-pack:\).//'`
    let reposize_b=$reposize_kb*1024
    let sizelimit_b=$sizelimit_gb*1024*1024*1024
    
    if [ $reposize_b -gt $sizelimit_b ]; then
        echo "Error: repository size > $sizelimit_gb GB"
        exit 1
    #else
    #    echo "<= $sizelimit_gb GB"
    fi
    
    exit 0
    

    Above script must be saved as .git/hooks/pre-receive in server, with execution permissions enabled (chmod +x .git/hooks/pre-receive).