topgit: version 0.19.11
[topgit/pro.git] / awk / topgit_branches.awk
blob7cb01b81bd78155fb39b5260bb5ef514ca178094
1 #!/usr/bin/awk -f
3 # topgit_branches - TopGit awk utility script used by tg--awksome
4 # Copyright (C) 2017 Kyle J. McKay <mackyle@gmail.com>
5 # All rights reserved.
6 # License GPLv2
8 # topgit_branches
10 # variable arguments (-v):
12 # brfile if non-empty, the named file gets a copy of the output stream
13 # anfile if non-empty, annihilated branch names are written here
14 # noann if true, omit annihilated branches from output (and brfile)
15 # exclbr whitespace separated list of names to exclude (only from stdout)
16 # inclbr whitespace separated list of names to include (only for stdout)
18 # note that the "noann" variable affects both stdout and, if given, brfile
19 # but not anfile.
21 # if inclbr is non-empty a branch name must be listed to appear on stdout
22 # (but brfile, if given, is not affected)
24 # if a branch name appears in exclbr it is omitted from stdout trumping inclbr
25 # (but brfile, if given, is not affected)
27 # input must be output from awk_ref_prepare (depsblob and msgblob may have any
28 # setting) after feeding through the correct git --batch-check command
30 # output is one TopGit branch name per line omitting annihilated ones if noann
32 # note that brfile and anfile are both fully written and closed before the
33 # first line of stdout is written and will be truncated to empty even if there
34 # are no lines directed to them
37 BEGIN { exitcode = "" }
38 function exitnow(e) { exitcode=e; exit e }
39 END { if (exitcode != "") exit exitcode }
41 BEGIN {
42 delay = 0
43 if (anfile != "") {
44 printf "" >anfile
45 delay=1
47 if (brfile != "") {
48 printf "" >brfile
49 delay=1
51 inconly = 0
52 cnt = split(inclbr, scratch, " ")
53 if (cnt) {
54 inconly = 1
55 for (i = 1; i <= cnt; ++i) incnames[scratch[i]] = 1
57 cnt = split(exclbr, scratch, " ")
58 for (i = 1; i <= cnt; ++i) excnames[scratch[i]] = 1
59 FS = " "
60 cnt = 0
63 NF == 4 && $4 == ":" && $3 != "" && $2 != "missing" && $1 != "" {
64 if ((getline bc + getline hc + \
65 getline bct + getline hct) != 4) exitnow(2)
66 split(bc, abc)
67 split(hc, ahc)
68 split(bct, abct)
69 split(hct, ahct)
70 if (abc[2] != "commit" || ahc[2] != "commit" ||
71 abct[2] != "tree" || ahct[2] != "tree") next
72 if (abct[1] == ahct[1]) {
73 if (anfile) print $3 >anfile
74 if (noann) next
76 if (brfile) print $3 >brfile
77 if ((!inconly || incnames[$3]) && !excnames[$3]) {
78 if (delay)
79 items[++cnt] = $3
80 else
81 print $3
85 END {
86 if (anfile) close(anfile)
87 if (brfile) close(brfile)
88 for (i = 1; i <= cnt; ++i) print items[i]