@fern-api/replay 0.6.0 → 0.6.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 (78) hide show
  1. package/dist/cli.cjs +16642 -0
  2. package/dist/cli.cjs.map +1 -0
  3. package/dist/index.cjs +2014 -0
  4. package/dist/index.cjs.map +1 -0
  5. package/dist/index.d.cts +463 -0
  6. package/dist/index.d.ts +463 -12
  7. package/dist/index.js +1968 -10
  8. package/dist/index.js.map +1 -1
  9. package/package.json +16 -6
  10. package/dist/FernignoreMigrator.d.ts +0 -38
  11. package/dist/FernignoreMigrator.d.ts.map +0 -1
  12. package/dist/FernignoreMigrator.js +0 -210
  13. package/dist/FernignoreMigrator.js.map +0 -1
  14. package/dist/LockfileManager.d.ts +0 -31
  15. package/dist/LockfileManager.d.ts.map +0 -1
  16. package/dist/LockfileManager.js +0 -124
  17. package/dist/LockfileManager.js.map +0 -1
  18. package/dist/ReplayApplicator.d.ts +0 -30
  19. package/dist/ReplayApplicator.d.ts.map +0 -1
  20. package/dist/ReplayApplicator.js +0 -544
  21. package/dist/ReplayApplicator.js.map +0 -1
  22. package/dist/ReplayCommitter.d.ts +0 -19
  23. package/dist/ReplayCommitter.d.ts.map +0 -1
  24. package/dist/ReplayCommitter.js +0 -64
  25. package/dist/ReplayCommitter.js.map +0 -1
  26. package/dist/ReplayDetector.d.ts +0 -22
  27. package/dist/ReplayDetector.d.ts.map +0 -1
  28. package/dist/ReplayDetector.js +0 -147
  29. package/dist/ReplayDetector.js.map +0 -1
  30. package/dist/ReplayService.d.ts +0 -100
  31. package/dist/ReplayService.d.ts.map +0 -1
  32. package/dist/ReplayService.js +0 -596
  33. package/dist/ReplayService.js.map +0 -1
  34. package/dist/ThreeWayMerge.d.ts +0 -11
  35. package/dist/ThreeWayMerge.d.ts.map +0 -1
  36. package/dist/ThreeWayMerge.js +0 -48
  37. package/dist/ThreeWayMerge.js.map +0 -1
  38. package/dist/cli.d.ts +0 -3
  39. package/dist/cli.d.ts.map +0 -1
  40. package/dist/cli.js +0 -462
  41. package/dist/cli.js.map +0 -1
  42. package/dist/commands/bootstrap.d.ts +0 -46
  43. package/dist/commands/bootstrap.d.ts.map +0 -1
  44. package/dist/commands/bootstrap.js +0 -237
  45. package/dist/commands/bootstrap.js.map +0 -1
  46. package/dist/commands/forget.d.ts +0 -16
  47. package/dist/commands/forget.d.ts.map +0 -1
  48. package/dist/commands/forget.js +0 -27
  49. package/dist/commands/forget.js.map +0 -1
  50. package/dist/commands/index.d.ts +0 -6
  51. package/dist/commands/index.d.ts.map +0 -1
  52. package/dist/commands/index.js +0 -6
  53. package/dist/commands/index.js.map +0 -1
  54. package/dist/commands/reset.d.ts +0 -16
  55. package/dist/commands/reset.d.ts.map +0 -1
  56. package/dist/commands/reset.js +0 -25
  57. package/dist/commands/reset.js.map +0 -1
  58. package/dist/commands/resolve.d.ts +0 -16
  59. package/dist/commands/resolve.d.ts.map +0 -1
  60. package/dist/commands/resolve.js +0 -28
  61. package/dist/commands/resolve.js.map +0 -1
  62. package/dist/commands/status.d.ts +0 -26
  63. package/dist/commands/status.d.ts.map +0 -1
  64. package/dist/commands/status.js +0 -24
  65. package/dist/commands/status.js.map +0 -1
  66. package/dist/git/CommitDetection.d.ts +0 -7
  67. package/dist/git/CommitDetection.d.ts.map +0 -1
  68. package/dist/git/CommitDetection.js +0 -26
  69. package/dist/git/CommitDetection.js.map +0 -1
  70. package/dist/git/GitClient.d.ts +0 -22
  71. package/dist/git/GitClient.d.ts.map +0 -1
  72. package/dist/git/GitClient.js +0 -109
  73. package/dist/git/GitClient.js.map +0 -1
  74. package/dist/index.d.ts.map +0 -1
  75. package/dist/types.d.ts +0 -80
  76. package/dist/types.d.ts.map +0 -1
  77. package/dist/types.js +0 -3
  78. package/dist/types.js.map +0 -1
