t4000: add bare branch sanity checks
[topgit/pro.git] / tg-summary.sh
blobc5c5c83a0bf9244766aba561d890855693edf449
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] [--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=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 --without-deps|--no-with-deps)
56 head=HEAD
57 withdeps=0;;
58 --graphviz)
59 graphviz=1;;
60 --sort)
61 sort=1;;
62 --deps)
63 deps=1;;
64 --tgish-only)
65 tgish=1;;
66 --deps-only)
67 head=HEAD
68 depsonly=1;;
69 --rdeps)
70 head=HEAD
71 rdeps=1;;
72 --rdeps-full)
73 head=HEAD
74 rdeps=1 rdepsonce=;;
75 --rdeps-once)
76 head=HEAD
77 rdeps=1 rdepsonce=1;;
78 --all)
79 break;;
80 --exclude=*)
81 [ -n "${1#--exclude=}" ] || die "--exclude= requires a branch name"
82 exclude="$exclude ${1#--exclude=}";;
83 --exclude)
84 shift
85 [ -n "$1" -a "$1" != "--all" ] || die "--exclude requires a branch name"
86 exclude="$exclude $1";;
87 -*)
88 usage;;
90 break;;
91 esac
92 shift
93 done
94 [ $# -eq 0 ] || defwithdeps=1
95 [ -z "$exclude" ] || exclude="$exclude "
96 doingall=
97 if [ "$1" = "--all" ]; then
98 [ -z "$withdeps" ] || die "mutually exclusive options given"
99 [ $# -eq 1 ] || usage
100 shift
101 head=
102 defwithdeps=
103 doingall=1
105 [ "$heads$rdeps" != "11" ] || head=
106 [ $# -ne 0 -o -z "$head" ] || set -- "$head"
108 [ "$terse$heads$headsonly$graphviz$sort$deps$depsonly" = "" ] ||
109 [ "$terse$heads$headsonly$graphviz$sort$deps$depsonly$rdeps" = "1" ] ||
110 [ "$terse$heads$headsonly$graphviz$sort$deps$depsonly$rdeps" = "11" -a "$heads$rdeps" = "11" ] ||
111 die "mutually exclusive options given"
112 [ -z "$withdeps" -o -z "$rdeps$depsonly$heads$headsonly" ] ||
113 die "mutually exclusive options given"
115 for b; do
116 [ "$b" != "--all" ] || usage
117 branches="$branches $(verify_topgit_branch "$b")"
118 done
120 get_branch_list()
122 if [ -n "$branches" ]; then
123 if [ -n "$1" ]; then
124 printf '%s\n' $branches | sort -u
125 else
126 printf '%s\n' $branches
128 else
129 non_annihilated_branches
133 show_heads_independent()
135 topics="$(get_temp topics)"
136 get_branch_list | sed -e 's,^\(.*\)$,refs/heads/\1 \1,' |
137 git cat-file --batch-check='%(objectname) %(rest)' |
138 sort -u -b -k1,1 >"$topics"
139 git merge-base --independent $(cut -d ' ' -f 1 <"$topics") |
140 sort -u -b -k1,1 | join - "$topics" | sort -u -b -k2,2 |
141 while read rev name; do
142 case "$exclude" in *" $name "*) continue; esac
143 printf '%s\n' "$name"
144 done
147 show_heads_topgit()
149 if [ -n "$branches" ]; then
150 navigate_deps -s=-1 -1 -- "$branches" | sort
151 else
152 navigate_deps -s=-1
153 fi |
154 while read -r name; do
155 case "$exclude" in *" $name "*) continue; esac
156 printf '%s\n' "$name"
157 done
160 show_heads()
162 if [ -n "$headsindep" ]; then
163 show_heads_independent "$@"
164 else
165 show_heads_topgit "$@"
169 if [ -n "$heads" -a -z "$rdeps" ]; then
170 show_heads
171 exit 0
174 skip_ann=
175 show_dep() {
176 case "$exclude" in *" $_dep "*) return; esac
177 case " $seen_deps " in *" $_dep "*) return 0; esac
178 seen_deps="${seen_deps:+$seen_deps }$_dep"
179 [ -z "$tgish" -o -n "$_dep_is_tgish" ] || return 0
180 [ -z "$skip_ann" ] || [ -z "$_dep_annihilated" ] && printf '%s\n' "$_dep"
181 return 0
184 show_deps()
186 no_remotes=1
187 recurse_deps_exclude=
188 get_branch_list | while read _b; do
189 case "$exclude" in *" $_b "*) continue; esac
190 case " $recurse_deps_exclude " in *" $_b "*) continue; esac
191 seen_deps=
192 save_skip="$skip_ann"
193 _dep="$_b"; _dep_is_tgish=1; skip_ann=; show_dep; skip_ann="$save_skip"
194 recurse_deps show_dep "$_b"
195 recurse_deps_exclude="$recurse_deps_exclude $seen_deps"
196 done
199 if [ -n "$depsonly" ]; then
200 show_deps | sort -u -b -k1,1
201 exit 0
204 show_rdeps()
206 case "$exclude" in *" $_dep "*) return; esac
207 [ -z "$tgish" -o -n "$_dep_is_tgish" ] || return 0
208 printf '%s %s\n' "$_depchain" "$_dep"
211 if [ -n "$rdeps" ]; then
212 no_remotes=1
213 showbreak=
215 if [ -n "$heads" ]; then
216 show_heads
217 else
218 get_branch_list
220 } | while read b; do
221 case "$exclude" in *" $b "*) continue; esac
222 [ -z "$showbreak" ] || echo
223 showbreak=1
224 ref_exists "refs/heads/$b" || continue
226 echol "$b"
227 recurse_preorder=1
228 recurse_deps ${rdepsonce:+-o=-o=-1} show_rdeps "$b"
229 } | sed -e 's/[^ ][^ ]*[ ]/ /g'
230 done
231 exit 0
234 if [ -n "$deps" ]; then
235 if [ -n "$branches" ]; then
236 no_remotes=1
237 recurse_deps_exclude="$exclude"
238 recurse_deps_internal -n -t -m -e=2 -- $branches | sort -u
239 else
240 refslist=
241 [ -z "$tg_read_only" ] || [ -z "$tg_ref_cache" ] || ! [ -s "$tg_ref_cache" ] ||
242 refslist="-r=\"$tg_ref_cache\""
243 tdopt=
244 v_get_tdopt tdopt "$head_from"
245 eval run_awk_topgit_deps "$refslist" "$tdopt" '-n -t -x="$exclude" "refs/$topbases"'
247 exit 0
250 if [ -n "$headsonly" ]; then
251 defwithdeps=
252 branches="$(show_heads)"
255 [ -n "$withdeps" ] || withdeps="$defwithdeps"
256 if [ -z "$doingall$terse$graphviz$sort$withdeps$branches" ]; then
257 branches="$(tg info --heads 2>/dev/null | paste -d " " -s -)" || :
258 [ -z "$branches" ] || withdeps=1
260 [ "$withdeps" != "0" ] || withdeps=
261 if [ -n "$withdeps" ]; then
262 savetgish="$tgish"
263 tgish=1
264 origbranches="$branches"
265 branches="$(skip_ann=1; show_deps | sort -u -b -k1,1 | paste -d " " -s -)"
266 tgish="$savetgish"
269 curname="$(strip_ref "$(git symbolic-ref -q HEAD)")" || :
271 if [ -n "$graphviz" ]; then
272 cat <<EOT
273 # GraphViz output; pipe to:
274 # | dot -Tpng -o <output>
275 # or
276 # | dot -Txlib
278 digraph G {
280 graph [
281 rankdir = "TB"
282 label="TopGit Layout\n\n\n"
283 fontsize = 14
284 labelloc=top
285 pad = "0.5,0.5"
291 if [ -n "$sort" ]; then
292 tsort_input="$(get_temp tg-summary-sort)"
293 exec 4>$tsort_input
294 exec 5<$tsort_input
297 ## List branches
299 aheadlist=
300 processed=' '
301 needslist=' '
302 compute_ahead_list()
304 [ -z "$branches" ] || [ -n "$withdeps" ] || return 0
305 [ -n "$withdeps" ] || origbranches="$(tg summary --topgit-heads | paste -d ' ' -s -)"
306 aheadfile="$(get_temp aheadlist)"
307 savebr="$base_remote"
308 savenr="$no_remotes"
309 base_remote=
310 no_remotes=1
311 for onehead in $origbranches; do
312 case "$exclude" in *" $onehead "*) continue; esac
313 case "$processed" in *" $onehead "*) continue; esac
314 processed="${processed}$onehead "
315 needs_update "$onehead" || needslist="${needslist}$onehead "
316 done >"$aheadfile"
317 no_remotes="$savenr"
318 base_remote="$savebr"
319 aheadlist=" $(cut -d ' ' -f 1 <"$aheadfile" | sort -u | paste -d ' ' -s -) "
322 process_branch()
324 missing_deps=
326 current=' '
327 [ "$name" != "$curname" ] || current='>'
328 from=$head_from
329 [ "$name" = "$curname" ] ||
330 from=
331 nonempty=' '
332 ! branch_empty "$name" $from || nonempty='0'
333 remote=' '
334 [ -z "$base_remote" ] || remote='l'
335 ! has_remote "$name" || remote='r'
336 rem_update=' '
337 [ "$remote" != 'r' ] || ! ref_exists "refs/remotes/$base_remote/${topbases#heads/}/$name" || {
338 branch_contains "refs/$topbases/$name" "refs/remotes/$base_remote/${topbases#heads/}/$name" &&
339 branch_contains "refs/heads/$name" "refs/remotes/$base_remote/$name"
340 } || rem_update='R'
341 [ "$remote" != 'r' -o "$rem_update" = 'R' ] || {
342 branch_contains "refs/remotes/$base_remote/$name" "refs/heads/$name" 2>/dev/null
343 } || rem_update='L'
344 deps_update=' '
345 case "$processed" in
346 *" $name "*)
347 case "$needslist" in *" $name "*) deps_update='D'; esac;;
349 needs_update "$name" >/dev/null || deps_update='D';;
350 esac
351 deps_missing=' '
352 [ -z "$missing_deps" ] || deps_missing='!'
353 base_update=' '
354 branch_contains "refs/heads/$name" "refs/$topbases/$name" || base_update='B'
355 ahead=' '
356 case "$aheadlist" in *" $name "*) ahead='*'; esac
358 if [ "$(ref_exists_rev "refs/heads/$name")" != "$(ref_exists_rev "refs/$topbases/$name")" ]; then
359 subject="$(cat_file "refs/heads/$name:.topmsg" $from | sed -n 's/^Subject: //p')"
360 else
361 # No commits yet
362 subject="(No commits)"
365 printf '%-8s %-30s\t%s\n' "$current$nonempty$remote$rem_update$deps_update$deps_missing$base_update$ahead" \
366 "$name" "$subject"
369 if [ -n "$terse" ]; then
370 refslist=
371 [ -z "$tg_read_only" ] || [ -z "$tg_ref_cache" ] || ! [ -s "$tg_ref_cache" ] ||
372 refslist="-r=\"$tg_ref_cache\""
373 cmd="run_awk_topgit_msg --list"
374 [ "${verbose:-0}" != "0" ] || cmd="run_awk_topgit_branches -n"
375 eval "$cmd" "$refslist" '-i="$branches" -x="$exclude" "refs/$topbases"'
376 exit 0
379 [ -n "$graphviz$sort" ] || compute_ahead_list
380 get_branch_list |
381 while read name; do
382 case "$exclude" in *" $name "*) continue; esac
383 if [ -n "$graphviz$sort" ]; then
384 from=$head_from
385 [ "$name" = "$curname" ] ||
386 from=
387 cat_file "refs/heads/$name:.topdeps" $from | while read -r dep || [ -n "$dep" ]; do
388 dep_is_tgish=true
389 ref_exists "refs/$topbases/$dep" ||
390 dep_is_tgish=false
391 [ -z "$tgish" ] || [ "$dep_is_tgish" = "true" ] || continue
392 if ! "$dep_is_tgish" || ! branch_annihilated $dep; then
393 if [ -n "$graphviz" ]; then
394 echo "\"$name\" -> \"$dep\";"
395 if [ "$name" = "$curname" ] || [ "$dep" = "$curname" ]; then
396 echo "\"$curname\" [style=filled,fillcolor=yellow];"
398 else
399 echo "$name $dep" >&4
402 done
403 else
404 process_branch
406 done
408 if [ -n "$graphviz" ]; then
409 echo '}'
412 if [ -n "$sort" ]; then
413 tsort <&5