-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate
421 lines (354 loc) · 10.9 KB
/
update
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#!/bin/bash
#shellcheck disable=SC2184 # Quote arguments to unset so they're not glob expanded.
#shellcheck disable=SC2088 # Tilde does not expand in quotes. Use $HOME.
#shellcheck disable=SC2053 # Quote the right-hand side of != in [[ ]] to prevent glob matching.
#shellcheck disable=SC2091 # Remove surrounding $() to avoid executing output (or use eval if intentional).
#shellcheck disable=SC2086 # Double quote to prevent globbing and word splitting.
#shellcheck disable=SC2181 # Check exit code directly with e.g. 'if mycmd;', not indirectly with $?.
#shellcheck disable=SC2119 # Use mucl "$@" if function's $1 should mean script's $1
#shellcheck disable=SC2155 # Declare and assign separately to avoid masking return values.
##################
# util functions #
##################
SUCCESS=0
FAILURE=1
MOVE_UP=$(tput cuu 1)
CLEAR_LINE=$(tput el 1)
function mucl() {
if [[ $# -eq 0 ]]; then
numIterations=1
else
numIterations=${1}
fi
for (( i = 0; i < numIterations; i++)); do
echo "${MOVE_UP}${CLEAR_LINE}${MOVE_UP}"
done
}
currDir="$(pwd)"
function apply_tput() {
case "${1}" in
black) tput setaf 0
;;
red) tput setaf 1
;;
green) tput setaf 2
;;
yellow) tput setaf 3
;;
blue) tput setaf 4
;;
magenta) tput setaf 5
;;
cyan) tput setaf 6
;;
white) tput setaf 7
;;
reset) tput sgr0
;;
*) tput "${1}"
;;
esac
}
# Usage: insert_new_lines 3
function insert_new_lines() {
for (( i = 0; i < ${1}; i++)); do
echo
done
}
# Usage: banner_color 1 "red" "*" "my title" 2
function banner_color() {
insert_new_lines ${1}
apply_tput "${2}"
apply_tput bold
local msg="${3} ${4} ${3}"
local edge
edge=${msg//?/$3}
echo "${edge}"
echo "${msg}"
printf "%s" "${edge}"
apply_tput reset
insert_new_lines ${5}
}
# Usage: banner_color_err 1 "error message" 2
function banner_color_err() {
banner_color ${1} "red" "~" "[error] ${2}" ${3}
}
# Usage: print_as 3 "blue" "msg" 1
function print_as() {
insert_new_lines ${1}; shift
while [[ $# -gt 2 ]]; do
apply_tput "${1}"; shift
done
local msg="${1}"; shift
printf "%s" "${msg}"
apply_tput reset
insert_new_lines ${1};
}
# Usage: print_info 3 "msg" 2
function print_info() {
print_as ${1} "blue" "${2}" ${3}
}
# Usage: print_err 3 "msg" 2
function print_err() {
print_as ${1} "red" "${2}" ${3}
}
function is_command_installed() {
if command -v "${1}" &>/dev/null; then
echo true
else
echo false
fi
}
function ask() {
while true; do
read -p "${1} ([y]/n) " -r
REPLY=${REPLY:-"y"}
if [[ $REPLY =~ ^[Yy]$ ]]; then
return 0
elif [[ $REPLY =~ ^[Nn]$ ]]; then
return 1
fi
done
}
function append_line() {
set -e
local line="${1}" file="${2}" pat="${3:-}" lno=""
if [ -f "$file" ]; then
if [ $# -lt 4 ]; then
lno=$(grep -nF "$line" "$file" | sed 's/:.*//' | tr '\n' ' ')
else
lno=$(grep -nF "$pat" "$file" | sed 's/:.*//' | tr '\n' ' ')
fi
fi
if [ -z "$lno" ]; then
[ -f "$file" ] && echo >> "$file"
echo "$line" >> "$file"
fi
set +e
}
function remove_file() {
local file="${1}"
[ -f "${file}" ] && {
print_info 0 "removing ${file}" 1
rm -f "${file}"
}
}
function remove_line() {
src=$(readlink "$1")
if [ $? -eq 0 ]; then
print_info 0 "removing from ${1} ($src):" 1
else
src=$1
print_info 0 "removing from ${1}:" 1
fi
shift
line_no=1
match=0
while [ -n "$1" ]; do
line=$(sed -n "$line_no,\$p" "$src" | \grep -m1 -nF "$1")
if [ $? -ne 0 ]; then
shift
line_no=1
continue
fi
line_no=$(( $(sed 's/:.*//' <<< "$line") + line_no - 1 ))
content=$(sed 's/^[0-9]*://' <<< "$line")
match=1
echo " - line #$line_no: $content"
[ "$content" = "$1" ] || ask " - remove?"
if [ $? -eq 0 ]; then
awk -v n=$line_no 'NR == n {next} {print}' "$src" > "$src.bak" &&
mv "$src.bak" "$src" || break
echo " - removed"
else
echo " - skipped"
line_no=$(( line_no + 1 ))
fi
done
[ $match -eq 0 ] && echo " - nothing found"
echo
}
# Usage: assert_equal A B msg
function assert_equal() {
local A="${1}" B="${2}" msg="${3}"
[[ "${A}" != "${B}" ]] && {
mucl
banner_color_err 0 "${msg}" 2
exit ${FAILURE}
}
}
# Usage: assert_success msg
function assert_success() {
local status="$?" msg="${1}"
assert_equal "${status}" ${SUCCESS} "${msg}"
}
function get_latest_annotated_tag() {
git describe --abbrev=0 &> /dev/null
if (( $? != SUCCESS )); then # there are no tags
echo ""
return
fi
git describe --abbrev=0
}
function install_clipboard() {
if ! $(is_command_installed "npm"); then
banner_color_err 1 "npm not installed." 1
return ${FAILURE}
fi
print_info 1 "installing clipboard-cli ..." 1
npm install -g clipboard-cli &> /dev/null
if (( $? != SUCCESS )); then
mucl 1
banner_color 0 "black" "~" "couldn't install clipboard-cli" 1
return ${FAILURE}
fi
mucl 1
banner_color 0 "magenta" "*" "clipboard-cli installed" 1
}
function install_fzf() {
print_info 1 "installing fzf ..." 1
git clone --depth 1 https://github.com/junegunn/fzf.git "${FZF_HOME}" &> /dev/null
if (( $? != SUCCESS )); then
mucl
banner_color_err 0 "couldn't fetch fzf! please check your network" 1
return ${FAILURE}
fi
cd "${FZF_HOME}" || {
mucl
banner_color_err 0 "couldn't enter directory: ${FZF_HOME}" 1
return ${FAILURE}
}
./install --all &> /dev/null
if (( $? != SUCCESS )); then
mucl
banner_color_err 0 "fzf installation failed" 1
return ${FAILURE}
fi
mucl
banner_color 0 "magenta" "*" "fzf installed" 1
}
function handle_v0.1.0_install_issue() {
local currVersion="$(get_latest_annotated_tag)"
if [[ "${currVersion}" == "" || "${currVersion}" == "v0.1.0" ]]; then
for shell in "${shells[@]}"; do
local initFile="${CUSTOM_GIT_HOME}.${shell}"
[ -f "${initFile}" ] && {
remove_file "${initFile}"
remove_line ~/.${shell}rc \
"[ -f ${prefix}.${shell} ] && source ${prefix}.${shell}" \
"source ${prefix}.${shell}"
}
done
local clone_depth=$(git rev-list --count --all)
if (( clone_depth == 1 )); then
git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'
git fetch --unshallow &> /dev/null
fi
fi
}
function finish {
cd "${currDir}" || {
print_info 0 "couldn't enter the directory: ${currDir}" 2
exit ${FAILURE}
}
}
trap finish EXIT
#####################
# Update custom-git #
#####################
shells=("bash" "zsh")
numShells=${#shells[@]}
for ((i = 0 ; i < numShells ; i++)); do
shell="${shells[$i]}"
if ! $(is_command_installed "${shell}"); then
unset shells[$i]
fi
done
if [[ ${#shells[@]} -eq 0 ]]; then
banner_color_err 0 "custom-git is only supported for bash and zsh shells" 2
print_info 0 "visit " 0
print_as 0 "magenta" "bold" "https://custom-git.io" 0
print_as 0 "blue" " for more information." 2
exit ${FAILURE}
fi
prefix='~/.custom-git'
CUSTOM_GIT_HOME="${HOME}"/.custom-git
FZF_HOME="${HOME}"/.fzf
CUSTOM_GIT_BASH_REPO_URL="https://github.com/custom-git/custom-git-bash"
CUSTOM_GIT_WEBSITE_URL="https://custom-git.io"
if [[ ! -d "${CUSTOM_GIT_HOME}" ]]; then
banner_color_err 0 "custom-git is not installed on your system." 2
print_info 0 "visit " 0
print_as 0 "magenta" "bold" "https://custom-git.io" 0
print_as 0 "blue" " for quick installation." 2
exit ${FAILURE}
fi
if ! $(is_command_installed "clipboard"); then
install_clipboard
if (( $? != SUCCESS )); then
print_info 0 "please install clipboard-cli from " 0
print_as 0 "magenta" "bold" "https://github.com/sindresorhus/clipboard-cli#install" 1
print_info 0 "some custom-git features will not work without it" 1
print_info 0 "for more details, visit: " 0
print_as 0 "magenta" "bold" "${CUSTOM_GIT_WEBSITE_URL}/explorer/show/commit-logs" 1
fi
fi
if ! $(is_command_installed "fzf"); then
install_fzf
if (( $? != SUCCESS )); then
print_info 0 "please try again after installing fzf from " 0
print_as 0 "magenta" "bold" "https://github.com/junegunn/fzf#installation" 2
exit ${FAILURE}
fi
fi
cd "${CUSTOM_GIT_HOME}" || {
banner_color_err 0 "update failure! couldn't enter the directory: ${CUSTOM_GIT_HOME}" 2
exit ${FAILURE}
}
handle_v0.1.0_install_issue
currVersion="$(get_latest_annotated_tag)"
git switch main &> /dev/null
git pull --quiet
git fetch --all --tags --prune --prune-tags --force --quiet # reference:- https://stackoverflow.com/a/49215190
assert_success "couldn't fetch custom-git! please check your network"
latestVersion="$(get_latest_annotated_tag)"
if [[ "${currVersion}" == "${latestVersion}" ]]; then
git checkout "${currVersion}" &> /dev/null
print_info 0 "(${currVersion}) already up to date" 1
exit ${SUCCESS}
fi
print_info 1 "updating - custom-git ${latestVersion}..." 1
git checkout "${latestVersion}" &> /dev/null
assert_success "couldn't switch to new version! please check your network"
git submodule update --init --recursive &> /dev/null
assert_success "couldn't update submodule! please check your network"
newVersion="$(get_latest_annotated_tag)"
assert_equal "${newVersion}" "${latestVersion}" \
"couldn't update custom-git to ${latestVersion}! unknown error"
for shell in "${shells[@]}"; do
[ $shell = zsh ] && dest=${ZDOTDIR:-~}/.zshrc || dest=~/.bashrc
append_line "[ -f ${prefix}/init.${shell} ] && source ${prefix}/init.${shell}" \
"$dest" "source ${prefix}/init.${shell}"
done
mucl
banner_color 0 "magenta" "*" "update complete - custom-git ${latestVersion}" 1
print_info 0 "to view the updates, visit: " 0
print_as 0 "magenta" "bold" "${CUSTOM_GIT_BASH_REPO_URL}/releases" 1
print_info 1 "to apply the updates, restart your shell session or reload the config file with:" 2
for shell in "${shells[@]}"; do
case "${shell}" in
bash)
printf "$ source ~/.bashrc # bash shell"
archi=$(uname -sm)
[[ "$archi" =~ Darwin ]] && printf " (~/.bashrc should be sourced from ~/.bash_profile)"
echo
;;
zsh)
echo "$ source ${ZDOTDIR:-~}/.zshrc # zsh shell"
;;
*)
printf "\n%s shell is not supported.\n" "${shell}"
;;
esac
done
echo