Search code examples
gitgithooksgit-lfs

git lfs pre-receive hooks to prevent unwanted files in repository


Is it possible to write a server side pre-receive hook to prevent large or binary files to be added to the repository via commits?

Locally I can control git-lfs behaviour by adding file types to a .gitattribute file via:

    git lfs track "*.zip"

This would cause that if a new zip file is added, it will be managed by git-lfs and not committed to the git object store.

git-p4 even allows me file size thresholds like:

    largeFileCompressedThreshold = 1m
    largeFileThreshold = 50m

With multiple developers and developing environments it might happen, that one adds such a binary file without configuring git-lfs, hence such a file could be submitted to git directly. I would like a server side hook which refuses such a commit and gives a meaningful message, so the developer can easily identify the problem.

Ideally this receive hook would use existing git functionality to parse e.g. .gitattributes instead of manually parsing all files which are being committed in a script.


Solution

  • This could be accomplished using the sizelimit directive of the CheckFile plugin from the Git-Hooks framework. We use it and it works really well.