From b7da02cecf542f5c27be0abfddb20be98137a0d4 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Wed, 24 Jul 2013 07:23:54 +0000 Subject: [PATCH] fix(diff) rm, work bad with win line ending --- ChangeLog | 3 + lib/client/dom.js | 2 +- lib/client/edit.js | 35 +--- lib/server/main.js | 1 - lib/server/rest.js | 46 ----- lib/util/jsdiff/LICENSE | 31 --- lib/util/jsdiff/README.md | 101 ---------- lib/util/jsdiff/diff.js | 352 ----------------------------------- lib/util/jsdiff/package.json | 39 ---- 9 files changed, 7 insertions(+), 603 deletions(-) delete mode 100644 lib/util/jsdiff/LICENSE delete mode 100644 lib/util/jsdiff/README.md delete mode 100644 lib/util/jsdiff/diff.js delete mode 100644 lib/util/jsdiff/package.json diff --git a/ChangeLog b/ChangeLog index c6af7437..93c57c41 100644 --- a/ChangeLog +++ b/ChangeLog @@ -126,6 +126,9 @@ getJSONfromFileTable. * feature(win) diskpart -> wmic +* fix(diff) rm, work bad with win line ending + + 2012.04.22, v0.2.0 * Added alerting about errors. diff --git a/lib/client/dom.js b/lib/client/dom.js index f4d1f43c..ead6b6fb 100644 --- a/lib/client/dom.js +++ b/lib/client/dom.js @@ -116,7 +116,7 @@ var CloudCmd, Util, DOM, CloudFunc; }); }; - this.save = function(pUrl, pData, pQuery, pCallBack){ + this.save = function(pUrl, pData, pCallBack, pQuery){ sendRequest({ method : 'PUT', url : CloudFunc.FS + pUrl + (pQuery || ''), diff --git a/lib/client/edit.js b/lib/client/edit.js index 0c0bbde9..5bf5b2a7 100644 --- a/lib/client/edit.js +++ b/lib/client/edit.js @@ -71,34 +71,13 @@ var CloudCmd, Util, DOM, JsDiff, ace; name : 'save', bindKey : { win: 'Ctrl-S', mac: 'Command-S' }, exec : function (pEditor) { - var lCurrent = DOM.getCurrentFile(), - lPath = DOM.getCurrentPath(lCurrent), - lName = DOM.getCurrentName(lCurrent), - lValue = Ace.getValue(), - lLength = lValue.length; + var lPath = DOM.getCurrentPath(), + lValue = Ace.getValue(); if ( Util.strCmp(Value, lValue) ) Util.log('edit: nothing to save'); else - diff(lName, lValue, function(pDiff) { - var lData, - lPatch = '', - lDiffLength = pDiff.length; - - if (lDiffLength >= lLength) - lData = lValue; - else { - lData = pDiff; - lPatch = '?patch'; - } - - Value = lValue; - DOM.setCurrentSize( lLength, lCurrent ); - DOM.RESTfull.save( lPath, lData, lPatch, function (pData){ - if (pData === 'patch : fail. trying to save...') - DOM.RESTfull.save( lPath, lValue); - }); - }); + DOM.RESTfull.save(lPath, lValue); } }); } @@ -134,14 +113,6 @@ var CloudCmd, Util, DOM, JsDiff, ace; Ace.moveCursorTo(0, 0); } - function diff(pName, pNewValue, pCallBack){ - DOM.jsload('/lib/util/jsdiff/diff.js', function(){ - var lPatch = JsDiff.createPatch(pName, Value, pNewValue); - - Util.exec(pCallBack, lPatch); - }); - } - function load(pCallBack){ Util.time(Name + ' load'); diff --git a/lib/server/main.js b/lib/server/main.js index f40cd871..3c3892f5 100644 --- a/lib/server/main.js +++ b/lib/server/main.js @@ -78,7 +78,6 @@ /* we can not use librequare here */ exports.util = Util = require(LIBDIR + UTIL), - exports.diff = mrequire(LIBDIR + UTIL + '/jsdiff/diff'), exports.zlib = zlib = mrequire('zlib'), diff --git a/lib/server/rest.js b/lib/server/rest.js index 7a693b65..77db4e65 100644 --- a/lib/server/rest.js +++ b/lib/server/rest.js @@ -19,7 +19,6 @@ Util = main.util, CloudFunc = main.cloudfunc, dir = main.dir, - diff = main.diff, OK = 200, Header = main.generateHeaders({ name:'api.json' @@ -141,51 +140,6 @@ else main.sendError(pParams, pError); }); - else if(lQuery === 'patch') - getBody(p.request, function(pPatch) { - fs.readFile(p.name, Util.call(read, pParams)); - - function read(pParams) { - var lDiff, lStr, p, lData, lName, - lRet = main.checkCallBackParams(pParams) && - main.checkParams(pParams.params); - - if (lRet) { - p = pParams; - lName = p.params.name; - - if (p.error) - main.sendError(p.params, p.error); - else { - lStr = p.data.toString(); - lRet = Util.tryCatchLog(function(){ - lDiff = diff.applyPatch(lStr, pPatch); - }); - - if (lDiff && !lRet) - fs.writeFile(lName, lDiff, Util.call(write, p.params)); - else - main.sendResponse(p.params, 'patch : fail. trying to save...'); - } - } - } - - function write(pParams) { - var p, lName, - lRet = main.checkCallBackParams(pParams) && - main.checkParams(pParams.params); - - if(lRet) { - p = pParams; - if (p.error) - main.sendError(p.params, p.error); - else { - lName = path.basename(p.params.name); - main.sendResponse(p.params, 'patch: ok("' + lName +'")'); - } - } - } - }); else { lWriteStream = fs.createWriteStream(p.name); diff --git a/lib/util/jsdiff/LICENSE b/lib/util/jsdiff/LICENSE deleted file mode 100644 index c135dcf4..00000000 --- a/lib/util/jsdiff/LICENSE +++ /dev/null @@ -1,31 +0,0 @@ -Software License Agreement (BSD License) - -Copyright (c) 2009-2011, Kevin Decker - -All rights reserved. - -Redistribution and use of this software in source and binary forms, with or without modification, -are permitted provided that the following conditions are met: - -* Redistributions of source code must retain the above - copyright notice, this list of conditions and the - following disclaimer. - -* Redistributions in binary form must reproduce the above - copyright notice, this list of conditions and the - following disclaimer in the documentation and/or other - materials provided with the distribution. - -* Neither the name of Kevin Decker nor the names of its - contributors may be used to endorse or promote products - derived from this software without specific prior - written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR -IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR -CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER -IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT -OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/lib/util/jsdiff/README.md b/lib/util/jsdiff/README.md deleted file mode 100644 index 95bd8da2..00000000 --- a/lib/util/jsdiff/README.md +++ /dev/null @@ -1,101 +0,0 @@ -# jsdiff - -[![Build Status](https://secure.travis-ci.org/kpdecker/jsdiff.png)](http://travis-ci.org/kpdecker/jsdiff) - -A javascript text differencing implementation. - -Based on the algorithm proposed in -["An O(ND) Difference Algorithm and its Variations" (Myers, 1986)](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.4.6927). - -## Installation - - npm install diff - -or - - git clone git://github.com/kpdecker/jsdiff.git - -## API - -* JsDiff.diffChars(oldStr, newStr) - Diffs two blocks of text, comparing character by character. - - Returns a list of change objects (See below). - -* JsDiff.diffWords(oldStr, newStr) - Diffs two blocks of text, comparing word by word. - - Returns a list of change objects (See below). - -* JsDiff.diffLines(oldStr, newStr) - Diffs two blocks of text, comparing line by line. - - Returns a list of change objects (See below). - -* JsDiff.diffCss(oldStr, newStr) - Diffs two blocks of text, comparing CSS tokens. - - Returns a list of change objects (See below). - -* JsDiff.createPatch(fileName, oldStr, newStr, oldHeader, newHeader) - Creates a unified diff patch. - - Parameters: - * fileName : String to be output in the filename sections of the patch - * 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.applyPatch(oldStr, diffStr) - Applies a unified diff patch. - - Return a string containing new version of provided data. - -* convertChangesToXML(changes) - Converts a list of changes to a serialized XML format - -### Change Objects -Many of the methods above return change objects. These objects are consist of the following fields: - -* value: Text content -* added: True if the value was inserted into the new string -* removed: True of the value was removed from the old string - -Note that some cases may omit a particular flag field. Comparison on the flag fields should always be done in a truthy or falsy manner. - -## [Example](http://kpdecker.github.com/jsdiff) - -## License - -Software License Agreement (BSD License) - -Copyright (c) 2009-2011, Kevin Decker kpdecker@gmail.com - -All rights reserved. - -Redistribution and use of this software in source and binary forms, with or without modification, -are permitted provided that the following conditions are met: - -* Redistributions of source code must retain the above - copyright notice, this list of conditions and the - following disclaimer. - -* Redistributions in binary form must reproduce the above - copyright notice, this list of conditions and the - following disclaimer in the documentation and/or other - materials provided with the distribution. - -* Neither the name of Kevin Decker nor the names of its - contributors may be used to endorse or promote products - derived from this software without specific prior - written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR -IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR -CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER -IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT -OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/lib/util/jsdiff/diff.js b/lib/util/jsdiff/diff.js deleted file mode 100644 index 4cfe05ba..00000000 --- a/lib/util/jsdiff/diff.js +++ /dev/null @@ -1,352 +0,0 @@ -/* See LICENSE file for terms of use */ - -/* - * Text diff implementation. - * - * This library supports the following APIS: - * JsDiff.diffChars: Character by character diff - * JsDiff.diffWords: Word (as defined by \b regex) diff which ignores whitespace - * JsDiff.diffLines: Line based diff - * - * JsDiff.diffCss: Diff targeted at CSS content - * - * These methods are based on the implementation proposed in - * "An O(ND) Difference Algorithm and its Variations" (Myers, 1986). - * http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.4.6927 - */ -var JsDiff = (function() { - /*jshint maxparams: 5*/ - function clonePath(path) { - return { newPos: path.newPos, components: path.components.slice(0) }; - } - function removeEmpty(array) { - var ret = []; - for (var i = 0; i < array.length; i++) { - if (array[i]) { - ret.push(array[i]); - } - } - return ret; - } - function escapeHTML(s) { - var n = s; - n = n.replace(/&/g, '&'); - n = n.replace(//g, '>'); - n = n.replace(/"/g, '"'); - - return n; - } - - var Diff = function(ignoreWhitespace) { - this.ignoreWhitespace = ignoreWhitespace; - }; - Diff.prototype = { - diff: function(oldString, newString) { - // Handle the identity case (this is due to unrolling editLength == 0 - if (newString === oldString) { - return [{ value: newString }]; - } - if (!newString) { - return [{ value: oldString, removed: true }]; - } - if (!oldString) { - return [{ value: newString, added: true }]; - } - - newString = this.tokenize(newString); - oldString = this.tokenize(oldString); - - var newLen = newString.length, oldLen = oldString.length; - var maxEditLength = newLen + oldLen; - var bestPath = [{ newPos: -1, components: [] }]; - - // Seed editLength = 0 - var oldPos = this.extractCommon(bestPath[0], newString, oldString, 0); - if (bestPath[0].newPos+1 >= newLen && oldPos+1 >= oldLen) { - return bestPath[0].components; - } - - for (var editLength = 1; editLength <= maxEditLength; editLength++) { - for (var diagonalPath = -1*editLength; diagonalPath <= editLength; diagonalPath+=2) { - var basePath; - var addPath = bestPath[diagonalPath-1], - removePath = bestPath[diagonalPath+1]; - oldPos = (removePath ? removePath.newPos : 0) - diagonalPath; - if (addPath) { - // No one else is going to attempt to use this value, clear it - bestPath[diagonalPath-1] = undefined; - } - - var canAdd = addPath && addPath.newPos+1 < newLen; - var canRemove = removePath && 0 <= oldPos && oldPos < oldLen; - if (!canAdd && !canRemove) { - bestPath[diagonalPath] = undefined; - continue; - } - - // Select the diagonal that we want to branch from. We select the prior - // path whose position in the new string is the farthest from the origin - // and does not pass the bounds of the diff graph - if (!canAdd || (canRemove && addPath.newPos < removePath.newPos)) { - basePath = clonePath(removePath); - this.pushComponent(basePath.components, oldString[oldPos], undefined, true); - } else { - basePath = clonePath(addPath); - basePath.newPos++; - this.pushComponent(basePath.components, newString[basePath.newPos], true, undefined); - } - - var oldPos = this.extractCommon(basePath, newString, oldString, diagonalPath); - - if (basePath.newPos+1 >= newLen && oldPos+1 >= oldLen) { - return basePath.components; - } else { - bestPath[diagonalPath] = basePath; - } - } - } - }, - - pushComponent: function(components, value, added, removed) { - var last = components[components.length-1]; - if (last && last.added === added && last.removed === removed) { - // We need to clone here as the component clone operation is just - // as shallow array clone - components[components.length-1] = - {value: this.join(last.value, value), added: added, removed: removed }; - } else { - components.push({value: value, added: added, removed: removed }); - } - }, - extractCommon: function(basePath, newString, oldString, diagonalPath) { - var newLen = newString.length, - oldLen = oldString.length, - newPos = basePath.newPos, - oldPos = newPos - diagonalPath; - while (newPos+1 < newLen && oldPos+1 < oldLen && this.equals(newString[newPos+1], oldString[oldPos+1])) { - newPos++; - oldPos++; - - this.pushComponent(basePath.components, newString[newPos], undefined, undefined); - } - basePath.newPos = newPos; - return oldPos; - }, - - equals: function(left, right) { - var reWhitespace = /\S/; - if (this.ignoreWhitespace && !reWhitespace.test(left) && !reWhitespace.test(right)) { - return true; - } else { - return left === right; - } - }, - join: function(left, right) { - return left + right; - }, - tokenize: function(value) { - return value; - } - }; - - var CharDiff = new Diff(); - - var WordDiff = new Diff(true); - WordDiff.tokenize = function(value) { - return removeEmpty(value.split(/(\s+|\b)/)); - }; - - var CssDiff = new Diff(true); - CssDiff.tokenize = function(value) { - return removeEmpty(value.split(/([{}:;,]|\s+)/)); - }; - - var LineDiff = new Diff(); - LineDiff.tokenize = function(value) { - return value.split(/^/m); - }; - - return { - Diff: Diff, - - diffChars: function(oldStr, newStr) { return CharDiff.diff(oldStr, newStr); }, - diffWords: function(oldStr, newStr) { return WordDiff.diff(oldStr, newStr); }, - diffLines: function(oldStr, newStr) { return LineDiff.diff(oldStr, newStr); }, - - diffCss: function(oldStr, newStr) { return CssDiff.diff(oldStr, newStr); }, - - createPatch: function(fileName, oldStr, newStr, oldHeader, newHeader) { - var ret = []; - - ret.push('Index: ' + fileName); - ret.push('==================================================================='); - ret.push('--- ' + fileName + (typeof oldHeader === 'undefined' ? '' : '\t' + oldHeader)); - ret.push('+++ ' + fileName + (typeof newHeader === 'undefined' ? '' : '\t' + newHeader)); - - var diff = LineDiff.diff(oldStr, newStr); - if (!diff[diff.length-1].value) { - diff.pop(); // Remove trailing newline add - } - diff.push({value: '', lines: []}); // Append an empty value to make cleanup easier - - function contextLines(lines) { - return lines.map(function(entry) { return ' ' + entry; }); - } - function eofNL(curRange, i, current) { - var last = diff[diff.length-2], - isLast = i === diff.length-2, - isLastOfType = i === diff.length-3 && (current.added !== last.added || current.removed !== last.removed); - - // Figure out if this is the last line for the given file and missing NL - if (!/\n$/.test(current.value) && (isLast || isLastOfType)) { - curRange.push('\\ No newline at end of file'); - } - } - - var oldRangeStart = 0, newRangeStart = 0, curRange = [], - oldLine = 1, newLine = 1; - for (var i = 0; i < diff.length; i++) { - var current = diff[i], - lines = current.lines || current.value.replace(/\n$/, '').split('\n'); - current.lines = lines; - - if (current.added || current.removed) { - if (!oldRangeStart) { - var prev = diff[i-1]; - oldRangeStart = oldLine; - newRangeStart = newLine; - - if (prev) { - curRange = contextLines(prev.lines.slice(-4)); - oldRangeStart -= curRange.length; - newRangeStart -= curRange.length; - } - } - curRange.push.apply(curRange, lines.map(function(entry) { return (current.added?'+':'-') + entry; })); - eofNL(curRange, i, current); - - if (current.added) { - newLine += lines.length; - } else { - oldLine += lines.length; - } - } else { - if (oldRangeStart) { - // Close out any changes that have been output (or join overlapping) - if (lines.length <= 8 && i < diff.length-2) { - // Overlapping - curRange.push.apply(curRange, contextLines(lines)); - } else { - // end the range and output - var contextSize = Math.min(lines.length, 4); - ret.push( - '@@ -' + oldRangeStart + ',' + (oldLine-oldRangeStart+contextSize) - + ' +' + newRangeStart + ',' + (newLine-newRangeStart+contextSize) - + ' @@'); - ret.push.apply(ret, curRange); - ret.push.apply(ret, contextLines(lines.slice(0, contextSize))); - if (lines.length <= 4) { - eofNL(ret, i, current); - } - - oldRangeStart = 0; newRangeStart = 0; curRange = []; - } - } - oldLine += lines.length; - newLine += lines.length; - } - } - - return ret.join('\n') + '\n'; - }, - - applyPatch: function(oldStr, uniDiff) { - var diffstr = uniDiff.split('\n'); - var diff = []; - var remEOFNL = false, - addEOFNL = false; - - for (var i = (diffstr[0][0]==='I'?4:0); i < diffstr.length; i++) { - if(diffstr[i][0] === '@') { - var meh = diffstr[i].split(/@@ -(\d+),(\d+) \+(\d+),(\d+) @@/); - diff.unshift({ - start:meh[3], - oldlength:meh[2], - oldlines:[], - newlength:meh[4], - newlines:[] - }); - } else if(diffstr[i][0] === '+') { - diff[0].newlines.push(diffstr[i].substr(1)); - } else if(diffstr[i][0] === '-') { - diff[0].oldlines.push(diffstr[i].substr(1)); - } else if(diffstr[i][0] === ' ') { - diff[0].newlines.push(diffstr[i].substr(1)); - diff[0].oldlines.push(diffstr[i].substr(1)); - } else if(diffstr[i][0] === '\\') { - if (diffstr[i-1][0] === '+') { - remEOFNL = true; - } else if(diffstr[i-1][0] === '-') { - addEOFNL = true; - } - } - } - - var str = oldStr.split('\n'); - for (var i = diff.length - 1; i >= 0; i--) { - var d = diff[i]; - for (var j = 0; j < d.oldlength; j++) { - if(str[d.start-1+j] !== d.oldlines[j]) { - return false; - } - } - Array.prototype.splice.apply(str,[d.start-1,+d.oldlength].concat(d.newlines)); - } - - if (remEOFNL) { - while (!str[str.length-1]) { - str.pop(); - } - } else if (addEOFNL) { - str.push(''); - } - return str.join('\n'); - }, - - convertChangesToXML: function(changes){ - var ret = []; - for ( var i = 0; i < changes.length; i++) { - var change = changes[i]; - if (change.added) { - ret.push(''); - } else if (change.removed) { - ret.push(''); - } - - ret.push(escapeHTML(change.value)); - - if (change.added) { - ret.push(''); - } else if (change.removed) { - ret.push(''); - } - } - return ret.join(''); - }, - - // See: http://code.google.com/p/google-diff-match-patch/wiki/API - convertChangesToDMP: function(changes){ - var ret = [], change; - for ( var i = 0; i < changes.length; i++) { - change = changes[i]; - ret.push([(change.added ? 1 : change.removed ? -1 : 0), change.value]); - } - return ret; - } - }; -})(); - -if (typeof module !== 'undefined') { - module.exports = JsDiff; -} diff --git a/lib/util/jsdiff/package.json b/lib/util/jsdiff/package.json deleted file mode 100644 index e8ec3c3c..00000000 --- a/lib/util/jsdiff/package.json +++ /dev/null @@ -1,39 +0,0 @@ -{ - "name": "diff", - "version": "1.0.5", - "description": "A javascript text diff implementation.", - "keywords": [ - "diff", - "javascript" - ], - "maintainers": [ - "Kevin Decker (http://incaseofstairs.com)" - ], - "bugs": { - "email": "kpdecker@gmail.com", - "url": "http://github.com/kpdecker/jsdiff/issues" - }, - "licenses": [ - { - "type": "BSD", - "url": "http://github.com/kpdecker/jsdiff/blob/master/LICENSE" - } - ], - "repository": { - "type": "git", - "url": "git://github.com/kpdecker/jsdiff.git" - }, - "engines": { - "node": ">=0.3.1" - }, - "main": "./diff", - "scripts": { - "test": "node_modules/.bin/mocha test/*.js" - }, - "dependencies": {}, - "devDependencies": { - "mocha": "~1.6", - "should": "~1.2" - }, - "optionalDependencies": {} -}