GitHooks: Difference between revisions

From Cheaha
Jump to navigation Jump to search
(added notes on preparing commit message based on current working branch (ticket number))
 
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). It is a good practice to create project management ticket for issue tracking and also reference this ticket number in your version control system's history (commit messages). Also, many developers find it easier to isolate their development work using 'branches'. If you are following such ticket-driven development and want to include ticket-number in every commit message then you may find following commit hook useful. Following example adds reference to a ticket number based on your current working branch.   
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]/'`
   msg=`git branch | sed -e '/^[^*]/d' -e 's/* \(.*\)/# Commit for [galaxy:ticket:\1]/'`
   sed -i -e "1i $msg" "$1"
   sed -i -e "1i $msg" "$1"


The above example works 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.  
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"
   ruby  -i -pe 'puts `git branch`.split(/\n/).select {|e| e =~ /\*.*/}.to_s.sub(/\* /,"# Commit for [galaxy:ticket:").concat("]") if $.==1' "$1"


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

Revision as of 20:58, 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 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.