Skip to content

Instantly share code, notes, and snippets.

@elboletaire
Created April 2, 2015 10:44
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 elboletaire/53edfba0ed40bf83baa2 to your computer and use it in GitHub Desktop.
Save elboletaire/53edfba0ed40bf83baa2 to your computer and use it in GitHub Desktop.
Git hook prepare-commit-msg

This git hook extracts the number from a branch and adds it to the commit message as refs. i.e.:

From a branch named whatever-branch-name-5234 will create a commit message like this:

refs #5234
# git COMMIT_MSG comments
#!/bin/bash
BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_NAME="${BRANCH_NAME##*/}"
ISSUE_ID=$(echo $BRANCH_NAME | sed 's/^[a-z\-]*-//I')
IS_NUMBER='^[0-9]+$'
if [ -n "$BRANCH_NAME" ] && [[ $ISSUE_ID =~ $IS_NUMBER ]]; then
sed -i.bak -e "1s/^/\n\nrefs #$ISSUE_ID /" $1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment