Skip to content

Instantly share code, notes, and snippets.

@toymachiner62
Last active March 8, 2023 15:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save toymachiner62/6d86a81e5b167ff4e260 to your computer and use it in GitHub Desktop.
Save toymachiner62/6d86a81e5b167ff4e260 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