topgit: version 0.19.13
[topgit/pro.git] / tg-summary.sh
blob778dbb534803597eca670831e32dca330d06c70e
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,2018,2021
5 # All rights reserved
6 # GPLv2
8 USAGE="\
9 Usage: ${tgname:-tg} [...] summary [<option>...] [--all | <branch>...]
10 Or: ${tgname:-tg} [...] summary [--terse | --list] [<option>...] [--all | <branch>...]
11 Or: ${tgname:-tg} [...] summary --rdeps[-full] [<option>...] [--all | <branch>...]
12 Or: ${tgname:-tg} [...] summary --heads[-independent] [<option>...] [--all | <branch>...]
13 Or: ${tgname:-tg} [...] summary --deps [<option>...] [--all | <branch>...]
14 Or: ${tgname:-tg} [...] summary --deps-only [<option>...] [--all | <branch>...]
15 Or: ${tgname:-tg} [...] summary --sort [<option>...] [--all | <branch>...]
16 Or: ${tgname:-tg} [...] summary --graphviz [<option>...] [--all | <branch>...]
17 Options:
18 --terse / --list list TopGit branches (aka '-t' / '-l')
19 --rdeps[-once] show branch dependencies as multi-indented list
20 --rdeps-full like '--rdeps' but do not collapse repeated items
21 --rdeps --heads use '--rdeps' and \$('--heads of <branch>...)
22 --[topgit]-heads list only independent TopGit branch heads
23 --heads-independent list only merge-base --independent heads
24 --deps list all .topdeps branch dependencies
25 --deps-only list sorted unique .topdeps branches + dependencies
26 --sort feed output of '--deps' through 'tsort'
27 --graphviz generate dependency graph description in DOT format
28 --verbose / -v include subjects with '-l' (twice shows annihilated)
29 --exclude <branch> exclude <branch> during operation (may be repeated)
30 --tgish-only exclude non-TopGit branches (aka '--tgish')
31 --with-deps include dependencies of given <branch>... (default)
32 --without-deps do not include dependencies of given <branch>...
33 --with-related use '--with-deps' and \$('--heads' of <branch>...)
34 --without-related do not use '--with-related' processing
35 --heads-only use '--without-deps' and \$('--heads' of <branch>...)
36 -i use TopGit metadata from index instead of HEAD branch
37 -w use metadata from working directory instead of branch"
39 usage()
41 if [ "${1:-0}" != 0 ]; then
42 printf '%s\n' "$USAGE" >&2
43 else
44 printf '%s\n' "$USAGE"
46 exit ${1:-0}
49 ## Parse options
51 terse=
52 graphviz=
53 sort=
54 deps=
55 depsonly=
56 rdeps=
57 rdepsonce=1
58 head_from=
59 branches=
60 head=
61 heads=
62 headsindep=
63 headsonly=
64 exclude=
65 tgish=
66 withdeps=
67 verbose=0
69 ## Parse options
71 while [ -n "$1" ]; do
72 arg="$1"
73 case "$arg" in
74 -h|--help)
75 usage;;
76 -i|-w)
77 [ -z "$head_from" ] || die "-i and -w are mutually exclusive"
78 head_from="$arg";;
79 -t|--list|-l|--terse)
80 terse=1;;
81 -v|--verbose)
82 verbose=$(( $verbose + 1 ));;
83 -vl|-lv)
84 terse=1 verbose=$(( $verbose + 1 ));;
85 -vv)
86 verbose=$(( $verbose + 2 ));;
87 -vvl|-vlv|-lvv)
88 terse=1 verbose=$(( $verbose + 2 ));;
89 --heads|--topgit-heads)
90 heads=1
91 headsindep=;;
92 --heads-independent)
93 heads=1
94 headsindep=1;;
95 --heads-only)
96 headsonly=1;;
97 --with-deps)
98 head=HEAD
99 withdeps=1;;
100 --with-related)
101 head=HEAD
102 withdeps=2;;
103 --without-deps|--no-with-deps|--without-related|--no-with-related)
104 head=HEAD
105 withdeps=0;;
106 --graphviz)
107 graphviz=1;;
108 --sort)
109 sort=1;;
110 --deps)
111 deps=1;;
112 --tgish-only|--tgish)
113 tgish=1;;
114 --deps-only)
115 head=HEAD
116 depsonly=1;;
117 --rdeps)
118 head=HEAD
119 rdeps=1;;
120 --rdeps-full)
121 head=HEAD
122 rdeps=1 rdepsonce=;;
123 --rdeps-once)
124 head=HEAD
125 rdeps=1 rdepsonce=1;;
126 --all)
127 break;;
128 --exclude=*)
129 [ -n "${1#--exclude=}" ] || die "--exclude= requires a branch name"
130 exclude="$exclude ${1#--exclude=}";;
131 --exclude)
132 shift
133 [ -n "$1" ] && [ "$1" != "--all" ] || die "--exclude requires a branch name"
134 exclude="$exclude $1";;
136 usage 1;;
138 break;;
139 esac
140 shift
141 done
142 [ $# -eq 0 ] || defwithdeps=1
143 [ -z "$exclude" ] || exclude="$exclude "
144 doingall=
145 [ $# -ne 0 ] || [ z"$head" != z"" ] || doingall=1
146 if [ "$1" = "--all" ]; then
147 [ -z "$withdeps" ] || die "mutually exclusive options given"
148 [ $# -eq 1 ] || usage
149 shift
150 head=
151 defwithdeps=
152 doingall=1
154 [ "$heads$rdeps" != "11" ] || head=
155 [ $# -ne 0 ] || [ -z "$head" ] || set -- "$head"
156 [ -z "$defwithdeps" ] || [ $# -ne 1 ] || { [ z"$1" != z"HEAD" ] && [ z"$1" != z"@" ]; } || defwithdeps=2
158 [ "$terse$heads$headsonly$graphviz$sort$deps$depsonly" = "" ] ||
159 [ "$terse$heads$headsonly$graphviz$sort$deps$depsonly$rdeps" = "1" ] ||
160 { [ "$terse$heads$headsonly$graphviz$sort$deps$depsonly$rdeps" = "11" ] && [ "$heads$rdeps" = "11" ]; } ||
161 die "mutually exclusive options given"
162 [ -z "$withdeps" ] || [ -z "$rdeps$depsonly$heads$headsonly" ] ||
163 die "mutually exclusive options given"
165 for b; do
166 [ "$b" != "--all" ] || usage
167 v_verify_topgit_branch b "$b"
168 branches="$branches $b"
169 done
171 get_branch_list()
173 if [ -n "$branches" ]; then
174 if [ -n "$1" ]; then
175 printf '%s\n' $branches | sort -u
176 else
177 printf '%s\n' $branches
179 else
180 non_annihilated_branches
184 show_heads_independent()
186 topics="$(get_temp topics)"
187 get_branch_list | sed -e 's,^\(.*\)$,refs/heads/\1 \1,' |
188 git cat-file --batch-check='%(objectname) %(rest)' |
189 sort -u -b -k1,1 >"$topics"
190 git merge-base --independent $(cut -d ' ' -f 1 <"$topics") |
191 sort -u -b -k1,1 | join - "$topics" | sort -u -b -k2,2 |
192 while read rev name; do
193 case "$exclude" in *" $name "*) continue; esac
194 printf '%s\n' "$name"
195 done
198 show_heads_topgit()
200 [ -z "$head_from" ] || [ -n "$with_deps_opts" ] ||
201 v_get_tdopt with_deps_opts "$head_from"
202 if [ -n "$branches" ]; then
203 eval navigate_deps "$with_deps_opts" -s=-1 -1 -- '"$branches"' | sort
204 else
205 eval navigate_deps "$with_deps_opts" -s=-1
206 fi |
207 while read -r name; do
208 case "$exclude" in *" $name "*) continue; esac
209 printf '%s\n' "$name"
210 done
213 show_heads()
215 if [ -n "$headsindep" ]; then
216 show_heads_independent "$@"
217 else
218 show_heads_topgit "$@"
222 if [ -n "$heads" ] && [ -z "$rdeps" ]; then
223 show_heads
224 exit 0
227 # if $1 is non-empty, show the dep only (including self), not the edge (no self)
228 show_deps()
230 [ -z "$head_from" ] || [ -n "$with_deps_opts" ] ||
231 v_get_tdopt with_deps_opts "$head_from"
232 if [ -n "$branches" ]; then
233 edgenum=2
234 [ -z "$1" ] || edgenum=1
235 no_remotes=1
236 recurse_deps_exclude="$exclude"
237 recurse_deps_internal -n ${tgish:+-t} -m ${1:+-s} -e=$edgenum -- $branches | sort -u
238 else
239 cutcmd=
240 [ -z "$1" ] || cutcmd='| cut -d " " -f 2 | sort -u'
241 refslist=
242 [ -z "$tg_read_only" ] || [ -z "$tg_ref_cache" ] || ! [ -s "$tg_ref_cache" ] ||
243 refslist="-r=\"$tg_ref_cache\""
244 tdopt=
245 eval run_awk_topgit_deps "$refslist" "$with_deps_opts" "${tgish:+-t}" \
246 "${1:+-s}" '-n -x="$exclude" "refs/$topbases"' "$cutcmd"
250 if [ -n "$deps$depsonly$sort" ]; then
251 eval show_deps $depsonly "${sort:+|tsort}"
252 exit 0
255 if [ -n "$rdeps" ]; then
256 no_remotes=1
257 recurse_preorder=1
258 recurse_deps_exclude="$exclude"
259 showbreak=
260 v_get_tdopt with_deps_opts "$head_from"
262 if [ -n "$heads" ]; then
263 show_heads
264 else
265 get_branch_list
267 } | while read -r b; do
268 case "$exclude" in *" $b "*) continue; esac
269 ref_exists "refs/heads/$b" || continue
270 [ -z "$showbreak" ] || echo
271 showbreak=1
272 recurse_deps_internal ${tgish:+-t} -n -s ${rdepsonce:+-o=-1} "$b" |
273 awk -v elided="$rdepsonce" '{
274 if ($1 == "1" || NF < 5) next
275 xvisits = $4
276 dep = $5
277 if ($6 != "") haschild[$6] = 1
278 sub(/^[^ ]+ +[^ ]+ +[^ ]+ +[^ ]+ +[^ ]+/, "")
279 gsub(/ [^ ]+/, " ")
280 xtra = ""
281 if (elided && xvisits > 0 && haschild[dep]) xtra="^"
282 print $0 dep xtra
284 done
285 exit 0
288 if [ -n "$headsonly" ]; then
289 defwithdeps=
290 branches="$(show_heads)"
293 [ -n "$withdeps" ] || withdeps="$defwithdeps"
294 if [ -z "$doingall$terse$graphviz$sort$withdeps$branches" ]; then
295 branches="$(tg info --heads 2>/dev/null | paste -d " " -s -)" || :
296 [ -z "$branches" ] || withdeps=1
298 [ "$withdeps" != "0" ] || withdeps=
299 if [ -n "$withdeps" ]; then
300 v_get_tdopt with_deps_opts "$head_from"
301 [ "$withdeps" != "2" ] || branches="$(show_heads_topgit | paste -d " " -s -)"
302 savetgish="$tgish"
303 tgish=1
304 origbranches="$branches"
305 branches="$(show_deps 1 | paste -d " " -s -)"
306 tgish="$savetgish"
309 if [ -n "$terse" ]; then
310 refslist=
311 [ -z "$tg_read_only" ] || [ -z "$tg_ref_cache" ] || ! [ -s "$tg_ref_cache" ] ||
312 refslist="-r=\"$tg_ref_cache\""
313 cmd="run_awk_topgit_branches -n"
314 if [ $verbose -gt 0 ]; then
315 v_get_tmopt tm_opt "$head_from"
316 cmd="run_awk_topgit_msg --list${tm_opt:+ $tm_opt}"
317 [ $verbose -lt 2 ] || cmd="run_awk_topgit_msg -c -nokind${tm_opt:+ $tm_opt}"
319 eval "$cmd" "$refslist" '-i="$branches" -x="$exclude" "refs/$topbases"'
320 exit 0
323 v_strip_ref curname "$(git symbolic-ref -q HEAD)"
325 if [ -n "$graphviz" ]; then
326 printf '%s\n\n' \
327 '# GraphViz output; pipe to:
328 # | dot -Tpng -o <output>
329 # or
330 # | dot -Txlib
332 digraph G {
334 graph [
335 rankdir = "TB"
336 label="TopGit Layout\n\n\n"
337 fontsize = 14
338 labelloc=top
339 pad = "0.5,0.5"
341 show_deps | while read -r name dep; do
342 printf '"%s" -> "%s";\n' "$name" "$dep"
343 if [ "$name" = "$curname" ] || [ "$dep" = "$curname" ]; then
344 printf '"%s" [%s];\n' "$curname" "style=filled,fillcolor=yellow"
346 done
347 printf '%s\n' '}'
348 exit 0
351 compute_ahead_list()
353 aheadlist=
354 refslist=
355 [ -z "$tg_read_only" ] || [ -z "$tg_ref_cache" ] || ! [ -s "$tg_ref_cache" ] ||
356 refslist="-r=\"$tg_ref_cache\""
357 msgsfile="$(get_temp msgslist)"
358 v_get_tmopt tm_opt "$head_from"
359 eval run_awk_topgit_msg "-nokind${tm_opt:+ $tm_opt}" "$refslist" '"refs/$topbases"' >"$msgsfile"
360 needs_update_check_clear
361 needs_update_check_no_same=1
362 [ -z "$branches" ] || [ -n "$withdeps" ] || return 0
363 v_get_tdopt with_deps_opts "$head_from"
364 [ -n "$withdeps" ] || origbranches="$(navigate_deps -s=-1 | paste -d ' ' -s -)"
365 for onehead in $origbranches; do
366 case "$exclude" in *" $onehead "*) continue; esac
367 needs_update_check $onehead
368 done
369 aheadlist=" $needs_update_ahead "
372 process_branch()
374 missing_deps=
376 current=' '
377 [ "$name" != "$curname" ] || current='>'
378 from=$head_from
379 [ "$name" = "$curname" ] ||
380 from=
381 nonempty=' '
382 ! branch_empty "$name" $from || nonempty='0'
383 remote=' '
384 [ -z "$base_remote" ] || remote='l'
385 ! has_remote "$name" || remote='r'
386 rem_update=' '
387 [ "$remote" != 'r' ] || ! ref_exists "refs/remotes/$base_remote/${topbases#heads/}/$name" || {
388 branch_contains "refs/$topbases/$name" "refs/remotes/$base_remote/${topbases#heads/}/$name" &&
389 branch_contains "refs/heads/$name" "refs/remotes/$base_remote/$name"
390 } || rem_update='R'
391 [ "$remote" != 'r' ] || [ "$rem_update" = 'R' ] || {
392 branch_contains "refs/remotes/$base_remote/$name" "refs/heads/$name" 2>/dev/null
393 } || rem_update='L'
394 needs_update_check "$name"
395 deps_update=' '
396 ! vcontains needs_update_behind "$name" || deps_update='D'
397 deps_missing=' '
398 ! vcontains needs_update_partial "$name" || deps_missing='!'
399 base_update=' '
400 branch_contains "refs/heads/$name" "refs/$topbases/$name" || base_update='B'
401 ahead=' '
402 case "$aheadlist" in *" $name "*) ahead='*'; esac
404 printf '%-8s %s\n' "$current$nonempty$remote$rem_update$deps_update$deps_missing$base_update$ahead" \
405 "$name"
408 awkpgm='
409 BEGIN {
410 if (msgsfile != "") {
411 while ((e = (getline msg <msgsfile)) > 0) {
412 gsub(/[ \t]+/, " ", msg)
413 sub(/^ /, "", msg)
414 if (split(msg, scratch, " ") < 2 ||
415 scratch[1] == "" || scratch[2] == "") continue
416 msg = substr(msg, length(scratch[1]) + 2)
417 msgs[scratch[1]] = msg
419 close(msgsfile)
423 name = substr($0, 10)
424 if (name != "" && name in msgs)
425 printf "%-39s\t%s\n", $0, msgs[name]
426 else
427 print $0
430 msgsfile=
431 compute_ahead_list
432 cmd='get_branch_list | while read name; do process_branch; done'
433 [ -z "$msgsfile" ] || cmd="$cmd"' | awk -v msgsfile="$msgsfile" "$awkpgm"'
434 eval "$cmd"