'Git repository size and git push size

We have set a quota limit for our Git repository (which will be on cloud as a remote repository). We have to restrict the size of the repository within this limit.

For this, I am trying to get the size of the 'git push' in the pre-receive hook of the remote repository. If I get the push's size, I will sum up the present git repo's size with this push's size to find how much will be the total repository size after the push. Then, I can restrict the push, if the total size exceeds the quota or accept the push, if the size does not exceed the quota.

Basically, I need to calculate both the git remote repository's size and also the git push's size in a pre hook.



Solution 1:[1]

The surest way to implement this would be to:

  • push to a local repo
  • check the size of that local repo after the push
  • if the size is compliant with your constraints, then push to the remote repo on the could.

Solution 2:[2]

Here's a script that can be used as the pre-receive hook for the repository on the server and that restricts the current size of the repository + the incoming push to a maximum size:

    set -e
    GIT=/usr/bin/git
    MAXSIZE=1024 # size in KB
    EXIT=0
    size=$(du -s | cut -f 1)
    human_size=$(du -sh | cut -f 1)
    if [ ! -z "${size}" ]; then
        if [ ${size} -gt ${MAXSIZE} ]; then
            echo "Push will make repository size ${human_size} which is more than ${MAXSIZE}KB, rejecting"
            exit 1;
        fi
    else
        echo "could not determine repository size, rejecting push"
        exit 1;
    fi

Note that this works to calculate the repository size plus the incoming push because in the pre-receive hook the incoming data is stored in a quarantine folder within the repository folder, as explained in the documentation:

When receive-pack takes in objects, they are placed into a temporary "quarantine" directory within the $GIT_DIR/objects directory and migrated into the main object store only after the pre-receive hook has completed. If the push fails before then, the temporary directory is removed entirely.

Since the working directory for the hook is the repository itself, it is enough to run du -s.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 VonC
Solution 2 Amos Joshua