()
| 36 | } |
| 37 | |
| 38 | function readWorktreeFromGit(): WorktreeInfo | null { |
| 39 | const start = Date.now(); |
| 40 | let result: WorktreeInfo | null = null; |
| 41 | |
| 42 | try { |
| 43 | const cwd = getInvokedCwd(); |
| 44 | const isInside = runGit(['rev-parse', '--is-inside-work-tree'], cwd); |
| 45 | if (isInside !== 'true') { |
| 46 | return null; |
| 47 | } |
| 48 | |
| 49 | const gitDir = runGit(['rev-parse', '--git-dir'], cwd); |
| 50 | const gitCommonDir = runGit(['rev-parse', '--git-common-dir'], cwd); |
| 51 | if (!gitDir || !gitCommonDir) { |
| 52 | return null; |
| 53 | } |
| 54 | |
| 55 | const resolvedGitDir = normalizePath(gitDir, cwd); |
| 56 | const resolvedGitCommonDir = normalizePath(gitCommonDir, cwd); |
| 57 | if (resolvedGitDir === resolvedGitCommonDir) { |
| 58 | return null; |
| 59 | } |
| 60 | |
| 61 | const worktreeRoot = runGit(['rev-parse', '--show-toplevel'], cwd); |
| 62 | if (!worktreeRoot) { |
| 63 | return null; |
| 64 | } |
| 65 | const worktreePath = normalizePath(worktreeRoot, cwd); |
| 66 | const basePath = dirname(resolvedGitCommonDir); |
| 67 | |
| 68 | const branch = runGit(['symbolic-ref', '--short', 'HEAD'], cwd) |
| 69 | ?? runGit(['rev-parse', '--short', 'HEAD'], cwd); |
| 70 | if (!branch) { |
| 71 | return null; |
| 72 | } |
| 73 | |
| 74 | result = { |
| 75 | basePath, |
| 76 | branch, |
| 77 | name: basename(worktreePath), |
| 78 | worktreePath, |
| 79 | createdAt: readCreatedAt(worktreePath) |
| 80 | }; |
| 81 | return result; |
| 82 | } finally { |
| 83 | const elapsedMs = Date.now() - start; |
| 84 | logger.debug(`[WORKTREE] Git probe ${result ? 'hit' : 'miss'} in ${elapsedMs}ms`); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | function runGit(args: string[], cwd: string): string | null { |
| 89 | try { |
no test coverage detected