package/dist/index.js CHANGED
@@ -1,11 +1,1969 @@
1
- export { GitClient } from "./git/GitClient.js";
2
- export { isGenerationCommit, isReplayCommit, FERN_BOT_NAME, FERN_BOT_EMAIL, FERN_BOT_LOGIN, } from "./git/CommitDetection.js";
3
- export { LockfileManager } from "./LockfileManager.js";
4
- export { ReplayDetector } from "./ReplayDetector.js";
5
- export { threeWayMerge } from "./ThreeWayMerge.js";
6
- export { ReplayApplicator } from "./ReplayApplicator.js";
7
- export { ReplayCommitter } from "./ReplayCommitter.js";
8
- export { ReplayService } from "./ReplayService.js";
9
- export { FernignoreMigrator } from "./FernignoreMigrator.js";
10
- export { bootstrap, forget, reset, resolve, status, } from "./commands/index.js";
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropNames = Object.getOwnPropertyNames;
3
+ var __esm = (fn, res) => function __init() {
4
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
5
+ };
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+
11
+ // src/git/GitClient.ts
12
+ var GitClient_exports = {};
13
+ __export(GitClient_exports, {
14
+ GitClient: () => GitClient
15
+ });
16
+ import { simpleGit } from "simple-git";
17
+ var GitClient;
18
+ var init_GitClient = __esm({
19
+ "src/git/GitClient.ts"() {
20
+ "use strict";
21
+ GitClient = class {
22
+ git;
23
+ repoPath;
24
+ constructor(repoPath) {
25
+ this.repoPath = repoPath;
26
+ this.git = simpleGit(repoPath);
27
+ }
28
+ async exec(args) {
29
+ return this.git.raw(args);
30
+ }
31
+ async execWithInput(args, input) {
32
+ const { spawn } = await import("child_process");
33
+ return new Promise((resolve2, reject) => {
34
+ const proc = spawn("git", args, { cwd: this.repoPath });
35
+ let stdout = "";
36
+ let stderr = "";
37
+ proc.stdout.on("data", (data) => {
38
+ stdout += data.toString();
39
+ });
40
+ proc.stderr.on("data", (data) => {
41
+ stderr += data.toString();
42
+ });
43
+ proc.on("close", (code) => {
44
+ if (code === 0) {
45
+ resolve2(stdout);
46
+ } else {
47
+ reject(new Error(`git ${args.join(" ")} failed (code ${code}): ${stderr}`));
48
+ }
49
+ });
50
+ proc.stdin.write(input);
51
+ proc.stdin.end();
52
+ });
53
+ }
54
+ async formatPatch(commitSha) {
55
+ return this.exec(["format-patch", "-1", commitSha, "--stdout"]);
56
+ }
57
+ async applyPatch(patchContent) {
58
+ await this.execWithInput(["am", "--3way"], patchContent);
59
+ }
60
+ async getTreeHash(commitSha) {
61
+ return (await this.exec(["rev-parse", `${commitSha}^{tree}`])).trim();
62
+ }
63
+ async showFile(treeish, filePath) {
64
+ try {
65
+ return await this.exec(["show", `${treeish}:${filePath}`]);
66
+ } catch {
67
+ return null;
68
+ }
69
+ }
70
+ async getCommitInfo(commitSha) {
71
+ const format = "%H%x00%an%x00%ae%x00%s";
72
+ const output = await this.exec(["log", "-1", `--format=${format}`, commitSha]);
73
+ const [sha, authorName, authorEmail, message] = output.trim().split("\0");
74
+ return { sha, authorName, authorEmail, message };
75
+ }
76
+ async getCommitParents(commitSha) {
77
+ const output = await this.exec(["rev-parse", `${commitSha}^@`]);
78
+ return output.trim().split("\n").filter(Boolean);
79
+ }
80
+ async detectRenames(fromTree, toTree) {
81
+ try {
82
+ const output = await this.exec(["diff", "--find-renames", "--name-status", fromTree, toTree]);
83
+ const renames = [];
84
+ for (const line of output.trim().split("\n")) {
85
+ if (!line) continue;
86
+ if (line.startsWith("R")) {
87
+ const parts = line.split(" ");
88
+ if (parts.length >= 3) {
89
+ renames.push({ from: parts[1], to: parts[2] });
90
+ }
91
+ }
92
+ }
93
+ return renames;
94
+ } catch {
95
+ return [];
96
+ }
97
+ }
98
+ async isAncestor(commit, descendant) {
99
+ try {
100
+ const mergeBase = (await this.exec(["merge-base", commit, descendant])).trim();
101
+ return mergeBase === commit;
102
+ } catch {
103
+ return false;
104
+ }
105
+ }
106
+ async commitExists(sha) {
107
+ try {
108
+ const type = await this.exec(["cat-file", "-t", sha]);
109
+ return type.trim() === "commit";
110
+ } catch {
111
+ return false;
112
+ }
113
+ }
114
+ getRepoPath() {
115
+ return this.repoPath;
116
+ }
117
+ };
118
+ }
119
+ });
120
+
121
+ // src/index.ts
122
+ init_GitClient();
123
+
124
+ // src/git/CommitDetection.ts
125
+ var FERN_BOT_NAME = "fern-api";
126
+ var FERN_BOT_EMAIL = "115122769+fern-api[bot]@users.noreply.github.com";
127
+ var FERN_BOT_LOGIN = "fern-api[bot]";
128
+ var FERN_SUPPORT_NAMES = ["fern-support", "Fern Support"];
129
+ function isGenerationCommit(commit) {
130
+ const isFernSupport = FERN_SUPPORT_NAMES.includes(commit.authorName);
131
+ const isBotAuthor = !isFernSupport && (commit.authorLogin === FERN_BOT_LOGIN || commit.authorEmail === FERN_BOT_EMAIL || commit.authorName === FERN_BOT_NAME);
132
+ const hasGenerationMarker = commit.message.startsWith("[fern-generated]") || commit.message.startsWith("[fern-replay]") || commit.message.includes("Generated by Fern") || commit.message.includes("\u{1F916} Generated with Fern") || // Squash merge of a Fern-generated PR uses the PR title as commit message.
133
+ // The default PR title is "SDK Generation" (from GithubStep's commitMessage default).
134
+ // GitHub appends "(#N)" for the PR number, e.g. "SDK Generation (#70)".
135
+ commit.message.startsWith("SDK Generation");
136
+ return isBotAuthor || hasGenerationMarker;
137
+ }
138
+ function isReplayCommit(commit) {
139
+ return commit.message.startsWith("[fern-replay]");
140
+ }
141
+
142
+ // src/LockfileManager.ts
143
+ import { readFileSync, writeFileSync, existsSync, mkdirSync, renameSync } from "fs";
144
+ import { join, dirname } from "path";
145
+ import { stringify, parse } from "yaml";
146
+ var LOCKFILE_HEADER = "# DO NOT EDIT MANUALLY - Managed by Fern Replay\n";
147
+ var LockfileManager = class {
148
+ outputDir;
149
+ lock = null;
150
+ constructor(outputDir) {
151
+ this.outputDir = outputDir;
152
+ }
153
+ get lockfilePath() {
154
+ return join(this.outputDir, ".fern", "replay.lock");
155
+ }
156
+ get customizationsPath() {
157
+ return join(this.outputDir, ".fern", "replay.yml");
158
+ }
159
+ exists() {
160
+ return existsSync(this.lockfilePath);
161
+ }
162
+ read() {
163
+ if (this.lock) {
164
+ return this.lock;
165
+ }
166
+ if (!this.exists()) {
167
+ throw new Error(`Lockfile not found: ${this.lockfilePath}`);
168
+ }
169
+ const content = readFileSync(this.lockfilePath, "utf-8");
170
+ this.lock = parse(content);
171
+ return this.lock;
172
+ }
173
+ initialize(firstGeneration) {
174
+ this.initializeInMemory(firstGeneration);
175
+ this.save();
176
+ }
177
+ /**
178
+ * Set up in-memory lock state without writing to disk.
179
+ * Useful for bootstrap dry-run where we need state for detection
180
+ * but don't want to persist anything.
181
+ */
182
+ initializeInMemory(firstGeneration) {
183
+ this.lock = {
184
+ version: "1.0",
185
+ generations: [firstGeneration],
186
+ current_generation: firstGeneration.commit_sha,
187
+ patches: []
188
+ };
189
+ }
190
+ save() {
191
+ if (!this.lock) {
192
+ throw new Error("No lockfile data to save. Call read() or initialize() first.");
193
+ }
194
+ const dir = dirname(this.lockfilePath);
195
+ if (!existsSync(dir)) {
196
+ mkdirSync(dir, { recursive: true });
197
+ }
198
+ const yaml = stringify(this.lock, {
199
+ lineWidth: 0,
200
+ blockQuote: "literal"
201
+ });
202
+ const content = LOCKFILE_HEADER + yaml;
203
+ const tmpPath = this.lockfilePath + ".tmp";
204
+ writeFileSync(tmpPath, content, "utf-8");
205
+ renameSync(tmpPath, this.lockfilePath);
206
+ }
207
+ addGeneration(record) {
208
+ this.ensureLoaded();
209
+ this.lock.generations.push(record);
210
+ this.lock.current_generation = record.commit_sha;
211
+ }
212
+ addPatch(patch) {
213
+ this.ensureLoaded();
214
+ this.lock.patches.push(patch);
215
+ }
216
+ updatePatch(patchId, updates) {
217
+ this.ensureLoaded();
218
+ const patch = this.lock.patches.find((p) => p.id === patchId);
219
+ if (!patch) {
220
+ throw new Error(`Patch not found: ${patchId}`);
221
+ }
222
+ Object.assign(patch, updates);
223
+ }
224
+ removePatch(patchId) {
225
+ this.ensureLoaded();
226
+ this.lock.patches = this.lock.patches.filter((p) => p.id !== patchId);
227
+ }
228
+ clearPatches() {
229
+ this.ensureLoaded();
230
+ this.lock.patches = [];
231
+ }
232
+ getPatches() {
233
+ this.ensureLoaded();
234
+ return this.lock.patches;
235
+ }
236
+ setReplaySkippedAt(timestamp) {
237
+ this.ensureLoaded();
238
+ this.lock.replay_skipped_at = timestamp;
239
+ }
240
+ clearReplaySkippedAt() {
241
+ this.ensureLoaded();
242
+ delete this.lock.replay_skipped_at;
243
+ }
244
+ isReplaySkipped() {
245
+ this.ensureLoaded();
246
+ return this.lock.replay_skipped_at != null;
247
+ }
248
+ getGeneration(commitSha) {
249
+ this.ensureLoaded();
250
+ return this.lock.generations.find((g) => g.commit_sha === commitSha);
251
+ }
252
+ getCustomizationsConfig() {
253
+ if (!existsSync(this.customizationsPath)) {
254
+ return {};
255
+ }
256
+ const content = readFileSync(this.customizationsPath, "utf-8");
257
+ return parse(content) ?? {};
258
+ }
259
+ ensureLoaded() {
260
+ if (!this.lock) {
261
+ throw new Error("No lockfile loaded. Call read() or initialize() first.");
262
+ }
263
+ }
264
+ };
265
+
266
+ // src/ReplayDetector.ts
267
+ import { createHash } from "crypto";
268
+ var INFRASTRUCTURE_FILES = /* @__PURE__ */ new Set([".fernignore"]);
269
+ var ReplayDetector = class {
270
+ git;
271
+ lockManager;
272
+ sdkOutputDir;
273
+ warnings = [];
274
+ constructor(git, lockManager, sdkOutputDir) {
275
+ this.git = git;
276
+ this.lockManager = lockManager;
277
+ this.sdkOutputDir = sdkOutputDir;
278
+ }
279
+ async detectNewPatches() {
280
+ const lock = this.lockManager.read();
281
+ const lastGen = this.getLastGeneration(lock);
282
+ if (!lastGen) {
283
+ return [];
284
+ }
285
+ const exists = await this.git.commitExists(lastGen.commit_sha);
286
+ if (!exists) {
287
+ this.warnings.push(
288
+ `Generation commit ${lastGen.commit_sha.slice(0, 7)} not found in git history. Skipping new patch detection. Existing lockfile patches will still be applied.`
289
+ );
290
+ return [];
291
+ }
292
+ const isAncestor = await this.git.isAncestor(lastGen.commit_sha, "HEAD");
293
+ if (!isAncestor) {
294
+ return this.detectPatchesViaTreeDiff(lastGen);
295
+ }
296
+ const log = await this.git.exec([
297
+ "log",
298
+ "--format=%H%x00%an%x00%ae%x00%s",
299
+ `${lastGen.commit_sha}..HEAD`,
300
+ "--",
301
+ this.sdkOutputDir
302
+ ]);
303
+ if (!log.trim()) {
304
+ return [];
305
+ }
306
+ const commits = this.parseGitLog(log);
307
+ const newPatches = [];
308
+ for (const commit of commits) {
309
+ if (isGenerationCommit(commit)) {
310
+ continue;
311
+ }
312
+ const parents = await this.git.getCommitParents(commit.sha);
313
+ if (parents.length > 1) {
314
+ continue;
315
+ }
316
+ if (lock.patches.find((p) => p.original_commit === commit.sha)) {
317
+ continue;
318
+ }
319
+ const patchContent = await this.git.formatPatch(commit.sha);
320
+ const contentHash = this.computeContentHash(patchContent);
321
+ if (lock.patches.find((p) => p.content_hash === contentHash)) {
322
+ continue;
323
+ }
324
+ const filesOutput = await this.git.exec(["diff-tree", "--no-commit-id", "--name-only", "-r", commit.sha]);
325
+ const files = filesOutput.trim().split("\n").filter(Boolean).filter((f) => !INFRASTRUCTURE_FILES.has(f));
326
+ if (files.length === 0) {
327
+ continue;
328
+ }
329
+ newPatches.push({
330
+ id: `patch-${commit.sha.slice(0, 8)}`,
331
+ content_hash: contentHash,
332
+ original_commit: commit.sha,
333
+ original_message: commit.message,
334
+ original_author: `${commit.authorName} <${commit.authorEmail}>`,
335
+ base_generation: lastGen.commit_sha,
336
+ files,
337
+ patch_content: patchContent
338
+ });
339
+ }
340
+ return newPatches.reverse();
341
+ }
342
+ /**
343
+ * Compute content hash for deduplication.
344
+ * Removes commit SHA line and index lines before hashing,
345
+ * so rebased commits with same content produce the same hash.
346
+ */
347
+ computeContentHash(patchContent) {
348
+ const normalized = patchContent.split("\n").filter((line) => !line.startsWith("From ") && !line.startsWith("index ") && !line.startsWith("Date: ")).join("\n");
349
+ return `sha256:${createHash("sha256").update(normalized).digest("hex")}`;
350
+ }
351
+ /** Detect patches via tree diff for non-linear history. Returns a composite patch. */
352
+ async detectPatchesViaTreeDiff(lastGen) {
353
+ const filesOutput = await this.git.exec(["diff", "--name-only", lastGen.commit_sha, "HEAD"]);
354
+ const files = filesOutput.trim().split("\n").filter(Boolean).filter((f) => !INFRASTRUCTURE_FILES.has(f)).filter((f) => !f.startsWith(".fern/"));
355
+ if (files.length === 0) return [];
356
+ const diff = await this.git.exec(["diff", lastGen.commit_sha, "HEAD", "--", ...files]);
357
+ if (!diff.trim()) return [];
358
+ const contentHash = this.computeContentHash(diff);
359
+ const lock = this.lockManager.read();
360
+ if (lock.patches.some((p) => p.content_hash === contentHash)) {
361
+ return [];
362
+ }
363
+ const headSha = (await this.git.exec(["rev-parse", "HEAD"])).trim();
364
+ return [
365
+ {
366
+ id: `patch-composite-${headSha.slice(0, 8)}`,
367
+ content_hash: contentHash,
368
+ original_commit: headSha,
369
+ original_message: "Customer customizations (composite)",
370
+ original_author: "composite",
371
+ base_generation: lastGen.commit_sha,
372
+ files,
373
+ patch_content: diff
374
+ }
375
+ ];
376
+ }
377
+ parseGitLog(log) {
378
+ return log.trim().split("\n").map((line) => {
379
+ const [sha, authorName, authorEmail, message] = line.split("\0");
380
+ return { sha, authorName, authorEmail, message };
381
+ });
382
+ }
383
+ getLastGeneration(lock) {
384
+ return lock.generations.find((g) => g.commit_sha === lock.current_generation);
385
+ }
386
+ };
387
+
388
+ // src/ThreeWayMerge.ts
389
+ import { diff3Merge } from "node-diff3";
390
+ function threeWayMerge(base, ours, theirs) {
391
+ const baseLines = base.split("\n");
392
+ const oursLines = ours.split("\n");
393
+ const theirsLines = theirs.split("\n");
394
+ const regions = diff3Merge(oursLines, baseLines, theirsLines);
395
+ const outputLines = [];
396
+ const conflicts = [];
397
+ let currentLine = 1;
398
+ for (const region of regions) {
399
+ if (region.ok) {
400
+ outputLines.push(...region.ok);
401
+ currentLine += region.ok.length;
402
+ } else if (region.conflict) {
403
+ const startLine = currentLine;
404
+ outputLines.push("<<<<<<< Generated");
405
+ outputLines.push(...region.conflict.a);
406
+ outputLines.push("=======");
407
+ outputLines.push(...region.conflict.b);
408
+ outputLines.push(">>>>>>> Your customization");
409
+ const conflictLines = region.conflict.a.length + region.conflict.b.length + 3;
410
+ conflicts.push({
411
+ startLine,
412
+ endLine: startLine + conflictLines - 1,
413
+ ours: region.conflict.a,
414
+ theirs: region.conflict.b
415
+ });
416
+ currentLine += conflictLines;
417
+ }
418
+ }
419
+ return {
420
+ content: outputLines.join("\n"),
421
+ hasConflicts: conflicts.length > 0,
422
+ conflicts
423
+ };
424
+ }
425
+
426
+ // src/ReplayApplicator.ts
427
+ import { mkdir, mkdtemp, readFile, rm, writeFile } from "fs/promises";
428
+ import { tmpdir } from "os";
429
+ import { dirname as dirname2, extname, join as join2 } from "path";
430
+ import { minimatch } from "minimatch";
431
+ var BINARY_EXTENSIONS = /* @__PURE__ */ new Set([
432
+ ".png",
433
+ ".jpg",
434
+ ".jpeg",
435
+ ".gif",
436
+ ".bmp",
437
+ ".ico",
438
+ ".webp",
439
+ ".svg",
440
+ ".pdf",
441
+ ".doc",
442
+ ".docx",
443
+ ".xls",
444
+ ".xlsx",
445
+ ".ppt",
446
+ ".pptx",
447
+ ".zip",
448
+ ".gz",
449
+ ".tar",
450
+ ".bz2",
451
+ ".7z",
452
+ ".rar",
453
+ ".jar",
454
+ ".war",
455
+ ".ear",
456
+ ".class",
457
+ ".exe",
458
+ ".dll",
459
+ ".so",
460
+ ".dylib",
461
+ ".o",
462
+ ".a",
463
+ ".woff",
464
+ ".woff2",
465
+ ".ttf",
466
+ ".eot",
467
+ ".otf",
468
+ ".mp3",
469
+ ".mp4",
470
+ ".avi",
471
+ ".mov",
472
+ ".wav",
473
+ ".flac",
474
+ ".sqlite",
475
+ ".db",
476
+ ".pyc",
477
+ ".pyo",
478
+ ".DS_Store"
479
+ ]);
480
+ var ReplayApplicator = class {
481
+ git;
482
+ lockManager;
483
+ outputDir;
484
+ renameCache = /* @__PURE__ */ new Map();
485
+ fileTheirsAccumulator = /* @__PURE__ */ new Map();
486
+ constructor(git, lockManager, outputDir) {
487
+ this.git = git;
488
+ this.lockManager = lockManager;
489
+ this.outputDir = outputDir;
490
+ }
491
+ /** Reset inter-patch accumulator for a new cycle. */
492
+ resetAccumulator() {
493
+ this.fileTheirsAccumulator.clear();
494
+ }
495
+ /**
496
+ * Apply all patches, returning results for each.
497
+ * Skips patches that match exclude patterns in replay.yml
498
+ */
499
+ async applyPatches(patches) {
500
+ this.resetAccumulator();
501
+ const results = [];
502
+ for (const patch of patches) {
503
+ if (this.isExcluded(patch)) {
504
+ results.push({
505
+ patch,
506
+ status: "skipped",
507
+ method: "git-am"
508
+ });
509
+ continue;
510
+ }
511
+ const result = await this.applyPatchWithFallback(patch);
512
+ results.push(result);
513
+ }
514
+ return results;
515
+ }
516
+ /** Populate accumulator after git apply succeeds. */
517
+ async populateAccumulatorForPatch(patch, baseGen, currentTreeHash) {
518
+ if (!baseGen) return;
519
+ const tempDir = await mkdtemp(join2(tmpdir(), "replay-acc-"));
520
+ const { GitClient: GitClient2 } = await Promise.resolve().then(() => (init_GitClient(), GitClient_exports));
521
+ const tempGit = new GitClient2(tempDir);
522
+ await tempGit.exec(["init"]);
523
+ await tempGit.exec(["config", "user.email", "replay@fern.com"]);
524
+ await tempGit.exec(["config", "user.name", "Fern Replay"]);
525
+ try {
526
+ for (const filePath of patch.files) {
527
+ if (isBinaryFile(filePath)) continue;
528
+ const resolvedPath = await this.resolveFilePath(filePath, baseGen.tree_hash, currentTreeHash);
529
+ const base = await this.git.showFile(baseGen.tree_hash, filePath);
530
+ const theirs = await this.applyPatchToContent(base, patch.patch_content, filePath, tempGit, tempDir);
531
+ if (theirs && base) {
532
+ this.fileTheirsAccumulator.set(resolvedPath, {
533
+ content: theirs,
534
+ baseGeneration: patch.base_generation
535
+ });
536
+ }
537
+ }
538
+ } finally {
539
+ await rm(tempDir, { recursive: true }).catch(() => {
540
+ });
541
+ }
542
+ }
543
+ async applyPatchWithFallback(patch) {
544
+ const baseGen = this.lockManager.getGeneration(patch.base_generation);
545
+ const lock = this.lockManager.read();
546
+ const currentGen = lock.generations.find((g) => g.commit_sha === lock.current_generation);
547
+ const currentTreeHash = currentGen?.tree_hash ?? baseGen?.tree_hash ?? "";
548
+ const needsAccumulation = await Promise.all(
549
+ patch.files.map(async (f) => {
550
+ if (!baseGen) return false;
551
+ const resolved = await this.resolveFilePath(f, baseGen.tree_hash, currentTreeHash);
552
+ return this.fileTheirsAccumulator.has(resolved);
553
+ })
554
+ ).then((results) => results.some(Boolean));
555
+ if (!needsAccumulation) {
556
+ const snapshots = /* @__PURE__ */ new Map();
557
+ const resolvedFiles = {};
558
+ for (const filePath of patch.files) {
559
+ const resolvedPath = baseGen ? await this.resolveFilePath(filePath, baseGen.tree_hash, currentTreeHash) : filePath;
560
+ if (resolvedPath !== filePath) {
561
+ resolvedFiles[filePath] = resolvedPath;
562
+ }
563
+ const fullPath = join2(this.outputDir, resolvedPath);
564
+ snapshots.set(resolvedPath, await readFile(fullPath, "utf-8").catch(() => null));
565
+ }
566
+ try {
567
+ await this.git.execWithInput(["apply", "--3way"], patch.patch_content);
568
+ await this.populateAccumulatorForPatch(patch, baseGen, currentTreeHash);
569
+ return {
570
+ patch,
571
+ status: "applied",
572
+ method: "git-am",
573
+ ...Object.keys(resolvedFiles).length > 0 && { resolvedFiles }
574
+ };
575
+ } catch {
576
+ for (const [resolvedPath, content] of snapshots) {
577
+ if (content != null) {
578
+ await writeFile(join2(this.outputDir, resolvedPath), content);
579
+ }
580
+ }
581
+ }
582
+ }
583
+ return this.applyWithThreeWayMerge(patch);
584
+ }
585
+ async applyWithThreeWayMerge(patch) {
586
+ const fileResults = [];
587
+ const resolvedFiles = {};
588
+ const tempDir = await mkdtemp(join2(tmpdir(), "replay-"));
589
+ const { GitClient: GitClient2 } = await Promise.resolve().then(() => (init_GitClient(), GitClient_exports));
590
+ const tempGit = new GitClient2(tempDir);
591
+ await tempGit.exec(["init"]);
592
+ await tempGit.exec(["config", "user.email", "replay@fern.com"]);
593
+ await tempGit.exec(["config", "user.name", "Fern Replay"]);
594
+ try {
595
+ for (const filePath of patch.files) {
596
+ if (isBinaryFile(filePath)) {
597
+ fileResults.push({
598
+ file: filePath,
599
+ status: "skipped",
600
+ reason: "binary-file"
601
+ });
602
+ continue;
603
+ }
604
+ const result = await this.mergeFile(patch, filePath, tempGit, tempDir);
605
+ if (result.file !== filePath) {
606
+ resolvedFiles[filePath] = result.file;
607
+ }
608
+ fileResults.push(result);
609
+ }
610
+ } finally {
611
+ await rm(tempDir, { recursive: true }).catch(() => {
612
+ });
613
+ }
614
+ const conflictFiles = fileResults.filter((r) => r.status === "conflict");
615
+ const hasConflicts = conflictFiles.length > 0;
616
+ const conflictReason = hasConflicts ? conflictFiles.some((f) => f.conflictReason === "base-generation-mismatch") ? "base-generation-mismatch" : conflictFiles[0]?.conflictReason : void 0;
617
+ return {
618
+ patch,
619
+ status: hasConflicts ? "conflict" : "applied",
620
+ method: "3way-merge",
621
+ fileResults,
622
+ conflictReason,
623
+ ...Object.keys(resolvedFiles).length > 0 && { resolvedFiles }
624
+ };
625
+ }
626
+ async mergeFile(patch, filePath, tempGit, tempDir) {
627
+ try {
628
+ const baseGen = this.lockManager.getGeneration(patch.base_generation);
629
+ if (!baseGen) {
630
+ return { file: filePath, status: "skipped", reason: "base-generation-not-found" };
631
+ }
632
+ const lock = this.lockManager.read();
633
+ const currentGen = lock.generations.find((g) => g.commit_sha === lock.current_generation);
634
+ const currentTreeHash = currentGen?.tree_hash ?? baseGen.tree_hash;
635
+ const resolvedPath = await this.resolveFilePath(filePath, baseGen.tree_hash, currentTreeHash);
636
+ const metadata = {
637
+ patchId: patch.id,
638
+ patchMessage: patch.original_message,
639
+ baseGeneration: patch.base_generation,
640
+ currentGeneration: lock.current_generation
641
+ };
642
+ let base = await this.git.showFile(baseGen.tree_hash, filePath);
643
+ let renameSourcePath;
644
+ if (!base) {
645
+ const renameSource = this.extractRenameSource(patch.patch_content, filePath);
646
+ if (renameSource) {
647
+ base = await this.git.showFile(baseGen.tree_hash, renameSource);
648
+ renameSourcePath = renameSource;
649
+ }
650
+ }
651
+ const oursPath = join2(this.outputDir, resolvedPath);
652
+ const ours = await readFile(oursPath, "utf-8").catch(() => null);
653
+ let theirs = await this.applyPatchToContent(
654
+ base,
655
+ patch.patch_content,
656
+ filePath,
657
+ tempGit,
658
+ tempDir,
659
+ renameSourcePath
660
+ );
661
+ let useAccumulatorAsMergeBase = false;
662
+ const accumulatorEntry = this.fileTheirsAccumulator.get(resolvedPath);
663
+ if (!theirs && base && accumulatorEntry) {
664
+ theirs = await this.applyPatchToContent(
665
+ accumulatorEntry.content,
666
+ patch.patch_content,
667
+ filePath,
668
+ tempGit,
669
+ tempDir
670
+ );
671
+ if (theirs) {
672
+ useAccumulatorAsMergeBase = true;
673
+ }
674
+ }
675
+ let effective_theirs = theirs;
676
+ let baseMismatchSkipped = false;
677
+ if (theirs && base && !useAccumulatorAsMergeBase) {
678
+ if (accumulatorEntry && accumulatorEntry.baseGeneration === patch.base_generation) {
679
+ try {
680
+ const preMerged = threeWayMerge(base, accumulatorEntry.content, theirs);
681
+ if (!preMerged.hasConflicts) {
682
+ effective_theirs = preMerged.content;
683
+ } else {
684
+ effective_theirs = theirs;
685
+ }
686
+ } catch {
687
+ effective_theirs = theirs;
688
+ }
689
+ } else if (accumulatorEntry) {
690
+ baseMismatchSkipped = true;
691
+ }
692
+ }
693
+ if (!base && !ours && effective_theirs) {
694
+ const outDir2 = dirname2(oursPath);
695
+ await mkdir(outDir2, { recursive: true });
696
+ await writeFile(oursPath, effective_theirs);
697
+ return { file: resolvedPath, status: "merged", reason: "new-file" };
698
+ }
699
+ if (!base && ours && effective_theirs) {
700
+ const merged2 = threeWayMerge("", ours, effective_theirs);
701
+ const outDir2 = dirname2(oursPath);
702
+ await mkdir(outDir2, { recursive: true });
703
+ await writeFile(oursPath, merged2.content);
704
+ if (merged2.hasConflicts) {
705
+ return {
706
+ file: resolvedPath,
707
+ status: "conflict",
708
+ conflicts: merged2.conflicts,
709
+ conflictReason: "new-file-both",
710
+ conflictMetadata: metadata
711
+ };
712
+ }
713
+ return { file: resolvedPath, status: "merged" };
714
+ }
715
+ if (!effective_theirs) {
716
+ return {
717
+ file: resolvedPath,
718
+ status: "skipped",
719
+ reason: "missing-content"
720
+ };
721
+ }
722
+ if (!base || !ours) {
723
+ return {
724
+ file: resolvedPath,
725
+ status: "skipped",
726
+ reason: "missing-content"
727
+ };
728
+ }
729
+ const mergeBase = useAccumulatorAsMergeBase && accumulatorEntry ? accumulatorEntry.content : base;
730
+ const merged = threeWayMerge(mergeBase, ours, effective_theirs);
731
+ const outDir = dirname2(oursPath);
732
+ await mkdir(outDir, { recursive: true });
733
+ await writeFile(oursPath, merged.content);
734
+ if (effective_theirs && base) {
735
+ this.fileTheirsAccumulator.set(resolvedPath, {
736
+ content: effective_theirs,
737
+ baseGeneration: patch.base_generation
738
+ });
739
+ }
740
+ if (merged.hasConflicts) {
741
+ return {
742
+ file: resolvedPath,
743
+ status: "conflict",
744
+ conflicts: merged.conflicts,
745
+ conflictReason: baseMismatchSkipped ? "base-generation-mismatch" : "same-line-edit",
746
+ conflictMetadata: metadata
747
+ };
748
+ }
749
+ return { file: resolvedPath, status: "merged" };
750
+ } catch (error) {
751
+ return {
752
+ file: filePath,
753
+ status: "skipped",
754
+ reason: `error: ${error instanceof Error ? error.message : String(error)}`
755
+ };
756
+ }
757
+ }
758
+ isExcluded(patch) {
759
+ const config = this.lockManager.getCustomizationsConfig();
760
+ if (!config.exclude) return false;
761
+ return patch.files.some((file) => config.exclude.some((pattern) => minimatch(file, pattern)));
762
+ }
763
+ async resolveFilePath(filePath, baseTreeHash, currentTreeHash) {
764
+ const config = this.lockManager.getCustomizationsConfig();
765
+ if (config.moves) {
766
+ for (const move of config.moves) {
767
+ if (minimatch(filePath, move.from) || filePath === move.from) {
768
+ if (filePath === move.from) {
769
+ return move.to;
770
+ }
771
+ const fromBase = move.from.replace(/\*\*.*$/, "");
772
+ const toBase = move.to.replace(/\*\*.*$/, "");
773
+ if (filePath.startsWith(fromBase)) {
774
+ return toBase + filePath.slice(fromBase.length);
775
+ }
776
+ }
777
+ }
778
+ }
779
+ const cacheKey = `${baseTreeHash}:${currentTreeHash}`;
780
+ let renames = this.renameCache.get(cacheKey);
781
+ if (!renames) {
782
+ renames = await this.git.detectRenames(baseTreeHash, currentTreeHash);
783
+ this.renameCache.set(cacheKey, renames);
784
+ }
785
+ const gitRename = renames.find((r) => r.from === filePath);
786
+ if (gitRename) {
787
+ return gitRename.to;
788
+ }
789
+ return filePath;
790
+ }
791
+ async applyPatchToContent(base, patchContent, filePath, tempGit, tempDir, sourceFilePath) {
792
+ if (!base) {
793
+ return this.extractNewFileFromPatch(patchContent, filePath);
794
+ }
795
+ const fileDiff = this.extractFileDiff(patchContent, filePath);
796
+ if (!fileDiff) return null;
797
+ try {
798
+ if (sourceFilePath) {
799
+ const tempSourcePath = join2(tempDir, sourceFilePath);
800
+ await mkdir(dirname2(tempSourcePath), { recursive: true });
801
+ await writeFile(tempSourcePath, base);
802
+ await tempGit.exec(["add", sourceFilePath]);
803
+ await tempGit.exec([
804
+ "commit",
805
+ "-m",
806
+ `base for rename ${sourceFilePath} -> ${filePath}`,
807
+ "--allow-empty"
808
+ ]);
809
+ await tempGit.execWithInput(["apply", "--allow-empty"], fileDiff);
810
+ const tempTargetPath = join2(tempDir, filePath);
811
+ return await readFile(tempTargetPath, "utf-8");
812
+ }
813
+ const tempFilePath = join2(tempDir, filePath);
814
+ await mkdir(dirname2(tempFilePath), { recursive: true });
815
+ await writeFile(tempFilePath, base);
816
+ await tempGit.exec(["add", filePath]);
817
+ await tempGit.exec(["commit", "-m", `base for ${filePath}`, "--allow-empty"]);
818
+ await tempGit.execWithInput(["apply", "--allow-empty"], fileDiff);
819
+ return await readFile(tempFilePath, "utf-8");
820
+ } catch {
821
+ return null;
822
+ }
823
+ }
824
+ extractFileDiff(patchContent, filePath) {
825
+ const lines = patchContent.split("\n");
826
+ const diffLines = [];
827
+ let inTargetFile = false;
828
+ for (const line of lines) {
829
+ if (line.startsWith("diff --git")) {
830
+ if (inTargetFile) {
831
+ break;
832
+ }
833
+ if (isDiffLineForFile(line, filePath)) {
834
+ inTargetFile = true;
835
+ diffLines.push(line);
836
+ }
837
+ continue;
838
+ }
839
+ if (inTargetFile) {
840
+ diffLines.push(line);
841
+ }
842
+ }
843
+ return diffLines.length > 0 ? diffLines.join("\n") + "\n" : null;
844
+ }
845
+ extractRenameSource(patchContent, targetFilePath) {
846
+ const lines = patchContent.split("\n");
847
+ let inTargetFile = false;
848
+ for (const line of lines) {
849
+ if (line.startsWith("diff --git")) {
850
+ if (inTargetFile) break;
851
+ inTargetFile = isDiffLineForFile(line, targetFilePath);
852
+ continue;
853
+ }
854
+ if (!inTargetFile) continue;
855
+ if (line.startsWith("@@")) break;
856
+ if (line.startsWith("rename from ")) {
857
+ return line.slice("rename from ".length);
858
+ }
859
+ }
860
+ return null;
861
+ }
862
+ extractNewFileFromPatch(patchContent, filePath) {
863
+ const lines = patchContent.split("\n");
864
+ const addedLines = [];
865
+ let inTargetFile = false;
866
+ let inHunk = false;
867
+ let noTrailingNewline = false;
868
+ for (const line of lines) {
869
+ if (line.startsWith("diff --git")) {
870
+ if (inTargetFile) break;
871
+ inTargetFile = isDiffLineForFile(line, filePath);
872
+ inHunk = false;
873
+ continue;
874
+ }
875
+ if (!inTargetFile) continue;
876
+ if (line.startsWith("@@")) {
877
+ inHunk = true;
878
+ continue;
879
+ }
880
+ if (!inHunk) continue;
881
+ if (line === "\") {
882
+ noTrailingNewline = true;
883
+ continue;
884
+ }
885
+ if (line.startsWith("+") && !line.startsWith("+++")) {
886
+ addedLines.push(line.slice(1));
887
+ }
888
+ }
889
+ if (addedLines.length === 0) return null;
890
+ return addedLines.join("\n") + (noTrailingNewline ? "" : "\n");
891
+ }
892
+ };
893
+ function isBinaryFile(filePath) {
894
+ const ext = extname(filePath).toLowerCase();
895
+ return BINARY_EXTENSIONS.has(ext);
896
+ }
897
+ function isDiffLineForFile(diffLine, filePath) {
898
+ const match = diffLine.match(/^diff --git a\/.+ b\/(.+)$/);
899
+ return match !== null && match[1] === filePath;
900
+ }
901
+
902
+ // src/ReplayCommitter.ts
903
+ var ReplayCommitter = class {
904
+ git;
905
+ outputDir;
906
+ constructor(git, outputDir) {
907
+ this.git = git;
908
+ this.outputDir = outputDir;
909
+ }
910
+ async commitGeneration(message, options) {
911
+ await this.stageAll();
912
+ if (!await this.hasStagedChanges()) {
913
+ return (await this.git.exec(["rev-parse", "HEAD"])).trim();
914
+ }
915
+ let fullMessage = `[fern-generated] ${message}
916
+
917
+ Generated by Fern`;
918
+ if (options?.cliVersion) {
919
+ fullMessage += `
920
+ CLI Version: ${options.cliVersion}`;
921
+ }
922
+ if (options?.generatorVersions && Object.keys(options.generatorVersions).length > 0) {
923
+ fullMessage += "\nGenerators:";
924
+ for (const [name, version] of Object.entries(options.generatorVersions)) {
925
+ fullMessage += `
926
+ - ${name}: ${version}`;
927
+ }
928
+ }
929
+ await this.git.exec(["commit", "-m", fullMessage]);
930
+ return (await this.git.exec(["rev-parse", "HEAD"])).trim();
931
+ }
932
+ async commitReplay(_patchCount, patches) {
933
+ await this.stageAll();
934
+ if (!await this.hasStagedChanges()) {
935
+ return (await this.git.exec(["rev-parse", "HEAD"])).trim();
936
+ }
937
+ let fullMessage = `[fern-replay] Applied customizations`;
938
+ if (patches && patches.length > 0) {
939
+ fullMessage += "\n\nPatches replayed:";
940
+ for (const patch of patches) {
941
+ fullMessage += `
942
+ - ${patch.id}: ${patch.original_message}`;
943
+ }
944
+ }
945
+ await this.git.exec(["commit", "-m", fullMessage]);
946
+ return (await this.git.exec(["rev-parse", "HEAD"])).trim();
947
+ }
948
+ async createGenerationRecord(options) {
949
+ const commitSha = (await this.git.exec(["rev-parse", "HEAD"])).trim();
950
+ const treeHash = await this.getTreeHash(commitSha);
951
+ return {
952
+ commit_sha: commitSha,
953
+ tree_hash: treeHash,
954
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
955
+ cli_version: options?.cliVersion ?? "unknown",
956
+ generator_versions: options?.generatorVersions ?? {},
957
+ base_branch_head: options?.baseBranchHead
958
+ };
959
+ }
960
+ async stageAll() {
961
+ await this.git.exec(["add", "-A", this.outputDir]);
962
+ }
963
+ async hasStagedChanges() {
964
+ const output = await this.git.exec(["diff", "--cached", "--name-only"]);
965
+ return output.trim().length > 0;
966
+ }
967
+ async getTreeHash(commitSha) {
968
+ return this.git.getTreeHash(commitSha);
969
+ }
970
+ };
971
+
972
+ // src/ReplayService.ts
973
+ init_GitClient();
974
+ import { existsSync as existsSync2, readFileSync as readFileSync2 } from "fs";
975
+ import { join as join3 } from "path";
976
+ import { minimatch as minimatch2 } from "minimatch";
977
+ var ReplayService = class {
978
+ git;
979
+ detector;
980
+ applicator;
981
+ committer;
982
+ lockManager;
983
+ outputDir;
984
+ constructor(outputDir, _config) {
985
+ const git = new GitClient(outputDir);
986
+ this.git = git;
987
+ this.outputDir = outputDir;
988
+ this.lockManager = new LockfileManager(outputDir);
989
+ this.detector = new ReplayDetector(git, this.lockManager, outputDir);
990
+ this.applicator = new ReplayApplicator(git, this.lockManager, outputDir);
991
+ this.committer = new ReplayCommitter(git, outputDir);
992
+ }
993
+ async runReplay(options) {
994
+ if (options?.skipApplication) {
995
+ return this.handleSkipApplication(options);
996
+ }
997
+ const flow = this.determineFlow();
998
+ switch (flow) {
999
+ case "first-generation":
1000
+ return this.handleFirstGeneration(options);
1001
+ case "no-patches":
1002
+ return this.handleNoPatchesRegeneration(options);
1003
+ case "normal-regeneration":
1004
+ return this.handleNormalRegeneration(options);
1005
+ }
1006
+ }
1007
+ /**
1008
+ * Sync the lockfile after a divergent PR was squash-merged.
1009
+ * Call this BEFORE runReplay() when the CLI detects a merged divergent PR.
1010
+ *
1011
+ * After updating the generation record, re-detects customer patches via
1012
+ * tree diff between the generation tag (pure generation tree) and HEAD
1013
+ * (which includes customer customizations after squash merge). This ensures
1014
+ * patches survive the squash merge → regenerate cycle even when the lockfile
1015
+ * was restored from the base branch during conflict PR creation.
1016
+ */
1017
+ async syncFromDivergentMerge(generationCommitSha, options) {
1018
+ const treeHash = await this.git.getTreeHash(generationCommitSha);
1019
+ const timestamp = (await this.git.exec(["log", "-1", "--format=%aI", generationCommitSha])).trim();
1020
+ const record = {
1021
+ commit_sha: generationCommitSha,
1022
+ tree_hash: treeHash,
1023
+ timestamp,
1024
+ cli_version: options?.cliVersion ?? "unknown",
1025
+ generator_versions: options?.generatorVersions ?? {},
1026
+ base_branch_head: options?.baseBranchHead
1027
+ };
1028
+ if (!this.lockManager.exists()) {
1029
+ this.lockManager.initializeInMemory(record);
1030
+ } else {
1031
+ this.lockManager.read();
1032
+ this.lockManager.addGeneration(record);
1033
+ this.lockManager.clearPatches();
1034
+ }
1035
+ this.lockManager.save();
1036
+ try {
1037
+ const redetectedPatches = await this.detector.detectNewPatches();
1038
+ if (redetectedPatches.length > 0) {
1039
+ for (const patch of redetectedPatches) {
1040
+ this.lockManager.addPatch(patch);
1041
+ }
1042
+ this.lockManager.save();
1043
+ }
1044
+ } catch {
1045
+ }
1046
+ }
1047
+ determineFlow() {
1048
+ if (!this.lockManager.exists()) {
1049
+ return "first-generation";
1050
+ }
1051
+ const lock = this.lockManager.read();
1052
+ if (lock.patches.length === 0) {
1053
+ return "no-patches";
1054
+ }
1055
+ return "normal-regeneration";
1056
+ }
1057
+ async handleFirstGeneration(options) {
1058
+ if (options?.dryRun) {
1059
+ return {
1060
+ flow: "first-generation",
1061
+ patchesDetected: 0,
1062
+ patchesApplied: 0,
1063
+ patchesWithConflicts: 0,
1064
+ patchesSkipped: 0,
1065
+ conflicts: []
1066
+ };
1067
+ }
1068
+ const commitOpts = options ? {
1069
+ cliVersion: options.cliVersion ?? "unknown",
1070
+ generatorVersions: options.generatorVersions ?? {},
1071
+ baseBranchHead: options.baseBranchHead
1072
+ } : void 0;
1073
+ await this.committer.commitGeneration("Initial SDK generation", commitOpts);
1074
+ const genRecord = await this.committer.createGenerationRecord(commitOpts);
1075
+ this.lockManager.initialize(genRecord);
1076
+ return {
1077
+ flow: "first-generation",
1078
+ patchesDetected: 0,
1079
+ patchesApplied: 0,
1080
+ patchesWithConflicts: 0,
1081
+ patchesSkipped: 0,
1082
+ conflicts: []
1083
+ };
1084
+ }
1085
+ /**
1086
+ * Skip-application mode: commit the generation and update the lockfile
1087
+ * but don't detect or apply patches. Sets a marker so the next normal
1088
+ * run skips revert detection in preGenerationRebase().
1089
+ */
1090
+ async handleSkipApplication(options) {
1091
+ const commitOpts = options ? {
1092
+ cliVersion: options.cliVersion ?? "unknown",
1093
+ generatorVersions: options.generatorVersions ?? {},
1094
+ baseBranchHead: options.baseBranchHead
1095
+ } : void 0;
1096
+ await this.committer.commitGeneration("Update SDK (replay skipped)", commitOpts);
1097
+ const genRecord = await this.committer.createGenerationRecord(commitOpts);
1098
+ if (this.lockManager.exists()) {
1099
+ this.lockManager.read();
1100
+ this.lockManager.addGeneration(genRecord);
1101
+ } else {
1102
+ this.lockManager.initializeInMemory(genRecord);
1103
+ }
1104
+ this.lockManager.setReplaySkippedAt((/* @__PURE__ */ new Date()).toISOString());
1105
+ this.lockManager.save();
1106
+ if (!options?.stageOnly) {
1107
+ await this.committer.commitReplay(0);
1108
+ } else {
1109
+ await this.committer.stageAll();
1110
+ }
1111
+ return {
1112
+ flow: "skip-application",
1113
+ patchesDetected: 0,
1114
+ patchesApplied: 0,
1115
+ patchesWithConflicts: 0,
1116
+ patchesSkipped: 0,
1117
+ conflicts: []
1118
+ };
1119
+ }
1120
+ async handleNoPatchesRegeneration(options) {
1121
+ const newPatches = await this.detector.detectNewPatches();
1122
+ const warnings = [...this.detector.warnings];
1123
+ if (options?.dryRun) {
1124
+ return {
1125
+ flow: "no-patches",
1126
+ patchesDetected: newPatches.length,
1127
+ patchesApplied: 0,
1128
+ patchesWithConflicts: 0,
1129
+ patchesSkipped: 0,
1130
+ conflicts: [],
1131
+ wouldApply: newPatches,
1132
+ warnings: warnings.length > 0 ? warnings : void 0
1133
+ };
1134
+ }
1135
+ const commitOpts = options ? {
1136
+ cliVersion: options.cliVersion ?? "unknown",
1137
+ generatorVersions: options.generatorVersions ?? {},
1138
+ baseBranchHead: options.baseBranchHead
1139
+ } : void 0;
1140
+ await this.committer.commitGeneration("Update SDK", commitOpts);
1141
+ const genRecord = await this.committer.createGenerationRecord(commitOpts);
1142
+ this.lockManager.addGeneration(genRecord);
1143
+ let results = [];
1144
+ if (newPatches.length > 0) {
1145
+ results = await this.applicator.applyPatches(newPatches);
1146
+ }
1147
+ const rebaseCounts = await this.rebasePatches(results, genRecord.commit_sha);
1148
+ for (const patch of newPatches) {
1149
+ if (!rebaseCounts.absorbedPatchIds.has(patch.id)) {
1150
+ this.lockManager.addPatch(patch);
1151
+ }
1152
+ }
1153
+ this.lockManager.save();
1154
+ if (newPatches.length > 0) {
1155
+ if (!options?.stageOnly) {
1156
+ const appliedCount = results.filter((r) => r.status === "applied" || r.status === "conflict").length;
1157
+ if (appliedCount > 0) {
1158
+ await this.committer.commitReplay(appliedCount, newPatches);
1159
+ }
1160
+ } else {
1161
+ await this.committer.stageAll();
1162
+ }
1163
+ }
1164
+ return this.buildReport("no-patches", newPatches, results, options, warnings, rebaseCounts);
1165
+ }
1166
+ async handleNormalRegeneration(options) {
1167
+ if (options?.dryRun) {
1168
+ const existingPatches2 = this.lockManager.getPatches();
1169
+ const newPatches2 = await this.detector.detectNewPatches();
1170
+ const warnings2 = [...this.detector.warnings];
1171
+ const allPatches2 = [...existingPatches2, ...newPatches2];
1172
+ return {
1173
+ flow: "normal-regeneration",
1174
+ patchesDetected: allPatches2.length,
1175
+ patchesApplied: 0,
1176
+ patchesWithConflicts: 0,
1177
+ patchesSkipped: 0,
1178
+ conflicts: [],
1179
+ wouldApply: allPatches2,
1180
+ warnings: warnings2.length > 0 ? warnings2 : void 0
1181
+ };
1182
+ }
1183
+ let existingPatches = this.lockManager.getPatches();
1184
+ const preRebaseCounts = await this.preGenerationRebase(existingPatches);
1185
+ existingPatches = this.lockManager.getPatches();
1186
+ const seenHashes = /* @__PURE__ */ new Set();
1187
+ for (const p of existingPatches) {
1188
+ if (seenHashes.has(p.content_hash)) {
1189
+ this.lockManager.removePatch(p.id);
1190
+ } else {
1191
+ seenHashes.add(p.content_hash);
1192
+ }
1193
+ }
1194
+ existingPatches = this.lockManager.getPatches();
1195
+ const newPatches = await this.detector.detectNewPatches();
1196
+ const warnings = [...this.detector.warnings];
1197
+ const allPatches = [...existingPatches, ...newPatches];
1198
+ const commitOpts = options ? {
1199
+ cliVersion: options.cliVersion ?? "unknown",
1200
+ generatorVersions: options.generatorVersions ?? {},
1201
+ baseBranchHead: options.baseBranchHead
1202
+ } : void 0;
1203
+ await this.committer.commitGeneration("Update SDK", commitOpts);
1204
+ const genRecord = await this.committer.createGenerationRecord(commitOpts);
1205
+ this.lockManager.addGeneration(genRecord);
1206
+ const results = await this.applicator.applyPatches(allPatches);
1207
+ const rebaseCounts = await this.rebasePatches(results, genRecord.commit_sha);
1208
+ for (const patch of newPatches) {
1209
+ if (!rebaseCounts.absorbedPatchIds.has(patch.id)) {
1210
+ this.lockManager.addPatch(patch);
1211
+ }
1212
+ }
1213
+ this.lockManager.save();
1214
+ if (options?.stageOnly) {
1215
+ await this.committer.stageAll();
1216
+ } else {
1217
+ const appliedCount = results.filter((r) => r.status === "applied" || r.status === "conflict").length;
1218
+ if (appliedCount > 0) {
1219
+ await this.committer.commitReplay(appliedCount, allPatches);
1220
+ }
1221
+ }
1222
+ return this.buildReport(
1223
+ "normal-regeneration",
1224
+ allPatches,
1225
+ results,
1226
+ options,
1227
+ warnings,
1228
+ rebaseCounts,
1229
+ preRebaseCounts
1230
+ );
1231
+ }
1232
+ /**
1233
+ * Rebase cleanly applied patches so they are relative to the current generation.
1234
+ * This prevents recurring conflicts on subsequent regenerations.
1235
+ * Returns the number of patches rebased.
1236
+ */
1237
+ async rebasePatches(results, currentGenSha) {
1238
+ let absorbed = 0;
1239
+ let repointed = 0;
1240
+ let contentRebased = 0;
1241
+ let keptAsUserOwned = 0;
1242
+ const seenContentHashes = /* @__PURE__ */ new Set();
1243
+ const absorbedPatchIds = /* @__PURE__ */ new Set();
1244
+ for (const result of results) {
1245
+ if (result.resolvedFiles && Object.keys(result.resolvedFiles).length > 0) {
1246
+ const patch = result.patch;
1247
+ const updatedFiles = patch.files.map((f) => result.resolvedFiles[f] ?? f);
1248
+ patch.files = updatedFiles;
1249
+ try {
1250
+ this.lockManager.updatePatch(patch.id, { files: updatedFiles });
1251
+ } catch {
1252
+ }
1253
+ }
1254
+ }
1255
+ for (const result of results) {
1256
+ if (result.status === "conflict" && result.fileResults) {
1257
+ await this.trimAbsorbedFiles(result, currentGenSha);
1258
+ continue;
1259
+ }
1260
+ if (result.status !== "applied") continue;
1261
+ const patch = result.patch;
1262
+ if (patch.base_generation === currentGenSha) continue;
1263
+ try {
1264
+ const fernignorePatterns = this.readFernignorePatterns();
1265
+ const isUserOwned = await Promise.all(
1266
+ patch.files.map(async (file) => {
1267
+ if (file === ".fernignore") {
1268
+ return true;
1269
+ }
1270
+ if (fernignorePatterns.some((p) => file === p || minimatch2(file, p))) {
1271
+ return true;
1272
+ }
1273
+ const content = await this.git.showFile(currentGenSha, file);
1274
+ return content === null;
1275
+ })
1276
+ );
1277
+ const hasUserOwnedFiles = isUserOwned.some(Boolean);
1278
+ if (hasUserOwnedFiles) {
1279
+ this.lockManager.updatePatch(patch.id, {
1280
+ base_generation: currentGenSha
1281
+ });
1282
+ keptAsUserOwned++;
1283
+ continue;
1284
+ }
1285
+ const diff = await this.git.exec(["diff", currentGenSha, "--", ...patch.files]).catch(() => null);
1286
+ if (!diff || !diff.trim()) {
1287
+ this.lockManager.removePatch(patch.id);
1288
+ absorbedPatchIds.add(patch.id);
1289
+ absorbed++;
1290
+ continue;
1291
+ }
1292
+ const newContentHash = this.detector.computeContentHash(diff);
1293
+ if (seenContentHashes.has(newContentHash)) {
1294
+ this.lockManager.removePatch(patch.id);
1295
+ absorbedPatchIds.add(patch.id);
1296
+ absorbed++;
1297
+ continue;
1298
+ }
1299
+ seenContentHashes.add(newContentHash);
1300
+ this.lockManager.updatePatch(patch.id, {
1301
+ base_generation: currentGenSha,
1302
+ patch_content: diff,
1303
+ content_hash: newContentHash
1304
+ });
1305
+ contentRebased++;
1306
+ } catch {
1307
+ }
1308
+ }
1309
+ return { absorbed, repointed, contentRebased, keptAsUserOwned, absorbedPatchIds };
1310
+ }
1311
+ /**
1312
+ * For conflict patches with mixed results (some files merged, some conflicted),
1313
+ * check if the cleanly merged files were absorbed by the generator (empty diff).
1314
+ * If so, remove them from patch.files so they don't pollute the pre-generation
1315
+ * rebase conflict marker check (`git grep <<<<<<< -- ...patch.files`).
1316
+ *
1317
+ * Non-absorbed clean files stay in patch.files — removing them would lose
1318
+ * the customization on the next generation.
1319
+ */
1320
+ async trimAbsorbedFiles(result, currentGenSha) {
1321
+ const cleanFiles = result.fileResults.filter((f) => f.status === "merged").map((f) => f.file);
1322
+ if (cleanFiles.length === 0) return;
1323
+ const patch = result.patch;
1324
+ const fernignorePatterns = this.readFernignorePatterns();
1325
+ const generatorCleanFiles = [];
1326
+ for (const file of cleanFiles) {
1327
+ if (file === ".fernignore") continue;
1328
+ if (fernignorePatterns.some((p) => file === p || minimatch2(file, p))) continue;
1329
+ const content = await this.git.showFile(currentGenSha, file);
1330
+ if (content === null) continue;
1331
+ generatorCleanFiles.push(file);
1332
+ }
1333
+ if (generatorCleanFiles.length === 0) return;
1334
+ const absorbedFiles = /* @__PURE__ */ new Set();
1335
+ for (const file of generatorCleanFiles) {
1336
+ try {
1337
+ const diff = await this.git.exec(["diff", currentGenSha, "--", file]).catch(() => null);
1338
+ if (!diff || !diff.trim()) {
1339
+ absorbedFiles.add(file);
1340
+ }
1341
+ } catch {
1342
+ }
1343
+ }
1344
+ if (absorbedFiles.size === 0) return;
1345
+ const remainingFiles = patch.files.filter((f) => !absorbedFiles.has(f));
1346
+ if (remainingFiles.length === patch.files.length) return;
1347
+ try {
1348
+ this.lockManager.updatePatch(patch.id, { files: remainingFiles });
1349
+ } catch {
1350
+ patch.files = remainingFiles;
1351
+ }
1352
+ }
1353
+ /**
1354
+ * Pre-generation rebase: update patches using the customer's current state.
1355
+ * Called BEFORE commitGeneration() while HEAD has customer code.
1356
+ */
1357
+ async preGenerationRebase(patches) {
1358
+ const lock = this.lockManager.read();
1359
+ const currentGen = lock.current_generation;
1360
+ if (this.lockManager.isReplaySkipped()) {
1361
+ this.lockManager.clearReplaySkippedAt();
1362
+ return { conflictResolved: 0, conflictAbsorbed: 0, contentRefreshed: 0 };
1363
+ }
1364
+ let conflictResolved = 0;
1365
+ let conflictAbsorbed = 0;
1366
+ let contentRefreshed = 0;
1367
+ for (const patch of patches) {
1368
+ if (patch.base_generation === currentGen) {
1369
+ try {
1370
+ const diff = await this.git.exec(["diff", currentGen, "HEAD", "--", ...patch.files]).catch(() => null);
1371
+ if (diff === null) continue;
1372
+ if (!diff.trim()) {
1373
+ this.lockManager.removePatch(patch.id);
1374
+ contentRefreshed++;
1375
+ continue;
1376
+ }
1377
+ const newContentHash = this.detector.computeContentHash(diff);
1378
+ if (newContentHash !== patch.content_hash) {
1379
+ const filesOutput = await this.git.exec(["diff", "--name-only", currentGen, "HEAD", "--", ...patch.files]).catch(() => null);
1380
+ const newFiles = filesOutput ? filesOutput.trim().split("\n").filter(Boolean).filter((f) => !f.startsWith(".fern/")) : patch.files;
1381
+ if (newFiles.length === 0) {
1382
+ this.lockManager.removePatch(patch.id);
1383
+ } else {
1384
+ this.lockManager.updatePatch(patch.id, {
1385
+ patch_content: diff,
1386
+ content_hash: newContentHash,
1387
+ files: newFiles
1388
+ });
1389
+ }
1390
+ contentRefreshed++;
1391
+ }
1392
+ } catch {
1393
+ }
1394
+ continue;
1395
+ }
1396
+ try {
1397
+ const markerFiles = await this.git.exec(["grep", "-l", "<<<<<<<", "HEAD", "--", ...patch.files]).catch(() => "");
1398
+ if (markerFiles.trim()) continue;
1399
+ const diff = await this.git.exec(["diff", currentGen, "HEAD", "--", ...patch.files]).catch(() => null);
1400
+ if (diff === null) continue;
1401
+ if (!diff.trim()) {
1402
+ this.lockManager.removePatch(patch.id);
1403
+ conflictAbsorbed++;
1404
+ continue;
1405
+ }
1406
+ const newContentHash = this.detector.computeContentHash(diff);
1407
+ this.lockManager.updatePatch(patch.id, {
1408
+ base_generation: currentGen,
1409
+ patch_content: diff,
1410
+ content_hash: newContentHash
1411
+ });
1412
+ conflictResolved++;
1413
+ } catch {
1414
+ }
1415
+ }
1416
+ return { conflictResolved, conflictAbsorbed, contentRefreshed };
1417
+ }
1418
+ readFernignorePatterns() {
1419
+ const fernignorePath = join3(this.outputDir, ".fernignore");
1420
+ if (!existsSync2(fernignorePath)) return [];
1421
+ return readFileSync2(fernignorePath, "utf-8").split("\n").map((line) => line.trim()).filter((line) => line && !line.startsWith("#"));
1422
+ }
1423
+ buildReport(flow, patches, results, options, warnings, rebaseCounts, preRebaseCounts) {
1424
+ const conflictResults = results.filter((r) => r.status === "conflict");
1425
+ const conflictDetails = conflictResults.map((r) => {
1426
+ const conflictFiles = r.fileResults?.filter((f) => f.status === "conflict") ?? [];
1427
+ const cleanFiles = r.fileResults?.filter((f) => f.status === "merged").map((f) => f.file) ?? [];
1428
+ return {
1429
+ patchId: r.patch.id,
1430
+ patchMessage: r.patch.original_message,
1431
+ reason: r.conflictReason,
1432
+ files: conflictFiles,
1433
+ cleanFiles: cleanFiles.length > 0 ? cleanFiles : void 0
1434
+ };
1435
+ }).filter((d) => d.files.length > 0);
1436
+ const partialCount = conflictDetails.filter((d) => d.cleanFiles && d.cleanFiles.length > 0).length;
1437
+ return {
1438
+ flow,
1439
+ patchesDetected: patches.length,
1440
+ patchesApplied: results.filter((r) => r.status === "applied").length,
1441
+ patchesWithConflicts: conflictResults.length,
1442
+ patchesSkipped: results.filter((r) => r.status === "skipped").length,
1443
+ patchesPartiallyApplied: partialCount > 0 ? partialCount : void 0,
1444
+ patchesAbsorbed: rebaseCounts && rebaseCounts.absorbed > 0 ? rebaseCounts.absorbed : void 0,
1445
+ patchesRepointed: rebaseCounts && rebaseCounts.repointed > 0 ? rebaseCounts.repointed : void 0,
1446
+ patchesContentRebased: rebaseCounts && rebaseCounts.contentRebased > 0 ? rebaseCounts.contentRebased : void 0,
1447
+ patchesKeptAsUserOwned: rebaseCounts && rebaseCounts.keptAsUserOwned > 0 ? rebaseCounts.keptAsUserOwned : void 0,
1448
+ patchesConflictResolved: preRebaseCounts && preRebaseCounts.conflictResolved + preRebaseCounts.conflictAbsorbed > 0 ? preRebaseCounts.conflictResolved + preRebaseCounts.conflictAbsorbed : void 0,
1449
+ patchesRefreshed: preRebaseCounts && preRebaseCounts.contentRefreshed > 0 ? preRebaseCounts.contentRefreshed : void 0,
1450
+ conflicts: conflictResults.flatMap((r) => r.fileResults?.filter((f) => f.status === "conflict") ?? []),
1451
+ conflictDetails: conflictDetails.length > 0 ? conflictDetails : void 0,
1452
+ wouldApply: options?.dryRun ? patches : void 0,
1453
+ warnings: warnings && warnings.length > 0 ? warnings : void 0
1454
+ };
1455
+ }
1456
+ };
1457
+
1458
+ // src/FernignoreMigrator.ts
1459
+ import { createHash as createHash2 } from "crypto";
1460
+ import { existsSync as existsSync3, mkdirSync as mkdirSync2, readFileSync as readFileSync3, statSync, writeFileSync as writeFileSync2 } from "fs";
1461
+ import { dirname as dirname3, join as join4 } from "path";
1462
+ import { minimatch as minimatch3 } from "minimatch";
1463
+ import { parse as parse2, stringify as stringify2 } from "yaml";
1464
+ var FernignoreMigrator = class {
1465
+ git;
1466
+ lockManager;
1467
+ outputDir;
1468
+ constructor(git, lockManager, outputDir) {
1469
+ this.git = git;
1470
+ this.lockManager = lockManager;
1471
+ this.outputDir = outputDir;
1472
+ }
1473
+ fernignoreExists() {
1474
+ return existsSync3(join4(this.outputDir, ".fernignore"));
1475
+ }
1476
+ readFernignorePatterns() {
1477
+ const fernignorePath = join4(this.outputDir, ".fernignore");
1478
+ if (!existsSync3(fernignorePath)) {
1479
+ return [];
1480
+ }
1481
+ const content = readFileSync3(fernignorePath, "utf-8");
1482
+ return content.split("\n").map((line) => line.trim()).filter((line) => line && !line.startsWith("#"));
1483
+ }
1484
+ /** Analyze .fernignore patterns vs git history. Creates synthetic patches for files differing from pristine generation. */
1485
+ async analyzeMigration() {
1486
+ const patterns = this.readFernignorePatterns();
1487
+ const detector = new ReplayDetector(this.git, this.lockManager, this.outputDir);
1488
+ const patches = await detector.detectNewPatches();
1489
+ const trackedByBoth = [];
1490
+ const fernignoreOnly = [];
1491
+ const commitsOnly = [];
1492
+ const syntheticPatches = [];
1493
+ for (const pattern of patterns) {
1494
+ const matchingPatch = patches.find((p) => p.files.some((f) => minimatch3(f, pattern) || f === pattern));
1495
+ if (matchingPatch) {
1496
+ trackedByBoth.push({
1497
+ file: pattern,
1498
+ commit: matchingPatch.original_commit
1499
+ });
1500
+ } else {
1501
+ fernignoreOnly.push(pattern);
1502
+ }
1503
+ }
1504
+ const lock = this.lockManager.read();
1505
+ const currentGen = lock.generations.find((g) => g.commit_sha === lock.current_generation);
1506
+ if (currentGen && fernignoreOnly.length > 0) {
1507
+ const resolvedFiles = await this.resolvePatterns(fernignoreOnly);
1508
+ const remainingFernignoreOnly = [];
1509
+ for (const { pattern, files } of resolvedFiles) {
1510
+ const patchFiles = [];
1511
+ const diffParts = [];
1512
+ for (const filePath of files) {
1513
+ const pristine = await this.git.showFile(currentGen.tree_hash, filePath);
1514
+ const currentContent = this.readFileContent(filePath);
1515
+ if (currentContent === null) {
1516
+ continue;
1517
+ }
1518
+ if (pristine === null) {
1519
+ patchFiles.push(filePath);
1520
+ diffParts.push(this.createNewFileDiff(filePath, currentContent));
1521
+ } else if (pristine !== currentContent) {
1522
+ patchFiles.push(filePath);
1523
+ diffParts.push(this.createFileDiff(filePath, pristine, currentContent));
1524
+ }
1525
+ }
1526
+ if (patchFiles.length > 0) {
1527
+ const patchContent = diffParts.join("\n");
1528
+ const contentHash = `sha256:${createHash2("sha256").update(patchContent).digest("hex")}`;
1529
+ syntheticPatches.push({
1530
+ id: `patch-fernignore-${createHash2("sha256").update(pattern).digest("hex").slice(0, 8)}`,
1531
+ content_hash: contentHash,
1532
+ original_commit: currentGen.commit_sha,
1533
+ original_message: `[fernignore-migration] Customizations for ${pattern}`,
1534
+ original_author: "Fern Replay <replay@buildwithfern.com>",
1535
+ base_generation: currentGen.commit_sha,
1536
+ files: patchFiles,
1537
+ patch_content: patchContent
1538
+ });
1539
+ trackedByBoth.push({
1540
+ file: pattern,
1541
+ commit: `synthetic (differs from generated)`
1542
+ });
1543
+ } else {
1544
+ remainingFernignoreOnly.push(pattern);
1545
+ }
1546
+ }
1547
+ fernignoreOnly.length = 0;
1548
+ fernignoreOnly.push(...remainingFernignoreOnly);
1549
+ }
1550
+ for (const patch of patches) {
1551
+ const hasUnprotectedFiles = patch.files.some((f) => !patterns.some((p) => minimatch3(f, p) || f === p));
1552
+ if (hasUnprotectedFiles) {
1553
+ commitsOnly.push(patch);
1554
+ }
1555
+ }
1556
+ return { trackedByBoth, fernignoreOnly, commitsOnly, syntheticPatches };
1557
+ }
1558
+ async resolvePatterns(patterns) {
1559
+ const allFiles = (await this.git.exec(["ls-files"])).trim().split("\n").filter(Boolean);
1560
+ const results = [];
1561
+ for (const pattern of patterns) {
1562
+ const matching = allFiles.filter(
1563
+ (f) => minimatch3(f, pattern) || f === pattern || f.startsWith(pattern + "/")
1564
+ );
1565
+ results.push({ pattern, files: matching.length > 0 ? matching : [pattern] });
1566
+ }
1567
+ return results;
1568
+ }
1569
+ readFileContent(filePath) {
1570
+ const fullPath = join4(this.outputDir, filePath);
1571
+ if (!existsSync3(fullPath)) {
1572
+ return null;
1573
+ }
1574
+ try {
1575
+ const stat = statSync(fullPath);
1576
+ if (stat.isDirectory()) {
1577
+ return null;
1578
+ }
1579
+ return readFileSync3(fullPath, "utf-8");
1580
+ } catch {
1581
+ return null;
1582
+ }
1583
+ }
1584
+ createNewFileDiff(filePath, content) {
1585
+ const lines = content.split("\n");
1586
+ const hunks = lines.map((l) => `+${l}`).join("\n");
1587
+ return [
1588
+ `diff --git a/${filePath} b/${filePath}`,
1589
+ "new file mode 100644",
1590
+ `--- /dev/null`,
1591
+ `+++ b/${filePath}`,
1592
+ `@@ -0,0 +1,${lines.length} @@`,
1593
+ hunks
1594
+ ].join("\n");
1595
+ }
1596
+ createFileDiff(filePath, pristine, current) {
1597
+ const oldLines = pristine.split("\n");
1598
+ const newLines = current.split("\n");
1599
+ const removals = oldLines.map((l) => `-${l}`).join("\n");
1600
+ const additions = newLines.map((l) => `+${l}`).join("\n");
1601
+ return [
1602
+ `diff --git a/${filePath} b/${filePath}`,
1603
+ `--- a/${filePath}`,
1604
+ `+++ b/${filePath}`,
1605
+ `@@ -1,${oldLines.length} +1,${newLines.length} @@`,
1606
+ removals,
1607
+ additions
1608
+ ].join("\n");
1609
+ }
1610
+ async migrate() {
1611
+ const analysis = await this.analyzeMigration();
1612
+ const detector = new ReplayDetector(this.git, this.lockManager, this.outputDir);
1613
+ const patches = await detector.detectNewPatches();
1614
+ const warnings = [];
1615
+ let patchesCreated = 0;
1616
+ for (const patch of patches) {
1617
+ this.lockManager.addPatch(patch);
1618
+ patchesCreated++;
1619
+ }
1620
+ if (patchesCreated > 0) {
1621
+ this.lockManager.save();
1622
+ }
1623
+ for (const file of analysis.fernignoreOnly) {
1624
+ warnings.push(
1625
+ `${file}: in .fernignore but no commit history found. Commit this file or keep in .fernignore as fallback.`
1626
+ );
1627
+ }
1628
+ return {
1629
+ patchesCreated,
1630
+ filesSkipped: analysis.fernignoreOnly,
1631
+ warnings
1632
+ };
1633
+ }
1634
+ movePatternsToReplayYml(patterns) {
1635
+ const replayYmlPath = join4(this.outputDir, ".fern", "replay.yml");
1636
+ let config = {};
1637
+ if (existsSync3(replayYmlPath)) {
1638
+ const content = readFileSync3(replayYmlPath, "utf-8");
1639
+ config = parse2(content) ?? {};
1640
+ }
1641
+ const existing = config.exclude ?? [];
1642
+ const merged = [.../* @__PURE__ */ new Set([...existing, ...patterns])];
1643
+ config.exclude = merged;
1644
+ const dir = dirname3(replayYmlPath);
1645
+ if (!existsSync3(dir)) {
1646
+ mkdirSync2(dir, { recursive: true });
1647
+ }
1648
+ writeFileSync2(replayYmlPath, stringify2(config, { lineWidth: 0 }), "utf-8");
1649
+ }
1650
+ };
1651
+
1652
+ // src/commands/bootstrap.ts
1653
+ import { createHash as createHash3 } from "crypto";
1654
+ import { existsSync as existsSync4, readFileSync as readFileSync4, writeFileSync as writeFileSync3 } from "fs";
1655
+ import { join as join5 } from "path";
1656
+ init_GitClient();
1657
+ async function bootstrap(outputDir, options) {
1658
+ const git = new GitClient(outputDir);
1659
+ const lockManager = new LockfileManager(outputDir);
1660
+ const maxCommits = options?.maxCommitsToScan ?? 500;
1661
+ const warnings = [];
1662
+ if (lockManager.exists() && !options?.force) {
1663
+ return {
1664
+ generationCommit: null,
1665
+ patchesDetected: 0,
1666
+ patchesCreated: 0,
1667
+ patches: [],
1668
+ fernignorePatterns: [],
1669
+ fernignoreUpdated: false,
1670
+ warnings: ["Replay lockfile already exists. Use --force to overwrite."],
1671
+ staleGenerationsSkipped: 0,
1672
+ scannedSinceGeneration: ""
1673
+ };
1674
+ }
1675
+ const genCommits = await findAllGenerationCommits(git, maxCommits);
1676
+ if (genCommits.length === 0) {
1677
+ return {
1678
+ generationCommit: null,
1679
+ patchesDetected: 0,
1680
+ patchesCreated: 0,
1681
+ patches: [],
1682
+ fernignorePatterns: [],
1683
+ fernignoreUpdated: false,
1684
+ warnings: [
1685
+ "No generation commits found in the last " + maxCommits + " commits. Run 'fern generate' first to establish a baseline."
1686
+ ],
1687
+ staleGenerationsSkipped: 0,
1688
+ scannedSinceGeneration: ""
1689
+ };
1690
+ }
1691
+ const latestGen = genCommits[0];
1692
+ const anchorSha = options?.importHistory ? latestGen.sha : (await git.exec(["rev-parse", "HEAD"])).trim();
1693
+ const treeHash = await git.getTreeHash(anchorSha);
1694
+ const genRecord = {
1695
+ commit_sha: anchorSha,
1696
+ tree_hash: treeHash,
1697
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
1698
+ cli_version: "unknown",
1699
+ generator_versions: {}
1700
+ };
1701
+ lockManager.initializeInMemory(genRecord);
1702
+ const patches = options?.importHistory ? await findAllUserPatches(git, genCommits) : [];
1703
+ const migrator = new FernignoreMigrator(git, lockManager, outputDir);
1704
+ const fernignorePatterns = migrator.readFernignorePatterns();
1705
+ let fernignoreAnalysis;
1706
+ if (options?.importHistory && migrator.fernignoreExists() && fernignorePatterns.length > 0) {
1707
+ fernignoreAnalysis = await migrator.analyzeMigration();
1708
+ if (fernignoreAnalysis.syntheticPatches.length > 0) {
1709
+ patches.push(...fernignoreAnalysis.syntheticPatches);
1710
+ }
1711
+ if (fernignoreAnalysis.fernignoreOnly.length > 0) {
1712
+ for (const file of fernignoreAnalysis.fernignoreOnly) {
1713
+ warnings.push(
1714
+ `${file}: in .fernignore but no recent commits found since last generation. File may be stale, matches generated output, or does not exist. No customization to track.`
1715
+ );
1716
+ }
1717
+ }
1718
+ }
1719
+ if (options?.dryRun) {
1720
+ return {
1721
+ generationCommit: latestGen,
1722
+ patchesDetected: patches.length,
1723
+ patchesCreated: 0,
1724
+ patches,
1725
+ fernignorePatterns,
1726
+ fernignoreAnalysis,
1727
+ fernignoreUpdated: false,
1728
+ warnings,
1729
+ staleGenerationsSkipped: genCommits.length - 1,
1730
+ scannedSinceGeneration: latestGen.sha
1731
+ };
1732
+ }
1733
+ for (const patch of patches) {
1734
+ lockManager.addPatch(patch);
1735
+ }
1736
+ lockManager.save();
1737
+ const fernignoreUpdated = ensureFernignoreEntries(outputDir);
1738
+ if (migrator.fernignoreExists() && fernignorePatterns.length > 0) {
1739
+ const action = options?.fernignoreAction ?? "skip";
1740
+ if (action === "migrate") {
1741
+ const patternsToExclude = fernignoreAnalysis?.fernignoreOnly ?? [];
1742
+ if (patternsToExclude.length > 0) {
1743
+ migrator.movePatternsToReplayYml(patternsToExclude);
1744
+ }
1745
+ }
1746
+ }
1747
+ return {
1748
+ generationCommit: latestGen,
1749
+ patchesDetected: patches.length,
1750
+ patchesCreated: patches.length,
1751
+ patches,
1752
+ fernignorePatterns,
1753
+ fernignoreAnalysis,
1754
+ fernignoreUpdated,
1755
+ warnings,
1756
+ staleGenerationsSkipped: genCommits.length - 1,
1757
+ scannedSinceGeneration: latestGen.sha
1758
+ };
1759
+ }
1760
+ async function findAllGenerationCommits(git, maxCommits) {
1761
+ const log = await git.exec(["log", "--format=%H%x00%an%x00%ae%x00%s", `-${maxCommits}`]);
1762
+ if (!log.trim()) {
1763
+ return [];
1764
+ }
1765
+ const genCommits = [];
1766
+ for (const line of log.trim().split("\n")) {
1767
+ if (!line) continue;
1768
+ const [sha, authorName, authorEmail, message] = line.split("\0");
1769
+ const commit = { sha, authorName, authorEmail, message };
1770
+ if (isGenerationCommit(commit) && !isReplayCommit(commit)) {
1771
+ genCommits.push(commit);
1772
+ }
1773
+ }
1774
+ return genCommits;
1775
+ }
1776
+ async function findAllUserPatches(git, genCommits) {
1777
+ const patches = [];
1778
+ const seenHashes = /* @__PURE__ */ new Set();
1779
+ const latestGen = genCommits[0];
1780
+ const recentPatches = await extractUserPatches(git, latestGen.sha, "HEAD", latestGen.sha, seenHashes);
1781
+ patches.push(...recentPatches);
1782
+ return patches;
1783
+ }
1784
+ async function extractUserPatches(git, fromSha, toRef, baseGeneration, seenHashes) {
1785
+ const log = await git.exec(["log", "--format=%H%x00%an%x00%ae%x00%s", `${fromSha}..${toRef}`]);
1786
+ if (!log.trim()) {
1787
+ return [];
1788
+ }
1789
+ const commits = parseGitLog(log);
1790
+ const patches = [];
1791
+ for (const commit of commits.reverse()) {
1792
+ if (isGenerationCommit(commit)) continue;
1793
+ const parents = await git.getCommitParents(commit.sha);
1794
+ if (parents.length > 1) continue;
1795
+ const patchContent = await git.formatPatch(commit.sha);
1796
+ const contentHash = computeContentHash(patchContent);
1797
+ if (seenHashes.has(contentHash)) continue;
1798
+ seenHashes.add(contentHash);
1799
+ const filesOutput = await git.exec(["diff-tree", "--no-commit-id", "--name-only", "-r", commit.sha]);
1800
+ patches.push({
1801
+ id: `patch-${commit.sha.slice(0, 8)}`,
1802
+ content_hash: contentHash,
1803
+ original_commit: commit.sha,
1804
+ original_message: commit.message,
1805
+ original_author: `${commit.authorName} <${commit.authorEmail}>`,
1806
+ base_generation: baseGeneration,
1807
+ files: filesOutput.trim().split("\n").filter(Boolean),
1808
+ patch_content: patchContent
1809
+ });
1810
+ }
1811
+ return patches;
1812
+ }
1813
+ function parseGitLog(log) {
1814
+ return log.trim().split("\n").filter(Boolean).map((line) => {
1815
+ const [sha, authorName, authorEmail, message] = line.split("\0");
1816
+ return { sha, authorName, authorEmail, message };
1817
+ });
1818
+ }
1819
+ var REPLAY_FERNIGNORE_ENTRIES = [".fern/replay.lock", ".fern/replay.yml"];
1820
+ function ensureFernignoreEntries(outputDir) {
1821
+ const fernignorePath = join5(outputDir, ".fernignore");
1822
+ let content = "";
1823
+ if (existsSync4(fernignorePath)) {
1824
+ content = readFileSync4(fernignorePath, "utf-8");
1825
+ }
1826
+ const lines = content.split("\n");
1827
+ const toAdd = [];
1828
+ for (const entry of REPLAY_FERNIGNORE_ENTRIES) {
1829
+ if (!lines.some((line) => line.trim() === entry)) {
1830
+ toAdd.push(entry);
1831
+ }
1832
+ }
1833
+ if (toAdd.length === 0) {
1834
+ return false;
1835
+ }
1836
+ if (content && !content.endsWith("\n")) {
1837
+ content += "\n";
1838
+ }
1839
+ content += toAdd.join("\n") + "\n";
1840
+ writeFileSync3(fernignorePath, content, "utf-8");
1841
+ return true;
1842
+ }
1843
+ function computeContentHash(patchContent) {
1844
+ const normalized = patchContent.split("\n").filter((line) => !line.startsWith("From ") && !line.startsWith("index ") && !line.startsWith("Date: ")).join("\n");
1845
+ return `sha256:${createHash3("sha256").update(normalized).digest("hex")}`;
1846
+ }
1847
+
1848
+ // src/commands/forget.ts
1849
+ import { minimatch as minimatch4 } from "minimatch";
1850
+ function forget(outputDir, filePattern, options) {
1851
+ const lockManager = new LockfileManager(outputDir);
1852
+ if (!lockManager.exists()) {
1853
+ return { removed: [], notFound: true };
1854
+ }
1855
+ const lock = lockManager.read();
1856
+ const matchingPatches = lock.patches.filter(
1857
+ (patch) => patch.files.some((file) => file === filePattern || minimatch4(file, filePattern))
1858
+ );
1859
+ if (matchingPatches.length === 0) {
1860
+ return { removed: [], notFound: true };
1861
+ }
1862
+ const removed = matchingPatches.map((p) => ({
1863
+ id: p.id,
1864
+ message: p.original_message,
1865
+ files: p.files
1866
+ }));
1867
+ if (!options?.dryRun) {
1868
+ for (const patch of matchingPatches) {
1869
+ lockManager.removePatch(patch.id);
1870
+ }
1871
+ lockManager.save();
1872
+ }
1873
+ return { removed, notFound: false };
1874
+ }
1875
+
1876
+ // src/commands/reset.ts
1877
+ import { unlinkSync } from "fs";
1878
+ function reset(outputDir, options) {
1879
+ const lockManager = new LockfileManager(outputDir);
1880
+ if (!lockManager.exists()) {
1881
+ return {
1882
+ success: true,
1883
+ patchesRemoved: 0,
1884
+ lockfileDeleted: false,
1885
+ nothingToReset: true
1886
+ };
1887
+ }
1888
+ const lock = lockManager.read();
1889
+ const patchCount = lock.patches.length;
1890
+ if (!options?.dryRun) {
1891
+ unlinkSync(lockManager.lockfilePath);
1892
+ }
1893
+ return {
1894
+ success: true,
1895
+ patchesRemoved: patchCount,
1896
+ lockfileDeleted: true,
1897
+ nothingToReset: false
1898
+ };
1899
+ }
1900
+
1901
+ // src/commands/resolve.ts
1902
+ init_GitClient();
1903
+ async function resolve(outputDir, options) {
1904
+ const lockManager = new LockfileManager(outputDir);
1905
+ if (!lockManager.exists()) {
1906
+ return { success: false, reason: "no-lockfile" };
1907
+ }
1908
+ const lock = lockManager.read();
1909
+ if (lock.patches.length === 0) {
1910
+ return { success: false, reason: "no-patches" };
1911
+ }
1912
+ const git = new GitClient(outputDir);
1913
+ if (options?.checkMarkers !== false) {
1914
+ const markerFiles = await git.exec(["grep", "-l", "<<<<<<<", "--", "."]).catch(() => "");
1915
+ if (markerFiles.trim()) {
1916
+ const files = markerFiles.trim().split("\n").filter(Boolean);
1917
+ return { success: false, reason: "unresolved-conflicts", unresolvedFiles: files };
1918
+ }
1919
+ }
1920
+ const committer = new ReplayCommitter(git, outputDir);
1921
+ await committer.stageAll();
1922
+ const commitSha = await committer.commitReplay(lock.patches.length, lock.patches);
1923
+ return { success: true, commitSha };
1924
+ }
1925
+
1926
+ // src/commands/status.ts
1927
+ function status(outputDir) {
1928
+ const lockManager = new LockfileManager(outputDir);
1929
+ if (!lockManager.exists()) {
1930
+ return { initialized: false, patches: [], lastGeneration: void 0 };
1931
+ }
1932
+ const lock = lockManager.read();
1933
+ const patches = lock.patches.map((patch) => ({
1934
+ sha: patch.original_commit.slice(0, 7),
1935
+ author: patch.original_author.split("<")[0]?.trim() ?? "unknown",
1936
+ message: patch.original_message,
1937
+ files: patch.files
1938
+ }));
1939
+ let lastGeneration;
1940
+ const lastGen = lock.generations.find((g) => g.commit_sha === lock.current_generation);
1941
+ if (lastGen) {
1942
+ lastGeneration = {
1943
+ sha: lastGen.commit_sha,
1944
+ timestamp: lastGen.timestamp
1945
+ };
1946
+ }
1947
+ return { initialized: true, patches, lastGeneration };
1948
+ }
1949
+ export {
1950
+ FERN_BOT_EMAIL,
1951
+ FERN_BOT_LOGIN,
1952
+ FERN_BOT_NAME,
1953
+ FernignoreMigrator,
1954
+ GitClient,
1955
+ LockfileManager,
1956
+ ReplayApplicator,
1957
+ ReplayCommitter,
1958
+ ReplayDetector,
1959
+ ReplayService,
1960
+ bootstrap,
1961
+ forget,
1962
+ isGenerationCommit,
1963
+ isReplayCommit,
1964
+ reset,
1965
+ resolve,
1966
+ status,
1967
+ threeWayMerge
1968
+ };
11
1969
  //# sourceMappingURL=index.js.map