/ nginx-keycloak / pre-commit.sh
pre-commit.sh
 1  #!/bin/bash
 2  
 3  # install via ./pre-commit.sh install
 4  
 5  SELF=$(realpath $0)
 6  SRC=$(realpath pre-commit.sh)
 7  _HOOK=$(git rev-parse --git-path hooks)/pre-commit
 8  HOOK=$(realpath $_HOOK)
 9  
10  install() {
11      echo Installing pre-commit hook...
12      rm -f $_HOOK
13      cp $SRC $HOOK && chmod +x $HOOK && echo Pre-Commit hook has been installed successfully!
14  }
15  
16  uninstall() {
17      echo Uninstalling pre-commit hook...
18      rm -f $_HOOK && echo Pre-Commit hook has been uninstalled successfully!
19  }
20  
21  save() {
22      git submodule foreach "git name-rev --name-only --always --exclude='tags/*' HEAD > .head.ref && /bin/bash $SELF save" && git submodule update
23  
24      git diff > .unstaged.patch
25      git diff --staged > .staged.patch
26      git restore -WS .
27  }
28  
29  restore() {
30      git submodule foreach "git restore . && git checkout \$(cat .head.ref) && rm .head.ref && /bin/bash $SELF restore"
31  
32      git apply -3 --allow-empty .staged.patch && rm .staged.patch || echo "Warning: Could not restore staged changes in $(pwd)"
33      git apply --allow-empty .unstaged.patch && rm .unstaged.patch || echo "Warning: Could not restore unstaged changes in $(pwd)"
34  }
35  
36  if [[ "$1" =~ ^(install|uninstall|save|restore)$ ]]; then
37      $1
38      exit $?
39  fi
40  
41  if ! cmp -s $SRC $HOOK; then
42      set -e
43      echo Updating pre-commit hook...
44      rm -f $_HOOK
45      cp $SRC $HOOK && chmod +x $HOOK && echo Pre-Commit hook has been updated successfully!
46      /bin/bash $HOOK "$@"
47      exit $?
48  elif [[ "$SELF" != "$HOOK" ]]; then
49      /bin/bash $HOOK "$@"
50      exit $?
51  fi
52  
53  save
54  git apply --allow-empty --index .staged.patch && rm .staged.patch && touch .staged.patch
55  git add -u
56  
57  just pre-commit
58  code=$?
59  
60  git add -u
61  
62  restore
63  
64  exit $code