Skip to content

Instantly share code, notes, and snippets.

@drobinson
Created May 12, 2023 15:18
Show Gist options
  • Save drobinson/38fe30626b0f95f2a0c2f6537f92442b to your computer and use it in GitHub Desktop.
Save drobinson/38fe30626b0f95f2a0c2f6537f92442b to your computer and use it in GitHub Desktop.
Prepend the ticket number from current branch to commit message
#!/bin/sh
# Put this in .git/hooks/
# Get current branch
branchName=`git rev-parse --abbrev-ref HEAD`
echo "branchName: $branchName" >&2
# Match Jira issue ID in a pattern such as "{any_word}/abc-123-description" or "ABC-123-description"
jiraId=$(echo $branchName | sed -Ene 's,^([a-zA-Z0-9_/-]+/)?([a-zA-Z]+-[0-9]+)-.+,\2,p')
echo "jiraId: $jiraId" >&2
# Only prepare commit message if pattern matched and JiraId was found
if [[ ! -z $jiraId ]]; then
# $1 is the name of the file containing the commit message
echo "Adding Jira issue ID to commit message..." >&2
sed -i.bak -e "1s/^/[$jiraId] /" $1
else
echo "No Jira issue ID found in branch name." >&2
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment