Skip to content

Instantly share code, notes, and snippets.

@rm--
Last active April 9, 2017 09:56
Show Gist options
  • Select an option

  • Save rm--/f2954c9eed5d8feadf71e1ce67044e30 to your computer and use it in GitHub Desktop.

Select an option

Save rm--/f2954c9eed5d8feadf71e1ce67044e30 to your computer and use it in GitHub Desktop.
add issue number from branch to commit msg
#!/bin/bash
# Extact issue tag (e.g. feature/TAG-123 -> TAG-123, ANOTHERTAG-123-description -> ANOTHERTAG-123)
fetch_issue_tag() {
git rev-parse --abbrev-ref HEAD | grep -e '[A-Z]\+-[0-9]\+' -o
}
BRANCH_NAME=$( git branch | grep '*' | sed 's/* //' )
ISSUE_TAG=$( fetch_issue_tag )
FIRSTLINE="$( head -n 1 "$1" )"
BODY="$( tail -n +2 "$1" )"
# Skip editing the commit message when rebasing.
if [[ "$BRANCH_NAME" != '(no branch'* ]]
then
# Append the issue tag if the issue tag is in the branch
# and not already in the commit message.
if [[ -z "${FIRSTLINE//}" ]]
then
if [[ -n "$ISSUE_TAG" ]]
then
NEW_MESSAGE="$ISSUE_TAG: \n$BODY"
printf "$NEW_MESSAGE" > "$1"
else
NEW_MESSAGE="# Issue tag? \n$BODY"
printf "$NEW_MESSAGE" > "$1"
fi
fi
fi
@rm--

rm-- commented Mar 8, 2017

Copy link
Copy Markdown
Author

chmod +x .git/hooks/prepare-commit-msg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment