@leitwerk-dev/coding 0.2.1 → 0.3.0

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 (58) hide show
  1. package/README.md +18 -12
  2. package/dist/actions.d.ts +0 -5
  3. package/dist/actions.d.ts.map +1 -1
  4. package/dist/auto-work-branch-internal.d.ts +2 -18
  5. package/dist/auto-work-branch-internal.d.ts.map +1 -1
  6. package/dist/auto-work-branch.d.ts +1 -2
  7. package/dist/auto-work-branch.d.ts.map +1 -1
  8. package/dist/auto-work-branch.js +17 -94
  9. package/dist/auto-work-branch.js.map +1 -1
  10. package/dist/chunk-GO44KCUS.js +154 -0
  11. package/dist/chunk-GO44KCUS.js.map +1 -0
  12. package/dist/finalization-git-internal.d.ts +6 -45
  13. package/dist/finalization-git-internal.d.ts.map +1 -1
  14. package/dist/finalization-git.js +1 -1
  15. package/dist/index.d.ts +1 -1
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/index.js +48 -91
  18. package/dist/index.js.map +1 -1
  19. package/dist/repository-change-launch-internal.d.ts +3 -84
  20. package/dist/repository-change-launch-internal.d.ts.map +1 -1
  21. package/dist/repository-change-launch.d.ts +2 -2
  22. package/dist/repository-change-launch.d.ts.map +1 -1
  23. package/dist/repository-change-launch.js +2 -159
  24. package/dist/repository-change-launch.js.map +1 -1
  25. package/dist/repository-change-process.d.ts +7 -19
  26. package/dist/repository-change-process.d.ts.map +1 -1
  27. package/dist/repository-change-publication.d.ts +229 -0
  28. package/dist/repository-change-publication.d.ts.map +1 -0
  29. package/dist/repository-change-publication.js +368 -0
  30. package/dist/repository-change-publication.js.map +1 -0
  31. package/dist/repository-change-state-internal.d.ts +2 -11
  32. package/dist/repository-change-state-internal.d.ts.map +1 -1
  33. package/dist/repository-change-state.d.ts +0 -1
  34. package/dist/repository-change-state.d.ts.map +1 -1
  35. package/dist/repository-change-state.js +0 -7
  36. package/package.json +10 -4
  37. package/src/actions.ts +0 -13
  38. package/src/auto-work-branch-internal.ts +22 -130
  39. package/src/auto-work-branch.ts +1 -7
  40. package/src/finalization-git-internal.ts +60 -697
  41. package/src/index.ts +0 -1
  42. package/src/repository-change-launch-internal.ts +4 -268
  43. package/src/repository-change-launch.ts +1 -6
  44. package/src/repository-change-process.ts +8 -126
  45. package/src/repository-change-publication.ts +689 -0
  46. package/src/repository-change-state-internal.ts +4 -34
  47. package/src/repository-change-state.ts +0 -1
  48. package/dist/chunk-HUVXPZSF.js +0 -61
  49. package/dist/chunk-HUVXPZSF.js.map +0 -1
  50. package/dist/chunk-MY6XFGHM.js +0 -614
  51. package/dist/chunk-MY6XFGHM.js.map +0 -1
  52. package/dist/chunk-OERAYSVI.js +0 -27
  53. package/dist/chunk-OERAYSVI.js.map +0 -1
  54. package/dist/turns/resolve-merge-conflict.d.ts +0 -4
  55. package/dist/turns/resolve-merge-conflict.d.ts.map +0 -1
  56. package/dist/turns/resolve-merge-conflict.js +0 -8
  57. package/dist/turns/resolve-merge-conflict.js.map +0 -1
  58. package/src/turns/resolve-merge-conflict.ts +0 -26
@@ -1,614 +0,0 @@
1
- // src/finalization-git-internal.ts
2
- import { execFileSync } from "child_process";
3
- import { existsSync } from "fs";
4
- import { homedir } from "os";
5
- import path from "path";
6
- import { fileURLToPath } from "url";
7
- import { detectRepoLocatorKind, trimToNull } from "@leitwerk-dev/domain";
8
- import {
9
- repositoryGitArgs,
10
- repositoryGitSubprocessEnv
11
- } from "@leitwerk-dev/process-sdk";
12
- import { resolveGitBinary } from "@leitwerk-dev/process-sdk/git-binary";
13
- var DeterministicGitError = class extends Error {
14
- /** @internal */
15
- errorClass = "git_error";
16
- /** @internal */
17
- constructor(message) {
18
- super(message);
19
- this.name = "DeterministicGitError";
20
- }
21
- };
22
- function normalizeLines(value) {
23
- if (!value) {
24
- return [];
25
- }
26
- return value.split(/\r?\n/).map((line) => line.trim()).filter((line) => line !== "");
27
- }
28
- function abbreviateSha(sha) {
29
- return sha.slice(0, 12);
30
- }
31
- function describeMergeMode(mode) {
32
- switch (mode) {
33
- case "noop":
34
- return "Already up to date";
35
- case "fast_forward":
36
- return "Fast-forwarded from origin";
37
- case "merge_commit":
38
- return "Created a merge commit";
39
- }
40
- }
41
- function gitConfigIdentityEnv() {
42
- const env = repositoryGitSubprocessEnv("repo", { GIT_TERMINAL_PROMPT: "0" });
43
- for (const key of [
44
- "GIT_AUTHOR_NAME",
45
- "GIT_AUTHOR_EMAIL",
46
- "GIT_AUTHOR_DATE",
47
- "GIT_COMMITTER_NAME",
48
- "GIT_COMMITTER_EMAIL",
49
- "GIT_COMMITTER_DATE"
50
- ])
51
- delete env[key];
52
- return env;
53
- }
54
- function gitIdentityArgs(identity) {
55
- const name = trimToNull(identity.name);
56
- const email = trimToNull(identity.email);
57
- if (!name || !email || /[\r\n\0]/.test(name) || /[\r\n\0]/.test(email)) {
58
- throw new DeterministicGitError("Git identity requires a valid name and email");
59
- }
60
- return ["-c", `user.name=${name}`, "-c", `user.email=${email}`];
61
- }
62
- function runGit(repoPath, args, identity) {
63
- const trustedArgs = identity ? [...gitIdentityArgs(identity), ...args] : args;
64
- try {
65
- const stdout = execFileSync(resolveGitBinary(), repositoryGitArgs(trustedArgs), {
66
- cwd: repoPath,
67
- encoding: "utf8",
68
- env: gitConfigIdentityEnv(),
69
- stdio: ["ignore", "pipe", "pipe"]
70
- });
71
- return {
72
- ok: true,
73
- stdout,
74
- stderr: ""
75
- };
76
- } catch (error) {
77
- const execError = error;
78
- return {
79
- ok: false,
80
- stdout: execError.stdout?.toString() ?? "",
81
- stderr: execError.stderr?.toString() ?? trimToNull(execError.message) ?? ""
82
- };
83
- }
84
- }
85
- function gitFailureDetail(result) {
86
- return trimToNull(result.stderr) ?? trimToNull(result.stdout) ?? "unknown git error";
87
- }
88
- function checkedGit(repoPath, args, failureMessage, identity) {
89
- const result = runGit(repoPath, args, identity);
90
- if (!result.ok) {
91
- throw new DeterministicGitError(`${failureMessage}: ${gitFailureDetail(result)}`);
92
- }
93
- return result.stdout;
94
- }
95
- function git(repoPath, ...args) {
96
- return checkedGit(repoPath, args, `git ${args.join(" ")} failed in '${repoPath}'`).trim();
97
- }
98
- function gitOrNull(repoPath, ...args) {
99
- const result = runGit(repoPath, args);
100
- if (!result.ok) {
101
- return null;
102
- }
103
- return trimToNull(result.stdout);
104
- }
105
- function repoExists(repoPath) {
106
- return existsSync(repoPath) && existsSync(path.join(repoPath, ".git"));
107
- }
108
- function gitRepositoryExists(repoPath) {
109
- return existsSync(repoPath) && runGit(repoPath, ["rev-parse", "--git-dir"]).ok;
110
- }
111
- function currentBranch(repoPath) {
112
- return git(repoPath, "branch", "--show-current");
113
- }
114
- function currentHeadSha(repoPath) {
115
- return git(repoPath, "rev-parse", "HEAD");
116
- }
117
- function originBaseRef(baseBranch) {
118
- return `origin/${baseBranch}`;
119
- }
120
- function remoteOriginUrl(repoPath) {
121
- const remoteUrl = gitOrNull(repoPath, "remote", "get-url", "origin");
122
- if (!remoteUrl) {
123
- throw new DeterministicGitError(`Repository '${repoPath}' does not have an 'origin' remote`);
124
- }
125
- return remoteUrl;
126
- }
127
- function expandHomePath(value) {
128
- if (value === "~") {
129
- return homedir();
130
- }
131
- if (value.startsWith("~/")) {
132
- return path.join(homedir(), value.slice(2));
133
- }
134
- return value;
135
- }
136
- function resolveLocalRepoPath(repoPath, repoLocator) {
137
- if (detectRepoLocatorKind(repoLocator) !== "local_path") {
138
- return null;
139
- }
140
- if (repoLocator.startsWith("file://")) {
141
- try {
142
- return fileURLToPath(repoLocator);
143
- } catch {
144
- return null;
145
- }
146
- }
147
- return path.resolve(repoPath, expandHomePath(repoLocator));
148
- }
149
- function isBareRepository(repoPath) {
150
- return git(repoPath, "rev-parse", "--is-bare-repository") === "true";
151
- }
152
- function resolveLocalSourceRepo(repoPath) {
153
- const remoteUrl = remoteOriginUrl(repoPath);
154
- const localRepoPath = resolveLocalRepoPath(repoPath, remoteUrl);
155
- if (!localRepoPath || !gitRepositoryExists(localRepoPath)) {
156
- return null;
157
- }
158
- return {
159
- path: localRepoPath,
160
- kind: isBareRepository(localRepoPath) ? "bare" : "non_bare"
161
- };
162
- }
163
- function workingTreeStatus(repoPath) {
164
- const rawStatus = gitOrNull(repoPath, "status", "--porcelain");
165
- return (rawStatus ?? "").split(/\r?\n/).map((line) => line.replace(/\s+$/, "")).filter((line) => line !== "").map((line) => line.slice(2).trim()).filter((line) => line !== "");
166
- }
167
- function conflictedFiles(repoPath) {
168
- return normalizeLines(gitOrNull(repoPath, "diff", "--name-only", "--diff-filter=U"));
169
- }
170
- function mergeHeadPath(repoPath) {
171
- const gitDir = git(repoPath, "rev-parse", "--git-dir");
172
- return path.resolve(repoPath, gitDir, "MERGE_HEAD");
173
- }
174
- function mergeInProgress(repoPath) {
175
- return existsSync(mergeHeadPath(repoPath));
176
- }
177
- function assertExpectedCheckout(repoPath, workBranch) {
178
- const branch = currentBranch(repoPath);
179
- if (branch !== workBranch) {
180
- throw new DeterministicGitError(
181
- `Expected '${repoPath}' to be checked out on '${workBranch}', but found '${branch || "detached HEAD"}'`
182
- );
183
- }
184
- }
185
- function assertFinalizationCheckout(input) {
186
- if (!repoExists(input.repoPath)) {
187
- throw new DeterministicGitError(
188
- `Repository workspace '${input.repoPath}' does not exist or is not a git repository`
189
- );
190
- }
191
- assertExpectedCheckout(input.repoPath, input.workBranch);
192
- if (!trimToNull(input.commitMessage)) {
193
- throw new DeterministicGitError(input.missingCommitMessage);
194
- }
195
- }
196
- function assertGitIdentity(repoPath, identity) {
197
- const author = runGit(repoPath, ["var", "GIT_AUTHOR_IDENT"], identity);
198
- const committer = runGit(repoPath, ["var", "GIT_COMMITTER_IDENT"], identity);
199
- if (!author.ok || !committer.ok) {
200
- throw new DeterministicGitError(
201
- "Git author/committer identity is not configured. Configure user.name and user.email in this repository or the operator's global Git config, then retry finalization."
202
- );
203
- }
204
- }
205
- function commitDirtyWorktree(repoPath, commitMessage, identity) {
206
- assertGitIdentity(repoPath, identity);
207
- checkedGit(repoPath, ["add", "--all"], "Failed to stage the completed change");
208
- checkedGit(
209
- repoPath,
210
- ["-c", "core.hooksPath=/dev/null", "commit", "--no-gpg-sign", "-m", commitMessage],
211
- "Failed to commit the completed change",
212
- identity
213
- );
214
- const remaining = workingTreeStatus(repoPath);
215
- if (remaining.length > 0) {
216
- throw new DeterministicGitError(
217
- `Deterministic commit left uncommitted changes: ${remaining.join(", ")}`
218
- );
219
- }
220
- return currentHeadSha(repoPath);
221
- }
222
- function commitIfDirty(input) {
223
- const status = workingTreeStatus(input.repoPath);
224
- return status.length > 0 ? commitDirtyWorktree(input.repoPath, input.commitMessage, input.gitIdentity) : currentHeadSha(input.repoPath);
225
- }
226
- function assertPostConflictCheckpoint(input, repoPath) {
227
- if (!trimToNull(input.expectedPostConflictHeadSha)) {
228
- return;
229
- }
230
- if (mergeInProgress(repoPath) || conflictedFiles(repoPath).length > 0) {
231
- throw new DeterministicGitError(
232
- "Conflict-resolution helper reported a clean merge, but the repository is still in a merge-conflict state"
233
- );
234
- }
235
- const status = workingTreeStatus(repoPath);
236
- if (status.length > 0) {
237
- throw new DeterministicGitError(
238
- `Conflict-resolution helper reported a clean merge, but the repository still has uncommitted changes: ${status.join(", ")}`
239
- );
240
- }
241
- }
242
- function fetchOriginBase(repoPath, baseBranch) {
243
- const ref = originBaseRef(baseBranch);
244
- checkedGit(repoPath, ["fetch", "origin", baseBranch], `Failed to fetch '${ref}'`);
245
- const baseSha = gitOrNull(repoPath, "rev-parse", ref);
246
- if (!baseSha) {
247
- throw new DeterministicGitError(`Fetched '${ref}' but could not resolve its commit sha`);
248
- }
249
- return baseSha;
250
- }
251
- function isAncestor(repoPath, olderRef, newerRef) {
252
- return runGit(repoPath, ["merge-base", "--is-ancestor", olderRef, newerRef]).ok;
253
- }
254
- function deterministicMerge(input) {
255
- const { repoPath, baseBranch, headBefore, baseSha } = input;
256
- const ref = originBaseRef(baseBranch);
257
- const headIsAncestorOfBase = isAncestor(repoPath, headBefore, ref);
258
- const baseIsAncestorOfHead = isAncestor(repoPath, ref, headBefore);
259
- if (baseIsAncestorOfHead) {
260
- return { ok: true, mergeMode: "noop", headSha: headBefore };
261
- }
262
- if (headIsAncestorOfBase) {
263
- checkedGit(repoPath, ["merge", "--ff-only", ref], `Fast-forward merge from '${ref}' failed`);
264
- return {
265
- ok: true,
266
- mergeMode: "fast_forward",
267
- headSha: currentHeadSha(repoPath)
268
- };
269
- }
270
- assertGitIdentity(repoPath, input.gitIdentity);
271
- const mergeResult = runGit(
272
- repoPath,
273
- ["-c", "core.hooksPath=/dev/null", "merge", "--no-edit", "--no-gpg-sign", ref],
274
- input.gitIdentity
275
- );
276
- if (!mergeResult.ok) {
277
- const conflicts = conflictedFiles(repoPath);
278
- if (mergeInProgress(repoPath) || conflicts.length > 0) {
279
- return {
280
- ok: false,
281
- conflict: true,
282
- headSha: currentHeadSha(repoPath),
283
- conflictedFiles: conflicts
284
- };
285
- }
286
- throw new DeterministicGitError(
287
- `Deterministic merge from '${ref}' failed: ${gitFailureDetail(mergeResult)}`
288
- );
289
- }
290
- const headAfter = currentHeadSha(repoPath);
291
- return {
292
- ok: true,
293
- mergeMode: headAfter === baseSha ? "fast_forward" : "merge_commit",
294
- headSha: headAfter
295
- };
296
- }
297
- function branchRef(branch) {
298
- return `refs/heads/${branch}`;
299
- }
300
- function pushAndVerify(repoPath, branch, expectedHeadSha) {
301
- const pushTarget = `origin/${branch}`;
302
- checkedGit(
303
- repoPath,
304
- ["push", "origin", `HEAD:${branchRef(branch)}`],
305
- `Failed to push HEAD to '${pushTarget}'`
306
- );
307
- const remoteHead = gitOrNull(repoPath, "ls-remote", "--heads", "origin", branchRef(branch))?.split(/\s+/)[0]?.trim();
308
- if (remoteHead !== expectedHeadSha) {
309
- throw new DeterministicGitError(
310
- `Published '${pushTarget}' resolved to '${remoteHead ?? "missing"}', expected '${expectedHeadSha}'`
311
- );
312
- }
313
- return pushTarget;
314
- }
315
- function assertLocalSourceBaseRef(input) {
316
- const ref = branchRef(input.baseBranch);
317
- const actualHeadSha = gitOrNull(input.localSourceRepoPath, "rev-parse", ref);
318
- if (!actualHeadSha) {
319
- throw new DeterministicGitError(
320
- `Local source repo '${input.localSourceRepoPath}' does not have '${ref}' ${input.operation}`
321
- );
322
- }
323
- if (actualHeadSha !== input.expectedHeadSha) {
324
- throw new DeterministicGitError(
325
- `Local source repo '${input.localSourceRepoPath}' has '${ref}' at '${actualHeadSha}', but expected '${input.expectedHeadSha}' ${input.operation}`
326
- );
327
- }
328
- }
329
- function assertLocalBareSourceRepoReady(localSourceRepoPath, baseBranch, expectedBaseSha) {
330
- assertLocalSourceBaseRef({
331
- localSourceRepoPath,
332
- baseBranch,
333
- expectedHeadSha: expectedBaseSha,
334
- operation: "before finalization could publish the workspace branch. Retry finalization."
335
- });
336
- }
337
- function pushLocalSourceBaseBranchToOriginIfPresent(input) {
338
- if (!runGit(input.localSourceRepoPath, ["remote", "get-url", "origin"]).ok) {
339
- return null;
340
- }
341
- assertLocalSourceBaseRef({
342
- localSourceRepoPath: input.localSourceRepoPath,
343
- baseBranch: input.baseBranch,
344
- expectedHeadSha: input.expectedHeadSha,
345
- operation: "before pushing it to its origin remote"
346
- });
347
- const ref = branchRef(input.baseBranch);
348
- const pushTarget = originBaseRef(input.baseBranch);
349
- checkedGit(
350
- input.localSourceRepoPath,
351
- ["push", "origin", `${ref}:${ref}`],
352
- `Failed to push local source repo '${input.localSourceRepoPath}' base branch '${input.baseBranch}' to '${pushTarget}'`
353
- );
354
- return pushTarget;
355
- }
356
- function assertLocalBaseRepoReady(localBaseRepoPath, baseBranch, expectedBaseSha) {
357
- const branch = currentBranch(localBaseRepoPath);
358
- if (branch !== baseBranch) {
359
- throw new DeterministicGitError(
360
- `Expected local source repo '${localBaseRepoPath}' to be checked out on '${baseBranch}', but found '${branch || "detached HEAD"}'`
361
- );
362
- }
363
- if (mergeInProgress(localBaseRepoPath) || conflictedFiles(localBaseRepoPath).length > 0) {
364
- throw new DeterministicGitError(
365
- `Local source repo '${localBaseRepoPath}' must not already be in a merge-conflict state before finalization updates '${baseBranch}'`
366
- );
367
- }
368
- const status = workingTreeStatus(localBaseRepoPath);
369
- if (status.length > 0) {
370
- throw new DeterministicGitError(
371
- `Local source repo '${localBaseRepoPath}' must be clean before finalization updates '${baseBranch}': ${status.join(", ")}`
372
- );
373
- }
374
- const actualBaseSha = currentHeadSha(localBaseRepoPath);
375
- if (actualBaseSha !== expectedBaseSha) {
376
- throw new DeterministicGitError(
377
- `Local source repo '${localBaseRepoPath}' moved on '${baseBranch}' from '${expectedBaseSha}' to '${actualBaseSha}' before finalization could continue. Retry finalization.`
378
- );
379
- }
380
- }
381
- function mergeWorkBranchIntoLocalBaseRepo(input) {
382
- assertLocalBaseRepoReady(input.localBaseRepoPath, input.baseBranch, input.expectedBaseSha);
383
- checkedGit(
384
- input.localBaseRepoPath,
385
- ["merge", "--ff-only", `refs/heads/${input.workBranch}`],
386
- `Failed to fast-forward local source repo '${input.localBaseRepoPath}' branch '${input.baseBranch}' from '${input.workBranch}'`
387
- );
388
- const actualHeadSha = currentHeadSha(input.localBaseRepoPath);
389
- if (actualHeadSha !== input.expectedHeadSha) {
390
- throw new DeterministicGitError(
391
- `Local source repo '${input.localBaseRepoPath}' finished finalization at '${actualHeadSha}', but expected '${input.expectedHeadSha}' after merging '${input.workBranch}' into '${input.baseBranch}'`
392
- );
393
- }
394
- }
395
- function updateLocalBaseRef(repoPath, baseBranch) {
396
- const branch = currentBranch(repoPath);
397
- if (branch === baseBranch) {
398
- return;
399
- }
400
- checkedGit(
401
- repoPath,
402
- ["branch", "--force", baseBranch, "HEAD"],
403
- `Failed to update the local '${baseBranch}' ref to the finalized HEAD`
404
- );
405
- }
406
- function buildFinalizationMarkdown(input) {
407
- const lines = [
408
- "## Finalized change",
409
- "",
410
- `- Repository workspace: ${input.workspaceClonePath}`,
411
- `- Work branch: ${input.workBranch}`,
412
- `- Base branch: ${input.baseBranch}`,
413
- `- Merge result: ${describeMergeMode(input.mergeMode)}`
414
- ];
415
- if (input.localSourceRepoPath) {
416
- lines.push(
417
- `- Local source repo: ${input.localSourceRepoPath} (${input.localSourceRepoKind === "bare" ? "bare" : "non-bare"})`
418
- );
419
- if (input.workspacePushTarget) {
420
- lines.push(`- Workspace publish: HEAD -> ${input.workspacePushTarget}`);
421
- }
422
- if (input.localSourceRepoKind === "non_bare") {
423
- lines.push(`- Base repo merge: ${input.workBranch} -> ${input.baseBranch}`);
424
- }
425
- if (input.sourceOriginPushTarget) {
426
- lines.push(
427
- `- Source origin push: ${branchRef(input.baseBranch)} -> ${input.sourceOriginPushTarget}`
428
- );
429
- } else {
430
- lines.push("- Source origin push: skipped (local source repo has no origin remote)");
431
- }
432
- } else {
433
- lines.push(`- Pushed: HEAD -> ${input.pushTarget}`);
434
- }
435
- lines.push(
436
- `- Final HEAD: \`${abbreviateSha(input.headSha)}\``,
437
- `- Conflict resolution used: ${input.usedConflictResolution ? "yes" : "no"}`
438
- );
439
- return lines.join("\n");
440
- }
441
- function commitAndPushWorkBranch(input) {
442
- const repoPath = path.resolve(input.repoPath);
443
- assertFinalizationCheckout({
444
- repoPath,
445
- workBranch: input.workBranch,
446
- commitMessage: input.commitMessage,
447
- missingCommitMessage: "A generated commit message is required before publication"
448
- });
449
- const conflicts = conflictedFiles(repoPath);
450
- if (mergeInProgress(repoPath) || conflicts.length > 0) {
451
- throw new DeterministicGitError(
452
- `Cannot publish a work branch with unresolved conflicts: ${conflicts.join(", ") || "merge in progress"}`
453
- );
454
- }
455
- const headSha = commitIfDirty({
456
- repoPath,
457
- commitMessage: input.commitMessage,
458
- gitIdentity: input.gitIdentity
459
- });
460
- const pushTarget = pushAndVerify(repoPath, input.workBranch, headSha);
461
- return {
462
- /** @public */
463
- headSha,
464
- /** @internal */
465
- pushTarget
466
- };
467
- }
468
- function runDeterministicFinalization(ctx, gitIdentity) {
469
- const progressSteps = [
470
- ["validate_checkout", "Validate the repository checkout"],
471
- ["fetch_base", "Fetch and verify the latest base branch"],
472
- ["commit_merge", "Commit changes and integrate the base branch"],
473
- ["publish", "Push the finalized branch"]
474
- ];
475
- const reportProgress = (activeIndex, completedCount, failure) => ctx.reportProgress?.({
476
- title: "Finalization progress",
477
- steps: progressSteps.map(([id, label], index) => ({
478
- id,
479
- label,
480
- status: index === failure?.index ? "failed" : index < completedCount ? "completed" : index === activeIndex ? "in_progress" : "incomplete",
481
- ...index === failure?.index ? { detail: failure.detail } : {}
482
- }))
483
- });
484
- reportProgress(0, 0);
485
- const repo = ctx.repo.get("repo");
486
- const input = {
487
- repoPath: repo.fsPath,
488
- workspaceClonePath: repo.workspaceClonePath,
489
- baseBranch: repo.baseBranch,
490
- workBranch: repo.workBranch,
491
- expectedPostConflictHeadSha: ctx.state.finalization.expectedPostConflictHeadSha,
492
- usedConflictResolution: ctx.state.finalization.usedConflictResolution,
493
- commitMessage: ctx.state.finalization.generatedCommitMessage ?? "",
494
- gitIdentity
495
- };
496
- const repoPath = path.resolve(input.repoPath);
497
- assertFinalizationCheckout({
498
- repoPath,
499
- workBranch: input.workBranch,
500
- commitMessage: input.commitMessage,
501
- missingCommitMessage: "No generated commit message is available; retry commit-message generation before finalization"
502
- });
503
- assertPostConflictCheckpoint(input, repoPath);
504
- reportProgress(1, 1);
505
- let headSha = currentHeadSha(repoPath);
506
- const conflicts = conflictedFiles(repoPath);
507
- if (mergeInProgress(repoPath) || conflicts.length > 0) {
508
- reportProgress(null, 1, {
509
- index: 2,
510
- detail: "The checkout already contains merge conflicts"
511
- });
512
- return {
513
- outcome: "merge_conflict",
514
- params: {
515
- headSha,
516
- conflictedFiles: conflicts,
517
- fetchedBaseSha: null
518
- }
519
- };
520
- }
521
- const baseSha = fetchOriginBase(repoPath, input.baseBranch);
522
- const localSourceRepo = resolveLocalSourceRepo(repoPath);
523
- if (localSourceRepo?.kind === "non_bare") {
524
- assertLocalBaseRepoReady(localSourceRepo.path, input.baseBranch, baseSha);
525
- } else if (localSourceRepo?.kind === "bare") {
526
- assertLocalBareSourceRepoReady(localSourceRepo.path, input.baseBranch, baseSha);
527
- }
528
- reportProgress(2, 2);
529
- headSha = commitIfDirty({
530
- repoPath,
531
- commitMessage: input.commitMessage,
532
- gitIdentity: input.gitIdentity
533
- });
534
- const mergeResult = deterministicMerge({
535
- repoPath,
536
- baseBranch: input.baseBranch,
537
- headBefore: headSha,
538
- baseSha,
539
- gitIdentity: input.gitIdentity
540
- });
541
- if (!mergeResult.ok) {
542
- reportProgress(null, 2, {
543
- index: 2,
544
- detail: "Base integration produced merge conflicts"
545
- });
546
- return {
547
- outcome: "merge_conflict",
548
- params: {
549
- headSha: mergeResult.headSha,
550
- conflictedFiles: mergeResult.conflictedFiles,
551
- fetchedBaseSha: baseSha
552
- }
553
- };
554
- }
555
- reportProgress(3, 3);
556
- let pushTarget;
557
- let workspacePushTarget = null;
558
- let sourceOriginPushTarget = null;
559
- if (localSourceRepo) {
560
- if (localSourceRepo.kind === "non_bare") {
561
- assertLocalBaseRepoReady(localSourceRepo.path, input.baseBranch, baseSha);
562
- workspacePushTarget = pushAndVerify(repoPath, input.workBranch, mergeResult.headSha);
563
- mergeWorkBranchIntoLocalBaseRepo({
564
- localBaseRepoPath: localSourceRepo.path,
565
- baseBranch: input.baseBranch,
566
- workBranch: input.workBranch,
567
- expectedBaseSha: baseSha,
568
- expectedHeadSha: mergeResult.headSha
569
- });
570
- } else {
571
- assertLocalBareSourceRepoReady(localSourceRepo.path, input.baseBranch, baseSha);
572
- workspacePushTarget = pushAndVerify(repoPath, input.baseBranch, mergeResult.headSha);
573
- }
574
- sourceOriginPushTarget = pushLocalSourceBaseBranchToOriginIfPresent({
575
- localSourceRepoPath: localSourceRepo.path,
576
- baseBranch: input.baseBranch,
577
- expectedHeadSha: mergeResult.headSha
578
- });
579
- pushTarget = sourceOriginPushTarget ?? originBaseRef(input.baseBranch);
580
- } else {
581
- pushTarget = pushAndVerify(repoPath, input.baseBranch, mergeResult.headSha);
582
- }
583
- updateLocalBaseRef(repoPath, input.baseBranch);
584
- const finalHeadSha = currentHeadSha(repoPath);
585
- reportProgress(null, 4);
586
- return {
587
- outcome: "finalized",
588
- params: {
589
- headSha: finalHeadSha,
590
- mergeMode: mergeResult.mergeMode,
591
- pushTarget,
592
- usedConflictResolution: input.usedConflictResolution === true
593
- },
594
- markdown: buildFinalizationMarkdown({
595
- workspaceClonePath: input.workspaceClonePath,
596
- baseBranch: input.baseBranch,
597
- workBranch: input.workBranch,
598
- headSha: finalHeadSha,
599
- mergeMode: mergeResult.mergeMode,
600
- pushTarget,
601
- localSourceRepoPath: localSourceRepo?.path ?? null,
602
- localSourceRepoKind: localSourceRepo?.kind ?? null,
603
- workspacePushTarget,
604
- sourceOriginPushTarget,
605
- usedConflictResolution: input.usedConflictResolution === true
606
- })
607
- };
608
- }
609
-
610
- export {
611
- commitAndPushWorkBranch,
612
- runDeterministicFinalization
613
- };
614
- //# sourceMappingURL=chunk-MY6XFGHM.js.map