Skip to content

Commit da4c29d

Browse files
committed
Raw and convenience API merged into one, using decorators
1 parent a2a5479 commit da4c29d

34 files changed

+333
-1277
lines changed

TODO

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
- rename all async methods getXXX
2-
- reorder functions so the raw api is oo-like
2+
- audit all ** methods so that all finds go through the repo in js land so that the .repo pointer is set.
33
- rename files remove .h
4+
- Cleanup diff api

example/convenience-commit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ git.repo('.git', function(error, repository) {
77
if (error) throw error;
88

99
// Use the master branch (a branch is the HEAD commit)
10-
repository.branch('master', function(error, branch) {
10+
repository.getBranch('master', function(error, branch) {
1111
if (error) throw error;
1212

1313
// History returns an event, and begins walking the history

example/convenience-tree.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ git.repo('.git', function(error, repository) {
66
if (error) throw error;
77

88
// Use the master branch.
9-
repository.branch('master', function(error, branch) {
9+
repository.getBranch('master', function(error, branch) {
1010
if (error) throw error;
1111

1212
// Iterate over the revision history.

include/commit.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,6 @@ class GitCommit : public ObjectWrap {
3838
static Handle<Value> Offset(const Arguments& args);
3939
static Handle<Value> Committer(const Arguments& args);
4040
static Handle<Value> Author(const Arguments& args);
41-
static Handle<Value> Tree(const Arguments& args);
42-
static void TreeWork(uv_work_t* req);
43-
static void TreeAfterWork(uv_work_t* req);
44-
45-
struct TreeBaton {
46-
uv_work_t request;
47-
int error_code;
48-
const git_error* error;
49-
git_tree * tree_out;
50-
Persistent<Value> commitReference;
51-
const git_commit * commit;
52-
Persistent<Function> callback;
53-
};
5441
static Handle<Value> TreeId(const Arguments& args);
5542
static Handle<Value> ParentCount(const Arguments& args);
5643
static Handle<Value> Parent(const Arguments& args);

lib/blob.js

Lines changed: 9 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,17 @@
1-
var git = require('../');
1+
var git = require('../'),
2+
Blob = git.Blob;
23

34
/**
4-
* Blob convenience class constructor.
5-
*
6-
* @constructor
7-
* @param {git.raw.Blob} [rawBlob = new git.raw.Blob(rawRepo)] Raw blob object.
5+
* Replace old content method with something nicer.
86
*/
9-
var Blob = function(rawBlob) {
10-
if (!(rawBlob instanceof git.raw.Blob)) {
11-
throw new Error('First parameter for Blob must be a raw blob');
12-
}
13-
this.rawBlob = rawBlob;
7+
var oldContent = Blob.prototype.content;
8+
Blob.prototype.content = function() {
9+
return oldContent.call(this).toBuffer(this.size());
1410
};
1511

1612
/**
17-
* Retrieve the blob's raw content buffer.
13+
* Retrieve the blob's content as String.
1814
*/
19-
Blob.prototype.rawContent = function() {
20-
return this.rawBlob.content().toBuffer(this.size());
15+
Blob.prototype.toString = function() {
16+
return this.content().toString();
2117
};
22-
23-
/**
24-
* Retrieve the blob's length.
25-
*/
26-
Blob.prototype.size = function() {
27-
return this.rawBlob.size();
28-
};
29-
30-
/**
31-
* Retrieve the blob's content.
32-
*/
33-
Blob.prototype.content = function(callback) {
34-
var content = this.rawContent();
35-
return content.toString();
36-
};
37-
38-
/**
39-
* Create a new blob from the file at the given path.
40-
*
41-
* @param {String} path Full path to the file.
42-
* @param {Blob~createFromFileCallback} callback
43-
*/
44-
Blob.prototype.createFromFile = function(path, callback) {
45-
/**
46-
* @callback Blob~createFromFileCallback Callback executed after blob is created.
47-
* @param {GitError|null} error An Error or null if successful.
48-
* @param {Blob|null} blob The new blob or null.
49-
*/
50-
git.raw.Blob.createFromFile(path, self.rawRepo, function blobCreateFromFileCallback(error, rawBlob) {
51-
if (error) return callback(error);
52-
callback(null, new Blob(rawBlob));
53-
});
54-
};
55-
56-
/**
57-
* Create a new blob from the given buffer.
58-
*
59-
* @param {Buffer} buffer Buffer used to create blob.
60-
* @param {Blob~createFromBufferCallback} callback
61-
*/
62-
Blob.prototype.createFromFile = function(path, callback) {
63-
/**
64-
* @callback Blob~createFromBufferCallback Callback executed after blob is created.
65-
* @param {GitError|null} error An Error or null if successful.
66-
* @param {Blob|null} content The new blob or null.
67-
*/
68-
var self = this;
69-
self.rawBlob.createFromBuffer(buffer, self.rawRepo, function blobCreateFromBufferCallback(error, rawBlob) {
70-
if (error) return callback(error);
71-
callback(null, new Blob(rawBlob));
72-
});
73-
};
74-
75-
exports.blob = Blob;

lib/commit.js

Lines changed: 19 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,6 @@
1-
var git = require( '../' ),
2-
events = require('events');
3-
4-
/**
5-
* Convenience commit constructor.
6-
*
7-
* @constructor
8-
* @param {git.raw.Repo} rawRepo Raw repository object.
9-
* @param {git.raw.Commit} [rawCommit = new git.raw.Commit(rawRepo)] Raw commit object.
10-
*/
11-
var Commit = function(repo, rawCommit) {
12-
if (!(repo instanceof git.repo)) {
13-
throw new Error('First parameter for Commit must be a raw repo');
14-
}
15-
this.repo = repo;
16-
17-
if (!(rawCommit instanceof git.raw.Commit)) {
18-
throw new Error('Second parameter for Commit must be a raw commit');
19-
}
20-
this.rawCommit = rawCommit;
21-
};
22-
23-
/**
24-
* Retrieve the commit's OID.
25-
*/
26-
Commit.prototype.oid = function() {
27-
return new git.oid(this.rawCommit.oid());
28-
};
1+
var git = require('../'),
2+
Commit = git.Commit,
3+
events = require('events');
294

305
/**
316
* Retrieve the SHA.
@@ -34,66 +9,25 @@ Commit.prototype.sha = function() {
349
return this.oid().sha();
3510
};
3611

37-
/**
38-
* Retrieve the message
39-
*/
40-
Commit.prototype.message = function() {
41-
return this.rawCommit.message();
42-
};
43-
4412
/**
4513
* Retrieve the commit time as a unix timestamp.
4614
*/
47-
Commit.prototype.time = function() {
48-
return this.rawCommit.time() * 1000;
15+
Commit.prototype.timeMs = function() {
16+
return this.time() * 1000;
4917
};
5018

5119
/**
5220
* Retrieve the commit time as a Date object.
5321
*/
5422
Commit.prototype.date = function() {
55-
return new Date(this.time());
56-
};
57-
58-
/**
59-
* Retrieve the commit's positive or negative timezone offset, in minutes from UTC.
60-
*/
61-
Commit.prototype.offset = function(callback) {
62-
return this.rawCommit.offset();
63-
};
64-
65-
/**
66-
* Retrieve the commit's author signature.
67-
*/
68-
Commit.prototype.author = function() {
69-
return this.rawCommit.author();
23+
return new Date(this.timeMs());
7024
};
7125

7226
/**
73-
* Retrieve the commit's committer.
74-
*/
75-
Commit.prototype.committer = function() {
76-
return this.rawCommit.committer();
77-
};
78-
79-
/**
80-
* Retrieve the tree for this commit.
81-
*
82-
* @param {Commit~getTreeCallback} callback
27+
* Get the tree associated with this commit.
8328
*/
8429
Commit.prototype.getTree = function(callback) {
85-
/**
86-
* @callback Commit~getTreeCallback Callback executed on tree retrieval
87-
* @param {GitError|null} error An Error or null if successful.
88-
* @param {Tree|null} Tree
89-
*/
90-
91-
var self = this;
92-
this.rawCommit.tree(function(error, rawTree) {
93-
if (error) return callback(error);
94-
95-
callback(null, new git.tree(self.repo, rawTree));
96-
});
30+
this.repo.getTree(this.treeId(), callback);
9731
};
9832

9933
/**
@@ -103,18 +37,16 @@ Commit.prototype.getTree = function(callback) {
10337
* @param {String} path
10438
* @param {Commit~fileCallback} callback
10539
*/
106-
Commit.prototype.file = function(path, callback) {
40+
Commit.prototype.getFile = function(path, callback) {
10741
/**
10842
* @callback Commit~fileCallback Callback executed on file retrieval.
10943
* @param {GitError|null} error An Error or null if successful.
11044
* @param {Entry|null} file Retrieved file entry.
11145
*/
11246
this.getTree(function (error, tree) {
11347
if (error) return callback(error);
114-
tree.entry(path, function(error, entry) {
115-
if (error) return callback(error);
116-
callback(null, entry);
117-
});
48+
49+
tree.getEntryByPath(path, callback);
11850
});
11951
};
12052

@@ -132,7 +64,7 @@ Commit.prototype.history = function() {
13264
var event = new events.EventEmitter();
13365

13466
var oid = this.oid();
135-
var revwalk = new git.revwalk(this.repo, this.repo.rawRepo.createRevWalk());
67+
var revwalk = this.repo.createRevWalk();
13668
var commits = [];
13769
event.start = function() {
13870
revwalk.walk(oid, function commitRevWalk(error, commit) {
@@ -170,24 +102,23 @@ Commit.prototype.history = function() {
170102
*
171103
* @param {Commit~parentsCallback} callback
172104
*/
173-
Commit.prototype.parents = function(callback) {
105+
Commit.prototype.getParents = function(callback) {
174106
/**
175107
* @callback Commit~parentsCallback Callback executed on parents retrieval.
176108
* @param {GitError|null} error An Error or null if successful.
177109
* @param {Commit[]|null} parents Commit's parent(s).
178110
*/
179111
var self = this;
180-
181-
function processParents(rawCommit, n, acc, callback) {
112+
function processParents(commit, n, acc, callback) {
182113
if (n < 0) return callback(null, acc);
183114

184-
rawCommit.parent(n, function nextParent(error, rawParentCommit) {
115+
self.repo.getCommit(self.parentId(n), function nextParent(error, parent) {
185116
if (error) return callback(error);
186-
processParents(rawParentCommit, n-1, acc.concat([new Commit(self.repo, rawParentCommit)]), callback);
117+
processParents(parent, n-1, acc.concat([parent]), callback);
187118
});
188119
}
189120

190-
processParents(this.rawCommit, this.rawCommit.parentCount() - 1, [], callback);
121+
processParents(this, this.parentCount() - 1, [], callback);
191122
};
192123

193124
/**
@@ -196,14 +127,14 @@ Commit.prototype.parents = function(callback) {
196127
*
197128
* @param {Commit~parentsDiffTreesCallback} callback
198129
*/
199-
Commit.prototype.parentsDiffTrees = function(callback) {
130+
Commit.prototype.diff = function(callback) {
200131
/**
201132
* @callback Commit~parentsDiffTreesCallback Callback executed on diff trees retrieval.
202133
* @param {GitError|null} error An Error or null if successful.
203134
* @param {DiffList[]|null} diffLists Array of DiffTrees showing changes between this commit and its parent(s)
204135
*/
205136
var self = this;
206-
self.parents(function commitParents(error, parents) {
137+
self.getParents(function commitParents(error, parents) {
207138
if (error) return callback(error);
208139

209140
var parentDiffLists = [];
@@ -227,5 +158,3 @@ Commit.prototype.parentsDiffTrees = function(callback) {
227158
});
228159
});
229160
};
230-
231-
exports.commit = Commit;

0 commit comments

Comments
 (0)