-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·218 lines (185 loc) · 3.95 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·218 lines (185 loc) · 3.95 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#! /usr/bin/env bash
main_dir()
{
dirname "$(readlink -f ${BASH_SOURCE})"
}
backup_dir()
{
echo "$(main_dir)/.backup"
}
templates_dir()
{
echo "$(main_dir)/templates"
}
usage()
{
cat <<USAGE
./install.sh [OPTIONS]
Options:
-h display this help
-t globally configure git to use all templates
-u uninstall
By default, if the -t flag is not used, globally configure git to use ./templates/hooks
USAGE
}
GIT_BACKUP_FILE="$(backup_dir)/gitconfig"
INSTALL_TEMPLATES=1
INSTALLED_FILE="$(main_dir)/.installed"
declare -A GIT_CURRENT_CONFIGS=(
["core.hooksPath"]="$(git config --global --get core.hooksPath)"
["init.templateDir"]="$(git config --global --get init.templateDir)"
)
declare -A GIT_CONFIG_VALUES=(
["core.hooksPath"]="$(templates_dir)/hooks"
["init.templateDir"]="$(templates_dir)"
)
backup()
{
mkdir -p "$(backup_dir)"
backup_git_config
}
backup_git_config()
{
[ -e "${GIT_BACKUP_FILE}" ] || touch "${GIT_BACKUP_FILE}"
for key in "${!GIT_CURRENT_CONFIGS[@]}"; do
local value="${GIT_CURRENT_CONFIGS["${key}"]}"
if [ -n "${value}" ] && [[ "${value}" != "${GIT_CONFIG_VALUES["${key}"]}" ]]; then
if grep "${key}" "${GIT_BACKUP_FILE}" &>/dev/null; then
sed -i -re "s/^${key/./\\.}[^\n]+/${key} ${value//\//\\\/}/" "${GIT_BACKUP_FILE}"
else
echo "${key} ${value}" >> "${GIT_BACKUP_FILE}"
fi
fi
done
}
_detect_package_manager()
{
if [ -n "${TERMUX_VERSION:-}" ] || [ -d "/data/data/com.termux" ]; then
echo "termux"
elif command -v apt-get &>/dev/null; then
echo "apt"
else
echo "unknown"
fi
}
_try_install()
{
local -a cmd=("$@")
printf " Run: %s\n Install now? [y/N] " "${cmd[*]}" >&2
local answer
read -r answer </dev/tty
if [[ "${answer,,}" == "y" ]]; then
"${cmd[@]}" || {
echo "Error: installation failed — install libgit2 manually and re-run." >&2
return 1
}
else
echo " Skipped. The commit-msg hook will not validate messages until libgit2 is installed." >&2
fi
}
check_cog_dependency()
{
"$(main_dir)"/bin/cog --version &>/dev/null 2>&1 && return 0
echo "" >&2
echo "Warning: cog cannot run — libgit2 is missing." >&2
echo " The commit-msg hook requires libgit2 to validate commit messages." >&2
case "$(_detect_package_manager)" in
termux)
_try_install pkg install libgit2
;;
apt)
_try_install sudo apt-get install -y libgit2-dev
;;
*)
echo " Install libgit2 for your system then re-run this installer." >&2
echo " See: https://libgit2.org/" >&2
;;
esac
echo "" >&2
}
link_hooks()
{
for hook in "$(main_dir)"/src/git/hooks/*; do
ln -sf "${hook}" "$(templates_dir)"/hooks
done
}
check_git_config()
{
[ "$(git config --global "$1")" == "$2" ]
}
set_git_config()
{
git config --global "$1" "$2"
}
unset_git_config()
{
git config --global --unset "$1"
}
restore()
{
restore_git_config
}
restore_git_config()
{
if [ -f "${GIT_BACKUP_FILE}" ]; then
cat "${GIT_BACKUP_FILE}" | while read -r conf; do
[ -n "${conf}" ] && set_git_config "${conf% *}" "${conf#* }"
done
echo > "${GIT_BACKUP_FILE}"
fi
}
is_installed()
{
[ -f "${INSTALLED_FILE}" ]
}
install()
{
is_installed && uninstall
backup
link_hooks
check_cog_dependency
install_git
touch "${INSTALLED_FILE}"
}
install_git()
{
if [[ "${INSTALL_TEMPLATES}" -eq 0 ]]; then
check_git_config "core.hooksPath" "${GIT_CONFIG_VALUES[core.hooksPath]}" && unset_git_config "core.hooksPath"
set_git_config "init.templateDir" "$(templates_dir)"
else
check_git_config "init.templateDir" "${GIT_CONFIG_VALUES[init.templateDir]}" && unset_git_config "init.templateDir"
set_git_config "core.hooksPath" "$(templates_dir)/hooks"
fi
}
uninstall()
{
uninstall_git
restore
unlink "${INSTALLED_FILE}"
}
uninstall_git()
{
for key in "${!GIT_CURRENT_CONFIGS[@]}"; do
unset_git_config "${key}"
done
}
while getopts ":htu" option
do
case $option in
t)
INSTALL_TEMPLATES=0
;;
u)
is_installed && uninstall
exit $?
;;
h)
usage
exit 0
;;
\?) usage
exit 1
;;
esac
done
install