Skip to content

Instantly share code, notes, and snippets.

@iocanel
Created May 12, 2022 07:04
Show Gist options
  • Save iocanel/d9e42b28a6789778a21998daa36664fe to your computer and use it in GitHub Desktop.
Save iocanel/d9e42b28a6789778a21998daa36664fe to your computer and use it in GitHub Desktop.
Git pre-commit hook to prevent pdf and adoc changes in the same commit
#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit".
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
# If you want to allow non-ASCII filenames set this variable to true.
allownonascii=$(git config --type=bool hooks.allownonascii)
# Redirect output to stderr.
exec 1>&2
# Just check that pdf and adoc changes are kept in different commits.
HAS_PDF=`git diff --cached --name-status | awk -F "." '{print $NF}' | grep pdf | uniq`
HAS_ADOC=`git diff --cached --name-status | awk -F "." '{print $NF}' | grep adoc | uniq`
[ -n "$HAS_PDF" ] && echo "Changes in pdf:" && git diff --cached --name-status | grep "pdf" | while read f; do echo " $f"; done
[ -n "$HAS_ADOC" ] && echo "Changes in adoc:" && git diff --cached --name-status | grep "adoc" | while read f; do echo " $f"; done
if [ -n "$HAS_PDF" ] && [ -n "$HAS_ADOC" ]; then
echo "You should not mix adoc and pdf changes in the same commit message. Plese separate them and try again!"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment