function

diff.applyPatch

function applyPatch(
source: string,
patch: string | Pick<Hunk, 'oldStart' | 'lines'>[]
): undefined | string;

Apply a patch for one file to source.

The lines that a hunk keeps and removes have to match exactly. If they don't match at oldStart, Bun uses the nearest line where they do. If source only has

\r
line endings and the patch only has

, or the reverse, the patch is converted first.

@param patch

A unified diff, or hunks from hunks or parsePatch

@returns

The patched text, or undefined if a hunk does not match

Referenced types

interface Hunk

One @@ section of a unified diff.

  • lines: string[]

    Each line without its


    , after " " (kept), "-" (removed) or "+" (added). "\\ No newline at end of file" follows a line that has no

    .

  • newLines: number

    The number of lines the hunk keeps or adds.

  • newStart: number

    The first line of the hunk in the new text, from 1. If newLines is 0, the line after the hunk.

  • oldLines: number

    The number of lines the hunk keeps or removes.

  • oldStart: number

    The first line of the hunk in the old text, from 1. If oldLines is 0, the line after the hunk.