@@ -11,6 +11,7 @@ Copyright (c) 2011, Tim Branyen @tbranyen <tim@tabdeveloper.com>
1111#include " ../include/object.h"
1212#include " ../include/repo.h"
1313#include " ../include/commit.h"
14+ #include " ../include/error.h"
1415
1516using namespace v8 ;
1617using namespace node ;
@@ -61,15 +62,12 @@ Handle<Value> GitRepo::New(const Arguments& args) {
6162 GitRepo *repo = new GitRepo ();
6263 repo->Wrap (args.This ());
6364
64- return scope.Close ( args.This () );
65+ return scope.Close (args.This ());
6566}
6667
6768Handle<Value> GitRepo::Open (const Arguments& args) {
6869 HandleScope scope;
6970
70- GitRepo *repo = ObjectWrap::Unwrap<GitRepo>(args.This ());
71- Local<Function> callback;
72-
7371 if (args.Length () == 0 || !args[0 ]->IsString ()) {
7472 return ThrowException (Exception::Error (String::New (" Path is required and must be a String." )));
7573 }
@@ -78,52 +76,64 @@ Handle<Value> GitRepo::Open(const Arguments& args) {
7876 return ThrowException (Exception::Error (String::New (" Callback is required and must be a Function." )));
7977 }
8078
81- callback = Local<Function>::Cast (args[1 ]);
82-
83- open_request *ar = new open_request ();
84- ar->repo = repo;
85-
79+ OpenBaton *baton = new OpenBaton ();
80+ baton->request .data = baton;
81+ baton->error = NULL ;
8682 String::Utf8Value path (args[0 ]);
87- ar->path = *path;
83+ baton->path = *path;
84+ baton->callback = Persistent<Function>::New (Local<Function>::Cast (args[1 ]));
8885
89- ar->callback = Persistent<Function>::New (callback);
90-
91- repo->Ref ();
86+ uv_queue_work (uv_default_loop (), &baton->request , OpenWork, OpenAfterWork);
9287
93- uv_work_t *req = new uv_work_t ;
94- req->data = ar;
95- uv_queue_work (uv_default_loop (), req, EIO_Open, EIO_AfterOpen);
96-
97- return scope.Close ( Undefined () );
88+ return Undefined ();
9889}
9990
100- void GitRepo::EIO_Open (uv_work_t *req) {
101- open_request *ar = static_cast <open_request *>(req->data );
102-
103- ar->err = ar->repo ->Open (ar->path .c_str ());
91+ void GitRepo::OpenWork (uv_work_t *req) {
92+ OpenBaton *baton = static_cast <OpenBaton *>(req->data );
10493
94+ int returnCode = git_repository_open (&baton->repo , baton->path .c_str ());
95+ if (returnCode != GIT_OK ) {
96+ baton->error = giterr_last ();
97+ }
10598}
10699
107- void GitRepo::EIO_AfterOpen (uv_work_t *req) {
100+ void GitRepo::OpenAfterWork (uv_work_t *req) {
108101 HandleScope scope;
109102
110- open_request *ar = static_cast <open_request *>(req->data );
103+ OpenBaton *baton = static_cast <OpenBaton *>(req->data );
111104 delete req;
112- ar->repo ->Unref ();
113105
114- Local<Value> argv[1 ];
115- argv[0 ] = Integer::New (ar->err );
106+ if (baton->error ) {
107+ Local<Value> argv[1 ] = {
108+ GitError::WrapError (baton->error )
109+ };
116110
117- TryCatch try_catch;
111+ TryCatch try_catch;
118112
119- ar ->callback ->Call (Context::GetCurrent ()->Global (), 1 , argv);
113+ baton ->callback ->Call (Context::GetCurrent ()->Global (), 1 , argv);
120114
121- if (try_catch.HasCaught ())
122- FatalException (try_catch);
115+ if (try_catch.HasCaught ()) {
116+ node::FatalException (try_catch);
117+ }
118+ } else {
123119
124- ar->callback .Dispose ();
120+ Local<Object> repository = GitRepo::constructor_template->NewInstance ();
121+ GitRepo *repositoryInstance = ObjectWrap::Unwrap<GitRepo>(repository);
122+ repositoryInstance->SetValue (baton->repo );
125123
126- delete ar;
124+ Handle<Value> argv[2 ] = {
125+ Local<Value>::New (Null ()),
126+ repository
127+ };
128+
129+ TryCatch try_catch;
130+
131+ baton->callback ->Call (Context::GetCurrent ()->Global (), 2 , argv);
132+
133+ if (try_catch.HasCaught ()) {
134+ node::FatalException (try_catch);
135+ }
136+ }
127137}
128138
129139Handle<Value> GitRepo::Lookup (const Arguments& args) {
0 commit comments