This repository has been archived by the owner on Oct 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
pre-commit.sh
executable file
·64 lines (49 loc) · 1.64 KB
/
pre-commit.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/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 --exclude='tags/*' HEAD > .head.ref && /bin/bash $SELF save" && git submodule update
git diff > .unstaged.patch
git diff --staged > .staged.patch
git restore -WS .
}
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 --index .staged.patch && rm .staged.patch && touch .staged.patch
git add -u
$HOME/.local/bin/poe pre-commit
code=$?
git add -u
restore
exit $code