mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
fall back to full diff on disparate histories
This commit is contained in:
parent
abc1c5d9ba
commit
0fe959df60
2 changed files with 27 additions and 6 deletions
|
|
@ -155,12 +155,15 @@ def get_all_files() -> List[str]:
|
||||||
|
|
||||||
|
|
||||||
def get_changed_files(old: str, new: str) -> List[str]:
|
def get_changed_files(old: str, new: str) -> List[str]:
|
||||||
return zsplit(
|
diff_cmd = ('git', 'diff', '--name-only', '--no-ext-diff', '-z')
|
||||||
cmd_output(
|
try:
|
||||||
'git', 'diff', '--name-only', '--no-ext-diff', '-z',
|
_, out, _ = cmd_output(*diff_cmd, f'{old}...{new}')
|
||||||
f'{old}...{new}',
|
except CalledProcessError: # pragma: no cover (new git)
|
||||||
)[1],
|
# on newer git where old and new do not have a merge base git fails
|
||||||
)
|
# so we try a full diff (this is what old git did for us!)
|
||||||
|
_, out, _ = cmd_output(*diff_cmd, f'{old}..{new}')
|
||||||
|
|
||||||
|
return zsplit(out)
|
||||||
|
|
||||||
|
|
||||||
def head_rev(remote: str) -> str:
|
def head_rev(remote: str) -> str:
|
||||||
|
|
|
||||||
|
|
@ -139,6 +139,24 @@ def test_get_changed_files(in_git_dir):
|
||||||
assert files == []
|
assert files == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_changed_files_disparate_histories(in_git_dir):
|
||||||
|
"""in modern versions of git, `...` does not fall back to full diff"""
|
||||||
|
git_commit()
|
||||||
|
in_git_dir.join('a.txt').ensure()
|
||||||
|
cmd_output('git', 'add', '.')
|
||||||
|
git_commit()
|
||||||
|
cmd_output('git', 'branch', '-m', 'branch1')
|
||||||
|
|
||||||
|
cmd_output('git', 'checkout', '--orphan', 'branch2')
|
||||||
|
cmd_output('git', 'rm', '-rf', '.')
|
||||||
|
in_git_dir.join('a.txt').ensure()
|
||||||
|
in_git_dir.join('b.txt').ensure()
|
||||||
|
cmd_output('git', 'add', '.')
|
||||||
|
git_commit()
|
||||||
|
|
||||||
|
assert git.get_changed_files('branch1', 'branch2') == ['b.txt']
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
('s', 'expected'),
|
('s', 'expected'),
|
||||||
(
|
(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue