Friday, November 23, 2012

Git - Get all short hashes in line by filter. Add custom command to Git

Get all short hashes in line by filter

On work we have to add all hashes of Git revisions into an appropriate task. We wrote in the commit comments code of task and title:

Fixed PROJECT1-1245: Bug on the product page in the product price
- Fixed bug in the price calculation
- Updated tests

When I did many commits I have to copy each hash and paste to ticket.

It's not dificult but we are programmers! :) So, I throught that it would be good to write a little script which will do this one.
# Get all hashes by filtering of commit comment
if [ "$1" != "" ]; then
    OUT=$(git log --pretty=format:'%h' --reverse --grep $@)
    OUT=$(echo -e $OUT) #del line break
    echo ${OUT// /","}
    echo "Please set filter string as first parameter."

Call to script in Git bash:

$ ~/ PROJECT1-1245

Also you can add any Git parameters:

$ ~/ PROJECT1-1245 -3

Add custom command to Git

Let's make it as Git functional. :) I gave to this command name allhash. So we have to copy this file to
C:\Program Files (x86)\Git\libexec\git-core (on Windows 64)
with new filename by mask git-newcommandname. Our file have to get name git-allhash.
Let's call out new command:

$ git allhash PROJECT1-1245

$ git allhash PROJECT1-1245 -3
Post a Comment