Skip to content

feat: detect already-applied diffs (is_diff_applied_with_config, apply_bytes_reporting)#18

Merged
wolfv merged 1 commit into
mainfrom
feat/is-diff-applied
Jul 4, 2026
Merged

feat: detect already-applied diffs (is_diff_applied_with_config, apply_bytes_reporting)#18
wolfv merged 1 commit into
mainfrom
feat/is-diff-applied

Conversation

@wolfv

@wolfv wolfv commented Jul 4, 2026

Copy link
Copy Markdown
Member

What

Adds two public primitives for detecting whether a diff is already applied to a base image, so callers (e.g. rattler-build, see prefix-dev/rattler-build#1953) can warn-and-skip instead of silently re-applying a patch that was already merged upstream.

  • is_diff_applied_with_config(base, diff, config) -> bool — robust reverse round-trip predicate.
  • apply_bytes_reporting(base, diff, config) -> ApplyOutcome — apply and report the outcome as Applied(Vec<u8>, ApplyStats) / AlreadyApplied(Vec<u8>) / Failed(ApplyError).

Both are exported from the crate root.

Why a reverse round-trip (not a forward apply)

A forward apply is not a reliable "is it applied?" signal under fuzzy matching. On already-applied content flickzeug's matcher does one of two wrong things depending on hunk shape:

  • Line-replacement diff → forward apply fails (fuzzy only fuzzes context lines, not delete lines, so the removed -3.1 no longer matches 3.12).
  • Pure-insertion diff → forward apply succeeds but re-inserts the already-present line, producing corrupted doubled content.

So the already-applied case is detected via a reverse round-trip (reversing an already-applied diff must produce a different pre-image that patches forward back to the input), never via a forward no-op. apply_bytes_reporting therefore checks the already-applied case first and only forward-applies when the diff is genuinely not yet applied.

Note

This contradicts the assumption that a forward re-apply of an already-applied diff succeeds as a no-op — that does not hold for flickzeug's matcher, as covered by test_forward_reapply_is_unreliable_but_classifier_is_correct.

Tests

New unit tests (run with rattler-build's max_fuzz: 2, ignore_whitespace: true config) covering line-replacement, pure-insertion, unrelated content, the unreliable-forward behavior, and all three apply_bytes_reporting outcomes. Plus 2 doctests on the new functions. cargo test, cargo clippy --all-targets, and cargo fmt --check are clean.

Release

Additive / backwards-compatible → feat: commit yields a 0.5.1 → 0.6.0 minor bump via release-plz.

🤖 Generated with Claude Code

Add primitives to detect whether a diff is already applied to a base
image, so callers (e.g. rattler-build) can warn-and-skip instead of
silently re-applying a merged-upstream patch.

- `is_diff_applied_with_config(base, diff, config) -> bool`: robust
  reverse round-trip check.
- `apply_bytes_reporting(base, diff, config) -> ApplyOutcome`: apply,
  reporting Applied / AlreadyApplied / Failed.

Both are robust under fuzzy matching, where a forward apply of an
already-applied diff is unreliable: it may fail (a deleted line no
longer matches) or succeed while wrongly re-applying the change (e.g.
duplicating an inserted line). The already-applied case is therefore
detected via a reverse round-trip, never via a forward no-op.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@wolfv wolfv merged commit 31337d4 into main Jul 4, 2026
6 checks passed
@wolfv wolfv mentioned this pull request Jul 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant