@algochad/archcoder 2.0.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 (157) hide show
  1. package/README.md +113 -0
  2. package/bin/cli-entry.js +55 -0
  3. package/bin/cli-output.js +145 -0
  4. package/bin/cli.js +5108 -0
  5. package/bin/cli.test.js +56 -0
  6. package/dist/apple-touch-icon-120x120.png +0 -0
  7. package/dist/apple-touch-icon-152x152.png +0 -0
  8. package/dist/apple-touch-icon-167x167.png +0 -0
  9. package/dist/apple-touch-icon-180x180.png +0 -0
  10. package/dist/apple-touch-icon.png +0 -0
  11. package/dist/apple-touch-icon.svg +67 -0
  12. package/dist/assets/MultiRunWindow-BZp3MjJP.js +1 -0
  13. package/dist/assets/SettingsWindow-DoGYXpX7.js +1 -0
  14. package/dist/assets/TerminalView-BN7BR5Ff.js +3 -0
  15. package/dist/assets/TimelineDialog-ZQ33oVQR.js +1 -0
  16. package/dist/assets/ToolOutputDialog-Blv3pnug.js +16 -0
  17. package/dist/assets/ibm-plex-mono-latin-400-normal-CvHOgSBP.woff +0 -0
  18. package/dist/assets/ibm-plex-mono-latin-400-normal-DMJ8VG8y.woff2 +0 -0
  19. package/dist/assets/ibm-plex-mono-latin-500-normal-CB9ihrfo.woff +0 -0
  20. package/dist/assets/ibm-plex-mono-latin-500-normal-DSY6xOcd.woff2 +0 -0
  21. package/dist/assets/ibm-plex-mono-latin-600-normal-BgSNZQsw.woff2 +0 -0
  22. package/dist/assets/ibm-plex-mono-latin-600-normal-DWFSQ4vo.woff +0 -0
  23. package/dist/assets/ibm-plex-sans-latin-400-normal-CDDApCn2.woff2 +0 -0
  24. package/dist/assets/ibm-plex-sans-latin-400-normal-CYLoc0-x.woff +0 -0
  25. package/dist/assets/ibm-plex-sans-latin-500-normal-6ng42L7E.woff2 +0 -0
  26. package/dist/assets/ibm-plex-sans-latin-500-normal-BgVn5rGT.woff +0 -0
  27. package/dist/assets/ibm-plex-sans-latin-600-normal-Cu4Hd6ag.woff +0 -0
  28. package/dist/assets/ibm-plex-sans-latin-600-normal-CuJfVYMP.woff2 +0 -0
  29. package/dist/assets/index-CtCEGYrr.css +1 -0
  30. package/dist/assets/index-o_d2wtWC.js +48 -0
  31. package/dist/assets/main-5QGBtzdq.css +1 -0
  32. package/dist/assets/main-B6oiMU86.js +8033 -0
  33. package/dist/assets/vendor--DbVqbJpV.css +1 -0
  34. package/dist/assets/vendor-.bun-HTKwyaEM.js +10086 -0
  35. package/dist/assets/wasm-CG6Dc4jp.js +1 -0
  36. package/dist/assets/worker-bqd4RMrj.js +155 -0
  37. package/dist/favicon-16.png +0 -0
  38. package/dist/favicon-32.png +0 -0
  39. package/dist/favicon.png +0 -0
  40. package/dist/favicon.svg +67 -0
  41. package/dist/index.html +533 -0
  42. package/dist/logo-dark-192x192.png +0 -0
  43. package/dist/logo-dark-512x512.svg +16 -0
  44. package/dist/logo-light-192x192.png +0 -0
  45. package/dist/logo-light-512x512.svg +16 -0
  46. package/dist/pwa-192.png +0 -0
  47. package/dist/pwa-512.png +0 -0
  48. package/dist/pwa-maskable-192.png +0 -0
  49. package/dist/pwa-maskable-512.png +0 -0
  50. package/dist/site.webmanifest +22 -0
  51. package/dist/sw.js +1 -0
  52. package/package.json +107 -0
  53. package/public/apple-touch-icon-120x120.png +0 -0
  54. package/public/apple-touch-icon-152x152.png +0 -0
  55. package/public/apple-touch-icon-167x167.png +0 -0
  56. package/public/apple-touch-icon-180x180.png +0 -0
  57. package/public/apple-touch-icon.png +0 -0
  58. package/public/apple-touch-icon.svg +67 -0
  59. package/public/favicon-16.png +0 -0
  60. package/public/favicon-32.png +0 -0
  61. package/public/favicon.png +0 -0
  62. package/public/favicon.svg +67 -0
  63. package/public/logo-dark-192x192.png +0 -0
  64. package/public/logo-dark-512x512.svg +16 -0
  65. package/public/logo-light-192x192.png +0 -0
  66. package/public/logo-light-512x512.svg +16 -0
  67. package/public/pwa-192.png +0 -0
  68. package/public/pwa-512.png +0 -0
  69. package/public/pwa-maskable-192.png +0 -0
  70. package/public/pwa-maskable-512.png +0 -0
  71. package/public/site.webmanifest +22 -0
  72. package/server/TERMINAL_INPUT_WS_PROTOCOL.md +44 -0
  73. package/server/index.d.ts +37 -0
  74. package/server/index.js +14694 -0
  75. package/server/lib/cloudflare-tunnel.js +650 -0
  76. package/server/lib/git/DOCUMENTATION.md +146 -0
  77. package/server/lib/git/credentials.js +74 -0
  78. package/server/lib/git/identity-storage.js +110 -0
  79. package/server/lib/git/index.js +6 -0
  80. package/server/lib/git/service.js +3117 -0
  81. package/server/lib/github/DOCUMENTATION.md +170 -0
  82. package/server/lib/github/auth.js +307 -0
  83. package/server/lib/github/device-flow.js +50 -0
  84. package/server/lib/github/index.js +24 -0
  85. package/server/lib/github/octokit.js +10 -0
  86. package/server/lib/github/pr-status.js +478 -0
  87. package/server/lib/github/repo/index.js +55 -0
  88. package/server/lib/installer/desktop.js +289 -0
  89. package/server/lib/installer/download.js +208 -0
  90. package/server/lib/installer/index.js +45 -0
  91. package/server/lib/installer/platform.js +100 -0
  92. package/server/lib/notifications/DOCUMENTATION.md +61 -0
  93. package/server/lib/notifications/index.js +1 -0
  94. package/server/lib/notifications/message.js +49 -0
  95. package/server/lib/notifications/message.test.js +59 -0
  96. package/server/lib/opencode/DOCUMENTATION.md +59 -0
  97. package/server/lib/opencode/agents.js +634 -0
  98. package/server/lib/opencode/auth.js +81 -0
  99. package/server/lib/opencode/commands.js +339 -0
  100. package/server/lib/opencode/index.js +66 -0
  101. package/server/lib/opencode/mcp.js +206 -0
  102. package/server/lib/opencode/providers.js +96 -0
  103. package/server/lib/opencode/shared.js +527 -0
  104. package/server/lib/opencode/skills.js +480 -0
  105. package/server/lib/opencode/tunnel-auth.js +591 -0
  106. package/server/lib/opencode/ui-auth.js +510 -0
  107. package/server/lib/package-manager.js +505 -0
  108. package/server/lib/quota/DOCUMENTATION.md +55 -0
  109. package/server/lib/quota/index.js +24 -0
  110. package/server/lib/quota/providers/claude.js +107 -0
  111. package/server/lib/quota/providers/codex.js +113 -0
  112. package/server/lib/quota/providers/copilot.js +165 -0
  113. package/server/lib/quota/providers/google/api.js +92 -0
  114. package/server/lib/quota/providers/google/auth.js +108 -0
  115. package/server/lib/quota/providers/google/index.js +124 -0
  116. package/server/lib/quota/providers/google/transforms.js +109 -0
  117. package/server/lib/quota/providers/index.js +152 -0
  118. package/server/lib/quota/providers/interface.js +55 -0
  119. package/server/lib/quota/providers/kimi.js +108 -0
  120. package/server/lib/quota/providers/minimax-cn-coding-plan.js +15 -0
  121. package/server/lib/quota/providers/minimax-coding-plan.js +15 -0
  122. package/server/lib/quota/providers/minimax-shared.js +136 -0
  123. package/server/lib/quota/providers/nanogpt.js +124 -0
  124. package/server/lib/quota/providers/ollama-cloud.js +112 -0
  125. package/server/lib/quota/providers/openai.js +91 -0
  126. package/server/lib/quota/providers/openrouter.js +92 -0
  127. package/server/lib/quota/providers/zai.js +91 -0
  128. package/server/lib/quota/utils/auth.js +46 -0
  129. package/server/lib/quota/utils/formatters.js +76 -0
  130. package/server/lib/quota/utils/index.js +10 -0
  131. package/server/lib/quota/utils/transformers.js +55 -0
  132. package/server/lib/skills-catalog/DOCUMENTATION.md +178 -0
  133. package/server/lib/skills-catalog/cache.js +32 -0
  134. package/server/lib/skills-catalog/clawdhub/api.js +158 -0
  135. package/server/lib/skills-catalog/clawdhub/index.js +30 -0
  136. package/server/lib/skills-catalog/clawdhub/install.js +238 -0
  137. package/server/lib/skills-catalog/clawdhub/scan.js +113 -0
  138. package/server/lib/skills-catalog/curated-sources.js +21 -0
  139. package/server/lib/skills-catalog/git.js +77 -0
  140. package/server/lib/skills-catalog/index.js +42 -0
  141. package/server/lib/skills-catalog/install.js +294 -0
  142. package/server/lib/skills-catalog/scan.js +221 -0
  143. package/server/lib/skills-catalog/source.js +85 -0
  144. package/server/lib/terminal/DOCUMENTATION.md +114 -0
  145. package/server/lib/terminal/index.js +12 -0
  146. package/server/lib/terminal/input-ws-protocol.js +66 -0
  147. package/server/lib/terminal/input-ws-protocol.test.js +138 -0
  148. package/server/lib/tts/DOCUMENTATION.md +134 -0
  149. package/server/lib/tts/index.js +16 -0
  150. package/server/lib/tts/service.js +162 -0
  151. package/server/lib/tts/summarization.js +171 -0
  152. package/server/lib/tunnels/index.js +166 -0
  153. package/server/lib/tunnels/providers/cloudflare.js +260 -0
  154. package/server/lib/tunnels/registry.js +51 -0
  155. package/server/lib/tunnels/types.js +219 -0
  156. package/server/lib/utils/lru.js +107 -0
  157. package/server/lib/utils/sse.js +121 -0
@@ -0,0 +1,3117 @@
1
+ import simpleGit from 'simple-git';
2
+ import fs from 'fs';
3
+ import path from 'path';
4
+ import os from 'os';
5
+ import { execFile } from 'child_process';
6
+ import { promisify } from 'util';
7
+
8
+ const fsp = fs.promises;
9
+ const execFileAsync = promisify(execFile);
10
+ const gpgconfCandidates = ['gpgconf', '/opt/homebrew/bin/gpgconf', '/usr/local/bin/gpgconf'];
11
+ let resolvedGitBinary = null;
12
+ const worktreeBootstrapState = new Map();
13
+
14
+ const WORKTREE_BOOTSTRAP_PENDING = 'pending';
15
+ const WORKTREE_BOOTSTRAP_READY = 'ready';
16
+ const WORKTREE_BOOTSTRAP_FAILED = 'failed';
17
+
18
+ const toBootstrapStateKey = (directory) => {
19
+ const normalized = normalizeDirectoryPath(directory);
20
+ if (!normalized) {
21
+ return '';
22
+ }
23
+ return path.resolve(normalized);
24
+ };
25
+
26
+ const setWorktreeBootstrapState = (directory, status, error = null) => {
27
+ const key = toBootstrapStateKey(directory);
28
+ if (!key) {
29
+ return;
30
+ }
31
+ worktreeBootstrapState.set(key, {
32
+ status,
33
+ error: typeof error === 'string' && error.trim().length > 0 ? error.trim() : null,
34
+ updatedAt: Date.now(),
35
+ });
36
+ };
37
+
38
+ const clearWorktreeBootstrapState = (directory) => {
39
+ const key = toBootstrapStateKey(directory);
40
+ if (!key) {
41
+ return;
42
+ }
43
+ worktreeBootstrapState.delete(key);
44
+ };
45
+
46
+ const isExecutableFile = (candidate) => {
47
+ if (typeof candidate !== 'string' || candidate.trim().length === 0) {
48
+ return false;
49
+ }
50
+ try {
51
+ const stat = fs.statSync(candidate);
52
+ if (!stat.isFile()) {
53
+ return false;
54
+ }
55
+ if (process.platform === 'win32') {
56
+ const ext = path.extname(candidate).toLowerCase();
57
+ return ext.length === 0 || ext === '.exe' || ext === '.cmd' || ext === '.bat' || ext === '.com';
58
+ }
59
+ fs.accessSync(candidate, fs.constants.X_OK);
60
+ return true;
61
+ } catch {
62
+ return false;
63
+ }
64
+ };
65
+
66
+ const normalizeGitExecutableCandidate = (candidate) => {
67
+ if (typeof candidate !== 'string') {
68
+ return null;
69
+ }
70
+ const trimmed = candidate.trim();
71
+ if (!trimmed) {
72
+ return null;
73
+ }
74
+
75
+ const ext = path.extname(trimmed).toLowerCase();
76
+ if (ext === '.cmd' || ext === '.bat' || ext === '.com') {
77
+ const exeCandidate = trimmed.slice(0, -ext.length) + '.exe';
78
+ if (isExecutableFile(exeCandidate)) {
79
+ return exeCandidate;
80
+ }
81
+ }
82
+
83
+ return trimmed;
84
+ };
85
+
86
+ const listPathExecutableCandidates = (binaryName) => {
87
+ const currentPath = process.env.PATH || '';
88
+ const seen = new Set();
89
+ const matches = [];
90
+ for (const segment of currentPath.split(path.delimiter)) {
91
+ const dir = typeof segment === 'string' ? segment.trim() : '';
92
+ if (!dir || seen.has(dir)) {
93
+ continue;
94
+ }
95
+ seen.add(dir);
96
+ matches.push(path.join(dir, binaryName));
97
+ }
98
+ return matches;
99
+ };
100
+
101
+ const listWindowsGitInstallCandidates = () => {
102
+ const roots = [
103
+ process.env.ProgramFiles,
104
+ process.env['ProgramFiles(x86)'],
105
+ process.env.LocalAppData,
106
+ ]
107
+ .map((value) => (typeof value === 'string' ? value.trim() : ''))
108
+ .filter(Boolean);
109
+
110
+ const candidates = [];
111
+ for (const root of roots) {
112
+ candidates.push(path.join(root, 'Git', 'cmd', 'git.exe'));
113
+ candidates.push(path.join(root, 'Git', 'bin', 'git.exe'));
114
+ candidates.push(path.join(root, 'Git', 'mingw64', 'bin', 'git.exe'));
115
+ candidates.push(path.join(root, 'Programs', 'Git', 'cmd', 'git.exe'));
116
+ candidates.push(path.join(root, 'Programs', 'Git', 'bin', 'git.exe'));
117
+ }
118
+ return candidates;
119
+ };
120
+
121
+ const resolveGitBinary = () => {
122
+ if (process.platform !== 'win32') {
123
+ return 'git';
124
+ }
125
+ if (resolvedGitBinary) {
126
+ return resolvedGitBinary;
127
+ }
128
+
129
+ const explicit = [process.env.GIT_BINARY, process.env.OPENCHAMBER_GIT_BINARY]
130
+ .map((value) => (typeof value === 'string' ? value.trim() : ''))
131
+ .filter(Boolean);
132
+ for (const candidate of explicit) {
133
+ if (isExecutableFile(candidate)) {
134
+ resolvedGitBinary = candidate;
135
+ return resolvedGitBinary;
136
+ }
137
+ }
138
+
139
+ const discovered = [
140
+ ...listPathExecutableCandidates('git.exe'),
141
+ ...listPathExecutableCandidates('git'),
142
+ ...listWindowsGitInstallCandidates(),
143
+ ]
144
+ .map(normalizeGitExecutableCandidate)
145
+ .filter(Boolean)
146
+ .filter((candidate) => isExecutableFile(candidate));
147
+
148
+ const preferredExe = discovered.find((candidate) => candidate.toLowerCase().endsWith('.exe'));
149
+ resolvedGitBinary = preferredExe || discovered[0] || 'git.exe';
150
+ return resolvedGitBinary;
151
+ };
152
+
153
+ const getGitBinary = () => resolveGitBinary();
154
+
155
+ /**
156
+ * Escape an SSH key path for use in core.sshCommand.
157
+ * Handles Windows/Unix differences and prevents command injection.
158
+ */
159
+ function escapeSshKeyPath(sshKeyPath) {
160
+ const isWindows = process.platform === 'win32';
161
+
162
+ // Normalize path first on Windows (convert backslashes to forward slashes)
163
+ let normalizedPath = sshKeyPath;
164
+ if (isWindows) {
165
+ normalizedPath = sshKeyPath.replace(/\\/g, '/');
166
+ }
167
+
168
+ // Validate: reject paths with characters that could enable injection
169
+ // Allow only alphanumeric, path separators, dots, dashes, underscores, spaces, and colons (for Windows drives)
170
+ // Note: backslash is not in this list since we've already normalized Windows paths
171
+ const dangerousChars = /[`$!"';&|<>(){}[\]*?#~]/;
172
+ if (dangerousChars.test(normalizedPath)) {
173
+ throw new Error(`SSH key path contains invalid characters: ${sshKeyPath}`);
174
+ }
175
+
176
+ if (isWindows) {
177
+ // On Windows, Git (via MSYS/MinGW) expects Unix-style paths
178
+ // Convert "C:/path" to "/c/path" for MSYS compatibility
179
+ let unixPath = normalizedPath;
180
+ const driveMatch = unixPath.match(/^([A-Za-z]):\//);
181
+ if (driveMatch) {
182
+ unixPath = `/${driveMatch[1].toLowerCase()}${unixPath.slice(2)}`;
183
+ }
184
+
185
+ // Use single quotes for the path (prevents shell interpretation)
186
+ return `'${unixPath}'`;
187
+ } else {
188
+ // On Unix, use single quotes and escape any single quotes in the path
189
+ // Single quotes prevent all shell interpretation except for single quotes themselves
190
+ const escaped = normalizedPath.replace(/'/g, "'\\''");
191
+ return `'${escaped}'`;
192
+ }
193
+ }
194
+
195
+ /**
196
+ * Build the SSH command string for git config
197
+ */
198
+ function buildSshCommand(sshKeyPath) {
199
+ const escapedPath = escapeSshKeyPath(sshKeyPath);
200
+ return `ssh -i ${escapedPath} -o IdentitiesOnly=yes`;
201
+ }
202
+
203
+ const isSocketPath = async (candidate) => {
204
+ if (!candidate || typeof candidate !== 'string') {
205
+ return false;
206
+ }
207
+ try {
208
+ const stat = await fsp.stat(candidate);
209
+ return typeof stat.isSocket === 'function' && stat.isSocket();
210
+ } catch {
211
+ return false;
212
+ }
213
+ };
214
+
215
+ const resolveSshAuthSock = async () => {
216
+ const existing = (process.env.SSH_AUTH_SOCK || '').trim();
217
+ if (existing) {
218
+ return existing;
219
+ }
220
+
221
+ if (process.platform === 'win32') {
222
+ return null;
223
+ }
224
+
225
+ const gpgSock = path.join(os.homedir(), '.gnupg', 'S.gpg-agent.ssh');
226
+ if (await isSocketPath(gpgSock)) {
227
+ return gpgSock;
228
+ }
229
+
230
+ const runGpgconf = async (args) => {
231
+ for (const candidate of gpgconfCandidates) {
232
+ try {
233
+ const { stdout } = await execFileAsync(candidate, args);
234
+ return String(stdout || '');
235
+ } catch {
236
+ continue;
237
+ }
238
+ }
239
+ return '';
240
+ };
241
+
242
+ const candidate = (await runGpgconf(['--list-dirs', 'agent-ssh-socket'])).trim();
243
+ if (candidate && await isSocketPath(candidate)) {
244
+ return candidate;
245
+ }
246
+
247
+ if (candidate) {
248
+ await runGpgconf(['--launch', 'gpg-agent']);
249
+ const retried = (await runGpgconf(['--list-dirs', 'agent-ssh-socket'])).trim();
250
+ if (retried && await isSocketPath(retried)) {
251
+ return retried;
252
+ }
253
+ }
254
+
255
+ return null;
256
+ };
257
+
258
+ const buildGitEnv = async () => {
259
+ const env = { ...process.env };
260
+ if (!env.SSH_AUTH_SOCK || !env.SSH_AUTH_SOCK.trim()) {
261
+ const resolved = await resolveSshAuthSock();
262
+ if (resolved) {
263
+ env.SSH_AUTH_SOCK = resolved;
264
+ }
265
+ }
266
+ return env;
267
+ };
268
+
269
+ const createGit = async (directory) => {
270
+ const env = await buildGitEnv();
271
+ const spawnOptions = { windowsHide: true };
272
+ const binary = getGitBinary();
273
+ const hasCustomBinary = typeof binary === 'string' && binary.trim() && binary !== 'git' && binary !== 'git.exe';
274
+ const unsafe = hasCustomBinary ? { allowUnsafeCustomBinary: true } : undefined;
275
+ if (!directory) {
276
+ return simpleGit({ env, spawnOptions, binary, unsafe });
277
+ }
278
+ return simpleGit({
279
+ baseDir: normalizeDirectoryPath(directory),
280
+ env,
281
+ spawnOptions,
282
+ binary,
283
+ unsafe,
284
+ });
285
+ };
286
+
287
+ const normalizeDirectoryPath = (value) => {
288
+ if (typeof value !== 'string') {
289
+ return value;
290
+ }
291
+
292
+ const trimmed = value.trim();
293
+ if (!trimmed) {
294
+ return trimmed;
295
+ }
296
+
297
+ if (trimmed === '~') {
298
+ return os.homedir();
299
+ }
300
+
301
+ if (trimmed.startsWith('~/') || trimmed.startsWith('~\\')) {
302
+ return path.join(os.homedir(), trimmed.slice(2));
303
+ }
304
+
305
+ return trimmed;
306
+ };
307
+
308
+ const cleanBranchName = (branch) => {
309
+ if (!branch) {
310
+ return branch;
311
+ }
312
+ if (branch.startsWith('refs/heads/')) {
313
+ return branch.substring('refs/heads/'.length);
314
+ }
315
+ if (branch.startsWith('heads/')) {
316
+ return branch.substring('heads/'.length);
317
+ }
318
+ if (branch.startsWith('refs/')) {
319
+ return branch.substring('refs/'.length);
320
+ }
321
+ return branch;
322
+ };
323
+
324
+ const OPENCODE_ADJECTIVES = [
325
+ 'brave',
326
+ 'calm',
327
+ 'clever',
328
+ 'cosmic',
329
+ 'crisp',
330
+ 'curious',
331
+ 'eager',
332
+ 'gentle',
333
+ 'glowing',
334
+ 'happy',
335
+ 'hidden',
336
+ 'jolly',
337
+ 'kind',
338
+ 'lucky',
339
+ 'mighty',
340
+ 'misty',
341
+ 'neon',
342
+ 'nimble',
343
+ 'playful',
344
+ 'proud',
345
+ 'quick',
346
+ 'quiet',
347
+ 'shiny',
348
+ 'silent',
349
+ 'stellar',
350
+ 'sunny',
351
+ 'swift',
352
+ 'tidy',
353
+ 'witty',
354
+ ];
355
+
356
+ const OPENCODE_NOUNS = [
357
+ 'cabin',
358
+ 'cactus',
359
+ 'canyon',
360
+ 'circuit',
361
+ 'comet',
362
+ 'eagle',
363
+ 'engine',
364
+ 'falcon',
365
+ 'forest',
366
+ 'garden',
367
+ 'harbor',
368
+ 'island',
369
+ 'knight',
370
+ 'lagoon',
371
+ 'meadow',
372
+ 'moon',
373
+ 'mountain',
374
+ 'nebula',
375
+ 'orchid',
376
+ 'otter',
377
+ 'panda',
378
+ 'pixel',
379
+ 'planet',
380
+ 'river',
381
+ 'rocket',
382
+ 'sailor',
383
+ 'squid',
384
+ 'star',
385
+ 'tiger',
386
+ 'wizard',
387
+ 'wolf',
388
+ ];
389
+
390
+ const OPENCODE_WORKTREE_ATTEMPTS = 26;
391
+
392
+ const getOpenCodeDataPath = () => {
393
+ const xdgDataHome = process.env.XDG_DATA_HOME || path.join(os.homedir(), '.local', 'share');
394
+ return path.join(xdgDataHome, 'opencode');
395
+ };
396
+
397
+ const pickRandom = (values) => values[Math.floor(Math.random() * values.length)];
398
+
399
+ const generateOpenCodeRandomName = () => `${pickRandom(OPENCODE_ADJECTIVES)}-${pickRandom(OPENCODE_NOUNS)}`;
400
+
401
+ const slugWorktreeName = (value) => {
402
+ return String(value || '')
403
+ .trim()
404
+ .replace(/^refs\/heads\//, '')
405
+ .replace(/^heads\//, '')
406
+ .replace(/\s+/g, '-')
407
+ .replace(/^\/+|\/+$/g, '')
408
+ .split('/').join('-')
409
+ .replace(/[^A-Za-z0-9._-]+/g, '-')
410
+ .replace(/-+/g, '-')
411
+ .replace(/^-+/, '')
412
+ .replace(/-+$/, '')
413
+ .slice(0, 80);
414
+ };
415
+
416
+ const parseWorktreePorcelain = (raw) => {
417
+ const lines = String(raw || '').split('\n').map((line) => line.trim());
418
+ const entries = [];
419
+ let current = null;
420
+
421
+ for (const line of lines) {
422
+ if (!line) {
423
+ if (current?.worktree) {
424
+ entries.push(current);
425
+ }
426
+ current = null;
427
+ continue;
428
+ }
429
+
430
+ if (line.startsWith('worktree ')) {
431
+ if (current?.worktree) {
432
+ entries.push(current);
433
+ }
434
+ current = { worktree: line.substring('worktree '.length).trim() };
435
+ continue;
436
+ }
437
+
438
+ if (!current) {
439
+ continue;
440
+ }
441
+
442
+ if (line.startsWith('HEAD ')) {
443
+ current.head = line.substring('HEAD '.length).trim();
444
+ continue;
445
+ }
446
+
447
+ if (line.startsWith('branch ')) {
448
+ const branchRef = line.substring('branch '.length).trim();
449
+ current.branchRef = branchRef;
450
+ current.branch = cleanBranchName(branchRef);
451
+ }
452
+ }
453
+
454
+ if (current?.worktree) {
455
+ entries.push(current);
456
+ }
457
+
458
+ return entries;
459
+ };
460
+
461
+ const canonicalPath = async (input) => {
462
+ const absolutePath = path.resolve(input);
463
+ const realPath = await fsp.realpath(absolutePath).catch(() => absolutePath);
464
+ const normalized = path.normalize(realPath);
465
+ return process.platform === 'win32' ? normalized.toLowerCase() : normalized;
466
+ };
467
+
468
+ const checkPathExists = async (targetPath) => {
469
+ try {
470
+ await fsp.stat(targetPath);
471
+ return true;
472
+ } catch {
473
+ return false;
474
+ }
475
+ };
476
+
477
+ const normalizeStartRef = (value) => {
478
+ const trimmed = String(value || '').trim();
479
+ if (!trimmed) {
480
+ return 'HEAD';
481
+ }
482
+ return trimmed;
483
+ };
484
+
485
+ const parseRemoteBranchRef = (value) => {
486
+ const trimmed = String(value || '').trim();
487
+ if (!trimmed) {
488
+ return null;
489
+ }
490
+
491
+ if (trimmed.startsWith('refs/remotes/')) {
492
+ const rest = trimmed.substring('refs/remotes/'.length);
493
+ const slashIndex = rest.indexOf('/');
494
+ if (slashIndex <= 0 || slashIndex === rest.length - 1) {
495
+ return null;
496
+ }
497
+ return {
498
+ remote: rest.slice(0, slashIndex),
499
+ branch: rest.slice(slashIndex + 1),
500
+ remoteRef: rest,
501
+ fullRef: `refs/remotes/${rest}`,
502
+ };
503
+ }
504
+
505
+ if (trimmed.startsWith('remotes/')) {
506
+ return parseRemoteBranchRef(`refs/${trimmed}`);
507
+ }
508
+
509
+ const slashIndex = trimmed.indexOf('/');
510
+ if (slashIndex <= 0 || slashIndex === trimmed.length - 1) {
511
+ return null;
512
+ }
513
+
514
+ return {
515
+ remote: trimmed.slice(0, slashIndex),
516
+ branch: trimmed.slice(slashIndex + 1),
517
+ remoteRef: trimmed,
518
+ fullRef: `refs/remotes/${trimmed}`,
519
+ };
520
+ };
521
+
522
+ const resolveRemoteBranchRef = async (primaryWorktree, value) => {
523
+ const raw = String(value || '').trim();
524
+ const parsed = parseRemoteBranchRef(raw);
525
+ if (!parsed) {
526
+ return null;
527
+ }
528
+
529
+ if (raw.startsWith('refs/remotes/') || raw.startsWith('remotes/')) {
530
+ return parsed;
531
+ }
532
+
533
+ const localRef = `refs/heads/${raw}`;
534
+ const localExists = await runGitCommand(primaryWorktree, ['show-ref', '--verify', '--quiet', localRef]);
535
+ if (localExists.success) {
536
+ return null;
537
+ }
538
+
539
+ return parsed;
540
+ };
541
+
542
+ const normalizeUpstreamTarget = (remote, branch) => {
543
+ const remoteName = String(remote || '').trim();
544
+ const branchName = String(branch || '').trim();
545
+ if (!remoteName || !branchName) {
546
+ return null;
547
+ }
548
+ return {
549
+ remote: remoteName,
550
+ branch: branchName,
551
+ full: `${remoteName}/${branchName}`,
552
+ };
553
+ };
554
+
555
+ const parseGitErrorText = (error) => {
556
+ const stderr = typeof error?.stderr === 'string' ? error.stderr : '';
557
+ const stdout = typeof error?.stdout === 'string' ? error.stdout : '';
558
+ const message = typeof error?.message === 'string' ? error.message : '';
559
+ return [stderr, stdout, message]
560
+ .map((chunk) => String(chunk || '').trim())
561
+ .filter(Boolean)
562
+ .join('\n')
563
+ .trim();
564
+ };
565
+
566
+ const runGitCommand = async (cwd, args) => {
567
+ try {
568
+ const { stdout, stderr } = await execFileAsync(getGitBinary(), args, {
569
+ cwd,
570
+ env: await buildGitEnv(),
571
+ windowsHide: true,
572
+ maxBuffer: 20 * 1024 * 1024,
573
+ });
574
+ return {
575
+ success: true,
576
+ exitCode: 0,
577
+ stdout: String(stdout || ''),
578
+ stderr: String(stderr || ''),
579
+ };
580
+ } catch (error) {
581
+ return {
582
+ success: false,
583
+ exitCode: typeof error?.code === 'number' ? error.code : 1,
584
+ stdout: String(error?.stdout || ''),
585
+ stderr: String(error?.stderr || ''),
586
+ message: parseGitErrorText(error),
587
+ };
588
+ }
589
+ };
590
+
591
+ const runGitCommandOrThrow = async (cwd, args, fallbackMessage) => {
592
+ const result = await runGitCommand(cwd, args);
593
+ if (!result.success) {
594
+ throw new Error(result.message || fallbackMessage || 'Git command failed');
595
+ }
596
+ return result;
597
+ };
598
+
599
+ const ensureOpenCodeProjectId = async (primaryWorktree) => {
600
+ const gitDir = path.join(primaryWorktree, '.git');
601
+ const idFile = path.join(gitDir, 'opencode');
602
+ const existing = await fsp.readFile(idFile, 'utf8').then((value) => value.trim()).catch(() => '');
603
+ if (existing) {
604
+ return existing;
605
+ }
606
+
607
+ const rootsResult = await runGitCommandOrThrow(
608
+ primaryWorktree,
609
+ ['rev-list', '--max-parents=0', '--all'],
610
+ 'Failed to resolve repository roots'
611
+ );
612
+
613
+ const roots = rootsResult.stdout
614
+ .split('\n')
615
+ .map((line) => line.trim())
616
+ .filter(Boolean)
617
+ .sort((a, b) => a.localeCompare(b));
618
+
619
+ const projectId = roots[0] || '';
620
+ if (!projectId) {
621
+ throw new Error('Failed to derive OpenCode project ID');
622
+ }
623
+
624
+ await fsp.mkdir(gitDir, { recursive: true }).catch(() => undefined);
625
+ await fsp.writeFile(idFile, projectId, 'utf8').catch(() => undefined);
626
+
627
+ return projectId;
628
+ };
629
+
630
+ const resolveWorktreeProjectContext = async (directory) => {
631
+ const directoryPath = normalizeDirectoryPath(directory);
632
+ if (!directoryPath) {
633
+ throw new Error('Directory is required');
634
+ }
635
+
636
+ const topResult = await runGitCommandOrThrow(
637
+ directoryPath,
638
+ ['rev-parse', '--show-toplevel'],
639
+ 'Failed to resolve git top-level directory'
640
+ );
641
+ const sandbox = path.resolve(directoryPath, topResult.stdout.trim());
642
+
643
+ const commonResult = await runGitCommandOrThrow(
644
+ sandbox,
645
+ ['rev-parse', '--git-common-dir'],
646
+ 'Failed to resolve git common directory'
647
+ );
648
+ const commonDir = path.resolve(sandbox, commonResult.stdout.trim());
649
+ const primaryWorktree = path.dirname(commonDir);
650
+ const projectID = await ensureOpenCodeProjectId(primaryWorktree);
651
+ const worktreeRoot = path.join(getOpenCodeDataPath(), 'worktree', projectID);
652
+
653
+ return {
654
+ projectID,
655
+ sandbox,
656
+ primaryWorktree,
657
+ worktreeRoot,
658
+ };
659
+ };
660
+
661
+ const listWorktreeEntries = async (directory) => {
662
+ const rawResult = await runGitCommandOrThrow(
663
+ directory,
664
+ ['worktree', 'list', '--porcelain'],
665
+ 'Failed to list git worktrees'
666
+ );
667
+ return parseWorktreePorcelain(rawResult.stdout);
668
+ };
669
+
670
+ const resolveWorktreeNameCandidates = (baseName) => {
671
+ const normalizedBase = slugWorktreeName(baseName || '');
672
+ if (!normalizedBase) {
673
+ return Array.from({ length: OPENCODE_WORKTREE_ATTEMPTS }, () => generateOpenCodeRandomName());
674
+ }
675
+ return Array.from({ length: OPENCODE_WORKTREE_ATTEMPTS }, (_, index) => {
676
+ if (index === 0) {
677
+ return normalizedBase;
678
+ }
679
+ return `${normalizedBase}-${generateOpenCodeRandomName()}`;
680
+ });
681
+ };
682
+
683
+ const resolveCandidateDirectory = async (worktreeRoot, preferredName, explicitBranchName, primaryWorktree) => {
684
+ const candidates = resolveWorktreeNameCandidates(preferredName);
685
+
686
+ for (const name of candidates) {
687
+ const directory = path.join(worktreeRoot, name);
688
+ if (await checkPathExists(directory)) {
689
+ continue;
690
+ }
691
+
692
+ if (explicitBranchName) {
693
+ return { name, directory, branch: explicitBranchName };
694
+ }
695
+
696
+ const branch = `archcoder/${name}`;
697
+ const branchRef = `refs/heads/${branch}`;
698
+ const branchExists = await runGitCommand(primaryWorktree, ['show-ref', '--verify', '--quiet', branchRef]);
699
+ if (branchExists.success) {
700
+ continue;
701
+ }
702
+
703
+ return { name, directory, branch };
704
+ }
705
+
706
+ throw new Error('Failed to generate a unique worktree name');
707
+ };
708
+
709
+ const resolveBranchForExistingMode = async (primaryWorktree, existingBranch, preferredBranchName) => {
710
+ const requested = String(existingBranch || '').trim();
711
+ if (!requested) {
712
+ throw new Error('existingBranch is required in existing mode');
713
+ }
714
+
715
+ const normalizedLocal = cleanBranchName(requested);
716
+ const localRef = `refs/heads/${normalizedLocal}`;
717
+ const localExists = await runGitCommand(primaryWorktree, ['show-ref', '--verify', '--quiet', localRef]);
718
+ if (localExists.success) {
719
+ return {
720
+ localBranch: normalizedLocal,
721
+ checkoutRef: normalizedLocal,
722
+ createLocalBranch: false,
723
+ remoteRef: null,
724
+ };
725
+ }
726
+
727
+ const remoteRef = parseRemoteBranchRef(requested);
728
+ if (!remoteRef) {
729
+ throw new Error(`Branch not found: ${requested}`);
730
+ }
731
+
732
+ const remoteExists = await runGitCommand(primaryWorktree, ['show-ref', '--verify', '--quiet', remoteRef.fullRef]);
733
+ if (!remoteExists.success) {
734
+ await fetchRemoteBranchRef(primaryWorktree, remoteRef.remote, remoteRef.branch).catch(() => undefined);
735
+ const recheck = await runGitCommand(primaryWorktree, ['show-ref', '--verify', '--quiet', remoteRef.fullRef]);
736
+ if (!recheck.success) {
737
+ throw new Error(`Remote branch not found: ${requested}`);
738
+ }
739
+ }
740
+
741
+ const localBranch = cleanBranchName(preferredBranchName || remoteRef.branch || requested);
742
+ if (!localBranch) {
743
+ throw new Error('Failed to resolve local branch name for existing branch worktree');
744
+ }
745
+
746
+ return {
747
+ localBranch,
748
+ checkoutRef: remoteRef.remoteRef,
749
+ createLocalBranch: true,
750
+ remoteRef,
751
+ };
752
+ };
753
+
754
+ const findBranchInUse = async (primaryWorktree, localBranchName) => {
755
+ if (!localBranchName) {
756
+ return null;
757
+ }
758
+ const entries = await listWorktreeEntries(primaryWorktree);
759
+ const targetRef = `refs/heads/${localBranchName}`;
760
+ const targetClean = cleanBranchName(targetRef);
761
+ return entries.find((entry) => {
762
+ const entryRef = String(entry.branchRef || '').trim();
763
+ const entryClean = cleanBranchName(entryRef || entry.branch || '');
764
+ return entryRef === targetRef || entryClean === targetClean;
765
+ }) || null;
766
+ };
767
+
768
+ const runWorktreeStartCommand = async (directory, command) => {
769
+ const text = String(command || '').trim();
770
+ if (!text) {
771
+ return { success: true };
772
+ }
773
+
774
+ if (process.platform === 'win32') {
775
+ const result = await execFileAsync('cmd', ['/c', text], {
776
+ cwd: directory,
777
+ env: await buildGitEnv(),
778
+ windowsHide: true,
779
+ maxBuffer: 20 * 1024 * 1024,
780
+ }).then(({ stdout, stderr }) => ({ success: true, stdout, stderr })).catch((error) => ({
781
+ success: false,
782
+ stdout: error?.stdout,
783
+ stderr: error?.stderr,
784
+ message: parseGitErrorText(error),
785
+ }));
786
+ return result;
787
+ }
788
+
789
+ const result = await execFileAsync('bash', ['-lc', text], {
790
+ cwd: directory,
791
+ env: await buildGitEnv(),
792
+ maxBuffer: 20 * 1024 * 1024,
793
+ }).then(({ stdout, stderr }) => ({ success: true, stdout, stderr })).catch((error) => ({
794
+ success: false,
795
+ stdout: error?.stdout,
796
+ stderr: error?.stderr,
797
+ message: parseGitErrorText(error),
798
+ }));
799
+ return result;
800
+ };
801
+
802
+ const loadProjectStartCommand = async (projectID) => {
803
+ const storagePath = path.join(getOpenCodeDataPath(), 'storage', 'project', `${projectID}.json`);
804
+ try {
805
+ const raw = await fsp.readFile(storagePath, 'utf8');
806
+ const parsed = JSON.parse(raw);
807
+ const start = typeof parsed?.commands?.start === 'string' ? parsed.commands.start.trim() : '';
808
+ return start || '';
809
+ } catch {
810
+ return '';
811
+ }
812
+ };
813
+
814
+ const getProjectStoragePath = (projectID) => {
815
+ return path.join(getOpenCodeDataPath(), 'storage', 'project', `${projectID}.json`);
816
+ };
817
+
818
+ const updateProjectSandboxes = async (projectID, primaryWorktree, updater) => {
819
+ const storagePath = getProjectStoragePath(projectID);
820
+ await fsp.mkdir(path.dirname(storagePath), { recursive: true });
821
+
822
+ const now = Date.now();
823
+ const base = {
824
+ id: projectID,
825
+ worktree: primaryWorktree,
826
+ vcs: 'git',
827
+ sandboxes: [],
828
+ time: {
829
+ created: now,
830
+ updated: now,
831
+ },
832
+ };
833
+
834
+ const parsed = await fsp.readFile(storagePath, 'utf8').then((raw) => JSON.parse(raw)).catch(() => null);
835
+ const current = parsed && typeof parsed === 'object' ? { ...base, ...parsed } : base;
836
+ current.id = String(current.id || projectID);
837
+ current.worktree = String(current.worktree || primaryWorktree);
838
+ current.vcs = current.vcs || 'git';
839
+ current.sandboxes = Array.isArray(current.sandboxes)
840
+ ? current.sandboxes.map((entry) => String(entry || '').trim()).filter(Boolean)
841
+ : [];
842
+ const createdAt = Number(current?.time?.created);
843
+ current.time = {
844
+ created: Number.isFinite(createdAt) && createdAt > 0 ? createdAt : now,
845
+ updated: now,
846
+ };
847
+
848
+ updater(current);
849
+
850
+ current.sandboxes = [...new Set(
851
+ (Array.isArray(current.sandboxes) ? current.sandboxes : [])
852
+ .map((entry) => String(entry || '').trim())
853
+ .filter(Boolean)
854
+ )];
855
+
856
+ await fsp.writeFile(storagePath, `${JSON.stringify(current, null, 2)}\n`, 'utf8');
857
+ };
858
+
859
+ const syncProjectSandboxAdd = async (projectID, primaryWorktree, sandboxPath) => {
860
+ const sandbox = String(sandboxPath || '').trim();
861
+ if (!sandbox) {
862
+ return;
863
+ }
864
+ await updateProjectSandboxes(projectID, primaryWorktree, (project) => {
865
+ if (!project.sandboxes.includes(sandbox)) {
866
+ project.sandboxes.push(sandbox);
867
+ }
868
+ });
869
+ };
870
+
871
+ const syncProjectSandboxRemove = async (projectID, primaryWorktree, sandboxPath) => {
872
+ const sandbox = String(sandboxPath || '').trim();
873
+ if (!sandbox) {
874
+ return;
875
+ }
876
+ await updateProjectSandboxes(projectID, primaryWorktree, (project) => {
877
+ project.sandboxes = project.sandboxes.filter((entry) => entry !== sandbox);
878
+ });
879
+ };
880
+
881
+ const runWorktreeStartScripts = async (directory, projectID, startCommand) => {
882
+ const projectStart = await loadProjectStartCommand(projectID);
883
+ if (projectStart) {
884
+ const projectResult = await runWorktreeStartCommand(directory, projectStart);
885
+ if (!projectResult.success) {
886
+ console.warn('Worktree project start command failed:', projectResult.message || projectResult.stderr || projectResult.stdout);
887
+ return;
888
+ }
889
+ }
890
+
891
+ const extraCommand = String(startCommand || '').trim();
892
+ if (!extraCommand) {
893
+ return;
894
+ }
895
+ const extraResult = await runWorktreeStartCommand(directory, extraCommand);
896
+ if (!extraResult.success) {
897
+ console.warn('Worktree start command failed:', extraResult.message || extraResult.stderr || extraResult.stdout);
898
+ }
899
+ };
900
+
901
+ const queueWorktreeBootstrap = (args) => {
902
+ const {
903
+ directory,
904
+ projectID,
905
+ primaryWorktree,
906
+ localBranch,
907
+ setUpstream,
908
+ upstreamRemote,
909
+ upstreamBranch,
910
+ ensureRemoteName,
911
+ ensureRemoteUrl,
912
+ startCommand,
913
+ } = args;
914
+ setTimeout(() => {
915
+ const run = async () => {
916
+ await runGitCommandOrThrow(directory, ['reset', '--hard'], 'Failed to populate worktree');
917
+ if (setUpstream) {
918
+ await applyUpstreamConfiguration({
919
+ primaryWorktree,
920
+ worktreeDirectory: directory,
921
+ localBranch,
922
+ setUpstream,
923
+ upstreamRemote,
924
+ upstreamBranch,
925
+ ensureRemoteName,
926
+ ensureRemoteUrl,
927
+ }).catch((error) => {
928
+ console.warn('Worktree upstream configuration failed:', error instanceof Error ? error.message : String(error));
929
+ });
930
+ }
931
+ await runWorktreeStartScripts(directory, projectID, startCommand).catch((error) => {
932
+ console.warn('Worktree start script task failed:', error instanceof Error ? error.message : String(error));
933
+ });
934
+ setWorktreeBootstrapState(directory, WORKTREE_BOOTSTRAP_READY);
935
+ };
936
+
937
+ void run().catch((error) => {
938
+ setWorktreeBootstrapState(
939
+ directory,
940
+ WORKTREE_BOOTSTRAP_FAILED,
941
+ error instanceof Error ? error.message : String(error)
942
+ );
943
+ console.warn('Worktree bootstrap task failed:', error instanceof Error ? error.message : String(error));
944
+ });
945
+ }, 0);
946
+ };
947
+
948
+ const ensureRemoteWithUrl = async (primaryWorktree, remoteName, remoteUrl) => {
949
+ const name = String(remoteName || '').trim();
950
+ const url = String(remoteUrl || '').trim();
951
+ if (!name || !url) {
952
+ return;
953
+ }
954
+
955
+ const getUrl = await runGitCommand(primaryWorktree, ['remote', 'get-url', name]);
956
+ if (getUrl.success) {
957
+ const currentUrl = String(getUrl.stdout || '').trim();
958
+ if (currentUrl !== url) {
959
+ await runGitCommandOrThrow(primaryWorktree, ['remote', 'set-url', name, url], 'Failed to update git remote URL');
960
+ }
961
+ return;
962
+ }
963
+
964
+ await runGitCommandOrThrow(primaryWorktree, ['remote', 'add', name, url], 'Failed to add git remote');
965
+ };
966
+
967
+ const fetchRemoteBranchRef = async (primaryWorktree, remoteName, branchName) => {
968
+ const remote = String(remoteName || '').trim();
969
+ const branch = String(branchName || '').trim();
970
+ if (!remote || !branch) {
971
+ return;
972
+ }
973
+
974
+ const refspec = `+refs/heads/${branch}:refs/remotes/${remote}/${branch}`;
975
+ await runGitCommandOrThrow(
976
+ primaryWorktree,
977
+ ['fetch', remote, refspec],
978
+ `Failed to fetch ${remote}/${branch}`
979
+ );
980
+ };
981
+
982
+ const checkRemoteBranchExists = async (primaryWorktree, remoteName, branchName, remoteUrl = '') => {
983
+ const remote = String(remoteName || '').trim();
984
+ const branch = String(branchName || '').trim();
985
+ const url = String(remoteUrl || '').trim();
986
+ if (!remote || !branch) {
987
+ return { success: false, found: false };
988
+ }
989
+
990
+ const target = url || remote;
991
+ const lsRemote = await runGitCommand(
992
+ primaryWorktree,
993
+ ['ls-remote', '--heads', target, `refs/heads/${branch}`]
994
+ );
995
+ if (!lsRemote.success) {
996
+ return { success: false, found: false };
997
+ }
998
+
999
+ return {
1000
+ success: true,
1001
+ found: Boolean(String(lsRemote.stdout || '').trim()),
1002
+ };
1003
+ };
1004
+
1005
+ const setBranchTrackingFallback = async (worktreeDirectory, localBranch, upstream) => {
1006
+ await runGitCommandOrThrow(
1007
+ worktreeDirectory,
1008
+ ['config', `branch.${localBranch}.remote`, upstream.remote],
1009
+ `Failed to set branch.${localBranch}.remote`
1010
+ );
1011
+ await runGitCommandOrThrow(
1012
+ worktreeDirectory,
1013
+ ['config', `branch.${localBranch}.merge`, `refs/heads/${upstream.branch}`],
1014
+ `Failed to set branch.${localBranch}.merge`
1015
+ );
1016
+ };
1017
+
1018
+ const applyUpstreamConfiguration = async (args) => {
1019
+ const {
1020
+ primaryWorktree,
1021
+ worktreeDirectory,
1022
+ localBranch,
1023
+ setUpstream,
1024
+ upstreamRemote,
1025
+ upstreamBranch,
1026
+ ensureRemoteName,
1027
+ ensureRemoteUrl,
1028
+ } = args;
1029
+
1030
+ if (!setUpstream) {
1031
+ return;
1032
+ }
1033
+
1034
+ if (ensureRemoteName && ensureRemoteUrl) {
1035
+ await ensureRemoteWithUrl(primaryWorktree, ensureRemoteName, ensureRemoteUrl);
1036
+ }
1037
+
1038
+ const upstream = normalizeUpstreamTarget(upstreamRemote, upstreamBranch);
1039
+ if (!upstream || !localBranch) {
1040
+ return;
1041
+ }
1042
+
1043
+ let fetched = true;
1044
+ try {
1045
+ await fetchRemoteBranchRef(primaryWorktree, upstream.remote, upstream.branch);
1046
+ } catch {
1047
+ fetched = false;
1048
+ }
1049
+
1050
+ if (fetched) {
1051
+ await runGitCommandOrThrow(
1052
+ worktreeDirectory,
1053
+ ['branch', `--set-upstream-to=${upstream.full}`, localBranch],
1054
+ `Failed to set upstream to ${upstream.full}`
1055
+ );
1056
+ return;
1057
+ }
1058
+
1059
+ await setBranchTrackingFallback(worktreeDirectory, localBranch, upstream);
1060
+ };
1061
+
1062
+ export async function isGitRepository(directory) {
1063
+ const directoryPath = normalizeDirectoryPath(directory);
1064
+ if (!directoryPath || !fs.existsSync(directoryPath)) {
1065
+ return false;
1066
+ }
1067
+
1068
+ const gitDir = path.join(directoryPath, '.git');
1069
+ return fs.existsSync(gitDir);
1070
+ }
1071
+
1072
+ export async function getGlobalIdentity() {
1073
+ const git = await createGit();
1074
+
1075
+ try {
1076
+ const userName = await git.getConfig('user.name', 'global').catch(() => null);
1077
+ const userEmail = await git.getConfig('user.email', 'global').catch(() => null);
1078
+ const sshCommand = await git.getConfig('core.sshCommand', 'global').catch(() => null);
1079
+
1080
+ return {
1081
+ userName: userName?.value || null,
1082
+ userEmail: userEmail?.value || null,
1083
+ sshCommand: sshCommand?.value || null
1084
+ };
1085
+ } catch (error) {
1086
+ console.error('Failed to get global Git identity:', error);
1087
+ return {
1088
+ userName: null,
1089
+ userEmail: null,
1090
+ sshCommand: null
1091
+ };
1092
+ }
1093
+ }
1094
+
1095
+ export async function getRemoteUrl(directory, remoteName = 'origin') {
1096
+ const git = await createGit(directory);
1097
+
1098
+ try {
1099
+ const url = await git.remote(['get-url', remoteName]);
1100
+ return url?.trim() || null;
1101
+ } catch {
1102
+ return null;
1103
+ }
1104
+ }
1105
+
1106
+ export async function getCurrentIdentity(directory) {
1107
+ const git = await createGit(directory);
1108
+
1109
+ try {
1110
+
1111
+ const userName = await git.getConfig('user.name', 'local').catch(() =>
1112
+ git.getConfig('user.name', 'global')
1113
+ );
1114
+
1115
+ const userEmail = await git.getConfig('user.email', 'local').catch(() =>
1116
+ git.getConfig('user.email', 'global')
1117
+ );
1118
+
1119
+ const sshCommand = await git.getConfig('core.sshCommand', 'local').catch(() =>
1120
+ git.getConfig('core.sshCommand', 'global')
1121
+ );
1122
+
1123
+ return {
1124
+ userName: userName?.value || null,
1125
+ userEmail: userEmail?.value || null,
1126
+ sshCommand: sshCommand?.value || null
1127
+ };
1128
+ } catch (error) {
1129
+ console.error('Failed to get current Git identity:', error);
1130
+ return {
1131
+ userName: null,
1132
+ userEmail: null,
1133
+ sshCommand: null
1134
+ };
1135
+ }
1136
+ }
1137
+
1138
+ export async function hasLocalIdentity(directory) {
1139
+ const git = await createGit(directory);
1140
+
1141
+ try {
1142
+ const localName = await git.getConfig('user.name', 'local').catch(() => null);
1143
+ const localEmail = await git.getConfig('user.email', 'local').catch(() => null);
1144
+ return Boolean(localName?.value || localEmail?.value);
1145
+ } catch {
1146
+ return false;
1147
+ }
1148
+ }
1149
+
1150
+ export async function setLocalIdentity(directory, profile) {
1151
+ const git = await createGit(directory);
1152
+
1153
+ try {
1154
+
1155
+ await git.addConfig('user.name', profile.userName, false, 'local');
1156
+ await git.addConfig('user.email', profile.userEmail, false, 'local');
1157
+
1158
+ const authType = profile.authType || 'ssh';
1159
+
1160
+ if (authType === 'ssh' && profile.sshKey) {
1161
+ await git.addConfig(
1162
+ 'core.sshCommand',
1163
+ buildSshCommand(profile.sshKey),
1164
+ false,
1165
+ 'local'
1166
+ );
1167
+ await git.raw(['config', '--local', '--unset', 'credential.helper']).catch(() => {});
1168
+ } else if (authType === 'token' && profile.host) {
1169
+ await git.addConfig(
1170
+ 'credential.helper',
1171
+ 'store',
1172
+ false,
1173
+ 'local'
1174
+ );
1175
+ await git.raw(['config', '--local', '--unset', 'core.sshCommand']).catch(() => {});
1176
+ }
1177
+
1178
+ return true;
1179
+ } catch (error) {
1180
+ console.error('Failed to set Git identity:', error);
1181
+ throw error;
1182
+ }
1183
+ }
1184
+
1185
+ export async function getStatus(directory) {
1186
+ const directoryPath = normalizeDirectoryPath(directory);
1187
+ const git = await createGit(directoryPath);
1188
+
1189
+ try {
1190
+ // Use -uall to show all untracked files individually, not just directories
1191
+ const status = await git.status(['-uall']);
1192
+
1193
+ const [stagedStatsRaw, workingStatsRaw] = await Promise.all([
1194
+ git.raw(['diff', '--cached', '--numstat']).catch(() => ''),
1195
+ git.raw(['diff', '--numstat']).catch(() => ''),
1196
+ ]);
1197
+
1198
+ const diffStatsMap = new Map();
1199
+
1200
+ const accumulateStats = (raw) => {
1201
+ if (!raw) return;
1202
+ raw
1203
+ .split('\n')
1204
+ .map((line) => line.trim())
1205
+ .filter(Boolean)
1206
+ .forEach((line) => {
1207
+ const parts = line.split('\t');
1208
+ if (parts.length < 3) {
1209
+ return;
1210
+ }
1211
+ const [insertionsRaw, deletionsRaw, ...pathParts] = parts;
1212
+ const path = pathParts.join('\t');
1213
+ if (!path) {
1214
+ return;
1215
+ }
1216
+ const insertions = insertionsRaw === '-' ? 0 : parseInt(insertionsRaw, 10) || 0;
1217
+ const deletions = deletionsRaw === '-' ? 0 : parseInt(deletionsRaw, 10) || 0;
1218
+
1219
+ const existing = diffStatsMap.get(path) || { insertions: 0, deletions: 0 };
1220
+ diffStatsMap.set(path, {
1221
+ insertions: existing.insertions + insertions,
1222
+ deletions: existing.deletions + deletions,
1223
+ });
1224
+ });
1225
+ };
1226
+
1227
+ accumulateStats(stagedStatsRaw);
1228
+ accumulateStats(workingStatsRaw);
1229
+
1230
+ const diffStats = Object.fromEntries(diffStatsMap.entries());
1231
+
1232
+ const newFileStats = await Promise.all(
1233
+ status.files.map(async (file) => {
1234
+ const working = (file.working_dir || '').trim();
1235
+ const indexStatus = (file.index || '').trim();
1236
+ const statusCode = working || indexStatus;
1237
+
1238
+ if (statusCode !== '?' && statusCode !== 'A') {
1239
+ return null;
1240
+ }
1241
+
1242
+ const existing = diffStats[file.path];
1243
+ if (existing && existing.insertions > 0) {
1244
+ return null;
1245
+ }
1246
+
1247
+ const absolutePath = path.join(directoryPath, file.path);
1248
+
1249
+ try {
1250
+ const stat = await fsp.stat(absolutePath);
1251
+ if (!stat.isFile()) {
1252
+ return null;
1253
+ }
1254
+
1255
+ const buffer = await fsp.readFile(absolutePath);
1256
+ if (buffer.indexOf(0) !== -1) {
1257
+ return {
1258
+ path: file.path,
1259
+ insertions: existing?.insertions ?? 0,
1260
+ deletions: existing?.deletions ?? 0,
1261
+ };
1262
+ }
1263
+
1264
+ const normalized = buffer.toString('utf8').replace(/\r\n/g, '\n');
1265
+ if (!normalized.length) {
1266
+ return {
1267
+ path: file.path,
1268
+ insertions: 0,
1269
+ deletions: 0,
1270
+ };
1271
+ }
1272
+
1273
+ const segments = normalized.split('\n');
1274
+ if (normalized.endsWith('\n')) {
1275
+ segments.pop();
1276
+ }
1277
+
1278
+ const lineCount = segments.length;
1279
+ return {
1280
+ path: file.path,
1281
+ insertions: lineCount,
1282
+ deletions: 0,
1283
+ };
1284
+ } catch (error) {
1285
+ console.warn('Failed to estimate diff stats for new file', file.path, error);
1286
+ return null;
1287
+ }
1288
+ })
1289
+ );
1290
+
1291
+ for (const entry of newFileStats) {
1292
+ if (!entry) continue;
1293
+ diffStats[entry.path] = {
1294
+ insertions: entry.insertions,
1295
+ deletions: entry.deletions,
1296
+ };
1297
+ }
1298
+
1299
+ const selectBaseRefForUnpublished = async () => {
1300
+ const candidates = [];
1301
+
1302
+ const originHead = await git
1303
+ .raw(['symbolic-ref', '-q', 'refs/remotes/origin/HEAD'])
1304
+ .then((value) => String(value || '').trim())
1305
+ .catch(() => '');
1306
+
1307
+ if (originHead) {
1308
+ // "refs/remotes/origin/main" -> "origin/main"
1309
+ candidates.push(originHead.replace(/^refs\/remotes\//, ''));
1310
+ }
1311
+
1312
+ candidates.push('origin/main', 'origin/master', 'main', 'master');
1313
+
1314
+ for (const ref of candidates) {
1315
+ const exists = await git
1316
+ .raw(['rev-parse', '--verify', ref])
1317
+ .then((value) => String(value || '').trim())
1318
+ .catch(() => '');
1319
+ if (exists) return ref;
1320
+ }
1321
+
1322
+ return null;
1323
+ };
1324
+
1325
+ let tracking = status.tracking || null;
1326
+ let ahead = status.ahead;
1327
+ let behind = status.behind;
1328
+
1329
+ // When no upstream is configured (common for new worktree branches), Git doesn't report ahead/behind.
1330
+ // We still want to show the number of unpublished commits to the user.
1331
+ if (!tracking && status.current) {
1332
+ const baseRef = await selectBaseRefForUnpublished();
1333
+ if (baseRef) {
1334
+ const countRaw = await git
1335
+ .raw(['rev-list', '--count', `${baseRef}..HEAD`])
1336
+ .then((value) => String(value || '').trim())
1337
+ .catch(() => '');
1338
+ const count = parseInt(countRaw, 10);
1339
+ if (Number.isFinite(count)) {
1340
+ ahead = count;
1341
+ behind = 0;
1342
+ }
1343
+ }
1344
+ }
1345
+
1346
+ // Check for in-progress operations
1347
+ let mergeInProgress = null;
1348
+ let rebaseInProgress = null;
1349
+
1350
+ try {
1351
+ // Check MERGE_HEAD for merge in progress
1352
+ const mergeHeadExists = await git
1353
+ .raw(['rev-parse', '--verify', '--quiet', 'MERGE_HEAD'])
1354
+ .then(() => true)
1355
+ .catch(() => false);
1356
+
1357
+ if (mergeHeadExists) {
1358
+ const mergeHead = await git.raw(['rev-parse', 'MERGE_HEAD']).catch(() => '');
1359
+ const headSha = mergeHead.trim().slice(0, 7);
1360
+ // Only set mergeInProgress if we actually have a valid head SHA
1361
+ if (headSha) {
1362
+ const mergeMsg = await fsp.readFile(path.join(directoryPath, '.git', 'MERGE_MSG'), 'utf8').catch(() => '');
1363
+ mergeInProgress = {
1364
+ head: headSha,
1365
+ message: mergeMsg.split('\n')[0] || '',
1366
+ };
1367
+ }
1368
+ }
1369
+ } catch {
1370
+ // ignore
1371
+ }
1372
+
1373
+ try {
1374
+ // Check for rebase in progress (.git/rebase-merge or .git/rebase-apply)
1375
+ const rebaseMergeExists = await fsp.stat(path.join(directoryPath, '.git', 'rebase-merge')).then(() => true).catch(() => false);
1376
+ const rebaseApplyExists = await fsp.stat(path.join(directoryPath, '.git', 'rebase-apply')).then(() => true).catch(() => false);
1377
+
1378
+ if (rebaseMergeExists || rebaseApplyExists) {
1379
+ const rebaseDir = rebaseMergeExists ? 'rebase-merge' : 'rebase-apply';
1380
+ const headName = await fsp.readFile(path.join(directoryPath, '.git', rebaseDir, 'head-name'), 'utf8').catch(() => '');
1381
+ const onto = await fsp.readFile(path.join(directoryPath, '.git', rebaseDir, 'onto'), 'utf8').catch(() => '');
1382
+
1383
+ const headNameTrimmed = headName.trim().replace('refs/heads/', '');
1384
+ const ontoTrimmed = onto.trim().slice(0, 7);
1385
+
1386
+ // Only set rebaseInProgress if we have valid data
1387
+ if (headNameTrimmed || ontoTrimmed) {
1388
+ rebaseInProgress = {
1389
+ headName: headNameTrimmed,
1390
+ onto: ontoTrimmed,
1391
+ };
1392
+ }
1393
+ }
1394
+ } catch {
1395
+ // ignore
1396
+ }
1397
+
1398
+ return {
1399
+ current: status.current,
1400
+ tracking,
1401
+ ahead,
1402
+ behind,
1403
+ files: status.files.map((f) => ({
1404
+ path: f.path,
1405
+ index: f.index,
1406
+ working_dir: f.working_dir,
1407
+ })),
1408
+ isClean: status.isClean(),
1409
+ diffStats,
1410
+ mergeInProgress,
1411
+ rebaseInProgress,
1412
+ };
1413
+ } catch (error) {
1414
+ console.error('Failed to get Git status:', error);
1415
+ throw error;
1416
+ }
1417
+ }
1418
+
1419
+ export async function getDiff(directory, { path, staged = false, contextLines = 3 } = {}) {
1420
+ const git = await createGit(directory);
1421
+
1422
+ try {
1423
+ const args = ['diff', '--no-color'];
1424
+
1425
+ if (typeof contextLines === 'number' && !Number.isNaN(contextLines)) {
1426
+ args.push(`-U${Math.max(0, contextLines)}`);
1427
+ }
1428
+
1429
+ if (staged) {
1430
+ args.push('--cached');
1431
+ }
1432
+
1433
+ if (path) {
1434
+ args.push('--', path);
1435
+ }
1436
+
1437
+ const diff = await git.raw(args);
1438
+ if (diff && diff.trim().length > 0) {
1439
+ return diff;
1440
+ }
1441
+
1442
+ if (staged) {
1443
+ return diff;
1444
+ }
1445
+
1446
+ try {
1447
+ await git.raw(['ls-files', '--error-unmatch', path]);
1448
+ return diff;
1449
+ } catch {
1450
+ const noIndexArgs = ['diff', '--no-color'];
1451
+ if (typeof contextLines === 'number' && !Number.isNaN(contextLines)) {
1452
+ noIndexArgs.push(`-U${Math.max(0, contextLines)}`);
1453
+ }
1454
+ noIndexArgs.push('--no-index', '--', '/dev/null', path);
1455
+ try {
1456
+ const noIndexDiff = await git.raw(noIndexArgs);
1457
+ return noIndexDiff;
1458
+ } catch (noIndexError) {
1459
+ // git diff --no-index returns exit code 1 when differences exist (not a real error)
1460
+ if (noIndexError.exitCode === 1 && noIndexError.message) {
1461
+ return noIndexError.message;
1462
+ }
1463
+ throw noIndexError;
1464
+ }
1465
+ }
1466
+ } catch (error) {
1467
+ console.error('Failed to get Git diff:', error);
1468
+ throw error;
1469
+ }
1470
+ }
1471
+
1472
+ export async function getRangeDiff(directory, { base, head, path, contextLines = 3 } = {}) {
1473
+ const git = await createGit(directory);
1474
+ const baseRef = typeof base === 'string' ? base.trim() : '';
1475
+ const headRef = typeof head === 'string' ? head.trim() : '';
1476
+ if (!baseRef || !headRef) {
1477
+ throw new Error('base and head are required');
1478
+ }
1479
+
1480
+ // Prefer remote-tracking base ref so merged commits don't reappear
1481
+ // when local base branch is stale (common when user stays on feature branch).
1482
+ let resolvedBase = baseRef;
1483
+ const originCandidate = `refs/remotes/origin/${baseRef}`;
1484
+ try {
1485
+ const verified = await git.raw(['rev-parse', '--verify', originCandidate]);
1486
+ if (verified && verified.trim()) {
1487
+ resolvedBase = `origin/${baseRef}`;
1488
+ }
1489
+ } catch {
1490
+ // ignore
1491
+ }
1492
+
1493
+ const args = ['diff', '--no-color'];
1494
+ if (typeof contextLines === 'number' && !Number.isNaN(contextLines)) {
1495
+ args.push(`-U${Math.max(0, contextLines)}`);
1496
+ }
1497
+ args.push(`${resolvedBase}...${headRef}`);
1498
+ if (path) {
1499
+ args.push('--', path);
1500
+ }
1501
+ const diff = await git.raw(args);
1502
+ return diff;
1503
+ }
1504
+
1505
+ export async function getRangeFiles(directory, { base, head } = {}) {
1506
+ const git = await createGit(directory);
1507
+ const baseRef = typeof base === 'string' ? base.trim() : '';
1508
+ const headRef = typeof head === 'string' ? head.trim() : '';
1509
+ if (!baseRef || !headRef) {
1510
+ throw new Error('base and head are required');
1511
+ }
1512
+
1513
+ let resolvedBase = baseRef;
1514
+ const originCandidate = `refs/remotes/origin/${baseRef}`;
1515
+ try {
1516
+ const verified = await git.raw(['rev-parse', '--verify', originCandidate]);
1517
+ if (verified && verified.trim()) {
1518
+ resolvedBase = `origin/${baseRef}`;
1519
+ }
1520
+ } catch {
1521
+ // ignore
1522
+ }
1523
+
1524
+ const raw = await git.raw(['diff', '--name-only', `${resolvedBase}...${headRef}`]);
1525
+ return String(raw || '')
1526
+ .split('\n')
1527
+ .map((l) => l.trim())
1528
+ .filter(Boolean);
1529
+ }
1530
+
1531
+ const IMAGE_EXTENSIONS = ['png', 'jpg', 'jpeg', 'gif', 'svg', 'webp', 'ico', 'bmp', 'avif'];
1532
+
1533
+ const BINARY_SNIFF_BYTES = 8192;
1534
+
1535
+ function isImageFile(filePath) {
1536
+ const ext = filePath.split('.').pop()?.toLowerCase();
1537
+ return IMAGE_EXTENSIONS.includes(ext || '');
1538
+ }
1539
+
1540
+ function getImageMimeType(filePath) {
1541
+ const ext = filePath.split('.').pop()?.toLowerCase();
1542
+ const mimeMap = {
1543
+ 'png': 'image/png',
1544
+ 'jpg': 'image/jpeg',
1545
+ 'jpeg': 'image/jpeg',
1546
+ 'gif': 'image/gif',
1547
+ 'svg': 'image/svg+xml',
1548
+ 'webp': 'image/webp',
1549
+ 'ico': 'image/x-icon',
1550
+ 'bmp': 'image/bmp',
1551
+ 'avif': 'image/avif',
1552
+ };
1553
+ return mimeMap[ext] || 'application/octet-stream';
1554
+ }
1555
+
1556
+ const parseIsBinaryFromNumstat = (raw) => {
1557
+ const text = String(raw || '').trim();
1558
+ if (!text) {
1559
+ return false;
1560
+ }
1561
+
1562
+ // Expected format: <added>\t<deleted>\t<path>
1563
+ const firstLine = text.split('\n').map((line) => line.trim()).find(Boolean) || '';
1564
+ const [added, deleted] = firstLine.split('\t');
1565
+ return added === '-' || deleted === '-';
1566
+ };
1567
+
1568
+ const looksBinaryBySniff = async (absolutePath) => {
1569
+ try {
1570
+ const handle = await fsp.open(absolutePath, 'r');
1571
+ try {
1572
+ const buffer = Buffer.alloc(BINARY_SNIFF_BYTES);
1573
+ const { bytesRead } = await handle.read(buffer, 0, BINARY_SNIFF_BYTES, 0);
1574
+ if (bytesRead <= 0) {
1575
+ return false;
1576
+ }
1577
+ return buffer.subarray(0, bytesRead).includes(0);
1578
+ } finally {
1579
+ await handle.close();
1580
+ }
1581
+ } catch {
1582
+ return false;
1583
+ }
1584
+ };
1585
+
1586
+ const isBinaryDiff = async (directoryPath, filePath, staged) => {
1587
+ // Fast path: ask git for numstat. For binary, it returns "-\t-\t<path>".
1588
+ const args = ['diff', '--numstat'];
1589
+ if (staged) {
1590
+ args.push('--cached');
1591
+ }
1592
+ args.push('--', filePath);
1593
+
1594
+ const result = await runGitCommand(directoryPath, args);
1595
+ if (parseIsBinaryFromNumstat(result.stdout)) {
1596
+ return true;
1597
+ }
1598
+
1599
+ // Fallback for untracked files (diff output is empty): use --no-index against /dev/null
1600
+ if (!staged) {
1601
+ const tracked = await runGitCommand(directoryPath, ['ls-files', '--error-unmatch', '--', filePath]).then((r) => r.success);
1602
+ if (!tracked) {
1603
+ const noIndex = await runGitCommand(directoryPath, ['diff', '--no-index', '--numstat', '--', '/dev/null', filePath]);
1604
+ if (parseIsBinaryFromNumstat(noIndex.stdout) || parseIsBinaryFromNumstat(noIndex.stderr) || parseIsBinaryFromNumstat(noIndex.message)) {
1605
+ return true;
1606
+ }
1607
+ const text = `${noIndex.stdout || ''}\n${noIndex.stderr || ''}\n${noIndex.message || ''}`.toLowerCase();
1608
+ if (text.includes('binary files') || text.includes('git binary patch')) {
1609
+ return true;
1610
+ }
1611
+ }
1612
+ }
1613
+
1614
+ return false;
1615
+ };
1616
+
1617
+ export async function getFileDiff(directory, { path: filePath, staged = false } = {}) {
1618
+ if (!directory || !filePath) {
1619
+ throw new Error('directory and path are required for getFileDiff');
1620
+ }
1621
+
1622
+ const directoryPath = normalizeDirectoryPath(directory);
1623
+ const git = await createGit(directoryPath);
1624
+ const isImage = isImageFile(filePath);
1625
+ const mimeType = isImage ? getImageMimeType(filePath) : null;
1626
+
1627
+ if (!isImage) {
1628
+ const absolutePath = path.join(directoryPath, filePath);
1629
+ const isBinaryBySniff = await looksBinaryBySniff(absolutePath);
1630
+ const isBinary = isBinaryBySniff || (await isBinaryDiff(directoryPath, filePath, staged));
1631
+ if (isBinary) {
1632
+ return {
1633
+ original: '',
1634
+ modified: '',
1635
+ path: filePath,
1636
+ isBinary: true,
1637
+ };
1638
+ }
1639
+ }
1640
+
1641
+ let original = '';
1642
+ try {
1643
+ if (isImage) {
1644
+ // For images, use git show with raw output and convert to base64
1645
+ try {
1646
+ const { stdout } = await execFileAsync(getGitBinary(), ['show', `HEAD:${filePath}`], {
1647
+ cwd: directoryPath,
1648
+ encoding: 'buffer',
1649
+ windowsHide: true,
1650
+ maxBuffer: 50 * 1024 * 1024, // 50MB max
1651
+ });
1652
+ if (stdout && stdout.length > 0) {
1653
+ original = `data:${mimeType};base64,${stdout.toString('base64')}`;
1654
+ }
1655
+ } catch {
1656
+ original = '';
1657
+ }
1658
+ } else {
1659
+ original = await git.show([`HEAD:${filePath}`]);
1660
+ }
1661
+ } catch {
1662
+ original = '';
1663
+ }
1664
+
1665
+ const fullPath = path.join(directoryPath, filePath);
1666
+ let modified = '';
1667
+ try {
1668
+ const stat = await fsp.stat(fullPath);
1669
+ if (stat.isFile()) {
1670
+ if (isImage) {
1671
+ // For images, read as binary and convert to data URL
1672
+ const buffer = await fsp.readFile(fullPath);
1673
+ modified = `data:${mimeType};base64,${buffer.toString('base64')}`;
1674
+ } else {
1675
+ modified = await fsp.readFile(fullPath, 'utf8');
1676
+ }
1677
+ }
1678
+ } catch (error) {
1679
+ if (error && typeof error === 'object' && error.code === 'ENOENT') {
1680
+ modified = '';
1681
+ } else {
1682
+ console.error('Failed to read modified file contents for diff:', error);
1683
+ throw error;
1684
+ }
1685
+ }
1686
+
1687
+ return {
1688
+ original,
1689
+ modified,
1690
+ path: filePath,
1691
+ isBinary: false,
1692
+ };
1693
+ }
1694
+
1695
+ export async function revertFile(directory, filePath) {
1696
+ const directoryPath = normalizeDirectoryPath(directory);
1697
+ const git = await createGit(directoryPath);
1698
+ const repoRoot = path.resolve(directoryPath);
1699
+ const absoluteTarget = path.resolve(repoRoot, filePath);
1700
+
1701
+ if (!absoluteTarget.startsWith(repoRoot + path.sep) && absoluteTarget !== repoRoot) {
1702
+ throw new Error('Invalid file path');
1703
+ }
1704
+
1705
+ const isTracked = await git
1706
+ .raw(['ls-files', '--error-unmatch', filePath])
1707
+ .then(() => true)
1708
+ .catch(() => false);
1709
+
1710
+ if (!isTracked) {
1711
+ try {
1712
+ await git.raw(['clean', '-f', '-d', '--', filePath]);
1713
+ return;
1714
+ } catch (cleanError) {
1715
+ try {
1716
+ await fsp.rm(absoluteTarget, { recursive: true, force: true });
1717
+ return;
1718
+ } catch (fsError) {
1719
+ if (fsError && typeof fsError === 'object' && fsError.code === 'ENOENT') {
1720
+ return;
1721
+ }
1722
+ console.error('Failed to remove untracked file during revert:', fsError);
1723
+ throw fsError;
1724
+ }
1725
+ }
1726
+ }
1727
+
1728
+ try {
1729
+ await git.raw(['restore', '--staged', filePath]);
1730
+ } catch (error) {
1731
+ await git.raw(['reset', 'HEAD', '--', filePath]).catch(() => {});
1732
+ }
1733
+
1734
+ try {
1735
+ await git.raw(['restore', filePath]);
1736
+ } catch (error) {
1737
+ try {
1738
+ await git.raw(['checkout', '--', filePath]);
1739
+ } catch (fallbackError) {
1740
+ console.error('Failed to revert git file:', fallbackError);
1741
+ throw fallbackError;
1742
+ }
1743
+ }
1744
+ }
1745
+
1746
+ export async function collectDiffs(directory, files = []) {
1747
+ const results = [];
1748
+ for (const filePath of files) {
1749
+ try {
1750
+ const diff = await getDiff(directory, { path: filePath });
1751
+ if (diff && diff.trim().length > 0) {
1752
+ results.push({ path: filePath, diff });
1753
+ }
1754
+ } catch (error) {
1755
+ console.error(`Failed to diff ${filePath}:`, error);
1756
+ }
1757
+ }
1758
+ return results;
1759
+ }
1760
+
1761
+ export async function pull(directory, options = {}) {
1762
+ const git = await createGit(directory);
1763
+
1764
+ try {
1765
+ const result = await git.pull(
1766
+ options.remote || 'origin',
1767
+ options.branch,
1768
+ options.options || {}
1769
+ );
1770
+
1771
+ return {
1772
+ success: true,
1773
+ summary: result.summary,
1774
+ files: result.files,
1775
+ insertions: result.insertions,
1776
+ deletions: result.deletions
1777
+ };
1778
+ } catch (error) {
1779
+ console.error('Failed to pull:', error);
1780
+ throw error;
1781
+ }
1782
+ }
1783
+
1784
+ export async function push(directory, options = {}) {
1785
+ const git = await createGit(directory);
1786
+
1787
+ const describePushError = (error) => {
1788
+ const fromNestedGit = error?.git && typeof error.git === 'object'
1789
+ ? [error.git.message, error.git.stderr, error.git.stdout]
1790
+ : [];
1791
+ const candidates = [
1792
+ error?.message,
1793
+ error?.stderr,
1794
+ error?.stdout,
1795
+ ...fromNestedGit,
1796
+ ]
1797
+ .map((value) => String(value || '').trim())
1798
+ .filter(Boolean);
1799
+
1800
+ return candidates[0] || 'Failed to push to remote';
1801
+ };
1802
+
1803
+ const buildUpstreamOptions = (raw) => {
1804
+ if (Array.isArray(raw)) {
1805
+ return raw.includes('--set-upstream') ? raw : [...raw, '--set-upstream'];
1806
+ }
1807
+
1808
+ if (raw && typeof raw === 'object') {
1809
+ return { ...raw, '--set-upstream': null };
1810
+ }
1811
+
1812
+ return ['--set-upstream'];
1813
+ };
1814
+
1815
+ const looksLikeMissingUpstream = (error) => {
1816
+ const message = String(error?.message || error?.stderr || '').toLowerCase();
1817
+ return (
1818
+ message.includes('has no upstream') ||
1819
+ message.includes('no upstream') ||
1820
+ message.includes('set-upstream') ||
1821
+ message.includes('set upstream') ||
1822
+ (message.includes('upstream') && message.includes('push') && message.includes('-u'))
1823
+ );
1824
+ };
1825
+
1826
+ const normalizePushResult = (result) => {
1827
+ return {
1828
+ success: true,
1829
+ pushed: result.pushed,
1830
+ repo: result.repo,
1831
+ ref: result.ref,
1832
+ };
1833
+ };
1834
+
1835
+ const remote = String(options.remote || '').trim();
1836
+
1837
+ if (!remote && !options.branch) {
1838
+ try {
1839
+ await git.push();
1840
+ return {
1841
+ success: true,
1842
+ pushed: [],
1843
+ repo: directory,
1844
+ ref: null,
1845
+ };
1846
+ } catch (error) {
1847
+ if (!looksLikeMissingUpstream(error)) {
1848
+ const message = describePushError(error);
1849
+ console.error('Failed to push:', error);
1850
+ throw new Error(message);
1851
+ }
1852
+
1853
+ try {
1854
+ const status = await git.status();
1855
+ const branch = status.current;
1856
+ const remotes = await git.getRemotes(true);
1857
+ const fallbackRemote = remotes.find((entry) => entry.name === 'origin')?.name || remotes[0]?.name;
1858
+ if (!branch || !fallbackRemote) {
1859
+ const message = describePushError(error);
1860
+ throw new Error(message);
1861
+ }
1862
+
1863
+ const result = await git.push(fallbackRemote, branch, buildUpstreamOptions(options.options));
1864
+ return normalizePushResult(result);
1865
+ } catch (fallbackError) {
1866
+ const message = describePushError(fallbackError);
1867
+ console.error('Failed to push (including upstream fallback):', fallbackError);
1868
+ throw new Error(message);
1869
+ }
1870
+ }
1871
+ }
1872
+
1873
+ const remoteName = remote || 'origin';
1874
+
1875
+ // If caller didn't specify a branch, this is the common "Push"/"Commit & Push" path.
1876
+ // When there's no upstream yet (typical for freshly-created worktree branches), publish it on first push.
1877
+ if (!options.branch) {
1878
+ try {
1879
+ const status = await git.status();
1880
+ if (status.current && !status.tracking) {
1881
+ const result = await git.push(remoteName, status.current, buildUpstreamOptions(options.options));
1882
+ return normalizePushResult(result);
1883
+ }
1884
+ } catch (error) {
1885
+ // If we can't read status, fall back to the regular push path below.
1886
+ console.warn('Failed to read git status before push:', error);
1887
+ }
1888
+ }
1889
+
1890
+ try {
1891
+ const result = await git.push(remoteName, options.branch, options.options || {});
1892
+ return normalizePushResult(result);
1893
+ } catch (error) {
1894
+ // Last-resort fallback: retry with upstream if the error suggests it's missing.
1895
+ if (!looksLikeMissingUpstream(error)) {
1896
+ const message = describePushError(error);
1897
+ console.error('Failed to push:', error);
1898
+ throw new Error(message);
1899
+ }
1900
+
1901
+ try {
1902
+ const status = await git.status();
1903
+ const branch = options.branch || status.current;
1904
+ if (!branch) {
1905
+ console.error('Failed to push: missing branch name for upstream setup:', error);
1906
+ throw error;
1907
+ }
1908
+
1909
+ const result = await git.push(remoteName, branch, buildUpstreamOptions(options.options));
1910
+ return normalizePushResult(result);
1911
+ } catch (fallbackError) {
1912
+ const message = describePushError(fallbackError);
1913
+ console.error('Failed to push (including upstream fallback):', fallbackError);
1914
+ throw new Error(message);
1915
+ }
1916
+ }
1917
+ }
1918
+
1919
+ export async function deleteRemoteBranch(directory, options = {}) {
1920
+ const { branch, remote } = options;
1921
+ if (!branch) {
1922
+ throw new Error('branch is required to delete remote branch');
1923
+ }
1924
+
1925
+ const git = await createGit(directory);
1926
+ const targetBranch = branch.startsWith('refs/heads/')
1927
+ ? branch.substring('refs/heads/'.length)
1928
+ : branch;
1929
+ const remoteName = remote || 'origin';
1930
+
1931
+ try {
1932
+ await git.push(remoteName, `:${targetBranch}`);
1933
+ return { success: true };
1934
+ } catch (error) {
1935
+ console.error('Failed to delete remote branch:', error);
1936
+ throw error;
1937
+ }
1938
+ }
1939
+
1940
+ export async function fetch(directory, options = {}) {
1941
+ const git = await createGit(directory);
1942
+
1943
+ try {
1944
+ await git.fetch(
1945
+ options.remote || 'origin',
1946
+ options.branch,
1947
+ options.options || {}
1948
+ );
1949
+
1950
+ return { success: true };
1951
+ } catch (error) {
1952
+ console.error('Failed to fetch:', error);
1953
+ throw error;
1954
+ }
1955
+ }
1956
+
1957
+ export async function commit(directory, message, options = {}) {
1958
+ const git = await createGit(directory);
1959
+
1960
+ try {
1961
+ const requestedFiles = Array.isArray(options.files)
1962
+ ? options.files
1963
+ .map((value) => String(value || '').trim())
1964
+ .filter(Boolean)
1965
+ : [];
1966
+ let filesToCommit = requestedFiles;
1967
+
1968
+ if (options.addAll) {
1969
+ await git.add('.');
1970
+ } else if (requestedFiles.length > 0) {
1971
+ const status = await git.status();
1972
+ const fileStatusByPath = new Map(status.files.map((file) => [file.path, file]));
1973
+ filesToCommit = requestedFiles.filter((filePath) => fileStatusByPath.has(filePath));
1974
+
1975
+ if (filesToCommit.length === 0) {
1976
+ throw new Error('No selected files are available to commit. Refresh git status and try again.');
1977
+ }
1978
+
1979
+ const filesNeedingAdd = filesToCommit.filter((filePath) => {
1980
+ const fileStatus = fileStatusByPath.get(filePath);
1981
+ if (!fileStatus) {
1982
+ return false;
1983
+ }
1984
+
1985
+ const alreadyFullyStaged = fileStatus.index !== ' ' && fileStatus.working_dir === ' ';
1986
+ return !alreadyFullyStaged;
1987
+ });
1988
+
1989
+ if (filesNeedingAdd.length > 0) {
1990
+ await git.add(filesNeedingAdd);
1991
+ }
1992
+ }
1993
+
1994
+ const commitArgs =
1995
+ !options.addAll && filesToCommit.length > 0
1996
+ ? filesToCommit
1997
+ : undefined;
1998
+
1999
+ let result;
2000
+ try {
2001
+ result = await git.commit(message, commitArgs);
2002
+ } catch (error) {
2003
+ const gitErrorText = parseGitErrorText(error);
2004
+ const isPathspecError = gitErrorText.includes('pathspec') && gitErrorText.includes('did not match any files');
2005
+ if (!isPathspecError || !commitArgs || commitArgs.length === 0) {
2006
+ throw error;
2007
+ }
2008
+
2009
+ // Fallback for deleted/stale selections: commit currently staged changes.
2010
+ result = await git.commit(message);
2011
+ }
2012
+
2013
+ return {
2014
+ success: true,
2015
+ commit: result.commit,
2016
+ branch: result.branch,
2017
+ summary: result.summary
2018
+ };
2019
+ } catch (error) {
2020
+ console.error('Failed to commit:', error);
2021
+ throw error;
2022
+ }
2023
+ }
2024
+
2025
+ export async function getBranches(directory) {
2026
+ const git = await createGit(directory);
2027
+
2028
+ try {
2029
+ const result = await git.branch();
2030
+
2031
+ const allBranches = result.all;
2032
+ const remoteBranches = allBranches.filter(branch => branch.startsWith('remotes/'));
2033
+ const activeRemoteBranches = await filterActiveRemoteBranches(git, remoteBranches);
2034
+
2035
+ const filteredAll = [
2036
+ ...allBranches.filter(branch => !branch.startsWith('remotes/')),
2037
+ ...activeRemoteBranches
2038
+ ];
2039
+
2040
+ return {
2041
+ all: filteredAll,
2042
+ current: result.current,
2043
+ branches: result.branches
2044
+ };
2045
+ } catch (error) {
2046
+ console.error('Failed to get branches:', error);
2047
+ throw error;
2048
+ }
2049
+ }
2050
+
2051
+ async function filterActiveRemoteBranches(git, remoteBranches) {
2052
+ try {
2053
+
2054
+ const lsRemoteResult = await git.raw(['ls-remote', '--heads', 'origin']);
2055
+ const actualRemoteBranches = new Set();
2056
+
2057
+ const lines = lsRemoteResult.trim().split('\n');
2058
+ for (const line of lines) {
2059
+ if (line.includes('\trefs/heads/')) {
2060
+ const branchName = line.split('\t')[1].replace('refs/heads/', '');
2061
+ actualRemoteBranches.add(branchName);
2062
+ }
2063
+ }
2064
+
2065
+ return remoteBranches.filter(remoteBranch => {
2066
+
2067
+ const match = remoteBranch.match(/^remotes\/[^\/]+\/(.+)$/);
2068
+ if (!match) return false;
2069
+
2070
+ const branchName = match[1];
2071
+ return actualRemoteBranches.has(branchName);
2072
+ });
2073
+ } catch (error) {
2074
+ console.warn('Failed to filter active remote branches, returning all:', error.message);
2075
+ return remoteBranches;
2076
+ }
2077
+ }
2078
+
2079
+ export async function createBranch(directory, branchName, options = {}) {
2080
+ const git = await createGit(directory);
2081
+
2082
+ try {
2083
+ await git.checkoutBranch(branchName, options.startPoint || 'HEAD');
2084
+ return { success: true, branch: branchName };
2085
+ } catch (error) {
2086
+ console.error('Failed to create branch:', error);
2087
+ throw error;
2088
+ }
2089
+ }
2090
+
2091
+ export async function checkoutBranch(directory, branchName) {
2092
+ const git = await createGit(directory);
2093
+
2094
+ try {
2095
+ await git.checkout(branchName);
2096
+ return { success: true, branch: branchName };
2097
+ } catch (error) {
2098
+ console.error('Failed to checkout branch:', error);
2099
+ throw error;
2100
+ }
2101
+ }
2102
+
2103
+ export async function getWorktrees(directory) {
2104
+ const directoryPath = normalizeDirectoryPath(directory);
2105
+ if (!directoryPath || !fs.existsSync(directoryPath) || !fs.existsSync(path.join(directoryPath, '.git'))) {
2106
+ return [];
2107
+ }
2108
+ try {
2109
+ const result = await runGitCommandOrThrow(
2110
+ directoryPath,
2111
+ ['worktree', 'list', '--porcelain'],
2112
+ 'Failed to list git worktrees'
2113
+ );
2114
+ return parseWorktreePorcelain(result.stdout).map((entry) => ({
2115
+ head: entry.head || '',
2116
+ name: path.basename(entry.worktree || ''),
2117
+ branch: entry.branch || '',
2118
+ path: entry.worktree,
2119
+ }));
2120
+ } catch (error) {
2121
+ console.warn('Failed to list worktrees, returning empty list:', error?.message || error);
2122
+ return [];
2123
+ }
2124
+ }
2125
+
2126
+ export async function validateWorktreeCreate(directory, input = {}) {
2127
+ const mode = input?.mode === 'existing' ? 'existing' : 'new';
2128
+ const errors = [];
2129
+
2130
+ try {
2131
+ const context = await resolveWorktreeProjectContext(directory);
2132
+ const preferredBranchName = cleanBranchName(String(input?.branchName || '').trim());
2133
+ const startRef = normalizeStartRef(input?.startRef);
2134
+ const ensureRemoteName = String(input?.ensureRemoteName || '').trim();
2135
+ const ensureRemoteUrl = String(input?.ensureRemoteUrl || '').trim();
2136
+
2137
+ let localBranch = '';
2138
+ let inferredUpstream = null;
2139
+
2140
+ if (mode === 'existing') {
2141
+ try {
2142
+ const requestedExistingBranch = String(input?.existingBranch || '').trim();
2143
+ const parsedExistingRemote = await resolveRemoteBranchRef(context.primaryWorktree, requestedExistingBranch);
2144
+ if (parsedExistingRemote && ensureRemoteName && ensureRemoteUrl && ensureRemoteName === parsedExistingRemote.remote) {
2145
+ const lsRemote = await runGitCommand(
2146
+ context.primaryWorktree,
2147
+ ['ls-remote', '--heads', ensureRemoteUrl, `refs/heads/${parsedExistingRemote.branch}`]
2148
+ );
2149
+ if (!lsRemote.success) {
2150
+ throw new Error(`Unable to query remote ${ensureRemoteName}`);
2151
+ }
2152
+ if (!String(lsRemote.stdout || '').trim()) {
2153
+ throw new Error(`Remote branch not found: ${parsedExistingRemote.remoteRef}`);
2154
+ }
2155
+ localBranch = cleanBranchName(preferredBranchName || parsedExistingRemote.branch);
2156
+ inferredUpstream = {
2157
+ remote: parsedExistingRemote.remote,
2158
+ branch: parsedExistingRemote.branch,
2159
+ };
2160
+ } else {
2161
+ const resolved = await resolveBranchForExistingMode(context.primaryWorktree, requestedExistingBranch, preferredBranchName);
2162
+ localBranch = resolved.localBranch || '';
2163
+ if (resolved.remoteRef) {
2164
+ inferredUpstream = {
2165
+ remote: resolved.remoteRef.remote,
2166
+ branch: resolved.remoteRef.branch,
2167
+ };
2168
+ }
2169
+ }
2170
+ } catch (error) {
2171
+ errors.push({
2172
+ code: 'branch_not_found',
2173
+ message: error instanceof Error ? error.message : 'Existing branch not found',
2174
+ });
2175
+ }
2176
+ } else {
2177
+ if (preferredBranchName) {
2178
+ const exists = await runGitCommand(context.primaryWorktree, ['show-ref', '--verify', '--quiet', `refs/heads/${preferredBranchName}`]);
2179
+ if (exists.success) {
2180
+ errors.push({
2181
+ code: 'branch_exists',
2182
+ message: `Branch already exists: ${preferredBranchName}`,
2183
+ });
2184
+ }
2185
+ localBranch = preferredBranchName;
2186
+ }
2187
+
2188
+ const parsedRemoteRef = await resolveRemoteBranchRef(context.primaryWorktree, startRef);
2189
+ if (startRef && startRef !== 'HEAD') {
2190
+ if (parsedRemoteRef && ensureRemoteName && ensureRemoteUrl && ensureRemoteName === parsedRemoteRef.remote) {
2191
+ const remoteCheck = await checkRemoteBranchExists(
2192
+ context.primaryWorktree,
2193
+ parsedRemoteRef.remote,
2194
+ parsedRemoteRef.branch,
2195
+ ensureRemoteUrl
2196
+ );
2197
+ if (!remoteCheck.success) {
2198
+ errors.push({
2199
+ code: 'remote_unreachable',
2200
+ message: `Unable to query remote ${ensureRemoteName}`,
2201
+ });
2202
+ } else if (!remoteCheck.found) {
2203
+ errors.push({
2204
+ code: 'start_ref_not_found',
2205
+ message: `Remote branch not found: ${parsedRemoteRef.remoteRef}`,
2206
+ });
2207
+ }
2208
+ } else if (parsedRemoteRef) {
2209
+ const remoteCheck = await checkRemoteBranchExists(
2210
+ context.primaryWorktree,
2211
+ parsedRemoteRef.remote,
2212
+ parsedRemoteRef.branch
2213
+ );
2214
+ if (!remoteCheck.success) {
2215
+ errors.push({
2216
+ code: 'remote_unreachable',
2217
+ message: `Unable to query remote ${parsedRemoteRef.remote}`,
2218
+ });
2219
+ } else if (!remoteCheck.found) {
2220
+ errors.push({
2221
+ code: 'start_ref_not_found',
2222
+ message: `Remote branch not found: ${parsedRemoteRef.remoteRef}`,
2223
+ });
2224
+ }
2225
+ } else {
2226
+ const startRefExists = await runGitCommand(context.primaryWorktree, ['rev-parse', '--verify', '--quiet', startRef]);
2227
+ if (!startRefExists.success) {
2228
+ errors.push({
2229
+ code: 'start_ref_not_found',
2230
+ message: `Start ref not found: ${startRef}`,
2231
+ });
2232
+ }
2233
+ }
2234
+ }
2235
+
2236
+ if (parsedRemoteRef) {
2237
+ inferredUpstream = {
2238
+ remote: parsedRemoteRef.remote,
2239
+ branch: parsedRemoteRef.branch,
2240
+ };
2241
+ }
2242
+ }
2243
+
2244
+ if (localBranch) {
2245
+ const inUse = await findBranchInUse(context.primaryWorktree, localBranch);
2246
+ if (inUse) {
2247
+ errors.push({
2248
+ code: 'branch_in_use',
2249
+ message: `Branch is already checked out in ${inUse.worktree}`,
2250
+ });
2251
+ }
2252
+ }
2253
+
2254
+ if ((ensureRemoteName && !ensureRemoteUrl) || (!ensureRemoteName && ensureRemoteUrl)) {
2255
+ errors.push({
2256
+ code: 'invalid_remote_config',
2257
+ message: 'Both ensureRemoteName and ensureRemoteUrl are required together',
2258
+ });
2259
+ }
2260
+
2261
+ const shouldSetUpstream = Boolean(input?.setUpstream);
2262
+ if (shouldSetUpstream) {
2263
+ const upstreamRemote = String(input?.upstreamRemote || inferredUpstream?.remote || '').trim();
2264
+ const upstreamBranch = String(input?.upstreamBranch || inferredUpstream?.branch || '').trim();
2265
+
2266
+ if (!upstreamRemote || !upstreamBranch) {
2267
+ errors.push({
2268
+ code: 'upstream_incomplete',
2269
+ message: 'upstreamRemote and upstreamBranch are required when setUpstream is true',
2270
+ });
2271
+ } else {
2272
+ const remoteExists = await runGitCommand(context.primaryWorktree, ['remote', 'get-url', upstreamRemote]);
2273
+ if (!remoteExists.success && (!ensureRemoteName || ensureRemoteName !== upstreamRemote)) {
2274
+ errors.push({
2275
+ code: 'remote_not_found',
2276
+ message: `Remote not found: ${upstreamRemote}`,
2277
+ });
2278
+ }
2279
+ }
2280
+ }
2281
+
2282
+ return {
2283
+ ok: errors.length === 0,
2284
+ errors,
2285
+ resolved: {
2286
+ mode,
2287
+ localBranch: localBranch || null,
2288
+ },
2289
+ };
2290
+ } catch (error) {
2291
+ return {
2292
+ ok: false,
2293
+ errors: [{
2294
+ code: 'validation_failed',
2295
+ message: error instanceof Error ? error.message : 'Failed to validate worktree creation',
2296
+ }],
2297
+ };
2298
+ }
2299
+ }
2300
+
2301
+ export async function previewWorktreeCreate(directory, input = {}) {
2302
+ const mode = input?.mode === 'existing' ? 'existing' : 'new';
2303
+ const context = await resolveWorktreeProjectContext(directory);
2304
+ await fsp.mkdir(context.worktreeRoot, { recursive: true });
2305
+
2306
+ const preferredName = String(input?.worktreeName || input?.name || '').trim();
2307
+ const preferredBranchName = cleanBranchName(String(input?.branchName || '').trim());
2308
+ const candidate = await resolveCandidateDirectory(
2309
+ context.worktreeRoot,
2310
+ preferredName,
2311
+ mode === 'new' && preferredBranchName ? preferredBranchName : '',
2312
+ context.primaryWorktree
2313
+ );
2314
+
2315
+ return {
2316
+ name: candidate.name,
2317
+ branch: mode === 'new' ? candidate.branch : preferredBranchName,
2318
+ path: candidate.directory,
2319
+ };
2320
+ }
2321
+
2322
+ export async function createWorktree(directory, input = {}) {
2323
+ const mode = input?.mode === 'existing' ? 'existing' : 'new';
2324
+ const context = await resolveWorktreeProjectContext(directory);
2325
+ await fsp.mkdir(context.worktreeRoot, { recursive: true });
2326
+
2327
+ const preferredName = String(input?.worktreeName || input?.name || '').trim();
2328
+ const preferredBranchName = cleanBranchName(String(input?.branchName || '').trim());
2329
+ const startRef = normalizeStartRef(input?.startRef);
2330
+ const ensureRemoteName = String(input?.ensureRemoteName || '').trim();
2331
+ const ensureRemoteUrl = String(input?.ensureRemoteUrl || '').trim();
2332
+
2333
+ const candidate = await resolveCandidateDirectory(
2334
+ context.worktreeRoot,
2335
+ preferredName,
2336
+ mode === 'new' && preferredBranchName ? preferredBranchName : '',
2337
+ context.primaryWorktree
2338
+ );
2339
+
2340
+ let localBranch = '';
2341
+ let inferredUpstream = null;
2342
+ const worktreeAddArgs = ['worktree', 'add', '--no-checkout'];
2343
+
2344
+ if (mode === 'existing') {
2345
+ const requestedExistingBranch = String(input?.existingBranch || '').trim();
2346
+ const parsedExistingRemote = await resolveRemoteBranchRef(context.primaryWorktree, requestedExistingBranch);
2347
+ if (parsedExistingRemote && ensureRemoteName && ensureRemoteUrl && parsedExistingRemote.remote === ensureRemoteName) {
2348
+ await ensureRemoteWithUrl(context.primaryWorktree, ensureRemoteName, ensureRemoteUrl);
2349
+ await fetchRemoteBranchRef(context.primaryWorktree, parsedExistingRemote.remote, parsedExistingRemote.branch);
2350
+ }
2351
+
2352
+ const resolved = await resolveBranchForExistingMode(context.primaryWorktree, requestedExistingBranch, preferredBranchName);
2353
+ localBranch = resolved.localBranch;
2354
+
2355
+ const inUse = await findBranchInUse(context.primaryWorktree, localBranch);
2356
+ if (inUse) {
2357
+ throw new Error(`Branch is already checked out in ${inUse.worktree}`);
2358
+ }
2359
+
2360
+ if (resolved.createLocalBranch) {
2361
+ worktreeAddArgs.push('-b', localBranch);
2362
+ }
2363
+ worktreeAddArgs.push(candidate.directory, resolved.checkoutRef);
2364
+
2365
+ if (resolved.remoteRef) {
2366
+ inferredUpstream = {
2367
+ remote: resolved.remoteRef.remote,
2368
+ branch: resolved.remoteRef.branch,
2369
+ };
2370
+ }
2371
+ } else {
2372
+ localBranch = candidate.branch;
2373
+ if (!localBranch) {
2374
+ throw new Error('Failed to resolve branch name for new worktree');
2375
+ }
2376
+
2377
+ const branchExists = await runGitCommand(context.primaryWorktree, ['show-ref', '--verify', '--quiet', `refs/heads/${localBranch}`]);
2378
+ if (branchExists.success) {
2379
+ throw new Error(`Branch already exists: ${localBranch}`);
2380
+ }
2381
+
2382
+ const inUse = await findBranchInUse(context.primaryWorktree, localBranch);
2383
+ if (inUse) {
2384
+ throw new Error(`Branch is already checked out in ${inUse.worktree}`);
2385
+ }
2386
+
2387
+ worktreeAddArgs.push('-b', localBranch, candidate.directory);
2388
+ if (startRef && startRef !== 'HEAD') {
2389
+ worktreeAddArgs.push(startRef);
2390
+ }
2391
+
2392
+ const parsedRemoteStartRef = await resolveRemoteBranchRef(context.primaryWorktree, startRef);
2393
+ if (parsedRemoteStartRef) {
2394
+ inferredUpstream = {
2395
+ remote: parsedRemoteStartRef.remote,
2396
+ branch: parsedRemoteStartRef.branch,
2397
+ };
2398
+ }
2399
+ }
2400
+
2401
+ if (ensureRemoteName && ensureRemoteUrl) {
2402
+ await ensureRemoteWithUrl(context.primaryWorktree, ensureRemoteName, ensureRemoteUrl);
2403
+ }
2404
+
2405
+ if (mode === 'new') {
2406
+ const parsedRemoteStartRef = await resolveRemoteBranchRef(context.primaryWorktree, startRef);
2407
+ if (parsedRemoteStartRef) {
2408
+ await fetchRemoteBranchRef(context.primaryWorktree, parsedRemoteStartRef.remote, parsedRemoteStartRef.branch);
2409
+ }
2410
+ }
2411
+
2412
+ await runGitCommandOrThrow(context.primaryWorktree, worktreeAddArgs, 'Failed to create git worktree');
2413
+
2414
+ try {
2415
+ await syncProjectSandboxAdd(context.projectID, context.primaryWorktree, candidate.directory);
2416
+ } catch (error) {
2417
+ console.warn('Failed to sync OpenCode sandbox metadata (add):', error instanceof Error ? error.message : String(error));
2418
+ }
2419
+
2420
+ const shouldSetUpstream = Boolean(input?.setUpstream);
2421
+ const upstreamRemote = String(input?.upstreamRemote || inferredUpstream?.remote || '').trim();
2422
+ const upstreamBranch = String(input?.upstreamBranch || inferredUpstream?.branch || '').trim();
2423
+
2424
+ setWorktreeBootstrapState(candidate.directory, WORKTREE_BOOTSTRAP_PENDING);
2425
+
2426
+ queueWorktreeBootstrap({
2427
+ directory: candidate.directory,
2428
+ projectID: context.projectID,
2429
+ primaryWorktree: context.primaryWorktree,
2430
+ localBranch,
2431
+ setUpstream: shouldSetUpstream,
2432
+ upstreamRemote,
2433
+ upstreamBranch,
2434
+ ensureRemoteName,
2435
+ ensureRemoteUrl,
2436
+ startCommand: input?.startCommand,
2437
+ });
2438
+
2439
+ const headResult = await runGitCommand(candidate.directory, ['rev-parse', 'HEAD']);
2440
+ const head = String(headResult.stdout || '').trim();
2441
+
2442
+ return {
2443
+ head,
2444
+ name: candidate.name,
2445
+ branch: localBranch,
2446
+ path: candidate.directory,
2447
+ };
2448
+ }
2449
+
2450
+ export async function getWorktreeBootstrapStatus(directory) {
2451
+ const key = toBootstrapStateKey(directory);
2452
+ if (!key) {
2453
+ throw new Error('Worktree directory is required');
2454
+ }
2455
+
2456
+ const current = worktreeBootstrapState.get(key);
2457
+ if (current) {
2458
+ return current;
2459
+ }
2460
+
2461
+ return {
2462
+ status: WORKTREE_BOOTSTRAP_READY,
2463
+ error: null,
2464
+ updatedAt: Date.now(),
2465
+ };
2466
+ }
2467
+
2468
+ export async function removeWorktree(directory, input = {}) {
2469
+ const targetDirectory = normalizeDirectoryPath(input?.directory);
2470
+ if (!targetDirectory) {
2471
+ throw new Error('Worktree directory is required');
2472
+ }
2473
+
2474
+ const context = await resolveWorktreeProjectContext(directory);
2475
+ const deleteLocalBranch = input?.deleteLocalBranch === true;
2476
+
2477
+ const targetCanonical = await canonicalPath(targetDirectory);
2478
+ const primaryCanonical = await canonicalPath(context.primaryWorktree);
2479
+ if (targetCanonical === primaryCanonical) {
2480
+ throw new Error('Cannot remove the primary workspace');
2481
+ }
2482
+
2483
+ const entries = await listWorktreeEntries(context.primaryWorktree);
2484
+ const matchedEntry = await (async () => {
2485
+ for (const entry of entries) {
2486
+ if (!entry?.worktree) {
2487
+ continue;
2488
+ }
2489
+ const entryCanonical = await canonicalPath(entry.worktree);
2490
+ if (entryCanonical === targetCanonical) {
2491
+ return entry;
2492
+ }
2493
+ }
2494
+ return null;
2495
+ })();
2496
+
2497
+ if (!matchedEntry?.worktree) {
2498
+ const targetExists = await checkPathExists(targetDirectory);
2499
+ if (targetExists) {
2500
+ await fsp.rm(targetDirectory, { recursive: true, force: true });
2501
+ }
2502
+
2503
+ try {
2504
+ await syncProjectSandboxRemove(context.projectID, context.primaryWorktree, targetDirectory);
2505
+ } catch (error) {
2506
+ console.warn('Failed to sync OpenCode sandbox metadata (remove):', error instanceof Error ? error.message : String(error));
2507
+ }
2508
+
2509
+ clearWorktreeBootstrapState(targetDirectory);
2510
+
2511
+ return true;
2512
+ }
2513
+
2514
+ await runGitCommandOrThrow(
2515
+ context.primaryWorktree,
2516
+ ['worktree', 'remove', '--force', matchedEntry.worktree],
2517
+ 'Failed to remove git worktree'
2518
+ );
2519
+
2520
+ if (deleteLocalBranch) {
2521
+ const branchName = cleanBranchName(String(matchedEntry.branchRef || matchedEntry.branch || '').trim());
2522
+ if (branchName) {
2523
+ await runGitCommandOrThrow(
2524
+ context.primaryWorktree,
2525
+ ['branch', '-D', branchName],
2526
+ `Failed to delete local branch ${branchName}`
2527
+ );
2528
+ }
2529
+ }
2530
+
2531
+ try {
2532
+ await syncProjectSandboxRemove(context.projectID, context.primaryWorktree, matchedEntry.worktree);
2533
+ } catch (error) {
2534
+ console.warn('Failed to sync OpenCode sandbox metadata (remove):', error instanceof Error ? error.message : String(error));
2535
+ }
2536
+
2537
+ clearWorktreeBootstrapState(matchedEntry.worktree);
2538
+
2539
+ return true;
2540
+ }
2541
+
2542
+ export async function deleteBranch(directory, branch, options = {}) {
2543
+ const git = await createGit(directory);
2544
+
2545
+ try {
2546
+ const branchName = branch.startsWith('refs/heads/')
2547
+ ? branch.substring('refs/heads/'.length)
2548
+ : branch;
2549
+ const args = ['branch', options.force ? '-D' : '-d', branchName];
2550
+ await git.raw(args);
2551
+ return { success: true };
2552
+ } catch (error) {
2553
+ console.error('Failed to delete branch:', error);
2554
+ throw error;
2555
+ }
2556
+ }
2557
+
2558
+ export async function getLog(directory, options = {}) {
2559
+ const git = await createGit(directory);
2560
+
2561
+ try {
2562
+ const maxCount = options.maxCount || 50;
2563
+ const baseLog = await git.log({
2564
+ maxCount,
2565
+ from: options.from,
2566
+ to: options.to,
2567
+ file: options.file
2568
+ });
2569
+
2570
+ const logArgs = [
2571
+ 'log',
2572
+ `--max-count=${maxCount}`,
2573
+ '--date=iso',
2574
+ '--pretty=format:%H%x1f%an%x1f%ae%x1f%ad%x1f%s%x1e',
2575
+ '--shortstat'
2576
+ ];
2577
+
2578
+ if (options.from && options.to) {
2579
+ logArgs.push(`${options.from}..${options.to}`);
2580
+ } else if (options.from) {
2581
+ logArgs.push(`${options.from}..HEAD`);
2582
+ } else if (options.to) {
2583
+ logArgs.push(options.to);
2584
+ }
2585
+
2586
+ if (options.file) {
2587
+ logArgs.push('--', options.file);
2588
+ }
2589
+
2590
+ const rawLog = await git.raw(logArgs);
2591
+ const records = rawLog
2592
+ .split('\x1e')
2593
+ .map((entry) => entry.trim())
2594
+ .filter(Boolean);
2595
+
2596
+ const statsMap = new Map();
2597
+
2598
+ records.forEach((record) => {
2599
+ const lines = record.split('\n').filter((line) => line.trim().length > 0);
2600
+ const header = lines.shift() || '';
2601
+ const [hash] = header.split('\x1f');
2602
+ if (!hash) {
2603
+ return;
2604
+ }
2605
+
2606
+ let filesChanged = 0;
2607
+ let insertions = 0;
2608
+ let deletions = 0;
2609
+
2610
+ lines.forEach((line) => {
2611
+ const filesMatch = line.match(/(\d+)\s+files?\s+changed/);
2612
+ const insertMatch = line.match(/(\d+)\s+insertions?\(\+\)/);
2613
+ const deleteMatch = line.match(/(\d+)\s+deletions?\(-\)/);
2614
+
2615
+ if (filesMatch) {
2616
+ filesChanged = parseInt(filesMatch[1], 10);
2617
+ }
2618
+ if (insertMatch) {
2619
+ insertions = parseInt(insertMatch[1], 10);
2620
+ }
2621
+ if (deleteMatch) {
2622
+ deletions = parseInt(deleteMatch[1], 10);
2623
+ }
2624
+ });
2625
+
2626
+ statsMap.set(hash, { filesChanged, insertions, deletions });
2627
+ });
2628
+
2629
+ const merged = baseLog.all.map((entry) => {
2630
+ const stats = statsMap.get(entry.hash) || { filesChanged: 0, insertions: 0, deletions: 0 };
2631
+ return {
2632
+ hash: entry.hash,
2633
+ date: entry.date,
2634
+ message: entry.message,
2635
+ refs: entry.refs || '',
2636
+ body: entry.body || '',
2637
+ author_name: entry.author_name,
2638
+ author_email: entry.author_email,
2639
+ filesChanged: stats.filesChanged,
2640
+ insertions: stats.insertions,
2641
+ deletions: stats.deletions
2642
+ };
2643
+ });
2644
+
2645
+ return {
2646
+ all: merged,
2647
+ latest: merged[0] || null,
2648
+ total: baseLog.total
2649
+ };
2650
+ } catch (error) {
2651
+ console.error('Failed to get log:', error);
2652
+ throw error;
2653
+ }
2654
+ }
2655
+
2656
+ export async function isLinkedWorktree(directory) {
2657
+ const git = await createGit(directory);
2658
+ try {
2659
+ const [gitDir, gitCommonDir] = await Promise.all([
2660
+ git.raw(['rev-parse', '--git-dir']).then((output) => output.trim()),
2661
+ git.raw(['rev-parse', '--git-common-dir']).then((output) => output.trim())
2662
+ ]);
2663
+ return gitDir !== gitCommonDir;
2664
+ } catch (error) {
2665
+ console.error('Failed to determine worktree type:', error);
2666
+ return false;
2667
+ }
2668
+ }
2669
+
2670
+ export async function getCommitFiles(directory, commitHash) {
2671
+ const git = await createGit(directory);
2672
+
2673
+ try {
2674
+
2675
+ const numstatRaw = await git.raw([
2676
+ 'show',
2677
+ '--numstat',
2678
+ '--format=',
2679
+ commitHash
2680
+ ]);
2681
+
2682
+ const files = [];
2683
+ const lines = numstatRaw.trim().split('\n').filter(Boolean);
2684
+
2685
+ for (const line of lines) {
2686
+ const parts = line.split('\t');
2687
+ if (parts.length < 3) continue;
2688
+
2689
+ const [insertionsRaw, deletionsRaw, ...pathParts] = parts;
2690
+ const filePath = pathParts.join('\t');
2691
+ if (!filePath) continue;
2692
+
2693
+ const insertions = insertionsRaw === '-' ? 0 : parseInt(insertionsRaw, 10) || 0;
2694
+ const deletions = deletionsRaw === '-' ? 0 : parseInt(deletionsRaw, 10) || 0;
2695
+ const isBinary = insertionsRaw === '-' && deletionsRaw === '-';
2696
+
2697
+ let changeType = 'M';
2698
+ let displayPath = filePath;
2699
+
2700
+ if (filePath.includes(' => ')) {
2701
+ changeType = 'R';
2702
+
2703
+ const match = filePath.match(/(?:\{[^}]*\s=>\s[^}]*\}|.*\s=>\s.*)/);
2704
+ if (match) {
2705
+ displayPath = filePath;
2706
+ }
2707
+ }
2708
+
2709
+ files.push({
2710
+ path: displayPath,
2711
+ insertions,
2712
+ deletions,
2713
+ isBinary,
2714
+ changeType
2715
+ });
2716
+ }
2717
+
2718
+ const nameStatusRaw = await git.raw([
2719
+ 'show',
2720
+ '--name-status',
2721
+ '--format=',
2722
+ commitHash
2723
+ ]).catch(() => '');
2724
+
2725
+ const statusMap = new Map();
2726
+ const statusLines = nameStatusRaw.trim().split('\n').filter(Boolean);
2727
+ for (const line of statusLines) {
2728
+ const match = line.match(/^([AMDRC])\d*\t(.+)$/);
2729
+ if (match) {
2730
+ const [, status, path] = match;
2731
+ statusMap.set(path, status);
2732
+ }
2733
+ }
2734
+
2735
+ for (const file of files) {
2736
+ const basePath = file.path.includes(' => ')
2737
+ ? file.path.split(' => ').pop()?.replace(/[{}]/g, '') || file.path
2738
+ : file.path;
2739
+
2740
+ const status = statusMap.get(basePath) || statusMap.get(file.path);
2741
+ if (status) {
2742
+ file.changeType = status;
2743
+ }
2744
+ }
2745
+
2746
+ return { files };
2747
+ } catch (error) {
2748
+ console.error('Failed to get commit files:', error);
2749
+ throw error;
2750
+ }
2751
+ }
2752
+
2753
+ export async function renameBranch(directory, oldName, newName) {
2754
+ const git = await createGit(directory);
2755
+
2756
+ try {
2757
+ const normalizedOldName = cleanBranchName(String(oldName || '').trim());
2758
+ const normalizedNewName = cleanBranchName(String(newName || '').trim());
2759
+
2760
+ const previousRemote = await git
2761
+ .raw(['config', '--get', `branch.${normalizedOldName}.remote`])
2762
+ .then((value) => String(value || '').trim())
2763
+ .catch(() => '');
2764
+ const previousMerge = await git
2765
+ .raw(['config', '--get', `branch.${normalizedOldName}.merge`])
2766
+ .then((value) => String(value || '').trim())
2767
+ .catch(() => '');
2768
+
2769
+ // Use git branch -m command to rename the branch
2770
+ await git.raw(['branch', '-m', oldName, newName]);
2771
+
2772
+ if (previousRemote && previousMerge && normalizedNewName) {
2773
+ const previousMergeBranch = cleanBranchName(previousMerge);
2774
+ const nextMergeBranch =
2775
+ previousMergeBranch === normalizedOldName
2776
+ ? normalizedNewName
2777
+ : previousMergeBranch;
2778
+ const upstream = normalizeUpstreamTarget(previousRemote, nextMergeBranch);
2779
+
2780
+ if (upstream) {
2781
+ try {
2782
+ await runGitCommandOrThrow(
2783
+ directory,
2784
+ ['branch', `--set-upstream-to=${upstream.full}`, normalizedNewName],
2785
+ `Failed to set upstream to ${upstream.full}`
2786
+ );
2787
+ } catch {
2788
+ await setBranchTrackingFallback(directory, normalizedNewName, upstream);
2789
+ }
2790
+ }
2791
+ }
2792
+
2793
+ return { success: true, branch: newName };
2794
+ } catch (error) {
2795
+ console.error('Failed to rename branch:', error);
2796
+ throw error;
2797
+ }
2798
+ }
2799
+
2800
+ export async function getRemotes(directory) {
2801
+ const git = await createGit(directory);
2802
+
2803
+ try {
2804
+ const remotes = await git.getRemotes(true);
2805
+
2806
+ return remotes.map((remote) => ({
2807
+ name: remote.name,
2808
+ fetchUrl: remote.refs.fetch,
2809
+ pushUrl: remote.refs.push
2810
+ }));
2811
+ } catch (error) {
2812
+ console.error('Failed to get remotes:', error);
2813
+ throw error;
2814
+ }
2815
+ }
2816
+
2817
+ export async function removeRemote(directory, options = {}) {
2818
+ const remoteName = String(options.remote || '').trim();
2819
+ if (!remoteName) {
2820
+ throw new Error('remote is required to remove a remote');
2821
+ }
2822
+ if (remoteName === 'origin') {
2823
+ throw new Error('Cannot remove origin remote');
2824
+ }
2825
+
2826
+ const git = await createGit(directory);
2827
+
2828
+ try {
2829
+ await git.removeRemote(remoteName);
2830
+ return { success: true };
2831
+ } catch (error) {
2832
+ console.error('Failed to remove remote:', error);
2833
+ throw error;
2834
+ }
2835
+ }
2836
+
2837
+ export async function rebase(directory, options = {}) {
2838
+ const git = await createGit(directory);
2839
+
2840
+ try {
2841
+ const { onto } = options;
2842
+ if (!onto) {
2843
+ throw new Error('onto parameter is required for rebase');
2844
+ }
2845
+
2846
+ await git.rebase([onto]);
2847
+
2848
+ return {
2849
+ success: true,
2850
+ conflict: false
2851
+ };
2852
+ } catch (error) {
2853
+ const errorMessage = String(error?.message || error || '').toLowerCase();
2854
+ const isConflict = errorMessage.includes('conflict') ||
2855
+ errorMessage.includes('could not apply') ||
2856
+ errorMessage.includes('merge conflict');
2857
+
2858
+ if (isConflict) {
2859
+ // Get list of conflicted files
2860
+ const status = await git.status().catch(() => ({ conflicted: [] }));
2861
+ return {
2862
+ success: false,
2863
+ conflict: true,
2864
+ conflictFiles: status.conflicted || []
2865
+ };
2866
+ }
2867
+
2868
+ console.error('Failed to rebase:', error);
2869
+ throw error;
2870
+ }
2871
+ }
2872
+
2873
+ export async function abortRebase(directory) {
2874
+ const git = await createGit(directory);
2875
+
2876
+ try {
2877
+ await git.rebase(['--abort']);
2878
+ return { success: true };
2879
+ } catch (error) {
2880
+ console.error('Failed to abort rebase:', error);
2881
+ throw error;
2882
+ }
2883
+ }
2884
+
2885
+ export async function merge(directory, options = {}) {
2886
+ const git = await createGit(directory);
2887
+
2888
+ try {
2889
+ const { branch } = options;
2890
+ if (!branch) {
2891
+ throw new Error('branch parameter is required for merge');
2892
+ }
2893
+
2894
+ await git.merge([branch]);
2895
+
2896
+ return {
2897
+ success: true,
2898
+ conflict: false
2899
+ };
2900
+ } catch (error) {
2901
+ const errorMessage = String(error?.message || error || '').toLowerCase();
2902
+ const isConflict = errorMessage.includes('conflict') ||
2903
+ errorMessage.includes('merge conflict') ||
2904
+ errorMessage.includes('automatic merge failed');
2905
+
2906
+ if (isConflict) {
2907
+ // Get list of conflicted files
2908
+ const status = await git.status().catch(() => ({ conflicted: [] }));
2909
+ return {
2910
+ success: false,
2911
+ conflict: true,
2912
+ conflictFiles: status.conflicted || []
2913
+ };
2914
+ }
2915
+
2916
+ console.error('Failed to merge:', error);
2917
+ throw error;
2918
+ }
2919
+ }
2920
+
2921
+ export async function abortMerge(directory) {
2922
+ const git = await createGit(directory);
2923
+
2924
+ try {
2925
+ await git.merge(['--abort']);
2926
+ return { success: true };
2927
+ } catch (error) {
2928
+ console.error('Failed to abort merge:', error);
2929
+ throw error;
2930
+ }
2931
+ }
2932
+
2933
+ export async function continueRebase(directory) {
2934
+ const directoryPath = normalizeDirectoryPath(directory);
2935
+ const git = await createGit(directoryPath);
2936
+
2937
+ try {
2938
+ // Set GIT_EDITOR to prevent editor prompts
2939
+ await git.env('GIT_EDITOR', 'true').rebase(['--continue']);
2940
+ return { success: true, conflict: false };
2941
+ } catch (error) {
2942
+ const errorMessage = String(error?.message || error || '').toLowerCase();
2943
+ const isConflict = errorMessage.includes('conflict') ||
2944
+ errorMessage.includes('needs merge') ||
2945
+ errorMessage.includes('unmerged') ||
2946
+ errorMessage.includes('fix conflicts');
2947
+
2948
+ if (isConflict) {
2949
+ const status = await git.status().catch(() => ({ conflicted: [] }));
2950
+ return {
2951
+ success: false,
2952
+ conflict: true,
2953
+ conflictFiles: status.conflicted || []
2954
+ };
2955
+ }
2956
+
2957
+ // Check for "nothing to commit" which means rebase step is complete
2958
+ if (errorMessage.includes('nothing to commit') || errorMessage.includes('no changes')) {
2959
+ // Skip this commit and continue
2960
+ try {
2961
+ await git.env('GIT_EDITOR', 'true').rebase(['--skip']);
2962
+ return { success: true, conflict: false };
2963
+ } catch {
2964
+ // If skip also fails, the rebase may be complete
2965
+ return { success: true, conflict: false };
2966
+ }
2967
+ }
2968
+
2969
+ console.error('Failed to continue rebase:', error);
2970
+ throw error;
2971
+ }
2972
+ }
2973
+
2974
+ export async function continueMerge(directory) {
2975
+ const directoryPath = normalizeDirectoryPath(directory);
2976
+ const git = await createGit(directoryPath);
2977
+
2978
+ try {
2979
+ // Check if there are still unmerged files
2980
+ const status = await git.status();
2981
+ if (status.conflicted && status.conflicted.length > 0) {
2982
+ return {
2983
+ success: false,
2984
+ conflict: true,
2985
+ conflictFiles: status.conflicted
2986
+ };
2987
+ }
2988
+
2989
+ // For merge, we commit after resolving conflicts
2990
+ // Use --no-edit to use the default merge commit message
2991
+ await git.env('GIT_EDITOR', 'true').commit([], { '--no-edit': null });
2992
+ return { success: true, conflict: false };
2993
+ } catch (error) {
2994
+ const errorMessage = String(error?.message || error || '').toLowerCase();
2995
+ const isConflict = errorMessage.includes('conflict') ||
2996
+ errorMessage.includes('needs merge') ||
2997
+ errorMessage.includes('unmerged') ||
2998
+ errorMessage.includes('fix conflicts');
2999
+
3000
+ if (isConflict) {
3001
+ const status = await git.status().catch(() => ({ conflicted: [] }));
3002
+ return {
3003
+ success: false,
3004
+ conflict: true,
3005
+ conflictFiles: status.conflicted || []
3006
+ };
3007
+ }
3008
+
3009
+ // "nothing to commit" can happen if all conflicts resolved to one side
3010
+ if (errorMessage.includes('nothing to commit') || errorMessage.includes('no changes added')) {
3011
+ // The merge is effectively complete (all changes already committed or no changes needed)
3012
+ return { success: true, conflict: false };
3013
+ }
3014
+
3015
+ console.error('Failed to continue merge:', error);
3016
+ throw error;
3017
+ }
3018
+ }
3019
+
3020
+ export async function getConflictDetails(directory) {
3021
+ const directoryPath = normalizeDirectoryPath(directory);
3022
+ const git = await createGit(directoryPath);
3023
+
3024
+ try {
3025
+ // Get git status --porcelain
3026
+ const statusPorcelain = await git.raw(['status', '--porcelain']).catch(() => '');
3027
+
3028
+ // Get unmerged files
3029
+ const unmergedFilesRaw = await git.raw(['diff', '--name-only', '--diff-filter=U']).catch(() => '');
3030
+ const unmergedFiles = unmergedFilesRaw
3031
+ .split('\n')
3032
+ .map((line) => line.trim())
3033
+ .filter(Boolean);
3034
+
3035
+ // Get current diff
3036
+ const diff = await git.raw(['diff']).catch(() => '');
3037
+
3038
+ // Detect operation type and get head info
3039
+ let operation = 'merge';
3040
+ let headInfo = '';
3041
+
3042
+ // Check for MERGE_HEAD (merge in progress)
3043
+ const mergeHeadExists = await git
3044
+ .raw(['rev-parse', '--verify', '--quiet', 'MERGE_HEAD'])
3045
+ .then(() => true)
3046
+ .catch(() => false);
3047
+
3048
+ if (mergeHeadExists) {
3049
+ operation = 'merge';
3050
+ const mergeHead = await git.raw(['rev-parse', 'MERGE_HEAD']).catch(() => '');
3051
+ const mergeMsg = await fsp
3052
+ .readFile(path.join(directoryPath, '.git', 'MERGE_MSG'), 'utf8')
3053
+ .catch(() => '');
3054
+ headInfo = `MERGE_HEAD: ${mergeHead.trim()}\n${mergeMsg}`;
3055
+ } else {
3056
+ // Check for REBASE_HEAD (rebase in progress)
3057
+ const rebaseHeadExists = await git
3058
+ .raw(['rev-parse', '--verify', '--quiet', 'REBASE_HEAD'])
3059
+ .then(() => true)
3060
+ .catch(() => false);
3061
+
3062
+ if (rebaseHeadExists) {
3063
+ operation = 'rebase';
3064
+ const rebaseHead = await git.raw(['rev-parse', 'REBASE_HEAD']).catch(() => '');
3065
+ headInfo = `REBASE_HEAD: ${rebaseHead.trim()}`;
3066
+ }
3067
+ }
3068
+
3069
+ return {
3070
+ statusPorcelain: statusPorcelain.trim(),
3071
+ unmergedFiles,
3072
+ diff: diff.trim(),
3073
+ headInfo: headInfo.trim(),
3074
+ operation,
3075
+ };
3076
+ } catch (error) {
3077
+ console.error('Failed to get conflict details:', error);
3078
+ throw error;
3079
+ }
3080
+ }
3081
+
3082
+ // ============== Stash Operations ==============
3083
+
3084
+ export async function stash(directory, options = {}) {
3085
+ const git = await createGit(directory);
3086
+
3087
+ try {
3088
+ const args = ['stash', 'push'];
3089
+
3090
+ // Include untracked files by default
3091
+ if (options.includeUntracked !== false) {
3092
+ args.push('--include-untracked');
3093
+ }
3094
+
3095
+ if (options.message) {
3096
+ args.push('-m', options.message);
3097
+ }
3098
+
3099
+ await git.raw(args);
3100
+ return { success: true };
3101
+ } catch (error) {
3102
+ console.error('Failed to stash:', error);
3103
+ throw error;
3104
+ }
3105
+ }
3106
+
3107
+ export async function stashPop(directory) {
3108
+ const git = await createGit(directory);
3109
+
3110
+ try {
3111
+ await git.raw(['stash', 'pop']);
3112
+ return { success: true };
3113
+ } catch (error) {
3114
+ console.error('Failed to pop stash:', error);
3115
+ throw error;
3116
+ }
3117
+ }