Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·114 lines (105 loc) · 3.23 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·114 lines (105 loc) · 3.23 KB
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
#!/usr/bin/env bash
# Install git hooks globally via core.hooksPath.
# Usage: ./install.sh [--template] [--check]
# --template Use init.templateDir instead (only affects new repos)
# --check Show current hooks configuration without changing anything
set -euo pipefail
hook_dir="$(cd "$(dirname "$0")" && pwd)"
use_template=false
check_only=false
usage() {
cat <<EOF
Usage: ./install.sh [--template] [--check] [--help]
Options:
--template Use init.templateDir instead of core.hooksPath
--check Show current hooks configuration without changing anything
--help Show this help text
EOF
}
for arg in "$@"; do
case "$arg" in
--template)
use_template=true
;;
--check)
check_only=true
;;
--help|-h)
usage
exit 0
;;
*)
echo "Unknown option: $arg" >&2
usage >&2
exit 1
;;
esac
done
if [ "$check_only" = true ]; then
hooks_path=$(git config --global --get core.hooksPath 2>/dev/null || true)
template_dir=$(git config --global --get init.templateDir 2>/dev/null || true)
if [ -n "$hooks_path" ]; then
echo "core.hooksPath = $hooks_path"
if [ "$hooks_path" = "$hook_dir" ]; then
echo " (points to this directory)"
else
echo " (points elsewhere — not this directory)"
fi
fi
if [ -n "$template_dir" ]; then
echo "init.templateDir = $template_dir"
if [ "$template_dir" = "$hook_dir" ]; then
echo " (points to this directory)"
else
echo " (points elsewhere — not this directory)"
fi
fi
if [ -z "$hooks_path" ] && [ -z "$template_dir" ]; then
echo "No global hooks configuration found."
fi
if [ "$use_template" = true ]; then
echo "Would set: init.templateDir = $hook_dir (and clear a matching core.hooksPath)"
else
echo "Would set: core.hooksPath = $hook_dir (and clear a matching init.templateDir)"
fi
exit 0
fi
# Clear the *other* mechanism only when it points at this directory, so we never
# silently delete a user's unrelated hook manager or template configuration.
clear_conflicting() {
local key="$1"
local current
current=$(git config --global --get "$key" 2>/dev/null || true)
if [ -z "$current" ]; then
return
fi
if [ "$current" = "$hook_dir" ]; then
git config --global --unset "$key" >/dev/null 2>&1 || true
echo "Cleared global $key (it pointed to this directory)."
else
echo "WARNING: global $key = $current is set and may conflict." >&2
echo " Leaving it untouched; unset it manually if needed." >&2
fi
}
# Warn (but proceed) when the key we are about to set already holds a foreign
# value, so the user can recover the overwritten path if it was intentional.
set_hook_config() {
local key="$1"
local current
current=$(git config --global --get "$key" 2>/dev/null || true)
if [ -n "$current" ] && [ "$current" != "$hook_dir" ]; then
echo "WARNING: overwriting existing global $key = $current" >&2
fi
git config --global "$key" "$hook_dir"
}
if [ "$use_template" = true ]; then
clear_conflicting core.hooksPath
set_hook_config init.templateDir
echo "Installed: init.templateDir set to $hook_dir"
echo "New repos created with git init/clone will copy these hooks."
else
clear_conflicting init.templateDir
set_hook_config core.hooksPath
echo "Installed: core.hooksPath set to $hook_dir"
echo "All repos will use hooks from $hook_dir."
fi