Skip to content

Instantly share code, notes, and snippets.

@GokhanArik
Created September 24, 2019 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GokhanArik/fca53add222b62fc2fcb05b61d69275d to your computer and use it in GitHub Desktop.
Save GokhanArik/fca53add222b62fc2fcb05b61d69275d to your computer and use it in GitHub Desktop.
Git hook to add Jira story id to the commit message.
#!/bin/sh
####################################################
# #
# DO NOT TRY ON A WINDOWS MACHINE #
# (nothing happens if you try, it just won't work) #
####################################################
# This script edits commit message and attaches story number to commit message.
# NOTE: We assume story number is included in branch name
# For example, 'ABC-123-test-branch-name'
# After script executed, new commit message would be: "ABC-123 : your commit message"
# Copy and paste it into your `projectDirectory/.git/hooks`
# Make sure the file is executable by running `chmod +x commit-msg`
# ticket is the ID of the issue parsed from branch name: ABC-123
ticket=$(git symbolic-ref HEAD | perl -ne '/([A-Za-z]*-\d+)/; print "$1"')
# merge is a flag that determines if a commit message is from merge.
merge=$(cat "$1" | perl -ne '/(Merge branch)/; print "$1"')
if [ -n "$merge" ]; then
exit 0
elif [ -n "$ticket" ]; then
echo "$ticket"' : '$(cat "$1") > "$1"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment