Skip to content

Instantly share code, notes, and snippets.

Embed
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.
#!/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