Skip to content

Commit 9de86eb

Browse files
committed
get_git_root_path: support worktrees
1 parent 6b7dea4 commit 9de86eb

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

lib/fontv/utilities.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,18 @@ def get_git_root_path(filepath):
4444

4545
# search up to five directories above for the git repo root
4646
for _ in range(6):
47-
if dir_exists(os.path.join(gitroot_path, ".git")):
47+
dot_git = os.path.join(gitroot_path, ".git")
48+
if dir_exists(dot_git):
4849
return gitroot_path
50+
elif file_exists(dot_git):
51+
# Could be a worktree, in which case .git is a file with content like:
52+
# gitdir: /home/atk/Tools/font-v/.git/worktrees/work-worktrees-work
53+
with open(dot_git, "r") as dot_git:
54+
line = dot_git.readline()
55+
# "gitdir: font-v/.git/worktrees/work-worktrees-work" -> "font-v/.git/worktrees/work-worktrees-work"
56+
worktree_path = line.split(" ", 2)[1]
57+
# "font-v/.git/worktrees/work-worktrees-work" -> "font-v/.git"
58+
gitroot_path = worktree_path.split("/worktrees/", 2)[0]
4959
gitroot_path = os.path.dirname(gitroot_path)
5060

5161
raise IOError(

0 commit comments

Comments
 (0)