FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Renamed async methods, added refactored constructor template, added · philschatz/nodegit@87c31e8 · GitHub

Repository navigation

Commit 87c31e8

Browse files
committed
Renamed async methods, added refactored constructor template, added
parentSync, made parent async
1 parent 2d71044 commit 87c31e8

2 files changed

Lines changed: 151 additions & 45 deletions

File tree

‎include/commit.h‎

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class GitCommit : public ObjectWrap {
2727
/**
2828
* v8::FunctionTemplate used to create Node.js constructor
2929
*/
30-
static Persistent<FunctionTemplate> constructor_template;
30+
static Persistent<Function> constructor_template;
3131

3232
/**
3333
* Used to intialize the EventEmitter from Node.js
@@ -56,10 +56,11 @@ class GitCommit : public ObjectWrap {
5656
~GitCommit() {}
5757

5858
static Handle<Value> New(const Arguments& args);
59+
static Handle<Value> NewInstance();
5960

6061
static Handle<Value> Lookup(const Arguments& args);
61-
static void EIO_Lookup(uv_work_t *req);
62-
static void EIO_AfterLookup(uv_work_t *req);
62+
static void LookupWork(uv_work_t *req);
63+
static void LookupAfterWork(uv_work_t *req);
6364

6465
static Handle<Value> Close(const Arguments& args);
6566
static Handle<Value> Id(const Arguments& args);
@@ -71,11 +72,15 @@ class GitCommit : public ObjectWrap {
7172
static Handle<Value> Author(const Arguments& args);
7273

7374
static Handle<Value> Tree(const Arguments& args);
74-
static void EIO_Tree(uv_work_t* req);
75-
static void EIO_AfterTree(uv_work_t* req);
75+
static void TreeWork(uv_work_t* req);
76+
static void TreeAfterWork(uv_work_t* req);
7677

7778
static Handle<Value> ParentCount(const Arguments& args);
79+
80+
static Handle<Value> ParentSync(const Arguments& args);
7881
static Handle<Value> Parent(const Arguments& args);
82+
static void ParentWork(uv_work_t* req);
83+
static void ParentAfterWork(uv_work_t* req);
7984

8085
private:
8186
git_commit* commit;
@@ -89,10 +94,22 @@ class GitCommit : public ObjectWrap {
8994
Persistent<Function> callback;
9095
};
9196

92-
//struct tree_request {
93-
// GitCommit* commit;
94-
// Tree* tree;
95-
// Persistent<Function> callback;
96-
//};
97+
/**
98+
* Struct: parent_request
99+
* Contains references to the current commit, parent commit (output)
100+
* parent commit's index, also contains references to an error code post
101+
* lookup, and a callback function to execute.
102+
*/
103+
struct ParentBaton {
104+
uv_work_t request;
105+
int errorCode;
106+
const char* errorMessage;
107+
108+
int index;
109+
GitCommit *commit;
110+
git_commit *rawParentCommit;
111+
Persistent<Function> callback;
112+
};
113+
97114
};
98115
#endif

‎src/commit.cc‎

Lines changed: 124 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <node.h>
99

1010
#include "../vendor/libgit2/include/git2.h"
11+
#include "cvv8/v8-convert.hpp"
1112

