-
Notifications
You must be signed in to change notification settings - Fork 699
Expand file tree
/
Copy pathtree.cc
More file actions
executable file
·303 lines (219 loc) · 8.06 KB
/
tree.cc
File metadata and controls
executable file
·303 lines (219 loc) · 8.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
Copyright (c) 2011, Tim Branyen @tbranyen <[email protected]>
*/
#include <v8.h>
#include <node.h>
#include "../vendor/libgit2/include/git2.h"
#include "../include/repo.h"
#include "../include/oid.h"
#include "../include/tree.h"
#include "../include/tree_entry.h"
using namespace v8;
using namespace node;
void GitTree::Initialize (Handle<v8::Object> target) {
HandleScope scope;
Local<FunctionTemplate> t = FunctionTemplate::New(New);
constructor_template = Persistent<FunctionTemplate>::New(t);
constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
constructor_template->SetClassName(String::NewSymbol("Tree"));
NODE_SET_PROTOTYPE_METHOD(constructor_template, "lookup", Lookup);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "entryCount", EntryCount);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "entryByIndex", EntryByIndex);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "entryByName", EntryByName);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "sortEntries", EntryCount);
target->Set(String::NewSymbol("Tree"), constructor_template->GetFunction());
}
git_tree* GitTree::GetValue() {
return this->tree;
}
void GitTree::SetValue(git_tree* tree) {
this->tree = tree;
}
int GitTree::Lookup(git_repository* repo, const git_oid* id) {
return git_tree_lookup(&this->tree, repo, id);
}
size_t GitTree::EntryCount() {
return git_tree_entrycount(this->tree);
}
git_tree_entry* GitTree::EntryByIndex(int idx) {
return const_cast<git_tree_entry*>(git_tree_entry_byindex(this->tree, idx));
}
git_tree_entry* GitTree::EntryByName(const char* name) {
return const_cast<git_tree_entry*>(git_tree_entry_byname(this->tree, name));
}
int GitTree::SortEntries() {
//return git_tree_sort_entries(this->tree);
return 0;
}
Handle<Value> GitTree::New(const Arguments& args) {
HandleScope scope;
GitTree *tree = new GitTree();
tree->Wrap(args.This());
return scope.Close(args.This());
}
Handle<Value> GitTree::Lookup(const Arguments& args) {
HandleScope scope;
GitTree *tree = ObjectWrap::Unwrap<GitTree>(args.This());
if(args.Length() == 0 || !args[0]->IsObject()) {
return ThrowException(Exception::Error(String::New("Repo is required and must be an Object.")));
}
if(args.Length() == 1 || !args[1]->IsObject()) {
return ThrowException(Exception::Error(String::New("Oid is required and must be an Object.")));
}
GitRepo* repo = ObjectWrap::Unwrap<GitRepo>(args[0]->ToObject());
GitOid* oid = ObjectWrap::Unwrap<GitOid>(args[1]->ToObject());
git_oid ref_oid = oid->GetValue();
int err = tree->Lookup(repo->GetValue(), &ref_oid);
return scope.Close( Integer::New(err) );
// if(args.Length() == 2 || !args[2]->IsFunction()) {
// return ThrowException(Exception::Error(String::New("Callback is required and must be a Function.")));
// }
//
// callback = Local<Function>::Cast(args[2]);
//
// lookup_request *lr = new lookup_request();
// lr->tree = tree;
// lr->repo = ObjectWrap::Unwrap<GitRepo>(args[0]->ToObject());
// lr->oid = ObjectWrap::Unwrap<GitOid>(args[1]->ToObject());
// lr->callback = Persistent<Function>::New(callback);
//
// tree->Ref();
//
// eio_custom(EIO_Lookup, EIO_PRI_DEFAULT, EIO_AfterLookup, lr);
// ev_ref(EV_DEFAULT_UC);
//
// return scope.Close( Undefined() );
}
//void GitTree::EIO_Lookup(uv_work_t *req) {
// lookup_request *lr = static_cast<lookup_request *>(req->data);
//
// git_oid oid = lr->oid->GetValue();
// lr->err = lr->tree->Lookup(lr->repo->GetValue(), &oid);
//
// return 0;
//}
//void GitTree::EIO_AfterLookup(uv_work_t *req) {
// lookup_request *lr = static_cast<lookup_request *>(req->data);
//
// ev_unref(EV_DEFAULT_UC);
// lr->tree->Unref();
//
// Handle<Value> argv[1];
// argv[0] = Integer::New(lr->err);
//
// TryCatch try_catch;
//
// lr->callback->Call(Context::GetCurrent()->Global(), 1, argv);
//
// if(try_catch.HasCaught())
// FatalException(try_catch);
//
// lr->callback.Dispose();
//
// delete lr;
//
// return 0;
//}
Handle<Value> GitTree::EntryCount(const Arguments& args) {
HandleScope scope;
GitTree *tree = ObjectWrap::Unwrap<GitTree>(args.This());
int count = tree->EntryCount();
return scope.Close( Integer::New(count) );
}
Handle<Value> GitTree::EntryByIndex(const Arguments& args) {
HandleScope scope;
GitTree *tree = ObjectWrap::Unwrap<GitTree>(args.This());
Local<Function> callback;
if(args.Length() == 0 || !args[0]->IsObject()) {
return ThrowException(Exception::Error(String::New("TreeEntry is required and must be a Object.")));
}
if(args.Length() == 1 || !args[1]->IsNumber()) {
return ThrowException(Exception::Error(String::New("Index is required and must be a Number.")));
}
if(args.Length() == 2 || !args[2]->IsFunction()) {
return ThrowException(Exception::Error(String::New("Callback is required and must be a Function.")));
}
callback = Local<Function>::Cast(args[2]);
entryindex_request *er = new entryindex_request();
er->tree = tree;
er->entry = ObjectWrap::Unwrap<GitTreeEntry>(args[0]->ToObject());
er->idx = args[1]->ToInteger()->Value();
er->callback = Persistent<Function>::New(callback);
tree->Ref();
uv_work_t *req = new uv_work_t;
req->data = er;
uv_queue_work(uv_default_loop(), req, EIO_EntryByIndex, (uv_after_work_cb)EIO_AfterEntryByIndex);
return scope.Close( Undefined() );
}
void GitTree::EIO_EntryByIndex(uv_work_t *req) {
entryindex_request *er = static_cast<entryindex_request *>(req->data);
er->entry->SetValue(er->tree->EntryByIndex(er->idx));
}
void GitTree::EIO_AfterEntryByIndex(uv_work_t *req) {
entryindex_request *er = static_cast<entryindex_request *>(req->data);
Handle<Value> argv[0];
TryCatch try_catch;
er->callback->Call(Context::GetCurrent()->Global(), 0, argv);
if(try_catch.HasCaught())
FatalException(try_catch);
er->callback.Dispose();
delete req;
er->tree->Unref();
delete er;
}
Handle<Value> GitTree::EntryByName(const Arguments& args) {
HandleScope scope;
GitTree *tree = ObjectWrap::Unwrap<GitTree>(args.This());
Local<Function> callback;
if(args.Length() == 0 || !args[0]->IsObject()) {
return ThrowException(Exception::Error(String::New("TreeEntry is required and must be a Object.")));
}
if(args.Length() == 1 || !args[1]->IsString()) {
return ThrowException(Exception::Error(String::New("Name is required and must be a String.")));
}
if(args.Length() == 2 || !args[2]->IsFunction()) {
return ThrowException(Exception::Error(String::New("Callback is required and must be a Function.")));
}
callback = Local<Function>::Cast(args[2]);
String::Utf8Value name(args[1]->ToString());
entryname_request *er = new entryname_request();
er->tree = tree;
er->entry = ObjectWrap::Unwrap<GitTreeEntry>(args[0]->ToObject());
er->name = *name;
er->callback = Persistent<Function>::New(callback);
tree->Ref();
uv_work_t *req = new uv_work_t;
req->data = er;
uv_queue_work(uv_default_loop(), req, EIO_EntryByName, (uv_after_work_cb)EIO_AfterEntryByName);
return scope.Close( Undefined() );
}
void GitTree::EIO_EntryByName(uv_work_t *req) {
entryname_request *er = static_cast<entryname_request *>(req->data);
er->entry->SetValue(er->tree->EntryByName(er->name.c_str()));
}
void GitTree::EIO_AfterEntryByName(uv_work_t *req) {
entryname_request *er = static_cast<entryname_request *>(req->data);
delete req;
er->tree->Unref();
Handle<Value> argv[1];
argv[0] = Boolean::New(er->entry->GetValue() != NULL);
TryCatch try_catch;
er->callback->Call(Context::GetCurrent()->Global(), 1, argv);
if(try_catch.HasCaught())
FatalException(try_catch);
er->callback.Dispose();
delete er;
}
Handle<Value> GitTree::SortEntries(const Arguments& args) {
HandleScope scope;
GitTree *tree = ObjectWrap::Unwrap<GitTree>(args.This());
int err = tree->SortEntries();
return scope.Close( Integer::New(err) );
}
Handle<Value> GitTree::ClearEntries(const Arguments& args) {
HandleScope scope;
//GitTree *tree = ObjectWrap::Unwrap<GitTree>(args.This());
//tree->ClearEntries();
return scope.Close( Undefined() );
}
Persistent<FunctionTemplate> GitTree::constructor_template;