tg--awksome.sh: make run_awk_topmsg_header see the truth
[topgit/pro.git] / tg-summary.sh
blob2aee1699e0e22d0c165ab86e90cd451e962d2636
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 -vl|-lv)
45 terse=1 verbose=$(( $verbose + 1 ));;
46 -vv)
47 verbose=$(( $verbose + 2 ));;
48 -vvl|-vlv|-lvv)
49 terse=1 verbose=$(( $verbose + 2 ));;
50 --heads|--topgit-heads)
51 heads=1
52 headsindep=;;
53 --heads-independent)
54 heads=1
55 headsindep=1;;
56 --heads-only)
57 headsonly=1;;
58 --with-deps)
59 head=HEAD
60 withdeps=1;;
61 --with-related)
62 head=HEAD
63 withdeps=2;;
64 --without-deps|--no-with-deps|--without-related|--no-with-related)
65 head=HEAD
66 withdeps=0;;
67 --graphviz)
68 graphviz=1;;
69 --sort)
70 sort=1;;
71 --deps)
72 deps=1;;
73 --tgish-only)
74 tgish=1;;
75 --deps-only)
76 head=HEAD
77 depsonly=1;;
78 --rdeps)
79 head=HEAD
80 rdeps=1;;
81 --rdeps-full)
82 head=HEAD
83 rdeps=1 rdepsonce=;;
84 --rdeps-once)
85 head=HEAD
86 rdeps=1 rdepsonce=1;;
87 --all)
88 break;;
89 --exclude=*)
90 [ -n "${1#--exclude=}" ] || die "--exclude= requires a branch name"
91 exclude="$exclude ${1#--exclude=}";;
92 --exclude)
93 shift
94 [ -n "$1" -a "$1" != "--all" ] || die "--exclude requires a branch name"
95 exclude="$exclude $1";;
96 -*)
97 usage;;
99 break;;
100 esac
101 shift
102 done
103 [ $# -eq 0 ] || defwithdeps=1
104 [ -z "$exclude" ] || exclude="$exclude "
105 doingall=
106 [ $# -ne 0 ] || [ z"$head" != z"" ] || doingall=1
107 if [ "$1" = "--all" ]; then
108 [ -z "$withdeps" ] || die "mutually exclusive options given"
109 [ $# -eq 1 ] || usage
110 shift
111 head=
112 defwithdeps=
113 doingall=1
115 [ "$heads$rdeps" != "11" ] || head=
116 [ $# -ne 0 -o -z "$head" ] || set -- "$head"
117 [ -z "$defwithdeps" ] || [ $# -ne 1 ] || [ z"$1" != z"HEAD" -a z"$1" != z"@" ] || defwithdeps=2
119 [ "$terse$heads$headsonly$graphviz$sort$deps$depsonly" = "" ] ||
120 [ "$terse$heads$headsonly$graphviz$sort$deps$depsonly$rdeps" = "1" ] ||
121 [ "$terse$heads$headsonly$graphviz$sort$deps$depsonly$rdeps" = "11" -a "$heads$rdeps" = "11" ] ||
122 die "mutually exclusive options given"
123 [ -z "$withdeps" -o -z "$rdeps$depsonly$heads$headsonly" ] ||
124 die "mutually exclusive options given"
126 for b; do
127 [ "$b" != "--all" ] || usage
128 branches="$branches $(verify_topgit_branch "$b")"
129 done
131 get_branch_list()
133 if [ -n "$branches" ]; then
134 if [ -n "$1" ]; then
135 printf '%s\n' $branches | sort -u
136 else
137 printf '%s\n' $branches
139 else
140 non_annihilated_branches
144 show_heads_independent()
146 topics="$(get_temp topics)"
147 get_branch_list | sed -e 's,^\(.*\)$,refs/heads/\1 \1,' |
148 git cat-file --batch-check='%(objectname) %(rest)' |
149 sort -u -b -k1,1 >"$topics"
150 git merge-base --independent $(cut -d ' ' -f 1 <"$topics") |
151 sort -u -b -k1,1 | join - "$topics" | sort -u -b -k2,2 |
152 while read rev name; do
153 case "$exclude" in *" $name "*) continue; esac
154 printf '%s\n' "$name"
155 done
158 show_heads_topgit()
160 if [ -n "$branches" ]; then
161 navigate_deps -s=-1 -1 -- "$branches" | sort
162 else
163 navigate_deps -s=-1
164 fi |
165 while read -r name; do
166 case "$exclude" in *" $name "*) continue; esac
167 printf '%s\n' "$name"
168 done
171 show_heads()
173 if [ -n "$headsindep" ]; then
174 show_heads_independent "$@"
175 else
176 show_heads_topgit "$@"
180 if [ -n "$heads" -a -z "$rdeps" ]; then
181 show_heads
182 exit 0
185 skip_ann=
186 show_dep() {
187 case "$exclude" in *" $_dep "*) return; esac
188 case " $seen_deps " in *" $_dep "*) return 0; esac
189 seen_deps="${seen_deps:+$seen_deps }$_dep"
190 [ -z "$tgish" -o -n "$_dep_is_tgish" ] || return 0
191 [ -z "$skip_ann" ] || [ -z "$_dep_annihilated" ] && printf '%s\n' "$_dep"
192 return 0
195 show_deps()
197 no_remotes=1
198 recurse_deps_exclude=
199 get_branch_list | while read _b; do
200 case "$exclude" in *" $_b "*) continue; esac
201 case " $recurse_deps_exclude " in *" $_b "*) continue; esac
202 seen_deps=
203 save_skip="$skip_ann"
204 _dep="$_b"; _dep_is_tgish=1; skip_ann=; show_dep; skip_ann="$save_skip"
205 recurse_deps show_dep "$_b"
206 recurse_deps_exclude="$recurse_deps_exclude $seen_deps"
207 done
210 if [ -n "$depsonly" ]; then
211 show_deps | sort -u -b -k1,1
212 exit 0
215 show_rdeps()
217 case "$exclude" in *" $_dep "*) return; esac
218 [ -z "$tgish" -o -n "$_dep_is_tgish" ] || return 0
219 elided=
220 [ -z "$rdepsonce" ] || [ "$_dep_xvisits" = 0 ] || elided="^"
221 printf '%s %s\n' "$_depchain" "$_dep$elided"
224 if [ -n "$rdeps" ]; then
225 no_remotes=1
226 showbreak=
228 if [ -n "$heads" ]; then
229 show_heads
230 else
231 get_branch_list
233 } | while read b; do
234 case "$exclude" in *" $b "*) continue; esac
235 [ -z "$showbreak" ] || echo
236 showbreak=1
237 ref_exists "refs/heads/$b" || continue
239 echol "$b"
240 recurse_preorder=1
241 recurse_deps ${rdepsonce:+-o=-o=-1} show_rdeps "$b"
242 } | sed -e 's/[^ ][^ ]*[ ]/ /g'
243 done
244 exit 0
247 if [ -n "$deps" ]; then
248 if [ -n "$branches" ]; then
249 no_remotes=1
250 recurse_deps_exclude="$exclude"
251 recurse_deps_internal -n -t -m -e=2 -- $branches | sort -u
252 else
253 refslist=
254 [ -z "$tg_read_only" ] || [ -z "$tg_ref_cache" ] || ! [ -s "$tg_ref_cache" ] ||
255 refslist="-r=\"$tg_ref_cache\""
256 tdopt=
257 v_get_tdopt tdopt "$head_from"
258 eval run_awk_topgit_deps "$refslist" "$tdopt" '-n -t -x="$exclude" "refs/$topbases"'
260 exit 0
263 if [ -n "$headsonly" ]; then
264 defwithdeps=
265 branches="$(show_heads)"
268 [ -n "$withdeps" ] || withdeps="$defwithdeps"
269 if [ -z "$doingall$terse$graphviz$sort$withdeps$branches" ]; then
270 branches="$(tg info --heads 2>/dev/null | paste -d " " -s -)" || :
271 [ -z "$branches" ] || withdeps=1
273 [ "$withdeps" != "0" ] || withdeps=
274 if [ -n "$withdeps" ]; then
275 [ "$withdeps" != "2" ] || branches="$(show_heads_topgit | paste -d " " -s -)"
276 savetgish="$tgish"
277 tgish=1
278 origbranches="$branches"
279 branches="$(skip_ann=1; show_deps | sort -u -b -k1,1 | paste -d " " -s -)"
280 tgish="$savetgish"
283 curname="$(strip_ref "$(git symbolic-ref -q HEAD)")" || :
285 if [ -n "$graphviz" ]; then
286 cat <<EOT
287 # GraphViz output; pipe to:
288 # | dot -Tpng -o <output>
289 # or
290 # | dot -Txlib
292 digraph G {
294 graph [
295 rankdir = "TB"
296 label="TopGit Layout\n\n\n"
297 fontsize = 14
298 labelloc=top
299 pad = "0.5,0.5"
305 if [ -n "$sort" ]; then
306 tsort_input="$(get_temp tg-summary-sort)"
307 exec 4>$tsort_input
308 exec 5<$tsort_input
311 ## List branches
313 aheadlist=
314 processed=' '
315 needslist=' '
316 compute_ahead_list()
318 refslist=
319 [ -z "$tg_read_only" ] || [ -z "$tg_ref_cache" ] || ! [ -s "$tg_ref_cache" ] ||
320 refslist="-r=\"$tg_ref_cache\""
321 msgsfile="$(get_temp msgslist)"
322 eval run_awk_topgit_msg -nokind "$refslist" '"refs/$topbases"' >"$msgsfile"
323 needs_update_check_clear
324 needs_update_check_no_same=1
325 [ -z "$branches" ] || [ -n "$withdeps" ] || return 0
326 [ -n "$withdeps" ] || origbranches="$(navigate_deps -s=-1 | paste -d ' ' -s -)"
327 for onehead in $origbranches; do
328 case "$exclude" in *" $onehead "*) continue; esac
329 needs_update_check $onehead
330 done
331 aheadlist=" $needs_update_ahead "
334 process_branch()
336 missing_deps=
338 current=' '
339 [ "$name" != "$curname" ] || current='>'
340 from=$head_from
341 [ "$name" = "$curname" ] ||
342 from=
343 nonempty=' '
344 ! branch_empty "$name" $from || nonempty='0'
345 remote=' '
346 [ -z "$base_remote" ] || remote='l'
347 ! has_remote "$name" || remote='r'
348 rem_update=' '
349 [ "$remote" != 'r' ] || ! ref_exists "refs/remotes/$base_remote/${topbases#heads/}/$name" || {
350 branch_contains "refs/$topbases/$name" "refs/remotes/$base_remote/${topbases#heads/}/$name" &&
351 branch_contains "refs/heads/$name" "refs/remotes/$base_remote/$name"
352 } || rem_update='R'
353 [ "$remote" != 'r' -o "$rem_update" = 'R' ] || {
354 branch_contains "refs/remotes/$base_remote/$name" "refs/heads/$name" 2>/dev/null
355 } || rem_update='L'
356 needs_update_check "$name"
357 deps_update=' '
358 ! vcontains needs_update_behind "$name" || deps_update='D'
359 deps_missing=' '
360 ! vcontains needs_update_partial "$name" || deps_missing='!'
361 base_update=' '
362 branch_contains "refs/heads/$name" "refs/$topbases/$name" || base_update='B'
363 ahead=' '
364 case "$aheadlist" in *" $name "*) ahead='*'; esac
366 printf '%-8s %s\n' "$current$nonempty$remote$rem_update$deps_update$deps_missing$base_update$ahead" \
367 "$name"
370 if [ -n "$terse" ]; then
371 refslist=
372 [ -z "$tg_read_only" ] || [ -z "$tg_ref_cache" ] || ! [ -s "$tg_ref_cache" ] ||
373 refslist="-r=\"$tg_ref_cache\""
374 cmd="run_awk_topgit_msg --list"
375 [ $verbose -lt 2 ] || cmd="run_awk_topgit_msg -c -nokind"
376 [ $verbose -gt 0 ] || cmd="run_awk_topgit_branches -n"
377 eval "$cmd" "$refslist" '-i="$branches" -x="$exclude" "refs/$topbases"'
378 exit 0
381 msgsfile=
382 [ -n "$graphviz$sort" ] || compute_ahead_list
383 process_branches()
385 while read name; do
386 case "$exclude" in *" $name "*) continue; esac
387 if [ -n "$graphviz$sort" ]; then
388 from=$head_from
389 [ "$name" = "$curname" ] ||
390 from=
391 cat_file "refs/heads/$name:.topdeps" $from | while read -r dep || [ -n "$dep" ]; do
392 dep_is_tgish=true
393 ref_exists "refs/$topbases/$dep" ||
394 dep_is_tgish=false
395 [ -z "$tgish" ] || [ "$dep_is_tgish" = "true" ] || continue
396 if ! "$dep_is_tgish" || ! branch_annihilated $dep; then
397 if [ -n "$graphviz" ]; then
398 echo "\"$name\" -> \"$dep\";"
399 if [ "$name" = "$curname" ] || [ "$dep" = "$curname" ]; then
400 echo "\"$curname\" [style=filled,fillcolor=yellow];"
402 else
403 echo "$name $dep" >&4
406 done
407 else
408 process_branch
410 done
412 awkpgm='
413 BEGIN {
414 if (msgsfile != "") {
415 while ((e = (getline msg <msgsfile)) > 0) {
416 gsub(/[ \t]+/, " ", msg)
417 sub(/^ /, "", msg)
418 if (split(msg, scratch, " ") < 2 ||
419 scratch[1] == "" || scratch[2] == "") continue
420 msg = substr(msg, length(scratch[1]) + 2)
421 msgs[scratch[1]] = msg
423 close(msgsfile)
427 name = substr($0, 10)
428 if (name != "" && name in msgs)
429 printf "%-39s\t%s\n", $0, msgs[name]
430 else
431 print $0
434 cmd='get_branch_list | process_branches'
435 [ -z "$msgsfile" ] || cmd="$cmd"' | awk -v msgsfile="$msgsfile" "$awkpgm"'
436 eval "$cmd"
438 if [ -n "$graphviz" ]; then
439 echo '}'
442 if [ -n "$sort" ]; then
443 tsort <&5