Skip to content

Commit

Permalink
Check the new code in a side-directory
Browse files Browse the repository at this point in the history
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: 38412fb
  • Loading branch information
praiskup committed Aug 28, 2024
1 parent e704cf9 commit dee173f
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion vcs-diff-lint
Original file line number Diff line number Diff line change
Expand Up @@ -287,15 +287,30 @@ 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)
check_call(['git', 'checkout', '-q', self.checkout])
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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit dee173f

Please sign in to comment.