Repository navigation
Expand file tree
/
Copy pathclonewatch
More file actions
executable file
·289 lines (253 loc) · 8.78 KB
/
Copy pathclonewatch
File metadata and controls
executable file
·289 lines (253 loc) · 8.78 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
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
#!/usr/bin/env bash
#
# clonewatch - live stats for git clones/fetches/submodules already in flight.
#
# Read-only: it inspects processes and file sizes, it never touches a clone.
#
# Exit codes:
# 0 watched a transfer until it finished, or --once printed a snapshot
# 1 nothing was transferring when we started
# 2 usage error
# No `set -e` here on purpose: half of this script is greps that legitimately
# find nothing, and every one of those would abort the run.
set -uo pipefail
usage() {
cat <<'HELP'
----------------------------------------
Dimitry4Now :: clonewatch
----------------------------------------
USAGE
clonewatch [OPTIONS]
OPTIONS
-1, --once print one snapshot and exit
-i, --interval SEC seconds between refreshes (default 2)
-h, --help show this help
One line per pack being written, redrawn in place. Git never announces the
total size of a transfer, so there is no percentage or ETA - only bytes so
far, throughput, and phase.
HELP
}
die() {
printf '%s: %s\n' "${0##*/}" "$1" >&2
exit "${2:-2}"
}
# ---------------------------------------------------------------------------
# OPTIONS
# Add script-specific option handling here.
# ---------------------------------------------------------------------------
interval=2
once=0
while [ $# -gt 0 ]; do
case "$1" in
-1|--once) once=1 ;;
-i|--interval)
[ $# -ge 2 ] || die "--interval needs a value"
interval=$2
shift
;;
--interval=*) interval=${1#*=} ;;
-h|--help) usage; exit 0 ;;
--) shift; break ;;
-*) die "unknown option: $1 (try --help)" ;;
*) break ;;
esac
shift
done
case "$interval" in
''|*[!0-9]*) die "--interval must be a whole number of seconds" ;;
esac
[ "$interval" -ge 1 ] || die "--interval must be at least 1 second"
# ---------------------------------------------------------------------------
# PROBES
# The two things that differ between systems: how to size a file, and how to
# ask what a process has open.
# ---------------------------------------------------------------------------
# Probe with -c, not -f: on GNU, -f means --file-system and `stat -f %z` prints
# "?" and exits 0, so probing that way picks the wrong branch on Linux.
if stat -c %s / >/dev/null 2>&1; then
filesize() { stat -c %s "$1" 2>/dev/null || echo 0; } # GNU / Linux
else
filesize() { stat -f %z "$1" 2>/dev/null || echo 0; } # BSD / macOS
fi
if [ -d "/proc/$$/fd" ]; then
open_pack() {
local fd target
for fd in "/proc/$1/fd"/*; do
target=$(readlink "$fd" 2>/dev/null) || continue
case "$target" in
*/objects/pack/*.idx) ;;
*/objects/pack/*) printf '%s\n' "$target"; return ;;
esac
done
}
proc_cwd() { readlink "/proc/$1/cwd" 2>/dev/null; }
else
open_pack() {
lsof -p "$1" -Fn 2>/dev/null \
| sed -n 's|^n\(.*/objects/pack/.*\)|\1|p' \
| grep -v '\.idx$' \
| head -n 1
}
proc_cwd() {
lsof -a -p "$1" -d cwd -Fn 2>/dev/null | sed -n 's/^n//p' | head -n 1
}
fi
# ---------------------------------------------------------------------------
# FORMATTING
# ---------------------------------------------------------------------------
human() {
local b=$1
if [ "$b" -ge 1073741824 ]; then printf '%d.%dG' $((b / 1073741824)) $((b * 10 / 1073741824 % 10))
elif [ "$b" -ge 1048576 ]; then printf '%d.%dM' $((b / 1048576)) $((b * 10 / 1048576 % 10))
elif [ "$b" -ge 1024 ]; then printf '%dK' $((b / 1024))
else printf '%dB' "$b"
fi
}
# /path/repo/.git/objects/pack/tmp_pack_x -> repo
# /path/super/.git/modules/lib/foo/objects/... -> super:lib/foo
label_for_pack() {
local gitdir=${1%/objects/pack/*} super sub
case "$gitdir" in
*/.git/modules/*)
super=${gitdir%%/.git/modules/*}
sub=${gitdir#*/.git/modules/}
printf '%s:%s' "${super##*/}" "$sub"
;;
*/.git)
gitdir=${gitdir%/.git}
printf '%s' "${gitdir##*/}"
;;
*) printf '%s' "${gitdir##*/}" ;; # bare repo
esac
}
row() { printf '%-30.30s %8s %10s %-17s %s' "$1" "$2" "$3" "$4" "$5"; }
# ---------------------------------------------------------------------------
# SNAPSHOT
# Pack writers are the only processes that know how far a transfer has got, so
# they get the detailed lines. When none exist we are either negotiating or
# checking out, and the user-level git command gets one line instead.
# ---------------------------------------------------------------------------
# Matched against the command line only, so `git` has to sit in a command
# position: this keeps out gitstatusd, `bash -c "git clone ..."` wrappers and a
# script of our own called git-clonewatch, while still catching both
# /libexec/git-core/git-submodule and a bare `git submodule--helper`.
WORKERS='(^|/)git[ -](index-pack|unpack-objects)([^a-z]|$)'
COMMANDS='(^|/)git[ -](clone|fetch|pull|submodule)([^a-z]|$)'
STATE=$(mktemp "${TMPDIR:-/tmp}/clonewatch.XXXXXX") || die "cannot create state file" 1
snapshot() {
local now pid cpu etime args pack size phase rate prev prev_size prev_ts dt db
local next="" found=0 procs
now=$(date +%s)
procs=$(ps -A -o pid=,%cpu=,etime=,args= 2>/dev/null)
while read -r pid cpu etime args; do
[[ $args =~ $WORKERS ]] || continue
pack=$(open_pack "$pid")
[ -n "$pack" ] || continue # opened the connection, no pack yet
found=1
size=$(filesize "$pack")
prev=$(grep "^$pid " "$STATE" 2>/dev/null | head -n 1)
if [ -n "$prev" ]; then
prev=${prev#* }
prev_size=${prev%% *}
prev_ts=${prev##* }
dt=$((now - prev_ts))
[ "$dt" -gt 0 ] || dt=1
db=$((size - prev_size))
else
db=-1 # first tick: no baseline yet
dt=1
fi
if [ "$db" -gt 0 ]; then
phase="downloading"
rate="$(human $((db / dt)))/s"
elif [ "$db" -lt 0 ]; then
phase="starting"
rate="-"
elif [ "${cpu%%.*}" -ge 20 ]; then
phase="resolving deltas"
rate="-"
else
phase="stalled?"
rate="-"
fi
row "$(label_for_pack "$pack")" "$(human "$size")" "$rate" "$phase" "$etime"
printf '\n'
next="$next$pid $size $now
"
done <<EOF
$procs
EOF
printf '%s' "$next" > "$STATE"
[ "$found" -eq 0 ] || return 0
# No pack in flight: report what the user-level git commands are up to.
# One clone is several processes (git-submodule, submodule--helper, ...),
# so collapse them to one line per repository.
local cwd label listed=" "
while read -r pid cpu etime args; do
[[ $args =~ $COMMANDS ]] || continue
case "$args" in
*checkout*) phase="checking out" ;;
*submodule*) phase="submodules" ;;
*) phase="negotiating" ;;
esac
cwd=$(proc_cwd "$pid")
label=${cwd##*/}
case "$listed" in *" $label "*) continue ;; esac
listed="$listed$label "
row "$label" "-" "-" "$phase" "$etime"
printf '\n'
done <<EOF
$procs
EOF
}
# ---------------------------------------------------------------------------
# RENDER
# In a terminal, redraw the same block every tick; in a pipe, just append.
# ---------------------------------------------------------------------------
tty=0
[ -t 1 ] && tty=1
cols=$(tput cols 2>/dev/null) || cols=80
drawn=0
if [ "$tty" -eq 1 ]; then
printf '\033[?25l'
trap 'printf "\033[?25h"; rm -f "$STATE"' EXIT
else
trap 'rm -f "$STATE"' EXIT
fi
render() {
local line printed=0
if [ "$tty" -eq 0 ]; then
printf '%s\n' "$1"
return
fi
[ "$drawn" -gt 0 ] && printf '\033[%dA' "$drawn"
while IFS= read -r line; do
printf '\033[2K%s\n' "${line:0:$cols}"
printed=$((printed + 1))
done <<EOF
$1
EOF
# A shrinking block leaves stale lines behind; blank them and keep counting
# them, so the next cursor-up lands in the same place.
while [ "$printed" -lt "$drawn" ]; do
printf '\033[2K\n'
printed=$((printed + 1))
done
drawn=$printed
}
# ---------------------------------------------------------------------------
# MAIN
# ---------------------------------------------------------------------------
seen=0
while :; do
out=$(snapshot)
if [ -z "$out" ]; then
[ "$seen" -eq 1 ] || die "no git clone, fetch or submodule transfer in progress" 1
render "done"
exit 0
fi
seen=1
render "$out"
[ "$once" -eq 0 ] || exit 0
sleep "$interval"
done