GitHooks: Difference between revisions

From Cheaha
Jump to navigation Jump to search
No edit summary
 
Line 2: Line 2:


=== Prepare commit message ===
=== 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.  
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 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.   
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]/'`
   msg=`git branch | sed -e '/^[^*]/d' -e 's/* \(.*\)/# Commit for [galaxy:ticket:\1]/'`

Latest revision as of 21:04, 10 January 2012

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.