t4100: add several summary tests
[topgit/pro.git] / tg-summary.sh
blob576504b1dfa403d87edc93e2be40e9ec7aee0563
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 [ -z "$branches" ] || [ -n "$withdeps" ] || return 0
313 [ -n "$withdeps" ] || origbranches="$(tg summary --topgit-heads | paste -d ' ' -s -)"
314 aheadfile="$(get_temp aheadlist)"
315 savebr="$base_remote"
316 savenr="$no_remotes"
317 base_remote=
318 no_remotes=1
319 for onehead in $origbranches; do
320 case "$exclude" in *" $onehead "*) continue; esac
321 case "$processed" in *" $onehead "*) continue; esac
322 processed="${processed}$onehead "
323 needs_update "$onehead" || needslist="${needslist}$onehead "
324 done >"$aheadfile"
325 no_remotes="$savenr"
326 base_remote="$savebr"
327 aheadlist=" $(cut -d ' ' -f 1 <"$aheadfile" | sort -u | paste -d ' ' -s -) "
330 process_branch()
332 missing_deps=
334 current=' '
335 [ "$name" != "$curname" ] || current='>'
336 from=$head_from
337 [ "$name" = "$curname" ] ||
338 from=
339 nonempty=' '
340 ! branch_empty "$name" $from || nonempty='0'
341 remote=' '
342 [ -z "$base_remote" ] || remote='l'
343 ! has_remote "$name" || remote='r'
344 rem_update=' '
345 [ "$remote" != 'r' ] || ! ref_exists "refs/remotes/$base_remote/${topbases#heads/}/$name" || {
346 branch_contains "refs/$topbases/$name" "refs/remotes/$base_remote/${topbases#heads/}/$name" &&
347 branch_contains "refs/heads/$name" "refs/remotes/$base_remote/$name"
348 } || rem_update='R'
349 [ "$remote" != 'r' -o "$rem_update" = 'R' ] || {
350 branch_contains "refs/remotes/$base_remote/$name" "refs/heads/$name" 2>/dev/null
351 } || rem_update='L'
352 deps_update=' '
353 case "$processed" in
354 *" $name "*)
355 case "$needslist" in *" $name "*) deps_update='D'; esac;;
357 needs_update "$name" >/dev/null || deps_update='D';;
358 esac
359 deps_missing=' '
360 [ -z "$missing_deps" ] || 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 if [ "$(ref_exists_rev "refs/heads/$name")" != "$(ref_exists_rev "refs/$topbases/$name")" ]; then
367 subject="$(cat_file "refs/heads/$name:.topmsg" $from | sed -n 's/^Subject: //p')"
368 else
369 # No commits yet
370 subject="(No commits)"
373 printf '%-8s %-30s\t%s\n' "$current$nonempty$remote$rem_update$deps_update$deps_missing$base_update$ahead" \
374 "$name" "$subject"
377 if [ -n "$terse" ]; then
378 refslist=
379 [ -z "$tg_read_only" ] || [ -z "$tg_ref_cache" ] || ! [ -s "$tg_ref_cache" ] ||
380 refslist="-r=\"$tg_ref_cache\""
381 cmd="run_awk_topgit_msg --list"
382 [ $verbose -lt 2 ] || cmd="run_awk_topgit_msg -c -nokind"
383 [ $verbose -gt 0 ] || cmd="run_awk_topgit_branches -n"
384 eval "$cmd" "$refslist" '-i="$branches" -x="$exclude" "refs/$topbases"'
385 exit 0
388 [ -n "$graphviz$sort" ] || compute_ahead_list
389 get_branch_list |
390 while read name; do
391 case "$exclude" in *" $name "*) continue; esac
392 if [ -n "$graphviz$sort" ]; then
393 from=$head_from
394 [ "$name" = "$curname" ] ||
395 from=
396 cat_file "refs/heads/$name:.topdeps" $from | while read -r dep || [ -n "$dep" ]; do
397 dep_is_tgish=true
398 ref_exists "refs/$topbases/$dep" ||
399 dep_is_tgish=false
400 [ -z "$tgish" ] || [ "$dep_is_tgish" = "true" ] || continue
401 if ! "$dep_is_tgish" || ! branch_annihilated $dep; then
402 if [ -n "$graphviz" ]; then
403 echo "\"$name\" -> \"$dep\";"
404 if [ "$name" = "$curname" ] || [ "$dep" = "$curname" ]; then
405 echo "\"$curname\" [style=filled,fillcolor=yellow];"
407 else
408 echo "$name $dep" >&4
411 done
412 else
413 process_branch
415 done
417 if [ -n "$graphviz" ]; then
418 echo '}'
421 if [ -n "$sort" ]; then
422 tsort <&5