From d249461af876a34343aa36fe8fe2d59cf5a1066d Mon Sep 17 00:00:00 2001 From: John Haley Date: Tue, 13 Jan 2015 14:40:09 -0700 Subject: [PATCH 1/6] Added in some diff functions from libgit2 New functions: git_diff_tree_to_workdir git_diff_tree_to_workdir_with_index --- generate/input/descriptor.json | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) 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 + } + } } } }, From d9be77053c0503f2b5a5c4d822a357c4323ea063 Mon Sep 17 00:00:00 2001 From: Kris Ciccarello Date: Fri, 16 Jan 2015 14:37:55 -0500 Subject: [PATCH 2/6] Added failing test for `Diff.treeToWorkdirWithIndex` --- .gitignore | 1 + test/tests/diff.js | 44 ++++++++++++++++++++++++++++++++++++++------ 2 files changed, 39 insertions(+), 6 deletions(-) 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/test/tests/diff.js b/test/tests/diff.js index 23800e6ff..c874589af 100644 --- a/test/tests/diff.js +++ b/test/tests/diff.js @@ -1,24 +1,34 @@ var assert = require("assert"); var path = require("path"); +var fs = require("fs"); +var exec = require("child_process").exec; +var Diff = require("../../lib/diff"); describe("Diff", function() { var reposPath = path.resolve("test/repos/workdir/.git"); var oid = "fce88902e66c72b5b93e75bdb5ae717038b221f6"; 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 commit.getDiff().then(function(diff) { - test.diff = diff; + 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; + done(); + }); + }); }); }); }); @@ -53,4 +63,26 @@ describe("Diff", function() { assert.equal(lines[4].origin(), Diff.LINE.ADDITION); assert.equal(lines[3].contentLen(), 162); }); + + it("can diff the workdir with index", function(done) { + var test = this; + var newFilePath = path.join(path.resolve("test/repos/workdir"), "wddiff.txt"); + + fs.writeFileSync(newFilePath, "1 line\n2 line\n3 line\n\n4"); + + exec("git add wddiff.txt", {cwd: path.resolve("test/repos/workdir")}, function() { + Diff.treeToWorkdirWithIndex(test.repository, test.masterCommitTree, null) + .then(function(workdirDiff) { + var patches = 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(), "1 line\n"); + done(); + }); + }); + }); }); From fe26afd071233344ebb6dd333cef47c59bf010fc Mon Sep 17 00:00:00 2001 From: Kris Ciccarello Date: Sat, 17 Jan 2015 16:39:40 -0500 Subject: [PATCH 3/6] Shuffled around workdir diff unit test stubs --- test/tests/diff.js | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/test/tests/diff.js b/test/tests/diff.js index c874589af..19d3a516a 100644 --- a/test/tests/diff.js +++ b/test/tests/diff.js @@ -12,6 +12,7 @@ describe("Diff", function() { before(function(done) { var test = this; + var newFilePath = path.join(path.resolve("test/repos/workdir"), "wddiff.txt"); return Repository.open(reposPath).then(function(repository) { test.repository = repository; @@ -26,7 +27,15 @@ describe("Diff", function() { return commit.getDiff().then(function(diff) { test.diff = diff; - done(); + fs.writeFileSync(newFilePath, "1 line\n2 line\n3 line\n\n4"); + + exec("git add wddiff.txt", {cwd: path.resolve("test/repos/workdir")}, function() { + Diff.treeToWorkdirWithIndex(test.repository, test.masterCommitTree, null) + .then(function(workdirDiff) { + test.workdirDiff = workdirDiff; + done(); + }); + }); }); }); }); @@ -64,25 +73,14 @@ describe("Diff", function() { assert.equal(lines[3].contentLen(), 162); }); - it("can diff the workdir with index", function(done) { - var test = this; - var newFilePath = path.join(path.resolve("test/repos/workdir"), "wddiff.txt"); - - fs.writeFileSync(newFilePath, "1 line\n2 line\n3 line\n\n4"); + it("can diff the workdir with index", function() { + var patches = this.workdirDiff.patches(); + assert.equal(patches.length, 1); - exec("git add wddiff.txt", {cwd: path.resolve("test/repos/workdir")}, function() { - Diff.treeToWorkdirWithIndex(test.repository, test.masterCommitTree, null) - .then(function(workdirDiff) { - var patches = workdirDiff.patches(); - assert.equal(patches.length, 1); + var hunks = patches[0].hunks(); + assert.equal(hunks.length, 1); - var hunks = patches[0].hunks(); - assert.equal(hunks.length, 1); - - var lines = hunks[0].lines(); - assert.equal(lines[0].content(), "1 line\n"); - done(); - }); - }); + var lines = hunks[0].lines(); + assert.equal(lines[0].content(), "1 line\n"); }); }); From d9f49f807adcc44b6c1ccf952268b8d1dd7153f7 Mon Sep 17 00:00:00 2001 From: Kris Ciccarello Date: Sat, 17 Jan 2015 19:09:36 -0500 Subject: [PATCH 4/6] Diff workdir tests pass DiffLine objects have all content and need to be parsed out based on the content length. --- test/tests/diff.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/tests/diff.js b/test/tests/diff.js index 19d3a516a..b707fc6e0 100644 --- a/test/tests/diff.js +++ b/test/tests/diff.js @@ -81,6 +81,6 @@ describe("Diff", function() { assert.equal(hunks.length, 1); var lines = hunks[0].lines(); - assert.equal(lines[0].content(), "1 line\n"); + assert.equal(lines[0].content().substr(0, lines[0].contentLen()), "1 line\n"); }); }); From 6c049ab4b6afcebeb90e8666d62ebe3581955b37 Mon Sep 17 00:00:00 2001 From: Kris Ciccarello Date: Sun, 18 Jan 2015 12:41:43 -0500 Subject: [PATCH 5/6] `commit.getDiff` was diffing backwards --- lib/commit.js | 2 +- test/tests/diff.js | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) 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 b707fc6e0..b4a9968ac 100644 --- a/test/tests/diff.js +++ b/test/tests/diff.js @@ -59,18 +59,18 @@ 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() { From 7ac4dc3261e5324531e63faeeab2a96e0a2f9c09 Mon Sep 17 00:00:00 2001 From: Kris Ciccarello Date: Tue, 20 Jan 2015 07:07:16 -0500 Subject: [PATCH 6/6] Diff test: use nodegit to add diff file to index --- test/tests/diff.js | 48 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/test/tests/diff.js b/test/tests/diff.js index b4a9968ac..ecb9001e6 100644 --- a/test/tests/diff.js +++ b/test/tests/diff.js @@ -1,18 +1,21 @@ var assert = require("assert"); var path = require("path"); -var fs = require("fs"); -var exec = require("child_process").exec; +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 Repository = require("../../lib/repository"); + var diffFilename = "wddiff.txt"; + var diffFilepath = path.join( + path.resolve("test/repos/workdir"), + diffFilename + ); before(function(done) { var test = this; - var newFilePath = path.join(path.resolve("test/repos/workdir"), "wddiff.txt"); return Repository.open(reposPath).then(function(repository) { test.repository = repository; @@ -27,13 +30,33 @@ describe("Diff", function() { return commit.getDiff().then(function(diff) { test.diff = diff; - fs.writeFileSync(newFilePath, "1 line\n2 line\n3 line\n\n4"); - exec("git add wddiff.txt", {cwd: path.resolve("test/repos/workdir")}, function() { - Diff.treeToWorkdirWithIndex(test.repository, test.masterCommitTree, null) + 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(); + test.workdirDiff = workdirDiff; + done(); }); }); }); @@ -81,6 +104,9 @@ describe("Diff", function() { assert.equal(hunks.length, 1); var lines = hunks[0].lines(); - assert.equal(lines[0].content().substr(0, lines[0].contentLen()), "1 line\n"); + assert.equal( + lines[0].content().substr(0, lines[0].contentLen()), + "1 line\n" + ); }); });