-
-
Save cgarnier/7847451c0873987e63c31365cfa05a79 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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 =~ ^([^/]+)/([A-Z]+\-[0-9]+) ]]; then | |
issue_number=${BASH_REMATCH[2]} | |
issue_type=${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'"$issue_type\n""$issue_number/" "$1" | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment