Skip to content

Commit 07af540

Browse files
errsynasottile
authored andcommitted
Adjust git dir when checking for merge in worktree
1 parent e35ba8d commit 07af540

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

pre_commit_hooks/check_merge_conflict.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from typing import Optional
44
from typing import Sequence
55

6+
from pre_commit_hooks.util import cmd_output
7+
68

79
CONFLICT_PATTERNS = [
810
b'<<<<<<< ',
@@ -12,13 +14,14 @@
1214
]
1315

1416

15-
def is_in_merge() -> int:
17+
def is_in_merge() -> bool:
18+
git_dir = cmd_output('git', 'rev-parse', '--git-dir').rstrip()
1619
return (
17-
os.path.exists(os.path.join('.git', 'MERGE_MSG')) and
20+
os.path.exists(os.path.join(git_dir, 'MERGE_MSG')) and
1821
(
19-
os.path.exists(os.path.join('.git', 'MERGE_HEAD')) or
20-
os.path.exists(os.path.join('.git', 'rebase-apply')) or
21-
os.path.exists(os.path.join('.git', 'rebase-merge'))
22+
os.path.exists(os.path.join(git_dir, 'MERGE_HEAD')) or
23+
os.path.exists(os.path.join(git_dir, 'rebase-apply')) or
24+
os.path.exists(os.path.join(git_dir, 'rebase-merge'))
2225
)
2326
)
2427

tests/check_merge_conflict_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,15 @@ def test_care_when_assumed_merge(tmpdir):
135135
f = tmpdir.join('README.md')
136136
f.write_binary(b'problem\n=======\n')
137137
assert main([str(f.realpath()), '--assume-in-merge']) == 1
138+
139+
140+
def test_worktree_merge_conflicts(f1_is_a_conflict_file, tmpdir):
141+
worktree = tmpdir.join('worktree')
142+
cmd_output('git', 'worktree', 'add', str(worktree))
143+
with worktree.as_cwd():
144+
cmd_output(
145+
'git', 'pull', '--no-rebase', 'origin', 'master', retcode=None,
146+
)
147+
msg = f1_is_a_conflict_file.join('.git/worktrees/worktree/MERGE_MSG')
148+
assert msg.exists()
149+
test_merge_conflicts_git()

0 commit comments

Comments
 (0)