-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.sh
executable file
·129 lines (113 loc) · 3.17 KB
/
install.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env sh
set -ue
print_help ()
{
printf "Install git hooks in a repository.\n"
printf "Usage: %s [--force] [TARGET_DIR]\n" "$0"
printf "Existing hooks will be skipped, unles --force is given.\n"
printf "TARGET_DIR defaults to \$PWD.\n"
}
get_package_name ()
{
dir="$1"
if [ -e "$dir/package.json" ] && command -v node >/dev/null
then
node -e '
process.stdout.write(require(process.argv[1]).name);
' -- "$dir/package.json"
fi
}
if [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]
then
print_help
exit 0
fi
ARG_FORCE=
if [ "${1:-}" = "-f" ] || [ "${1:-}" = "--force" ]
then
ARG_FORCE=1
shift
fi
if ! command -v realpath >/dev/null
then
realpath ()
{
(
path="$1"
cd "$( dirname "$path " )"
while [ -h "$path" ]
do
path="$( readlink "$path" )"
cd "$( dirname "$path" )"
done
echo "$PWD/$( basename "$path" )"
)
}
fi
package_dir="$( cd "$( dirname "$( realpath "$0" )" )" && pwd -P )"
dir="${1:-${INIT_CWD:-$PWD}}"
git_dir="$(
cd "$dir"
cd "$( git rev-parse --git-dir )"
pwd -P
)"
if [ -z "$git_dir" ]
then
printf "ERROR: No git repository found in: %s\n" "$dir"
exit 66
fi
if [ "${npm_config_global:-}" = "true" ]
then
# Ran `npm install -g` and postinstall kicked in
exit 0
fi
if
[ -n "${npm_package_name:-}" ] &&
[ "$npm_package_name" = "$( get_package_name "$git_dir" )" ]
then
# Ran `npm install` in this package and postinstall kicked in
exit 0
fi
source_hooks_dir="$package_dir/hooks"
target_source_hooks_dir="$git_dir/hooks"
mkdir -p "$target_source_hooks_dir"
cd "$target_source_hooks_dir"
find "$source_hooks_dir" -type f |
while read -r path
do
name="$( basename "$path" )"
source_hook_file="$source_hooks_dir/$name"
target_hook_file="./$name"
# Instead of linking, we create small scripts that source the
# hook files. This is, because some other hooks (e.g. lefthook)
# tend to change contents of hook files when they install
# themselves.
if [ -n "$ARG_FORCE" ]
then
rm -fv "$target_hook_file"
fi
if [ -e "$target_hook_file" ]
then
printf "%s hook already exists — skipping.\n" "$name"
else
# shellcheck disable=SC2016
printf '#!/usr/bin/env sh\nset -eu\n\nexport GIT_PID="$PPID"\nexport HOOKS_DIR="%s"\n. "%s" "$@"\n' \
"$package_dir" \
"$source_hook_file" \
> "$target_hook_file"
chmod +x "$target_hook_file"
printf "%s hook installed\n" "$name"
fi
done
hookspath="$( git config --get core.hookspath || : )"
if [ -n "$hookspath" ] && [ "$hookspath" != ".git/hooks/" ]
then
if ! git config --global core.hookspath >/dev/null
then
printf "core.hookspath was set to '%s', unsetting.\n" "$hookspath"
git config --unset core.hookspath
else
printf "core.hookspath was set to '%s', changing to '.git/hooks/'\n" "$hookspath"
git config core.hookspath '.git/hooks/'
fi
fi