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.
73b7468,97fdcc7,60914f6,439bd39,e32ff69,199aa4

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.
#!/bin/sh
# 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// /","}
else
    echo "Please set filter string as first parameter."
    exit
fi

Call to script in Git bash:

$ ~/get_hash.sh PROJECT1-1245
73b7468,97fdcc7,60914f6,439bd39,e32ff69,199aa46


Also you can add any Git parameters:

$ ~/get_hash.sh PROJECT1-1245 -3
439bd39,e32ff69,199aa46


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
73b7468,97fdcc7,60914f6,439bd39,e32ff69,199aa46

$ git allhash PROJECT1-1245 -3
439bd39,e32ff69,199aa46
Post a Comment