MW

A git hook to prevent pushes with untracked source files

Hey all,

do you know this: You work on something locally in git, ensure everything compiles and the tests pass, then commit and hit git push.What could possibly go wrong at that point, eh? Well, far too often I forgot to git add some new source file. Best-case I’ll notice this directly, worst-case I’ll see my CI complaining. But, like yesterday in kdev-clang, I might be afk at that point and someone else will have to revert my change and I’ll have to fix it up the day after, polluting the git history while at it…

Thanks to some simple shell scripting and the powerful git hook architecture, it is pretty simple to protect oneself against such issues:

    #!/bin/sh
     
    #
    # A hook script to verify that a push is not done with untracked source file
    #
    # To use it, either symlink this script to $your-git-clone/.git/hooks/pre-push
    # or include it in your existing pre-push script.
    #
     
    # Perl-style regular expression which limits the files we interpret as source files.
    # The default pattern here excludes CMakeLists.txt files and any .h/.cpp/.cmake files.
    # Extend/adapt this to your needs. Alternatively, set the pattern in your repo via:
    #     git config hooks.prepush.sourcepattern "$your-pattern"
    pattern=$(git config --get hooks.prepush.sourcepattern)
    if [ -z "$pattern" ]; then
      pattern="(?:(?:^|/)CMakeLists\.txt|\.h|\.cpp|\.cmake)$"
    fi
     
    files=$(git status -u --porcelain --no-column | sed "s/^?? //" | grep -P "$pattern")
    if [ -z "$files" ]; then
      exit 0
    fi
     
    echo
    echo "ERROR: Preventing push with untracked source files:"
    echo
    echo "$files" | sed "s/^/    /"
    echo
    echo "Either include these files in your commits, add them to .gitignore"
    echo "or stash them with git stash -u."
    echo
    exit 1

Note: The last version of the above code can be found on GitHub: pre-push-check-untracked

When you then try to push with some untracked source files, i.e. files matched by the regex pattern which can be configured via git config hooks.prepush.sourcepattern, you’ll see output like this:

    # to show the current status, note the untracked bar file which is not included as a source file
    $ git status
    On branch master
    Your branch is up-to-date with 'origin/master'.
     
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
     
            CMakeLists.txt
            bar
            foo/
     
    nothing added to commit but untracked files present (use "git add" to track)
     
    # now try to push something
     
    $ git push
     
    ERROR: Preventing push with untracked source files:
     
        CMakeLists.txt
        foo/asdf.h
     
    Either include these files in your commits, add them to .gitignore
    or stash them with git stash -u.
     
    error: failed to push some refs to '/tmp/repo'

Happy hacking!

Comments

Want to comment? Send me an email!

Comment by Anonymous (2014-08-13 22:04:00)

This is a very good idea. Thanks for sharing it!

Comment by Anonymous (2014-08-08 22:35:00)

Nice one! :)

Published on August 08, 2014.