From dee173f6127d4fa0602f52d2078fcea5865d4c2c Mon Sep 17 00:00:00 2001 From: Pavel Raiskup Date: Tue, 27 Aug 2024 13:04:26 +0200 Subject: [PATCH] Check the new code in a side-directory This makes a better Ruff linter isolation; without this, Ruff traverses all the gitroot sub-directories, including separate worktrees, temporary files, etc. Then, since potential issues in such files are not present in the "old code" (even before linted on a clean checkout), vcs-diff-lint would report such irrelevant errors as "new" issues. After this commit, both the old and new code is analyzed in a clean separate temporary directory. Complements: 38412fb334387dfe8ca3857f7d6c12fe7e3f81d3 --- vcs-diff-lint | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/vcs-diff-lint b/vcs-diff-lint index 95e31b8..1e72733 100755 --- a/vcs-diff-lint +++ b/vcs-diff-lint @@ -287,8 +287,10 @@ class _Worker: # pylint: disable=too-few-public-methods # prepare the old checkout old_gitroot = os.path.join(self.workdir, 'old_dir') + new_gitroot = os.path.join(self.workdir, 'new_dir') origin_from = self.gitroot check_call(['git', 'clone', '--quiet', origin_from, old_gitroot]) + check_call(['git', 'clone', '--quiet', origin_from, new_gitroot]) ret_cwd = os.getcwd() try: os.chdir(old_gitroot) @@ -296,6 +298,19 @@ class _Worker: # pylint: disable=too-few-public-methods finally: os.chdir(ret_cwd) + try: + os.chdir(new_gitroot) + with Popen(["git", "-C", origin_from, "diff", "--cached"], + stdout=PIPE) as diff: + check_call(["git", "apply", "--allow-empty", "--index", "-"], + stdin=diff.stdout) + with Popen(["git", "-C", origin_from, "diff"], + stdout=PIPE) as diff: + check_call(["git", "apply", "--allow-empty", "-"], + stdin=diff.stdout) + finally: + os.chdir(ret_cwd) + def add_file(gitroot, files, filename): if not filename: return @@ -336,7 +351,7 @@ class _Worker: # pylint: disable=too-few-public-methods break for linterClass in selected_linters: - linter_new = linterClass(self.gitroot) + linter_new = linterClass(new_gitroot) linter_old = linterClass(old_gitroot, renames) linter_new.lint(self.projectdir, new_files, logfd=new_report_fd) linter_old.lint(self.projectdir, old_files, logfd=old_report_fd)