| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,7 +18,8 @@ | |||
| 18 | 18 | "maxlen": 80, | |
| 19 | 19 | "node": true, | |
| 20 | 20 | "predef": [ | |
| 21 | - "Promise" | ||
| 21 | + "Promise", | ||
| 22 | + "Set" | ||
| 22 | 23 | ], | |
| 23 | 24 | "proto": true, | |
| 24 | 25 | "quotmark": "double", | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -127,6 +127,8 @@ Tree.prototype.walk = function(blobsOnly) { | |||
| 127 | 127 | var event = new events.EventEmitter(); | |
| 128 | 128 | ||
| 129 | 129 | var total = 1; | |
| 130 | + var entries = new Set(); | ||
| 131 | + var finalEntires = []; | ||
| 130 | 132 | ||
| 131 | 133 | // This looks like a DFS, but it is a BFS because of implicit queueing in | |
| 132 | 134 | // the recursive call to `entry.getTree(bfs)` | |
@@ -136,11 +138,15 @@ Tree.prototype.walk = function(blobsOnly) { | |||
| 136 | 138 | if (error) { | |
| 137 | 139 | return event.emit("error", error); | |
| 138 | 140 | } | |
| 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)) { | ||
| 142 | 144 | 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); | ||
| 144 | 150 | } | |
| 145 | 151 | ||
| 146 | 152 | if (entry.isTree()) { | |
@@ -150,7 +156,7 @@ Tree.prototype.walk = function(blobsOnly) { | |||
| 150 | 156 | }); | |
| 151 | 157 | ||
| 152 | 158 | if (total === 0) { | |
| 153 | - event.emit("end", entries); | ||
| 159 | + event.emit("end", finalEntires); | ||
| 154 | 160 | } | |
| 155 | 161 | } | |
| 156 | 162 | ||
| Original file line number | Diff line number | Diff 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 | + }); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,18 +18,28 @@ var RepositorySetup = { | |||
| 18 | 18 | }, | |
| 19 | 19 | ||
| 20 | 20 | commitFileToRepo: | |
| 21 | - function commitFileToRepo(repository, fileName, fileContent) { | ||
| 21 | + function commitFileToRepo(repository, fileName, fileContent, parentCommit) { | ||
| 22 | 22 | var repoWorkDir = repository.workdir(); | |
| 23 | 23 | var signature = NodeGit.Signature.create("Foo bar", | |
| 24 | 24 | "foo@bar.com", 123456789, 60); | |
| 25 | 25 | ||
| 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 | + }) | ||
| 27 | 37 | .then(function() { | |
| 28 | 38 | return RepositorySetup.addFileToIndex(repository, fileName); | |
| 29 | 39 | }) | |
| 30 | 40 | .then(function(oid) { | |
| 31 | 41 | return repository.createCommit("HEAD", signature, signature, | |
| 32 | - "initial commit", oid, []); | ||
| 42 | + "initial commit", oid, parents); | ||
| 33 | 43 | }) | |
| 34 | 44 | .then(function(commitOid) { | |
| 35 | 45 | return repository.getCommit(commitOid); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments