@@ -11,26 +11,29 @@ Copyright (c) 2011, Tim Branyen @tbranyen <tim@tabdeveloper.com>
1111#include " ../include/oid.h"
1212#include " ../include/tree.h"
1313#include " ../include/tree_entry.h"
14+ #include " ../include/error.h"
15+
16+ #include " ../include/functions/string.h"
1417
1518using namespace v8 ;
1619using namespace node ;
1720
1821void GitTree::Initialize (Handle<v8::Object> target) {
1922 HandleScope scope;
2023
21- Local<FunctionTemplate> t = FunctionTemplate::New (New);
24+ Local<FunctionTemplate> tpl = FunctionTemplate::New (New);
2225
23- constructor_template = Persistent<FunctionTemplate>::New (t);
24- constructor_template->InstanceTemplate ()->SetInternalFieldCount (1 );
25- constructor_template->SetClassName (String::NewSymbol (" Tree" ));
26+ tpl->InstanceTemplate ()->SetInternalFieldCount (1 );
27+ tpl->SetClassName (String::NewSymbol (" Tree" ));
2628
27- NODE_SET_PROTOTYPE_METHOD (constructor_template , " lookup" , Lookup);
28- NODE_SET_PROTOTYPE_METHOD (constructor_template , " entryCount" , EntryCount);
29- NODE_SET_PROTOTYPE_METHOD (constructor_template , " entryByIndex" , EntryByIndex);
30- NODE_SET_PROTOTYPE_METHOD (constructor_template , " entryByName " , EntryByName );
31- NODE_SET_PROTOTYPE_METHOD (constructor_template , " sortEntries" , EntryCount);
29+ NODE_SET_PROTOTYPE_METHOD (tpl , " lookup" , Lookup);
30+ NODE_SET_PROTOTYPE_METHOD (tpl , " entryCount" , EntryCount);
31+ NODE_SET_PROTOTYPE_METHOD (tpl , " entryByIndex" , EntryByIndex);
32+ NODE_SET_PROTOTYPE_METHOD (tpl , " entryByPath " , EntryByPath );
33+ NODE_SET_PROTOTYPE_METHOD (tpl , " sortEntries" , EntryCount);
3234
33- target->Set (String::NewSymbol (" Tree" ), constructor_template->GetFunction ());
35+ constructor_template = Persistent<Function>::New (tpl->GetFunction ());
36+ target->Set (String::NewSymbol (" Tree" ), constructor_template);
3437}
3538
3639git_tree* GitTree::GetValue () {
@@ -53,10 +56,6 @@ git_tree_entry* GitTree::EntryByIndex(int idx) {
5356 return const_cast <git_tree_entry*>(git_tree_entry_byindex (this ->tree , idx));
5457}
5558
56- git_tree_entry* GitTree::EntryByName (const char * name) {
57- return const_cast <git_tree_entry*>(git_tree_entry_byname (this ->tree , name));
58- }
59-
6059int GitTree::SortEntries () {
6160 // return git_tree_sort_entries(this->tree);
6261 return 0 ;
@@ -216,69 +215,77 @@ void GitTree::EIO_AfterEntryByIndex(uv_work_t *req) {
216215 delete er;
217216}
218217
219- Handle<Value> GitTree::EntryByName (const Arguments& args) {
218+ Handle<Value> GitTree::EntryByPath (const Arguments& args) {
220219 HandleScope scope;
221220
222- GitTree *tree = ObjectWrap::Unwrap<GitTree>(args.This ());
223-
224- Local<Function> callback;
225-
226- if (args.Length () == 0 || !args[0 ]->IsObject ()) {
227- return ThrowException (Exception::Error (String::New (" TreeEntry is required and must be a Object." )));
228- }
229-
230- if (args.Length () == 1 || !args[1 ]->IsString ()) {
221+ if (args.Length () == 0 || !args[0 ]->IsString ()) {
231222 return ThrowException (Exception::Error (String::New (" Name is required and must be a String." )));
232223 }
233224
234- if (args.Length () == 2 || !args[2 ]->IsFunction ()) {
225+ if (args.Length () == 1 || !args[1 ]->IsFunction ()) {
235226 return ThrowException (Exception::Error (String::New (" Callback is required and must be a Function." )));
236227 }
237228
238- callback = Local<Function>::Cast (args[2 ]);
239- String::Utf8Value name (args[1 ]->ToString ());
240-
241- entryname_request *er = new entryname_request ();
242- er->tree = tree;
243- er->entry = ObjectWrap::Unwrap<GitTreeEntry>(args[0 ]->ToObject ());
244- er->name = *name;
245- er->callback = Persistent<Function>::New (callback);
229+ EntryByPathBaton *baton = new EntryByPathBaton;
230+ baton->request .data = baton;
231+ baton->error = NULL ;
232+ baton->rawEntry = NULL ;
246233
234+ GitTree *tree = ObjectWrap::Unwrap<GitTree>(args.This ());
247235 tree->Ref ();
248236
249- uv_work_t *req = new uv_work_t ;
250- req-> data = er ;
251- uv_queue_work ( uv_default_loop (), req, EIO_EntryByName, (uv_after_work_cb)EIO_AfterEntryByName );
237+ baton-> rawTree = tree-> GetValue () ;
238+ baton-> path = stringArgToString (args[ 0 ]-> ToString ()) ;
239+ baton-> callback = Persistent<Function>:: New (Local<Function>:: Cast (args[ 1 ]) );
252240
253- return scope.Close ( Undefined () );
254- }
241+ uv_queue_work (uv_default_loop (), &baton->request , EntryByPathWork, (uv_after_work_cb)EntryByPathAfterWork);
255242
256- void GitTree::EIO_EntryByName ( uv_work_t *req) {
257- entryname_request *er = static_cast <entryname_request *>(req-> data );
243+ return Undefined ();
244+ }
258245
259- er->entry ->SetValue (er->tree ->EntryByName (er->name .c_str ()));
246+ void GitTree::EntryByPathWork (uv_work_t *req) {
247+ EntryByPathBaton *baton = static_cast <EntryByPathBaton *>(req->data );
260248
249+ int returnCode = git_tree_entry_bypath (&baton->rawEntry , baton->rawTree , baton->path .c_str ());
250+ if (returnCode != GIT_OK) {
251+ baton->error = giterr_last ();
252+ }
261253}
262254
263- void GitTree::EIO_AfterEntryByName (uv_work_t *req) {
264- entryname_request *er = static_cast <entryname_request *>(req->data );
255+ void GitTree::EntryByPathAfterWork (uv_work_t *req) {
256+ HandleScope scope;
257+ EntryByPathBaton *baton = static_cast <EntryByPathBaton *>(req->data );
265258
266- delete req;
267- er->tree ->Unref ();
259+ if (baton->error ) {
260+ Local<Value> argv[1 ] = {
261+ GitError::WrapError (baton->error )
262+ };
268263
269- Handle<Value> argv[1 ];
270- argv[0 ] = Boolean::New (er->entry ->GetValue () != NULL );
264+ TryCatch try_catch;
271265
272- TryCatch try_catch ;
266+ baton-> callback -> Call ( Context::GetCurrent ()-> Global (), 1 , argv) ;
273267
274- er->callback ->Call (Context::GetCurrent ()->Global (), 1 , argv);
268+ if (try_catch.HasCaught ()) {
269+ node::FatalException (try_catch);
270+ }
271+ } else {
275272
276- if (try_catch.HasCaught ())
277- FatalException (try_catch);
273+ Local<Object> entry = GitTreeEntry::constructor_template->NewInstance ();
274+ GitTreeEntry *entryInstance = ObjectWrap::Unwrap<GitTreeEntry>(entry);
275+ entryInstance->SetValue (baton->rawEntry );
278276
279- er->callback .Dispose ();
277+ Handle<Value> argv[2 ] = {
278+ Local<Value>::New (Null ()),
279+ entry
280+ };
280281
281- delete er;
282+ TryCatch try_catch;
283+ baton->callback ->Call (Context::GetCurrent ()->Global (), 2 , argv);
284+ if (try_catch.HasCaught ()) {
285+ node::FatalException (try_catch);
286+ }
287+ }
288+ delete req;
282289}
283290
284291Handle<Value> GitTree::SortEntries (const Arguments& args) {
@@ -300,4 +307,4 @@ Handle<Value> GitTree::ClearEntries(const Arguments& args) {
300307
301308 return scope.Close ( Undefined () );
302309}
303- Persistent<FunctionTemplate > GitTree::constructor_template;
310+ Persistent<Function > GitTree::constructor_template;
0 commit comments