I wrote the git pre-commit hook below. It is supposed to reject commits that 
contain large files (e.g. accidental commits by inexperienced users, think of 
"git add .")

Anyway, I tried this under Linux, but the target platform is Windows. As per 
Git design the hook name *must* be "pre-commit" (no .py extension). How will 
Windows know that Python should be run? And (should it be relevant): how does 
Windows know which Python version to invoke? I read about custom shebangs with 
Pylauncher. Is that my only option? (see: 

In addition, I would really appreciate general feedback on the hook script 



albertjan@debian ~/Desktop/test_repo $ git config --global init.templatedir 
albertjan@debian ~/Desktop/test_repo $ cd ~/Desktop/git_template_dir
albertjan@debian ~/Desktop/git_template_dir $ cat hooks/pre-commit
#-*- mode: python -*-

"""Git pre-commit hook: reject large files"""

import os
import re
from subprocess import Popen, PIPE

def git_filesize_hook(megabytes_cutoff=5, verbose=False):
    """Git pre-commit hook: Return error if the maximum file size in the HEAD
    revision exceeds <megabytes_cutoff>, succes (0) otherwise. You can bypass 
    this hook by specifying '--no-verify' as an option in 'git commit'."""
    if verbose: print os.getcwd()
    cmd = "git ls-tree --full-tree -r -l HEAD"
    git = Popen(cmd, shell=True, stdout=PIPE, cwd=os.getcwd())
    get_size = lambda item: int(re.split(" +",
    sizes = map(get_size, git.stdout.readlines())
    cut_off_bytes = megabytes_cutoff * 2 ** 20
    if max(sizes) > cut_off_bytes:
        return ("ERROR: your commit contains at least one file "
                "that is larger than %d bytes" % cut_off_bytes)
    return 0

if __name__ == "__main__":
    sys.exit(git_filesize_hook(0.000001, True))

albertjan@debian ~/Desktop/git_template_dir $ cd -
albertjan@debian ~/Desktop/test_repo $ git init  ## this also fetches my own 
pre-commit hook from template_dir
Initialized empty Git repository in /home/antonia/Desktop/test_repo/.git/
albertjan@debian ~/Desktop/test_repo $ touch foo.txt
albertjan@debian ~/Desktop/test_repo $ git add foo.txt
albertjan@debian ~/Desktop/test_repo $ ls -l .git/hooks
total 4
-rw-r--r-- 1 albertjan albertjan 1468 May 22 14:49 pre-commit
albertjan@debian ~/Desktop/test_repo $ git commit -a -m "commit"   ##### hook 
does not yet work
[master (root-commit) dc82f3d] commit
 0 files changed
 create mode 100644 foo.txt
~/Desktop/test_repo $ chmod +x .git/hooks/pre-commit   ###### can I avoid this 
in Linux? What should I do in Windows?
albertjan@debian ~/Desktop/test_repo $ echo "blaah\n" >> foo.txt
albertjan@debian ~/Desktop/test_repo $ git commit -a -m "commit"  ##### now the 
hook does its job
ERROR: your commit contains at least one file that is larger than 1 bytes




All right, but apart from the sanitation, the medicine, education, wine, public 
order, irrigation, roads, a 

fresh water system, and public health, what have the Romans ever done for us?


Reply via email to