-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·167 lines (139 loc) · 2.8 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
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
#! /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
}
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
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