Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3e11c2d

Browse files
authored
Merge pull request #832 from pre-commit/rev_parse_error_mode
Fix rev-parse for older git versions
2 parents 4b0a22a + 18b6f4b commit 3e11c2d

1 file changed

Lines changed: 7 additions & 10 deletions

File tree

pre_commit/git.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,13 @@ def get_root():
3131

3232

3333
def get_git_dir(git_root):
34-
def _git_dir(opt):
35-
return os.path.normpath(os.path.join(
36-
git_root,
37-
cmd_output('git', 'rev-parse', opt, cwd=git_root)[1].strip(),
38-
))
39-
40-
try:
41-
return _git_dir('--git-common-dir')
42-
except CalledProcessError: # pragma: no cover (git < 2.5)
43-
return _git_dir('--git-dir')
34+
opts = ('--git-common-dir', '--git-dir')
35+
_, out, _ = cmd_output('git', 'rev-parse', *opts, cwd=git_root)
36+
for line, opt in zip(out.splitlines(), opts):
37+
if line != opt: # pragma: no branch (git < 2.5)
38+
return os.path.normpath(os.path.join(git_root, line))
39+
else:
40+
raise AssertionError('unreachable: no git dir')
4441

4542

4643
def get_remote_url(git_root):

0 commit comments

Comments
 (0)