Skip to content

Instantly share code, notes, and snippets.

@airblade
Created November 3, 2014 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save airblade/6f4c5c03742d8ea5cdc5 to your computer and use it in GitHub Desktop.
Save airblade/6f4c5c03742d8ea5cdc5 to your computer and use it in GitHub Desktop.
Git prepare-commit-msg hook to extract issue number from branch name and append to commit message.
#!/bin/bash
# Adds reference to GitHub issue if available in branch name.
# The issue number should be at the end of the branch name, following
# a hyphen.
# Only proceed if no second parameter is given to the script.
if [ x = x${2} ]; then
branch=$(git symbolic-ref --short HEAD)
if [[ $branch =~ .*-([0-9]+)$ ]]; then
issue_number=${BASH_REMATCH[1]}
# sed on osx doesn't treat \n in replacement pattern as a new line.
# If it did we could do this:
#
# sed -i.bak -e "1s/^/\n\nSee #$issue_number./" "$1"
#
# Instead we have do lots of weird quoting.
sed -i.bak -e '1s/^/\'$'\n'"\\"$'\n'"See #$issue_number./" "$1"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment