Skip to content

Commit e482010

Browse files
committed
Improved oid normalization
1 parent 6d77a5c commit e482010

File tree

4 files changed

+72
-15
lines changed

4 files changed

+72
-15
lines changed

lib/repository.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
var git = require('../');
22
var util = require('./util');
33
var Refs = require('./reference');
4+
var normalizeOid = require("./util/normalize_oid");
5+
46
var Commit = git.Commit;
57
var Revwalk = git.Revwalk;
68
var Repo = git.Repository;
79
var Blob = git.Blob;
810
var Tree = git.Tree;
9-
//var TreeBuilder = git.TreeBuilder;
1011
var Reference = git.Reference;
1112

1213
// Backwards compatibility.
@@ -79,7 +80,7 @@ util.makeSafe(Repo.prototype, 'getReference');
7980
Repo.prototype.getCommit = function(oid, callback) {
8081
var repository = this;
8182

82-
return Commit.lookup(repository, oid).then(function(commit) {
83+
return Commit.lookup(repository, normalizeOid(oid)).then(function(commit) {
8384
commit.repo = repository;
8485

8586
if (callback) {
@@ -89,8 +90,6 @@ Repo.prototype.getCommit = function(oid, callback) {
8990
return commit;
9091
});
9192
};
92-
util.normalizeOid(Repo.prototype, 'getCommit');
93-
util.makeSafe(Repo.prototype, 'getCommit');
9493

9594
/**
9695
* Retrieve the blob represented by the oid.
@@ -100,9 +99,9 @@ util.makeSafe(Repo.prototype, 'getCommit');
10099
* @return {Blob}
101100
*/
102101
Repo.prototype.getBlob = function(oid, callback) {
103-
var repository= this;
102+
var repository = this;
104103

105-
return Blob.lookup(repository, oid).then(function(blob) {
104+
return Blob.lookup(repository, normalizeOid(oid)).then(function(blob) {
106105
blob.repo = repository;
107106

108107
if (callback) {
@@ -112,8 +111,6 @@ Repo.prototype.getBlob = function(oid, callback) {
112111
return blob;
113112
}, callback);
114113
};
115-
util.normalizeOid(Repo.prototype, 'getBlob');
116-
util.makeSafe(Repo.prototype, 'getBlob');
117114

118115
/**
119116
* Retrieve the tree represented by the oid.

lib/util.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exports.makeSafe = function(object, key) {
44
var oldFn = object[key];
55
object[key] = function() {
66
try {
7-
return oldFn.apply(this, arguments);
7+
oldFn.apply(this, arguments);
88
} catch (e) {
99
var callback = arguments[arguments.length - 1];
1010
callback(e);
@@ -16,6 +16,6 @@ exports.normalizeOid = function(object, key) {
1616
var oldFn = object[key];
1717
object[key] = function(oid) {
1818
if (typeof oid === 'string') oid = git.Oid.fromString(oid);
19-
return oldFn.apply(this, arguments);
19+
oldFn.apply(this, arguments);
2020
};
2121
};

lib/util/normalize_oid.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var git = require("../../");
2+
3+
/**
4+
* Normalize an identifier to always be an OID instance.
5+
*
6+
* @param {String, Object} oid - The oid string or instance.
7+
* @return {Object} An Oid instance.
8+
*/
9+
function normalizeOid(oid) {
10+
try {
11+
return typeof oid === "string" ? git.Oid.fromString(oid) : oid;
12+
}
13+
catch (ex) {
14+
return null;
15+
}
16+
}
17+
18+
module.exports = normalizeOid;

test/tests/commit.js

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,65 @@ describe("Commit", function() {
2424
});
2525
});
2626

27-
it("makes its message available", function() {
27+
it("will fail with an invalid sha", function() {
28+
return this.repository.getCommit("invalid").then(null, function(error) {
29+
assert.ok(error instanceof Error);
30+
});
31+
});
32+
33+
it("has a message", function() {
2834
assert.equal(this.commit.message(), "Update README.md");
2935
});
3036

31-
it("makes its sha available", function() {
37+
it("has a sha", function() {
3238
assert.equal(this.commit.sha(), historyCountKnownSHA);
3339
});
3440

35-
it("makes its time available", function() {
41+
it("has a time", function() {
3642
assert.equal(this.commit.timeMs(), 1362012884000);
3743
});
3844

39-
it("makes its date available", function() {
45+
it("has a date", function() {
4046
assert.equal(this.commit.date().getTime(), 1362012884000);
4147
});
4248

43-
it("makes its offset available", function() {
49+
it("has an offset", function() {
4450
assert.equal(this.commit.offset(), 780);
4551
});
52+
53+
describe("author", function() {
54+
before(function() {
55+
this.author = this.commit.author();
56+
});
57+
58+
it("is available", function() {
59+
assert.ok(this.author instanceof nodegit.Signature);
60+
});
61+
62+
it("has a name", function() {
63+
assert.equal(this.author.name(), "Michael Robinson");
64+
});
65+
66+
it("has an email", function() {
67+
assert.equal(this.author.email(), "mike@panmedia.co.nz");
68+
});
69+
});
70+
71+
describe("committer", function() {
72+
before(function() {
73+
this.author = this.commit.committer();
74+
});
75+
76+
it("is available", function() {
77+
assert.ok(this.author instanceof nodegit.Signature);
78+
});
79+
80+
it("has a name", function() {
81+
assert.equal(this.author.name(), "Michael Robinson");
82+
});
83+
84+
it("has an email", function() {
85+
assert.equal(this.author.email(), "mike@panmedia.co.nz");
86+
});
87+
});
4688
});

0 commit comments

Comments
 (0)