Visitar URL original
Fix Tree.Prototype.entryById and .getEntry by miachenmtl · Pull Request #1731 · nodegit/nodegit · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
  • Loading branch information
miachenmtl committed Oct 2, 2019
commit bd3f48cc52ec22b5aa0221a81af901ad045be3c8
2 changes: 2 additions & 0 deletions generate/input/descriptor.json
Original file line number Diff line number Diff line change
Expand Up @@ -4262,6 +4262,7 @@
}
},
"git_tree_entry_byid": {
"jsFunctionName": "_entryById",
"return": {
"ownedByThis": true
}
Expand All @@ -4279,6 +4280,7 @@
}
},
"git_tree_entry_bypath": {
"jsFunctionName": "_entryByPath",
"isAsync": true,
"args": {
"out": {
Expand Down
14 changes: 13 additions & 1 deletion lib/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion test/tests/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
36 changes: 26 additions & 10 deletions test/tests/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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();
Expand All @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion test/tests/tree_entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});
});
Expand Down