Skip to content

Instantly share code, notes, and snippets.

@RafhaanShah
Last active March 8, 2021 15:27
Show Gist options
  • Save RafhaanShah/0cb02663f0e25477f6e117537337be76 to your computer and use it in GitHub Desktop.
Save RafhaanShah/0cb02663f0e25477f6e117537337be76 to your computer and use it in GitHub Desktop.
Random Git Hook Stuff
#!/bin/sh
# exit on any error, unset variable, or failed piped commands
set -euo pipefail
# set hooks path
git config core.hooksPath ".git-hooks"
chmod -R +x "$(git rev-parse --git-path hooks)"
# check if command is installed
if ! command -v COMMAND >/dev/null 2>&1; then
echo 'Error: COMMAND is not installed' >&2
exit 1
fi
# add dirs to path from paths.txt
while read -r line; do
if [ -n "${line}" ] && [ -d "${line}" ]; then
PATH="${line}:${PATH}"
fi
done <".git-hooks/paths.txt"
# run command on filtered modified files
FILES=$(git diff --cached --name-only --diff-filter=ACMR "*.EXTENSION_1" "*.EXTENSION_2" | sed 's| |\\ |g')
if [ -n "$FILES" ]; then
# apply command
echo "$FILES" | xargs COMMAND
# add back the modified files to staging
echo "$FILES" | xargs git add
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment