Skip to content

Commit

Permalink
Add function to reverse a patch (#450)
Browse files Browse the repository at this point in the history
* Add reversePatch function and tests

* Document new method

* Add release notes
  • Loading branch information
ExplodingCabbage committed Jan 2, 2024
1 parent 56c6a8a commit 8365367
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 1 deletion.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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


Expand Down
1 change: 1 addition & 0 deletions release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -56,6 +57,7 @@ export {
applyPatches,
parsePatch,
merge,
reversePatch,
convertChangesToDMP,
convertChangesToXML,
canonicalize
Expand Down
27 changes: 27 additions & 0 deletions src/patch/reverse.js
Original file line number Diff line number Diff line change
@@ -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;
})
};
})
};
}
97 changes: 97 additions & 0 deletions test/patch/reverse.js
Original file line number Diff line number Diff line change
@@ -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'
);
});
});
});

0 comments on commit 8365367

Please sign in to comment.