Repository navigation
Expand file tree
/
Copy pathassemble.py
More file actions
398 lines (326 loc) · 16.9 KB
/
Copy pathassemble.py
File metadata and controls
398 lines (326 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
"""Build `dist-next` by merging frozen feature branches onto a fresh base.
Merge, never rebase. Rebasing replays a feature's commits onto the new
upstream, which shifts the content that rerere keys on and defeats the cache.
Merging leaves each feature byte-stable, so a feature-vs-feature conflict
presents the same pre-image every sync and replays automatically. Only
feature-vs-upstream conflicts can be novel.
"""
from __future__ import annotations
import json
import re
from collections.abc import Iterable
from dataclasses import dataclass
from pathlib import Path
from minibwa_dist.gitutil import git, git_ok, stdout
from minibwa_dist.manifest import Feature, Manifest
# Every sync commits its own assembly JSON here on the assembled branch; it is
# what the next run reads to answer "what did the last build contain".
MANIFEST_ARTEFACT = "dist-manifest.json"
class AssemblyError(RuntimeError):
"""Anything that stops an assembly. Callers report these; they never traceback."""
class RequiredFeatureConflict(AssemblyError):
"""A `required = true` feature would not merge. The build must not proceed."""
class TrainingStop(RequiredFeatureConflict):
"""`--stop-at` reached its feature and left the conflict for a human.
A subclass, not a sibling: callers that only know `RequiredFeatureConflict`
still catch it, and the two outcomes -- a successful training stop and a
failed build -- are now told apart by type instead of by matching the
exception's message text, which any edit to either message would break.
"""
class MissingBranch(AssemblyError):
"""A manifest branch does not exist. Branch custody violation — never a silent drop."""
class MergeFailed(AssemblyError):
"""`git merge` refused to start. Not a conflict, and not something to commit."""
class UnknownStopTarget(AssemblyError):
"""`--stop-at` named something the manifest does not contain."""
@dataclass(frozen=True)
class DroppedFeature:
"""An optional feature excluded from this build because it would not merge."""
name: str
conflicting_files: tuple[str, ...]
@dataclass(frozen=True)
class AssemblyResult:
"""Outcome of one assembly run."""
base: str
head: str
merged: tuple[str, ...]
dropped: tuple[DroppedFeature, ...]
def _conflicting_files(repo: Path) -> tuple[str, ...]:
out = stdout(repo, "diff", "--name-only", "--diff-filter=U")
return tuple(line for line in out.splitlines() if line)
def _current_ref(repo: Path) -> str:
"""The ref to return the worktree to when the assembly is over.
`rev-parse --abbrev-ref HEAD` answers the literal string `HEAD` on a
detached checkout, so restoring it is `git checkout HEAD` -- a no-op that
silently leaves the caller parked on the assembled branch. Ask for the
symbolic ref, which fails cleanly when detached, and fall back to the commit.
"""
branch = git(repo, "symbolic-ref", "-q", "--short", "HEAD", check=False).stdout.strip()
return branch or stdout(repo, "rev-parse", "HEAD")
def _try_merge(repo: Path, feature: Feature, ref: str) -> tuple[bool, tuple[str, ...]]:
"""Merge `ref` for `feature`. Returns (merged, conflicting_files).
`commit.gpgsign=false` is forced: `git merge` honours the setting, and with
1Password signing that means 14 biometric prompts per run, or a hard failure
when the key is unavailable. Bot-built integration merges on `dist` are
unsigned by design; the signing rule governs human commits.
rerere.autoUpdate stages any resolution it recognises, so a merge that exits
nonzero but leaves no unmerged paths *may* be one rerere already handled --
but the same signature also covers a merge git refused to begin (a dirty
worktree, or an untracked file the merge would overwrite). `MERGE_HEAD`
tells the two apart positively: it exists only once the merge actually
started. Inferring the rerere hit from the absence of conflicts instead used
to run `git commit --no-edit` on a repository that was never merging, which
failed with git's own message swallowed.
All three rerere/conflict-style knobs are pinned here, on the merge
invocation itself, rather than left to ambient config, so this function's
behaviour does not depend on whoever's environment happens to run it.
`rerere.enabled` defaults to false until `.git/rr-cache` exists, which is
exactly the state a fresh clone is in, so without the pin the bootstrap
training records nothing. Without `rerere.autoUpdate`, rerere resolves the
content but leaves the path unmerged, so `_conflicting_files` reports a
conflict and every rerere *hit* reads as a drop instead of a merge.
`merge.conflictStyle` is a weaker case, and the comment here used to
overstate it: measured on git 2.50.1, rerere normalises its pre-image, so a
resolution trained under an ambient `zdiff3` replays fine against a plain
`merge`-style merge and vice versa. What the pin does buy is what the human
sees during `--stop-at` training: the conflict left in the worktree carries
the same markers CI would have produced, rather than whichever style that
maintainer happens to prefer.
"""
merge = git(
repo,
"-c",
"rerere.enabled=true",
"-c",
"rerere.autoUpdate=true",
"-c",
"merge.conflictStyle=merge",
"-c",
"commit.gpgsign=false",
"merge",
"--no-ff",
"--no-edit",
"-m",
f"merge {feature.name} ({feature.branch})",
ref,
check=False,
)
if merge.returncode == 0:
return True, ()
conflicts = _conflicting_files(repo)
if not conflicts:
if not git_ok(repo, "rev-parse", "-q", "--verify", "MERGE_HEAD"):
raise MergeFailed(
f"merging '{ref}' for feature '{feature.name}' never started: "
f"{merge.stderr.strip() or merge.stdout.strip()}"
)
git(repo, "-c", "commit.gpgsign=false", "
8000
;commit", "--no-edit")
return True, ()
return False, conflicts
def assemble(
repo: Path,
manifest: Manifest,
base_ref: str,
out_branch: str,
remote: str,
stop_at: str | None = None,
) -> AssemblyResult:
"""Assemble `out_branch` from `base_ref` plus every manifest feature.
Features resolve as `{remote}/{branch}` -- `nh13/...` locally, `origin/...`
in CI. Never bare: local branches have drifted from the fork, and assembling
from them would train the rerere cache on content CI never sees.
Merges run in manifest order: disjoint features first, the hub last. An
optional feature that will not merge is dropped and recorded; a required one
raises. The starting branch is always restored -- on success, on abort, and
on any unexpected git failure -- with the single deliberate exception of
`stop_at` below.
`stop_at` is training mode: stop at that feature and LEAVE its conflicted
merge in the worktree, so a human resolves it at the exact intermediate state
a later run will reproduce. Resolving it anywhere else records a pre-image
that will not match. It is validated against the manifest first: a typo that
matches nothing would otherwise run an ordinary full build and report
success, silently doing the opposite of what was asked.
"""
names = [f.name for f in manifest.features]
if stop_at is not None and stop_at not in names:
raise UnknownStopTarget(
f"--stop-at '{stop_at}' names no feature in the manifest; "
f"valid names: {', '.join(names) or '(none)'}"
)
# Report every missing branch at once. Custody is a property of the whole
# manifest, and fixing them one failed run at a time is a slow way to learn
# that a prune took three branches, not one.
missing = [
f"{remote}/{f.branch} (feature '{f.name}')"
for f in manifest.features
if not git_ok(repo, "rev-parse", "--verify", f"{remote}/{f.branch}^{{commit}}")
]
if missing:
raise MissingBranch(
f"missing from the fork: {', '.join(missing)} — "
"mirror every manifest branch into the fork before assembling"
)
# Resolve the base to a commit before anything moves: the release job proves
# a tag matches the tree it is about to name, and only the exact commit the
# features were merged onto can settle that. `head` cannot -- it is the tip
# after every merge, which is never an upstream commit.
base = stdout(repo, "rev-parse", f"{base_ref}^{{commit}}")
start_ref = _current_ref(repo)
git(repo, "checkout", "-q", "-B", out_branch, base_ref)
merged: list[str] = []
dropped: list[DroppedFeature] = []
restore_start_ref = True
try:
for feature in manifest.features:
ok, conflicts = _try_merge(repo, feature, f"{remote}/{feature.branch}")
if ok:
merged.append(feature.name)
continue
if stop_at == feature.name:
# Leave the conflict in the worktree on purpose. This is the one
# path that must NOT restore the starting branch -- the human
# needs this exact state.
restore_start_ref = False
raise TrainingStop(
f"stopped at '{feature.name}' for rerere training; "
f"resolve {', '.join(conflicts)} here, commit, then reset --hard HEAD~1"
)
git(repo, "merge", "--abort", check=False)
if feature.required:
raise RequiredFeatureConflict(
f"required feature '{feature.name}' conflicts in: {', '.join(conflicts)}"
)
dropped.append(DroppedFeature(name=feature.name, conflicting_files=conflicts))
# Read HEAD inside the `try`, before the `finally` can move it: a restore
# that ran first would report the caller's own commit as this build's.
head = stdout(repo, "rev-parse", "HEAD")
finally:
# A `finally`, not an `except RequiredFeatureConflict`: a git call that
# fails for any other reason strands the worktree on `out_branch`, where
# `minibwa_dist/` does not exist, and the caller is left holding a repo it
# never asked to be moved.
if restore_start_ref:
git(repo, "checkout", "-q", start_ref)
return AssemblyResult(base=base, head=head, merged=tuple(merged), dropped=tuple(dropped))
def previously_merged(repo: Path, ref: str) -> frozenset[str] | None:
"""Feature names the build at `ref` recorded, or None when there is no baseline.
Reads the `dist-manifest.json` that every sync commits onto the assembled
branch, so "what did the last build contain" is answered by the last build
itself rather than by re-deriving it.
Returns None -- deliberately not an empty set -- for a missing ref, a ref
with no manifest, and a manifest that will not parse. All three mean
"nothing to compare against". An empty set would mean "the last build
contained no features", which would report every feature as a regression:
a first run, or one corrupt artefact, would wedge the pipeline behind a
file the next assembly overwrites anyway.
"""
if not git_ok(repo, "rev-parse", "--verify", f"{ref}^{{commit}}"):
return None
show = git(repo, "show", f"{ref}:{MANIFEST_ARTEFACT}", check=False)
if show.returncode != 0:
return None
try:
recorded = json.loads(show.stdout)["merged"]
except (ValueError, KeyError, TypeError):
return None
if not isinstance(recorded, list) or not all(isinstance(n, str) for n in recorded):
return None
return frozenset(recorded)
def regressions(
previous: frozenset[str] | None, manifest: Manifest, merged: Iterable[str]
) -> tuple[str, ...]:
"""Manifest features that merged in the previous build and do not merge now.
This is the difference between "has never merged" and "merged yesterday,
dropped today". The engine treats both as an ordinary optional-feature drop
and exits zero, which is right for the first but hides the second: with the
feature excluded the build can match `dist`, the caller short-circuits on
"nothing to ship", and the open sync PR goes on advertising a feature the
build no longer contains.
Intersected with the *current* manifest, so retiring a feature -- deleting
its `[[feature]]` block once upstream takes it -- is not a regression. It
stops appearing in `merged` because it is gone, not because it broke.
Ordered by the manifest, not by set iteration, so the message a run prints
is stable between runs.
"""
if previous is None:
return ()
now = set(merged)
return tuple(f.name for f in manifest.features if f.name in previous and f.name not in now)
_MB_VERSION = re.compile(r'^#define MB_VERSION "([^"]+)"', re.M)
def set_version(repo: Path, version: str) -> str:
"""Rewrite `minibwa.h`'s MB_VERSION to `version`. Returns the previous value.
The single place that knows the header's shape. The release workflow used to
carry a second copy as a `sed` expression with looser matching and no
existence check, so a change to how the define is written had to be found in
two places and only one of them had tests -- and an operator-supplied version
containing `&` or `/` was spliced into `sed`'s replacement as syntax. Python's
`re.sub` takes the replacement as a callable here for the same reason: nothing
in `version` is ever interpreted.
"""
header = repo / "minibwa.h"
text = header.read_text()
current = _MB_VERSION.search(text)
if current is None:
raise RuntimeError("MB_VERSION not found in minibwa.h")
header.write_text(_MB_VERSION.sub(lambda _: f'#define MB_VERSION "{version}"', text, count=1))
return current.group(1)
_UPSTREAM_VERSION = re.compile(r"^(\d+\.\d+)-r\d+$")
def upstream_version(repo: Path, rev: str, nearest_tag: str | None = None) -> str:
"""Upstream's own version string at `rev`, e.g. `0.7-r427`, for an untagged release.
Read from MB_VERSION in `rev`'s minibwa.h rather than from a commit subject:
the header is the one place lh3 bumps mechanically, while `r427:` in a
subject line is free text. Commits between releases carry a `-dirty` suffix
(`0.7-r427-dirty`), which is dropped -- it describes upstream's working tree
when the header was written, not anything about this build.
`nearest_tag` guards the ordering the release scheme depends on: an untagged
build named `0.7-rNNN` must sort after `v0.7` and before `v0.8`, which holds
only while the header's `maj.min` still names the most recent tag. If lh3
bumped it ahead of tagging, `v0.8-rNNN-nh13.1` would sort above the real
`v0.8-nh13.1` cut later, so refuse instead.
"""
current = _MB_VERSION.search(stdout(repo, "show", f"{rev}:minibwa.h"))
if current is None:
raise RuntimeError(f"MB_VERSION not found in minibwa.h at {rev}")
version = current.group(1).removesuffix("-dirty")
shape = _UPSTREAM_VERSION.match(version)
if shape is None:
raise RuntimeError(f"MB_VERSION at {rev} is {version!r}, not <maj>.<min>-r<N>")
if nearest_tag is not None and shape.group(1) != nearest_tag.removeprefix("v"):
raise RuntimeError(
f"MB_VERSION at {rev} is {version!r} but the nearest upstream tag is "
f"{nearest_tag}; an untagged release named after it would mis-sort"
)
return version
def stamp_dev_version(repo: Path, out_branch: str) -> str:
"""Rewrite MB_VERSION on `out_branch` so every dist build identifies itself.
Without this, `dist` -- the default branch, what every clone gets -- builds as
stock upstream and its SAM @PG VN: tag lies about provenance. The release
workflow re-stamps the same line with the final version.
Deliberately leaves HEAD on `out_branch` on return -- a caller that needs
the original branch back is responsible for restoring it itself.
"""
# Switch FIRST: every read below must come from out_branch. Reading
# minibwa.h on the calling branch and writing it back after checkout would
# clobber whatever the assembled branch holds -- and four manifest features
# (submem-ablation, soft-clip-penalty, meth-cleanups, alt-liftgroup) edit
# this header. The byte-identity gate would not catch it: reverting toward
# stock makes the SAM more identical, not less. It also made the embedded
# sha identify the tooling checkout rather than the build being stamped.
git(repo, "checkout", "-q", out_branch)
current = _MB_VERSION.search((repo / "minibwa.h").read_text())
if current is None:
raise RuntimeError("MB_VERSION not found in minibwa.h")
upstream = current.group(1).split("-")[0]
sha = stdout(repo, "rev-parse", "--short", "HEAD")
version = f"{upstream}-nh13.dev+{sha}"
set_version(repo, version)
git(repo, "add", "--", "minibwa.h")
git(
repo,
"-c",
"commit.gpgsign=false",
"commit",
"-m",
f"build: identify this build as {version}",
)
return version