| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -27,7 +27,7 @@ class GitCommit : public ObjectWrap { | |||
| 27 | 27 | /** | |
| 28 | 28 | * v8::FunctionTemplate used to create Node.js constructor | |
| 29 | 29 | */ | |
| 30 | - static Persistent<FunctionTemplate> constructor_template; | ||
| 30 | + static Persistent<Function> constructor_template; | ||
| 31 | 31 | ||
| 32 | 32 | /** | |
| 33 | 33 | * Used to intialize the EventEmitter from Node.js | |
@@ -56,10 +56,11 @@ class GitCommit : public ObjectWrap { | |||
| 56 | 56 | ~GitCommit() {} | |
| 57 | 57 | ||
| 58 | 58 | static Handle<Value> New(const Arguments& args); | |
| 59 | + static Handle<Value> NewInstance(); | ||
| 59 | 60 | ||
| 60 | 61 | 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); | ||
| 63 | 64 | ||
| 64 | 65 | static Handle<Value> Close(const Arguments& args); | |
| 65 | 66 | static Handle<Value> Id(const Arguments& args); | |
@@ -71,11 +72,15 @@ class GitCommit : public ObjectWrap { | |||
| 71 | 72 | static Handle<Value> Author(const Arguments& args); | |
| 72 | 73 | ||
| 73 | 74 | 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); | ||
| 76 | 77 | ||
| 77 | 78 | static Handle<Value> ParentCount(const Arguments& args); | |
| 79 | + | ||
| 80 | + static Handle<Value> ParentSync(const Arguments& args); | ||
| 78 | 81 | static Handle<Value> Parent(const Arguments& args); | |
| 82 | + static void ParentWork(uv_work_t* req); | ||
| 83 | + static void ParentAfterWork(uv_work_t* req); | ||
| 79 | 84 | ||
| 80 | 85 | private: | |
| 81 | 86 | git_commit* commit; | |
@@ -89,10 +94,22 @@ class GitCommit : public ObjectWrap { | |||
| 89 | 94 | Persistent<Function> callback; | |
| 90 | 95 | }; | |
| 91 | 96 | ||
| 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 | + | ||
| 97 | 114 | }; | |
| 98 | 115 | #endif | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,6 +8,7 @@ | |||
| 8 | 8 | #include <node.h> | |
| 9 | 9 | ||
| 10 | 10 | #include "../vendor/libgit2/include/git2.h" | |
| 11 | + #include "cvv8/v8-convert.hpp" | ||
| 11 | 12 | ||
| 12 | 13 | #include "../include/reference.h" | |
| 13 | 14 | #include "../include/sig.h" | |
@@ -22,25 +23,26 @@ using namespace node; | |||
| 22 | 23 | void GitCommit::Initialize(Handle<Object> target) { | |
| 23 | 24 | HandleScope scope; | |
| 24 | 25 | ||
| 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); | ||
| 44 | 46 | } | |
| 45 | 47 | ||
| 46 | 48 | git_commit* GitCommit::GetValue() { | |
@@ -112,6 +114,14 @@ Handle<Value> GitCommit::New(const Arguments& args) { | |||
| 112 | 114 | return scope.Close(args.This()); | |
| 113 | 115 | } | |
| 114 | 116 | ||
| 117 | + Handle<Value> GitCommit::NewInstance() { | ||
| 118 | + HandleScope scope; | ||
| 119 | + | ||
| 120 | + Local<Object> instance = constructor_template->NewInstance(); | ||
| 121 | + | ||
| 122 | + return scope.Close(instance); | ||
| 123 | + } | ||
| 124 | + | ||
| 115 | 125 | Handle<Value> GitCommit::Lookup(const Arguments& args) { | |
| 116 | 126 | HandleScope scope; | |
| 117 | 127 | ||
@@ -142,20 +152,19 @@ Handle<Value> GitCommit::Lookup(const Arguments& args) { | |||
| 142 | 152 | ||
| 143 | 153 | uv_work_t *req = new uv_work_t; | |
| 144 | 154 | 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); | ||
| 146 | 156 | ||
| 147 | 157 | return scope.Close( Undefined() ); | |
| 148 | 158 | } | |
| 149 | 159 | ||
| 150 | - void GitCommit::EIO_Lookup(uv_work_t *req) { | ||
| 160 | + void GitCommit::LookupWork(uv_work_t *req) { | ||
| 151 | 161 | lookup_request *ar = static_cast<lookup_request *>(req->data); | |
| 152 | 162 | ||
| 153 | 163 | git_oid oid = ar->oid->GetValue(); | |
| 154 | 164 | ar->err = ar->commit->Lookup(ar->repo->GetValue(), &oid); | |
| 155 | - | ||
| 156 | 165 | } | |
| 157 | 166 | ||
| 158 | - void GitCommit::EIO_AfterLookup(uv_work_t *req) { | ||
| 167 | + void GitCommit::LookupAfterWork(uv_work_t *req) { | ||
| 159 | 168 | HandleScope scope; | |
| 160 | 169 | ||
| 161 | 170 | lookup_request *ar = static_cast<lookup_request *>(req->data); | |
@@ -217,7 +226,7 @@ Handle<Value> GitCommit::Message(const Arguments& args) { | |||
| 217 | 226 | ||
| 218 | 227 | GitCommit *commit = ObjectWrap::Unwrap<GitCommit>(args.This()); | |
| 219 | 228 | ||
| 220 | - return scope.Close( String::New(commit->Message()) ); | ||
| 229 | + return scope.Close(String::New(commit->Message())); | ||
| 221 | 230 | } | |
| 222 | 231 | ||
| 223 | 232 | Handle<Value> GitCommit::Time(const Arguments& args) { | |
@@ -296,27 +305,107 @@ Handle<Value> GitCommit::ParentCount(const Arguments& args) { | |||
| 296 | 305 | return scope.Close( Integer::New(count) ); | |
| 297 | 306 | } | |
| 298 | 307 | ||
| 299 | - Handle<Value> GitCommit::Parent(const Arguments& args) { | ||
| 308 | + Handle<Value> GitCommit::ParentSync(const Arguments& args) { | ||
| 300 | 309 | HandleScope scope; | |
| 301 | 310 | ||
| 311 | + if(args.Length() == 0 || !args[0]->IsNumber()) { | ||
| 312 | + return ThrowException(Exception::Error(String::New("Position must be a Number."))); | ||
| 313 | + } | ||
| 314 | + | ||
| 302 | 315 | GitCommit *commit = ObjectWrap::Unwrap<GitCommit>(args.This()); | |
| 303 | 316 | ||
| 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()))); | ||
| 306 | 322 | } | |
| 307 | 323 | ||
| 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()) { | ||
| 309 | 339 | return ThrowException(Exception::Error(String::New("Position is required and must be a Number."))); | |
| 310 | 340 | } | |
| 311 | 341 | ||
| 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 | + } | ||
| 315 | 345 | ||
| 316 | - int err = commit->Parent(&in, index); | ||
| 317 | - out->SetValue(in); | ||
| 346 | + Local<Function> callback = Local<Function>::Cast(args[1]); | ||
| 318 | 347 | ||
| 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; | ||
| 320 | 409 | } | |
| 321 | 410 | ||
| 322 | - Persistent<FunctionTemplate> GitCommit::constructor_template; | ||
| 411 | + Persistent<Function> GitCommit::constructor_template; | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments