GitHooks

Git hooks are custom scripts that trigger upon certain git action/command. The ProGit book has explained Git hooks mechanism in detail so I recommend reading it before proceeding with following sections.

Prepare commit message
In most projects a version control system (Git) is integrated with a project management software (trac). Most of the development activities or issues are tracked using 'ticket' system of project management software like trac. Often developers use version control system's 'branches' to isolate their development work according to issues or tickets and hence in such ticket-driven development branch names tend to be based on ticket number or description. Also, it is a good practice to reference these ticket numbers in version control system's history (commit messages) so that tickets and corresponding development activity (code changes) can be cross-referenced. Following is an example of prepare-commit-message hook to help adding current working branch name (assumed to be ticket number) to a commit message. It will add a commented-out line in addition to default pre-populated commented-out lines.

msg=`git branch | sed -e '/^[^*]/d' -e 's/* \(.*\)/# Commit for [galaxy:ticket:\1]/'` sed -i -e "1i $msg" "$1"

The above example uses GNU sed and hence doesn't work on the Mac OS which uses BSD sed. Following Ruby one-liner example works on both Linux and Mac OS systems. ruby -i -pe 'puts `git branch`.split(/\n/).select {|e| e =~ /\*.*/}.to_s.sub(/\* /,"# Commit for [galaxy:ticket:").concat("]") if $.==1' "$1"

You may need to modify above one-liners according to your ticket-branch naming convention and other requirements.