Skip to content

Instantly share code, notes, and snippets.

@Ionshard
Last active August 29, 2015 14:18
Show Gist options
  • Select an option

  • Save Ionshard/e49564fdbbbf0a0ed2a6 to your computer and use it in GitHub Desktop.

Select an option

Save Ionshard/e49564fdbbbf0a0ed2a6 to your computer and use it in GitHub Desktop.
Automatically Prepend Ticket ID to Git Commit Message Based on Feature Branch
#!/bin/sh
#
# An example hook script to prepare the commit log message.
# Called by "git commit" with the name of the file that has the
# commit message, followed by the description of the commit
# message's source. The hook's purpose is to edit the commit
# message file. If the hook fails with a non-zero status,
# the commit is aborted.
#If your current branch matches the pattern f/TICKET-ID1234 or f/TICKET-ID1234-some-description
# this script will append [TICKET-ID1234] to your commit message
branch=$(git symbolic-ref -q --short HEAD)
ticket=$(echo $branch | sed -n 's/^f\/\([A-Z]\+-[0-9]\+\)-\?.*/\1/p')
if [ -n "$ticket" ]; then
sed "1s/^/[$ticket] /" $1 > $1.new
mv $1.new $1
fi
@Ionshard
Copy link
Author

Ionshard commented Apr 1, 2015

Input

An Awesome Title!

  Here is an even awesomer description

Branch: f/XYZ-1234 OR f/XYZ-some-description

[XYZ-1234] An Awesome Title!

  Here is an even awesomer description

Branch: x/XYZ OR r/XYZ OR master OR integration

An Awesome Title!

  Here is an even awesomer description

@Ionshard
Copy link
Author

Ionshard commented Apr 1, 2015

After thinking about additional use cases (that I don't encounter, but since I have decided to make this public) I have hardened the regular expression to expect f/[A-Z]+-[0-9]+ so you can now have feature branches without ticket IDs

@aforward
Copy link

aforward commented Apr 2, 2015

And for those that don't know, I believe you want to look in

PROJECT/.git/hooks/

For where to put that script

@aforward
Copy link

aforward commented Apr 2, 2015

I don't think the sed script is working on a mac

23:47 ~/tp/cenx/rdops (f/RD-1012)$ branch=$(git symbolic-ref -q --short HEAD)
23:47 ~/tp/cenx/rdops (f/RD-1012)$ ticket=$(echo $branch | sed -n 's/^f\/\([A-Z]\+-[0-9]\+\)-\?.*/\1/p')
23:48 ~/tp/cenx/rdops (f/RD-1012)$ echo $branch
f/RD-1012
23:48 ~/tp/cenx/rdops (f/RD-1012)$ echo $ticket

23:48 ~/tp/cenx/rdops (f/RD-1012)$ 

@aforward
Copy link

aforward commented Apr 2, 2015

Looks like on a mac, the "+" is the problem, so on a mac try

branch=$(git symbolic-ref -q --short HEAD)
ticket=$(echo $branch | sed -n 's/\f\/\([A-Z][A-Z]*-\d*\)/\1/p')

if [ -n "$ticket" ]; then
    sed "1s/^/[$ticket] /" $1 > $1.new
    mv $1.new $1
fi

Tada (thanks Corey)

23:55 ~/tp/cenx/rdops (f/RD-1012)$ git commit -m "Testing prepare messsage hook"
[f/RD-1012 f1e3bb5] [RD-1012] Testing prepare messsage hook
 1 file changed, 1 insertion(+), 1 deletion(-)
23:55 ~/tp/cenx/rdops (f/RD-1012)$ git log -3
commit f1e3bb56d888b600aee77c6a2d75fe62facc8d2e
Author: Andrew Forward <[email protected]>
Date:   Wed Apr 1 23:55:55 2015 -0400

    [RD-1012] Testing prepare messsage hook

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment