Skip to content

Commit 578c7ad

Browse files
committed
Added tree entry Oid methods
1 parent 3467803 commit 578c7ad

1 file changed

Lines changed: 33 additions & 10 deletions

File tree

src/tree_entry.cc

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void GitTreeEntry::Initialize(Handle<v8::Object> target) {
3232
NODE_SET_PROTOTYPE_METHOD(tpl, "name", Name);
3333
NODE_SET_PROTOTYPE_METHOD(tpl, "root", Root);
3434
NODE_SET_PROTOTYPE_METHOD(tpl, "fileMode", FileMode);
35-
NODE_SET_PROTOTYPE_METHOD(tpl, "id", Id);
35+
NODE_SET_PROTOTYPE_METHOD(tpl, "oid", Oid);
3636
NODE_SET_PROTOTYPE_METHOD(tpl, "toBlob", ToBlob);
3737

3838
constructor_template = Persistent<Function>::New(tpl->GetFunction());
@@ -147,23 +147,46 @@ void GitTreeEntry::FileModeAfterWork(uv_work_t* req) {
147147

148148
}
149149

150-
Handle<Value> GitTreeEntry::Id(const Arguments& args) {
151-
// HandleScope scope;
150+
Handle<Value> GitTreeEntry::Oid(const Arguments& args) {
151+
HandleScope scope;
152152

153-
// GitTreeEntry *entry = ObjectWrap::Unwrap<GitTreeEntry>(args.This());
153+
if(args.Length() == 0 || !args[0]->IsFunction()) {
154+
return ThrowException(Exception::Error(String::New("Callback is required and must be a Function.")));
155+
}
154156

155-
// Handle<Object> oid = GitOid::constructor_template->NewInstance();
156-
// GitOid* oidInstance = ObjectWrap::Unwrap<GitOid>(oid);
157-
// oidInstance->SetValue(*const_cast<git_oid *>(git_tree_entry_id(entry->entry)));
157+
OidBaton* baton = new OidBaton;
158+
baton->request.data = baton;
159+
baton->rawEntry = ObjectWrap::Unwrap<GitTreeEntry>(args.This())->GetValue();
160+
baton->callback = Persistent<Function>::New(Local<Function>::Cast(args[0]));
161+
162+
uv_queue_work(uv_default_loop(), &baton->request, OidWork, (uv_after_work_cb)OidAfterWork);
158163

159-
// return oid;
160164
return Undefined();
161165
}
162-
void GitTreeEntry::IdWork(uv_work_t* req) {
166+
void GitTreeEntry::OidWork(uv_work_t* req) {
167+
OidBaton *baton = static_cast<OidBaton *>(req->data);
163168

169+
baton->rawOid = git_tree_entry_id(const_cast<git_tree_entry *>(baton->rawEntry));
164170
}
165-
void GitTreeEntry::IdAfterWork(uv_work_t* req) {
171+
void GitTreeEntry::OidAfterWork(uv_work_t* req) {
172+
HandleScope scope;
173+
OidBaton *baton = static_cast<OidBaton *>(req->data);
166174

175+
Handle<Object> oid = GitOid::constructor_template->NewInstance();
176+
GitOid* oidInstance = ObjectWrap::Unwrap<GitOid>(oid);
177+
oidInstance->SetValue(*const_cast<git_oid *>(baton->rawOid));
178+
179+
Handle<Value> argv[2] = {
180+
Local<Value>::New(Null()),
181+
oid
182+
};
183+
184+
TryCatch try_catch;
185+
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
186+
if (try_catch.HasCaught()) {
187+
node::FatalException(try_catch);
188+
}
189+
delete req;
167190
}
168191

169192
Handle<Value> GitTreeEntry::ToBlob(const Arguments& args) {

0 commit comments

Comments
 (0)