Skip to content

Commit 0fe959d

Browse files
committed
fall back to full diff on disparate histories
1 parent abc1c5d commit 0fe959d

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

pre_commit/git.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,15 @@ def get_all_files() -> List[str]:
155155

156156

157157
def get_changed_files(old: str, new: str) -> List[str]:
158-
return zsplit(
159-
cmd_output(
160-
'git', 'diff', '--name-only', '--no-ext-diff', '-z',
161-
f'{old}...{new}',
162-
)[1],
163-
)
158+
diff_cmd = ('git', 'diff', '--name-only', '--no-ext-diff', '-z')
159+
try:
160+
_, out, _ = cmd_output(*diff_cmd, f'{old}...{new}')
161+
except CalledProcessError: # pragma: no cover (new git)
162+
# on newer git where old and new do not have a merge base git fails
163+
# so we try a full diff (this is what old git did for us!)
164+
_, out, _ = cmd_output(*diff_cmd, f'{old}..{new}')
165+
166+
return zsplit(out)
164167

165168

166169
def head_rev(remote: str) -> str:

tests/git_test.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,24 @@ def test_get_changed_files(in_git_dir):
139139
assert files == []
140140

141141

142+
def test_get_changed_files_disparate_histories(in_git_dir):
143+
"""in modern versions of git, `...` does not fall back to full diff"""
144+
git_commit()
145+
in_git_dir.join('a.txt').ensure()
146+
cmd_output('git', 'add', '.')
147+
git_commit()
148+
cmd_output('git', 'branch', '-m', 'branch1')
149+
150+
cmd_output('git', 'checkout', '--orphan', 'branch2')
151+
cmd_output('git', 'rm', '-rf', '.')
152+
in_git_dir.join('a.txt').ensure()
153+
in_git_dir.join('b.txt').ensure()
154+
cmd_output('git', 'add', '.')
155+
git_commit()
156+
157+
assert git.get_changed_files('branch1', 'branch2') == ['b.txt']
158+
159+
142160
@pytest.mark.parametrize(
143161
('s', 'expected'),
144162
(

0 commit comments

Comments
 (0)