Repository navigation
Expand file tree
/
Copy pathgit-worktree.ts
More file actions
1092 lines (967 loc) · 32.2 KB
/
Copy pathgit-worktree.ts
File metadata and controls
1092 lines (967 loc) · 32.2 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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Git Worktree Plugin for OpenCode
*
* Manages git worktrees for concurrent branch development by delegating
* lifecycle operations (create/remove/reset) to OpenCode's native Worktree API
* and providing merge, list, prune, and status operations locally.
*
* IMPORTANT: Agents should ALWAYS use this tool for any git worktree operations.
* Direct `git worktree` commands are blocked and will error.
*
* ## Architecture
*
* - **create/remove/reset**: Delegated to OpenCode's upstream API
* (`POST/DELETE /experimental/worktree`, `POST /experimental/worktree/reset`)
* which handles branch management, bootstrap, sandbox registration, and
* bus event emission (worktree.ready / worktree.failed).
* - **merge/list/prune/status**: Handled locally via git commands.
* - **status**: Queries session status (`GET /session/status`) and worktree
* list to show which sessions are active and which worktrees exist.
*
* ## When to Use
*
* - User requests worktree operations (create, list, merge)
* - Running subagents that make concurrent changes to the same repository
* - Performing parallel tasks that modify files in the repo
* - Isolating branch work without affecting the main worktree
* - Reviewing/testing code from another branch while preserving current work
*
* ## Concurrent Subagent Pattern
*
* When spawning multiple subagents to work on the same repo:
* 1. Create a worktree for each subagent
* 2. Each subagent works in its isolated worktree directory
* 3. Merge results back to the target branch when complete
*
* ## Merge Strategies
*
* - "ours": Keep changes from the target branch on conflict
* - "theirs": Keep changes from the worktree branch on conflict
* - "manual": Stop on conflict and return diff for user decision (aborts merge to keep repo clean)
*/
import { type Plugin, tool } from "@opencode-ai/plugin";
const PLUGIN_NAME = "git-worktree";
interface WorktreeResult {
success: boolean;
message: string;
path?: string;
branch?: string;
conflicts?: string[];
diff?: string;
worktrees?: string[];
sessions?: Record<string, { type: string }>;
}
/**
* Validates branch name for git safety.
* Throws if the name contains unsafe characters.
*/
function validateBranchName(branch: string): void {
if (
/[\s;&|`$(){}[\]<>\\'"!*?~^]/.test(branch) ||
branch.startsWith("-") ||
branch.includes("..") ||
branch.endsWith(".lock") ||
/[\x00-\x1f\x7f]/.test(branch)
) {
throw new Error(`Invalid branch name: contains unsafe characters`);
}
}
/**
* Extracts stderr as string from Bun shell result.
*/
function getStderr(result: { stderr: Uint8Array }): string {
return new TextDecoder().decode(result.stderr).trim();
}
/**
* Extracts stdout as string from Bun shell result.
*/
function getStdout(result: { stdout: Uint8Array }): string {
return new TextDecoder().decode(result.stdout).trim();
}
// Logging helper - uses `any` to avoid SDK type complexity
async function log(
client: any,
level: "info" | "error" | "warn" | "debug",
message: string,
extra?: Record<string, unknown>,
): Promise<void> {
await client.app
.log({
service: PLUGIN_NAME,
level,
message,
extra,
})
.catch(() => {
// Logging failures should not break operations
});
}
export const GitWorktreePlugin: Plugin = async (ctx) => {
const { client, directory, worktree, serverUrl, $ } = ctx;
const repoRoot = worktree || directory;
/**
* Calls the OpenCode server API.
* Uses the plugin's serverUrl to hit the local OpenCode server directly.
*/
async function serverFetch(
path: string,
options: RequestInit = {},
): Promise<Response> {
const url = new URL(path, serverUrl);
url.searchParams.set("directory", directory);
const headers: Record<string, string> = {};
if (options.body) {
headers["Content-Type"] = "application/json";
}
return fetch(url.toString(), {
...options,
headers: {
...headers,
...(options.headers as Record<string, string>),
},
});
}
/**
* Creates a worktree via the OpenCode upstream API.
* POST /experimental/worktree
*/
async function createWorktreeViaAPI(
sessionID: string,
name?: string,
startCommand?: string,
): Promise<WorktreeResult> {
await log(client, "info", "Creating worktree via upstream API", {
sessionID,
name,
startCommand: startCommand ?? "none",
});
try {
const body: Record<string, string> = {};
if (name) body.name = name;
if (startCommand) body.startCommand = startCommand;
const response = await serverFetch("/experimental/worktree", {
method: "POST",
body: JSON.stringify(body),
});
if (!response.ok) {
const errorBody = await response.text();
await log(client, "error", "Upstream worktree create failed", {
sessionID,
status: response.status,
error: errorBody,
});
return {
success: false,
message: `Failed to create worktree: ${errorBody}`,
};
}
const info = (await response.json()) as {
name: string;
branch: string;
directory: string;
};
await log(client, "info", "Worktree created via upstream API", {
sessionID,
name: info.name,
branch: info.branch,
directory: info.directory,
});
return {
success: true,
message: `Created worktree '${info.name}' with branch '${info.branch}'`,
path: info.directory,
branch: info.branch,
};
} catch (error) {
const message =
error instanceof Error ? error.message : String(error);
await log(client, "error", "Worktree create error", {
sessionID,
error: message,
});
return {
success: false,
message: `Failed to create worktree: ${message}`,
};
}
}
/**
* Removes a worktree via the OpenCode upstream API.
* DELETE /experimental/worktree
*/
async function removeWorktreeViaAPI(
sessionID: string,
name?: string,
): Promise<WorktreeResult> {
if (!name) {
return {
success: false,
message: "Worktree name is required for remove action",
};
}
// Find the worktree directory by name from git
const worktreeDir = await findWorktreeDirectory(name);
if (!worktreeDir) {
return {
success: false,
message: `No worktree found with name '${name}'. Use 'list' action to see available worktrees.`,
};
}
// Don't allow removing the main worktree
if (worktreeDir === repoRoot) {
return {
success: false,
message: "Cannot remove the main worktree",
};
}
await log(client, "info", "Removing worktree via upstream API", {
sessionID,
name,
directory: worktreeDir,
});
try {
const response = await serverFetch("/experimental/worktree", {
method: "DELETE",
body: JSON.stringify({ directory: worktreeDir }),
});
if (!response.ok) {
const errorBody = await response.text();
await log(client, "error", "Upstream worktree remove failed", {
sessionID,
status: response.status,
error: errorBody,
});
return {
success: false,
message: `Failed to remove worktree: ${errorBody}`,
};
}
await log(client, "info", "Worktree removed via upstream API", {
sessionID,
name,
});
return {
success: true,
message: `Removed worktree '${name}' and cleaned up branch`,
};
} catch (error) {
const message =
error instanceof Error ? error.message : String(error);
await log(client, "error", "Worktree remove error", {
sessionID,
error: message,
});
return {
success: false,
message: `Failed to remove worktree: ${message}`,
};
}
}
/**
* Resets a worktree to the default branch via the OpenCode upstream API.
* POST /experimental/worktree/reset
*/
async function resetWorktreeViaAPI(
sessionID: string,
name?: string,
): Promise<WorktreeResult> {
if (!name) {
return {
success: false,
message: "Worktree name is required for reset action",
};
}
const worktreeDir = await findWorktreeDirectory(name);
if (!worktreeDir) {
return {
success: false,
message: `No worktree found with name '${name}'. Use 'list' action to see available worktrees.`,
};
}
await log(client, "info", "Resetting worktree via upstream API", {
sessionID,
name,
directory: worktreeDir,
});
try {
const response = await serverFetch("/experimental/worktree/reset", {
method: "POST",
body: JSON.stringify({ directory: worktreeDir }),
});
if (!response.ok) {
const errorBody = await response.text();
await log(client, "error", "Upstream worktree reset failed", {
sessionID,
status: response.status,
error: errorBody,
});
return {
success: false,
message: `Failed to reset worktree: ${errorBody}`,
};
}
await log(client, "info", "Worktree reset via upstream API", {
sessionID,
name,
});
return {
success: true,
message: `Reset worktree '${name}' to default branch`,
path: worktreeDir,
};
} catch (error) {
const message =
error instanceof Error ? error.message : String(error);
await log(client, "error", "Worktree reset error", {
sessionID,
error: message,
});
return {
success: false,
message: `Failed to reset worktree: ${message}`,
};
}
}
/**
* Finds a worktree directory by name using git worktree list.
*/
async function findWorktreeDirectory(
name: string,
): Promise<string | undefined> {
const result = await $`git worktree list --porcelain`
.quiet()
.nothrow()
.cwd(repoRoot);
if (result.exitCode !== 0) return undefined;
const stdout = getStdout(result);
for (const line of stdout.split("\n")) {
if (line.startsWith("worktree ")) {
const dir = line.slice(9);
const dirName = dir.split("/").pop();
if (dirName === name) {
return dir;
}
}
}
return undefined;
}
/**
* Finds the branch name for a worktree directory from git worktree list --porcelain.
*/
async function findWorktreeBranch(
worktreeDir: string,
): Promise<string | undefined> {
const result = await $`git worktree list --porcelain`
.quiet()
.nothrow()
.cwd(repoRoot);
if (result.exitCode !== 0) return undefined;
const stdout = getStdout(result);
const lines = stdout.split("\n");
let currentPath: string | undefined;
for (const line of lines) {
if (line.startsWith("worktree ")) {
currentPath = line.slice(9);
} else if (line.startsWith("branch ") && currentPath === worktreeDir) {
return line.slice(7).replace(/^refs\/heads\//, "");
} else if (line === "") {
currentPath = undefined;
}
}
return undefined;
}
/**
* Queries session status from the OpenCode server.
* GET /session/status
*/
async function getSessionStatus(): Promise<
Record<string, { type: string }> | undefined
> {
try {
const response = await serverFetch("/session/status");
if (!response.ok) return undefined;
return (await response.json()) as Record<string, { type: string }>;
} catch {
return undefined;
}
}
/**
* Lists worktree sandboxes from the OpenCode server.
* GET /experimental/worktree
*/
async function getWorktreeSandboxes(): Promise<string[] | undefined> {
try {
const response = await serverFetch("/experimental/worktree");
if (!response.ok) return undefined;
return (await response.json()) as string[];
} catch {
return undefined;
}
}
return {
"tool.execute.before": async (input, output) => {
// Intercept direct `git worktree` commands
if (input.tool === "bash" && typeof output.args?.command === "string") {
const cmd = output.args.command;
if (/(?:^|[\s\/])git\s+worktree\b/.test(cmd)) {
throw new Error(
`Direct 'git worktree' commands are not allowed. ` +
`Use the 'use-git-worktree' tool instead for managed worktree operations. ` +
`Available actions: create, list, merge, remove, reset, prune, status.`,
);
}
}
},
tool: {
"use-git-worktree": tool({
description: `REQUIRED: Use this tool for ALL git worktree operations. Do NOT run \`git worktree\` commands directly via bash.
## When to use this tool
**Always use this tool when:**
- The user asks to create, list, or manage git worktrees
- The user mentions "worktree", "worktrees", or wants isolated branch work
- You need to work on a branch without affecting the current working directory
- Running subagents that make concurrent changes to the same repository (each subagent should use its own worktree to avoid conflicts)
- Performing parallel tasks that modify files (e.g., multiple refactors, concurrent feature work)
- Testing or reviewing code from another branch while preserving current uncommitted changes
- You want to check if other sessions are active before starting parallel work
**Concurrent/parallel work pattern:** When spawning multiple subagents to work on the same repo simultaneously, create a worktree for each subagent. This prevents git conflicts and allows each agent to work independently. Merge results back when complete.
## Actions
- **create**: Create a new git worktree via the OpenCode API (handles branch, bootstrap, and sandbox registration)
- **list**: List all project worktrees with branch and session info
- **merge**: Merge worktree changes back to a target branch (done inside the worktree to avoid disrupting the main working directory)
- **remove**: Remove a worktree via the OpenCode API (handles branch cleanup and sandbox deregistration)
- **reset**: Reset a worktree branch to the default branch (e.g., main/master) via the OpenCode API
- **prune**: Remove stale worktree entries (where the directory no longer exists)
- **status**: Show active sessions and registered worktrees to understand concurrent activity
## Merge Strategies
When merging, use the \`mergeStrategy\` parameter:
- **ours**: On conflict, keep changes from the target branch
- **theirs**: On conflict, keep changes from the worktree branch
- **manual**: Stop on conflict and return diff for user to decide (merge is aborted to keep repo clean)
## Example Workflow
1. Status: action "status" to check for active sessions
2. Create: action "create", name "feature-work"
3. Work in the returned worktree directory
4. Merge: action "merge", name "feature-work", targetBranch "main", mergeStrategy "theirs"
5. Remove: action "remove", name "feature-work"`,
args: {
action: tool.schema
.enum(["create", "list", "merge", "remove", "reset", "prune", "status"])
.describe("The git worktree action to perform"),
name: tool.schema
.string()
.optional()
.describe(
"Worktree name for create/merge/remove/reset actions (auto-generated if not provided for create)",
),
targetBranch: tool.schema
.string()
.optional()
.describe("Target branch to merge into (for merge action)"),
mergeStrategy: tool.schema
.enum(["ours", "theirs", "manual"])
.optional()
.describe(
"Conflict resolution strategy: 'ours' (keep target), 'theirs' (keep worktree), 'manual' (return diff, aborts merge)",
),
startCommand: tool.schema
.string()
.optional()
.describe(
"Optional shell command to run after creating the worktree (e.g., 'npm install')",
),
commitMessage: tool.schema
.string()
.optional()
.describe("Commit message for merge commit"),
},
async execute(args, toolCtx): Promise<string> {
const { sessionID } = toolCtx;
let result: WorktreeResult;
try {
switch (args.action) {
case "create":
result = await createWorktreeViaAPI(
sessionID,
args.name,
args.startCommand,
);
break;
case "list":
result = await listWorktrees($, repoRoot, getSessionStatus);
break;
case "merge":
result = await mergeWorktree(
client,
$,
findWorktreeDirectory,
findWorktreeBranch,
repoRoot,
sessionID,
args.name,
args.targetBranch,
args.mergeStrategy,
args.commitMessage,
);
break;
case "remove":
result = await removeWorktreeViaAPI(sessionID, args.name);
break;
case "reset":
result = await resetWorktreeViaAPI(sessionID, args.name);
break;
case "prune":
result = await pruneWorktrees(client, $, repoRoot, sessionID);
break;
case "status":
result = await getStatus(
getSessionStatus,
getWorktreeSandboxes,
$,
repoRoot,
);
break;
default: {
const _exhaustive: never = args.action;
result = {
success: false,
message: `Unknown action: ${_exhaustive}`,
};
}
}
} catch (error) {
const message =
error instanceof Error ? error.message : String(error);
result = {
success: false,
message: `Unexpected error: ${message}`,
};
await log(client, "error", "Tool execution error", {
error: message,
sessionID,
});
}
return formatResult(result);
},
}),
},
};
};
export default {
id: PLUGIN_NAME,
server: GitWorktreePlugin,
};
/**
* Gets combined status of sessions and worktrees.
*/
async function getStatus(
getSessionStatus: () => Promise<Record<string, { type: string }> | undefined>,
getWorktreeSandboxes: () => Promise<string[] | undefined>,
$: any,
repoRoot: string,
): Promise<WorktreeResult> {
const [sessions, sandboxes, gitWorktrees] = await Promise.all([
getSessionStatus(),
getWorktreeSandboxes(),
(async () => {
const result = await $`git worktree list --porcelain`
.quiet()
.nothrow()
.cwd(repoRoot);
if (result.exitCode !== 0) return [];
const stdout = getStdout(result);
const entries: { path: string; branch?: string }[] = [];
let current: { path: string; branch?: string } | undefined;
for (const line of stdout.split("\n")) {
if (line.startsWith("worktree ")) {
current = { path: line.slice(9) };
entries.push(current);
} else if (line.startsWith("branch ") && current) {
current.branch = line.slice(7).replace(/^refs\/heads\//, "");
} else if (line === "") {
current = undefined;
}
}
return entries;
})(),
]);
let message = "## Session Status\n\n";
if (sessions && Object.keys(sessions).length > 0) {
const busySessions = Object.entries(sessions).filter(
([, s]) => s.type === "busy",
);
message += `Active sessions: ${Object.keys(sessions).length} (${busySessions.length} busy)\n`;
for (const [id, status] of Object.entries(sessions)) {
message += ` - ${id}: ${status.type}\n`;
}
} else {
message += "No active sessions\n";
}
message += "\n## Git Worktrees\n\n";
if (gitWorktrees.length > 0) {
message += `Worktrees: ${gitWorktrees.length}\n`;
for (const wt of gitWorktrees) {
const dirName = wt.path.split("/").pop();
message += ` - ${dirName}: ${wt.path}`;
if (wt.branch) message += ` (branch: ${wt.branch})`;
message += "\n";
}
} else {
message += "No worktrees\n";
}
if (sandboxes && sandboxes.length > 0) {
message += "\n## Registered Sandboxes\n\n";
for (const sb of sandboxes) {
message += ` - ${sb}\n`;
}
}
return {
success: true,
message,
sessions,
};
}
/**
* Prunes stale worktree entries (where the directory no longer exists).
*/
async function pruneWorktrees(
client: any,
$: any,
repoRoot: string,
sessionID: string,
): Promise<WorktreeResult> {
await log(client, "info", "Pruning stale worktrees", { sessionID });
const result = await $`git worktree prune -v`.quiet().nothrow().cwd(repoRoot);
if (result.exitCode !== 0) {
const stderr = getStderr(result);
await log(client, "error", "Failed to prune worktrees", {
sessionID,
error: stderr,
});
return {
success: false,
message: `Failed to prune worktrees: ${stderr}`,
};
}
const stdout = getStdout(result);
const prunedEntries = stdout
.split("\n")
.filter((line) => line.startsWith("Removing"))
.map((line) => line.replace(/^Removing\s+/, "").replace(/:.*$/, ""));
await log(client, "info", "Worktrees pruned", {
sessionID,
pruned: prunedEntries.length,
});
if (prunedEntries.length === 0) {
return {
success: true,
message: "No stale worktree entries to prune",
};
}
return {
success: true,
message: `Pruned ${prunedEntries.length} stale worktree entries:\n${prunedEntries.map((e) => ` - ${e}`).join("\n")}`,
};
}
/**
* Lists all git worktrees in the repository, augmented with session status.
*/
async function listWorktrees(
$: any,
repoRoot: string,
getSessionStatus: () => Promise<Record<string, { type: string }> | undefined>,
): Promise<WorktreeResult> {
const [gitResult, sessions] = await Promise.all([
$`git worktree list --porcelain`.quiet().nothrow().cwd(repoRoot),
getSessionStatus(),
]);
if (gitResult.exitCode !== 0) {
return {
success: false,
message: `Failed to list worktrees: ${getStderr(gitResult)}`,
};
}
const stdout = getStdout(gitResult);
const entries: { path: string; branch?: string }[] = [];
let current: { path: string; branch?: string } | undefined;
for (const line of stdout.split("\n")) {
if (line.startsWith("worktree ")) {
current = { path: line.slice(9) };
entries.push(current);
} else if (line.startsWith("branch ") && current) {
current.branch = line.slice(7).replace(/^refs\/heads\//, "");
} else if (line === "") {
current = undefined;
}
}
let message = `Repository worktrees: ${entries.length}\n`;
if (entries.length > 0) {
message += "\nWorktrees:\n";
for (const entry of entries) {
const dirName = entry.path.split("/").pop();
message += ` - ${dirName}: ${entry.path}`;
if (entry.branch) message += ` (branch: ${entry.branch})`;
message += "\n";
}
}
if (sessions && Object.keys(sessions).length > 0) {
const busySessions = Object.entries(sessions).filter(
([, s]) => s.type === "busy",
);
message += `\nActive sessions: ${Object.keys(sessions).length} (${busySessions.length} busy)`;
}
message += `\nRaw output:\n${stdout}`;
return {
success: true,
message,
worktrees: entries.map((e) => e.path),
};
}
/**
* Merges a worktree branch into a target branch.
*
* IMPORTANT: This performs the merge INSIDE the worktree directory to avoid
* disrupting the main working directory. The worktree fetches the target branch
* and merges locally, then the result is available on the worktree's branch.
*
* If the caller wants the merge result on the target branch in the main worktree,
* they should fast-forward the target branch after this operation.
*/
async function mergeWorktree(
client: any,
$: any,
findWorktreeDirectory: (name: string) => Promise<string | undefined>,
findWorktreeBranch: (worktreeDir: string) => Promise<string | undefined>,
repoRoot: string,
sessionID: string,
name?: string,
targetBranch?: string,
mergeStrategy?: "ours" | "theirs" | "manual",
commitMessage?: string,
): Promise<WorktreeResult> {
if (!name) {
return {
success: false,
message: "Worktree name is required for merge action",
};
}
if (!targetBranch) {
return {
success: false,
message:
"Target branch is required for merge action. Specify which branch to merge into.",
};
}
// Find the worktree
const worktreeDir = await findWorktreeDirectory(name);
if (!worktreeDir) {
return {
success: false,
message: `No worktree found with name '${name}'. Use 'list' action to see available worktrees.`,
};
}
const branch = await findWorktreeBranch(worktreeDir);
if (!branch) {
return {
success: false,
message: `Could not determine branch for worktree '${name}'.`,
};
}
try {
validateBranchName(branch);
validateBranchName(targetBranch);
} catch (error) {
return {
success: false,
message: error instanceof Error ? error.message : String(error),
};
}
const strategy = mergeStrategy || "manual";
const message =
commitMessage || `Merge branch '${branch}' into ${targetBranch}`;
// Check if target branch exists
const branchCheck =
await $`git show-ref --verify --quiet refs/heads/${targetBranch}`
.quiet()
.nothrow()
.cwd(repoRoot);
if (branchCheck.exitCode !== 0) {
return {
success: false,
message: `Target branch '${targetBranch}' does not exist`,
};
}
// Guard: if targetBranch is checked out in the main worktree, update-ref
// would desync the working tree (index/working dir won't match HEAD).
// Check this BEFORE doing the merge to avoid leaving the worktree in
// a partially-merged state we can't propagate.
const mainBranchResult = await $`git symbolic-ref --short HEAD`
.quiet()
.nothrow()
.cwd(repoRoot);
if (
mainBranchResult.exitCode === 0 &&
getStdout(mainBranchResult) === targetBranch
) {
return {
success: false,
message: `Target branch '${targetBranch}' is currently checked out in the main worktree. ` +
`Updating its ref would desync the working directory. ` +
`Check out a different branch in the main worktree first, or use a different target branch.`,
branch,
};
}
await log(client, "info", "Merging worktree", {
sessionID,
name,
branch,
targetBranch,
strategy,
});
// ---- Merge inside the worktree to avoid disrupting the main working directory ----
//
// Strategy: merge the target branch INTO the worktree branch (inside the worktree),
// then update the target branch ref to point at the merge result.
// This avoids checking out any branch in the main worktree.
// First, ensure all changes in the worktree are committed
const statusCheck = await $`git status --porcelain`
.quiet()
.nothrow()
.cwd(worktreeDir);
if (statusCheck.exitCode === 0 && getStdout(statusCheck)) {
return {
success: false,
message: `Worktree '${name}' has uncommitted changes. Commit or stash them before merging.\n\nUncommitted files:\n${getStdout(statusCheck)}`,
};
}
// Merge the target branch into the worktree branch (inside the worktree)
let mergeResult: { exitCode: number; stdout: Uint8Array; stderr: Uint8Array };
if (strategy === "ours") {
// "ours" means keep the target branch's changes on conflict
// Since we're merging target INTO worktree, "theirs" in git terms = target's changes
mergeResult = await $`git merge -X theirs -m ${message} ${targetBranch}`
.quiet()
.nothrow()
.cwd(worktreeDir);
} else if (strategy === "theirs") {
// "theirs" means keep the worktree branch's changes on conflict
// Since we're merging target INTO worktree, "ours" in git terms = worktree's changes
mergeResult = await $`git merge -X ours -m ${message} ${targetBranch}`
.quiet()
.nothrow()
.cwd(worktreeDir);
} else {
// Manual - try merge without auto-commit
mergeResult = await $`git merge --no-commit --no-ff ${targetBranch}`
.quiet()
.nothrow()
.cwd(worktreeDir);
}
const mergeStdout = getStdout(mergeResult);
const mergeStderr = getStderr(mergeResult);
// Check for conflicts
if (mergeResult.exitCode !== 0) {
if (mergeStderr.includes("CONFLICT") || mergeStdout.includes("CONFLICT")) {
// Get diff and status for context (inside the worktree)
const diffResult = await $`git diff`.quiet().nothrow().cwd(worktreeDir);
const statusResult = await $`git status --porcelain`
.quiet()
.nothrow()
.cwd(worktreeDir);
// Extract conflicted files
const conflicts = getStdout(statusResult)
.split("\n")
.filter((line) => /^(UU|AA|AU|UA|DU|UD|DD)/.test(line))
.map((line) => line.slice(3));
// Abort merge to keep worktree clean
await $`git merge --abort`.quiet().nothrow().cwd(worktreeDir);
if (strategy === "manual") {
return {
success: false,
message: `Merge has conflicts that require resolution. The merge was aborted to keep the worktree clean.\n\nConflicted files:\n${conflicts.join("\n")}\n\nUse mergeStrategy 'ours' or 'theirs' to auto-resolve, or resolve conflicts manually in the worktree.`,
conflicts,
diff: getStdout(diffResult),
};
}
return {
success: false,
message: `Merge failed with conflicts that couldn't be auto-resolved: ${mergeStderr}`,
conflicts,
};
}