Repository navigation
Expand file tree
/
Copy pathrepo-status.sh
More file actions
executable file
·284 lines (247 loc) · 9.27 KB
/
Copy pathrepo-status.sh
File metadata and controls
executable file
·284 lines (247 loc) · 9.27 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
#!/usr/bin/env bash
#
# repo-status.sh - one line of state per git clone under a directory tree.
set -uo pipefail
usage() {
cat <<'HELP'
----------------------------------------
Dimitry4Now :: repo-status.sh
----------------------------------------
USAGE
repo-status.sh [OPTIONS] [DIRECTORY]
OPTIONS
-d, --dirty list only repos with uncommitted work or unpushed commits
-s, --size measure each repo on disk (slow: du walks every file)
-a, --all include repos under dotted directories (.oh-my-zsh, .nvm)
--sort KEY repo (default), sync, age or size
-r, --reverse flip the sort order
-h, --help show this help
SORT KEYS
repo path, A to Z
sync anything needing attention first, in-sync repos last
age most recently committed first
size largest on disk first; implies --size
DIRECTORY defaults to $HOME. Build and dependency directories are pruned -
node_modules, Library, .cache, .Trash, vendor, .venv, build, .gradle, Pods,
DerivedData, target, dist, .next - so this walks source trees rather than
everything on the disk. A repository that lives inside one of those (or that
is named after one) will not be listed.
COLUMNS
* uncommitted changes in the working tree
^ tree is clean but commits are unpushed
branch current branch, or "detached" / "no commits"
sync +N/-M commits ahead of / behind the upstream, = in sync,
"no upstream" when the branch tracks nothing
changes N tracked files modified, N? untracked files
size with -s: the whole working tree, dependency and build
directories included, which is where the bytes actually are
age author date of the last commit
Ahead/behind is measured against the last fetch. Nothing here touches the
network, so a stale +0/-0 only means you have not fetched lately.
HELP
}
die() {
printf '%s: %s\n' "${0##*/}" "$1" >&2
exit "${2:-2}"
}
# ---------------------------------------------------------------------------
# OPTIONS
# ---------------------------------------------------------------------------
dirty_only=0
show_all=0
size_on=0
sort_key=repo
reverse=0
while [ $# -gt 0 ]; do
case "$1" in
-d|--dirty) dirty_only=1 ;;
-a|--all) show_all=1 ;;
-s|--size) size_on=1 ;;
-r|--reverse) reverse=1 ;;
--sort)
[ $# -ge 2 ] || die "--sort needs a key (repo, sync, age or size)"
sort_key=$2
shift
;;
--sort=*) sort_key=${1#*=} ;;
-h|--help) usage; exit 0 ;;
--) shift; break ;;
-*) die "unknown option: $1 (try --help)" ;;
*) break ;;
esac
shift
done
root=${1:-$HOME}
shift 2>/dev/null || true
[ $# -eq 0 ] || die "unexpected extra argument: $1"
[ -d "$root" ] || die "not a directory: $root"
root=${root%/}
case "$sort_key" in
repo|sync|age) ;;
size) size_on=1 ;; # nothing to sort by unless we measure
*) die "unknown sort key: $sort_key (repo, sync, age or size)" ;;
esac
command -v git >/dev/null 2>&1 || die "git not found" 1
# ---------------------------------------------------------------------------
# MAIN
# ---------------------------------------------------------------------------
total=0
dirty_count=0
rows=""
width=46
total_kb=0
# du reports in 1K blocks on both BSD and GNU when handed -k.
human_kb() {
local k=$1
if [ "$k" -ge 1048576 ]; then printf '%d.%dG' $((k / 1048576)) $((k * 10 / 1048576 % 10))
elif [ "$k" -ge 1024 ]; then printf '%d.%dM' $((k / 1024)) $((k * 10 / 1024 % 10))
else printf '%dK' "$k"
fi
}
size_header=""
[ "$size_on" -eq 0 ] || size_header=$(printf '%8s ' 'SIZE')
printf ' %-*s %-24s %-12s %-12s %s%s\n' \
"$width" 'REPO' 'BRANCH' 'SYNC' 'CHANGES' "$size_header" 'AGE'
# Submodules keep a .git *file*, not a directory, so they are skipped here on
# purpose: their state belongs to the superproject's own line.
while IFS= read -r repo; do
label=${repo#"$root"/}
[ "$label" != "$repo" ] || label=${repo##*/}
# Tool checkouts living in dotted directories (.oh-my-zsh, .nvm, plugin
# marketplaces) are somebody else's code; they drown out your own work.
if [ "$show_all" -eq 0 ]; then
case "/$label/" in */.*/*) continue ;; esac
fi
status=$(git -C "$repo" status --porcelain -b 2>/dev/null) || continue
branch="?"
sync="-"
modified=0
untracked=0
while IFS= read -r line; do
case "$line" in
'## '*)
head=${line#'## '}
case "$head" in
'HEAD (no branch)')
branch="detached"
sync="-"
;;
'No commits yet on '*)
branch=${head#'No commits yet on '}
sync="no commits"
;;
*'...'*)
branch=${head%%...*}
# "main...origin/main [ahead 2, behind 1]"
case "$head" in
*'[ahead '*', behind '*)
ahead=${head#*'[ahead '}; ahead=${ahead%%,*}
behind=${head#*'behind '}; behind=${behind%%]*}
sync="+$ahead/-$behind"
;;
*'[ahead '*)
ahead=${head#*'[ahead '}; ahead=${ahead%%]*}
sync="+$ahead/-0"
;;
*'[behind '*)
behind=${head#*'[behind '}; behind=${behind%%]*}
sync="+0/-$behind"
;;
*) sync="=" ;;
esac
;;
*)
branch=$head
sync="no upstream"
;;
esac
;;
'??'*) untracked=$((untracked + 1)) ;;
'') ;;
*) modified=$((modified + 1)) ;;
esac
done <<EOF
$status
EOF
# Committer timestamp and the human phrasing in one call: the timestamp is
# what sorts, the phrasing is what prints.
last=$(git -C "$repo" log -1 --format='%ct %ar' 2>/dev/null)
stamp=${last%% *}
age=${last#* }
case "$stamp" in
''|*[!0-9]*) stamp=0; age="never" ;;
esac
flag=" "
is_dirty=0
if [ "$modified" -gt 0 ] || [ "$untracked" -gt 0 ]; then
flag="*"
is_dirty=1
fi
case "$sync" in
+0/-*) ;;
+*) is_dirty=1
[ "$flag" = "*" ] || flag="^"
;;
esac
total=$((total + 1))
[ "$is_dirty" -eq 0 ] || dirty_count=$((dirty_count + 1))
[ "$dirty_only" -eq 0 ] || [ "$is_dirty" -eq 1 ] || continue
# Trim from the left, not the right: the repository name is the last
# component, and two siblings under a long parent are indistinguishable if
# you cut the tail off instead.
if [ ${#label} -gt "$width" ]; then
label="...${label:$((${#label} - width + 3))}"
fi
# Whole working tree, dependency and build directories included: the point
# of this column is deciding what to delete, and that is where the bytes are.
size_col=""
kb=0
if [ "$size_on" -eq 1 ]; then
kb=$(du -sk "$repo" 2>/dev/null | while read -r n _; do printf '%s' "$n"; break; done)
[ -n "${kb:-}" ] || kb=0
total_kb=$((total_kb + kb))
size_col=$(printf '%8s ' "$(human_kb "$kb")")
fi
# Every row carries a sort key in a leading tab-separated field, stripped
# again by cut once sort has done its work. Numbers are zero-padded so a
# plain lexical sort orders them correctly.
case "$sort_key" in
repo) key=$label ;;
size) key=$(printf '%012d %s' "$kb" "$label") ;;
age) key=$(printf '%012d %s' "$stamp" "$label") ;;
sync) case "$sync" in
'=') key="1 $label" ;; # in sync sorts last
*) key="0 $label" ;;
esac
;;
esac
row=$(printf '%s%-*.*s %-24.24s %-12s %-12s %s%s' \
"$flag" "$width" "$width" "$label" "$branch" "$sync" \
"${modified}M ${untracked}?" "$size_col" "$age")
rows="$rows$key$(printf '\t')$row
"
done <<EOF
$(find "$root" \
\( -name node_modules -o -name Library -o -name .Trash -o -name .cache \
-o -name vendor -o -name .venv -o -name build -o -name .gradle \
-o -name Pods -o -name DerivedData -o -name target -o -name dist \
-o -name .next \) -prune \
-o -type d -name .git -print 2>/dev/null \
| sed 's|/\.git$||' | sort)
EOF
# Descending is the useful direction for the two numeric keys: biggest repo
# and most recently touched repo are what you came to look at.
sort_flags=""
case "$sort_key" in
size|age) sort_flags="-r" ;;
esac
[ "$reverse" -eq 0 ] || {
[ -n "$sort_flags" ] && sort_flags="" || sort_flags="-r"
}
[ -z "$rows" ] || printf '%s' "$rows" | sort $sort_flags | cut -f2-
if [ "$size_on" -eq 1 ]; then
printf '\n%d repos, %d needing attention, %s on disk\n' \
"$total" "$dirty_count" "$(human_kb "$total_kb")"
else
printf '\n%d repos, %d needing attention\n' "$total" "$dirty_count"
fi