@leitwerk-dev/coding 0.4.0 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/README.md +67 -11
  2. package/dist/{chunk-Q24RPXOA.js → chunk-3WJRJ3Q7.js} +68 -7
  3. package/dist/chunk-3WJRJ3Q7.js.map +1 -0
  4. package/dist/chunk-B3PE6DPF.js +820 -0
  5. package/dist/chunk-B3PE6DPF.js.map +1 -0
  6. package/dist/chunk-E3DB4TIZ.js +49 -0
  7. package/dist/chunk-E3DB4TIZ.js.map +1 -0
  8. package/dist/{chunk-BI45R2FH.js → chunk-QD6FI4HR.js} +1 -1
  9. package/dist/chunk-QD6FI4HR.js.map +1 -0
  10. package/dist/finalization-git-internal.d.ts +6 -0
  11. package/dist/finalization-git-internal.d.ts.map +1 -1
  12. package/dist/finalization-git.js +2 -1
  13. package/dist/index.js +3 -2
  14. package/dist/index.js.map +1 -1
  15. package/dist/pull-request-publication-internal.d.ts +8 -0
  16. package/dist/pull-request-publication-internal.d.ts.map +1 -1
  17. package/dist/repository-change-process.d.ts.map +1 -1
  18. package/dist/repository-change-publication.d.ts +16 -3
  19. package/dist/repository-change-publication.d.ts.map +1 -1
  20. package/dist/repository-change-publication.js +283 -69
  21. package/dist/repository-change-publication.js.map +1 -1
  22. package/dist/repository-publication-receipt.d.ts +23 -0
  23. package/dist/repository-publication-receipt.d.ts.map +1 -0
  24. package/dist/repository-rebase/git.d.ts +8 -0
  25. package/dist/repository-rebase/git.d.ts.map +1 -1
  26. package/dist/repository-reconciliation.d.ts +47 -0
  27. package/dist/repository-reconciliation.d.ts.map +1 -0
  28. package/dist/repository-record.d.ts +6 -0
  29. package/dist/repository-record.d.ts.map +1 -0
  30. package/dist/turns/implement.d.ts +1 -1
  31. package/dist/turns/implement.d.ts.map +1 -1
  32. package/dist/turns/implement.js +1 -1
  33. package/package.json +5 -5
  34. package/src/finalization-git-internal.ts +75 -6
  35. package/src/pull-request-publication-internal.ts +3 -2
  36. package/src/repository-change-process.ts +19 -2
  37. package/src/repository-change-publication.ts +243 -57
  38. package/src/repository-publication-receipt.ts +71 -0
  39. package/src/repository-rebase/git.ts +108 -29
  40. package/src/repository-reconciliation.ts +600 -0
  41. package/src/repository-record.ts +14 -0
  42. package/src/turns/implement.ts +4 -1
  43. package/dist/chunk-BI45R2FH.js.map +0 -1
  44. package/dist/chunk-OM2AMDTJ.js +0 -347
  45. package/dist/chunk-OM2AMDTJ.js.map +0 -1
  46. package/dist/chunk-Q24RPXOA.js.map +0 -1
@@ -1,13 +1,22 @@
1
1
  import { execFileSync } from "node:child_process";
2
- import { existsSync, readFileSync, renameSync, writeFileSync } from "node:fs";
2
+ import { existsSync } from "node:fs";
3
3
  import { join } from "node:path";
4
4
  import { repositoryGitArgs, repositoryGitSubprocessEnv } from "@leitwerk-dev/process-sdk";
5
5
  import { resolveGitBinary } from "@leitwerk-dev/process-sdk/git-binary";
6
+ import {
7
+ pushWasRejected,
8
+ type RepositoryPublicationReceipt,
9
+ readPublicationReceipt,
10
+ readRepositoryBinding,
11
+ repositoryBindingMatches,
12
+ savePublicationReceipt,
13
+ } from "../repository-publication-receipt.js";
14
+ import { assertReconciliationTargetSafe } from "../repository-reconciliation.js";
15
+ import { repositoryRecord } from "../repository-record.js";
6
16
 
7
17
  import { type ConflictEvidence, conflictKey } from "./index.js";
