Use the left/right arrow keys to navigate.

Using and Abusing Git hooks

Joseph Hsu, developer at Academic Software Plus

@jhsu

github.com/jhsu

What are git hooks

Scripts that are called at different steps when working with git.

Created on git init

copied from /usr/share/git-core/templates/hooks/ to .git/hooks/

Setting up git hooks

  • remove ".sample" suffix from hook inside .git/hooks/
  • create a new script, chmod +x it

Hooks exist on your local checkout of the repository

Don't Forget:

chmod +x .git/hooks/pre-commit

When the hook is called, exit with non-zero to abort

exit 1

git commit => pre-commit

  • git diff --staged --name-only to find what files are about to be commited
  • check for console.log(...)

git commit => post-commit

After the entire commit process is completed.

  • get list of open issues
  • automatically push if on master

or...

afplay ~/.git/happykids.wav > /dev/null 2>&1 &

git commit => commit-msg

  • gets passed the path to the proposed commit message

git merge => {pre,post}-merge

  • run migrations if any changes to db/migrations (check using git diff HEAD^)

post-receive

  • useful for shared repos
  • use for notifications (ie, irc, email, ci server, etc.)

Fun!

I respect you, bro.

brodown

Bro

  • allows you to append ", bro" to the end of the message

whatthecommit

#!/usr/bin/env bash
curl -s http://whatthecommit.com/index.txt > $1

"i think i fixed a bug..."

"committing according to the Prophecy."

"tl;dr"

Random author

pre-commit

git log --format='%aN' | sort -u | while read x; do echo \
"`expr $RANDOM % 1000`:$x"; done | sort -n| sed 's/[0-9]*://' | head -n 1