Skip to content

Instantly share code, notes, and snippets.

@tomaszprasolek
Last active July 19, 2018 06:30
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 tomaszprasolek/492c37ea9368ed86020e9e5b7ce92f33 to your computer and use it in GitHub Desktop.
Save tomaszprasolek/492c37ea9368ed86020e9e5b7ce92f33 to your computer and use it in GitHub Desktop.
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.
#!/bin/sh
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
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment