Skip to content

Commit 9b2f3a3

Browse files
committed
Refactored Blob and Commit constructors
1 parent 61e134a commit 9b2f3a3

File tree

10 files changed

+182
-154
lines changed

10 files changed

+182
-154
lines changed

.jshintrc

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
11
{
2+
"evil": true,
3+
"boss": true,
4+
"immed": false,
5+
"eqnull": true,
6+
"maxlen": 80,
27
"node": true,
3-
"proto": true
8+
"curly": true,
9+
"quotmark": "double",
10+
"trailing": true,
11+
"unused": "vars",
12+
"undef": true,
13+
"validthis": true,
14+
"globals": {
15+
"global": true,
16+
"define": true
17+
}
418
}

generate/descriptor.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@
3434
}
3535
},
3636

37+
"git_blob_is_binary": {
38+
"ignore": false,
39+
"args": [
40+
{ "isReturn": false, "isSelf": true }
41+
]
42+
},
43+
3744
"git_blob_create_frombuffer": {
3845
"ignore": false,
3946
"isAsync": true,
@@ -671,7 +678,7 @@
671678
"ignore": false,
672679
"isAsync": true,
673680
"isConstructorMethod": true,
674-
"args": [{ "isReturn": true }, { "isSelf": true }],
681+
"args": [{ "isReturn": true }],
675682
"return": {
676683
"isErrorCode": true
677684
}

lib/blob.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
1-
var git = require('../');
2-
var TreeEntry = git.TreeEntry;
3-
var Blob = git.Blob;
1+
var NodeGit = require("../");
2+
var TreeEntry = require("./tree_entry");
3+
var Blob = NodeGit.Blob;
44

5-
// Assign deprecated methods.
6-
Object.defineProperty(Blob.prototype, "size", {
7-
value: Blob.prototype.rawsize,
8-
enumerable: false
9-
});
5+
var FileMode = TreeEntry.FileMode;
106

117
/**
12-
* Retrieve the content of the blob.
13-
* @return {Buffer} content
8+
* Retrieve the content of the Blob.
9+
*
10+
* @return {Buffer} Contents.
1411
*/
1512
Blob.prototype.content = function() {
1613
return this.rawcontent().toBuffer(this.rawsize());
1714
};
1815

1916
/**
20-
* Retrieve the blob's content as String.
17+
* Retrieve the Blob's content as String.
18+
*
19+
* @return {string} Contents.
2120
*/
2221
Blob.prototype.toString = function() {
2322
return this.content().toString();
2423
};
2524

2625
/**
27-
* Retrieve the blob's type.
26+
* Retrieve the Blob's type.
27+
*
28+
* @return {number} The filemode.
2829
*/
2930
Blob.prototype.filemode = function() {
30-
return this.isBinary() ? TreeEntry.FileMode.Executable : TreeEntry.FileMode.Blob;
31+
return this.isBinary() ? FileMode.Executable : FileMode.Blob;
3132
};

lib/commit.js

Lines changed: 39 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,14 @@
11
var Promise = require("promise");
2-
var git = require('../');
2+
var git = require("../");
33
var Commit = git.Commit;
4-
var Oid = git.Oid;
5-
var Revwalk = git.Revwalk;
6-
var events = require('events');
7-
var Tree = require("./tree");
8-
9-
// Backwards compatibility.
10-
Object.defineProperties(Commit.prototype, {
11-
"oid": {
12-
value: Commit.prototype.id,
13-
enumerable: false
14-
},
15-
16-
"offset": {
17-
value: Commit.prototype.timeOffset,
18-
enumerable: false
19-
},
20-
21-
"parentCount": {
22-
value: Commit.prototype.parentcount,
23-
enumerable: false
24-
}
25-
});
4+
var events = require("events");
265

276
/**
287
* Retrieve the SHA.
298
* @return {String}
309
*/
3110
Commit.prototype.sha = function() {
32-
return this.oid().toString();
11+
return this.id().toString();
3312
};
3413

3514
/**
@@ -53,7 +32,7 @@ Commit.prototype.date = function() {
5332
* @return {Tree}
5433
*/
5534
Commit.prototype.getTree = function(callback) {
56-
this.repo.getTree(this.treeId(), callback);
35+
return this.repo.getTree(this.treeId(), callback);
5736
};
5837

5938
/**
@@ -65,18 +44,22 @@ Commit.prototype.getTree = function(callback) {
6544
* @return {TreeEntry}
6645
*/
6746
Commit.prototype.getEntry = function(path, callback) {
68-
this.getTree(function(error, tree) {
69-
if (error) return callback(error);
47+
return this.getTree().then(function(tree) {
48+
return tree.getEntry(path).then(function(entry) {
49+
if (callback) {
50+
callback(entry);
51+
}
7052

71-
tree.getEntry(path, callback);
72-
});
53+
return entry;
54+
});
55+
}, callback);
7356
};
7457

7558
/**
7659
* Walk the history from this commit backwards.
77-
* An EventEmitter is returned that will emit a 'commit' event for each
78-
* commit in the history, and one 'end' event when the walk is completed.
79-
* Don't forget to call `start()` on the returned event.
60+
* An EventEmitter is returned that will emit a "commit" event for each
61+
* commit in the history, and one "end" event when the walk is completed.
62+
* Don"t forget to call `start()` on the returned event.
8063
*
8164
* @fires Commit#commit
8265
* @fires Commit#end
@@ -85,7 +68,7 @@ Commit.prototype.getEntry = function(path, callback) {
8568
*/
8669
Commit.prototype.history = function() {
8770
var event = new events.EventEmitter();
88-
var oid = this.oid();
71+
var oid = this.id();
8972
var revwalk = this.repo.createRevWalk();
9073

9174
revwalk.sorting.apply(revwalk, arguments);
@@ -94,14 +77,16 @@ Commit.prototype.history = function() {
9477

9578
event.start = function() {
9679
revwalk.walk(oid, function commitRevWalk(error, commit) {
97-
if (error) return event.emit('error', error);
80+
if (error) {
81+
return event.emit("error", error);
82+
}
9883

9984
if (!commit) {
100-
event.emit('end', commits);
85+
event.emit("end", commits);
10186
return;
10287
}
10388

104-
event.emit('commit', commit);
89+
event.emit("commit", commit);
10590
commits.push(commit);
10691
});
10792
};
@@ -110,14 +95,13 @@ Commit.prototype.history = function() {
11095
};
11196

11297
/**
113-
* Retrieve the commit's parents -- as commit objects.
98+
* Retrieve the commit"s parents -- as commit objects.
11499
*
115100
* @param {number} limit - Optional amount of parents to return.
116101
* @param {Function} callback
117102
* @return {[Commit]} array of commits
118103
*/
119104
Commit.prototype.getParents = function(limit, callback) {
120-
var commit = this;
121105
var parents = [];
122106
var i = 0;
123107

@@ -127,7 +111,7 @@ Commit.prototype.getParents = function(limit, callback) {
127111
}
128112

129113
// If no limit was set, default to the maximum parents.
130-
limit = typeof limit === "number" ? limit : this.parentCount();
114+
limit = typeof limit === "number" ? limit : this.parentcount();
131115

132116
function processParents(commit, callback) {
133117
var oid = commit.parentId(i);
@@ -163,14 +147,14 @@ Commit.prototype.getParents = function(limit, callback) {
163147
};
164148

165149
/**
166-
* Retrieve the commit's parent shas.
150+
* Retrieve the commit"s parent shas.
167151
*
168152
* @param {Function} callback
169153
* @return {[Oid]} array of oids
170154
*/
171155
Commit.prototype.parents = function() {
172156
var result = [];
173-
for (var i = 0; i < this.parentCount(); i++) {
157+
for (var i = 0; i < this.parentcount(); i++) {
174158
result.push(this.parentId(i));
175159
}
176160
return result;
@@ -184,30 +168,23 @@ Commit.prototype.parents = function() {
184168
* @return {[Diff]} an array of diffs
185169
*/
186170
Commit.prototype.getDiff = function(callback) {
187-
var self = this;
188-
self.getParents(function commitParents(error, parents) {
189-
if (error) return callback(error);
190-
191-
var parentDiffs = [];
192-
parents.forEach(function commitEachParent(parent) {
193-
parent.getTree(function(error, parentTree) {
194-
if (error) return callback(error);
195-
196-
self.getTree(function(error, thisTree) {
197-
if (error) return callback(error);
198-
199-
parentTree.diff(thisTree, function walkDiff(error, diff) {
200-
if (error) return callback(error);
201-
202-
parentDiffs.push(diff);
203-
if (parentDiffs.length === parents.length) {
204-
callback(null, parentDiffs);
205-
}
206-
});
171+
var commit = this;
172+
173+
return commit.getParents().then(function(parents) {
174+
return parents.map(function(parent) {
175+
return parent.getTree(function(parentTree) {
176+
return commit.getTree(function(thisTree) {
177+
return parentTree.diff(thisTree);
207178
});
208179
});
209180
});
210-
});
181+
}).then(function(parentDiffs) {
182+
if (callback) {
183+
callback(null, parentDiffs);
184+
}
185+
186+
return parentDiffs;
187+
}, callback);
211188
};
212189

213190
/**

lib/repository.js

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,6 @@ Object.defineProperty(git, "Repo", {
1616
enumerable: false
1717
});
1818

19-
var oldGetReference = Reference.lookup,
20-
oldGetTree = Tree.lookup,
21-
oldGetTag = Repo.prototype.getTag,
22-
oldCreateRevwalk = Revwalk.createRevwalk,
23-
oldCreateCommit = Commit.createCommit,
24-
oldCreateBlobFromBuffer = Blob.createFrombuffer;
25-
2619
/**
2720
* Look up a branch's most recent commit.
2821
*
@@ -128,15 +121,21 @@ Repo.prototype.getBlob = function(oid, callback) {
128121
* @return {Tree}
129122
*/
130123
Repo.prototype.getTree = function(oid, callback) {
131-
var self = this;
132-
oldGetTree.call(this, oid, function(error, tree) {
133-
if (error) return callback(error);
134-
tree.repo = self;
135-
callback(null, tree);
136-
});
124+
oid = normalizeOid(oid);
125+
126+
var repository = this;
127+
128+
129+
return Tree.lookup(repository, oid).then(function(tree) {
130+
tree.repo = repository;
131+
132+
if (callback) {
133+
callback(null, tree);
134+
}
135+
136+
return tree;
137+
}, callback);
137138
};
138-
util.normalizeOid(Repo.prototype, 'getTree');
139-
util.makeSafe(Repo.prototype, 'getTree');
140139

141140
/**
142141
* Retrieve the tag represented by the oid.
@@ -146,11 +145,20 @@ util.makeSafe(Repo.prototype, 'getTree');
146145
* @return {Tag}
147146
*/
148147
Repo.prototype.getTag = function(oid, callback) {
149-
var self = this;
150-
oldGetTag.call(this, oid, callback);
148+
oid = normalizeOid(oid);
149+
150+
var repository = this;
151+
152+
return Reference.lookup(repository, oid).then(function(reference) {
153+
reference.repo = repository;
154+
155+
if (callback) {
156+
callback(null, reference);
157+
}
158+
159+
return reference;
160+
}, callback);
151161
};
152-
util.normalizeOid(Repo.prototype, 'getTag');
153-
util.makeSafe(Repo.prototype, 'getTag');
154162

155163
/**
156164
* Instantiate a new revision walker for browsing the Repo's history.
@@ -161,7 +169,7 @@ util.makeSafe(Repo.prototype, 'getTag');
161169
* @return {RevWalk}
162170
*/
163171
Repo.prototype.createRevWalk = function() {
164-
var revWalk = oldCreateRevwalk(this);
172+
var revWalk = Revwalk.createRevwalk(this);
165173
revWalk.repo = this;
166174
return revWalk;
167175
};
@@ -190,7 +198,7 @@ Repo.prototype.getMaster = function(callback) {
190198
*/
191199
Repo.prototype.createCommit = function(updateRef, author, committer, message, tree, parents, callback) {
192200
if (tree instanceof Tree) {
193-
oldCreateCommit.call(
201+
Commit.createCommit.call(
194202
this,
195203
updateRef,
196204
author,
@@ -204,7 +212,7 @@ Repo.prototype.createCommit = function(updateRef, author, committer, message, tr
204212
var self = this;
205213
this.getTree(tree, function(error, tree) {
206214
if (error) return callback(error);
207-
oldCreateCommit.call(
215+
Commit.createCommit.call(
208216
self,
209217
updateRef,
210218
author,
@@ -226,7 +234,7 @@ Repo.prototype.createCommit = function(updateRef, author, committer, message, tr
226234
* @return {Blob}
227235
*/
228236
Repo.prototype.createBlobFromBuffer = function(buffer, callback) {
229-
oldCreateBlobFromBuffer.call(this, buffer, buffer.length, callback);
237+
Blob.createFrombuffer.call(this, buffer, buffer.length, callback);
230238
};
231239

232240
/**

0 commit comments

Comments
 (0)