Thursday, March 10, 2011

My new Git pre-commit hook

This appears to be kind of useful:
#!/bin/sh

output=$(find . -name '.#*' -print)
if [ -n "$output" ]; then
        echo "unsaved Emacs files:" 1>&2
        echo "$output" 1>&2
        exit 1
fi
Had that kind of problem a few times. :-)

Now what would be really handy are system-wide Git hooks that apply to all repositories, like ~/.gitignore complements .git/info/exclude.

7 comments:

  1. You can use repository templates to apply a hook to all newly cloned or created repositories. That is how apply my standard hooks to all my repositories.

    ReplyDelete
  2. Why don't you put those in your global ~/.gitignore?

    ReplyDelete
  3. Boyd Stephen Smith Jr.10 March, 2011 19:43

    If you have set init.templatedir in your global git config, that template will be used for all newly init'd/cloned repositories.

    You can install hooks in there so they will be active "globally".

    ReplyDelete
  4. I suggest to adopt this patch, to make it more general (and also catch vim files):

    #!/bin/sh

    output=$(find . -name '.#*' -o -name '.*.sw*' -print)
    if [ -n "$output" ]; then
    echo "unsaved Editor files:" 1>&2
    echo "$output" 1>&2
    exit 1
    fi

    Thanks for the idea!

    ReplyDelete
  5. Consider using magit at this point.

    http://farm3.static.flickr.com/2642/4050674979_785b349e1b_o.png

    http://philjackson.github.com/magit/

    http://vimeo.com/2871241

    ReplyDelete
  6. @SEJeff: I don't want to ignore them, I want to be told when I have unsaved work before committing.

    ReplyDelete
  7. Great post Peter. Thought about using magit? We just wrote an article that may be informative. https://blog.mojotech.com/why-you-should-use-magit-git-porcelain-inside-emacs.

    ReplyDelete