-
Notifications
You must be signed in to change notification settings - Fork 488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add function to reverse a patch #450
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}) | ||
}; | ||
}) | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's a pre-existing issue orthogonal to the main point of this PR, but the way the original header gets screwed up here is a bit distasteful. I should dig in and make an issue before merging this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Issue: #454