fix: compare --diff from any directory and write lint-clean --fix guards

Merged
DS
1mo ago
+1347-130

This pull request was posted by Claude Code using claude-opus-5-5 on behalf of David.

What

This PR fixes three bugs that show up when complexipy runs on a large repository: --diff reports every function as NEW when run from outside the repository, --diff-only alone warns that both diff flags are set, and the C002 loop guard writes not (x in y), which ruff flags as E713.

Why

--diff found one repository, from the working directory, and read every file at its path from that repository's root. From outside the repository there was no such root, or the files sat outside it, so the diff had nothing to compare against and the enforcing --diff failed functions that had not changed. Separately, complexipy . --fix on a codebase linted with ruff left E713/E714 violations and redundant parentheses behind.

Changes

--diff reads each file from its own repository

  • Each file's directory is walked up to the nearest directory that holds a .git entry, a directory or a file, and git resolves the repository root once for each such directory. Each file is read at its path from that root, so a run from outside the repository reads the same history as a run from inside it. The files of each repository are read through one git cat-file --batch process, each only when it was a regular file at the compared commit, which one git ls-tree per repository lists.
  • A submodule, meaning a repository that the repository around it tracks as a gitlink in its index, is read at the commit the outer repository pins for it (the gitlink at its path in the outer repository's tree at the ref), resolved level by level for deeper nesting. Its own <ref> would name something unrelated to the outer change: a submodule added by the change, whose own main is the checked-out commit, would report every function UNCHANGED. The exception is a run that starts inside the submodule, which reads it at its own <ref>. Any other repository, including one placed inside another that the outer one does not track, keeps its own <ref>.
  • A file outside any repository, in a repository where the ref names no commit, or in a submodule that the outer repository does not pin at the ref (or pins at a commit it does not have) still reports its functions as NEW. The run now prints one warning per cause, for example "v9" does not name a commit in <repo>, so all functions in its 1 analyzed file(s) report as NEW.
  • The commit each repository is compared at is resolved once per repository. A run over one repository starts 4 git processes, however many directories it spans: the root, the commit, the regular files at that commit, and the batched read. run_git checks a finished git process after 1 ms, backing off to 10 ms.
  • compute_diff keeps its signature and delegates to the new compute_diff_report, which also returns the files it could not read. No new type crosses into Python. The invocation_path docstring now says it anchors relative paths.
  • --staged is unchanged: it still uses the repository of the working directory.

Diff flags warning

  • The "--diff and --diff-only both set" warning was checked after resolve_diff_flags had already cleared diff, so --diff-only alone triggered it. diff_flags_warning now checks the flags as given.

C002 loop guard negation

  • The new negate_condition works from the parsed expression instead of wrapping the text in not (...):
    • a leading not is dropped;
    • a single in, not in, is or is not is swapped for its opposite;
    • names, calls, attributes and arithmetic get a bare not;
    • parentheses stay around and, or, other comparisons, conditional expressions, lambdas, walrus and yield;
    • parentheses that already wrap the condition are reused, and text that does not parse keeps the old not (...) form.
  • == and < are not flipped to != and >=, because only in and is have exact opposites for every type.
  • The C002 help text and docs/refactoring-rules.md (English and Spanish) show the new output.

Tests

  • Rust: an outside working directory with an absolute path, a subdirectory run with a decoy root-level file of the same name, a file outside any repository, an unknown ref, an outer repository without the ref (reported against the outer repository), a linked worktree read from outside it, a submodule read at its pinned commit, through two levels of nesting, and at its own ref when the run starts inside it, a submodule the change adds and a submodule pinned at a missing commit (both NEW), an untracked nested repository read at its own ref, the warning texts, the flags warning, and the negate_condition cases.
  • Python: tests/test_diff_cli.py (new) runs the CLI from outside a repository, checks both warnings, and checks that the gate fails on a submodule the change adds; tests/test_refactor_plans.py covers 14 negation cases; tests/test_fix.py checks an end-to-end --fix.

Merge with main

This branch merges main after #288 and the 8.2.0 release:

  • main reads a file at the ref only when it was a regular file there. That check now runs per repository, at the commit each repository is compared at.
  • The C002 guard keeps #288's rewrite, its help text for statements after the chain, and its test cases, and negates each condition with negate_condition. Those tests now expect if not a: instead of if not (a):.
  • negate_condition and its helpers parse through syntax_tree::parse_expression, whose ParsedExpression now also keeps the expression's tokens.
  • The new warnings go through escape_controls and the broken-pipe-safe emit!, like the other CLI output.