tg-patch.sh: get all awked up on --from
[topgit/pro.git] / tg-summary.sh
blob51fa92760eeda13efba3f63571e32b7505e79be3
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 "$headsonly" ]; then
235 defwithdeps=
236 branches="$(show_heads)"
239 [ -n "$withdeps" ] || withdeps="$defwithdeps"
240 if [ -z "$doingall$terse$graphviz$sort$deps$withdeps$branches" ]; then
241 branches="$(tg info --heads 2>/dev/null | paste -d " " -s -)" || :
242 [ -z "$branches" ] || withdeps=1
244 [ "$withdeps" != "0" ] || withdeps=
245 if [ -n "$withdeps" ]; then
246 savetgish="$tgish"
247 tgish=1
248 origbranches="$branches"
249 branches="$(skip_ann=1; show_deps | sort -u -b -k1,1 | paste -d " " -s -)"
250 tgish="$savetgish"
253 curname="$(strip_ref "$(git symbolic-ref -q HEAD)")" || :
255 if [ -n "$graphviz" ]; then
256 cat <<EOT
257 # GraphViz output; pipe to:
258 # | dot -Tpng -o <output>
259 # or
260 # | dot -Txlib
262 digraph G {
264 graph [
265 rankdir = "TB"
266 label="TopGit Layout\n\n\n"
267 fontsize = 14
268 labelloc=top
269 pad = "0.5,0.5"
275 if [ -n "$sort" ]; then
276 tsort_input="$(get_temp tg-summary-sort)"
277 exec 4>$tsort_input
278 exec 5<$tsort_input
281 ## List branches
283 aheadlist=
284 processed=' '
285 needslist=' '
286 compute_ahead_list()
288 [ -z "$branches" ] || [ -n "$withdeps" ] || return 0
289 [ -n "$withdeps" ] || origbranches="$(tg summary --topgit-heads | paste -d ' ' -s -)"
290 aheadfile="$(get_temp aheadlist)"
291 savebr="$base_remote"
292 savenr="$no_remotes"
293 base_remote=
294 no_remotes=1
295 for onehead in $origbranches; do
296 case "$exclude" in *" $onehead "*) continue; esac
297 case "$processed" in *" $onehead "*) continue; esac
298 processed="${processed}$onehead "
299 needs_update "$onehead" || needslist="${needslist}$onehead "
300 done >"$aheadfile"
301 no_remotes="$savenr"
302 base_remote="$savebr"
303 aheadlist=" $(cut -d ' ' -f 1 <"$aheadfile" | sort -u | paste -d ' ' -s -) "
306 process_branch()
308 missing_deps=
310 current=' '
311 [ "$name" != "$curname" ] || current='>'
312 from=$head_from
313 [ "$name" = "$curname" ] ||
314 from=
315 nonempty=' '
316 ! branch_empty "$name" $from || nonempty='0'
317 remote=' '
318 [ -z "$base_remote" ] || remote='l'
319 ! has_remote "$name" || remote='r'
320 rem_update=' '
321 [ "$remote" != 'r' ] || ! ref_exists "refs/remotes/$base_remote/${topbases#heads/}/$name" || {
322 branch_contains "refs/$topbases/$name" "refs/remotes/$base_remote/${topbases#heads/}/$name" &&
323 branch_contains "refs/heads/$name" "refs/remotes/$base_remote/$name"
324 } || rem_update='R'
325 [ "$remote" != 'r' -o "$rem_update" = 'R' ] || {
326 branch_contains "refs/remotes/$base_remote/$name" "refs/heads/$name" 2>/dev/null
327 } || rem_update='L'
328 deps_update=' '
329 case "$processed" in
330 *" $name "*)
331 case "$needslist" in *" $name "*) deps_update='D'; esac;;
333 needs_update "$name" >/dev/null || deps_update='D';;
334 esac
335 deps_missing=' '
336 [ -z "$missing_deps" ] || deps_missing='!'
337 base_update=' '
338 branch_contains "refs/heads/$name" "refs/$topbases/$name" || base_update='B'
339 ahead=' '
340 case "$aheadlist" in *" $name "*) ahead='*'; esac
342 if [ "$(ref_exists_rev "refs/heads/$name")" != "$(ref_exists_rev "refs/$topbases/$name")" ]; then
343 subject="$(cat_file "refs/heads/$name:.topmsg" $from | sed -n 's/^Subject: //p')"
344 else
345 # No commits yet
346 subject="(No commits)"
349 printf '%-8s %-30s\t%s\n' "$current$nonempty$remote$rem_update$deps_update$deps_missing$base_update$ahead" \
350 "$name" "$subject"
353 if [ -n "$deps" ]; then
354 if [ -n "$branches" ]; then
355 get_branch_list |
356 while read b; do
357 case "$exclude" in *" $b "*) continue; esac
358 list_deps $head_from $b |
359 while read name dep; do
360 case "$exclude" in *" $dep "*) continue; esac
361 [ -z "$tgish" ] || ref_exists "refs/$topbases/$dep" || continue
362 echo "$name $dep"
363 done
364 done
365 else
366 list_deps $head_from |
367 while read name dep; do
368 case "$exclude" in *" $dep "*) continue; esac
369 [ -z "$tgish" ] || ref_exists "refs/$topbases/$dep" || continue
370 echo "$name $dep"
371 done
373 exit 0
376 if [ -n "$terse" ]; then
377 refslist=
378 [ -z "$tg_read_only" ] || [ -z "$tg_ref_cache" ] || ! [ -s "$tg_ref_cache" ] ||
379 refslist="-r=\"$tg_ref_cache\""
380 cmd="run_awk_topgit_msg --list"
381 [ "${verbose:-0}" != "0" ] || cmd="run_awk_topgit_branches -n"
382 eval "$cmd" "$refslist" '-i="$branches" -x="$exclude" "refs/$topbases"'
383 exit 0
386 [ -n "$graphviz$sort" ] || compute_ahead_list
387 get_branch_list |
388 while read name; do
389 case "$exclude" in *" $name "*) continue; esac
390 if [ -n "$graphviz$sort" ]; then
391 from=$head_from
392 [ "$name" = "$curname" ] ||
393 from=
394 cat_file "refs/heads/$name:.topdeps" $from | while read dep; do
395 dep_is_tgish=true
396 ref_exists "refs/$topbases/$dep" ||
397 dep_is_tgish=false
398 [ -z "$tgish" ] || [ "$dep_is_tgish" = "true" ] || continue
399 if ! "$dep_is_tgish" || ! branch_annihilated $dep; then
400 if [ -n "$graphviz" ]; then
401 echo "\"$name\" -> \"$dep\";"
402 if [ "$name" = "$curname" ] || [ "$dep" = "$curname" ]; then
403 echo "\"$curname\" [style=filled,fillcolor=yellow];"
405 else
406 echo "$name $dep" >&4
409 done
410 else
411 process_branch
413 done
415 if [ -n "$graphviz" ]; then
416 echo '}'
419 if [ -n "$sort" ]; then
420 tsort <&5