diff --git a/generate/templates/templates/nodegit.js b/generate/templates/templates/nodegit.js index acf31dd05..99e2b4a0a 100644 --- a/generate/templates/templates/nodegit.js +++ b/generate/templates/templates/nodegit.js @@ -71,6 +71,7 @@ require("./utils/lookup_wrapper"); require("./utils/normalize_options"); // Load up extra types; +require("./convenient_line"); require("./convenient_hunk"); require("./convenient_patch"); require("./status_file"); diff --git a/lib/convenient_hunk.js b/lib/convenient_hunk.js index 9c28dd296..ca3d8f0e6 100644 --- a/lib/convenient_hunk.js +++ b/lib/convenient_hunk.js @@ -1,4 +1,5 @@ var NodeGit = require("../"); +var ConvenientLine = NodeGit.ConvenientLine; function ConvenientHunk(raw, i) { this.raw = raw; @@ -29,7 +30,7 @@ ConvenientHunk.prototype.size = function() { ConvenientHunk.prototype.lines = function() { var result = []; for (var i = 0; i < this.size(); i++) { - result.push(this.raw.getLineInHunk(this.i, i)); + result.push(new ConvenientLine(this.raw.getLineInHunk(this.i, i), i)); } return result; }; diff --git a/lib/convenient_line.js b/lib/convenient_line.js new file mode 100644 index 000000000..2d39f6501 --- /dev/null +++ b/lib/convenient_line.js @@ -0,0 +1,73 @@ +var NodeGit = require("../"); + +function ConvenientLine(raw, i) { + this.raw = raw; + this.i = i; +} + +/** +* The content of the line +* @return {String} +*/ +ConvenientLine.prototype.origin = function() { + return this.raw.origin(); +}; + +/** +* The content of the line +* @return {String} +*/ +ConvenientLine.prototype.oldLineno = function() { + return this.raw.oldLineno(); +}; + +/** +* The content of the line +* @return {String} +*/ +ConvenientLine.prototype.newLineno = function() { + return this.raw.newLineno(); +}; + +/** +* The content of the line +* @return {String} +*/ +ConvenientLine.prototype.numLines = function() { + return this.raw.numLines(); +}; + +/** +* The content of the line +* @return {String} +*/ +ConvenientLine.prototype.contentLen = function() { + return this.raw.contentLen(); +}; + + +/** +* The content of the line +* @return {String} +*/ +ConvenientLine.prototype.contentOffset = function() { + return this.raw.contentOffset(); +}; + +/** +* The content of the line +* @return {String} +*/ +ConvenientLine.prototype.rawContent = function() { + return this.raw.content(); +}; + +/** +* The relevant line +* @return {String} +*/ +ConvenientLine.prototype.content = function() { + return this.raw.content().substring(0, this.raw.contentLen() - 1); +}; + +NodeGit.ConvenientLine = ConvenientLine; diff --git a/test/tests/diff.js b/test/tests/diff.js index 96c720a0e..dd8f6d8d8 100644 --- a/test/tests/diff.js +++ b/test/tests/diff.js @@ -108,14 +108,14 @@ describe("Diff", function() { var oldContent = "__Before submitting a pull request, please ensure " + "both unit tests and lint checks pass.__\n"; - assert.equal(lines[3].content(), oldContent); + assert.equal(lines[3].rawContent(), oldContent); assert.equal(lines[3].origin(), Diff.LINE.DELETION); assert.equal(lines[3].contentLen(), 90); var newContent = "__Before submitting a pull request, please ensure " + "both that you've added unit tests to cover your shiny new code, " + "and that all unit tests and lint checks pass.__\n"; - assert.equal(lines[4].content(), newContent); + assert.equal(lines[4].rawContent(), newContent); assert.equal(lines[4].origin(), Diff.LINE.ADDITION); assert.equal(lines[4].contentLen(), 162); }); @@ -134,6 +134,17 @@ describe("Diff", function() { assert.equal(newFile.size(), 23); }); + it("can resolve individual line chages from the patch hunks", function() { + this.workdirDiff.patches().forEach(function(convenientPatch) { + convenientPatch.hunks().forEach(function(convenientHunk) { + convenientHunk.lines().forEach(function(line) { + assert(!/\n/.exec(line.content())); + assert(/\n/.exec(line.rawContent())); + }); + }); + }); + }); + it("can diff with a null tree", function() { var repo = this.repository; var tree = this.masterCommitTree;