Skip to content

Commit

Permalink
Fix rev-parse for older git versions
Browse files Browse the repository at this point in the history
  • Loading branch information
asottile committed Sep 22, 2018
1 parent 4b0a22a commit 18b6f4b
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions pre_commit/git.py
Expand Up @@ -31,16 +31,13 @@ def get_root():


def get_git_dir(git_root):
def _git_dir(opt):
return os.path.normpath(os.path.join(
git_root,
cmd_output('git', 'rev-parse', opt, cwd=git_root)[1].strip(),
))

try:
return _git_dir('--git-common-dir')
except CalledProcessError: # pragma: no cover (git < 2.5)
return _git_dir('--git-dir')
opts = ('--git-common-dir', '--git-dir')
_, out, _ = cmd_output('git', 'rev-parse', *opts, cwd=git_root)
for line, opt in zip(out.splitlines(), opts):
if line != opt: # pragma: no branch (git < 2.5)
return os.path.normpath(os.path.join(git_root, line))
else:
raise AssertionError('unreachable: no git dir')


def get_remote_url(git_root):
Expand Down

0 comments on commit 18b6f4b

Please sign in to comment.