mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-04-16 02:21:46 +04:00
Split get_git_dir() into get_git_dir() and get_git_common_dir()
This fixes the conflicted state check when using work trees. #1972
This commit is contained in:
parent
835f9c65e9
commit
eba15d128b
2 changed files with 18 additions and 7 deletions
|
|
@ -40,7 +40,7 @@ def _hook_paths(
|
||||||
hook_type: str,
|
hook_type: str,
|
||||||
git_dir: Optional[str] = None,
|
git_dir: Optional[str] = None,
|
||||||
) -> Tuple[str, str]:
|
) -> Tuple[str, str]:
|
||||||
git_dir = git_dir if git_dir is not None else git.get_git_dir()
|
git_dir = git_dir if git_dir is not None else git.get_git_common_dir()
|
||||||
pth = os.path.join(git_dir, 'hooks', hook_type)
|
pth = os.path.join(git_dir, 'hooks', hook_type)
|
||||||
return pth, f'{pth}.legacy'
|
return pth, f'{pth}.legacy'
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -55,7 +55,7 @@ def get_root() -> str:
|
||||||
root = os.path.abspath(
|
root = os.path.abspath(
|
||||||
cmd_output('git', 'rev-parse', '--show-cdup')[1].strip(),
|
cmd_output('git', 'rev-parse', '--show-cdup')[1].strip(),
|
||||||
)
|
)
|
||||||
git_dir = os.path.abspath(get_git_dir())
|
git_dir = os.path.abspath(get_git_common_dir())
|
||||||
except CalledProcessError:
|
except CalledProcessError:
|
||||||
raise FatalError(
|
raise FatalError(
|
||||||
'git failed. Is it installed, and are you in a Git repository '
|
'git failed. Is it installed, and are you in a Git repository '
|
||||||
|
|
@ -70,15 +70,26 @@ def get_root() -> str:
|
||||||
|
|
||||||
|
|
||||||
def get_git_dir(git_root: str = '.') -> str:
|
def get_git_dir(git_root: str = '.') -> str:
|
||||||
opts = ('--git-common-dir', '--git-dir')
|
opt = '--git-dir'
|
||||||
_, out, _ = cmd_output('git', 'rev-parse', *opts, cwd=git_root)
|
_, out, _ = cmd_output('git', 'rev-parse', opt, cwd=git_root)
|
||||||
for line, opt in zip(out.splitlines(), opts):
|
git_dir = out.strip()
|
||||||
if line != opt: # pragma: no branch (git < 2.5)
|
if git_dir != opt:
|
||||||
return os.path.normpath(os.path.join(git_root, line))
|
return os.path.normpath(os.path.join(git_root, git_dir))
|
||||||
else:
|
else:
|
||||||
raise AssertionError('unreachable: no git dir')
|
raise AssertionError('unreachable: no git dir')
|
||||||
|
|
||||||
|
|
||||||
|
def get_git_common_dir(git_root: str = '.') -> str:
|
||||||
|
opt = '--git-common-dir'
|
||||||
|
_, out, _ = cmd_output('git', 'rev-parse', opt, cwd=git_root)
|
||||||
|
git_common_dir = out.strip()
|
||||||
|
if git_common_dir != opt:
|
||||||
|
return os.path.normpath(os.path.join(git_root, git_common_dir))
|
||||||
|
else:
|
||||||
|
# pragma: no branch (git < 2.5)
|
||||||
|
return get_git_dir(git_root)
|
||||||
|
|
||||||
|
|
||||||
def get_remote_url(git_root: str) -> str:
|
def get_remote_url(git_root: str) -> str:
|
||||||
_, out, _ = cmd_output('git', 'config', 'remote.origin.url', cwd=git_root)
|
_, out, _ = cmd_output('git', 'config', 'remote.origin.url', cwd=git_root)
|
||||||
return out.strip()
|
return out.strip()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue