From 83653673cbe22e1688985ccd3c468b418f48d6bb Mon Sep 17 00:00:00 2001 From: Mark Amery Date: Tue, 2 Jan 2024 17:41:03 +0000 Subject: [PATCH] Add function to reverse a patch (#450) * Add reversePatch function and tests * Document new method * Add release notes --- README.md | 6 ++- release-notes.md | 1 + src/index.js | 2 + src/patch/reverse.js | 27 ++++++++++++ test/patch/reverse.js | 97 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 src/patch/reverse.js create mode 100644 test/patch/reverse.js diff --git a/README.md b/README.md index 06eebfa4..75688b38 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,7 @@ npm install diff --save * `Diff.formatPatch(patch)` - creates a unified diff patch. - The argument provided can either be an object representing a structured patch (like returned by `structuredPatch`) or an array of such objects (like returned by `parsePatch`). + `patch` may be either a single structured patch object (as returned by `structuredPatch`) or an array of them (as returned by `parsePatch`). * `Diff.structuredPatch(oldFileName, newFileName, oldStr, newStr, oldHeader, newHeader, options)` - returns an object with an array of hunk objects. @@ -132,6 +132,10 @@ npm install diff --save Return a JSON object representation of the a patch, suitable for use with the `applyPatch` method. This parses to the same structure returned by `Diff.structuredPatch`. +* `Diff.reversePatch(patch)` - Returns a new structured patch which when applied will undo the original `patch`. + + `patch` may be either a single structured patch object (as returned by `structuredPatch`) or an array of them (as returned by `parsePatch`). + * `convertChangesToXML(changes)` - converts a list of changes to a serialized XML format diff --git a/release-notes.md b/release-notes.md index 628a180d..3919c740 100644 --- a/release-notes.md +++ b/release-notes.md @@ -9,6 +9,7 @@ - [#351](https://github.com/kpdecker/jsdiff/issues/351) Importing from the lib folder - e.g. `require("diff/lib/diff/word.js")` - will work again now. This had been broken for users on the latest version of Node since Node 17.5.0, which changed how Node interprets the `exports` property in jsdiff's `package.json` file. - [#344](https://github.com/kpdecker/jsdiff/issues/344) `diffLines`, `createTwoFilesPatch`, and other patch-creation methods now take an optional `stripTrailingCr: true` option which causes Windows-style `\r\n` line endings to be replaced with Unix-style `\n` line endings before calculating the diff, just like GNU `diff`'s `--strip-trailing-cr` flag. - [#451](https://github.com/kpdecker/jsdiff/pull/451) Added `diff.formatPatch`. +- [#450](https://github.com/kpdecker/jsdiff/pull/450) Added `diff.reversePatch`. ## v5.1.0 diff --git a/src/index.js b/src/index.js index 4a045c91..44e43ace 100644 --- a/src/index.js +++ b/src/index.js @@ -28,6 +28,7 @@ import {diffArrays} from './diff/array'; import {applyPatch, applyPatches} from './patch/apply'; import {parsePatch} from './patch/parse'; import {merge} from './patch/merge'; +import {reversePatch} from './patch/reverse'; import {structuredPatch, createTwoFilesPatch, createPatch, formatPatch} from './patch/create'; import {convertChangesToDMP} from './convert/dmp'; @@ -56,6 +57,7 @@ export { applyPatches, parsePatch, merge, + reversePatch, convertChangesToDMP, convertChangesToXML, canonicalize diff --git a/src/patch/reverse.js b/src/patch/reverse.js new file mode 100644 index 00000000..fb902b47 --- /dev/null +++ b/src/patch/reverse.js @@ -0,0 +1,27 @@ +export function reversePatch(structuredPatch) { + if (Array.isArray(structuredPatch)) { + return structuredPatch.map(reversePatch).reverse(); + } + + return { + ...structuredPatch, + oldFileName: structuredPatch.newFileName, + oldHeader: structuredPatch.newHeader, + newFileName: structuredPatch.oldFileName, + newHeader: structuredPatch.oldHeader, + hunks: structuredPatch.hunks.map(hunk => { + return { + oldLines: hunk.newLines, + oldStart: hunk.newStart, + newLines: hunk.oldLines, + newStart: hunk.oldStart, + linedelimiters: hunk.linedelimiters, + lines: hunk.lines.map(l => { + if (l.startsWith('-')) { return `+${l.slice(1)}`; } + if (l.startsWith('+')) { return `-${l.slice(1)}`; } + return l; + }) + }; + }) + }; +} diff --git a/test/patch/reverse.js b/test/patch/reverse.js new file mode 100644 index 00000000..1781aa1d --- /dev/null +++ b/test/patch/reverse.js @@ -0,0 +1,97 @@ +import {applyPatch} from '../../lib/patch/apply'; +import {structuredPatch, formatPatch} from '../../lib/patch/create'; +import {reversePatch} from '../../lib/patch/reverse'; +import {parsePatch} from '../../lib/patch/parse'; + +import {expect} from 'chai'; + +describe('patch/reverse', function() { + describe('#reversePatch', function() { + it('should output a patch that is the inverse of the provided patch', function() { + const file1 = 'line1\nline2\nline3\nline4\n'; + const file2 = 'line1\nline2\nline5\nline4\n'; + const patch = structuredPatch('file1', 'file2', file1, file2); + const reversedPatch = reversePatch(patch); + expect(formatPatch(reversedPatch)).to.equal( + '===================================================================\n' + + '--- file2\n' + + '+++ file1\n' + + '@@ -1,4 +1,4 @@\n' + + ' line1\n' + + ' line2\n' + + '+line3\n' + + '-line5\n' + + ' line4\n' + ); + expect(applyPatch(file2, reversedPatch)).to.equal(file1); + }); + + it('should support taking an array of structured patches, as output by parsePatch', function() { + const patch = parsePatch( + 'diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md\n' + + 'index 20b807a..4a96aff 100644\n' + + '--- a/CONTRIBUTING.md\n' + + '+++ b/CONTRIBUTING.md\n' + + '@@ -2,6 +2,8 @@\n' + + ' \n' + + ' ## Pull Requests\n' + + ' \n' + + '+bla bla bla\n' + + '+\n' + + ' We also accept [pull requests][pull-request]!\n' + + ' \n' + + ' Generally we like to see pull requests that\n' + + 'diff --git a/README.md b/README.md\n' + + 'index 06eebfa..40919a6 100644\n' + + '--- a/README.md\n' + + '+++ b/README.md\n' + + '@@ -1,5 +1,7 @@\n' + + ' # jsdiff\n' + + ' \n' + + '+foo\n' + + '+\n' + + ' [![Build Status](https://secure.travis-ci.org/kpdecker/jsdiff.svg)](http://travis-ci.org/kpdecker/jsdiff)\n' + + ' [![Sauce Test Status](https://saucelabs.com/buildstatus/jsdiff)](https://saucelabs.com/u/jsdiff)\n' + + ' \n' + + "@@ -225,3 +227,5 @@ jsdiff deviates from the published algorithm in a couple of ways that don't affe\n" + + ' \n' + + " * jsdiff keeps track of the diff for each diagonal using a linked list of change objects for each diagonal, rather than the historical array of furthest-reaching D-paths on each diagonal contemplated on page 8 of Myers's paper.\n" + + ' * jsdiff skips considering diagonals where the furthest-reaching D-path would go off the edge of the edit graph. This dramatically reduces the time cost (from quadratic to linear) in cases where the new text just appends or truncates content at the end of the old text.\n' + + '+\n' + + '+bar\n' + ); + expect(formatPatch(reversePatch(patch))).to.equal( + '===================================================================\n' + + '--- b/README.md\t\n' + + '+++ a/README.md\t\n' + + '@@ -1,7 +1,5 @@\n' + + ' # jsdiff\n' + + ' \n' + + '-foo\n' + + '-\n' + + ' [![Build Status](https://secure.travis-ci.org/kpdecker/jsdiff.svg)](http://travis-ci.org/kpdecker/jsdiff)\n' + + ' [![Sauce Test Status](https://saucelabs.com/buildstatus/jsdiff)](https://saucelabs.com/u/jsdiff)\n' + + ' \n' + + '@@ -227,5 +225,3 @@\n' + + ' \n' + + " * jsdiff keeps track of the diff for each diagonal using a linked list of change objects for each diagonal, rather than the historical array of furthest-reaching D-paths on each diagonal contemplated on page 8 of Myers's paper.\n" + + ' * jsdiff skips considering diagonals where the furthest-reaching D-path would go off the edge of the edit graph. This dramatically reduces the time cost (from quadratic to linear) in cases where the new text just appends or truncates content at the end of the old text.\n' + + '-\n' + + '-bar\n' + + '\n' + + '===================================================================\n' + + '--- b/CONTRIBUTING.md\t\n' + + '+++ a/CONTRIBUTING.md\t\n' + + '@@ -2,8 +2,6 @@\n' + + ' \n' + + ' ## Pull Requests\n' + + ' \n' + + '-bla bla bla\n' + + '-\n' + + ' We also accept [pull requests][pull-request]!\n' + + ' \n' + + ' Generally we like to see pull requests that\n' + ); + }); + }); +});