Skip to content

Commit f0698bb

Browse files
committed
Rework get_main_worktree_path
1 parent 1ff3f96 commit f0698bb

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

git_machete/git_operations.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -635,15 +635,19 @@ def get_main_worktree_path(self) -> str:
635635
# If worktrees aren't supported, just return the current root dir
636636
return self.get_root_dir()
637637

638+
# We can't rely on `git rev-parse --git-common-dir`
639+
# since in some earlier supported versions of git
640+
# this path is apparently printed in a faulty way when dealing with worktrees.
641+
# Let's parse the output of of `git worktree list` instead.
638642
result = self._popen_git("worktree", "list", "--porcelain")
639643

640644
# The first worktree in the list is always the main worktree
641-
for line in result.stdout.strip().splitlines():
642-
if line.startswith('worktree '): # pragma: no branch
643-
return line[len('worktree '):]
644-
645-
# Fallback: if no worktrees found, return current root dir
646-
return self.get_root_dir() # pragma: no cover
645+
first_entry = result.stdout.splitlines()[0]
646+
if first_entry.startswith('worktree '):
647+
return first_entry[len('worktree '):]
648+
else:
649+
raise UnexpectedMacheteException("Could not obtain the main worktree path from "
650+
f"`git worktree list --porcelain` output (first line: `{first_entry}`)")
647651

648652
def checkout(self, branch: LocalBranchShortName) -> None:
649653
self._run_git("checkout", "--quiet", branch, "--", flush_caches=True)

0 commit comments

Comments
 (0)