1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332import { spawnSync } from 'child_process';
import { existsSync, readFileSync, statSync } from 'fs';
import * as path from 'path';
import { WorktreeInfo } from '../types/index.js';
/**
* Worktree Resolver
*
* Discovers and resolves worktree information from various input patterns:
* 1. Explicit path
* 2. Name-based lookup
* 3. Auto-detect from current directory
*/
export class WorktreeResolver {
/**
* Resolve worktree from path, name, or auto-detect
*
* @param inputPath - Optional explicit path to worktree
* @param name - Optional worktree name to search for
* @returns WorktreeInfo
* @throws Error if worktree cannot be resolved
*/
async resolve(inputPath?: string, name?: string): Promise<WorktreeInfo> {
// Pattern 1: Explicit path provided
if (inputPath) {
return this.resolveByPath(inputPath);
}
// Pattern 2: Name-based lookup
if (name) {
return this.resolveByName(name);
}
// Pattern 3: Auto-detect from current directory
return this.autoDetect();
}
/**
* Get all worktrees for a repository
*
* @param repoRoot - Repository root path
* @returns Array of WorktreeInfo
*/
async getWorktrees(repoRoot: string): Promise<WorktreeInfo[]> {
const result = spawnSync('git', ['worktree', 'list', '--porcelain'], {
cwd: repoRoot,
encoding: 'utf8',
stdio: 'pipe'
});
if (result.status !== 0) {
throw new Error('Failed to list worktrees');
}
return this.parseWorktreeList(result.stdout);
}
/**
* Find worktree by branch name
*
* @param branchName - Branch name to search for (partial match)
* @returns WorktreeInfo or null if not found
*/
async findByBranchName(branchName: string): Promise<WorktreeInfo | null> {
// Find repo root from current directory
const repoRoot = await this.findRepoRoot(process.cwd());
if (!repoRoot) {
throw new Error('Not inside a Git repository');
}
const worktrees = await this.getWorktrees(repoRoot);
// Find worktree with matching branch
for (const worktree of worktrees) {
if (worktree.branchName.includes(branchName)) {
return worktree;
}
}
return null;
}
/**
* Check if directory is a worktree
*
* @param directory - Directory path to check
* @returns True if it's a worktree
*/
async isWorktree(directory: string): Promise<boolean> {
const gitPath = path.join(directory, '.git');
if (!existsSync(gitPath)) {
return false;
}
try {
// Read .git file
const gitFile = readFileSync(gitPath, 'utf8').trim();
// Worktrees have .git file pointing to gitdir
// Main repos have .git directory
return gitFile.startsWith('gitdir:');
} catch {
return false;
}
}
/**
* Get main repository path from worktree
*
* @param worktreePath - Path to worktree
* @returns Main repository path
*/
async getMainRepo(worktreePath: string): Promise<string> {
const gitPath = path.join(worktreePath, '.git');
if (!existsSync(gitPath)) {
throw new Error('Not a Git repository or worktree');
}
// Check if .git is a file (worktree) or directory (main repo)
const stats = statSync(gitPath);
if (stats.isDirectory()) {
// This is the main repository (.git is a directory)
return worktreePath;
}
// .git is a file - read it to find main repo
const gitFile = readFileSync(gitPath, 'utf8').trim();
if (gitFile.startsWith('gitdir:')) {
// This is a worktree
// .git file contains: gitdir: /path/to/main/repo/.git/worktrees/name
const match = gitFile.match(/^gitdir: (.+)$/);
if (match) {
const gitDir = path.resolve(worktreePath, match[1]);
// Navigate up: .git/worktrees/name -> .git -> main repo
const mainRepoGitDir = path.dirname(path.dirname(gitDir));
const mainRepoPath = path.dirname(mainRepoGitDir);
if (existsSync(mainRepoPath)) {
return mainRepoPath;
}
}
}
// Fallback: assume current path is main repo
return worktreePath;
}
/**
* Resolve worktree by explicit path
*/
private async resolveByPath(inputPath: string): Promise<WorktreeInfo> {
const absolutePath = path.resolve(inputPath);
if (!existsSync(absolutePath)) {
throw new Error(`Worktree not found at path: ${absolutePath}`);
}
// Check if it's a worktree or main repo (used for directory structure)
const _isWorktree = await this.isWorktree(absolutePath);
const mainRepo = await this.getMainRepo(absolutePath);
// Get all worktrees to find this one
const worktrees = await this.getWorktrees(mainRepo);
// Find matching worktree
const matching = worktrees.find(w => w.path === absolutePath);
if (!matching) {
throw new Error(`Could not find worktree information for: ${absolutePath}`);
}
return matching;
}
/**
* Resolve worktree by name (search all worktrees)
*/
private async resolveByName(name: string): Promise<WorktreeInfo> {
// Find repo root
const repoRoot = await this.findRepoRoot(process.cwd());
if (!repoRoot) {
throw new Error('Not inside a Git repository');
}
// Get all worktrees
const worktrees = await this.getWorktrees(repoRoot);
// Search for matching branch name
const matches = worktrees.filter(w =>
w.branchName.includes(name) || w.path.includes(name)
);
if (matches.length === 0) {
throw new Error(`No worktree found with name: ${name}`);
}
if (matches.length > 1) {
const branchNames = matches.map(w => w.branchName).join(', ');
throw new Error(
`Multiple worktrees match "${name}": ${branchNames}. Please be more specific.`
);
}
return matches[0];
}
/**
* Auto-detect worktree from current directory
*/
private async autoDetect(): Promise<WorktreeInfo> {
const cwd = process.cwd();
// Check if current directory is a worktree
const isWorktree = await this.isWorktree(cwd);
if (!isWorktree) {
// Check if we're inside a worktree directory
const repoRoot = await this.findRepoRoot(cwd);
if (!repoRoot) {
throw new Error('Not inside a Git repository or worktree');
}
// If repo root is current directory, it's the main repo
if (repoRoot === cwd) {
throw new Error('Current directory is the main repository, not a worktree');
}
// We're inside a worktree, resolve it
return this.resolveByPath(repoRoot);
}
// Current directory is a worktree
return this.resolveByPath(cwd);
}
/**
* Find repository root by walking up directory tree
*
* @param startDir - Starting directory
* @returns Repository root or null if not found
*/
private async findRepoRoot(startDir: string): Promise<string | null> {
let currentDir = startDir;
while (currentDir !== path.dirname(currentDir)) {
const gitPath = path.join(currentDir, '.git');
if (existsSync(gitPath)) {
return currentDir;
}
currentDir = path.dirname(currentDir);
}
return null;
}
/**
* Parse git worktree list --porcelain output
*
* Format:
* worktree /path/to/worktree
* HEAD abc123def456
* branch refs/heads/branch-name
* locked reason (optional)
*
* @param output - Output from git worktree list --porcelain
* @returns Array of WorktreeInfo
*/
private parseWorktreeList(output: string): WorktreeInfo[] {
const worktrees: WorktreeInfo[] = [];
const lines = output.split('\n').map(l => l.trim()).filter(l => l !== '');
let current: Partial<WorktreeInfo> = {};
for (const line of lines) {
if (line.startsWith('worktree ')) {
// Start of new worktree entry
if (current.path) {
// Save previous entry
worktrees.push(this.completeWorktreeInfo(current));
}
current = {
path: line.substring('worktree '.length),
isLocked: false,
isPrunable: false
};
} else if (line.startsWith('HEAD ')) {
current.commitHash = line.substring('HEAD '.length);
} else if (line.startsWith('branch ')) {
const branchRef = line.substring('branch '.length);
// Convert refs/heads/branch-name to branch-name
current.branchName = branchRef.replace('refs/heads/', '');
} else if (line.startsWith('bare')) {
// Bare repository (main repo)
current.isMainRepo = true;
} else if (line.startsWith('locked')) {
current.isLocked = true;
} else if (line.startsWith('prunable')) {
current.isPrunable = true;
}
}
// Save last entry
if (current.path) {
worktrees.push(this.completeWorktreeInfo(current));
}
return worktrees;
}
/**
* Complete partial worktree info with defaults
*/
private completeWorktreeInfo(partial: Partial<WorktreeInfo>): WorktreeInfo {
return {
path: partial.path || '',
branchName: partial.branchName || 'HEAD',
isMainRepo: partial.isMainRepo || false,
commitHash: partial.commitHash || '',
remoteUrl: partial.remoteUrl,
isLocked: partial.isLocked || false,
isPrunable: partial.isPrunable || false
};
}
}