Skip to content

Commit 29d15de

Browse files
mrogaskiasottile
authored andcommitted
git: changed rev-parse option for Git 2.25 changes to --show-toplevel
Git 2.25 introduced a change to "rev-parse --show-toplevel" that exposed underlying volumes for Windows drives mapped with SUBST. We use "rev-parse --show-cdup" to get the appropriate path, but must perform an extra check to see if we are in the .git directory.
1 parent cf604f6 commit 29d15de

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

pre_commit/git.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,26 @@ def no_git_env(
4747

4848

4949
def get_root() -> str:
50+
# Git 2.25 introduced a change to "rev-parse --show-toplevel" that exposed
51+
# underlying volumes for Windows drives mapped with SUBST. We use
52+
# "rev-parse --show-cdup" to get the appropriate path, but must perform
53+
# an extra check to see if we are in the .git directory.
5054
try:
51-
root = cmd_output('git', 'rev-parse', '--show-toplevel')[1].strip()
55+
root = os.path.realpath(
56+
cmd_output('git', 'rev-parse', '--show-cdup')[1].strip(),
57+
)
58+
git_dir = os.path.realpath(get_git_dir())
5259
except CalledProcessError:
5360
raise FatalError(
5461
'git failed. Is it installed, and are you in a Git repository '
5562
'directory?',
5663
)
57-
else:
58-
if root == '': # pragma: no cover (old git)
59-
raise FatalError(
60-
'git toplevel unexpectedly empty! make sure you are not '
61-
'inside the `.git` directory of your repository.',
62-
)
63-
else:
64-
return root
64+
if os.path.commonpath((root, git_dir)) == git_dir:
65+
raise FatalError(
66+
'git toplevel unexpectedly empty! make sure you are not '
67+
'inside the `.git` directory of your repository.',
68+
)
69+
return root
6570

6671

6772
def get_git_dir(git_root: str = '.') -> str:

0 commit comments

Comments
 (0)