raz / site / Releases.raz

217 lines · 8.2 KiB · raw · history · last change fe3051dc

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. *)
7module Releases;
8import Site, Str, Heap, Buf, Fmt;
9import Entries := Vec(Release);
10import Commits := Map([]const U8, BOOL, Str.hash, Str.eq);
11import ByVersion := Sort(Release, older);
12
13type
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
29const
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. *)
34proc number(s: []const U8): (I64, BOOL);
35var i: INT;
36begin
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);
42end;
43
44(* The three numbers of a tag vX.Y.Z, and whether it is one. *)
45proc version*(tag: []const U8): (I64, I64, I64, BOOL);
46var rest, x, y, z: []const U8; a, b, c: I64; found, ok: BOOL;
47begin
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;
60end;
61
62(* Whether the release a is older than b. *)
63proc older(a, b: Release): BOOL;
64var a1, a2, a3, b1, b2, b3: I64;
65begin
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;
71end;
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. *)
76proc mainRef*(root: []const U8): ([]const U8, BOOL);
77begin
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;
85end;
86
87(* The index of the release called tag in v, or -1. *)
88proc find(const v: Entries.Vec; tag: []const U8): INT;
89var i: INT;
90begin
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;
95end;
96
97(* Says that the release tag is wrong, as why; false. *)
98proc mistake(var p: Pick; tag: []const U8; what: []const U8): BOOL;
99begin
100 Fmt.write(p.why, "release {}: {}", [Fmt.str(tag), Fmt.str(what)]);
101 return false;
102end;
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. *)
107proc add(var p: Pick; tag, rev: []const U8): BOOL;
108var commit: []const U8; a, b, c: I64; ok: BOOL; i: INT; text: Buf.Buf;
109begin
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;
134end;
135
136(* Adds the tags of the repository, those on main's line as releases. *)
137proc addTags(var p: Pick): BOOL;
138var all: [][]const U8; tag, commit: []const U8; i: INT; ok: BOOL;
139begin
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;
153end;
154
155(* Adds the records of site/releases, one "vX.Y.Z FULL_COMMIT" a line,
156 # starting a comment. *)
157proc addManifest(var p: Pick): BOOL;
158var
159 path, line: []const U8; all, words: [][]const U8; i, j: INT; hash: BOOL;
160begin
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;
181end;
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. *)
192proc refs*(root: []const U8; extra: []const Release): ([]Release, []const U8);
193var p: Pick; all: [][]const U8; ref: []const U8; ok: BOOL; i: INT;
194begin
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), "";
215end;
216
217end Releases.