Skip to content

Instantly share code, notes, and snippets.

Last active July 19, 2018 06:30
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Shell script for git hook to add work item ID to commit message. I work with VSTS and work items IDs is numbers. To associate commit with work item I must add at the end of commit message char # and ID e.g. #123.
BRANCH_NAME=$(git symbolic-ref --short HEAD)
STORY_NUMBER=$(echo $BRANCH_NAME | sed -n 's/.*-\([0-9]\)/\1/p') # Get story number from branch name - "my_feature_branch-123"
COMMIT_MSG=`cat $1`
if [ x != x${STORY_NUMBER} ]; then # Checf if STORY_NUMBER is NOT empty
if [ x = x${2} ]; then # Check if commit message is empty
sed -i.back "1s/^/#$STORY_NUMBER/" "$1"
elif [[ $COMMIT_MSG != *$STORY_NUMBER* ]]; then # # Commit message is not empty and NOT contains story number [git commit --amend --no-edit]
sed -i.back '${s/$/'" #$STORY_NUMBER"'/}' "$1" # Add story number at the end of line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment