Skip to content

Commit 552fae7

Browse files
committed
Threads & Tree now codgen'd
1 parent 2d3b9b6 commit 552fae7

24 files changed

+476
-500
lines changed

gen.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var idefs = JSON.parse(fs.readFileSync('v0.18.0.json')),
88

99
for (var i in idefs) {
1010
var idef = idefs[i];
11-
if (["Oid", "Blob", "Repo", "Reference", "Object", "TreeEntry", "Commit", "Signature", "Time", "Index", "Tag"].indexOf(idef.jsClassName) > -1) {
11+
if (["Oid", "Blob", "Repo", "Reference", "Object", "TreeEntry", "Commit", "Signature", "Time", "Index", "Tag", "Threads", "Tree"].indexOf(idef.jsClassName) > -1) {
1212
fs.writeFileSync(
1313
path.resolve("./include/" + idef.filename), headerTemplate(idef));
1414
fs.writeFileSync(

include/threads.h

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,33 @@
1-
/*
2-
* Copyright 2011, Tim Branyen @tbranyen <tim@tabdeveloper.com>
3-
* @author Michael Robinson @codeofinterest <mike@pagesofinterest.net>
4-
*
5-
* Dual licensed under the MIT and GPL licenses.
6-
*/
1+
/**
2+
* This code is auto-generated; unless you know what you're doing, do not modify!
3+
**/
4+
5+
#ifndef GITTHREADS_H
6+
#define GITTHREADS_H
77

88
#include <v8.h>
99
#include <node.h>
10+
#include <string>
1011

1112
#include "git2.h"
1213

1314
using namespace node;
1415
using namespace v8;
1516

16-
/**
17-
* Class wrapper for libgit2 git_threads_*
18-
*/
1917
class GitThreads : public ObjectWrap {
2018
public:
21-
/**
22-
* v8::FunctionTemplate used to create Node.js constructor
23-
*/
24-
static Persistent<Function> constructor_template;
2519

20+
static Persistent<Function> constructor_template;
2621
static void Initialize (Handle<v8::Object> target);
2722

28-
/**
29-
* Calls git_threads_init synchronously. This is called one time
30-
* on initialization, and is required for libgit2 to run correctly.
31-
*/
32-
static Handle<Value> Init(const Arguments& args);
33-
34-
protected:
3523

36-
GitThreads() {}
37-
~GitThreads() {}
24+
private:
3825

3926
static Handle<Value> New(const Arguments& args);
4027

28+
29+
static Handle<Value> Init(const Arguments& args);
30+
static Handle<Value> Shutdown(const Arguments& args);
4131
};
32+
33+
#endif

include/tree.h

Lines changed: 29 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,68 @@
1-
/*
2-
* Copyright 2013, Tim Branyen @tbranyen <tim@tabdeveloper.com>
3-
* @author Michael Robinson @codeofinterest <mike@pagesofinterest.net>
4-
*
5-
* Dual licensed under the MIT and GPL licenses.
6-
*/
1+
/**
2+
* This code is auto-generated; unless you know what you're doing, do not modify!
3+
**/
74

85
#ifndef GITTREE_H
96
#define GITTREE_H
107

118
#include <v8.h>
129
#include <node.h>
1310
#include <string>
14-
#include <vector>
1511

1612
#include "git2.h"
1713

18-
#include "repo.h"
19-
#include "tree_entry.h"
20-
21-
using namespace v8;
2214
using namespace node;
15+
using namespace v8;
2316

24-
/**
25-
* Class wrapper for libgit2 git_tree
26-
*/
2717
class GitTree : public ObjectWrap {
2818
public:
2919

3020
static Persistent<Function> constructor_template;
21+
static void Initialize (Handle<v8::Object> target);
3122

32-
static const int WALK_ENTRY_SEND_THRESHOLD = 10;
33-
34-
static void Initialize(Handle<v8::Object> target);
35-
36-
git_tree* GetValue();
23+
git_tree *GetValue();
3724

38-
protected:
25+
private:
3926
GitTree(git_tree *raw);
4027
~GitTree();
4128

4229
static Handle<Value> New(const Arguments& args);
4330

31+
4432
static Handle<Value> Lookup(const Arguments& args);
4533
static void LookupWork(uv_work_t* req);
4634
static void LookupAfterWork(uv_work_t* req);
4735

48-
static Handle<Value> Walk(const Arguments& args);
49-
static void WalkWork(void* payload);
50-
static int WalkWorkEntry(const char *root, const git_tree_entry *entry, void *payload);
51-
static void WalkWorkSendEntry(uv_async_t *handle, int status /*UNUSED*/);
52-
static void WalkWorkSendEnd(uv_async_t *handle, int status /*UNUSED*/);
53-
54-
static Handle<Value> EntryByPath(const Arguments& args);
55-
static void EntryByPathWork(uv_work_t *req);
56-
static void EntryByPathAfterWork(uv_work_t *req);
57-
58-
private:
59-
60-
git_tree* raw;
61-
6236
struct LookupBaton {
6337
uv_work_t request;
6438
const git_error* error;
65-
66-
git_oid rawOid;
67-
git_repository* rawRepo;
68-
git_tree* rawTree;
69-
39+
git_tree * out;
40+
Persistent<Value> repoReference;
41+
git_repository * repo;
42+
Persistent<Value> idReference;
43+
const git_oid * id;
7044
Persistent<Function> callback;
7145
};
72-
73-
struct WalkEntry {
74-
git_tree_entry* rawEntry;
75-
std::string root;
76-
};
77-
78-
struct WalkBaton {
79-
uv_thread_t threadId;
80-
uv_mutex_t mutex;
81-
uv_async_t asyncEntry;
82-
uv_async_t asyncEnd;
83-
84-
const git_error* error;
85-
86-
std::vector<WalkEntry* > rawTreeEntries;
87-
88-
git_tree* rawTree;
89-
bool blobsOnly;
90-
91-
Persistent<Function> entryCallback;
92-
Persistent<Function> endCallback;
93-
};
94-
95-
struct EntryByPathBaton {
46+
static Handle<Value> Oid(const Arguments& args);
47+
static Handle<Value> Size(const Arguments& args);
48+
static Handle<Value> EntryByName(const Arguments& args);
49+
static Handle<Value> EntryByIndex(const Arguments& args);
50+
static Handle<Value> EntryByOid(const Arguments& args);
51+
static Handle<Value> GetEntryByPath(const Arguments& args);
52+
static void GetEntryByPathWork(uv_work_t* req);
53+
static void GetEntryByPathAfterWork(uv_work_t* req);
54+
55+
struct GetEntryByPathBaton {
9656
uv_work_t request;
9757
const git_error* error;
98-
99-
git_tree* rawTree;
100-
std::string path;
101-
git_tree_entry* rawEntry;
102-
58+
git_tree_entry * out;
59+
Persistent<Value> rootReference;
60+
git_tree * root;
61+
Persistent<Value> pathReference;
62+
const char * path;
10363
Persistent<Function> callback;
10464
};
65+
git_tree *raw;
10566
};
10667

10768
#endif

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ exports.entry = require('./tree_entry.js').entry;
3636
exports.version = require('../package').version;
3737

3838
// Initialize threads
39-
(new exports.raw.Threads()).init();
39+
exports.raw.Threads.init();

lib/repo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ Repo.prototype.tree = function(oid, callback) {
138138
* @param {Tree|null} tree The tree object or null.
139139
*/
140140
var self = this;
141-
git.raw.Tree.lookup(oid.rawOid, this.rawRepo, function(error, rawTree) {
141+
git.raw.Tree.lookup(this.rawRepo, oid.rawOid, function(error, rawTree) {
142142
if (success(error, callback)) {
143143
callback(null, new git.tree(self, rawTree));
144144
}

lib/tree.js

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,20 @@ Tree.prototype.entry = function(path, callback) {
3535
* @param {Entry|null} entry The tree entry object or null.
3636
*/
3737
var self = this;
38-
self.rawTree.entryByPath(path, function(error, rawEntry) {
38+
self.rawTree.getEntryByPath(path, function(error, rawEntry) {
3939
if (success(error, callback)) {
4040
callback(null, new git.entry(self.repo, rawEntry));
4141
}
4242
});
4343
};
4444

45+
/**
46+
* Retrieve the number of entries in this tree.
47+
*/
48+
Tree.prototype.size = function() {
49+
return self.rawTree.size();
50+
};
51+
4552
/**
4653
* Walk the tree.
4754
*
@@ -53,39 +60,49 @@ Tree.prototype.entry = function(path, callback) {
5360
* @return {EventEmitter}
5461
*/
5562
Tree.prototype.walk = function(blobsOnly) {
56-
blobsOnly = typeof blobsOnly === 'undefined' ? true : blobsOnly;
63+
if (typeof blobsOnly == 'undefined') blobsOnly = true;
5764

5865
var self = this,
5966
event = new events.EventEmitter(),
60-
entries = [];
67+
entries = [],
68+
errors = [];
6169

62-
var total = 0;
70+
var total = 1;
6371

64-
self.rawTree.walk(blobsOnly, function treeWalkEntries(error, rawEntries) {
65-
rawEntries.forEach(function treeWalkEntryEmitter(rawEntry) {
66-
var entry = new git.entry(self.repo, rawEntry);
67-
entries.push(entry);
68-
/**
69-
* Entry event.
70-
*
71-
* @event Tree#entry
72-
*
73-
* @param {GitError|null} error An error object if there was an issue, null otherwise.
74-
* @param {Entry} entry The tree entry.
75-
*/
76-
event.emit('entry', null, entry);
77-
});
78-
}, function treeWalkEnd(error) {
79-
/**
80-
* End event.
81-
*
82-
* @event Tree#end
83-
*
84-
* @param {GitError|null} error An error object if there was an issue, null otherwise.
85-
* @param {Entry[]} entries The tree entries.
86-
*/
87-
event.emit('end', error ? new git.error(error.message, error.code) : null, entries);
88-
});
72+
function dfs(error, tree) {
73+
total--;
74+
75+
var size = tree.rawTree.size();
76+
if (error) return errors.push(error);
77+
78+
for (var i = 0; i < size; i ++) {
79+
var rawEntry = tree.rawTree.entryByIndex(i),
80+
entry = new git.entry(tree.repo, rawEntry);
81+
if (!blobsOnly || entry.isFile()) {
82+
/**
83+
* Entry event.
84+
*
85+
* @event Tree#entry
86+
*
87+
* @param {GitError|null} error An error object if there was an issue, null otherwise.
88+
* @param {Entry} entry The tree entry.
89+
*/
90+
event.emit('entry', entry);
91+
entries.push(entry);
92+
}
93+
94+
if (entry.isTree()) {
95+
total++;
96+
entry.getTree(dfs);
97+
}
98+
}
99+
if (total === 0)
100+
event.emit('end', errors.length ? errors : null, entries);
101+
}
102+
103+
event.start = function(e) {
104+
dfs(null, self);
105+
};
89106

90107
return event;
91108
};

lib/tree_entry.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@ TreeEntry.prototype.isFile = function() {
4242
this.filemode() === TreeEntry.FileMode.Executable;
4343
};
4444

45-
TreeEntry.prototype.isDirectory = function() {
45+
TreeEntry.prototype.isTree = function() {
4646
return this.filemode() === TreeEntry.FileMode.Tree;
4747
};
4848

49+
TreeEntry.prototype.isDirectory = TreeEntry.prototype.isTree;
50+
4951
/**
5052
* Retrieve the Oid for this TreeEntry.
5153
*/
@@ -96,8 +98,7 @@ TreeEntry.prototype.getBlob = function(callback) {
9698
* @param {TreeEntry~treeCallback} callback
9799
*/
98100
TreeEntry.prototype.getTree = function(callback) {
99-
if (!this.isDirectory()) return callback(new git.error('Not a tree/directory', 0));
100-
101+
if (!this.isTree()) return callback(new git.error('Not a tree/directory', 0));
101102
/**
102103
* @callback TreeEntry~treeCallback Callback executed after blob is retrieved.
103104
* @param {GitError|null} error An Error or null if successful.

src/blob.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ void GitBlob::LookupAfterWork(uv_work_t *req) {
137137
Handle<Value> GitBlob::Oid(const Arguments& args) {
138138
HandleScope scope;
139139

140-
const git_oid * result = git_blob_id(
140+
const git_oid * result = git_blob_id(
141141

142142

143143
ObjectWrap::Unwrap<GitBlob>(args.This())->GetValue()
@@ -152,7 +152,7 @@ const git_oid * result = git_blob_id(
152152
Handle<Value> GitBlob::Content(const Arguments& args) {
153153
HandleScope scope;
154154

155-
const void * result = git_blob_rawcontent(
155+
const void * result = git_blob_rawcontent(
156156

157157

158158
ObjectWrap::Unwrap<GitBlob>(args.This())->GetValue()
@@ -167,7 +167,7 @@ const void * result = git_blob_rawcontent(
167167
Handle<Value> GitBlob::Size(const Arguments& args) {
168168
HandleScope scope;
169169

170-
git_off_t result = git_blob_rawsize(
170+
git_off_t result = git_blob_rawsize(
171171

172172

173173
ObjectWrap::Unwrap<GitBlob>(args.This())->GetValue()
@@ -325,7 +325,7 @@ void GitBlob::CreateFromBufferAfterWork(uv_work_t *req) {
325325
Handle<Value> GitBlob::IsBinary(const Arguments& args) {
326326
HandleScope scope;
327327

328-
int result = git_blob_is_binary(
328+
int result = git_blob_is_binary(
329329

330330

331331
ObjectWrap::Unwrap<GitBlob>(args.This())->GetValue()

0 commit comments

Comments
 (0)