Skip to content

Instantly share code, notes, and snippets.

@claudiosteuernagel
Forked from toymachiner62/README.md
Created July 30, 2018 21:22
Show Gist options
  • Save claudiosteuernagel/c7c3b634e69e98d9bae3a28c86381fde to your computer and use it in GitHub Desktop.
Save claudiosteuernagel/c7c3b634e69e98d9bae3a28c86381fde to your computer and use it in GitHub Desktop.
Git hook which prevents a commit without a having a link to a TFS work item

Usage - This hook prevents a dev from committing code without having a reference to a tfs work item

Installation. Create a folder in your project root called githooks and save the commit-msg file in there.

The githook can then be installed by symlinking them to your .git/hooks dir.

Run these commands from the project root

$ ln -s ../../githooks/pre-commit .git/hooks/
$ ln -s ../../githooks/commit-msg .git/hooks/

Using a symbolic link will keep your hook up to date with changes in the repo.

#!/bin/sh
#
# This hook verifies that the commit message contains a reference to a tfs item
RED=$(tput setaf 1)
NORMAL=$(tput sgr0)
# Regex to validate a string contains "#" followed by 4 or 5 digits anywhere in the commit message
regex="#[0-9]{4,5}($|[^0-9])"
file=`cat $1` # The file that contains the commit message
# If the commit message does not match the regex
if ! [[ $file =~ $regex ]]; then
echo "${RED}ERROR - Missing tfs item in commmit message.$NORMAL"
exit 1
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment