@@ -9,6 +9,7 @@ Copyright (c) 2011, Tim Branyen @tbranyen <tim@tabdeveloper.com>
99#include < git2.h>
1010
1111#include " reference.h"
12+ #include " obj.h"
1213#include " repo.h"
1314#include " commit.h"
1415
@@ -25,6 +26,7 @@ void Repo::Initialize(Handle<Object> target) {
2526 constructor_template->SetClassName (String::NewSymbol (" Repo" ));
2627
2728 NODE_SET_PROTOTYPE_METHOD (constructor_template, " open" , Open);
29+ NODE_SET_PROTOTYPE_METHOD (constructor_template, " lookup" , Lookup);
2830 NODE_SET_PROTOTYPE_METHOD (constructor_template, " free" , Free);
2931 NODE_SET_PROTOTYPE_METHOD (constructor_template, " init" , Init);
3032 NODE_SET_PROTOTYPE_METHOD (constructor_template, " lookupRef" , LookupRef);
@@ -137,6 +139,85 @@ int Repo::EIO_AfterOpen(eio_req *req) {
137139 return 0 ;
138140}
139141
142+ Handle<Value> Repo::Lookup (const Arguments& args) {
143+ Repo *repo = ObjectWrap::Unwrap<Repo>(args.This ());
144+ Local<Function> callback;
145+
146+ HandleScope scope;
147+
148+ if (args.Length () == 0 || !args[0 ]->IsObject ()) {
149+ return ThrowException (Exception::Error (String::New (" Git object is required and must be a Object." )));
150+ }
151+
152+ if (args.Length () == 1 || !args[1 ]->IsObject ()) {
153+ return ThrowException (Exception::Error (String::New (" Repo is required and must be a Object." )));
154+ }
155+
156+ if (args.Length () == 2 || !args[2 ]->IsObject ()) {
157+ return ThrowException (Exception::Error (String::New (" Oid is required and must be a Object." )));
158+ }
159+
160+ if (args.Length () == 3 || !args[3 ]->IsFunction ()) {
161+ return ThrowException (Exception::Error (String::New (" Callback is required and must be a Function." )));
162+ }
163+
164+ callback = Local<Function>::Cast (args[3 ]);
165+
166+ lookup_request *ar = new lookup_request ();
167+ ar->repo = repo;
168+ ar->callback = Persistent<Function>::New (callback);
169+
170+ repo->Ref ();
171+
172+ eio_custom (EIO_LookupRef, EIO_PRI_DEFAULT , EIO_AfterLookupRef, ar);
173+ ev_ref (EV_DEFAULT_UC );
174+
175+ return Undefined ();
176+ }
177+
178+ int Repo::EIO_Lookup (eio_req *req) {
179+ lookupref_request *ar = static_cast <lookupref_request *>(req->data );
180+
181+ String::Utf8Value name (ar->name );
182+ git_reference *ref;
183+
184+ int err = ar->repo ->LookupRef ((git_reference **)ref, *name);
185+ ar->err = Persistent<Value>::New (Integer::New (err));
186+
187+ if (Int32::Cast (*ar->err )->Value () == 0 ) {
188+ ar->ref ->SetValue (*&ref);
189+ }
190+
191+ return 0 ;
192+ }
193+
194+ int Repo::EIO_AfterLookup (eio_req *req) {
195+ HandleScope scope;
196+
197+ lookupref_request *ar = static_cast <lookupref_request *>(req->data );
198+ ev_unref (EV_DEFAULT_UC );
199+ ar->repo ->Unref ();
200+
201+ Local<Value> argv[2 ];
202+ argv[0 ] = Number::Cast (*ar->err );
203+ argv[1 ] = String::Cast (*ar->name );
204+
205+ TryCatch try_catch;
206+
207+ ar->callback ->Call (Context::GetCurrent ()->Global (), 2 , argv);
208+
209+ if (try_catch.HasCaught ())
210+ FatalException (try_catch);
211+
212+ ar->err .Dispose ();
213+ ar->name .Dispose ();
214+ ar->callback .Dispose ();
215+
216+ delete ar;
217+
218+ return 0 ;
219+ }
220+
140221Handle<Value> Repo::Free (const Arguments& args) {
141222 Repo *repo = ObjectWrap::Unwrap<Repo>(args.This ());
142223
@@ -280,7 +361,6 @@ int Repo::EIO_AfterLookupRef(eio_req *req) {
280361 Local<Value> argv[2 ];
281362 argv[0 ] = Number::Cast (*ar->err );
282363 argv[1 ] = String::Cast (*ar->name );
283- // argv[2] = ref->Wrap(*ar->ref);
284364
285365 TryCatch try_catch;
286366
0 commit comments