Skip to content

Instantly share code, notes, and snippets.

@albertzak
Created August 25, 2015 21:26
Show Gist options
  • Save albertzak/8d512b923533077f4df5 to your computer and use it in GitHub Desktop.
Save albertzak/8d512b923533077f4df5 to your computer and use it in GitHub Desktop.
Prevent committing dev tags
#!/bin/sh
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=$(git hash-object -t tree /dev/null)
fi
patch_filename=$(mktemp -t commit_hook_changes)
git diff --exit-code --binary --ignore-submodules --no-color > $patch_filename
has_unstaged_changes=$?
if [[ $has_unstaged_changes != 0 ]]; then
echo "Stashing unstaged changes in $patch_filename."
git checkout -- .
fi
function quit {
if [[ $has_unstaged_changes != 0 ]]; then
git apply $patch_filename
if [[ $? != 0 ]]; then
git checkout -- .
git apply $patch_filename
fi
fi
exit $1
}
# Redirect output to stderr.
exec 1>&2
files_with_nocommit=$(git diff --cached --name-only --diff-filter=ACM $against | xargs egrep -i "\@dev$" -l | tr '\n' ' ')
if [[ "x${files_with_nocommit}x" != "xx" ]]; then
tput setaf 1
echo "File being committed with '@dev' in it:"
echo $files_with_nocommit | tr ' ' '\n'
tput sgr0
quit 1
fi
quit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment