Permalink
Browse files

Ensure callbacks still work correctly

  • Loading branch information...
1 parent 6b727d7 commit 75b7424ead469847082da8c6b0479c3ef4546a3c @tbranyen tbranyen committed Jul 11, 2014
Showing with 89 additions and 97 deletions.
  1. +14 −6 lib/commit.js
  2. +1 −8 lib/diff.js
  3. +0 −29 lib/refs.js
  4. +37 −25 lib/repository.js
  5. +11 −12 lib/revwalk.js
  6. +25 −16 lib/tree.js
  7. +1 −1 lib/tree_entry.js
View
@@ -30,10 +30,12 @@ Commit.prototype.date = function() {
/**
* Get the tree associated with this commit.
+ *
+ * @param {Function} callback
* @return {Tree}
*/
-Commit.prototype.getTree = function() {
- return this.repo.getTree(this.treeId());
+Commit.prototype.getTree = function(callback) {
+ return this.repo.getTree(this.treeId(), callback);
};
/**
@@ -44,12 +46,16 @@ Commit.prototype.getTree = function() {
* @param {Function} callback
* @return {TreeEntry}
*/
-Commit.prototype.getEntry = function(path) {
+Commit.prototype.getEntry = function(path, callback) {
return this.getTree().then(function(tree) {
return tree.getEntry(path).then(function(entry) {
+ if (typeof callback === "function") {
+ callback(null, entry);
+ }
+
return entry;
});
- });
+ }, callback);
};
/**
@@ -135,7 +141,7 @@ Commit.prototype.getParents = function(limit, callback) {
// Wait for all parents to complete, before returning.
return Promise.all(parents).then(function(parents) {
- if (callback) {
+ if (typeof callback === "function") {
callback(null, parents);
}
@@ -151,9 +157,11 @@ Commit.prototype.getParents = function(limit, callback) {
*/
Commit.prototype.parents = function() {
var result = [];
+
for (var i = 0; i < this.parentcount(); i++) {
result.push(this.parentId(i));
}
+
return result;
};
@@ -178,7 +186,7 @@ Commit.prototype.getDiff = function(callback) {
return Promise.all(diffs);
}).then(function(diffs) {
- if (callback) {
+ if (typeof callback === "function") {
callback(null, diffs);
}
View
@@ -4,13 +4,6 @@ var ConvenientPatch = require("./convenient_patch");
var Diff = NodeGit.Diff;
-Object.defineProperties(Diff.prototype, {
- size: {
- value: Diff.prototype.numDeltas,
- enumerable: false
- }
-});
-
/**
* Refer to vendor/libgit2/include/git2/diff.h for delta type definitions.
*
@@ -52,7 +45,7 @@ Diff.LineOrigin = {
* @return {[ConvenientPatch]} an array of ConvenientPatches
*/
Diff.prototype.patches = function() {
- var size = this.size();
+ var size = this.numDeltas();
var result = [];
for (var i = 0; i < size; i++) {
View
@@ -2,9 +2,6 @@ var NodeGit = require("../");
var Reference = NodeGit.Refs;
-var oldSymbolicTarget = Reference.prototype.symbolicTarget;
-var oldTarget = Reference.prototype.target;
-
Reference.Type = {
Oid: 1,
Symbolic: 2,
@@ -27,32 +24,6 @@ Reference.prototype.isSymbolic = function() {
return this.type() == Reference.Type.Symbolic;
};
-/**
- * Returns the target of this symbolic reference.
- * @return {Reference}
- * @throws if the target is not symbolic.
- */
-Reference.prototype.symbolicTarget = function() {
- if (!this.isSymbolic()) {
- throw new Error(this.name() + " is not symbolic");
- }
-
- return oldSymbolicTarget.call(this);
-};
-
-/**
- * Returns the oid of this non-symbolic reference.
- * @return {Oid}
- * @throws if the target is symbolic.
- */
-Reference.prototype.target = function() {
- if (!this.isConcrete()) {
- throw new Error(this.name() + " is symbolic");
- }
-
- return oldTarget.call(this);
-};
-
/**
* Returns the name of the reference.
* @return {String}
View
@@ -21,13 +21,13 @@ Repository.prototype.getBranch = function(name, callback) {
return this.getReference("refs/heads/" + name).then(function(reference) {
return repository.getCommit(reference.target()).then(function(commit) {
- if (callback) {
+ if (typeof callback === "function") {
callback(null, commit);
}
return commit;
});
- });
+ }, callback);
};
/**
@@ -43,15 +43,12 @@ Repository.prototype.getReference = function(name, callback) {
return Reference.lookup(this, name).then(function(reference) {
if (reference.type() == Reference.Type.Symbolic) {
return reference.resolve(function (error, reference) {
- if (error) {
- return callback(error);
- }
-
reference.repo = repository;
if (callback) {
callback(null, reference);
}
+
return reference;
});
} else {
@@ -61,7 +58,7 @@ Repository.prototype.getReference = function(name, callback) {
}
return reference;
}
- });
+ }, callback);
};
/**
@@ -79,12 +76,12 @@ Repository.prototype.getCommit = function(oid, callback) {
return Commit.lookup(repository, oid).then(function(commit) {
commit.repo = repository;
- if (callback) {
+ if (typeof callback === "function") {
callback(null, commit);
}
return commit;
- });
+ }, callback);
};
/**
@@ -102,7 +99,7 @@ Repository.prototype.getBlob = function(oid, callback) {
return Blob.lookup(repository, oid).then(function(blob) {
blob.repo = repository;
- if (callback) {
+ if (typeof callback === "function") {
callback(null, blob);
}
@@ -125,7 +122,7 @@ Repository.prototype.getTree = function(oid, callback) {
return Tree.lookup(repository, oid).then(function(tree) {
tree.repo = repository;
- if (callback) {
+ if (typeof callback === "function") {
callback(null, tree);
}
@@ -148,7 +145,7 @@ Repository.prototype.getTag = function(oid, callback) {
return Reference.lookup(repository, oid).then(function(reference) {
reference.repo = repository;
- if (callback) {
+ if (typeof callback === "function") {
callback(null, reference);
}
@@ -177,48 +174,62 @@ Repository.prototype.createRevWalk = function() {
* @return {Branch}
*/
Repository.prototype.getMaster = function(callback) {
- this.getBranch("master", callback);
+ return this.getBranch("master", callback);
};
/**
* Create a commit
*
* @param {String} updateRef
* @param {Signature} author
- * @param {Signature} commiter
+ * @param {Signature} committer
* @param {String} message
* @param {Tree|Oid|String} Tree
* @param {Array} parents
* @param {Function} callback
* @return {Oid} The oid of the commit
*/
Repository.prototype.createCommit = function(
- // FIXME
updateRef, author, committer, message, tree, parents, callback) {
+
+ var createCommit = null;
+ var commit = this;
+
if (tree instanceof Tree) {
- return Commit.createCommit.call(
+ commit = Commit.createCommit.call(
this,
updateRef,
author,
committer,
null /* use default message encoding */,
message,
tree,
- parents.length, parents);
+ parents.length,
+ parents
+ );
} else {
- var self = this;
- return this.getTree(tree).then(function(tree) {
+ createCommit = this.getTree(tree).then(function(tree) {
return Commit.createCommit.call(
- self,
+ commit,
updateRef,
author,
committer,
null /* use default message encoding */,
message,
tree,
- parents.length, parents);
+ parents.length,
+ parents
+ );
});
}
+
+ return createCommit.then(function(commit) {
+ if (typeof callback === "function") {
+ callback(null, commit);
+ }
+
+ return commit;
+ }, callback);
};
/**
@@ -228,20 +239,21 @@ Repository.prototype.createCommit = function(
* @param {Function} callback
* @return {Blob}
*/
-Repository.prototype.createBlobFromBuffer = function(buffer) {
- return Blob.createFrombuffer.call(this, buffer, buffer.length);
+Repository.prototype.createBlobFromBuffer = function(buffer, callback) {
+ return Blob.createFrombuffer.call(this, buffer, buffer.length, callback);
};
/**
* Create a new tree builder.
*
* @param {Tree} tree
- * @param {Function} callback
*/
-Repository.prototype.treeBuilder = function(callback) {
+Repository.prototype.treeBuilder = function() {
var builder = TreeBuilder.create(null);
+
builder.root = builder;
builder.repo = this;
+
return builder;
};
View
@@ -48,29 +48,28 @@ Revwalk.prototype.sorting = function() {
Revwalk.prototype.walk = function(oid, callback) {
oid = normalizeOid(oid);
- var self = this;
+ var revwalk = this;
this.push(oid);
function walk() {
- self.next(function revWalkNext(error, oid) {
- if (error) {
- return callback(error);
- }
-
+ revwalk.next().then(function(oid) {
if (!oid) {
- return callback();
+ if (typeof callback === "function") {
+ return callback();
+ }
+
+ return;
}
- self.repo.getCommit(oid, function revWalkCommitLookup(error, commit) {
- if (error) {
- return callback(error);
+ revwalk.repo.getCommit(oid).then(function(commit) {
+ if (typeof callback === "function") {
+ callback(null, commit);
}
- callback(null, commit);
walk();
});
- });
+ }, callback);
}
walk();
Oops, something went wrong.

0 comments on commit 75b7424

Please sign in to comment.