Skip to content

Instantly share code, notes, and snippets.

@RafhaanShah
Last active February 23, 2021 10:27
Show Gist options
  • Save RafhaanShah/1259bcae7268ee58bfca0de6c4e1bf58 to your computer and use it in GitHub Desktop.
Save RafhaanShah/1259bcae7268ee58bfca0de6c4e1bf58 to your computer and use it in GitHub Desktop.
Git Hooks
#!/usr/bin/env sh
# exit on any error, unset variable
set -eu
# checks if a specific file (such as a version file) has been updated before commit
FILE="file.txt"
if ! git diff --cached "${FILE}"; then
echo "The file ${FILE} has not been updated and staged"
exit 1
fi
#!/usr/bin/env sh
# exit on any error, unset variable
set -eu
# checks if a specific file (such as a version file) has been updated before push
FILE="file.txt"
BRANCH="origin/master"
if ! git diff HEAD "${BRANCH}" --name-only "${FILE}"; then
echo "The file ${FILE} has not been updated and staged"
exit 1
fi
#!/usr/bin/env sh
# exit on any error, unset variable
set -eu
# staged files with specific extension
FILES="$(git diff --cached --name-only --diff-filter=ACMR "*.EXTENSION" | sed 's| |\\ |g')"
if ! command -v COMMAND >/dev/null 2>&1; then
echo 'Warning: COMMAND is not installed'
else
if [ -n "${FILES}" ]; then
echo "Running COMMAND..."
echo "${FILES}" | xargs COMMAND -i 4 -ci -l -w -s
echo "${FILES}" | xargs git add
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment