Skip to content

Commit b4d9783

Browse files
committed
Converted tree entry by name to entry by path
1 parent 48bd48b commit b4d9783

File tree

2 files changed

+74
-67
lines changed

2 files changed

+74
-67
lines changed

include/tree.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class GitTree : public ObjectWrap {
2525
/**
2626
* v8::FunctionTemplate used to create Node.js constructor
2727
*/
28-
static Persistent<FunctionTemplate> constructor_template;
28+
static Persistent<Function> constructor_template;
2929

3030
/**
3131
* Used to intialize the EventEmitter from Node.js
@@ -69,8 +69,6 @@ class GitTree : public ObjectWrap {
6969
*/
7070
git_tree_entry* EntryByIndex(int idx);
7171

72-
git_tree_entry* EntryByName(const char* name);
73-
7472
int SortEntries();
7573
void ClearEntries();
7674

@@ -99,9 +97,9 @@ class GitTree : public ObjectWrap {
9997
static Handle<Value> EntryByIndex(const Arguments& args);
10098
static void EIO_EntryByIndex(uv_work_t *req);
10199
static void EIO_AfterEntryByIndex(uv_work_t *req);
102-
static Handle<Value> EntryByName(const Arguments& args);
103-
static void EIO_EntryByName(uv_work_t *req);
104-
static void EIO_AfterEntryByName(uv_work_t *req);
100+
static Handle<Value> EntryByPath(const Arguments& args);
101+
static void EntryByPathWork(uv_work_t *req);
102+
static void EntryByPathAfterWork(uv_work_t *req);
105103
static Handle<Value> SortEntries(const Arguments& args);
106104
static Handle<Value> ClearEntries(const Arguments& args);
107105

@@ -129,13 +127,15 @@ class GitTree : public ObjectWrap {
129127
int idx;
130128
Persistent<Function> callback;
131129
};
132-
/**
133-
* Structure to handle async entryByName
134-
*/
135-
struct entryname_request {
136-
GitTree* tree;
137-
GitTreeEntry* entry;
138-
std::string name;
130+
131+
struct EntryByPathBaton {
132+
uv_work_t request;
133+
const git_error* error;
134+
135+
git_tree* rawTree;
136+
std::string path;
137+
git_tree_entry* rawEntry;
138+
139139
Persistent<Function> callback;
140140
};
141141
};

src/tree.cc

Lines changed: 61 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1518
using namespace v8;
1619
using namespace node;
1720

1821
void 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

3639
git_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-
6059
int 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

284291
Handle<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

Comments
 (0)