feat: detect already-applied diffs (is_diff_applied_with_config, apply_bytes_reporting)#18
Merged
Conversation
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>
Merged
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.
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 asApplied(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:
-3.1no longer matches3.12).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_reportingtherefore 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: trueconfig) covering line-replacement, pure-insertion, unrelated content, the unreliable-forward behavior, and all threeapply_bytes_reportingoutcomes. Plus 2 doctests on the new functions.cargo test,cargo clippy --all-targets, andcargo fmt --checkare clean.Release
Additive / backwards-compatible →
feat:commit yields a0.5.1 → 0.6.0minor bump via release-plz.🤖 Generated with Claude Code