| 1 | (* Releases: the release commits the home page (Build) and ladle's |
| 2 | download pages (Source) share. Tags supply the released versions; |
| 3 | site/releases records website distributions whose tags have not |
| 4 | been pushed. Both must name commits on main's first-parent line, and |
| 5 | a later tag must agree with the record. No tags or archives are |
| 6 | made here. It needs git, through Site. *) |
| 7 | module Releases; |
| 8 | import Site, Str, Heap, Buf, Fmt; |
| 9 | import Entries := Vec(Release); |
| 10 | import Commits := Map([]const U8, BOOL, Str.hash, Str.eq); |
| 11 | import ByVersion := Sort(Release, older); |
| 12 | |
| 13 | type |
| 14 | (* A release: its version tag, vX.Y.Z, and the full commit hash. *) |
| 15 | Release* = record |
| 16 | tag*: []const U8; |
| 17 | commit*: []const U8; |
| 18 | end; |
| 19 | |
| 20 | (* The state of one run of refs. *) |
| 21 | Pick = record |
| 22 | root: []const U8; (* the repository *) |
| 23 | line: Commits.Map; (* the commits of main's first-parent line *) |
| 24 | selected: Entries.Vec; (* the releases chosen so far *) |
| 25 | tagged: Entries.Vec; (* every tag of the form vX.Y.Z, with its commit *) |
| 26 | why: Buf.Buf; (* the message of the first mistake *) |
| 27 | end; |
| 28 | |
| 29 | const |
| 30 | OldestMinor = 7; (* the oldest release is v0.7.1 *) |
| 31 | OldestPatch = 1; |
| 32 | |
| 33 | (* The number the digits of s make, and whether s is only digits. *) |
| 34 | proc number(s: []const U8): (I64, BOOL); |
| 35 | var i: INT; |
| 36 | begin |
| 37 | if LEN(s) = 0 then return 0, false; end; |
| 38 | for i := 0 to LEN(s) - 1 do |
| 39 | if not Str.isDigit(s[i]) then return 0, false; end; |
| 40 | end; |
| 41 | return Str.toInt(s); |
| 42 | end; |
| 43 | |
| 44 | (* The three numbers of a tag vX.Y.Z, and whether it is one. *) |
| 45 | proc version*(tag: []const U8): (I64, I64, I64, BOOL); |
| 46 | var rest, x, y, z: []const U8; a, b, c: I64; found, ok: BOOL; |
| 47 | begin |
| 48 | if not Str.hasPrefix(tag, "v") then return 0, 0, 0, false; end; |
| 49 | x, rest, found := Str.cut(tag[1..LEN(tag)], "."); |
| 50 | if not found then return 0, 0, 0, false; end; |
| 51 | y, z, found := Str.cut(rest, "."); |
| 52 | if not found then return 0, 0, 0, false; end; |
| 53 | a, ok := number(x); |
| 54 | if not ok then return 0, 0, 0, false; end; |
| 55 | b, ok := number(y); |
| 56 | if not ok then return 0, 0, 0, false; end; |
| 57 | c, ok := number(z); |
| 58 | if not ok then return 0, 0, 0, false; end; |
| 59 | return a, b, c, true; |
| 60 | end; |
| 61 | |
| 62 | (* Whether the release a is older than b. *) |
| 63 | proc older(a, b: Release): BOOL; |
| 64 | var a1, a2, a3, b1, b2, b3: I64; |
| 65 | begin |
| 66 | a1, a2, a3, _ := version(a.tag); |
| 67 | b1, b2, b3, _ := version(b.tag); |
| 68 | if a1 # b1 then return a1 < b1; end; |
| 69 | if a2 # b2 then return a2 < b2; end; |
| 70 | return a3 < b3; |
| 71 | end; |
| 72 | |
| 73 | (* Main locally, or the detached commit a deployment gives: "refs/heads/main" |
| 74 | or "HEAD", or false if there is neither. A build never silently publishes another local branch. The |
| 75 | publishing service selects main before checking out its commit. *) |
| 76 | proc mainRef*(root: []const U8): ([]const U8, BOOL); |
| 77 | begin |
| 78 | if Site.runQuiet(["git", "-C", root, "show-ref", "--verify", "--quiet", "refs/heads/main"]) = 0 then |
| 79 | return "refs/heads/main", true; |
| 80 | end; |
| 81 | if Site.runQuiet(["git", "-C", root, "symbolic-ref", "--quiet", "HEAD"]) # 0 then |
| 82 | return "HEAD", true; |
| 83 | end; |
| 84 | return "", false; |
| 85 | end; |
| 86 | |
| 87 | (* The index of the release called tag in v, or -1. *) |
| 88 | proc find(const v: Entries.Vec; tag: []const U8): INT; |
| 89 | var i: INT; |
| 90 | begin |
| 91 | for i := 0 to v.len - 1 do |
| 92 | if Entries.all(v)[i].tag = tag then return i; end; |
| 93 | end; |
| 94 | return -1; |
| 95 | end; |
| 96 | |
| 97 | (* Says that the release tag is wrong, as why; false. *) |
| 98 | proc mistake(var p: Pick; tag: []const U8; what: []const U8): BOOL; |
| 99 | begin |
| 100 | Fmt.write(p.why, "release {}: {}", [Fmt.str(tag), Fmt.str(what)]); |
| 101 | return false; |
| 102 | end; |
| 103 | |
| 104 | (* Considers the release tag at rev: one before v0.7.1 is ignored; one |
| 105 | not on main's line, or at odds with a tag or a record, is a mistake, |
| 106 | false. *) |
| 107 | proc add(var p: Pick; tag, rev: []const U8): BOOL; |
| 108 | var commit: []const U8; a, b, c: I64; ok: BOOL; i: INT; text: Buf.Buf; |
| 109 | begin |
| 110 | a, b, c, ok := version(tag); |
| 111 | if not ok then |
| 112 | Fmt.write(p.why, "release version must be vX.Y.Z: {}", [Fmt.str(tag)]); |
| 113 | return false; |
| 114 | end; |
| 115 | if (a = 0) and ((b < OldestMinor) or ((b = OldestMinor) and (c < OldestPatch))) then return true; end; |
| 116 | commit := Site.git(p.root, ["rev-parse", "--verify", "--end-of-options", Site.cat([rev, "^{commit}"])]); |
| 117 | _, ok := Commits.get(p.line, commit); |
| 118 | if not ok then |
| 119 | Buf.init(text, Heap.heap); |
| 120 | Fmt.write(text, "{} is not on main's first-parent line", [Fmt.str(commit)]); |
| 121 | return mistake(p, tag, Buf.bytes(text)); |
| 122 | end; |
| 123 | i := find(p.tagged, tag); |
| 124 | if (i >= 0) and (Entries.all(p.tagged)[i].commit # commit) then |
| 125 | return mistake(p, tag, "tag and recorded commits disagree"); |
| 126 | end; |
| 127 | i := find(p.selected, tag); |
| 128 | if i < 0 then |
| 129 | Entries.push(p.selected, {tag: tag, commit: commit}); |
| 130 | elsif Entries.all(p.selected)[i].commit # commit then |
| 131 | return mistake(p, tag, "tag and recorded commits disagree"); |
| 132 | end; |
| 133 | return true; |
| 134 | end; |
| 135 | |
| 136 | (* Adds the tags of the repository, those on main's line as releases. *) |
| 137 | proc addTags(var p: Pick): BOOL; |
| 138 | var all: [][]const U8; tag, commit: []const U8; i: INT; ok: BOOL; |
| 139 | begin |
| 140 | all := Site.lines(Site.git(p.root, ["tag", "-l", "v*"])); |
| 141 | for i := 0 to LEN(all) - 1 do |
| 142 | tag := all[i]; |
| 143 | _, _, _, ok := version(tag); |
| 144 | if ok then |
| 145 | (* Tags on another development line are not this site's releases. *) |
| 146 | commit := Site.git(p.root, ["rev-parse", "--verify", Site.cat([tag, "^{commit}"])]); |
| 147 | Entries.push(p.tagged, {tag: tag, commit: commit}); |
| 148 | _, ok := Commits.get(p.line, commit); |
| 149 | if ok and not add(p, tag, commit) then return false; end; |
| 150 | end; |
| 151 | end; |
| 152 | return true; |
| 153 | end; |
| 154 | |
| 155 | (* Adds the records of site/releases, one "vX.Y.Z FULL_COMMIT" a line, |
| 156 | # starting a comment. *) |
| 157 | proc addManifest(var p: Pick): BOOL; |
| 158 | var |
| 159 | path, line: []const U8; all, words: [][]const U8; i, j: INT; hash: BOOL; |
| 160 | begin |
| 161 | path := Site.cat([p.root, "/site/releases"]); |
| 162 | all := Site.lines(Site.read(path)); |
| 163 | for i := 0 to LEN(all) - 1 do |
| 164 | line, _, _ := Str.cut(all[i], "#"); |
| 165 | words := Site.words(line); |
| 166 | if LEN(words) > 0 then |
| 167 | hash := (LEN(words) = 2) and (LEN(words[1]) = 40); |
| 168 | if hash then |
| 169 | for j := 0 to 39 do |
| 170 | hash := hash and (Str.isDigit(words[1][j]) or ((words[1][j] >= 'a') and (words[1][j] <= 'f'))); |
| 171 | end; |
| 172 | end; |
| 173 | if not hash then |
| 174 | Fmt.write(p.why, "{}:{}: expected vX.Y.Z and full commit hash", [Fmt.str(path), Fmt.int(i + 1)]); |
| 175 | return false; |
| 176 | end; |
| 177 | if not add(p, words[0], words[1]) then return false; end; |
| 178 | end; |
| 179 | end; |
| 180 | return true; |
| 181 | end; |
| 182 | |
| 183 | (* The (version tag, full commit) pairs, oldest first, of the tags on |
| 184 | main's line and the records of site/releases, and of extra, the |
| 185 | optional vX.Y.Z=COMMIT arguments of make site. Conflicting records |
| 186 | are mistakes, so a build cannot quietly move an existing version to |
| 187 | another commit: then the list is empty and the message, otherwise |
| 188 | empty, says what is wrong. A shallow |
| 189 | checkout, as a deployment service makes, is completed from its |
| 190 | origin first, since the source browser needs the history and the |
| 191 | tags. *) |
| 192 | proc refs*(root: []const U8; extra: []const Release): ([]Release, []const U8); |
| 193 | var p: Pick; all: [][]const U8; ref: []const U8; ok: BOOL; i: INT; |
| 194 | begin |
| 195 | if Site.git(root, ["rev-parse", "--is-shallow-repository"]) = "true" then |
| 196 | Fmt.print("site: fetching full history and tags from origin\n", []); |
| 197 | Site.must(["git", "-C", root, "fetch", "--unshallow", "--tags", "origin"]); |
| 198 | end; |
| 199 | ref, ok := mainRef(root); |
| 200 | if not ok then return [], "site build needs main or a detached deployment checkout"; end; |
| 201 | p.root := root; |
| 202 | Buf.init(p.why, Heap.heap); |
| 203 | Commits.init(p.line, Heap.heap); |
| 204 | Entries.init(p.selected, Heap.heap); |
| 205 | Entries.init(p.tagged, Heap.heap); |
| 206 | all := Site.lines(Site.git(root, ["rev-list", "--first-parent", ref])); |
| 207 | for i := 0 to LEN(all) - 1 do _ := Commits.put(p.line, all[i], true); end; |
| 208 | ok := addTags(p) and addManifest(p); |
| 209 | for i := 0 to LEN(extra) - 1 do |
| 210 | ok := ok and add(p, extra[i].tag, extra[i].commit); |
| 211 | end; |
| 212 | if not ok then return [], Buf.bytes(p.why); end; |
| 213 | ByVersion.sort(Entries.all(p.selected)); |
| 214 | return Entries.all(p.selected), ""; |
| 215 | end; |
| 216 | |
| 217 | end Releases. |