From 29d15de38ee0f78f598e8888fd3d6e8789a63bef Mon Sep 17 00:00:00 2001 From: Mark Rogaski Date: Sun, 6 Dec 2020 22:57:31 -0500 Subject: [PATCH] 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. --- pre_commit/git.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/pre_commit/git.py b/pre_commit/git.py index 156e53d2..50962745 100644 --- a/pre_commit/git.py +++ b/pre_commit/git.py @@ -47,21 +47,26 @@ def no_git_env( def get_root() -> str: + # 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. try: - root = cmd_output('git', 'rev-parse', '--show-toplevel')[1].strip() + root = os.path.realpath( + cmd_output('git', 'rev-parse', '--show-cdup')[1].strip(), + ) + git_dir = os.path.realpath(get_git_dir()) except CalledProcessError: raise FatalError( 'git failed. Is it installed, and are you in a Git repository ' 'directory?', ) - else: - if root == '': # pragma: no cover (old git) - raise FatalError( - 'git toplevel unexpectedly empty! make sure you are not ' - 'inside the `.git` directory of your repository.', - ) - else: - return root + if os.path.commonpath((root, git_dir)) == git_dir: + raise FatalError( + 'git toplevel unexpectedly empty! make sure you are not ' + 'inside the `.git` directory of your repository.', + ) + return root def get_git_dir(git_root: str = '.') -> str: