Skip to content

Commit 39357aa

Browse files
John Haleymaxkorp
authored andcommitted
Lookup functions now can normalize their objects
1 parent 6decdd1 commit 39357aa

File tree

8 files changed

+130
-35
lines changed

8 files changed

+130
-35
lines changed

lib/blob.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
var NodeGit = require("../");
22
var TreeEntry = require("./tree_entry");
3+
var LookupWrapper = require("./util/lookupWrapper");
34

45
var Blob = NodeGit.Blob;
56

7+
/**
8+
* Retrieves the blob pointed to by the oid
9+
* @param {Repository} repo The repo that the blob lives in
10+
* @param {String|Oid|Blob} id The blob to lookup
11+
* @param {Function} callback
12+
* @return {Blob}
13+
*/
14+
Blob.lookup = LookupWrapper(Blob);
15+
616
/**
717
* Retrieve the content of the Blob.
818
*

lib/commit.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
var events = require("events");
22
var Promise = require("nodegit-promise");
33
var NodeGit = require("../");
4+
var LookupWrapper = require("./util/lookupWrapper");
45

56
var Commit = NodeGit.Commit;
67

8+
/**
9+
* Retrieves the commit pointed to by the oid
10+
* @param {Repository} repo The repo that the commit lives in
11+
* @param {String|Oid|Commit} id The commit to lookup
12+
* @param {Function} callback
13+
* @return {Commit}
14+
*/
15+
Commit.lookup = LookupWrapper(Commit);
16+
717
/**
818
* Retrieve the SHA.
919
* @return {String}

lib/merge.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var NodeGit = require("../");
22
var normalizeOptions = require("./util/normalize_options");
3+
var Promise = require("nodegit-promise");
34

45
var Merge = NodeGit.Merge;
56
var mergeCommits = Merge.commits;
@@ -16,7 +17,12 @@ var mergeCommits = Merge.commits;
1617
Merge.commits = function(repo, ourCommit, theirCommit, options) {
1718
options = normalizeOptions(options, NodeGit.MergeOptions);
1819

19-
return mergeCommits.call(this, repo, ourCommit, theirCommit, options);
20+
return Promise.all([
21+
repo.getCommit(ourCommit),
22+
repo.getCommit(theirCommit)
23+
]).then(function(commits) {
24+
return mergeCommits.call(this, repo, commits[0], commits[1], options);
25+
});
2026
};
2127

2228
module.exports = Merge;

lib/reference.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
var NodeGit = require("../");
2+
var LookupWrapper = require("./util/lookupWrapper");
23

34
var Reference = NodeGit.Reference;
45
var Branch = NodeGit.Branch;
56

7+
/**
8+
* Retrieves the reference pointed to by the oid
9+
* @param {Repository} repo The repo that the reference lives in
10+
* @param {String|Reference} id The reference to lookup
11+
* @param {Function} callback
12+
* @return {Reference}
13+
*/
14+
Reference.lookup = LookupWrapper(Reference);
15+
616
/**
717
* Returns true if this reference is valid
818
* @return {Boolean}

lib/repository.js

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,14 @@ function(name, commit, force, signature, logMessage) {
5555
/**
5656
* Look up a branch
5757
*
58-
* @param {String} name Branch name, e.g. "master"
58+
* @param {String|Ref} name Branch name, e.g. "master" or Branch Ref
5959
* @param {Function} callback
6060
* @return {Ref}
6161
*/
6262
Repository.prototype.getBranch = function(name, callback) {
63-
name = ~name.indexOf("refs/heads/") ? name : "refs/heads/" + name;
63+
name = name instanceof Reference || ~name.indexOf("refs/heads/")
64+
? name
65+
: "refs/heads/" + name;
6466

6567
return this.getReference(name).then(function(reference) {
6668
if (typeof callback === "function") {
@@ -74,7 +76,7 @@ Repository.prototype.getBranch = function(name, callback) {
7476
/**
7577
* Look up a branch's most recent commit.
7678
*
77-
* @param {String} name Branch name, e.g. "master"
79+
* @param {String|Ref} name Branch name, e.g. "master" or Branch Ref
7880
* @param {Function} callback
7981
* @return {Commit}
8082
*/
@@ -331,40 +333,40 @@ Repository.prototype.getHeadCommit = function(callback) {
331333
Repository.prototype.createCommit = function(
332334
updateRef, author, committer, message, tree, parents, callback) {
333335

334-
var createCommit = null;
335336
var repo = this;
337+
var promises = [];
336338

337-
if (tree instanceof Tree) {
338-
createCommit = Promise.all([
339-
Commit.create(
340-
repo,
341-
updateRef,
342-
author,
343-
committer,
344-
null /* use default message encoding */,
345-
message,
346-
tree,
347-
parents.length,
348-
parents
349-
)
350-
]);
351-
} else {
352-
createCommit = this.getTree(tree).then(function(tree) {
353-
return Commit.create(
354-
repo,
355-
updateRef,
356-
author,
357-
committer,
358-
null /* use default message encoding */,
359-
message,
360-
tree,
361-
parents.length,
362-
parents
363-
);
364-
});
365-
}
339+
parents = parents || [];
340+
341+
promises.push(repo.getTree(tree));
342+
343+
parents.forEach(function(parent) {
344+
promises.push(repo.getCommit(parent));
345+
});
366346

367-
return createCommit.then(function(commit) {
347+
return Promise.all(promises).then(function(results) {
348+
tree = results[0];
349+
350+
// Get the normalized values for our input into the function
351+
var parentsLength = parents.length;
352+
parents = [];
353+
354+
for (var i = 0; i < parentsLength; i++) {
355+
parents.push(results[i + 1]);
356+
}
357+
358+
return Commit.create(
359+
repo,
360+
updateRef,
361+
author,
362+
committer,
363+
null /* use default message encoding */,
364+
message,
365+
tree,
366+
parents.length,
367+
parents
368+
);
369+
}).then(function(commit) {
368370
if (typeof callback === "function") {
369371
callback(null, commit);
370372
}

lib/tag.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
var git = require("../");
2+
var LookupWrapper = require("./util/lookupWrapper");
23

34
var Tag = git.Tag;
45

6+
/**
7+
* Retrieves the tag pointed to by the oid
8+
* @param {Repository} repo The repo that the tag lives in
9+
* @param {String|Oid|Tag} id The tag to lookup
10+
* @param {Function} callback
11+
* @return {Tag}
12+
*/
13+
Tag.lookup = LookupWrapper(Tag);
14+
515
module.exports = Tag;

lib/tree.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ var Tree = git.Tree;
33
var Treebuilder = git.Treebuilder;
44
var Diff = git.Diff;
55
var events = require("events");
6+
var LookupWrapper = require("./util/lookupWrapper");
7+
8+
/**
9+
* Retrieves the tree pointed to by the oid
10+
* @param {Repository} repo The repo that the tree lives in
11+
* @param {String|Oid|Tree} id The tree to lookup
12+
* @param {Function} callback
13+
* @return {Tree}
14+
*/
15+
Tree.lookup = LookupWrapper(Tree);
616

717
/**
818
* Diff two trees

lib/util/lookupWrapper.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
var Promise = require("nodegit-promise");
2+
3+
/**
4+
* Wraps a method so that you can pass in either a string, OID or the object
5+
* itself and you will always get back a promise that resolves to the object.
6+
* @param {Object} objectType The object type that you're expecting to receive.
7+
* @param {Function} lookupFunction The function to do the lookup for the
8+
* object. Defaults to `objectType.lookup`.
9+
* @return {Function}
10+
*/
11+
module.exports = function(objectType, lookupFunction) {
12+
lookupFunction = lookupFunction || objectType.lookup;
13+
14+
return function(repo, id, callback) {
15+
if (id instanceof objectType) {
16+
return Promise.resolve(id).then(function(obj) {
17+
obj.repo = repo;
18+
19+
if (typeof callback === "function") {
20+
callback(null, obj);
21+
}
22+
23+
return obj;
24+
}, callback);
25+
}
26+
27+
return lookupFunction(repo, id).then(function(obj) {
28+
obj.repo = repo;
29+
30+
if (typeof callback === "function") {
31+
callback(null, obj);
32+
}
33+
34+
return obj;
35+
}, callback);
36+
};
37+
};

0 commit comments

Comments
 (0)