Repository navigation
Expand file tree
/
Copy pathupdate.sh
More file actions
executable file
·186 lines (164 loc) · 4.83 KB
/
Copy pathupdate.sh
File metadata and controls
executable file
·186 lines (164 loc) · 4.83 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
#!/usr/bin/env bash
set -euo pipefail
# shellcheck source=lib/bash/color.sh
source "$ENV/lib/bash/color.sh"
BREW=1
GC=
SKIP=()
OPTS=$(getopt -n "$0" -o bgs: -l gc,no-gc,brew,no-brew,skip: -- "$@")
eval set -- "$OPTS"
while true; do
case "$1" in
-g | --gc)
GC=1
shift
;;
--no-gc)
GC=
shift
;;
-b | --brew)
BREW=1
shift
;;
--no-brew)
BREW=
shift
;;
-s | --skip)
SKIP+=("$2")
shift 2
;;
--)
shift
break
;;
*)
echo "Internal error!"
exit 1
;;
esac
done
# Check if a directory path matches any skip pattern using backward matching
# Pattern examples:
# sing-box -> matches /any/path/sing-box or sing-box
# etc/sing-box -> matches /any/path/etc/sing-box or etc/sing-box
# nvim/env -> matches /any/path/nvim/env or nvim/env
matches_skip() {
local dir="$1"
local pattern
for pattern in "${SKIP[@]}"; do
# Exact match
if [[ "$dir" == "$pattern" ]]; then
return 0
fi
# Ends with /{pattern}
if [[ "$dir" == *"/$pattern" ]]; then
return 0
fi
done
return 1
}
# Resolve the default branch of a module's origin (main / master / ...)
default_branch() {
local module="$1"
local branch
# Prefer the symbolic ref recorded for origin/HEAD
branch=$(git -C "$module" symbolic-ref --short refs/remotes/origin/HEAD 2>/dev/null) || true
branch=${branch#origin/}
if [[ -n $branch ]]; then
echo "$branch"
return 0
fi
# Fallback: ask the remote for its HEAD branch
branch=$(git -C "$module" remote show origin 2>/dev/null |
sed -n 's/.*HEAD branch: //p')
if [[ -n $branch && $branch != "(unknown)" ]]; then
echo "$branch"
return 0
fi
# Last resort: pick whichever common branch exists on the remote
local candidate
for candidate in main master; do
if git -C "$module" show-ref --verify --quiet "refs/remotes/origin/$candidate"; then
echo "$candidate"
return 0
fi
done
return 1
}
# Wait up to 30s for the proxy to reach GitHub. Returns 0 on success, 1 on timeout.
# Uses the `proxy` CLI to wrap curl so we don't need to know the proxy URL here.
# --connect-timeout keeps a single hung attempt from eating the whole deadline.
wait_for_proxy() {
local deadline=$((SECONDS + 30))
if ! hash proxy >/dev/null 2>&1; then
warn "proxy command not found, skipping reachability check"
return 1
fi
while ((SECONDS < deadline)); do
if proxy curl -fsSX HEAD --connect-timeout 5 \
https://github.com/ >/dev/null 2>&1; then
return 0
fi
sleep 2
done
warn "GitHub unreachable via proxy after 30s, continuing with homebrew update"
return 1
}
# Final report on exit: print a completion timestamp and fire a macOS
# notification. Trapped on EXIT so failures are reported too — this script
# usually runs unattended and the outcome should be visible when you return.
report_exit() {
local status=$?
local ts elapsed msg
ts=$(date '+%Y-%m-%d %H:%M:%S')
elapsed="$((SECONDS / 60))m$((SECONDS % 60))s"
echo
if ((status == 0)); then
h1 "全部更新完成 @ ${ts}(耗时 ${elapsed})"
msg="env 更新完成(耗时 ${elapsed})"
else
h1 "更新失败(exit ${status})@ ${ts}(耗时 ${elapsed})"
msg="env 更新失败:exit ${status}"
fi
# 统一走 notify.sh:自动适配当前终端(cmux/iTerm2/ghostty/terminal-notifier)
"$ENV/bin/notify.sh" "env update.sh" "${msg} @ ${ts}" >/dev/null 2>&1 || true
exit "$status"
}
trap report_exit EXIT
rg submodule .gitmodules | sed 's/.*"\(.*\)".*/\1/' | sort |
while read -r MODULE; do
echo "-------- $MODULE --------"
BRANCH=$(default_branch "$MODULE") || {
warn "Cannot determine default branch for $MODULE, skipping"
continue
}
git -C "$MODULE" checkout "$BRANCH"
git -C "$MODULE" fetch --tags --prune --no-tags origin "$BRANCH"
git -C "$MODULE" rebase "origin/$BRANCH"
if [[ -n $GC ]]; then
git -C "$MODULE" gc
fi
done
fd -tf '^update.sh$' | while read -r MODULE; do
if [[ $MODULE == update.sh ]]; then
continue
fi
DIR=${MODULE%/*}
echo "-------- $DIR --------"
if [[ ${#SKIP[@]} -gt 0 ]] && matches_skip "$DIR"; then
warn "Skipping $DIR"
else
(cd "$DIR" && ./update.sh)
fi
done
if [[ -n $BREW ]]; then
echo "-------- proxy --------"
h1 "Waiting for proxy to reach GitHub (up to 30s)..."
wait_for_proxy || true
echo
echo "-------- homebrew --------"
brew-up.sh
brew-livecheck.sh --parallel
fi