forked from mgmorcos/nodegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree_builder.cc
More file actions
353 lines (299 loc) · 9.62 KB
/
Copy pathtree_builder.cc
File metadata and controls
353 lines (299 loc) · 9.62 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/**
* This code is auto-generated; unless you know what you're doing, do not modify!
**/
#include <v8.h>
#include <node.h>
#include <string.h>
#include "git2.h"
#include "../include/functions/copy.h"
#include "../include/tree_builder.h"
#include "../include/repo.h"
#include "../include/oid.h"
#include "../include/tree_entry.h"
#include "../include/tree.h"
#include "../include/diff_list.h"
#include "../include/diff_options.h"
#include "../include/index.h"
using namespace v8;
using namespace node;
GitTreeBuilder::GitTreeBuilder(git_treebuilder *raw) {
this->raw = raw;
}
GitTreeBuilder::~GitTreeBuilder() {
git_treebuilder_free(this->raw);
}
void GitTreeBuilder::Initialize(Handle<v8::Object> target) {
HandleScope scope;
Local<FunctionTemplate> tpl = FunctionTemplate::New(New);
tpl->InstanceTemplate()->SetInternalFieldCount(1);
tpl->SetClassName(String::NewSymbol("TreeBuilder"));
NODE_SET_METHOD(tpl, "create", Create);
NODE_SET_PROTOTYPE_METHOD(tpl, "clear", Clear);
NODE_SET_METHOD(tpl, "size", Size);
NODE_SET_PROTOTYPE_METHOD(tpl, "get", Get);
NODE_SET_PROTOTYPE_METHOD(tpl, "insert", Insert);
NODE_SET_PROTOTYPE_METHOD(tpl, "gitTreebuilderRemove", GitTreebuilderRemove);
NODE_SET_PROTOTYPE_METHOD(tpl, "write", Write);
constructor_template = Persistent<Function>::New(tpl->GetFunction());
target->Set(String::NewSymbol("TreeBuilder"), constructor_template);
}
Handle<Value> GitTreeBuilder::New(const Arguments& args) {
HandleScope scope;
if (args.Length() == 0 || !args[0]->IsExternal()) {
return ThrowException(Exception::Error(String::New("git_treebuilder is required.")));
}
GitTreeBuilder* object = new GitTreeBuilder((git_treebuilder *) External::Unwrap(args[0]));
object->Wrap(args.This());
return scope.Close(args.This());
}
Handle<Value> GitTreeBuilder::New(void *raw) {
HandleScope scope;
Handle<Value> argv[1] = { External::New((void *)raw) };
return scope.Close(GitTreeBuilder::constructor_template->NewInstance(1, argv));
}
git_treebuilder *GitTreeBuilder::GetValue() {
return this->raw;
}
/**
* @param {Tree} source
* @return {TreeBuilder} out
*/
Handle<Value> GitTreeBuilder::Create(const Arguments& args) {
HandleScope scope;
git_treebuilder * out = 0;
const git_tree * from_source;
if (args[0]->IsObject()) {
from_source = ObjectWrap::Unwrap<GitTree>(args[0]->ToObject())->GetValue();
} else {
from_source = 0;
}
int result = git_treebuilder_create(
&out
, from_source
);
if (result != GIT_OK) {
if (giterr_last()) {
return ThrowException(Exception::Error(String::New(giterr_last()->message)));
} else {
return ThrowException(Exception::Error(String::New("Unkown Error")));
}
}
Handle<Value> to;
if (out != NULL) {
to = GitTreeBuilder::New((void *)out);
} else {
to = Null();
}
return scope.Close(to);
}
/**
* @param {TreeBuilder} bld
*/
Handle<Value> GitTreeBuilder::Clear(const Arguments& args) {
HandleScope scope;
if (args.Length() == 0 || !args[0]->IsObject()) {
return ThrowException(Exception::Error(String::New("TreeBuilder bld is required.")));
}
git_treebuilder * from_bld;
from_bld = ObjectWrap::Unwrap<GitTreeBuilder>(args[0]->ToObject())->GetValue();
git_treebuilder_clear(
from_bld
);
return Undefined();
}
/**
* @return {Number} result
*/
Handle<Value> GitTreeBuilder::Size(const Arguments& args) {
HandleScope scope;
unsigned int result = git_treebuilder_entrycount(
ObjectWrap::Unwrap<GitTreeBuilder>(args.This())->GetValue()
);
Handle<Value> to;
to = Uint32::New(result);
return scope.Close(to);
}
/**
* @param {String} filename
* @return {TreeEntry} result
*/
Handle<Value> GitTreeBuilder::Get(const Arguments& args) {
HandleScope scope;
if (args.Length() == 0 || !args[0]->IsString()) {
return ThrowException(Exception::Error(String::New("String filename is required.")));
}
const char * from_filename;
String::Utf8Value filename(args[0]->ToString());
from_filename = strdup(*filename);
const git_tree_entry * result = git_treebuilder_get(
ObjectWrap::Unwrap<GitTreeBuilder>(args.This())->GetValue()
, from_filename
);
free((void *)from_filename);
Handle<Value> to;
if (result != NULL) {
result = (const git_tree_entry * )git_tree_entry_dup(result);
}
if (result != NULL) {
to = GitTreeEntry::New((void *)result);
} else {
to = Null();
}
return scope.Close(to);
}
/**
* @param {String} filename
* @param {Oid} id
* @param {Number} filemode
* @return {TreeEntry} out
*/
Handle<Value> GitTreeBuilder::Insert(const Arguments& args) {
HandleScope scope;
if (args.Length() == 0 || !args[0]->IsString()) {
return ThrowException(Exception::Error(String::New("String filename is required.")));
}
if (args.Length() == 1 || !args[1]->IsObject()) {
return ThrowException(Exception::Error(String::New("Oid id is required.")));
}
if (args.Length() == 2 || !args[2]->IsNumber()) {
return ThrowException(Exception::Error(String::New("Number filemode is required.")));
}
const git_tree_entry * out = 0;
const char * from_filename;
String::Utf8Value filename(args[0]->ToString());
from_filename = strdup(*filename);
const git_oid * from_id;
from_id = ObjectWrap::Unwrap<GitOid>(args[1]->ToObject())->GetValue();
git_filemode_t from_filemode;
from_filemode = (git_filemode_t) args[2]->ToNumber()->Value();
int result = git_treebuilder_insert(
&out
, ObjectWrap::Unwrap<GitTreeBuilder>(args.This())->GetValue()
, from_filename
, from_id
, from_filemode
);
free((void *)from_filename);
if (result != GIT_OK) {
if (giterr_last()) {
return ThrowException(Exception::Error(String::New(giterr_last()->message)));
} else {
return ThrowException(Exception::Error(String::New("Unkown Error")));
}
}
Handle<Value> to;
if (out != NULL) {
out = (const git_tree_entry * )git_tree_entry_dup(out);
}
if (out != NULL) {
to = GitTreeEntry::New((void *)out);
} else {
to = Null();
}
return scope.Close(to);
}
/**
* @param {String} filename
*/
Handle<Value> GitTreeBuilder::GitTreebuilderRemove(const Arguments& args) {
HandleScope scope;
if (args.Length() == 0 || !args[0]->IsString()) {
return ThrowException(Exception::Error(String::New("String filename is required.")));
}
const char * from_filename;
String::Utf8Value filename(args[0]->ToString());
from_filename = strdup(*filename);
int result = git_treebuilder_remove(
ObjectWrap::Unwrap<GitTreeBuilder>(args.This())->GetValue()
, from_filename
);
free((void *)from_filename);
if (result != GIT_OK) {
if (giterr_last()) {
return ThrowException(Exception::Error(String::New(giterr_last()->message)));
} else {
return ThrowException(Exception::Error(String::New("Unkown Error")));
}
}
return Undefined();
}
#include "../include/functions/copy.h"
/**
* @param {Repository} repo
* @param {Oid} callback
*/
Handle<Value> GitTreeBuilder::Write(const Arguments& args) {
HandleScope scope;
if (args.Length() == 0 || !args[0]->IsObject()) {
return ThrowException(Exception::Error(String::New("Repository repo is required.")));
}
if (args.Length() == 1 || !args[1]->IsFunction()) {
return ThrowException(Exception::Error(String::New("Callback is required and must be a Function.")));
}
WriteBaton* baton = new WriteBaton;
baton->error_code = GIT_OK;
baton->error = NULL;
baton->request.data = baton;
baton->id = (git_oid *)malloc(sizeof(git_oid ));
baton->repoReference = Persistent<Value>::New(args[0]);
git_repository * from_repo;
from_repo = ObjectWrap::Unwrap<GitRepo>(args[0]->ToObject())->GetValue();
baton->repo = from_repo;
baton->bldReference = Persistent<Value>::New(args.This());
baton->bld = ObjectWrap::Unwrap<GitTreeBuilder>(args.This())->GetValue();
baton->callback = Persistent<Function>::New(Local<Function>::Cast(args[1]));
uv_queue_work(uv_default_loop(), &baton->request, WriteWork, (uv_after_work_cb)WriteAfterWork);
return Undefined();
}
void GitTreeBuilder::WriteWork(uv_work_t *req) {
WriteBaton *baton = static_cast<WriteBaton *>(req->data);
int result = git_treebuilder_write(
baton->id,
baton->repo,
baton->bld
);
baton->error_code = result;
if (result != GIT_OK && giterr_last() != NULL) {
baton->error = git_error_dup(giterr_last());
}
}
void GitTreeBuilder::WriteAfterWork(uv_work_t *req) {
HandleScope scope;
WriteBaton *baton = static_cast<WriteBaton *>(req->data);
TryCatch try_catch;
if (baton->error_code == GIT_OK) {
Handle<Value> to;
if (baton->id != NULL) {
to = GitOid::New((void *)baton->id);
} else {
to = Null();
}
Handle<Value> result = to;
Handle<Value> argv[2] = {
Local<Value>::New(Null()),
result
};
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
} else {
if (baton->error) {
Handle<Value> argv[1] = {
Exception::Error(String::New(baton->error->message))
};
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
if (baton->error->message)
free((void *)baton->error->message);
free((void *)baton->error);
} else {
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
}
free(baton->id);
}
if (try_catch.HasCaught()) {
node::FatalException(try_catch);
}
baton->repoReference.Dispose();
baton->bldReference.Dispose();
baton->callback.Dispose();
delete baton;
}
Persistent<Function> GitTreeBuilder::constructor_template;