huangcm
2025-07-03 a76b2fadf6ad4adf86e241e3753a63efe03ef80c
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
#!/bin/sh
 
if [ $(basename "$0") != "install" ]; then
  if [ -x "$0.local" ]; then
    "$0.local" "$@" || exit $?
  fi
  if [ -x hooks/$(basename $0) ]; then
    hooks/$(basename $0) "$0" || exit $?
  fi
else
  pushd "$(git rev-parse --show-toplevel)"
  python <<\EOF
import os, os.path
TOP = os.path.realpath(".")
HOOKS = os.path.realpath(".git/hooks")
src = os.path.join(TOP, "hooks", "install")
for hook in os.listdir("hooks"):
  if hook != "install":
    tgt = HOOKS + os.sep + hook
    # there is a file there
    if os.path.isfile(tgt) and os.access(tgt, os.X_OK):
      if os.path.realpath(tgt) != src:
        print("hook " + hook + " is already installed. Moving to " + hook + ".local")
        os.rename(tgt, tgt + ".local")
    if os.path.lexists(tgt):
      os.unlink(tgt)
    os.symlink(os.path.relpath(os.path.realpath("hooks/install"), os.path.realpath(".git/hooks/")), tgt)
EOF
  popd
fi