Skip to content

feat: add deslag check --diff - #53

Merged
webern merged 3 commits into
mainfrom
m/deslag-diff
Sep 27, 2026
Merged

webern merged 3 commits into
mainfrom
m/deslag-diff

Conversation

@webern

@webern webern commented Sep 27, 2026

Copy link
Copy Markdown
Owner

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 where BASE and HEAD meet 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 check stays the gate, and the guide says to drop the --diff job once the tree passes.

deslag: 1 of 2 Markdown files with banned characters.
deslag: 1 of 2 Markdown files changed since main; findings outside the change are not shown.

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, and deslag fix takes no --diff.

What is kept

  • An occurrence, such as a banned character, a banned phrase, a dense paragraph or a layout line, is kept when the change touched its lines. A line is touched when the change added it, or when it sits right beside lines the change only removed. So deleting the blank line between two paragraphs reports the paragraph they become.
  • A verdict on the whole file, such as max_size_bytes, max_emphasis or a repo_layout entry 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.
  • A file the change added, or an untracked file, is touched everywhere.
  • A file the change did not touch reports nothing.

How it is built

Three commits:

  1. feat: keep part of a finding by its marks adds Violation::retain and a Keep trait. A Keep answers two questions, one for each kind of mark: keep an occurrence at this location, and keep a verdict on the whole file. retain is an exhaustive match beside marks() and edits(), 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.
  2. feat: read what a change did from git adds src/change/. Git runs as a subprocess, with no new dependency. parse reads the patch, and File::touches and File::edits_content answer the two questions above for one file.
  3. feat: add deslag check --diff adds Report::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::against runs rev-parse to check the work tree and the base, merge-base, then one git diff -U0 from the merge base to the working tree, and ls-files --others --exclude-standard for 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, and core.quotePath. One of these was a surprise: diff.interHunkContext makes -U0 merge 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, --relative limits the diff to it. A file moved in from outside that directory then shows as added, so all of it is reported.

Output

  • The text summary adds the line on the change shown above.
  • --format json adds a change object with the base, the merge base and the count of changed files. A whole-tree run leaves it out.
  • --format github and --format sarif are narrowed the same way.
  • The guide says to upload SARIF only from whole-tree runs: GitHub code scanning closes any alert a later upload does not include, so a narrowed upload would close alerts for files the change did not touch.

Report::scanned is 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's files_scanned is 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 --diff job 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.

  • deslag's own history, up to dfbfc00, with a config much stricter than the repo's own so that old commits have findings: 27 commits change Markdown. They introduced 119 findings, and none were missed. The narrowed runs reported 82 of the 333 findings the whole-tree runs reported (25%).
  • A private repository of agent-written design docs: none missed, and the narrowed runs reported 3% of the whole-tree findings.

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.md entry.

  • unslop-ci (at 7e31097c) runs git diff -U0 against 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.
  • unicode-safety-check (at de490427) runs 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's retain says which of its marks are verdicts.
  • alex (at 9a57595d) has a --diff flag that drops messages on unchanged lines, through the unified-diff plugin; 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 0
  • tests/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 on PATH; and that fix takes no --diff.
  • Every case in tests/cases, committed on top of an empty commit, prints its whole-tree findings under --diff HEAD~1.
  • parse on a literal patch, including quoted paths, renames, a type change, a binary file and a removed line that reads like a header, without git.
  • For one file that fails every lint, a retained finding's marks and report lines are exactly what the Keep kept.
  • deslag check on the repo: exit 0, and deslag check --diff main: exit 0.
  • The history measurement above.

References

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.
@webern webern added enhancement New feature or request ai Primarily authored by AI (whether PR, Issue, Whatever) area/cli Deals with the command line interface. area/lint Deals with the linting algorithms themselves, how they work. labels Sep 27, 2026
@webern
webern merged commit 85693ad into main Sep 27, 2026
2 checks passed
@webern
webern deleted the m/deslag-diff branch September 27, 2026 08:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai Primarily authored by AI (whether PR, Issue, Whatever) area/cli Deals with the command line interface. area/lint Deals with the linting algorithms themselves, how they work. enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant