t4000: add bare branch sanity checks
[topgit/pro.git] / awk / topgit_deps_prepare.awk
blob4ca5d2936865e9c528579eca477217d8cda3c7af
1 #!/usr/bin/awk -f
3 # topgit_deps_prepare - 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_deps_prepare
10 # variable arguments (-v):
12 # brfile if non-empty, the named file gets a list of TopGit branches
13 # anfile if non-empty, annihilated branch names are written here
14 # noann if true, omit annihilated branches from brfile
15 # missing if non-empty output its value for the .topdeps blob instead
16 # of skipping, but still skip annihilated if noann is true
17 # misscmd if missing is used and not seen in a "check" line run this once
19 # note that the "noann" variable only affects brfile, if true unless missing
20 # is non-empty (in which case it suppresses the annihilated missing output), as
21 # annihilated branches normally do not contribute to the output of this script
23 # input must be result of awk_ref_prepare after feeding through the correct
24 # git --batch-check command and must be generated with the "depsblob" variable
25 # set to a TRUE value when awk_ref_prepare was run (the "msgblob" setting can
26 # be any value as the extra .topmsg blob line, if present, will always be
27 # silently ignored)
29 # if missing is non-empty there are two different possible choices:
31 # 1. the hash of the empty blob, this is the recommended value and will
32 # cause all annihilated (unless noann is true) and non-annihilaed branches
33 # without a .topdeps file to produce an output line which can sometimes be
34 # useful
36 # 2. an invalid ref value, do NOT use "missing" as there could be such a ref
37 # name in the repository ("?" or "?missing" are good choices) and it must
38 # not contain any whitespace either; in this case this will trigger the
39 # subsequent git cat-file --batch to generate a "xxx missing" line which
40 # will also remove the item and ultimately have the same effect as leaving
41 # missing unset in the first place
43 # 3. it says "two" above, so don't do this, but if the blob hash of
44 # a different .topdeps file is given its contents will be used as though
45 # it had been the branch's .topdeps file in the first place (only for
46 # annihildated and branches without one though)
48 # If missing is non-empty AND it gets used AND misscmd is non-empty AND no
49 # "blob check ?" line was seen for missing then misscmd will be run the FIRST
50 # time missing is about to be output (it always runs BEFORE the line is output).
52 # output is 1 line per non-annihilated TopGit branch with a .topdeps file where
53 # each output line has this format:
55 # <blob_hash_of_.topdeps_file> <TopGit_branch_name>
57 # which should then be feed to:
59 # git cat-file --batch='%(objecttype) %(objectsize) %(rest)' | tr '\0' '\27'
61 # note that brfile and anfile are both fully written and closed before the
62 # first line of stdout is written and will be truncated to empty even if there
63 # are no lines directed to them
66 BEGIN { exitcode = "" }
67 function exitnow(e) { exitcode=e; exit e }
68 END { if (exitcode != "") exit exitcode }
70 BEGIN {
71 cnt = 0
72 delay = 0
73 if (anfile != "") {
74 printf "" >anfile
75 delay=1
77 if (brfile != "") {
78 printf "" >brfile
79 delay=1
81 FS = " "
84 NF == 4 && $4 == "?" && $3 = "check" && $2 = "blob" && $1 != "" {
85 check[$1] = "blob"
86 next
89 function domissing() {
90 if (misscmd == "" || missing in check) return
91 system(misscmd)
92 check[missing] = ""
95 NF == 4 && $4 == ":" && $3 != "" && $2 != "missing" && $1 != "" {
96 if ((getline bc + getline hc + \
97 getline bct + getline hct + getline hcd) != 5) exitnow(2)
98 split(bc, abc)
99 split(hc, ahc)
100 split(bct, abct)
101 split(hct, ahct)
102 split(hcd, ahcd)
103 if (abc[2] != "commit" || ahc[2] != "commit" ||
104 abct[2] != "tree" || ahct[2] != "tree") next
105 if (abct[1] == ahct[1]) {
106 if (anfile) print $3 >anfile
107 if (noann || missing == "") {
108 if (!noann && brfile) print $3 >brfile
109 next
110 } else {
111 ahcd[1] = missing
112 ahcd[2] = "blob"
113 domissing()
116 if (brfile) print $3 >brfile
117 if (missing != "" && ahcd[2] != "blob") {
118 ahcd[1] = missing "^{}"
119 ahcd[2] = "blob"
120 domissing()
122 if (ahcd[2] == "blob") {
123 if (delay)
124 items[++cnt] = ahcd[1] " " $3
125 else
126 print ahcd[1] " " $3
130 END {
131 if (anfile) close(anfile)
132 if (brfile) close(brfile)
133 for (i = 1; i <= cnt; ++i) print items[i]