Skip to content

Commit 2f0fefe

Browse files
authored
Merge pull request pre-commit#809 from pre-commit/worktree
Support `pre-commit install` inside a worktree
2 parents ab556f4 + 3f78487 commit 2f0fefe

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

pre_commit/git.py

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

3232

3333
def get_git_dir(git_root):
34-
return os.path.normpath(os.path.join(
35-
git_root,
36-
cmd_output('git', 'rev-parse', '--git-dir', cwd=git_root)[1].strip(),
37-
))
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')
3844

3945

4046
def get_remote_url(git_root):

tests/commands/install_uninstall_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,19 @@ def test_install_in_submodule_and_run(tempdir_factory, store):
172172
assert NORMAL_PRE_COMMIT_RUN.match(output)
173173

174174

175+
def test_install_in_worktree_and_run(tempdir_factory, store):
176+
src_path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
177+
path = tempdir_factory.get()
178+
cmd_output('git', '-C', src_path, 'branch', '-m', 'notmaster')
179+
cmd_output('git', '-C', src_path, 'worktree', 'add', path, '-b', 'master')
180+
181+
with cwd(path):
182+
assert install(Runner(path, C.CONFIG_FILE), store) == 0
183+
ret, output = _get_commit_output(tempdir_factory)
184+
assert ret == 0
185+
assert NORMAL_PRE_COMMIT_RUN.match(output)
186+
187+
175188
def test_commit_am(tempdir_factory, store):
176189
"""Regression test for #322."""
177190
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')

0 commit comments

Comments
 (0)