1213
#include "../include/reference.h"
1314
#include "../include/sig.h"
@@ -22,25 +23,26 @@ using namespace node;
2223
void GitCommit::Initialize(Handle<Object> target) {
2324
HandleScope scope;
2425

25-
Local<FunctionTemplate> t = FunctionTemplate::New(New);
26-
27-
constructor_template = Persistent<FunctionTemplate>::New(t);
28-
constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
29-
constructor_template->SetClassName(String::NewSymbol("Commit"));
30-
31-
NODE_SET_PROTOTYPE_METHOD(constructor_template, "lookup", Lookup);
32-
NODE_SET_PROTOTYPE_METHOD(constructor_template, "close", Close);
33-
NODE_SET_PROTOTYPE_METHOD(constructor_template, "id", Id);
34-
NODE_SET_PROTOTYPE_METHOD(constructor_template, "messageShort", MessageShort);
35-
NODE_SET_PROTOTYPE_METHOD(constructor_template, "message", Message);
36-
NODE_SET_PROTOTYPE_METHOD(constructor_template, "time", Time);
37-
NODE_SET_PROTOTYPE_METHOD(constructor_template, "timeOffset", TimeOffset);
38-
NODE_SET_PROTOTYPE_METHOD(constructor_template, "author", Author);
39-
NODE_SET_PROTOTYPE_METHOD(constructor_template, "tree", Tree);
40-
NODE_SET_PROTOTYPE_METHOD(constructor_template, "parentCount", ParentCount);
41-
NODE_SET_PROTOTYPE_METHOD(constructor_template, "parent", Parent);
42-
43-
target->Set(String::NewSymbol("Commit"), constructor_template->GetFunction());
26+
Local<FunctionTemplate> tpl = FunctionTemplate::New(New);
27+
28+
tpl->InstanceTemplate()->SetInternalFieldCount(1);
29+
tpl->SetClassName(String::NewSymbol("Commit"));
30+
31+
NODE_SET_PROTOTYPE_METHOD(tpl, "lookup", Lookup);
32+
NODE_SET_PROTOTYPE_METHOD(tpl, "close", Close);
33+
NODE_SET_PROTOTYPE_METHOD(tpl, "id", Id);
34+
NODE_SET_PROTOTYPE_METHOD(tpl, "messageShort", MessageShort);
35+
NODE_SET_PROTOTYPE_METHOD(tpl, "message", Message);
36+
NODE_SET_PROTOTYPE_METHOD(tpl, "time", Time);
37+
NODE_SET_PROTOTYPE_METHOD(tpl, "timeOffset", TimeOffset);
38+
NODE_SET_PROTOTYPE_METHOD(tpl, "author", Author);
39+
NODE_SET_PROTOTYPE_METHOD(tpl, "tree", Tree);
40+
NODE_SET_PROTOTYPE_METHOD(tpl, "parentCount", ParentCount);
41+
NODE_SET_PROTOTYPE_METHOD(tpl, "parent", Parent);
42+
NODE_SET_PROTOTYPE_METHOD(tpl, "parentSync", ParentSync);
43+
44+
constructor_template = Persistent<Function>::New(tpl->GetFunction());
45+
target->Set(String::NewSymbol("Commit"), constructor_template);
4446
}
4547

4648
git_commit* GitCommit::GetValue() {
@@ -112,6 +114,14 @@ Handle<Value> GitCommit::New(const Arguments& args) {
112114
return scope.Close(args.This());
113115
}
114116

117+
Handle<Value> GitCommit::NewInstance() {
118+
HandleScope scope;
119+
120+
Local<Object> instance = constructor_template->NewInstance();
121+
122+
return scope.Close(instance);
123+
}
124+
115125
Handle<Value> GitCommit::Lookup(const Arguments& args) {
116126
HandleScope scope;
117127

@@ -142,20 +152,19 @@ Handle<Value> GitCommit::Lookup(const Arguments& args) {
142152

143153
uv_work_t *req = new uv_work_t;
144154
req->data = ar;
145-
uv_queue_work(uv_default_loop(), req, EIO_Lookup, EIO_AfterLookup);
155+
uv_queue_work(uv_default_loop(), req, LookupWork, LookupAfterWork);
146156

147157
return scope.Close( Undefined() );
148158
}
149159

150-
void GitCommit::EIO_Lookup(uv_work_t *req) {
160+
void GitCommit::LookupWork(uv_work_t *req) {
151161
lookup_request *ar = static_cast<lookup_request *>(req->data);
152162

153163
git_oid oid = ar->oid->GetValue();
154164
ar->err = ar->commit->Lookup(ar->repo->GetValue(), &oid);
155-
156165
}
157166

158-
void GitCommit::EIO_AfterLookup(uv_work_t *req) {
167+
void GitCommit::LookupAfterWork(uv_work_t *req) {
159168
HandleScope scope;
160169

161170
lookup_request *ar = static_cast<lookup_request *>(req->data);
@@ -217,7 +226,7 @@ Handle<Value> GitCommit::Message(const Arguments& args) {
217226

218227
GitCommit *commit = ObjectWrap::Unwrap<GitCommit>(args.This());
219228

220-
return scope.Close( String::New(commit->Message()) );
229+
return scope.Close(String::New(commit->Message()));
221230
}
222231

223232
Handle<Value> GitCommit::Time(const Arguments& args) {
@@ -296,27 +305,107 @@ Handle<Value> GitCommit::ParentCount(const Arguments& args) {
296305
return scope.Close( Integer::New(count) );
297306
}
298307

299-
Handle<Value> GitCommit::Parent(const Arguments& args) {
308+
Handle<Value> GitCommit::ParentSync(const Arguments& args) {
300309
HandleScope scope;
301310

311+
if(args.Length() == 0 || !args[0]->IsNumber()) {
312+
return ThrowException(Exception::Error(String::New("Position must be a Number.")));
313+
}
314+
302315
GitCommit *commit = ObjectWrap::Unwrap<GitCommit>(args.This());
303316

304-
if(args.Length() == 0 || !args[0]->IsObject()) {
305-
return ThrowException(Exception::Error(String::New("Commit is required and must be an Object.")));
317+
git_commit *parentCommitValue ;
318+
int errorCode = git_commit_parent(&parentCommitValue, commit->commit, args[0]->ToInteger()->Value());
319+
320+
if (errorCode) {
321+
return ThrowException(Exception::Error(String::New(git_lasterror())));
306322
}
307323

308-
if(args.Length() == 1 || !args[1]->IsNumber()) {
324+
Local<Object> parent = GitCommit::constructor_template->NewInstance();
325+
GitCommit *parentCommit = ObjectWrap::Unwrap<GitCommit>(parent);
326+
parentCommit->SetValue(parentCommitValue);
327+
328+
return scope.Close(parent);
329+
}
330+
331+
Handle<Value> GitCommit::Parent(const Arguments& args) {
332+
HandleScope scope;
333+
334+
if(args.Length() != 2) {
335+
return ThrowException(Exception::Error(String::New("Position and callback are required")));
336+
}
337+
338+
if(!args[0]->IsNumber()) {
309339
return ThrowException(Exception::Error(String::New("Position is required and must be a Number.")));
310340
}
311341

312-
GitCommit* out = ObjectWrap::Unwrap<GitCommit>(args[0]->ToObject());
313-
git_commit* in;
314-
int index = args[1]->ToInteger()->Value();
342+
if(!args[1]->IsFunction()) {
343+
return ThrowException(Exception::Error(String::New("Callback is required and must be a Function.")));
344+
}
315345

316-
int err = commit->Parent(&in, index);
317-
out->SetValue(in);
346+
Local<Function> callback = Local<Function>::Cast(args[1]);
318347

319-
return scope.Close( Integer::New(err) );
348+
ParentBaton* baton = new ParentBaton();
349+
baton->request.data = baton;
350+
baton->commit = ObjectWrap::Unwrap<GitCommit>(args.This());
351+
baton->commit->Ref();
352+
baton->index = args[0]->ToInteger()->Value();
353+
baton->callback = Persistent<Function>::New(callback);
354+
355+
uv_queue_work(uv_default_loop(), &baton->request, ParentWork, ParentAfterWork);
356+
357+
return Undefined();
358+
}
359+
360+
void GitCommit::ParentWork(uv_work_t* req) {
361+
ParentBaton* baton = static_cast<ParentBaton*>(req->data);
362+
363+
baton->rawParentCommit = NULL;
364+
baton->errorCode = git_commit_parent(&baton->rawParentCommit, baton->commit->commit, baton->index);
365+
366+
if (baton->errorCode) {
367+
baton->errorMessage = git_lasterror();
368+
}
369+
}
370+
371+
void GitCommit::ParentAfterWork(uv_work_t* req) {
372+
HandleScope scope;
373+
374+
ParentBaton* baton = static_cast<ParentBaton* >(req->data);
375+
delete req;
376+
377+
if (baton->errorCode) {
378+
Local<Value> argv[1] = {
379+
Exception::Error(String::New(baton->errorMessage))
380+
};
381+
382+
TryCatch try_catch;
383+
384+
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
385+
386+
if (try_catch.HasCaught()) {
387+
node::FatalException(try_catch);
388+
}
389+
} else {
390+
391+
Local<Object> parent = GitCommit::constructor_template->NewInstance();
392+
GitCommit *parentCommit = ObjectWrap::Unwrap<GitCommit>(parent);
393+
parentCommit->SetValue(baton->rawParentCommit);
394+
395+
Handle<Value> argv[2] = {
396+
Local<Value>::New(Null()),
397+
parent
398+
};
399+
400+
TryCatch try_catch;
401+
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
402+
if (try_catch.HasCaught()) {
403+
node::FatalException(try_catch);
404+
}
405+
}
406+
baton->commit->Unref();
407+
baton->callback.Dispose();
408+
delete baton;
320409
}
321410

322-
Persistent<FunctionTemplate> GitCommit::constructor_template;
411+
Persistent<Function> GitCommit::constructor_template;

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL