Skip to content

Commit 75b7424

Browse files
committed
Ensure callbacks still work correctly
1 parent 6b727d7 commit 75b7424

File tree

7 files changed

+89
-97
lines changed

7 files changed

+89
-97
lines changed

lib/commit.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ Commit.prototype.date = function() {
3030

3131
/**
3232
* Get the tree associated with this commit.
33+
*
34+
* @param {Function} callback
3335
* @return {Tree}
3436
*/
35-
Commit.prototype.getTree = function() {
36-
return this.repo.getTree(this.treeId());
37+
Commit.prototype.getTree = function(callback) {
38+
return this.repo.getTree(this.treeId(), callback);
3739
};
3840

3941
/**
@@ -44,12 +46,16 @@ Commit.prototype.getTree = function() {
4446
* @param {Function} callback
4547
* @return {TreeEntry}
4648
*/
47-
Commit.prototype.getEntry = function(path) {
49+
Commit.prototype.getEntry = function(path, callback) {
4850
return this.getTree().then(function(tree) {
4951
return tree.getEntry(path).then(function(entry) {
52+
if (typeof callback === "function") {
53+
callback(null, entry);
54+
}
55+
5056
return entry;
5157
});
52-
});
58+
}, callback);
5359
};
5460

5561
/**
@@ -135,7 +141,7 @@ Commit.prototype.getParents = function(limit, callback) {
135141

136142
// Wait for all parents to complete, before returning.
137143
return Promise.all(parents).then(function(parents) {
138-
if (callback) {
144+
if (typeof callback === "function") {
139145
callback(null, parents);
140146
}
141147

@@ -151,9 +157,11 @@ Commit.prototype.getParents = function(limit, callback) {
151157
*/
152158
Commit.prototype.parents = function() {
153159
var result = [];
160+
154161
for (var i = 0; i < this.parentcount(); i++) {
155162
result.push(this.parentId(i));
156163
}
164+
157165
return result;
158166
};
159167

@@ -178,7 +186,7 @@ Commit.prototype.getDiff = function(callback) {
178186

179187
return Promise.all(diffs);
180188
}).then(function(diffs) {
181-
if (callback) {
189+
if (typeof callback === "function") {
182190
callback(null, diffs);
183191
}
184192

lib/diff.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,6 @@ var ConvenientPatch = require("./convenient_patch");
44

55
var Diff = NodeGit.Diff;
66

7-
Object.defineProperties(Diff.prototype, {
8-
size: {
9-
value: Diff.prototype.numDeltas,
10-
enumerable: false
11-
}
12-
});
13-
147
/**
158
* Refer to vendor/libgit2/include/git2/diff.h for delta type definitions.
169
*
@@ -52,7 +45,7 @@ Diff.LineOrigin = {
5245
* @return {[ConvenientPatch]} an array of ConvenientPatches
5346
*/
5447
Diff.prototype.patches = function() {
55-
var size = this.size();
48+
var size = this.numDeltas();
5649
var result = [];
5750

5851
for (var i = 0; i < size; i++) {

lib/refs.js

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ var NodeGit = require("../");
22

33
var Reference = NodeGit.Refs;
44

5-
var oldSymbolicTarget = Reference.prototype.symbolicTarget;
6-
var oldTarget = Reference.prototype.target;
7-
85
Reference.Type = {
96
Oid: 1,
107
Symbolic: 2,
@@ -27,32 +24,6 @@ Reference.prototype.isSymbolic = function() {
2724
return this.type() == Reference.Type.Symbolic;
2825
};
2926

30-
/**
31-
* Returns the target of this symbolic reference.
32-
* @return {Reference}
33-
* @throws if the target is not symbolic.
34-
*/
35-
Reference.prototype.symbolicTarget = function() {
36-
if (!this.isSymbolic()) {
37-
throw new Error(this.name() + " is not symbolic");
38-
}
39-
40-
return oldSymbolicTarget.call(this);
41-
};
42-
43-
/**
44-
* Returns the oid of this non-symbolic reference.
45-
* @return {Oid}
46-
* @throws if the target is symbolic.
47-
*/
48-
Reference.prototype.target = function() {
49-
if (!this.isConcrete()) {
50-
throw new Error(this.name() + " is symbolic");
51-
}
52-
53-
return oldTarget.call(this);
54-
};
55-
5627
/**
5728
* Returns the name of the reference.
5829
* @return {String}

lib/repository.js

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ Repository.prototype.getBranch = function(name, callback) {
2121

2222
return this.getReference("refs/heads/" + name).then(function(reference) {
2323
return repository.getCommit(reference.target()).then(function(commit) {
24-
if (callback) {
24+
if (typeof callback === "function") {
2525
callback(null, commit);
2626
}
2727

2828
return commit;
2929
});
30-
});
30+
}, callback);
3131
};
3232

3333
/**
@@ -43,15 +43,12 @@ Repository.prototype.getReference = function(name, callback) {
4343
return Reference.lookup(this, name).then(function(reference) {
4444
if (reference.type() == Reference.Type.Symbolic) {
4545
return reference.resolve(function (error, reference) {
46-
if (error) {
47-
return callback(error);
48-
}
49-
5046
reference.repo = repository;
5147

5248
if (callback) {
5349
callback(null, reference);
5450
}
51+
5552
return reference;
5653
});
5754
} else {
@@ -61,7 +58,7 @@ Repository.prototype.getReference = function(name, callback) {
6158
}
6259
return reference;
6360
}
64-
});
61+
}, callback);
6562
};
6663

6764
/**
@@ -79,12 +76,12 @@ Repository.prototype.getCommit = function(oid, callback) {
7976
return Commit.lookup(repository, oid).then(function(commit) {
8077
commit.repo = repository;
8178

82-
if (callback) {
79+
if (typeof callback === "function") {
8380
callback(null, commit);
8481
}
8582

8683
return commit;
87-
});
84+
}, callback);
8885
};
8986

9087
/**
@@ -102,7 +99,7 @@ Repository.prototype.getBlob = function(oid, callback) {
10299
return Blob.lookup(repository, oid).then(function(blob) {
103100
blob.repo = repository;
104101

105-
if (callback) {
102+
if (typeof callback === "function") {
106103
callback(null, blob);
107104
}
108105

@@ -125,7 +122,7 @@ Repository.prototype.getTree = function(oid, callback) {
125122
return Tree.lookup(repository, oid).then(function(tree) {
126123
tree.repo = repository;
127124

128-
if (callback) {
125+
if (typeof callback === "function") {
129126
callback(null, tree);
130127
}
131128

@@ -148,7 +145,7 @@ Repository.prototype.getTag = function(oid, callback) {
148145
return Reference.lookup(repository, oid).then(function(reference) {
149146
reference.repo = repository;
150147

151-
if (callback) {
148+
if (typeof callback === "function") {
152149
callback(null, reference);
153150
}
154151

@@ -177,48 +174,62 @@ Repository.prototype.createRevWalk = function() {
177174
* @return {Branch}
178175
*/
179176
Repository.prototype.getMaster = function(callback) {
180-
this.getBranch("master", callback);
177+
return this.getBranch("master", callback);
181178
};
182179

183180
/**
184181
* Create a commit
185182
*
186183
* @param {String} updateRef
187184
* @param {Signature} author
188-
* @param {Signature} commiter
185+
* @param {Signature} committer
189186
* @param {String} message
190187
* @param {Tree|Oid|String} Tree
191188
* @param {Array} parents
192189
* @param {Function} callback
193190
* @return {Oid} The oid of the commit
194191
*/
195192
Repository.prototype.createCommit = function(
196-
// FIXME
197193
updateRef, author, committer, message, tree, parents, callback) {
194+
195+
var createCommit = null;
196+
var commit = this;
197+
198198
if (tree instanceof Tree) {
199-
return Commit.createCommit.call(
199+
commit = Commit.createCommit.call(
200200
this,
201201
updateRef,
202202
author,
203203
committer,
204204
null /* use default message encoding */,
205205
message,
206206
tree,
207-
parents.length, parents);
207+
parents.length,
208+
parents
209+
);
208210
} else {
209-
var self = this;
210-
return this.getTree(tree).then(function(tree) {
211+
createCommit = this.getTree(tree).then(function(tree) {
211212
return Commit.createCommit.call(
212-
self,
213+
commit,
213214
updateRef,
214215
author,
215216
committer,
216217
null /* use default message encoding */,
217218
message,
218219
tree,
219-
parents.length, parents);
220+
parents.length,
221+
parents
222+
);
220223
});
221224
}
225+
226+
return createCommit.then(function(commit) {
227+
if (typeof callback === "function") {
228+
callback(null, commit);
229+
}
230+
231+
return commit;
232+
}, callback);
222233
};
223234

224235
/**
@@ -228,20 +239,21 @@ Repository.prototype.createCommit = function(
228239
* @param {Function} callback
229240
* @return {Blob}
230241
*/
231-
Repository.prototype.createBlobFromBuffer = function(buffer) {
232-
return Blob.createFrombuffer.call(this, buffer, buffer.length);
242+
Repository.prototype.createBlobFromBuffer = function(buffer, callback) {
243+
return Blob.createFrombuffer.call(this, buffer, buffer.length, callback);
233244
};
234245

235246
/**
236247
* Create a new tree builder.
237248
*
238249
* @param {Tree} tree
239-
* @param {Function} callback
240250
*/
241-
Repository.prototype.treeBuilder = function(callback) {
251+
Repository.prototype.treeBuilder = function() {
242252
var builder = TreeBuilder.create(null);
253+
243254
builder.root = builder;
244255
builder.repo = this;
256+
245257
return builder;
246258
};
247259

lib/revwalk.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,29 +48,28 @@ Revwalk.prototype.sorting = function() {
4848
Revwalk.prototype.walk = function(oid, callback) {
4949
oid = normalizeOid(oid);
5050

51-
var self = this;
51+
var revwalk = this;
5252

5353
this.push(oid);
5454

5555
function walk() {
56-
self.next(function revWalkNext(error, oid) {
57-
if (error) {
58-
return callback(error);
59-
}
60-
56+
revwalk.next().then(function(oid) {
6157
if (!oid) {
62-
return callback();
58+
if (typeof callback === "function") {
59+
return callback();
60+
}
61+
62+
return;
6363
}
6464

65-
self.repo.getCommit(oid, function revWalkCommitLookup(error, commit) {
66-
if (error) {
67-
return callback(error);
65+
revwalk.repo.getCommit(oid).then(function(commit) {
66+
if (typeof callback === "function") {
67+
callback(null, commit);
6868
}
6969

70-
callback(null, commit);
7170
walk();
7271
});
73-
});
72+
}, callback);
7473
}
7574

7675
walk();

0 commit comments

Comments
 (0)