Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fall back to full diff on disparate histories #2005

Merged
merged 1 commit into from Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 9 additions & 6 deletions pre_commit/git.py
Expand Up @@ -155,12 +155,15 @@ def get_all_files() -> List[str]:


def get_changed_files(old: str, new: str) -> List[str]:
return zsplit(
cmd_output(
'git', 'diff', '--name-only', '--no-ext-diff', '-z',
f'{old}...{new}',
)[1],
)
diff_cmd = ('git', 'diff', '--name-only', '--no-ext-diff', '-z')
try:
_, out, _ = cmd_output(*diff_cmd, f'{old}...{new}')
except CalledProcessError: # pragma: no cover (new git)
# 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:
Expand Down
18 changes: 18 additions & 0 deletions tests/git_test.py
Expand Up @@ -139,6 +139,24 @@ def test_get_changed_files(in_git_dir):
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(
('s', 'expected'),
(
Expand Down