-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-cherry-pick-tool.test.ts
More file actions
442 lines (397 loc) · 15.8 KB
/
git-cherry-pick-tool.test.ts
File metadata and controls
442 lines (397 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/**
* Integration tests for git_cherry_pick.
*/
import { afterEach, describe, expect, test } from "bun:test";
import { existsSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { registerGitCherryPickTool } from "./git-cherry-pick-tool.js";
import { captureTool, cleanupTmpPaths, gitCmd, makeRepoWithSeed } from "./test-harness.js";
afterEach(cleanupTmpPaths);
function makeRepo(): string {
return makeRepoWithSeed("mcp-cherry-pick-test-");
}
function createBranchWithCommits(
dir: string,
branch: string,
commits: Array<{ path: string; body: string; message: string }>,
): string[] {
gitCmd(dir, "checkout", "-b", branch);
const shas: string[] = [];
for (const c of commits) {
writeFileSync(join(dir, c.path), c.body);
gitCmd(dir, "add", c.path);
gitCmd(dir, "commit", "-m", c.message);
shas.push(gitCmd(dir, "rev-parse", "HEAD").trim());
}
gitCmd(dir, "checkout", "main");
return shas;
}
// ---------------------------------------------------------------------------
// Branch-source flow (the primary agent-worktree use case)
// ---------------------------------------------------------------------------
describe("git_cherry_pick branch sources", () => {
test("single branch source plays every new commit onto destination", async () => {
const dir = makeRepo();
createBranchWithCommits(dir, "feature/a", [
{ path: "a1.txt", body: "a1\n", message: "feat: a1" },
{ path: "a2.txt", body: "a2\n", message: "feat: a2" },
]);
const run = captureTool(registerGitCherryPickTool);
const text = await run({
workspaceRoot: dir,
format: "json",
sources: ["feature/a"],
});
const parsed = JSON.parse(text) as {
ok: boolean;
applied: number;
picked: number;
results: Array<{ source: string; kind: string; keptCommits: number }>;
};
expect(parsed.ok).toBe(true);
expect(parsed.applied).toBe(2);
expect(parsed.picked).toBe(2);
expect(parsed.results[0]?.kind).toBe("branch");
expect(parsed.results[0]?.keptCommits).toBe(2);
// Destination file check
expect(existsSync(join(dir, "a1.txt"))).toBe(true);
expect(existsSync(join(dir, "a2.txt"))).toBe(true);
});
test("multiple branch sources applied in order", async () => {
const dir = makeRepo();
createBranchWithCommits(dir, "feature/a", [{ path: "a.txt", body: "a\n", message: "feat: a" }]);
createBranchWithCommits(dir, "feature/b", [{ path: "b.txt", body: "b\n", message: "feat: b" }]);
const run = captureTool(registerGitCherryPickTool);
const text = await run({
workspaceRoot: dir,
format: "json",
sources: ["feature/a", "feature/b"],
});
const parsed = JSON.parse(text) as { ok: boolean; applied: number };
expect(parsed.ok).toBe(true);
expect(parsed.applied).toBe(2);
const log = gitCmd(dir, "log", "--oneline").trim();
// Most recent first: b, then a, then seed.
expect(log.split("\n")[0]).toContain("feat: b");
expect(log.split("\n")[1]).toContain("feat: a");
});
test("re-applying a patch-equivalent commit adds nothing (--empty=drop)", async () => {
const dir = makeRepo();
const shas = createBranchWithCommits(dir, "feature/a", [
{ path: "a.txt", body: "a\n", message: "feat: a" },
]);
const headBefore = gitCmd(dir, "rev-parse", "HEAD").trim();
// Cherry-pick once (succeeds).
const run = captureTool(registerGitCherryPickTool);
await run({ workspaceRoot: dir, format: "json", sources: [shas[0] ?? ""] });
const headAfterFirst = gitCmd(dir, "rev-parse", "HEAD").trim();
expect(headAfterFirst).not.toBe(headBefore);
// Second call with the branch source. `onto..feature/a` still lists the original
// SHA (it is not an ancestor of main — different SHA than the cherry-picked copy).
// `--empty=drop` handles the patch-equivalence at cherry-pick time, so `applied`
// is 0 and HEAD does not advance.
const text2 = await run({
workspaceRoot: dir,
format: "json",
sources: ["feature/a"],
});
const parsed = JSON.parse(text2) as {
ok: boolean;
applied: number;
picked: number;
};
expect(parsed.ok).toBe(true);
expect(parsed.applied).toBe(0);
// picked is what was fed to `git cherry-pick`; git itself drops empties.
expect(parsed.picked).toBeGreaterThanOrEqual(0);
const headAfterSecond = gitCmd(dir, "rev-parse", "HEAD").trim();
expect(headAfterSecond).toBe(headAfterFirst);
});
});
// ---------------------------------------------------------------------------
// SHA and range sources
// ---------------------------------------------------------------------------
describe("git_cherry_pick SHA and range sources", () => {
test("single SHA picks exactly that commit", async () => {
const dir = makeRepo();
const shas = createBranchWithCommits(dir, "feature/a", [
{ path: "a1.txt", body: "a1\n", message: "feat: a1" },
{ path: "a2.txt", body: "a2\n", message: "feat: a2" },
]);
const secondSha = shas[1];
expect(secondSha).toBeDefined();
const run = captureTool(registerGitCherryPickTool);
const text = await run({
workspaceRoot: dir,
format: "json",
sources: [secondSha ?? ""],
});
const parsed = JSON.parse(text) as {
ok: boolean;
applied: number;
results: Array<{ kind: string; resolvedCommits: number }>;
};
expect(parsed.ok).toBe(true);
expect(parsed.applied).toBe(1);
expect(parsed.results[0]?.kind).toBe("sha");
// Only a2.txt appears on destination (a1.txt was skipped).
expect(existsSync(join(dir, "a1.txt"))).toBe(false);
expect(existsSync(join(dir, "a2.txt"))).toBe(true);
});
test("A..B range picks all commits in range, oldest-first", async () => {
const dir = makeRepo();
const shas = createBranchWithCommits(dir, "feature/a", [
{ path: "a1.txt", body: "a1\n", message: "feat: a1" },
{ path: "a2.txt", body: "a2\n", message: "feat: a2" },
{ path: "a3.txt", body: "a3\n", message: "feat: a3" },
]);
const run = captureTool(registerGitCherryPickTool);
const text = await run({
workspaceRoot: dir,
format: "json",
sources: [`main..${"feature/a"}`],
});
const parsed = JSON.parse(text) as {
ok: boolean;
applied: number;
results: Array<{ kind: string; resolvedCommits: number }>;
};
expect(parsed.ok).toBe(true);
expect(parsed.applied).toBe(3);
expect(parsed.results[0]?.kind).toBe("range");
// All three files present.
expect(existsSync(join(dir, "a1.txt"))).toBe(true);
expect(existsSync(join(dir, "a2.txt"))).toBe(true);
expect(existsSync(join(dir, "a3.txt"))).toBe(true);
// Log order: a3 newest.
const log = gitCmd(dir, "log", "--oneline").trim().split("\n");
expect(log[0]).toContain("feat: a3");
// shas array unused beyond creation, but keep ref to satisfy no-unused warning
expect(shas.length).toBe(3);
});
test("overlap between branch and SHA sources deduplicates", async () => {
const dir = makeRepo();
const shas = createBranchWithCommits(dir, "feature/a", [
{ path: "a.txt", body: "a\n", message: "feat: a" },
]);
const sha = shas[0] ?? "";
const run = captureTool(registerGitCherryPickTool);
const text = await run({
workspaceRoot: dir,
format: "json",
sources: [sha, "feature/a"],
});
const parsed = JSON.parse(text) as {
ok: boolean;
applied: number;
results: Array<{ keptCommits: number }>;
};
expect(parsed.ok).toBe(true);
expect(parsed.applied).toBe(1); // not 2
expect(parsed.results[0]?.keptCommits).toBe(1);
expect(parsed.results[1]?.keptCommits).toBe(0);
});
});
// ---------------------------------------------------------------------------
// Conflict handling
// ---------------------------------------------------------------------------
describe("git_cherry_pick conflicts", () => {
test("conflict aborts cherry-pick and reports structured paths", async () => {
const dir = makeRepo();
// Two branches touch the same file with incompatible content.
writeFileSync(join(dir, "shared.txt"), "common\n");
gitCmd(dir, "add", "shared.txt");
gitCmd(dir, "commit", "-m", "chore: shared");
// Branch A touches shared.txt one way.
gitCmd(dir, "checkout", "-b", "feature/a");
writeFileSync(join(dir, "shared.txt"), "alpha\n");
gitCmd(dir, "add", "shared.txt");
gitCmd(dir, "commit", "-m", "feat: alpha");
gitCmd(dir, "checkout", "main");
// Main advances on shared.txt differently.
writeFileSync(join(dir, "shared.txt"), "beta\n");
gitCmd(dir, "add", "shared.txt");
gitCmd(dir, "commit", "-m", "chore: beta");
const run = captureTool(registerGitCherryPickTool);
const text = await run({
workspaceRoot: dir,
format: "json",
sources: ["feature/a"],
});
const parsed = JSON.parse(text) as {
ok: boolean;
applied: number;
conflict?: { stage: string; paths: string[] };
};
expect(parsed.ok).toBe(false);
expect(parsed.applied).toBe(0);
expect(parsed.conflict?.stage).toBe("cherry-pick");
expect(parsed.conflict?.paths).toContain("shared.txt");
// Repo state is clean (cherry-pick aborted).
const status = gitCmd(dir, "status", "--porcelain").trim();
expect(status).toBe("");
});
});
// ---------------------------------------------------------------------------
// Cleanup flags
// ---------------------------------------------------------------------------
describe("git_cherry_pick cleanup", () => {
test("deleteMergedBranches deletes branch via patch-id after cherry-pick", async () => {
const dir = makeRepo();
createBranchWithCommits(dir, "feature/a", [{ path: "a.txt", body: "a\n", message: "feat: a" }]);
const run = captureTool(registerGitCherryPickTool);
const text = await run({
workspaceRoot: dir,
format: "json",
sources: ["feature/a"],
deleteMergedBranches: true,
});
const parsed = JSON.parse(text) as {
ok: boolean;
results: Array<{ branchDeleted?: boolean }>;
};
expect(parsed.ok).toBe(true);
// After cherry-pick the SHA differs (different committer date), so git branch -d
// would refuse. Patch-id comparison detects content equivalence → branch deleted.
expect(parsed.results[0]?.branchDeleted).toBe(true);
const branches = gitCmd(dir, "branch").trim();
expect(branches).not.toContain("feature/a");
});
test("deleteMergedBranches skips protected 'dev' name even if merged", async () => {
const dir = makeRepo();
// Create and merge a dev branch forward into main via fast-forward.
gitCmd(dir, "checkout", "-b", "dev");
writeFileSync(join(dir, "d.txt"), "d\n");
gitCmd(dir, "add", "d.txt");
gitCmd(dir, "commit", "-m", "feat: d");
gitCmd(dir, "checkout", "main");
// Fast-forward main to dev to make dev fully merged.
gitCmd(dir, "merge", "--ff-only", "dev");
const run = captureTool(registerGitCherryPickTool);
const text = await run({
workspaceRoot: dir,
format: "json",
sources: ["dev"],
deleteMergedBranches: true,
});
const parsed = JSON.parse(text) as {
ok: boolean;
results: Array<{ branchDeleted?: boolean }>;
};
expect(parsed.ok).toBe(true);
expect(parsed.results[0]?.branchDeleted).toBeUndefined();
const branches = gitCmd(dir, "branch").trim();
expect(branches).toContain("dev");
});
test("single branch source cherry-pick (markdown)", async () => {
const dir = makeRepo();
createBranchWithCommits(dir, "feature/cp", [
{ path: "cp.txt", body: "cp\n", message: "feat: cp" },
]);
const run = captureTool(registerGitCherryPickTool);
const text = await run({ workspaceRoot: dir, sources: ["feature/cp"] });
expect(text).toContain("# Cherry-pick onto `main`");
expect(text).toContain("feature/cp");
expect(text).toContain("branch");
expect(text).toContain("picked");
});
});
// ---------------------------------------------------------------------------
// Guardrails
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// Patch-id cleanup (deleteMergedBranches after cherry-pick)
// ---------------------------------------------------------------------------
describe("git_cherry_pick patch-id branch deletion", () => {
test("deleteMergedBranches deletes branch after cherry-pick despite SHA mismatch", async () => {
const dir = makeRepo();
// Create feature branch from seed
createBranchWithCommits(dir, "feature/cp", [
{ path: "cp.txt", body: "content\n", message: "feat: cherry-pick me" },
]);
// Add an unrelated commit to main so cherry-pick will have a different parent → different SHA
writeFileSync(join(dir, "unrelated.txt"), "extra\n");
gitCmd(dir, "add", "unrelated.txt");
gitCmd(dir, "commit", "-m", "chore: unrelated on main");
const run = captureTool(registerGitCherryPickTool);
const text = await run({
workspaceRoot: dir,
format: "json",
sources: ["feature/cp"],
deleteMergedBranches: true,
});
const parsed = JSON.parse(text) as {
ok: boolean;
results: Array<{ source: string; branchDeleted?: boolean }>;
};
expect(parsed.ok).toBe(true);
expect(parsed.results[0]?.branchDeleted).toBe(true);
// Branch should be gone
const branches = gitCmd(dir, "branch");
expect(branches).not.toContain("feature/cp");
});
test("strictMergedRefEquality: true skips deletion after cherry-pick (SHA mismatch)", async () => {
const dir = makeRepo();
createBranchWithCommits(dir, "feature/strict", [
{ path: "strict.txt", body: "strict\n", message: "feat: strict" },
]);
// Add unrelated commit so parent differs → different SHA after cherry-pick
writeFileSync(join(dir, "unrelated2.txt"), "extra\n");
gitCmd(dir, "add", "unrelated2.txt");
gitCmd(dir, "commit", "-m", "chore: diverge main");
const run = captureTool(registerGitCherryPickTool);
const text = await run({
workspaceRoot: dir,
format: "json",
sources: ["feature/strict"],
deleteMergedBranches: true,
strictMergedRefEquality: true,
});
const parsed = JSON.parse(text) as {
ok: boolean;
results: Array<{ source: string; branchDeleted?: boolean }>;
};
expect(parsed.ok).toBe(true);
// Branch NOT deleted because SHA differs
expect(parsed.results[0]?.branchDeleted).toBeUndefined();
const branches = gitCmd(dir, "branch");
expect(branches).toContain("feature/strict");
});
});
describe("git_cherry_pick guardrails", () => {
test("working_tree_dirty refuses unstaged changes", async () => {
const dir = makeRepo();
createBranchWithCommits(dir, "feature/a", [{ path: "a.txt", body: "a\n", message: "feat: a" }]);
writeFileSync(join(dir, "seed.txt"), "mutated\n");
const run = captureTool(registerGitCherryPickTool);
const text = await run({
workspaceRoot: dir,
format: "json",
sources: ["feature/a"],
});
const parsed = JSON.parse(text) as { error: string };
expect(parsed.error).toBe("working_tree_dirty");
});
test("unknown source returns source_not_found", async () => {
const dir = makeRepo();
const run = captureTool(registerGitCherryPickTool);
const text = await run({
workspaceRoot: dir,
format: "json",
sources: ["does-not-exist"],
});
const parsed = JSON.parse(text) as { error: string };
expect(parsed.error).toBe("source_not_found");
});
test("unsafe ref token rejected", async () => {
const dir = makeRepo();
const run = captureTool(registerGitCherryPickTool);
const text = await run({
workspaceRoot: dir,
format: "json",
sources: ["; rm -rf /"],
});
const parsed = JSON.parse(text) as { error: string };
expect(parsed.error).toBe("unsafe_ref_token");
});
});