Skip to content

Instantly share code, notes, and snippets.

@Defelo
Last active April 2, 2022 21:21
Show Gist options
  • Save Defelo/5f84ed11059a10117e3b1ec2cdcf0e39 to your computer and use it in GitHub Desktop.
Save Defelo/5f84ed11059a10117e3b1ec2cdcf0e39 to your computer and use it in GitHub Desktop.
#!/bin/bash
# install via ./pre-commit.sh install
SELF=$(realpath $0)
SRC=$(realpath pre-commit.sh)
_HOOK=$(git rev-parse --git-path hooks)/pre-commit
HOOK=$(realpath $_HOOK)
install() {
echo Installing pre-commit hook...
rm -f $_HOOK
cp $SRC $HOOK && chmod +x $HOOK && echo Pre-Commit hook has been installed successfully!
}
uninstall() {
echo Uninstalling pre-commit hook...
rm -f $_HOOK && echo Pre-Commit hook has been uninstalled successfully!
}
save() {
git submodule foreach "git name-rev --name-only --always HEAD > .head.ref && /bin/bash $SELF save" && git submodule update
git diff > .unstaged.patch
git diff --staged > .staged.patch
git restore --staged .
git restore .
}
restore() {
git submodule foreach "git restore . && git checkout \$(cat .head.ref) && rm .head.ref && /bin/bash $SELF restore"
git apply -3 --allow-empty .staged.patch && rm .staged.patch || echo "Warning: Could not restore staged changes in $(pwd)"
git apply --allow-empty .unstaged.patch && rm .unstaged.patch || echo "Warning: Could not restore unstaged changes in $(pwd)"
}
if [[ "$1" =~ ^(install|uninstall|save|restore)$ ]]; then
$1
exit $?
fi
if ! cmp -s $SRC $HOOK; then
set -e
echo Updating pre-commit hook...
rm -f $_HOOK
cp $SRC $HOOK && chmod +x $HOOK && echo Pre-Commit hook has been updated successfully!
/bin/bash $HOOK "$@"
exit $?
elif [[ "$SELF" != "$HOOK" ]]; then
/bin/bash $HOOK "$@"
exit $?
fi
save
git apply --allow-empty .staged.patch && rm .staged.patch && touch .staged.patch
git add -u
$HOME/.local/bin/poe pre-commit
code=$?
git add -u
restore
exit $code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment