Skip to content

Commit 170ef0e

Browse files
committed
fix(read): use git rev-parse --git-dir for worktree support
Replace custom .git detection with git rev-parse --git-dir. This correctly resolves the git directory in worktrees where .git is a file pointing to the actual git directory.
1 parent 5862227 commit 170ef0e

1 file changed

Lines changed: 11 additions & 13 deletions

File tree

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import path from "node:path";
2-
import { Stats } from "node:fs";
3-
import fs from "fs/promises";
2+
import { execFile } from "node:child_process";
3+
import { promisify } from "node:util";
4+
5+
const execFileAsync = promisify(execFile);
46

57
// Get path to recently edited commit message file
68
export async function getEditFilePath(
@@ -11,16 +13,12 @@ export async function getEditFilePath(
1113
return path.resolve(top, edit);
1214
}
1315

14-
const dotgitPath = path.join(top, ".git");
15-
const dotgitStats: Stats = await fs.lstat(dotgitPath);
16-
17-
if (dotgitStats.isDirectory()) {
18-
return path.join(top, ".git/COMMIT_EDITMSG");
19-
}
20-
21-
const gitFile: string = await fs.readFile(dotgitPath, {
22-
encoding: "utf-8",
16+
// Use git rev-parse --git-dir to get the correct git directory
17+
// This handles worktrees, submodules, and regular repositories correctly
18+
const { stdout } = await execFileAsync("git", ["rev-parse", "--git-dir"], {
19+
cwd: top,
2320
});
24-
const relativeGitPath = gitFile.replace("gitdir: ", "").replace("\n", "");
25-
return path.resolve(top, relativeGitPath, "COMMIT_EDITMSG");
21+
22+
const gitDir = stdout.trim();
23+
return path.resolve(top, gitDir, "COMMIT_EDITMSG");
2624
}

0 commit comments

Comments
 (0)