8
18
  import {
9
19
  type RebaseRecord,
10
- rebasePublicationDecision,
11
20
  requireRebaseHead,
12
21
  requireRebaseOrigin,
13
22
  requireRebaseTarget,
@@ -42,15 +51,7 @@ function area(input: RebaseInput): string {
42
51
  function active(gitDir: string): boolean {
43
52
  return ["rebase-merge", "rebase-apply"].some((name) => existsSync(join(gitDir, name)));
44
53
  }
45
- function save(gitDir: string, record: RebaseRecord): void {
46
- const path = join(gitDir, "leitwerk-rebase.json");
47
- writeFileSync(`${path}.tmp`, JSON.stringify(record), { mode: 0o600 });
48
- renameSync(`${path}.tmp`, path);
49
- }
50
- function read(gitDir: string): RebaseRecord | null {
51
- const file = join(gitDir, "leitwerk-rebase.json");
52
- return existsSync(file) ? (JSON.parse(readFileSync(file, "utf8")) as RebaseRecord) : null;
53
- }
54
+ const recordFile = (gitDir: string) => repositoryRecord<RebaseRecord>(gitDir, "rebase");
54
55
  function requireWorktree(input: RebaseInput): void {
55
56
  requireRebaseWorktree(input.workBranch, {
56
57
  branch: git(input, "branch", "--show-current"),
@@ -92,10 +93,15 @@ function verifyNoRepairNeeded(input: RebaseInput, headSha: string, baseSha: stri
92
93
  else git(input, "merge-tree", "--write-tree", headSha, baseSha);
93
94
  }
94
95
 
96
+ /** @internal */
97
+ export function hasPreparedRebase(input: RebaseInput): boolean {
98
+ return recordFile(area(input)).read()?.key === conflictKey(input.conflict);
99
+ }
100
+
95
101
  /** Persist the lease before changing HEAD; a retry resumes Git's own rebase state. @internal */
96
102
  export function prepareRebase(input: RebaseInput): RebaseRecord {
97
103
  const gitDir = area(input);
98
- const old = read(gitDir);
104
+ const old = recordFile(gitDir).read();
99
105
  requireTarget(input, old?.key === conflictKey(input.conflict) ? old : null);
100
106
  if (old?.key === conflictKey(input.conflict)) {
101
107
  verifyCurrentBase(input, old);
@@ -131,7 +137,7 @@ export function prepareRebase(input: RebaseInput): RebaseRecord {
131
137
  baseBranch: input.conflict.baseBranch,
132
138
  };
133
139
  git(input, "update-ref", "refs/leitwerk/rebase-original", record.originalHead);
134
- save(gitDir, record);
140
+ recordFile(gitDir).save(record);
135
141
  return record;
136
142
  }
137
143
  /** @internal */
@@ -146,13 +152,17 @@ export function startRebase(input: RebaseInput): RebaseRecord {
146
152
  try {
147
153
  verifyNoRepairNeeded(input, record.originalHead, record.baseSha);
148
154
  record.status = "clean";
149
- save(gitDir, record);
155
+ recordFile(gitDir).save(record);
150
156
  return record;
151
157
  } catch (error) {
152
158
  if ((error as { status?: number }).status !== 1) throw error;
153
159
  }
160
+ assertReconciliationTargetSafe(
161
+ { projectKey: input.projectKey, repoPath: input.path },
162
+ record.baseSha,
163
+ );
154
164
  record.status = "rebasing";
155
- save(gitDir, record);
165
+ recordFile(gitDir).save(record);
156
166
  try {
157
167
  git(input, "rebase", "--rebase-merges", record.baseSha);
158
168
  } catch (error) {
@@ -172,7 +182,7 @@ export function verifyRebase(input: RebaseInput): {
172
182
  baseSha: string;
173
183
  } {
174
184
  const gitDir = area(input);
175
- const record = read(gitDir);
185
+ const record = recordFile(gitDir).read();
176
186
  if (!record || record.key !== conflictKey(input.conflict))
177
187
  throw new Error("Missing matching rebase metadata");
178
188
  requireTarget(input, record);
@@ -194,26 +204,95 @@ export function verifyRebase(input: RebaseInput): {
194
204
  baseSha: record.baseSha,
195
205
  };
196
206
  }
207
+ /** @internal */
208
+ export class ConcurrentRebaseSourceError extends Error {
209
+ constructor(
210
+ readonly repair: ReturnType<typeof verifyRebase>,
211
+ readonly published = false,
212
+ ) {
213
+ super("Concurrent remote change rejected by rebase lease; local repair was retained");
214
+ }
215
+ }
216
+
197
217
  /** @internal */
198
218
  export function publishRebase(input: RebaseInput): ReturnType<typeof verifyRebase> {
199
219
  const result = verifyRebase(input);
220
+ assertReconciliationTargetSafe(
221
+ { projectKey: input.projectKey, repoPath: input.path },
222
+ result.baseSha,
223
+ );
200
224
  const gitDir = area(input);
201
- const record = read(gitDir);
225
+ const record = recordFile(gitDir).read();
202
226
  if (!record) throw new Error("Missing matching rebase metadata");
203
227
  verifyCurrentBase(input, record);
204
228
  const remote = remoteHead(input);
205
- if (
206
- rebasePublicationDecision(result.originalHead, result.headSha, remote) === "already_published"
207
- )
229
+ const binding = readRepositoryBinding((...args) => git(input, ...args), input.workBranch);
230
+ if (binding.origin !== binding.pushOrigin)
231
+ throw new Error("Fetch and push endpoints must match for publication");
232
+ const previous = readPublicationReceipt(gitDir);
233
+ if (!repositoryBindingMatches(previous, binding))
234
+ throw new Error("Repository publication receipt binding changed");
235
+ const receipt: RepositoryPublicationReceipt = {
236
+ ...binding,
237
+ expectedHead: result.originalHead,
238
+ candidate: result.headSha,
239
+ status: "attempting",
240
+ };
241
+ if (remote === result.headSha) {
242
+ savePublicationReceipt(gitDir, { ...receipt, status: "accepted" });
208
243
  return result;
209
- git(
210
- input,
211
- "push",
212
- `--force-with-lease=refs/heads/${input.workBranch}:${result.originalHead}`,
213
- "origin",
214
- `${result.headSha}:refs/heads/${input.workBranch}`,
215
- );
216
- if (remoteHead(input) !== result.headSha)
217
- throw new Error("Rebase publication could not be verified");
244
+ }
245
+ if (previous?.candidate === result.headSha && previous.status === "accepted")
246
+ throw new ConcurrentRebaseSourceError(result, true);
247
+ if (previous?.status === "attempting") {
248
+ git(
249
+ input,
250
+ "fetch",
251
+ "--no-tags",
252
+ "origin",
253
+ `+refs/heads/${input.workBranch}:refs/leitwerk/rebase-publication-proof`,
254
+ );
255
+ try {
256
+ git(
257
+ input,
258
+ "merge-base",
259
+ "--is-ancestor",
260
+ previous.candidate,
261
+ "refs/leitwerk/rebase-publication-proof",
262
+ );
263
+ } catch {
264
+ throw new Error(
265
+ "Prior target publication outcome is uncertain; local repair was retained for operator reconciliation",
266
+ );
267
+ }
268
+ savePublicationReceipt(gitDir, { ...previous, status: "accepted" });
269
+ throw new ConcurrentRebaseSourceError(result, previous.candidate === result.headSha);
270
+ }
271
+ if (remote !== result.originalHead) throw new ConcurrentRebaseSourceError(result);
272
+ savePublicationReceipt(gitDir, receipt);
273
+ try {
274
+ git(
275
+ input,
276
+ "push",
277
+ "--porcelain",
278
+ "--no-follow-tags",
279
+ "--recurse-submodules=no",
280
+ `--force-with-lease=refs/heads/${input.workBranch}:${result.originalHead}`,
281
+ "origin",
282
+ `${result.headSha}:refs/heads/${input.workBranch}`,
283
+ );
284
+ } catch (error) {
285
+ const rejected = pushWasRejected((error as { stdout?: Buffer }).stdout?.toString() ?? "");
286
+ if (rejected) savePublicationReceipt(gitDir, { ...receipt, status: "rejected" });
287
+ const latest = remoteHead(input);
288
+ if (latest === result.headSha) {
289
+ savePublicationReceipt(gitDir, { ...receipt, status: "accepted" });
290
+ return result;
291
+ }
292
+ if (rejected && latest !== result.originalHead) throw new ConcurrentRebaseSourceError(result);
293
+ throw error;
294
+ }
295
+ savePublicationReceipt(gitDir, { ...receipt, status: "accepted" });
296
+ if (remoteHead(input) !== result.headSha) throw new ConcurrentRebaseSourceError(result, true);
218
297
  return result;
219
298
  }