Skip to content

Commit

Permalink
Merge pull request #56 from rgeissert/twofilespatch
Browse files Browse the repository at this point in the history
Two files patch
  • Loading branch information
kpdecker committed May 4, 2015
2 parents 2b9ec73 + beede57 commit 62d7734
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,20 @@ or

Returns a list of change objects (See below).

* `JsDiff.createPatch(fileName, oldStr, newStr, oldHeader, newHeader)` - creates a unified diff patch.
* `JsDiff.createTwoFilesPatch(oldFileName, newFileName, oldStr, newStr, oldHeader, newHeader)` - creates a unified diff patch.

Parameters:
* `fileName` : String to be output in the filename sections of the patch
* `oldFileName` : String to be output in the filename section of the patch for the removals
* `newFileName` : String to be output in the filename section of the patch for the additions
* `oldStr` : Original string value
* `newStr` : New string value
* `oldHeader` : Additional information to include in the old file header
* `newHeader` : Additional information to include in thew new file header

* `JsDiff.createPatch(fileName, oldStr, newStr, oldHeader, newHeader)` - creates a unified diff patch.

Just like JsDiff.createTwoFilesPatch, but with oldFileName being equal to newFileName.

* `JsDiff.applyPatch(oldStr, diffStr)` - applies a unified diff patch.

Return a string containing new version of provided data.
Expand Down
14 changes: 10 additions & 4 deletions diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,13 +370,15 @@
);
},

createPatch: function(fileName, oldStr, newStr, oldHeader, newHeader) {
createTwoFilesPatch: function(oldFileName, newFileName, oldStr, newStr, oldHeader, newHeader) {
var ret = [];

ret.push('Index: ' + fileName);
if (oldFileName == newFileName) {
ret.push('Index: ' + oldFileName);
}
ret.push('===================================================================');
ret.push('--- ' + fileName + (typeof oldHeader === 'undefined' ? '' : '\t' + oldHeader));
ret.push('+++ ' + fileName + (typeof newHeader === 'undefined' ? '' : '\t' + newHeader));
ret.push('--- ' + oldFileName + (typeof oldHeader === 'undefined' ? '' : '\t' + oldHeader));
ret.push('+++ ' + newFileName + (typeof newHeader === 'undefined' ? '' : '\t' + newHeader));

var diff = LineDiff.diff(oldStr, newStr);
if (!diff[diff.length-1].value) {
Expand Down Expand Up @@ -455,6 +457,10 @@
return ret.join('\n') + '\n';
},

createPatch: function(fileName, oldStr, newStr, oldHeader, newHeader) {
return JsDiff.createTwoFilesPatch(fileName, fileName, oldStr, newStr, oldHeader, newHeader);
},

applyPatch: function(oldStr, uniDiff) {
var diffstr = uniDiff.split('\n');
var diff = [];
Expand Down

0 comments on commit 62d7734

Please sign in to comment.