-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcheck-for-updates.sh
80 lines (69 loc) · 2.54 KB
/
check-for-updates.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
#!/bin/bash
#Based on https://github.com/ohmyzsh/ohmyzsh/blob/master/tools/check_for_upgrade.sh
if [[ "$OMC_DISABLE_AUTO_UPDATE" = true ]] \
|| ! command -v git &>/dev/null; then
return
fi
OMC_EPOCH=$(date +%s)
set +x
function current_epoch() {
echo $(($OMC_EPOCH/60/60/24))
}
function update_last_updated_file() {
echo "LAST_EPOCH=$(current_epoch)" > "${OH_MY_COMMA_PATH}/log/.omc-update"
}
function omc_delete_update_lock() {
rm -rf '$OH_MY_COMMA_PATH/log/update.lock' || return 1
}
# Remove lock directory if older than a day
if mtime=$(date +%s -r "$OH_MY_COMMA_PATH/log/update.lock" 2>/dev/null); then
if (( (mtime + 3600 * 24) < OMC_EPOCH )); then
command rm -rf "$OH_MY_COMMA_PATH/log/update.lock"
fi
fi
# Check for lock directory
if ! command mkdir "$OH_MY_COMMA_PATH/log/update.lock" 2>/dev/null; then
return
fi
# Remove lock directory on exit. `return 1` is important for when trapping a SIGINT:
# The return status from the function is handled specially. If it is zero, the signal is
# assumed to have been handled, and execution continues normally. Otherwise, the shell
# will behave as interrupted except that the return status of the trap is retained.
omc_delete_update_lock
# Create or update .omc-update file if missing or malformed
if ! source "${OH_MY_COMMA_PATH}/log/.omc-update" 2>/dev/null || [[ -z "$LAST_EPOCH" ]]; then
touch ${OH_MY_COMMA_PATH}/log/.omc-update
update_last_updated_file
fi
# Number of days before trying to update again
epoch_target=${OMC_AUTOUPDATE_DAYS:-7}
# Test if enough time has passed until the next update
if (( ( $(current_epoch) - LAST_EPOCH ) < $epoch_target )); then
return
fi
cd ${OH_MY_COMMA_PATH}
git fetch
OMC_UPSTREAM=${1:-'@{u}'}
OMC_LOCAL=$(git rev-parse @)
OMC_REMOTE=$(git rev-parse "$OMC_UPSTREAM")
if [ $OMC_LOCAL != $OMC_REMOTE ]; then
# Ask for confirmation before updating unless disabled
if [[ "$OMC_DISABLE_UPDATE_PROMPT" = true ]]; then
emu update
else
echo "[emu.sh] Current OMC branch:"
echo "$(git branch | head -n 1)"
echo "$(git status | head -n 2 | tail -n 1)"
# input sink to swallow all characters typed before the prompt
# and add a newline if there wasn't one after characters typed
read -r -p "[emu.sh] Update .oh-my-comma? [Y/n] " option
[[ "$option" != $'\n' ]] && echo
case "$option" in
[yY$'\n']) emu update && update_last_updated_file ;;
[nN]) update_last_updated_file ;;
*) emu update ;;
esac
fi
fi
cd -
unset -f current_epoch update_last_updated_file