tg-info: omit annihilated dependencies from list
[topgit/pro.git] / tg-summary.sh
blob8b2f0d8b9a9a10b94a03478ffad3b5ff564421a5
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
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" ] && [ "$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 ] || [ -z "$head" ] || set -- "$head"
117 [ -z "$defwithdeps" ] || [ $# -ne 1 ] || { [ z"$1" != z"HEAD" ] && [ 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" ] && [ "$heads$rdeps" = "11" ]; } ||
122 die "mutually exclusive options given"
123 [ -z "$withdeps" ] || [ -z "$rdeps$depsonly$heads$headsonly" ] ||
124 die "mutually exclusive options given"
126 for b; do
127 [ "$b" != "--all" ] || usage
128 v_verify_topgit_branch b "$b"
129 branches="$branches $b"
130 done
132 get_branch_list()
134 if [ -n "$branches" ]; then
135 if [ -n "$1" ]; then
136 printf '%s\n' $branches | sort -u
137 else
138 printf '%s\n' $branches
140 else
141 non_annihilated_branches
145 show_heads_independent()
147 topics="$(get_temp topics)"
148 get_branch_list | sed -e 's,^\(.*\)$,refs/heads/\1 \1,' |
149 git cat-file --batch-check='%(objectname) %(rest)' |
150 sort -u -b -k1,1 >"$topics"
151 git merge-base --independent $(cut -d ' ' -f 1 <"$topics") |
152 sort -u -b -k1,1 | join - "$topics" | sort -u -b -k2,2 |
153 while read rev name; do
154 case "$exclude" in *" $name "*) continue; esac
155 printf '%s\n' "$name"
156 done
159 show_heads_topgit()
161 [ -z "$head_from" ] || [ -n "$with_deps_opts" ] ||
162 v_get_tdopt with_deps_opts "$head_from"
163 if [ -n "$branches" ]; then
164 eval navigate_deps "$with_deps_opts" -s=-1 -1 -- '"$branches"' | sort
165 else
166 eval navigate_deps "$with_deps_opts" -s=-1
167 fi |
168 while read -r name; do
169 case "$exclude" in *" $name "*) continue; esac
170 printf '%s\n' "$name"
171 done
174 show_heads()
176 if [ -n "$headsindep" ]; then
177 show_heads_independent "$@"
178 else
179 show_heads_topgit "$@"
183 if [ -n "$heads" ] && [ -z "$rdeps" ]; then
184 show_heads
185 exit 0
188 # if $1 is non-empty, show the dep only (including self), not the edge (no self)
189 show_deps()
191 [ -z "$head_from" ] || [ -n "$with_deps_opts" ] ||
192 v_get_tdopt with_deps_opts "$head_from"
193 if [ -n "$branches" ]; then
194 edgenum=2
195 [ -z "$1" ] || edgenum=1
196 no_remotes=1
197 recurse_deps_exclude="$exclude"
198 recurse_deps_internal -n ${tgish:+-t} -m ${1:+-s} -e=$edgenum -- $branches | sort -u
199 else
200 cutcmd=
201 [ -z "$1" ] || cutcmd='| cut -d " " -f 2 | sort -u'
202 refslist=
203 [ -z "$tg_read_only" ] || [ -z "$tg_ref_cache" ] || ! [ -s "$tg_ref_cache" ] ||
204 refslist="-r=\"$tg_ref_cache\""
205 tdopt=
206 eval run_awk_topgit_deps "$refslist" "$with_deps_opts" "${tgish:+-t}" \
207 "${1:+-s}" '-n -x="$exclude" "refs/$topbases"' "$cutcmd"
211 if [ -n "$deps$depsonly$sort" ]; then
212 eval show_deps $depsonly "${sort:+|tsort}"
213 exit 0
216 if [ -n "$rdeps" ]; then
217 no_remotes=1
218 recurse_preorder=1
219 recurse_deps_exclude="$exclude"
220 showbreak=
221 v_get_tdopt with_deps_opts "$head_from"
223 if [ -n "$heads" ]; then
224 show_heads
225 else
226 get_branch_list
228 } | while read -r b; do
229 case "$exclude" in *" $b "*) continue; esac
230 ref_exists "refs/heads/$b" || continue
231 [ -z "$showbreak" ] || echo
232 showbreak=1
233 recurse_deps_internal ${tgish:+-t} -n -s ${rdepsonce:+-o=-1} "$b" |
234 awk -v elided="$rdepsonce" '{
235 if ($1 == "1" || NF < 5) next
236 xvisits = $4
237 dep = $5
238 if ($6 != "") haschild[$6] = 1
239 sub(/^[^ ]+ +[^ ]+ +[^ ]+ +[^ ]+ +[^ ]+/, "")
240 gsub(/ [^ ]+/, " ")
241 xtra = ""
242 if (elided && xvisits > 0 && haschild[dep]) xtra="^"
243 print $0 dep xtra
245 done
246 exit 0
249 if [ -n "$headsonly" ]; then
250 defwithdeps=
251 branches="$(show_heads)"
254 [ -n "$withdeps" ] || withdeps="$defwithdeps"
255 if [ -z "$doingall$terse$graphviz$sort$withdeps$branches" ]; then
256 branches="$(tg info --heads 2>/dev/null | paste -d " " -s -)" || :
257 [ -z "$branches" ] || withdeps=1
259 [ "$withdeps" != "0" ] || withdeps=
260 if [ -n "$withdeps" ]; then
261 v_get_tdopt with_deps_opts "$head_from"
262 [ "$withdeps" != "2" ] || branches="$(show_heads_topgit | paste -d " " -s -)"
263 savetgish="$tgish"
264 tgish=1
265 origbranches="$branches"
266 branches="$(show_deps 1 | paste -d " " -s -)"
267 tgish="$savetgish"
270 if [ -n "$terse" ]; then
271 refslist=
272 [ -z "$tg_read_only" ] || [ -z "$tg_ref_cache" ] || ! [ -s "$tg_ref_cache" ] ||
273 refslist="-r=\"$tg_ref_cache\""
274 cmd="run_awk_topgit_branches -n"
275 if [ $verbose -gt 0 ]; then
276 v_get_tmopt tm_opt "$head_from"
277 cmd="run_awk_topgit_msg --list${tm_opt:+ $tm_opt}"
278 [ $verbose -lt 2 ] || cmd="run_awk_topgit_msg -c -nokind${tm_opt:+ $tm_opt}"
280 eval "$cmd" "$refslist" '-i="$branches" -x="$exclude" "refs/$topbases"'
281 exit 0
284 v_strip_ref curname "$(git symbolic-ref -q HEAD)"
286 if [ -n "$graphviz" ]; then
287 printf '%s\n\n' \
288 '# GraphViz output; pipe to:
289 # | dot -Tpng -o <output>
290 # or
291 # | dot -Txlib
293 digraph G {
295 graph [
296 rankdir = "TB"
297 label="TopGit Layout\n\n\n"
298 fontsize = 14
299 labelloc=top
300 pad = "0.5,0.5"
302 show_deps | while read -r name dep; do
303 printf '"%s" -> "%s";\n' "$name" "$dep"
304 if [ "$name" = "$curname" ] || [ "$dep" = "$curname" ]; then
305 printf '"%s" [%s];\n' "$curname" "style=filled,fillcolor=yellow"
307 done
308 printf '%s\n' '}'
309 exit 0
312 compute_ahead_list()
314 aheadlist=
315 refslist=
316 [ -z "$tg_read_only" ] || [ -z "$tg_ref_cache" ] || ! [ -s "$tg_ref_cache" ] ||
317 refslist="-r=\"$tg_ref_cache\""
318 msgsfile="$(get_temp msgslist)"
319 v_get_tmopt tm_opt "$head_from"
320 eval run_awk_topgit_msg "-nokind${tm_opt:+ $tm_opt}" "$refslist" '"refs/$topbases"' >"$msgsfile"
321 needs_update_check_clear
322 needs_update_check_no_same=1
323 [ -z "$branches" ] || [ -n "$withdeps" ] || return 0
324 v_get_tdopt with_deps_opts "$head_from"
325 [ -n "$withdeps" ] || origbranches="$(navigate_deps -s=-1 | paste -d ' ' -s -)"
326 for onehead in $origbranches; do
327 case "$exclude" in *" $onehead "*) continue; esac
328 needs_update_check $onehead
329 done
330 aheadlist=" $needs_update_ahead "
333 process_branch()
335 missing_deps=
337 current=' '
338 [ "$name" != "$curname" ] || current='>'
339 from=$head_from
340 [ "$name" = "$curname" ] ||
341 from=
342 nonempty=' '
343 ! branch_empty "$name" $from || nonempty='0'
344 remote=' '
345 [ -z "$base_remote" ] || remote='l'
346 ! has_remote "$name" || remote='r'
347 rem_update=' '
348 [ "$remote" != 'r' ] || ! ref_exists "refs/remotes/$base_remote/${topbases#heads/}/$name" || {
349 branch_contains "refs/$topbases/$name" "refs/remotes/$base_remote/${topbases#heads/}/$name" &&
350 branch_contains "refs/heads/$name" "refs/remotes/$base_remote/$name"
351 } || rem_update='R'
352 [ "$remote" != 'r' ] || [ "$rem_update" = 'R' ] || {
353 branch_contains "refs/remotes/$base_remote/$name" "refs/heads/$name" 2>/dev/null
354 } || rem_update='L'
355 needs_update_check "$name"
356 deps_update=' '
357 ! vcontains needs_update_behind "$name" || deps_update='D'
358 deps_missing=' '
359 ! vcontains needs_update_partial "$name" || deps_missing='!'
360 base_update=' '
361 branch_contains "refs/heads/$name" "refs/$topbases/$name" || base_update='B'
362 ahead=' '
363 case "$aheadlist" in *" $name "*) ahead='*'; esac
365 printf '%-8s %s\n' "$current$nonempty$remote$rem_update$deps_update$deps_missing$base_update$ahead" \
366 "$name"
369 awkpgm='
370 BEGIN {
371 if (msgsfile != "") {
372 while ((e = (getline msg <msgsfile)) > 0) {
373 gsub(/[ \t]+/, " ", msg)
374 sub(/^ /, "", msg)
375 if (split(msg, scratch, " ") < 2 ||
376 scratch[1] == "" || scratch[2] == "") continue
377 msg = substr(msg, length(scratch[1]) + 2)
378 msgs[scratch[1]] = msg
380 close(msgsfile)
384 name = substr($0, 10)
385 if (name != "" && name in msgs)
386 printf "%-39s\t%s\n", $0, msgs[name]
387 else
388 print $0
391 msgsfile=
392 compute_ahead_list
393 cmd='get_branch_list | while read name; do process_branch; done'
394 [ -z "$msgsfile" ] || cmd="$cmd"' | awk -v msgsfile="$msgsfile" "$awkpgm"'
395 eval "$cmd"