Skip to main content

apply_file_patch

Function apply_file_patch 

Source
pub fn apply_file_patch(base: &[u8], patch: &FilePatch) -> ApplyOutcome
Expand description

Apply a single-file patch to base, returning the patched bytes.

This mirrors git’s apply.c (apply_one_fragment / find_pos / match_fragment) for the default, no-whitespace-fuzz settings git am and git apply use:

  • Each hunk builds a preimage (context + deleted lines) and postimage (context + inserted lines).
  • A hunk anchored at the file start (old_start <= 1) must match the beginning of the file (match_beginning); a hunk with no trailing context must match the end of the file (match_end).
  • The full preimage is matched byte-for-byte; the search starts at the recorded position and ping-pongs outward across the whole image.
  • Fuzz is applied only by dropping leading/trailing context lines (never by jumping to a spurious context-only match); if no position matches even after dropping all context, the hunk — and thus the whole patch — is ApplyOutcome::Rejected.

Rejecting (rather than spuriously applying at a wrong offset) is what lets git am -3 correctly fall back to its 3-way merge path.

New-file patches (empty/ignored base) and the no-final-newline case are handled byte-accurately. Clean exact-position applies are byte-identical to the previous behaviour.