tg-update.sh: allow multiple branch names
[topgit/pro.git] / tg-summary.sh
blob2bb1be13b24c3ff8eb3fe5ed6f83adc27596e83d
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
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 exclude=
19 tgish=
20 withdeps=
22 ## Parse options
24 usage()
26 echo "Usage: ${tgname:-tg} [...] summary [-t | --list | --heads | --sort | --deps | --deps-only | --rdeps | --graphviz] [-i | -w] [--tgish-only] [--with[out]-deps] [--exclude branch]... [--all | branch...]" >&2
27 exit 1
30 while [ -n "$1" ]; do
31 arg="$1"
32 case "$arg" in
33 -i|-w)
34 [ -z "$head_from" ] || die "-i and -w are mutually exclusive"
35 head_from="$arg";;
36 -t|--list|-l)
37 terse=1;;
38 --heads)
39 heads=1;;
40 --with-deps)
41 head=HEAD
42 withdeps=1;;
43 --without-deps)
44 head=HEAD
45 withdeps=0;;
46 --graphviz)
47 graphviz=1;;
48 --sort)
49 sort=1;;
50 --deps)
51 deps=1;;
52 --tgish-only)
53 tgish=1;;
54 --deps-only)
55 head=HEAD
56 depsonly=1;;
57 --rdeps)
58 head=HEAD
59 rdeps=1;;
60 --all)
61 break;;
62 --exclude=*)
63 [ -n "${1#--exclude=}" ] || die "--exclude= requires a branch name"
64 exclude="$exclude ${1#--exclude=}";;
65 --exclude)
66 shift
67 [ -n "$1" -a "$1" != "--all" ] || die "--exclude requires a branch name"
68 exclude="$exclude $1";;
69 -*)
70 usage;;
72 break;;
73 esac
74 shift
75 done
76 defwithdeps=1
77 [ -z "$exclude" ] || exclude="$exclude "
78 if [ "$1" = "--all" ]; then
79 [ -z "$withdeps" ] || die "mutually exclusive options given"
80 [ $# -eq 1 ] || usage
81 shift
82 head=
83 defwithdeps=
85 [ "$heads$rdeps" != "11" ] || head=
86 [ $# -ne 0 -o -z "$head" ] || set -- "$head"
88 [ "$terse$heads$graphviz$sort$deps$depsonly" = "" ] ||
89 [ "$terse$heads$graphviz$sort$deps$depsonly$rdeps" = "1" ] ||
90 [ "$terse$heads$graphviz$sort$deps$depsonly$rdeps" = "11" -a "$heads$rdeps" = "11" ] ||
91 die "mutually exclusive options given"
92 [ -z "$withdeps" -o -z "$rdeps$depsonly$heads" ] ||
93 die "mutually exclusive options given"
95 for b; do
96 [ "$b" != "--all" ] || usage
97 branches="$branches $(verify_topgit_branch "$b")"
98 done
100 get_branch_list()
102 if [ -n "$branches" ]; then
103 printf '%s\n' $branches
104 else
105 non_annihilated_branches
109 show_heads()
111 topics="$(get_temp topics)"
112 get_branch_list | sed -e 's,^\(.*\)$,refs/heads/\1 \1,' |
113 git cat-file --batch-check='%(objectname) %(rest)' |
114 sort -u -b -k1,1 >"$topics"
115 git merge-base --independent $(cut -d ' ' -f 1 <"$topics") |
116 sort -u -b -k1,1 | join - "$topics" | sort -u -b -k2,2 |
117 while read rev name; do
118 case "$exclude" in *" $name "*) continue; esac
119 printf '%s\n' "$name"
120 done
123 if [ -n "$heads" -a -z "$rdeps" ]; then
124 show_heads
125 exit 0
128 show_dep() {
129 case "$exclude" in *" $_dep "*) return; esac
130 case " $seen_deps " in *" $_dep "*) return 0; esac
131 seen_deps="${seen_deps:+$seen_deps }$_dep"
132 [ -z "$tgish" -o -n "$_dep_is_tgish" ] || return 0
133 printf '%s\n' "$_dep"
136 show_deps()
138 no_remotes=1
139 recurse_deps_exclude=
140 get_branch_list | while read _b; do
141 case "$exclude" in *" $_b "*) continue; esac
142 case " $recurse_deps_exclude " in *" $_b "*) continue; esac
143 seen_deps=
144 _dep="$_b"; _dep_is_tgish=1; show_dep
145 recurse_deps show_dep "$_b"
146 recurse_deps_exclude="$recurse_deps_exclude $seen_deps"
147 done
150 if [ -n "$depsonly" ]; then
151 show_deps | LC_ALL=C sort -u -b -k1,1
152 exit 0
155 show_rdeps()
157 case "$exclude" in *" $_dep "*) return; esac
158 [ -z "$tgish" -o -n "$_dep_is_tgish" ] || return 0
159 printf '%s %s\n' "$_depchain" "$_dep"
162 if [ -n "$rdeps" ]; then
163 no_remotes=1
164 showbreak=
166 if [ -n "$heads" ]; then
167 show_heads
168 else
169 get_branch_list
171 } | while read b; do
172 case "$exclude" in *" $b "*) continue; esac
173 [ -z "$showbreak" ] || echo
174 showbreak=1
175 ref_exists "refs/heads/$b" || continue
177 echol "$b"
178 recurse_preorder=1
179 recurse_deps show_rdeps "$b"
180 } | sed -e 's/[^ ][^ ]*[ ]/ /g'
181 done
182 exit 0
185 [ -n "$withdeps" ] || withdeps="$defwithdeps"
186 [ "$withdeps" != "0" ] || withdeps=
187 if [ -n "$withdeps" ]; then
188 savetgish="$tgish"
189 tgish=1
190 branches="$(show_deps | LC_ALL=C sort -u -b -k1,1)"
191 tgish="$savetgish"
194 curname="$(strip_ref "$(git symbolic-ref -q HEAD)")" || :
196 if [ -n "$graphviz" ]; then
197 cat <<EOT
198 # GraphViz output; pipe to:
199 # | dot -Tpng -o <output>
200 # or
201 # | dot -Txlib
203 digraph G {
205 graph [
206 rankdir = "TB"
207 label="TopGit Layout\n\n\n"
208 fontsize = 14
209 labelloc=top
210 pad = "0.5,0.5"
216 if [ -n "$sort" ]; then
217 tsort_input="$(get_temp tg-summary-sort)"
218 exec 4>$tsort_input
219 exec 5<$tsort_input
222 ## List branches
224 process_branch()
226 missing_deps=
228 current=' '
229 [ "$name" != "$curname" ] || current='>'
230 from=$head_from
231 [ "$name" = "$curname" ] ||
232 from=
233 nonempty=' '
234 ! branch_empty "$name" $from || nonempty='0'
235 remote=' '
236 [ -z "$base_remote" ] || remote='l'
237 ! has_remote "$name" || remote='r'
238 rem_update=' '
239 [ "$remote" != 'r' ] || ! ref_exists "refs/remotes/$base_remote/$topbases/$name" || {
240 branch_contains "refs/$topbases/$name" "refs/remotes/$base_remote/$topbases/$name" &&
241 branch_contains "refs/heads/$name" "refs/remotes/$base_remote/$name"
242 } || rem_update='R'
243 [ "$remote" != 'r' -o "$rem_update" = 'R' ] || {
244 branch_contains "refs/remotes/$base_remote/$name" "refs/heads/$name" 2>/dev/null
245 } || rem_update='L'
246 deps_update=' '
247 needs_update "$name" >/dev/null || deps_update='D'
248 deps_missing=' '
249 [ -z "$missing_deps" ] || deps_missing='!'
250 base_update=' '
251 branch_contains "refs/heads/$name" "refs/$topbases/$name" || base_update='B'
253 if [ "$(ref_exists_rev "refs/heads/$name")" != "$(ref_exists_rev "refs/$topbases/$name")" ]; then
254 subject="$(cat_file "refs/heads/$name:.topmsg" $from | sed -n 's/^Subject: //p')"
255 else
256 # No commits yet
257 subject="(No commits)"
260 printf '%s\t%-31s\t%s\n' "$current$nonempty$remote$rem_update$deps_update$deps_missing$base_update" \
261 "$name" "$subject"
264 if [ -n "$deps" ]; then
265 if [ -n "$branches" ]; then
266 get_branch_list |
267 while read b; do
268 case "$exclude" in *" $b "*) continue; esac
269 list_deps $head_from $b |
270 while read name dep; do
271 case "$exclude" in *" $dep "*) continue; esac
272 [ -z "$tgish" ] || ref_exists "refs/$topbases/$dep" || continue
273 echo "$name $dep"
274 done
275 done
276 else
277 list_deps $head_from |
278 while read name dep; do
279 case "$exclude" in *" $dep "*) continue; esac
280 [ -z "$tgish" ] || ref_exists "refs/$topbases/$dep" || continue
281 echo "$name $dep"
282 done
284 exit 0
287 get_branch_list |
288 while read name; do
289 case "$exclude" in *" $name "*) continue; esac
290 if [ -n "$terse" ]; then
291 echol "$name"
292 elif [ -n "$graphviz$sort" ]; then
293 from=$head_from
294 [ "$name" = "$curname" ] ||
295 from=
296 cat_file "refs/heads/$name:.topdeps" $from | while read dep; do
297 dep_is_tgish=true
298 ref_exists "refs/$topbases/$dep" ||
299 dep_is_tgish=false
300 [ -z "$tgish" ] || [ "$dep_is_tgish" = "true" ] || continue
301 if ! "$dep_is_tgish" || ! branch_annihilated $dep; then
302 if [ -n "$graphviz" ]; then
303 echo "\"$name\" -> \"$dep\";"
304 if [ "$name" = "$curname" ] || [ "$dep" = "$curname" ]; then
305 echo "\"$curname\" [style=filled,fillcolor=yellow];"
307 else
308 echo "$name $dep" >&4
311 done
312 else
313 process_branch
315 done
317 if [ -n "$graphviz" ]; then
318 echo '}'
321 if [ -n "$sort" ]; then
322 tsort <&5
326 # vim:noet