From 7654e1a50e9e5e92768218cf624ec5e9c0469f9e Mon Sep 17 00:00:00 2001 From: Philipp Hahn Date: Fri, 20 Jan 2023 11:48:42 +0100 Subject: [PATCH] fix(git): Use diff-tree instead of diff which does not require all intermediate commits to exists, which helps when running from CI/CD pipelines which do shallow clones for performance reasons. Inspired by --- pre_commit/git.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/pre_commit/git.py b/pre_commit/git.py index 333dc7ba..3ac9acd9 100644 --- a/pre_commit/git.py +++ b/pre_commit/git.py @@ -156,14 +156,11 @@ def get_all_files() -> list[str]: def get_changed_files(old: str, new: str) -> list[str]: - 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}') - + _, out, _ = cmd_output( + 'git', 'diff-tree', + '--name-only', '--raw', '-z', + old, new, + ) return zsplit(out)