GitHooks

From Cheaha
Revision as of 20:58, 10 January 2012 by Pavgi@uab.edu (talk | contribs)
Jump to navigation Jump to search


Attention: Research Computing Documentation has Moved
https://docs.rc.uab.edu/


Please use the new documentation url https://docs.rc.uab.edu/ for all Research Computing documentation needs.


As a result of this move, we have deprecated use of this wiki for documentation. We are providing read-only access to the content to facilitate migration of bookmarks and to serve as an historical record. All content updates should be made at the new documentation site. The original wiki will not receive further updates.

Thank you,

The Research Computing Team

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 branche 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 commit 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.