tg-summary: the last of the great speed-ups
[topgit/pro.git] / tg-summary.sh
blob0879803768e5d68cbe08eb10fbd50c5a4698647f
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # Copyright (C) Petr Baudis <pasky@suse.cz> 2008
4 # Copyright (C) Kyle J. McKay <mackyle@gmail.com> 2015,2016,2017
5 # All rights reserved.
6 # GPLv2
8 terse=
9 graphviz=
10 sort=
11 deps=
12 depsonly=
13 rdeps=
14 rdepsonce=1
15 head_from=
16 branches=
17 head=
18 heads=
19 headsindep=
20 headsonly=
21 exclude=
22 tgish=
23 withdeps=
24 verbose=0
26 ## Parse options
28 usage()
30 echo "Usage: ${tgname:-tg} [...] summary [-t | --list | --heads[-only] | --sort | --deps[-only] | --rdeps | --graphviz] [-i | -w] [--tgish-only] [--with[out]-(deps|related)] [--exclude branch]... [--all | branch...]" >&2
31 exit 1
34 while [ -n "$1" ]; do
35 arg="$1"
36 case "$arg" in
37 -i|-w)
38 [ -z "$head_from" ] || die "-i and -w are mutually exclusive"
39 head_from="$arg";;
40 -t|--list|-l|--terse)
41 terse=1;;
42 -v|--verbose)
43 verbose=$(( $verbose + 1 ));;
44 --heads|--topgit-heads)
45 heads=1
46 headsindep=;;
47 --heads-independent)
48 heads=1
49 headsindep=1;;
50 --heads-only)
51 headsonly=1;;
52 --with-deps)
53 head=HEAD
54 withdeps=1;;
55 --with-related)
56 head=HEAD
57 withdeps=2;;
58 --without-deps|--no-with-deps|--without-related|--no-with-related)
59 head=HEAD
60 withdeps=0;;
61 --graphviz)
62 graphviz=1;;
63 --sort)
64 sort=1;;
65 --deps)
66 deps=1;;
67 --tgish-only)
68 tgish=1;;
69 --deps-only)
70 head=HEAD
71 depsonly=1;;
72 --rdeps)
73 head=HEAD
74 rdeps=1;;
75 --rdeps-full)
76 head=HEAD
77 rdeps=1 rdepsonce=;;
78 --rdeps-once)
79 head=HEAD
80 rdeps=1 rdepsonce=1;;
81 --all)
82 break;;
83 --exclude=*)
84 [ -n "${1#--exclude=}" ] || die "--exclude= requires a branch name"
85 exclude="$exclude ${1#--exclude=}";;
86 --exclude)
87 shift
88 [ -n "$1" -a "$1" != "--all" ] || die "--exclude requires a branch name"
89 exclude="$exclude $1";;
90 -*)
91 usage;;
93 break;;
94 esac
95 shift
96 done
97 [ $# -eq 0 ] || defwithdeps=1
98 [ -z "$exclude" ] || exclude="$exclude "
99 doingall=
100 [ $# -ne 0 ] || [ z"$head" != z"" ] || doingall=1
101 if [ "$1" = "--all" ]; then
102 [ -z "$withdeps" ] || die "mutually exclusive options given"
103 [ $# -eq 1 ] || usage
104 shift
105 head=
106 defwithdeps=
107 doingall=1
109 [ "$heads$rdeps" != "11" ] || head=
110 [ $# -ne 0 -o -z "$head" ] || set -- "$head"
111 [ -z "$defwithdeps" ] || [ $# -ne 1 ] || [ z"$1" != z"HEAD" -a z"$1" != z"@" ] || defwithdeps=2
113 [ "$terse$heads$headsonly$graphviz$sort$deps$depsonly" = "" ] ||
114 [ "$terse$heads$headsonly$graphviz$sort$deps$depsonly$rdeps" = "1" ] ||
115 [ "$terse$heads$headsonly$graphviz$sort$deps$depsonly$rdeps" = "11" -a "$heads$rdeps" = "11" ] ||
116 die "mutually exclusive options given"
117 [ -z "$withdeps" -o -z "$rdeps$depsonly$heads$headsonly" ] ||
118 die "mutually exclusive options given"
120 for b; do
121 [ "$b" != "--all" ] || usage
122 branches="$branches $(verify_topgit_branch "$b")"
123 done
125 get_branch_list()
127 if [ -n "$branches" ]; then
128 if [ -n "$1" ]; then
129 printf '%s\n' $branches | sort -u
130 else
131 printf '%s\n' $branches
133 else
134 non_annihilated_branches
138 show_heads_independent()
140 topics="$(get_temp topics)"
141 get_branch_list | sed -e 's,^\(.*\)$,refs/heads/\1 \1,' |
142 git cat-file --batch-check='%(objectname) %(rest)' |
143 sort -u -b -k1,1 >"$topics"
144 git merge-base --independent $(cut -d ' ' -f 1 <"$topics") |
145 sort -u -b -k1,1 | join - "$topics" | sort -u -b -k2,2 |
146 while read rev name; do
147 case "$exclude" in *" $name "*) continue; esac
148 printf '%s\n' "$name"
149 done
152 show_heads_topgit()
154 if [ -n "$branches" ]; then
155 navigate_deps -s=-1 -1 -- "$branches" | sort
156 else
157 navigate_deps -s=-1
158 fi |
159 while read -r name; do
160 case "$exclude" in *" $name "*) continue; esac
161 printf '%s\n' "$name"
162 done
165 show_heads()
167 if [ -n "$headsindep" ]; then
168 show_heads_independent "$@"
169 else
170 show_heads_topgit "$@"
174 if [ -n "$heads" -a -z "$rdeps" ]; then
175 show_heads
176 exit 0
179 skip_ann=
180 show_dep() {
181 case "$exclude" in *" $_dep "*) return; esac
182 case " $seen_deps " in *" $_dep "*) return 0; esac
183 seen_deps="${seen_deps:+$seen_deps }$_dep"
184 [ -z "$tgish" -o -n "$_dep_is_tgish" ] || return 0
185 [ -z "$skip_ann" ] || [ -z "$_dep_annihilated" ] && printf '%s\n' "$_dep"
186 return 0
189 show_deps()
191 no_remotes=1
192 recurse_deps_exclude=
193 get_branch_list | while read _b; do
194 case "$exclude" in *" $_b "*) continue; esac
195 case " $recurse_deps_exclude " in *" $_b "*) continue; esac
196 seen_deps=
197 save_skip="$skip_ann"
198 _dep="$_b"; _dep_is_tgish=1; skip_ann=; show_dep; skip_ann="$save_skip"
199 recurse_deps show_dep "$_b"
200 recurse_deps_exclude="$recurse_deps_exclude $seen_deps"
201 done
204 if [ -n "$depsonly" ]; then
205 show_deps | sort -u -b -k1,1
206 exit 0
209 show_rdeps()
211 case "$exclude" in *" $_dep "*) return; esac
212 [ -z "$tgish" -o -n "$_dep_is_tgish" ] || return 0
213 elided=
214 [ -z "$rdepsonce" ] || [ "$_dep_xvisits" = 0 ] || elided="^"
215 printf '%s %s\n' "$_depchain" "$_dep$elided"
218 if [ -n "$rdeps" ]; then
219 no_remotes=1
220 showbreak=
222 if [ -n "$heads" ]; then
223 show_heads
224 else
225 get_branch_list
227 } | while read b; do
228 case "$exclude" in *" $b "*) continue; esac
229 [ -z "$showbreak" ] || echo
230 showbreak=1
231 ref_exists "refs/heads/$b" || continue
233 echol "$b"
234 recurse_preorder=1
235 recurse_deps ${rdepsonce:+-o=-o=-1} show_rdeps "$b"
236 } | sed -e 's/[^ ][^ ]*[ ]/ /g'
237 done
238 exit 0
241 if [ -n "$deps" ]; then
242 if [ -n "$branches" ]; then
243 no_remotes=1
244 recurse_deps_exclude="$exclude"
245 recurse_deps_internal -n -t -m -e=2 -- $branches | sort -u
246 else
247 refslist=
248 [ -z "$tg_read_only" ] || [ -z "$tg_ref_cache" ] || ! [ -s "$tg_ref_cache" ] ||
249 refslist="-r=\"$tg_ref_cache\""
250 tdopt=
251 v_get_tdopt tdopt "$head_from"
252 eval run_awk_topgit_deps "$refslist" "$tdopt" '-n -t -x="$exclude" "refs/$topbases"'
254 exit 0
257 if [ -n "$headsonly" ]; then
258 defwithdeps=
259 branches="$(show_heads)"
262 [ -n "$withdeps" ] || withdeps="$defwithdeps"
263 if [ -z "$doingall$terse$graphviz$sort$withdeps$branches" ]; then
264 branches="$(tg info --heads 2>/dev/null | paste -d " " -s -)" || :
265 [ -z "$branches" ] || withdeps=1
267 [ "$withdeps" != "0" ] || withdeps=
268 if [ -n "$withdeps" ]; then
269 [ "$withdeps" != "2" ] || branches="$(show_heads_topgit | paste -d " " -s -)"
270 savetgish="$tgish"
271 tgish=1
272 origbranches="$branches"
273 branches="$(skip_ann=1; show_deps | sort -u -b -k1,1 | paste -d " " -s -)"
274 tgish="$savetgish"
277 curname="$(strip_ref "$(git symbolic-ref -q HEAD)")" || :
279 if [ -n "$graphviz" ]; then
280 cat <<EOT
281 # GraphViz output; pipe to:
282 # | dot -Tpng -o <output>
283 # or
284 # | dot -Txlib
286 digraph G {
288 graph [
289 rankdir = "TB"
290 label="TopGit Layout\n\n\n"
291 fontsize = 14
292 labelloc=top
293 pad = "0.5,0.5"
299 if [ -n "$sort" ]; then
300 tsort_input="$(get_temp tg-summary-sort)"
301 exec 4>$tsort_input
302 exec 5<$tsort_input
305 ## List branches
307 aheadlist=
308 processed=' '
309 needslist=' '
310 compute_ahead_list()
312 refslist=
313 [ -z "$tg_read_only" ] || [ -z "$tg_ref_cache" ] || ! [ -s "$tg_ref_cache" ] ||
314 refslist="-r=\"$tg_ref_cache\""
315 msgsfile="$(get_temp msgslist)"
316 eval run_awk_topgit_msg -nokind "$refslist" '"refs/$topbases"' >"$msgsfile"
317 needs_update_check_clear
318 [ -z "$branches" ] || [ -n "$withdeps" ] || return 0
319 [ -n "$withdeps" ] || origbranches="$(navigate_deps -s=-1 | paste -d ' ' -s -)"
320 for onehead in $origbranches; do
321 case "$exclude" in *" $onehead "*) continue; esac
322 needs_update_check $onehead
323 done
324 aheadlist=" $needs_update_ahead "
327 process_branch()
329 missing_deps=
331 current=' '
332 [ "$name" != "$curname" ] || current='>'
333 from=$head_from
334 [ "$name" = "$curname" ] ||
335 from=
336 nonempty=' '
337 ! branch_empty "$name" $from || nonempty='0'
338 remote=' '
339 [ -z "$base_remote" ] || remote='l'
340 ! has_remote "$name" || remote='r'
341 rem_update=' '
342 [ "$remote" != 'r' ] || ! ref_exists "refs/remotes/$base_remote/${topbases#heads/}/$name" || {
343 branch_contains "refs/$topbases/$name" "refs/remotes/$base_remote/${topbases#heads/}/$name" &&
344 branch_contains "refs/heads/$name" "refs/remotes/$base_remote/$name"
345 } || rem_update='R'
346 [ "$remote" != 'r' -o "$rem_update" = 'R' ] || {
347 branch_contains "refs/remotes/$base_remote/$name" "refs/heads/$name" 2>/dev/null
348 } || rem_update='L'
349 needs_update_check "$name"
350 deps_update=' '
351 ! vcontains needs_update_behind "$name" || deps_update='D'
352 deps_missing=' '
353 ! vcontains needs_update_partial "$name" || deps_missing='!'
354 base_update=' '
355 branch_contains "refs/heads/$name" "refs/$topbases/$name" || base_update='B'
356 ahead=' '
357 case "$aheadlist" in *" $name "*) ahead='*'; esac
359 printf '%-8s %s\n' "$current$nonempty$remote$rem_update$deps_update$deps_missing$base_update$ahead" \
360 "$name"
363 if [ -n "$terse" ]; then
364 refslist=
365 [ -z "$tg_read_only" ] || [ -z "$tg_ref_cache" ] || ! [ -s "$tg_ref_cache" ] ||
366 refslist="-r=\"$tg_ref_cache\""
367 cmd="run_awk_topgit_msg --list"
368 [ $verbose -lt 2 ] || cmd="run_awk_topgit_msg -c -nokind"
369 [ $verbose -gt 0 ] || cmd="run_awk_topgit_branches -n"
370 eval "$cmd" "$refslist" '-i="$branches" -x="$exclude" "refs/$topbases"'
371 exit 0
374 msgsfile=
375 [ -n "$graphviz$sort" ] || compute_ahead_list
376 process_branches()
378 while read name; do
379 case "$exclude" in *" $name "*) continue; esac
380 if [ -n "$graphviz$sort" ]; then
381 from=$head_from
382 [ "$name" = "$curname" ] ||
383 from=
384 cat_file "refs/heads/$name:.topdeps" $from | while read -r dep || [ -n "$dep" ]; do
385 dep_is_tgish=true
386 ref_exists "refs/$topbases/$dep" ||
387 dep_is_tgish=false
388 [ -z "$tgish" ] || [ "$dep_is_tgish" = "true" ] || continue
389 if ! "$dep_is_tgish" || ! branch_annihilated $dep; then
390 if [ -n "$graphviz" ]; then
391 echo "\"$name\" -> \"$dep\";"
392 if [ "$name" = "$curname" ] || [ "$dep" = "$curname" ]; then
393 echo "\"$curname\" [style=filled,fillcolor=yellow];"
395 else
396 echo "$name $dep" >&4
399 done
400 else
401 process_branch
403 done
405 awkpgm='
406 BEGIN {
407 if (msgsfile != "") {
408 while ((e = (getline msg <msgsfile)) > 0) {
409 gsub(/[ \t]+/, " ", msg)
410 sub(/^ /, "", msg)
411 if (split(msg, scratch, " ") < 2 ||
412 scratch[1] == "" || scratch[2] == "") continue
413 msg = substr(msg, length(scratch[1]) + 2)
414 msgs[scratch[1]] = msg
416 close(msgsfile)
420 name = substr($0, 10)
421 if (name != "" && name in msgs)
422 printf "%-39s\t%s\n", $0, msgs[name]
423 else
424 print $0
427 cmd='get_branch_list | process_branches'
428 [ -z "$msgsfile" ] || cmd="$cmd"' | awk -v msgsfile="$msgsfile" "$awkpgm"'
429 eval "$cmd"
431 if [ -n "$graphviz" ]; then
432 echo '}'
435 if [ -n "$sort" ]; then
436 tsort <&5