FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Merge pull request #929 from nodegit/tree-walk-fix · nodegit/nodegit@2caee3c · GitHub

Repository navigation

Commit 2caee3c

Browse files
committed
Merge pull request #929 from nodegit/tree-walk-fix
Fix tree.walk() and add test
2 parents 7bdb380 + 0e64add commit 2caee3c

4 files changed

Lines changed: 107 additions & 9 deletions

File tree

‎.jshintrc‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"maxlen": 80,
1919
"node": true,
2020
"predef": [
21-
"Promise"
21+
"Promise",
22+
"Set"
2223
],
2324
"proto": true,
2425
"quotmark": "double",

‎lib/tree.js‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ Tree.prototype.walk = function(blobsOnly) {
127127
var event = new events.EventEmitter();
128128

129129
var total = 1;
130+
var entries = new Set();
131+
var finalEntires = [];
130132

131133
// This looks like a DFS, but it is a BFS because of implicit queueing in
132134
// the recursive call to `entry.getTree(bfs)`
@@ -136,11 +138,15 @@ Tree.prototype.walk = function(blobsOnly) {
136138
if (error) {
137139
return event.emit("error", error);
138140
}
139-
var entries = tree.entries();
140-
entries.forEach(function (entry, entryIndex) {
141-
if (!blobsOnly || entry.isFile()) {
141+
142+
tree.entries().forEach(function (entry, entryIndex) {
143+
if (!blobsOnly || entry.isFile() && !entries.has(entry)) {
142144
event.emit("entry", entry);
143-
entries.push(entry);
145+
entries.add(entry);
146+
147+
// Node 0.12 doesn't support either [v for (v of entries)] nor
148+
// Array.from so we'll just maintain our own list.
149+
finalEntires.push(entry);
144150
}
145151

146152
if (entry.isTree()) {
@@ -150,7 +156,7 @@ Tree.prototype.walk = function(blobsOnly) {
150156
});
151157

152158
if (total === 0) {
153-
event.emit("end", entries);
159+
event.emit("end", finalEntires);
154160
}
155161
}
156162

‎test/tests/tree.js‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
var assert = require("assert");
2+
var path = require("path");
3+
var local = path.join.bind(path, __dirname);
4+
var promisify = require("promisify-node");
5+
var fse = promisify(require("fs-extra"));
6+
7+
describe("Tree", function() {
8+
var RepoUtils = require("../utils/repository_setup");
9+
10+
var repoPath = local("../repos/tree");
11+
12+
beforeEach(function() {
13+
var test = this;
14+
return RepoUtils.createRepository(repoPath)
15+
.then(function(repo) {
16+
test.repository = repo;
17+
});
18+
});
19+
20+
after(function() {
21+
return fse.remove(repoPath);
22+
});
23+
24+
it("walks its entries and returns the same entries on both progress and end",
25+
function() {
26+
var repo = this.repository;
27+
var file1 = "test.txt";
28+
var file2 = "foo/bar.txt";
29+
// index.addByPath doesn't like \s so normalize only for the expected paths
30+
var expectedPaths = [file1, path.normalize(file2)];
31+
var progressEntries = [];
32+
var endEntries;
33+
34+
return RepoUtils.commitFileToRepo(repo, file1, "")
35+
.then(function(commit) {
36+
return RepoUtils.commitFileToRepo(repo, file2, "", commit);
37+
})
38+
.then(function(commit) {
39+
return commit.getTree();
40+
})
41+
.then(function(tree) {
42+
assert(tree);
43+
44+
return new Promise(function (resolve, reject) {
45+
var walker = tree.walk();
46+
47+
walker.on("entry", function(entry) {
48+
progressEntries.push(entry);
49+
});
50+
walker.on("end", function(entries) {
51+
endEntries = entries;
52+
resolve();
53+
});
54+
walker.on("error", reject);
55+
56+
walker.start();
57+
});
58+
})
59+
.then(function() {
60+
assert(progressEntries.length);
61+
assert(endEntries && endEntries.length);
62+
63+
function getEntryPath(entry) {
64+
return entry.path();
65+
}
66+
67+
var progressFilePaths = progressEntries.map(getEntryPath);
68+
var endFilePaths = endEntries.map(getEntryPath);
69+
70+
assert.deepEqual(
71+
expectedPaths, progressFilePaths,
72+
"progress entry paths do not match expected paths"
73+
);
74+
75+
assert.deepEqual(
76+
expectedPaths, endFilePaths,
77+
"end entry paths do not match expected paths"
78+
);
79+
});
80+
});
81+
});

‎test/utils/repository_setup.js‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,28 @@ var RepositorySetup = {
1818
},
1919

2020
commitFileToRepo:
21-
function commitFileToRepo(repository, fileName, fileContent) {
21+
function commitFileToRepo(repository, fileName, fileContent, parentCommit) {
2222
var repoWorkDir = repository.workdir();
2323
var signature = NodeGit.Signature.create("Foo bar",
2424
"foo@bar.com", 123456789, 60);
2525

26-
return fse.writeFile(path.join(repoWorkDir, fileName), fileContent)
26+
var filePath = path.join(repoWorkDir, fileName);
27+
var parents = [];
28+
if (parentCommit) {
29+
parents.push(parentCommit);
30+
}
31+
32+
// fse.ensure allows us to write files inside new folders
33+
return fse.ensureFile(filePath)
34+
.then(function() {
35+
return fse.writeFile(filePath, fileContent);
36+
})
2737
.then(function() {
2838
return RepositorySetup.addFileToIndex(repository, fileName);
2939
})
3040
.then(function(oid) {
3141
return repository.createCommit("HEAD", signature, signature,
32-
"initial commit", oid, []);
42+
"initial commit", oid, parents);
3343
})
3444
.then(function(commitOid) {
3545
return repository.getCommit(commitOid);

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL