jsdiff icon indicating copy to clipboard operation
jsdiff copied to clipboard

Return unchanged lines in a patch?

Open anatoly314 opened this issue 7 years ago • 5 comments

I want to use this library to change the difference between the two files. But I need to show all file's content, not only changed one. Right now the library's call Diff.createPatch returns only changed part of the file, can it return all lines? Something like doing this command diff -U 1000000 file1.txt file2.txt from here: How can I diff two files with full context? Thank you.

anatoly314 avatar Feb 03 '19 07:02 anatoly314

You can use Diff.structuredPatch() which returns a data structure including all lines. As you can see in the "README.md", the array "lines" contains unchanged, removed(-) and inserted(+) lines.

hunks: [{
    oldStart: 1, oldLines: 3, newStart: 1, newLines: 3,
    lines: [' line2', ' line3', '-line4', '+line5', '\\ No newline at end of file'],
  }]

juergba avatar Feb 26 '19 15:02 juergba

it is does not work ..can supply a example

lunalusy avatar Apr 19 '20 09:04 lunalusy

I used {context: Number.MAX_SAFE_INTEGER} as option in Diff.createTwoFilesPatch to include all unchanged lines, this should work for Diff.createPatch too.

WIStudent avatar Oct 16 '20 13:10 WIStudent

Can anyone provide the syntax here to add options?

intc-hharshtk avatar Nov 27 '22 15:11 intc-hharshtk

Yes, passing {context: Number.MAX_SAFE_INTEGER} or {context: Infinity} as the value of the options argument to createPatch (or structuredPatch or createTwoFilesPatch) will do the trick here.

(You could also just use diffLines if you're okay working with jsdiff change objects instead of unified diff patches. jsdiff's arrays of change objects always include all the unchanged lines.)

ExplodingCabbage avatar Jan 04 '24 17:01 ExplodingCabbage