Skip to content

Commit 622e5d9

Browse files
committed
Fix issues with running tests, refactor to promises
1 parent e482010 commit 622e5d9

File tree

8 files changed

+154
-210
lines changed

8 files changed

+154
-210
lines changed

generate/descriptor.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,7 @@
484484
"functions": {
485485
"git_reference_lookup": {
486486
"ignore": false,
487-
"isConstructorMethod": true,
488-
"args": [{}, { "isSelf": true }]
487+
"isConstructorMethod": true
489488
},
490489

491490
"git_reference_type": {

lib/commit.js

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var Promise = require("promise");
12
var git = require('../');
23
var Commit = git.Commit;
34
var Oid = git.Oid;
@@ -111,21 +112,54 @@ Commit.prototype.history = function() {
111112
/**
112113
* Retrieve the commit's parents -- as commit objects.
113114
*
115+
* @param {number} limit - Optional amount of parents to return.
114116
* @param {Function} callback
115117
* @return {[Commit]} array of commits
116118
*/
117-
Commit.prototype.getParents = function(callback) {
118-
var self = this;
119-
function processParents(commit, n, acc, callback) {
120-
if (n < 0) return callback(null, acc);
119+
Commit.prototype.getParents = function(limit, callback) {
120+
var commit = this;
121+
var parents = [];
122+
var i = 0;
123+
124+
// Shift arguments.
125+
if (typeof limit === "function") {
126+
callback = limit;
127+
}
128+
129+
// If no limit was set, default to the maximum parents.
130+
limit = typeof limit === "number" ? limit : this.parentCount();
131+
132+
function processParents(commit, callback) {
133+
var oid = commit.parentId(i);
134+
135+
var parent = commit.repo.getCommit(oid).then(function(parent) {
136+
if (--limit) {
137+
i = i + 1;
138+
return processParents(parent, callback);
139+
}
121140

122-
self.repo.getCommit(self.parentId(n), function nextParent(error, parent) {
123-
if (error) return callback(error);
124-
processParents(parent, n-1, acc.concat([parent]), callback);
141+
return parent;
125142
});
143+
144+
// Add this parent to the list.
145+
parents.push(parent);
146+
147+
return parent;
126148
}
127149

128-
processParents(this, this.parentCount() - 1, [], callback);
150+
// Process all the parents.
151+
if (limit) {
152+
processParents(this, callback);
153+
}
154+
155+
// Wait for all parents to complete, before returning.
156+
return Promise.all(parents).then(function(parents) {
157+
if (callback) {
158+
callback(null, parents);
159+
}
160+
161+
return parents;
162+
}, callback);
129163
};
130164

131165
/**

lib/repository.js

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@ var oldGetReference = Reference.lookup,
3131
* @return {Branch}
3232
*/
3333
Repo.prototype.getBranch = function(name, callback) {
34-
var self = this;
35-
this.getReference('refs/heads/' + name, function referenceLookupCallback(error, reference) {
36-
if (error) return callback(error);
34+
var repository = this;
3735

38-
self.getCommit(reference.target(), function commitLookupCallback(error, commit) {
39-
if (error) return callback(error);
36+
return this.getReference('refs/heads/' + name).then(function(reference) {
37+
return repository.getCommit(reference.target()).then(function(commit) {
38+
if (callback) {
39+
callback(null, commit);
40+
}
4041

41-
callback(null, commit);
42+
return commit;
4243
});
4344
});
4445
};
45-
util.makeSafe(Repo.prototype, 'getBranch');
4646

4747
/**
4848
* Lookup the reference with the given name.
@@ -52,23 +52,27 @@ util.makeSafe(Repo.prototype, 'getBranch');
5252
* @return {Reference}
5353
*/
5454
Repo.prototype.getReference = function(name, callback) {
55-
var self = this;
56-
oldGetReference.call(this, name, function(error, reference) {
57-
if (error) return callback(error);
55+
var repository = this;
5856

57+
return Reference.lookup(this, name).then(function(reference) {
5958
if (reference.type() == Reference.Type.Symbolic) {
60-
reference.resolve(function (error, reference) {
59+
return reference.resolve(function (error, reference) {
6160
if (error) return callback(error);
62-
reference.repo = self;
63-
callback(null, reference);
61+
reference.repo = repository;
62+
if (callback) {
63+
callback(null, reference);
64+
}
65+
return reference;
6466
});
6567
} else {
66-
reference.repo = self;
67-
callback(null, reference);
68+
reference.repo = repository;
69+
if (callback) {
70+
callback(null, reference);
71+
}
72+
return reference;
6873
}
6974
});
7075
};
71-
util.makeSafe(Repo.prototype, 'getReference');
7276

7377
/**
7478
* Retrieve the commit identified by oid.
@@ -78,9 +82,11 @@ util.makeSafe(Repo.prototype, 'getReference');
7882
* @return {Commit}
7983
*/
8084
Repo.prototype.getCommit = function(oid, callback) {
85+
oid = normalizeOid(oid);
86+
8187
var repository = this;
8288

83-
return Commit.lookup(repository, normalizeOid(oid)).then(function(commit) {
89+
return Commit.lookup(repository, oid).then(function(commit) {
8490
commit.repo = repository;
8591

8692
if (callback) {
@@ -99,9 +105,11 @@ Repo.prototype.getCommit = function(oid, callback) {
99105
* @return {Blob}
100106
*/
101107
Repo.prototype.getBlob = function(oid, callback) {
108+
oid = normalizeOid(oid);
109+
102110
var repository = this;
103111

104-
return Blob.lookup(repository, normalizeOid(oid)).then(function(blob) {
112+
return Blob.lookup(repository, oid).then(function(blob) {
105113
blob.repo = repository;
106114

107115
if (callback) {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"rimraf": "~2.2.8",
5050
"tar": "~0.1.19",
5151
"which": "~1.0.5",
52+
"promise": "~4.0.0",
5253
"promisify-node": "~0.1.2"
5354
},
5455
"devDependencies": {
@@ -67,7 +68,7 @@
6768
},
6869
"scripts": {
6970
"lint": "jshint lib test/tests/*.js",
70-
"mocha": "istanbul cover _mocha -- test/tests --report=lcov",
71+
"mocha": "istanbul cover _mocha -- test/runner test/tests --report=lcov",
7172
"test": "npm run lint && npm run mocha",
7273
"publish": "node-pre-gyp package && node-pre-gyp publish",
7374
"generate": "node generate/setup && node generate",

test/commit.js

Lines changed: 0 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -1,139 +1,3 @@
1-
var git = require('../'),
2-
3-
exports.author = function(test) {
4-
test.expect(2);
5-
git.Repo.open('repos/workdir/.git', function(error, repository) {
6-
repository.getCommit(historyCountKnownSHA, function(error, commit) {
7-
var author = commit.author();
8-
test.equals(error, null, 'There should be no error');
9-
test.notEqual(author, null, 'Author should not be null');
10-
test.done();
11-
});
12-
});
13-
};
14-
15-
exports.authorName = function(test) {
16-
test.expect(1);
17-
git.Repo.open('repos/workdir/.git', function(error, repository) {
18-
repository.getCommit(historyCountKnownSHA, function(error, commit) {
19-
var author = commit.author();
20-
var name = author.name();
21-
test.equals(name, 'Michael Robinson', 'The author name should match expected value');
22-
test.done();
23-
});
24-
});
25-
};
26-
27-
exports.authorEmail = function(test) {
28-
test.expect(1);
29-
git.Repo.open('repos/workdir/.git', function(error, repository) {
30-
repository.getCommit(historyCountKnownSHA, function(error, commit) {
31-
var author = commit.author();
32-
var email = author.email();
33-
test.equals(email, 'mike@panmedia.co.nz', 'The author email should match expected value');
34-
test.done();
35-
});
36-
});
37-
};
38-
39-
exports.committerName = function(test) {
40-
test.expect(1);
41-
git.Repo.open('repos/workdir/.git', function(error, repository) {
42-
repository.getCommit(historyCountKnownSHA, function(error, commit) {
43-
var committer = commit.committer();
44-
var name = committer.name();
45-
test.equals(name, 'Michael Robinson', 'The author name should match expected value');
46-
test.done();
47-
});
48-
});
49-
};
50-
51-
exports.committerEmail = function(test) {
52-
test.expect(1);
53-
git.Repo.open('repos/workdir/.git', function(error, repository) {
54-
repository.getCommit(historyCountKnownSHA, function(error, commit) {
55-
var committer = commit.committer();
56-
var email = committer.email();
57-
test.equals(email, 'mike@panmedia.co.nz', 'The committer email should match expected value');
58-
test.done();
59-
});
60-
});
61-
};
62-
63-
/**
64-
* Test that improper commit ID's result in an error message
65-
*/
66-
exports.improperCommitId = function(test) {
67-
test.expect(1);
68-
git.Repo.open('repos/workdir/.git', function(error, repository) {
69-
repository.getCommit('not a proper commit sha', function(error, commit) {
70-
test.notEqual(error, null, 'Error should occur');
71-
test.done();
72-
});
73-
});
74-
};
75-
76-
/**
77-
* Test that retreiving walking a given commit's history works as expected.
78-
*/
79-
exports.history = function(test) {
80-
test.expect(4);
81-
git.Repo.open('repos/workdir/.git', function(error, repository) {
82-
repository.getCommit(historyCountKnownSHA, function(error, commit) {
83-
test.equals(null, error, 'Getting latest branch commit should not error');
84-
var historyCount = 0;
85-
var expectedHistoryCount = 364;
86-
commit.history().on('commit', function(commit) {
87-
historyCount++;
88-
}).on('end', function(commits) {
89-
test.equals(null, error, 'There should be no errors');
90-
test.equals(historyCount, expectedHistoryCount);
91-
test.equals(commits.length, expectedHistoryCount);
92-
test.done();
93-
}).on('error', function(error) {
94-
test.equals(null, error, 'There should be no errors');
95-
test.ok(false, 'There should be no errors');
96-
}).start();
97-
});
98-
});
99-
};
100-
101-
/**
102-
* Test that retreiving master branch's HEAD commit works as expected.
103-
*/
104-
exports.masterHead = function(test) {
105-
test.expect(1);
106-
git.Repo.open('repos/workdir/.git', function(error, repository) {
107-
repository.getBranch('master', function(error, branch) {
108-
var sha = branch.sha();
109-
repository.getCommit(sha, function(error, commit) {
110-
test.equals(error, null, 'Getting latest branch commit should not error');
111-
test.done();
112-
});
113-
});
114-
});
115-
};
116-
117-
/**
118-
* Test that retreiving parent works as expected.
119-
*
120-
* @param {Object} test
121-
*/
122-
exports.parents = function(test) {
123-
test.expect(3);
124-
git.Repo.open('repos/workdir/.git', function(error, repository) {
125-
repository.getCommit(historyCountKnownSHA, function(error, commit) {
126-
commit.getParents(function(error, parents) {
127-
test.equals(parents.length, 1, 'Commit should have exactly one parent');
128-
var sha = parents[0].sha();
129-
test.equals(error, null, 'Getting parent SHA should not error');
130-
test.equals(sha, 'ecfd36c80a3e9081f200dfda2391acadb56dac27', 'Parent SHA should match expected value');
131-
test.done();
132-
});
133-
});
134-
});
135-
};
136-
1371
/**
1382
* Test that retrieving and walking a commit's tree works as expected.
1393
*/

test/nodegit.js

Lines changed: 15 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,20 @@
1-
var fs = require('fs');
2-
var rimraf = require('rimraf');
3-
var exec = require('child_process').exec;
4-
var path = require('path');
5-
var async = require('async');
1+
var promisify = require("promisify-node");
2+
var fs = promisify("fs");
3+
var exec = promisify(("child_process").exec);
4+
var path = require("path");
65

7-
var testFiles = [
8-
'blob',
9-
'commit',
10-
'diff',
11-
'oid',
12-
'reference',
13-
'repository',
14-
'revwalk',
15-
'tree_entry',
16-
'tree',
17-
];
6+
before(function() {
7+
var url = "https://github.com/nodegit/nodegit";
188

19-
function setupReposCache(cb) {
20-
fs.mkdir('repos',function() {
21-
async.series([
22-
function empty(cb) { exec('git init repos/empty',function() { cb(); }); },
23-
function workdir(cb) { exec('git clone https://github.com/nodegit/nodegit.git repos/workdir',function() { cb(); }); },
24-
function nonrepo(cb) {
25-
fs.mkdir('repos/nonrepo',function() {
26-
fs.writeFile('repos/nonrepo/file.txt','This is a bogus file',function() {
27-
cb();
28-
});
29-
});
30-
}
31-
],function() {
32-
cb();
33-
});
34-
});
35-
}
36-
37-
exports.setUp = function(cb) {
38-
fs.exists('.reposCache', function(exists) {
39-
if (!exists) {
40-
setupReposCache(function(err) {
41-
cb();
9+
return fs.exists(".reposCache").then(function() {
10+
return fs.mkdir("repos").then(function() {
11+
return exec("git init repos/empty");
12+
}).then(function() {
13+
return exec("git clone " + url + " repos/workdir");
14+
}).then(function() {
15+
return fs.mkdir("repos/nonrepo").then(function() {
16+
return fs.writeFile("repos/nonrepo/file.txt", "This is a bogus file");
4217
});
43-
}
18+
})
4419
});
45-
};
46-
47-
Object.keys(testFiles).forEach(function(fileName) {
48-
var testFile = testFiles[fileName];
49-
exports[testFile] = require('./' + testFile);
5020
});

0 commit comments

Comments
 (0)