MCPcopy Create free account
hub / github.com/tiann/hapi / readWorktreeFromGit

Function readWorktreeFromGit

cli/src/utils/worktreeEnv.ts:38–86  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

36}
37
38function 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
88function runGit(args: string[], cwd: string): string | null {
89 try {

Callers 1

readWorktreeEnvFunction · 0.85

Calls 6

getInvokedCwdFunction · 0.90
basenameFunction · 0.85
readCreatedAtFunction · 0.85
debugMethod · 0.80
runGitFunction · 0.70
normalizePathFunction · 0.70

Tested by

no test coverage detected