t1050: test tg status output
[topgit/pro.git] / tg-summary.sh
blobc19d946bbb8cb167756362f64907313fefb9c1f7
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 headsonly=
19 exclude=
20 tgish=
21 withdeps=
23 ## Parse options
25 usage()
27 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
28 exit 1
31 while [ -n "$1" ]; do
32 arg="$1"
33 case "$arg" in
34 -i|-w)
35 [ -z "$head_from" ] || die "-i and -w are mutually exclusive"
36 head_from="$arg";;
37 -t|--list|-l)
38 terse=1;;
39 --heads)
40 heads=1;;
41 --heads-only)
42 headsonly=1;;
43 --with-deps)
44 head=HEAD
45 withdeps=1;;
46 --without-deps)
47 head=HEAD
48 withdeps=0;;
49 --graphviz)
50 graphviz=1;;
51 --sort)
52 sort=1;;
53 --deps)
54 deps=1;;
55 --tgish-only)
56 tgish=1;;
57 --deps-only)
58 head=HEAD
59 depsonly=1;;
60 --rdeps)
61 head=HEAD
62 rdeps=1;;
63 --all)
64 break;;
65 --exclude=*)
66 [ -n "${1#--exclude=}" ] || die "--exclude= requires a branch name"
67 exclude="$exclude ${1#--exclude=}";;
68 --exclude)
69 shift
70 [ -n "$1" -a "$1" != "--all" ] || die "--exclude requires a branch name"
71 exclude="$exclude $1";;
72 -*)
73 usage;;
75 break;;
76 esac
77 shift
78 done
79 [ $# -eq 0 ] || defwithdeps=1
80 [ -z "$exclude" ] || exclude="$exclude "
81 if [ "$1" = "--all" ]; then
82 [ -z "$withdeps" ] || die "mutually exclusive options given"
83 [ $# -eq 1 ] || usage
84 shift
85 head=
86 defwithdeps=
88 [ "$heads$rdeps" != "11" ] || head=
89 [ $# -ne 0 -o -z "$head" ] || set -- "$head"
91 [ "$terse$heads$headsonly$graphviz$sort$deps$depsonly" = "" ] ||
92 [ "$terse$heads$headsonly$graphviz$sort$deps$depsonly$rdeps" = "1" ] ||
93 [ "$terse$heads$headsonly$graphviz$sort$deps$depsonly$rdeps" = "11" -a "$heads$rdeps" = "11" ] ||
94 die "mutually exclusive options given"
95 [ -z "$withdeps" -o -z "$rdeps$depsonly$heads$headsonly" ] ||
96 die "mutually exclusive options given"
98 for b; do
99 [ "$b" != "--all" ] || usage
100 branches="$branches $(verify_topgit_branch "$b")"
101 done
103 get_branch_list()
105 if [ -n "$branches" ]; then
106 printf '%s\n' $branches
107 else
108 non_annihilated_branches
112 show_heads()
114 topics="$(get_temp topics)"
115 get_branch_list | sed -e 's,^\(.*\)$,refs/heads/\1 \1,' |
116 git cat-file --batch-check='%(objectname) %(rest)' |
117 sort -u -b -k1,1 >"$topics"
118 git merge-base --independent $(cut -d ' ' -f 1 <"$topics") |
119 sort -u -b -k1,1 | join - "$topics" | sort -u -b -k2,2 |
120 while read rev name; do
121 case "$exclude" in *" $name "*) continue; esac
122 printf '%s\n' "$name"
123 done
126 if [ -n "$heads" -a -z "$rdeps" ]; then
127 show_heads
128 exit 0
131 skip_ann=
132 show_dep() {
133 case "$exclude" in *" $_dep "*) return; esac
134 case " $seen_deps " in *" $_dep "*) return 0; esac
135 seen_deps="${seen_deps:+$seen_deps }$_dep"
136 [ -z "$tgish" -o -n "$_dep_is_tgish" ] || return 0
137 [ -z "$skip_ann" ] || ! branch_annihilated "$_dep" && printf '%s\n' "$_dep"
138 return 0
141 show_deps()
143 no_remotes=1
144 recurse_deps_exclude=
145 get_branch_list | while read _b; do
146 case "$exclude" in *" $_b "*) continue; esac
147 case " $recurse_deps_exclude " in *" $_b "*) continue; esac
148 seen_deps=
149 save_skip="$skip_ann"
150 _dep="$_b"; _dep_is_tgish=1; skip_ann=; show_dep; skip_ann="$save_skip"
151 recurse_deps show_dep "$_b"
152 recurse_deps_exclude="$recurse_deps_exclude $seen_deps"
153 done
156 if [ -n "$depsonly" ]; then
157 show_deps | LC_ALL=C sort -u -b -k1,1
158 exit 0
161 show_rdeps()
163 case "$exclude" in *" $_dep "*) return; esac
164 [ -z "$tgish" -o -n "$_dep_is_tgish" ] || return 0
165 printf '%s %s\n' "$_depchain" "$_dep"
168 if [ -n "$rdeps" ]; then
169 no_remotes=1
170 showbreak=
172 if [ -n "$heads" ]; then
173 show_heads
174 else
175 get_branch_list
177 } | while read b; do
178 case "$exclude" in *" $b "*) continue; esac
179 [ -z "$showbreak" ] || echo
180 showbreak=1
181 ref_exists "refs/heads/$b" || continue
183 echol "$b"
184 recurse_preorder=1
185 recurse_deps show_rdeps "$b"
186 } | sed -e 's/[^ ][^ ]*[ ]/ /g'
187 done
188 exit 0
191 if [ -n "$headsonly" ]; then
192 defwithdeps=
193 branches="$(show_heads)"
196 [ -n "$withdeps" ] || withdeps="$defwithdeps"
197 [ "$withdeps" != "0" ] || withdeps=
198 if [ -n "$withdeps" ]; then
199 savetgish="$tgish"
200 tgish=1
201 branches="$(skip_ann=1; show_deps | LC_ALL=C sort -u -b -k1,1)"
202 tgish="$savetgish"
205 curname="$(strip_ref "$(git symbolic-ref -q HEAD)")" || :
207 if [ -n "$graphviz" ]; then
208 cat <<EOT
209 # GraphViz output; pipe to:
210 # | dot -Tpng -o <output>
211 # or
212 # | dot -Txlib
214 digraph G {
216 graph [
217 rankdir = "TB"
218 label="TopGit Layout\n\n\n"
219 fontsize = 14
220 labelloc=top
221 pad = "0.5,0.5"
227 if [ -n "$sort" ]; then
228 tsort_input="$(get_temp tg-summary-sort)"
229 exec 4>$tsort_input
230 exec 5<$tsort_input
233 ## List branches
235 process_branch()
237 missing_deps=
239 current=' '
240 [ "$name" != "$curname" ] || current='>'
241 from=$head_from
242 [ "$name" = "$curname" ] ||
243 from=
244 nonempty=' '
245 ! branch_empty "$name" $from || nonempty='0'
246 remote=' '
247 [ -z "$base_remote" ] || remote='l'
248 ! has_remote "$name" || remote='r'
249 rem_update=' '
250 [ "$remote" != 'r' ] || ! ref_exists "refs/remotes/$base_remote/${topbases#heads/}/$name" || {
251 branch_contains "refs/$topbases/$name" "refs/remotes/$base_remote/${topbases#heads/}/$name" &&
252 branch_contains "refs/heads/$name" "refs/remotes/$base_remote/$name"
253 } || rem_update='R'
254 [ "$remote" != 'r' -o "$rem_update" = 'R' ] || {
255 branch_contains "refs/remotes/$base_remote/$name" "refs/heads/$name" 2>/dev/null
256 } || rem_update='L'
257 deps_update=' '
258 needs_update "$name" >/dev/null || deps_update='D'
259 deps_missing=' '
260 [ -z "$missing_deps" ] || deps_missing='!'
261 base_update=' '
262 branch_contains "refs/heads/$name" "refs/$topbases/$name" || base_update='B'
264 if [ "$(ref_exists_rev "refs/heads/$name")" != "$(ref_exists_rev "refs/$topbases/$name")" ]; then
265 subject="$(cat_file "refs/heads/$name:.topmsg" $from | sed -n 's/^Subject: //p')"
266 else
267 # No commits yet
268 subject="(No commits)"
271 printf '%s\t%-31s\t%s\n' "$current$nonempty$remote$rem_update$deps_update$deps_missing$base_update" \
272 "$name" "$subject"
275 if [ -n "$deps" ]; then
276 if [ -n "$branches" ]; then
277 get_branch_list |
278 while read b; do
279 case "$exclude" in *" $b "*) continue; esac
280 list_deps $head_from $b |
281 while read name dep; do
282 case "$exclude" in *" $dep "*) continue; esac
283 [ -z "$tgish" ] || ref_exists "refs/$topbases/$dep" || continue
284 echo "$name $dep"
285 done
286 done
287 else
288 list_deps $head_from |
289 while read name dep; do
290 case "$exclude" in *" $dep "*) continue; esac
291 [ -z "$tgish" ] || ref_exists "refs/$topbases/$dep" || continue
292 echo "$name $dep"
293 done
295 exit 0
298 get_branch_list |
299 while read name; do
300 case "$exclude" in *" $name "*) continue; esac
301 if [ -n "$terse" ]; then
302 echol "$name"
303 elif [ -n "$graphviz$sort" ]; then
304 from=$head_from
305 [ "$name" = "$curname" ] ||
306 from=
307 cat_file "refs/heads/$name:.topdeps" $from | while read dep; do
308 dep_is_tgish=true
309 ref_exists "refs/$topbases/$dep" ||
310 dep_is_tgish=false
311 [ -z "$tgish" ] || [ "$dep_is_tgish" = "true" ] || continue
312 if ! "$dep_is_tgish" || ! branch_annihilated $dep; then
313 if [ -n "$graphviz" ]; then
314 echo "\"$name\" -> \"$dep\";"
315 if [ "$name" = "$curname" ] || [ "$dep" = "$curname" ]; then
316 echo "\"$curname\" [style=filled,fillcolor=yellow];"
318 else
319 echo "$name $dep" >&4
322 done
323 else
324 process_branch
326 done
328 if [ -n "$graphviz" ]; then
329 echo '}'
332 if [ -n "$sort" ]; then
333 tsort <&5
337 # vim:noet