diff --git a/.gitignore b/.gitignore index 64ce99175..6ac1022b7 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,4 @@ *.log .DS_STORE +.idea diff --git a/generate/input/descriptor.json b/generate/input/descriptor.json index 0d1e24413..6e636829e 100644 --- a/generate/input/descriptor.json +++ b/generate/input/descriptor.json @@ -489,10 +489,24 @@ } }, "git_diff_tree_to_workdir": { - "ignore": true + "args": { + "old_tree": { + "isOptional": true + }, + "opts": { + "isOptional": true + } + } }, "git_diff_tree_to_workdir_with_index": { - "ignore": true + "args": { + "old_tree": { + "isOptional": true + }, + "opts": { + "isOptional": true + } + } } } }, diff --git a/lib/commit.js b/lib/commit.js index 1bc92b8f2..305ec7dea 100644 --- a/lib/commit.js +++ b/lib/commit.js @@ -173,7 +173,7 @@ Commit.prototype.getDiff = function(callback) { var diffs = parents.map(function(parent) { return parent.getTree().then(function(parentTree) { return commit.getTree().then(function(thisTree) { - return parentTree.diff(thisTree); + return thisTree.diff(parentTree); }); }); }); diff --git a/test/tests/diff.js b/test/tests/diff.js index 23800e6ff..ecb9001e6 100644 --- a/test/tests/diff.js +++ b/test/tests/diff.js @@ -1,24 +1,66 @@ var assert = require("assert"); var path = require("path"); +var promisify = require("promisify-node"); +var fse = promisify(require("fs-extra")); +var Diff = require("../../lib/diff"); describe("Diff", function() { + var Repository = require("../../lib/repository"); var reposPath = path.resolve("test/repos/workdir/.git"); var oid = "fce88902e66c72b5b93e75bdb5ae717038b221f6"; + var diffFilename = "wddiff.txt"; + var diffFilepath = path.join( + path.resolve("test/repos/workdir"), + diffFilename + ); - var Repository = require("../../lib/repository"); - var Diff = require("../../lib/diff"); - - before(function() { + before(function(done) { var test = this; return Repository.open(reposPath).then(function(repository) { test.repository = repository; - return repository.getCommit(oid).then(function(commit) { - test.commit = commit; + return repository.getBranchCommit("master").then(function(masterCommit) { + + return masterCommit.getTree().then(function(tree) { + test.masterCommitTree = tree; + + return repository.getCommit(oid).then(function(commit) { + test.commit = commit; - return commit.getDiff().then(function(diff) { - test.diff = diff; + return commit.getDiff().then(function(diff) { + test.diff = diff; + + fse.writeFile(diffFilepath, "1 line\n2 line\n3 line\n\n4") + .then(function() { + return test.repository.openIndex(); + }) + .then(function(indexResult) { + test.index = indexResult; + return test.index.read(1); + }) + .then(function() { + return test.index.addByPath(diffFilename); + }) + .then(function() { + return test.index.write(); + }) + .then(function() { + return test.index.writeTree(); + }) + .then(function() { + Diff.treeToWorkdirWithIndex( + test.repository, + test.masterCommitTree, + null + ) + .then(function(workdirDiff) { + test.workdirDiff = workdirDiff; + done(); + }); + }); + }); + }); }); }); }); @@ -40,17 +82,31 @@ describe("Diff", function() { assert.equal(lines[1].origin(), Diff.LINE.CONTEXT); assert.equal(lines[2].origin(), Diff.LINE.CONTEXT); - var oldContent = "\n__Before submitting a pull request, please ensure " + + var oldContent = "__Before submitting a pull request, please ensure " + "both unit tests and lint checks pass.__\n"; - assert.equal(lines[2].content(), oldContent); + assert.equal(lines[3].content(), oldContent); assert.equal(lines[3].origin(), Diff.LINE.DELETION); - assert.equal(lines[4].contentLen(), 90); + 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[3].content(), newContent); + assert.equal(lines[4].content(), newContent); assert.equal(lines[4].origin(), Diff.LINE.ADDITION); - assert.equal(lines[3].contentLen(), 162); + assert.equal(lines[4].contentLen(), 162); + }); + + it("can diff the workdir with index", function() { + var patches = this.workdirDiff.patches(); + assert.equal(patches.length, 1); + + var hunks = patches[0].hunks(); + assert.equal(hunks.length, 1); + + var lines = hunks[0].lines(); + assert.equal( + lines[0].content().substr(0, lines[0].contentLen()), + "1 line\n" + ); }); });