@@ -9,6 +9,8 @@ Copyright (c) 2011, Tim Branyen @tbranyen <tim@tabdeveloper.com>
99#include < git2.h>
1010
1111#include " repo.h"
12+ #include " ref.h"
13+ #include " commit.h"
1214
1315using namespace v8 ;
1416using namespace node ;
@@ -25,6 +27,7 @@ void Repo::Initialize(Handle<Object> target) {
2527 NODE_SET_PROTOTYPE_METHOD (constructor_template, " open" , Open);
2628 NODE_SET_PROTOTYPE_METHOD (constructor_template, " free" , Free);
2729 NODE_SET_PROTOTYPE_METHOD (constructor_template, " init" , Init);
30+ NODE_SET_PROTOTYPE_METHOD (constructor_template, " lookup_ref" , LookupRef);
2831
2932 target->Set (String::NewSymbol (" Repo" ), constructor_template->GetFunction ());
3033}
@@ -52,6 +55,12 @@ int Repo::Init(const char* path, bool is_bare) {
5255 return err;
5356}
5457
58+ int Repo::LookupRef (git_reference* ref, const char * name) {
59+ int err = git_repository_lookup_ref (&ref, this ->repo , name);
60+
61+ return err;
62+ }
63+
5564Handle<Value> Repo::New (const Arguments& args) {
5665 HandleScope scope;
5766
@@ -209,4 +218,80 @@ int Repo::EIO_AfterInit(eio_req *req) {
209218 return 0 ;
210219}
211220
221+ Handle<Value> Repo::LookupRef (const Arguments& args) {
222+ Repo *repo = ObjectWrap::Unwrap<Repo>(args.This ());
223+ Local<Function> callback;
224+
225+ HandleScope scope;
226+
227+ if (args.Length () == 0 || !args[0 ]->IsObject ()) {
228+ return ThrowException (Exception::Error (String::New (" Ref is required and must be an Object." )));
229+ }
230+
231+ if (args.Length () == 1 || !args[1 ]->IsString ()) {
232+ return ThrowException (Exception::Error (String::New (" Name is required and must be a String." )));
233+ }
234+
235+ if (args.Length () == 2 || !args[2 ]->IsFunction ()) {
236+ return ThrowException (Exception::Error (String::New (" Callback is required and must be a Function." )));
237+ }
238+
239+ callback = Local<Function>::Cast (args[2 ]);
240+
241+ lookupref_request *ar = new lookupref_request ();
242+ ar->repo = repo;
243+ // git_reference* ref;
244+ // ar->ref = ObjectWrap::Unwrap<Ref>(args[0]->ToObject());
245+ ar->name = Persistent<Value>::New (args[1 ]);
246+ ar->callback = Persistent<Function>::New (callback);
247+
248+ repo->Ref ();
249+
250+ eio_custom (EIO_LookupRef, EIO_PRI_DEFAULT , EIO_AfterLookupRef, ar);
251+ ev_ref (EV_DEFAULT_UC );
252+
253+ return Undefined ();
254+ }
255+
256+ int Repo::EIO_LookupRef (eio_req *req) {
257+ lookupref_request *ar = static_cast <lookupref_request *>(req->data );
258+
259+ String::Utf8Value name (ar->name );
260+ ar->err = Persistent<Value>::New (Integer::New (ar->repo ->LookupRef (ar->ref ->GetValue (), *name)));
261+
262+ // if(Int32::Cast(*ar->err)->Value() == 0) {
263+ // //ar->ref->SetValue(ref);
264+ // }
265+
266+ return 0 ;
267+ }
268+
269+ int Repo::EIO_AfterLookupRef (eio_req *req) {
270+ HandleScope scope;
271+
272+ lookupref_request *ar = static_cast <lookupref_request *>(req->data );
273+ ev_unref (EV_DEFAULT_UC );
274+ ar->repo ->Unref ();
275+
276+ Local<Value> argv[2 ];
277+ argv[0 ] = Number::Cast (*ar->err );
278+ argv[1 ] = String::Cast (*ar->name );
279+ // argv[2] = ref->Wrap(*ar->ref);
280+
281+ TryCatch try_catch;
282+
283+ ar->callback ->Call (Context::GetCurrent ()->Global (), 2 , argv);
284+
285+ if (try_catch.HasCaught ())
286+ FatalException (try_catch);
287+
288+ ar->err .Dispose ();
289+ ar->name .Dispose ();
290+ ar->callback .Dispose ();
291+
292+ delete ar;
293+
294+ return 0 ;
295+ }
296+
212297Persistent<FunctionTemplate> Repo::constructor_template;
0 commit comments