Skip to content

Commit 409ebb3

Browse files
committed
Fix TreeEntry.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.
1 parent 1047f66 commit 409ebb3

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed

generate/input/descriptor.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4262,6 +4262,7 @@
42624262
}
42634263
},
42644264
"git_tree_entry_byid": {
4265+
"jsFunctionName": "_entryById",
42654266
"return": {
42664267
"ownedByThis": true
42674268
}
@@ -4279,6 +4280,7 @@
42794280
}
42804281
},
42814282
"git_tree_entry_bypath": {
4283+
"jsFunctionName": "_entryByPath",
42824284
"isAsync": true,
42834285
"args": {
42844286
"out": {

lib/tree.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ Tree.prototype.entries = function() {
7373
return result;
7474
};
7575

76+
/**
77+
* Get an entry by oid (does not search subtrees).
78+
*
79+
* @param {String|Oid} id sha or Oid
80+
* @return {TreeEntry}
81+
*/
82+
Tree.prototype.entryById = function(id) {
83+
var entry = this._entryById(id);
84+
entry.parent = this;
85+
return entry;
86+
};
87+
7688
/**
7789
* Get an entry at the ith position.
7890
*
@@ -107,7 +119,7 @@ Tree.prototype.entryByName = function(name) {
107119
Tree.prototype.getEntry = function(filePath, callback) {
108120
var tree = this;
109121

110-
return this.entryByPath(filePath).then(function(entry) {
122+
return this._entryByPath(filePath).then(function(entry) {
111123
entry.parent = tree;
112124
entry.dirtoparent = path.dirname(filePath);
113125

test/tests/tree.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,19 @@ describe("Tree", function() {
1010
var repoPath = local("../repos/tree");
1111
var existingPath = local("../repos/workdir");
1212
var oid = "5716e9757886eaf38d51c86b192258c960d9cfea";
13+
var file1 = "test.txt";
14+
var file2 = "foo/bar.txt";
1315

1416
beforeEach(function() {
1517
var test = this;
1618
return RepoUtils.createRepository(repoPath)
1719
.then(function(repo) {
1820
test.repository = repo;
21+
return RepoUtils.commitFileToRepo(repo, file1, "");
22+
}).then(function(commit) {
23+
return RepoUtils.commitFileToRepo(test.repository, file2, "", commit);
24+
}).then(function(commit) {
25+
test.repositoryCommit = commit;
1926
}).then(function() {
2027
return NodeGit.Repository.open(existingPath);
2128
}).then(function(repository) {
@@ -30,6 +37,14 @@ describe("Tree", function() {
3037
return fse.remove(repoPath);
3138
});
3239

40+
it("gets an entry by id",
41+
function(done) {
42+
this.commit.getTree().then(function(tree) {
43+
var entry = tree.entryById("6cb45ba5d32532bf0d1310dc31ca4f20f59964bc");
44+
assert(entry);
45+
}).done(done);
46+
});
47+
3348
it("gets an entry by name",
3449
function(done) {
3550
this.commit.getTree().then(function(tree) {
@@ -38,6 +53,14 @@ describe("Tree", function() {
3853
}).done(done);
3954
});
4055

56+
it("gets an entry by path",
57+
function(done) {
58+
this.commit.getTree().then(function(tree) {
59+
var entry = tree.getEntry(file2);
60+
assert(entry);
61+
}).done(done);
62+
});
63+
4164
it("updates a tree", function () {
4265
var repo = this.existingRepo;
4366
var update = new NodeGit.TreeUpdate();
@@ -58,20 +81,13 @@ describe("Tree", function() {
5881

5982
it("walks its entries and returns the same entries on both progress and end",
6083
function() {
61-
var repo = this.repository;
62-
var file1 = "test.txt";
63-
var file2 = "foo/bar.txt";
84+
var commit = this.repositoryCommit;
85+
6486
var expectedPaths = [file1, file2];
6587
var progressEntries = [];
6688
var endEntries;
6789

68-
return RepoUtils.commitFileToRepo(repo, file1, "")
69-
.then(function(commit) {
70-
return RepoUtils.commitFileToRepo(repo, file2, "", commit);
71-
})
72-
.then(function(commit) {
73-
return commit.getTree();
74-
})
90+
return commit.getTree()
7591
.then(function(tree) {
7692
assert(tree);
7793

0 commit comments

Comments
 (0)