From bd3f48cc52ec22b5aa0221a81af901ad045be3c8 Mon Sep 17 00:00:00 2001 From: Mia Date: Tue, 1 Oct 2019 21:48:31 -0400 Subject: [PATCH] Fix Tree.prototype.entryById and .getEntry * entryById now follows pattern of entryByName and entryByIndex where the parent property is added to the entry. * entryByPath renamed to _entryByPath since getEntry should be used instead. * Simple test for entryById added. * Setup of commits used in test for TreeEntry.walk moved to test setup so getEntry can test searching by path as well. * Tests using Tree.prototype.entryByPath now use Tree.prototype.getEntry. --- generate/input/descriptor.json | 2 ++ lib/tree.js | 14 ++++++++++++- test/tests/filter.js | 2 +- test/tests/tree.js | 36 ++++++++++++++++++++++++---------- test/tests/tree_entry.js | 2 +- 5 files changed, 43 insertions(+), 13 deletions(-) diff --git a/generate/input/descriptor.json b/generate/input/descriptor.json index 5aaf5d0d9..1df6b7b4a 100644 --- a/generate/input/descriptor.json +++ b/generate/input/descriptor.json @@ -4262,6 +4262,7 @@ } }, "git_tree_entry_byid": { + "jsFunctionName": "_entryById", "return": { "ownedByThis": true } @@ -4279,6 +4280,7 @@ } }, "git_tree_entry_bypath": { + "jsFunctionName": "_entryByPath", "isAsync": true, "args": { "out": { diff --git a/lib/tree.js b/lib/tree.js index 1be687075..0f25d38c9 100644 --- a/lib/tree.js +++ b/lib/tree.js @@ -73,6 +73,18 @@ Tree.prototype.entries = function() { return result; }; +/** + * Get an entry by oid (does not search subtrees). + * + * @param {String|Oid} id sha or Oid + * @return {TreeEntry} + */ +Tree.prototype.entryById = function(id) { + var entry = this._entryById(id); + entry.parent = this; + return entry; +}; + /** * Get an entry at the ith position. * @@ -107,7 +119,7 @@ Tree.prototype.entryByName = function(name) { Tree.prototype.getEntry = function(filePath, callback) { var tree = this; - return this.entryByPath(filePath).then(function(entry) { + return this._entryByPath(filePath).then(function(entry) { entry.parent = tree; entry.dirtoparent = path.dirname(filePath); diff --git a/test/tests/filter.js b/test/tests/filter.js index 62aa4e863..d22b7f200 100644 --- a/test/tests/filter.js +++ b/test/tests/filter.js @@ -1065,7 +1065,7 @@ describe("Filter", function() { return commit.getTree(); }) .then(function(tree) { - return tree.entryByPath("README.md"); + return tree.getEntry("README.md"); }) .then(function(entry) { return test.repository.getBlob(entry.id()); diff --git a/test/tests/tree.js b/test/tests/tree.js index 98c5959e9..f5fbef07b 100644 --- a/test/tests/tree.js +++ b/test/tests/tree.js @@ -10,12 +10,19 @@ describe("Tree", function() { var repoPath = local("../repos/tree"); var existingPath = local("../repos/workdir"); var oid = "5716e9757886eaf38d51c86b192258c960d9cfea"; + var file1 = "test.txt"; + var file2 = "foo/bar.txt"; beforeEach(function() { var test = this; return RepoUtils.createRepository(repoPath) .then(function(repo) { test.repository = repo; + return RepoUtils.commitFileToRepo(repo, file1, ""); + }).then(function(commit) { + return RepoUtils.commitFileToRepo(test.repository, file2, "", commit); + }).then(function(commit) { + test.repositoryCommit = commit; }).then(function() { return NodeGit.Repository.open(existingPath); }).then(function(repository) { @@ -30,6 +37,14 @@ describe("Tree", function() { return fse.remove(repoPath); }); + it("gets an entry by id", + function(done) { + this.commit.getTree().then(function(tree) { + var entry = tree.entryById("6cb45ba5d32532bf0d1310dc31ca4f20f59964bc"); + assert(entry); + }).done(done); + }); + it("gets an entry by name", function(done) { this.commit.getTree().then(function(tree) { @@ -38,6 +53,14 @@ describe("Tree", function() { }).done(done); }); + it("gets an entry by path", + function(done) { + this.commit.getTree().then(function(tree) { + var entry = tree.getEntry(file2); + assert(entry); + }).done(done); + }); + it("updates a tree", function () { var repo = this.existingRepo; var update = new NodeGit.TreeUpdate(); @@ -58,20 +81,13 @@ describe("Tree", function() { it("walks its entries and returns the same entries on both progress and end", function() { - var repo = this.repository; - var file1 = "test.txt"; - var file2 = "foo/bar.txt"; + var commit = this.repositoryCommit; + var expectedPaths = [file1, file2]; var progressEntries = []; var endEntries; - return RepoUtils.commitFileToRepo(repo, file1, "") - .then(function(commit) { - return RepoUtils.commitFileToRepo(repo, file2, "", commit); - }) - .then(function(commit) { - return commit.getTree(); - }) + return commit.getTree() .then(function(tree) { assert(tree); diff --git a/test/tests/tree_entry.js b/test/tests/tree_entry.js index 086a4f8d2..23ecc3369 100644 --- a/test/tests/tree_entry.js +++ b/test/tests/tree_entry.js @@ -184,7 +184,7 @@ describe("TreeEntry", function() { return leakTest(NodeGit.TreeEntry, function() { return test.commit.getTree() .then(function(tree) { - return tree.entryByPath("example"); + return tree.getEntry("example"); }); }); });