summary/revert: make --heads DWIM and add alternative
[topgit/pro.git] / tg-summary.sh
blobf310ce6b7ca15e77fd901fcdba53b88f04252682
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 head_from=
15 branches=
16 head=
17 heads=
18 headsindep=
19 headsonly=
20 exclude=
21 tgish=
22 withdeps=
24 ## Parse options
26 usage()
28 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
29 exit 1
32 while [ -n "$1" ]; do
33 arg="$1"
34 case "$arg" in
35 -i|-w)
36 [ -z "$head_from" ] || die "-i and -w are mutually exclusive"
37 head_from="$arg";;
38 -t|--list|-l)
39 terse=1;;
40 --heads|--topgit-heads)
41 heads=1
42 headsindep=;;
43 --heads-independent)
44 heads=1
45 headsindep=1;;
46 --heads-only)
47 headsonly=1;;
48 --with-deps)
49 head=HEAD
50 withdeps=1;;
51 --without-deps)
52 head=HEAD
53 withdeps=0;;
54 --graphviz)
55 graphviz=1;;
56 --sort)
57 sort=1;;
58 --deps)
59 deps=1;;
60 --tgish-only)
61 tgish=1;;
62 --deps-only)
63 head=HEAD
64 depsonly=1;;
65 --rdeps)
66 head=HEAD
67 rdeps=1;;
68 --all)
69 break;;
70 --exclude=*)
71 [ -n "${1#--exclude=}" ] || die "--exclude= requires a branch name"
72 exclude="$exclude ${1#--exclude=}";;
73 --exclude)
74 shift
75 [ -n "$1" -a "$1" != "--all" ] || die "--exclude requires a branch name"
76 exclude="$exclude $1";;
77 -*)
78 usage;;
80 break;;
81 esac
82 shift
83 done
84 [ $# -eq 0 ] || defwithdeps=1
85 [ -z "$exclude" ] || exclude="$exclude "
86 if [ "$1" = "--all" ]; then
87 [ -z "$withdeps" ] || die "mutually exclusive options given"
88 [ $# -eq 1 ] || usage
89 shift
90 head=
91 defwithdeps=
93 [ "$heads$rdeps" != "11" ] || head=
94 [ $# -ne 0 -o -z "$head" ] || set -- "$head"
96 [ "$terse$heads$headsonly$graphviz$sort$deps$depsonly" = "" ] ||
97 [ "$terse$heads$headsonly$graphviz$sort$deps$depsonly$rdeps" = "1" ] ||
98 [ "$terse$heads$headsonly$graphviz$sort$deps$depsonly$rdeps" = "11" -a "$heads$rdeps" = "11" ] ||
99 die "mutually exclusive options given"
100 [ -z "$withdeps" -o -z "$rdeps$depsonly$heads$headsonly" ] ||
101 die "mutually exclusive options given"
103 for b; do
104 [ "$b" != "--all" ] || usage
105 branches="$branches $(verify_topgit_branch "$b")"
106 done
108 get_branch_list()
110 if [ -n "$branches" ]; then
111 if [ -n "$1" ]; then
112 printf '%s\n' $branches | LC_ALL=C sort -u
113 else
114 printf '%s\n' $branches
116 else
117 non_annihilated_branches
121 show_heads_independent()
123 topics="$(get_temp topics)"
124 get_branch_list | sed -e 's,^\(.*\)$,refs/heads/\1 \1,' |
125 git cat-file --batch-check='%(objectname) %(rest)' |
126 sort -u -b -k1,1 >"$topics"
127 git merge-base --independent $(cut -d ' ' -f 1 <"$topics") |
128 sort -u -b -k1,1 | join - "$topics" | sort -u -b -k2,2 |
129 while read rev name; do
130 case "$exclude" in *" $name "*) continue; esac
131 printf '%s\n' "$name"
132 done
135 show_heads_topgit()
137 topics="$(get_temp topics)"
138 topics2=
139 [ -z "$branches" ] || topics2="$(get_temp topics2)"
140 deplist="$(get_temp deplist)"
141 get_branch_list 1 >"$topics"
142 while read -r onetopic; do
143 if [ -n "$branches" ]; then
144 ! branch_annihilated "$onetopic" || continue
145 echol "$onetopic" >>"$topics2"
147 cat_deps "$onetopic"
148 done <"$topics" | LC_ALL=C sort -u >"$deplist"
149 [ -z "$branches" ] || topics="$topics2"
150 join -v 1 "$topics" "$deplist" |
151 while read -r name; do
152 case "$exclude" in *" $name "*) continue; esac
153 printf '%s\n' "$name"
154 done
157 show_heads()
159 if [ -n "$headsindep" ]; then
160 show_heads_independent "$@"
161 else
162 show_heads_topgit "$@"
166 if [ -n "$heads" -a -z "$rdeps" ]; then
167 show_heads
168 exit 0
171 skip_ann=
172 show_dep() {
173 case "$exclude" in *" $_dep "*) return; esac
174 case " $seen_deps " in *" $_dep "*) return 0; esac
175 seen_deps="${seen_deps:+$seen_deps }$_dep"
176 [ -z "$tgish" -o -n "$_dep_is_tgish" ] || return 0
177 [ -z "$skip_ann" ] || ! branch_annihilated "$_dep" && printf '%s\n' "$_dep"
178 return 0
181 show_deps()
183 no_remotes=1
184 recurse_deps_exclude=
185 get_branch_list | while read _b; do
186 case "$exclude" in *" $_b "*) continue; esac
187 case " $recurse_deps_exclude " in *" $_b "*) continue; esac
188 seen_deps=
189 save_skip="$skip_ann"
190 _dep="$_b"; _dep_is_tgish=1; skip_ann=; show_dep; skip_ann="$save_skip"
191 recurse_deps show_dep "$_b"
192 recurse_deps_exclude="$recurse_deps_exclude $seen_deps"
193 done
196 if [ -n "$depsonly" ]; then
197 show_deps | LC_ALL=C sort -u -b -k1,1
198 exit 0
201 show_rdeps()
203 case "$exclude" in *" $_dep "*) return; esac
204 [ -z "$tgish" -o -n "$_dep_is_tgish" ] || return 0
205 printf '%s %s\n' "$_depchain" "$_dep"
208 if [ -n "$rdeps" ]; then
209 no_remotes=1
210 showbreak=
212 if [ -n "$heads" ]; then
213 show_heads
214 else
215 get_branch_list
217 } | while read b; do
218 case "$exclude" in *" $b "*) continue; esac
219 [ -z "$showbreak" ] || echo
220 showbreak=1
221 ref_exists "refs/heads/$b" || continue
223 echol "$b"
224 recurse_preorder=1
225 recurse_deps show_rdeps "$b"
226 } | sed -e 's/[^ ][^ ]*[ ]/ /g'
227 done
228 exit 0
231 if [ -n "$headsonly" ]; then
232 defwithdeps=
233 branches="$(show_heads)"
236 [ -n "$withdeps" ] || withdeps="$defwithdeps"
237 [ "$withdeps" != "0" ] || withdeps=
238 if [ -n "$withdeps" ]; then
239 savetgish="$tgish"
240 tgish=1
241 branches="$(skip_ann=1; show_deps | LC_ALL=C sort -u -b -k1,1)"
242 tgish="$savetgish"
245 curname="$(strip_ref "$(git symbolic-ref -q HEAD)")" || :
247 if [ -n "$graphviz" ]; then
248 cat <<EOT
249 # GraphViz output; pipe to:
250 # | dot -Tpng -o <output>
251 # or
252 # | dot -Txlib
254 digraph G {
256 graph [
257 rankdir = "TB"
258 label="TopGit Layout\n\n\n"
259 fontsize = 14
260 labelloc=top
261 pad = "0.5,0.5"
267 if [ -n "$sort" ]; then
268 tsort_input="$(get_temp tg-summary-sort)"
269 exec 4>$tsort_input
270 exec 5<$tsort_input
273 ## List branches
275 process_branch()
277 missing_deps=
279 current=' '
280 [ "$name" != "$curname" ] || current='>'
281 from=$head_from
282 [ "$name" = "$curname" ] ||
283 from=
284 nonempty=' '
285 ! branch_empty "$name" $from || nonempty='0'
286 remote=' '
287 [ -z "$base_remote" ] || remote='l'
288 ! has_remote "$name" || remote='r'
289 rem_update=' '
290 [ "$remote" != 'r' ] || ! ref_exists "refs/remotes/$base_remote/${topbases#heads/}/$name" || {
291 branch_contains "refs/$topbases/$name" "refs/remotes/$base_remote/${topbases#heads/}/$name" &&
292 branch_contains "refs/heads/$name" "refs/remotes/$base_remote/$name"
293 } || rem_update='R'
294 [ "$remote" != 'r' -o "$rem_update" = 'R' ] || {
295 branch_contains "refs/remotes/$base_remote/$name" "refs/heads/$name" 2>/dev/null
296 } || rem_update='L'
297 deps_update=' '
298 needs_update "$name" >/dev/null || deps_update='D'
299 deps_missing=' '
300 [ -z "$missing_deps" ] || deps_missing='!'
301 base_update=' '
302 branch_contains "refs/heads/$name" "refs/$topbases/$name" || base_update='B'
304 if [ "$(ref_exists_rev "refs/heads/$name")" != "$(ref_exists_rev "refs/$topbases/$name")" ]; then
305 subject="$(cat_file "refs/heads/$name:.topmsg" $from | sed -n 's/^Subject: //p')"
306 else
307 # No commits yet
308 subject="(No commits)"
311 printf '%s\t%-31s\t%s\n' "$current$nonempty$remote$rem_update$deps_update$deps_missing$base_update" \
312 "$name" "$subject"
315 if [ -n "$deps" ]; then
316 if [ -n "$branches" ]; then
317 get_branch_list |
318 while read b; do
319 case "$exclude" in *" $b "*) continue; esac
320 list_deps $head_from $b |
321 while read name dep; do
322 case "$exclude" in *" $dep "*) continue; esac
323 [ -z "$tgish" ] || ref_exists "refs/$topbases/$dep" || continue
324 echo "$name $dep"
325 done
326 done
327 else
328 list_deps $head_from |
329 while read name dep; do
330 case "$exclude" in *" $dep "*) continue; esac
331 [ -z "$tgish" ] || ref_exists "refs/$topbases/$dep" || continue
332 echo "$name $dep"
333 done
335 exit 0
338 get_branch_list |
339 while read name; do
340 case "$exclude" in *" $name "*) continue; esac
341 if [ -n "$terse" ]; then
342 echol "$name"
343 elif [ -n "$graphviz$sort" ]; then
344 from=$head_from
345 [ "$name" = "$curname" ] ||
346 from=
347 cat_file "refs/heads/$name:.topdeps" $from | while read dep; do
348 dep_is_tgish=true
349 ref_exists "refs/$topbases/$dep" ||
350 dep_is_tgish=false
351 [ -z "$tgish" ] || [ "$dep_is_tgish" = "true" ] || continue
352 if ! "$dep_is_tgish" || ! branch_annihilated $dep; then
353 if [ -n "$graphviz" ]; then
354 echo "\"$name\" -> \"$dep\";"
355 if [ "$name" = "$curname" ] || [ "$dep" = "$curname" ]; then
356 echo "\"$curname\" [style=filled,fillcolor=yellow];"
358 else
359 echo "$name $dep" >&4
362 done
363 else
364 process_branch
366 done
368 if [ -n "$graphviz" ]; then
369 echo '}'
372 if [ -n "$sort" ]; then
373 tsort <&5
377 # vim:noet