Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to protect against pushing large binary blobs in git?

I have a central git repository which myself and several collaborators regularly push and pull from. In the past I have committed a large binary blob by accident, which requires rebasing to fully delete and is a pain for everyone, so I'd like to protect against this happening in the future. Is it possible to setup a hook in the remote repository that will check the file size of files being pushed (whether they are being added new or an existing file is updated) and reject pushes with files above a threshold size, say 2MB?

Importantly I want existing files already greater than 2MB that are untouched to be tolerated (so a push shouldn't be rejected if a 2MB file is already in the repository, only if the push adds a 2MB file or grows an existing file to be 2MB). Also, I want the hook to execute on the remote side so I don't have to worry that clients don't have to have a hook setup.

Edit: Since a push can contain multiple commits, and even one commit with a large file gets it stuck in the repo, I want to protect against pushes that contain /any commit/ that grows to or adds a >=2MB file.

like image 963
Joseph Garvin Avatar asked Jul 18 '12 20:07

Joseph Garvin


1 Answers

It sounds like the pre-receive hook would be the correct place for this check. This hook executes on the server side of a push, and has access to enough information for you to implement a file size check.

This hook is invoked by git-receive-pack on the remote repository, which happens when a git push is done on a local repository. Just before starting to update refs on the remote repository, the pre-receive hook is invoked. Its exit status determines the success or failure of the update.

like image 185
Greg Hewgill Avatar answered Nov 15 '22 08:11

Greg Hewgill