Repository navigation
feat: add deslag check --diff - #53
Merged
Merged
Conversation
Add Violation::retain, which gives the part of a finding that a Keep keeps, or None. A Keep answers two questions, one for each kind of mark: whether to keep an occurrence at a location, and whether to keep a verdict on the whole file with its evidence. A verdict and its evidence are kept whole or not at all, so what a report says of a file never rests on half its evidence. It is an exhaustive match, like marks() and edits(), so a new lint has to decide. max_size_bytes and max_emphasis are verdicts. Each other lint gets a retain() beside its marks(), and repo_layout's marks() and retain() now share one Problem::place, so the two cannot disagree on what kind of mark a problem is. SARIF now decides whether a finding gets a result for the whole file by asking retain for its verdicts, where it used to partition the marks itself. Its output does not change. No run narrows a report yet. The test fails one file with every lint and checks, under four Keeps, that each retained finding's marks are exactly the marks the Keep kept, and that its report lists their lines and no others.
Add the change module. Change::against(root, base) finds the merge base of the base and HEAD, then asks git for every line the working tree adds or removes since: commits, staged and unstaged edits. Files git does not track count as added whole. Git runs as a subprocess; there is no git library. Every diff option a user's git config could change is set on the command line: prefixes, color, renames, --relative, the algorithm, the hunk context and core.quotePath. diff.interHunkContext is one of these: under -U0 it merges nearby hunks and their context lines into one hunk, which would count unchanged lines as changed, so the diff passes --inter-hunk-context=0. parse reads the patch into a File per path, keyed from the root as the walk names files. It takes paths from each file's own header lines, unescapes quoted paths, and counts a hunk's lines off its header, so a removed line that reads "--- a/x" is not taken for a header. A file whose type changed is two patches, merged into one added file. File::touches says whether a range of lines was touched: a line a hunk added, or the lines on either side of a hunk that only removes. An added or binary file is touched everywhere. File::edits_content is false for a pure rename or mode change. Each way this cannot run is an Error::Change that says why: no git, no work tree, a base git does not know, or no history shared with HEAD, with a hint to fetch more when the clone is shallow. Nothing calls it yet. The tests read a literal patch and check which lines a hunk touches, without git.
deslag check --diff <BASE> runs the whole check, then reports only what the change since the merge base of BASE and HEAD touched. It helps a repository that does not pass yet stop getting worse; the whole-tree check stays the gate. Report::within narrows a report to a Change. A finding in a file the change did not touch is dropped. In a file it did touch, the finding keeps, through Violation::retain, each occurrence on a touched line, and a verdict on the whole file with its evidence when the change edits the file's content. So renaming a file that is over its budget reports nothing, and adding a line to it reports the budget. Narrowing never changes the whole-tree report: Report::scanned is now the list of paths examined, where it was a count, so the narrowed summary can say how many of them changed. The text summary adds a line on the change, and --format json adds a change object that a whole-tree run leaves out. The config has no setting for it, and fix takes no --diff. What a change does not touch is not reported. Deleting a directory that an untouched layout lists fails only the whole-tree run, so the guide says to drop the --diff job once the tree passes, and to upload SARIF only from whole-tree runs, since code scanning closes the alerts a narrowed run leaves out. The tests build repositories with git reading no global or system config. They pin each rule above, each way it cannot run, each --format, and that a hostile git config prints the same. Every case, committed on top of an empty commit, must print its whole-tree findings under --diff HEAD~1. preflight now needs git.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This adds
deslag check --diff <BASE>. It runs the whole check as before, then reports only what a change touched. The change runs from the commit whereBASEandHEADmeet to the working tree: commits, staged and unstaged edits, and untracked files.The point is adoption. A repository that does not pass yet can run deslag on its pull requests and stop getting worse, without first fixing every file. The whole-tree
deslag checkstays the gate, and the guide says to drop the--diffjob once the tree passes.Exit codes are as before: 0 when nothing is left, 1 when something is. A base it cannot use is exit 2, with a message that says why: no git, not in a work tree, a base git does not know, or no history shared with
HEAD(with a hint to fetch more when the clone is shallow). The config has no setting for it, anddeslag fixtakes no--diff.What is kept
max_size_bytes,max_emphasisor arepo_layoutentry count, is kept whole, with all of its evidence, when the change edits the file's content. A pure rename or mode change edits no content, so renaming a file that is already over its budget reports nothing, and adding a line to it reports the budget.How it is built
Three commits:
feat: keep part of a finding by its marksaddsViolation::retainand aKeeptrait. AKeepanswers two questions, one for each kind of mark: keep an occurrence at this location, and keep a verdict on the whole file.retainis an exhaustive match besidemarks()andedits(), so a new lint has to decide. SARIF now uses it to decide which findings get a result for the whole file; its output does not change.feat: read what a change did from gitaddssrc/change/. Git runs as a subprocess, with no new dependency.parsereads the patch, andFile::touchesandFile::edits_contentanswer the two questions above for one file.feat: add deslag check --diffaddsReport::within, the flag, the output, the docs and the guide.Narrowing only filters the whole-tree report, so a whole-tree run prints exactly what it did before; the golden set and the cases do not change.
Asking git
Change::againstrunsrev-parseto check the work tree and the base,merge-base, then onegit diff -U0from the merge base to the working tree, andls-files --others --exclude-standardfor untracked files.Every diff option a user's git config could change is on the command line: prefixes, color, external diff tools, rename detection,
--relative, the algorithm, andcore.quotePath. One of these was a surprise:diff.interHunkContextmakes-U0merge nearby hunks into one, context lines and all, which would count unchanged lines as changed. The diff passes--inter-hunk-context=0, and a test runs with a hostile git config (including that setting) and checks the output does not change.When deslag runs in a subdirectory of the repository,
--relativelimits the diff to it. A file moved in from outside that directory then shows as added, so all of it is reported.Output
--format jsonadds achangeobject with the base, the merge base and the count of changed files. A whole-tree run leaves it out.--format githuband--format sarifare narrowed the same way.Report::scannedis now the list of paths examined, where it was a count, so the summary can say how many of them the change touched. This changes the library's public API. The JSON'sfiles_scannedis still a count.What it misses
A change is judged by the lines it touched. Deleting a directory that an untouched layout still lists fails only the whole-tree run. The guide says so, and says to remove the
--diffjob once the tree passes.Measured on history
For each commit, whole-tree runs on it and on its parent give the findings the commit introduced: each occurrence or verdict the parent's run does not have. Then
deslag check --diff <parent>runs on the commit. A missed finding is one the commit introduced that the narrowed run does not report.Some narrowed runs still fail on findings the commit did not introduce (2 of 27 here). This is expected: a verdict on a file comes back when the file is edited, and an old banned character comes back when its line is edited.
Research
Three MIT-licensed tools with a diff mode were studied. No code was ported, so there is no
ACKNOWLEDGEMENTS.mdentry.git diff -U0against the merge base and keeps findings on added lines. It ignores pure deletions, does not see untracked files, and falls back silently when it cannot find a merge base. deslag takes the added-lines map, and touches the lines beside a deletion, sees untracked files, and fails with exit 2 where unslop-ci falls back.git diff -U0 --diff-filter=AMR <base> HEAD, so it does not see uncommitted work, and hardcodes in source which rules always scan the whole file in diff mode. deslag has no such list: each lint'sretainsays which of its marks are verdicts.--diffflag that drops messages on unchanged lines, through theunified-diffplugin; its help says it affects Travis only. deslag asks git itself, so it works in any CI and on a local tree.Testing
make ci: exit 0tests/diff.rs, 27 tests: repositories built with git reading no global or system config. They cover committed, staged, unstaged and untracked edits;--diff HEAD; deleted, renamed and moved files; budgets and emphasis verdicts; layouts; joined paragraphs; CRLF; a last line with no newline; a root below the top of the repository; every--format; a hostile git config; each exit 2, including a shallow clone and no git onPATH; and thatfixtakes no--diff.tests/cases, committed on top of an empty commit, prints its whole-tree findings under--diff HEAD~1.parseon a literal patch, including quoted paths, renames, a type change, a binary file and a removed line that reads like a header, without git.Keepkept.deslag checkon the repo: exit 0, anddeslag check --diff main: exit 0.References