fix: compare --diff from any directory and write lint-clean --fix guards
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
.gitentry, 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 onegit cat-file --batchprocess, each only when it was a regular file at the compared commit, which onegit ls-treeper 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 ownmainis the checked-out commit, would report every functionUNCHANGED. 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_gitchecks a finished git process after 1 ms, backing off to 10 ms. compute_diffkeeps its signature and delegates to the newcompute_diff_report, which also returns the files it could not read. No new type crosses into Python. Theinvocation_pathdocstring now says it anchors relative paths.--stagedis 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_flagshad already cleareddiff, so--diff-onlyalone triggered it.diff_flags_warningnow checks the flags as given.
C002 loop guard negation
- The new
negate_conditionworks from the parsed expression instead of wrapping the text innot (...):- a leading
notis dropped; - a single
in,not in,isoris notis swapped for its opposite; - names, calls, attributes and arithmetic get a bare
not; - parentheses stay around
and,or, other comparisons, conditional expressions, lambdas, walrus andyield; - parentheses that already wrap the condition are reused, and text that does not parse keeps the old
not (...)form.
- a leading
==and<are not flipped to!=and>=, because onlyinandishave exact opposites for every type.- The
C002help text anddocs/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 thenegate_conditioncases. - 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.pycovers 14 negation cases;tests/test_fix.pychecks an end-to-end--fix.
Merge with main
This branch merges main after #288 and the 8.2.0 release:
mainreads 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
C002guard keeps #288's rewrite, its help text for statements after the chain, and its test cases, and negates each condition withnegate_condition. Those tests now expectif not a:instead ofif not (a):. negate_conditionand its helpers parse throughsyntax_tree::parse_expression, whoseParsedExpressionnow also keeps the expression's tokens.- The new warnings go through
escape_controlsand the broken-pipe-safeemit!, like the other CLI output.