-
Notifications
You must be signed in to change notification settings - Fork 699
Expand file tree
/
Copy pathreference.cc
More file actions
executable file
·647 lines (552 loc) · 15.5 KB
/
reference.cc
File metadata and controls
executable file
·647 lines (552 loc) · 15.5 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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
/**
* This code is auto-generated; unless you know what you're doing, do not modify!
**/
#include <nan.h>
#include <string.h>
#include "git2.h"
#include "../include/functions/copy.h"
#include "../include/reference.h"
#include "../include/repo.h"
#include "../include/oid.h"
#include "../include/object.h"
using namespace v8;
using namespace node;
GitReference::GitReference(git_reference *raw) {
this->raw = raw;
}
GitReference::~GitReference() {
git_reference_free(this->raw);
}
void GitReference::Initialize(Handle<v8::Object> target) {
NanScope();
Local<FunctionTemplate> tpl = NanNew<FunctionTemplate>(New);
tpl->InstanceTemplate()->SetInternalFieldCount(1);
tpl->SetClassName(NanNew<String>("Reference"));
NODE_SET_METHOD(tpl, "oidForName", OidForName);
NODE_SET_PROTOTYPE_METHOD(tpl, "target", Target);
NODE_SET_PROTOTYPE_METHOD(tpl, "symbolicTarget", SymbolicTarget);
NODE_SET_PROTOTYPE_METHOD(tpl, "type", Type);
NODE_SET_PROTOTYPE_METHOD(tpl, "name", Name);
NODE_SET_PROTOTYPE_METHOD(tpl, "resolve", Resolve);
NODE_SET_PROTOTYPE_METHOD(tpl, "setSymbolicTarget", SetSymbolicTarget);
NODE_SET_PROTOTYPE_METHOD(tpl, "setTarget", setTarget);
NODE_SET_PROTOTYPE_METHOD(tpl, "rename", Rename);
NODE_SET_PROTOTYPE_METHOD(tpl, "delete", Delete);
NODE_SET_PROTOTYPE_METHOD(tpl, "isBranch", IsBranch);
NODE_SET_PROTOTYPE_METHOD(tpl, "isRemote", IsRemote);
NODE_SET_PROTOTYPE_METHOD(tpl, "peel", Peel);
NODE_SET_METHOD(tpl, "isValidName", IsValidName);
Local<Function> _constructor_template = tpl->GetFunction();
NanAssignPersistent(constructor_template, _constructor_template);
target->Set(NanNew<String>("Reference"), _constructor_template);
}
NAN_METHOD(GitReference::New) {
NanScope();
if (args.Length() == 0 || !args[0]->IsExternal()) {
return NanThrowError("git_reference is required.");
}
GitReference* object = new GitReference(static_cast<git_reference *>(Handle<External>::Cast(args[0])->Value()));
object->Wrap(args.This());
NanReturnValue(args.This());
}
Handle<Value> GitReference::New(void *raw) {
NanEscapableScope();
Handle<Value> argv[1] = { NanNew<External>((void *)raw) };
return NanEscapeScope(NanNew<Function>(GitReference::constructor_template)->NewInstance(1, argv));
}
git_reference *GitReference::GetValue() {
return this->raw;
}
#include "../include/functions/copy.h"
/**
* @param {Repository} repo
* @param {String} name
* @param {Oid} callback
*/
NAN_METHOD(GitReference::OidForName) {
NanScope();
if (args.Length() == 0 || !args[0]->IsObject()) {
return NanThrowError("Repository repo is required.");
}
if (args.Length() == 1 || !args[1]->IsString()) {
return NanThrowError("String name is required.");
}
if (args.Length() == 2 || !args[2]->IsFunction()) {
return NanThrowError("Callback is required and must be a Function.");
}
OidForNameBaton* baton = new OidForNameBaton;
baton->error_code = GIT_OK;
baton->error = NULL;
baton->out = (git_oid *)malloc(sizeof(git_oid ));
git_repository * from_repo;
from_repo = ObjectWrap::Unwrap<GitRepo>(args[0]->ToObject())->GetValue();
baton->repo = from_repo;
const char * from_name;
String::Utf8Value name(args[1]->ToString());
from_name = strdup(*name);
baton->name = from_name;
NanCallback *callback = new NanCallback(Local<Function>::Cast(args[2]));
OidForNameWorker *worker = new OidForNameWorker(baton, callback);
if (!args[0]->IsUndefined() && !args[0]->IsNull())
worker->SaveToPersistent("repo", args[0]->ToObject());
if (!args[1]->IsUndefined() && !args[1]->IsNull())
worker->SaveToPersistent("name", args[1]->ToObject());
NanAsyncQueueWorker(worker);
NanReturnUndefined();
}
void GitReference::OidForNameWorker::Execute() {
int result = git_reference_name_to_id(
baton->out,
baton->repo,
baton->name
);
baton->error_code = result;
if (result != GIT_OK && giterr_last() != NULL) {
baton->error = git_error_dup(giterr_last());
}
}
void GitReference::OidForNameWorker::HandleOKCallback() {
TryCatch try_catch;
if (baton->error_code == GIT_OK) {
Handle<Value> to;
if (baton->out != NULL) {
to = GitOid::New((void *)baton->out);
} else {
to = NanNull();
}
Handle<Value> result = to;
Handle<Value> argv[2] = {
NanNull(),
result
};
callback->Call(2, argv);
} else {
if (baton->error) {
Handle<Value> argv[1] = {
NanError(baton->error->message)
};
callback->Call(1, argv);
if (baton->error->message)
free((void *)baton->error->message);
free((void *)baton->error);
} else {
callback->Call(0, NULL);
}
free(baton->out);
}
if (try_catch.HasCaught()) {
node::FatalException(try_catch);
}
free((void *)baton->name);
delete baton;
}
/**
* @return {Oid} result
*/
NAN_METHOD(GitReference::Target) {
NanScope();
const git_oid * result = git_reference_target(
ObjectWrap::Unwrap<GitReference>(args.This())->GetValue()
);
Handle<Value> to;
if (result != NULL) {
result = (const git_oid * )git_oid_dup(result);
}
if (result != NULL) {
to = GitOid::New((void *)result);
} else {
to = NanNull();
}
NanReturnValue(to);
}
/**
* @return {String} result
*/
NAN_METHOD(GitReference::SymbolicTarget) {
NanScope();
const char * result = git_reference_symbolic_target(
ObjectWrap::Unwrap<GitReference>(args.This())->GetValue()
);
Handle<Value> to;
to = NanNew<String>(result);
NanReturnValue(to);
}
/**
* @return {Number} result
*/
NAN_METHOD(GitReference::Type) {
NanScope();
git_ref_t result = git_reference_type(
ObjectWrap::Unwrap<GitReference>(args.This())->GetValue()
);
Handle<Value> to;
to = NanNew<Number>(result);
NanReturnValue(to);
}
/**
* @return {String} result
*/
NAN_METHOD(GitReference::Name) {
NanScope();
const char * result = git_reference_name(
ObjectWrap::Unwrap<GitReference>(args.This())->GetValue()
);
Handle<Value> to;
to = NanNew<String>(result);
NanReturnValue(to);
}
#include "../include/functions/copy.h"
/**
* @param {Reference} callback
*/
NAN_METHOD(GitReference::Resolve) {
NanScope();
if (args.Length() == 0 || !args[0]->IsFunction()) {
return NanThrowError("Callback is required and must be a Function.");
}
ResolveBaton* baton = new ResolveBaton;
baton->error_code = GIT_OK;
baton->error = NULL;
baton->ref = ObjectWrap::Unwrap<GitReference>(args.This())->GetValue();
NanCallback *callback = new NanCallback(Local<Function>::Cast(args[0]));
ResolveWorker *worker = new ResolveWorker(baton, callback);
worker->SaveToPersistent("ref", args.This());
NanAsyncQueueWorker(worker);
NanReturnUndefined();
}
void GitReference::ResolveWorker::Execute() {
int result = git_reference_resolve(
&baton->out,
baton->ref
);
baton->error_code = result;
if (result != GIT_OK && giterr_last() != NULL) {
baton->error = git_error_dup(giterr_last());
}
}
void GitReference::ResolveWorker::HandleOKCallback() {
TryCatch try_catch;
if (baton->error_code == GIT_OK) {
Handle<Value> to;
if (baton->out != NULL) {
to = GitReference::New((void *)baton->out);
} else {
to = NanNull();
}
Handle<Value> result = to;
Handle<Value> argv[2] = {
NanNull(),
result
};
callback->Call(2, argv);
} else {
if (baton->error) {
Handle<Value> argv[1] = {
NanError(baton->error->message)
};
callback->Call(1, argv);
if (baton->error->message)
free((void *)baton->error->message);
free((void *)baton->error);
} else {
callback->Call(0, NULL);
}
}
if (try_catch.HasCaught()) {
node::FatalException(try_catch);
}
delete baton;
}
/**
* @param {String} target
* @return {Reference} out
*/
NAN_METHOD(GitReference::SetSymbolicTarget) {
NanScope();
if (args.Length() == 0 || !args[0]->IsString()) {
return NanThrowError("String target is required.");
}
git_reference * out = 0;
const char * from_target;
String::Utf8Value target(args[0]->ToString());
from_target = strdup(*target);
int result = git_reference_symbolic_set_target(
&out
, ObjectWrap::Unwrap<GitReference>(args.This())->GetValue()
, from_target
);
free((void *)from_target);
if (result != GIT_OK) {
if (giterr_last()) {
return NanThrowError(giterr_last()->message);
} else {
return NanThrowError("Unknown Error");
}
}
Handle<Value> to;
if (out != NULL) {
to = GitReference::New((void *)out);
} else {
to = NanNull();
}
NanReturnValue(to);
}
/**
* @param {Oid} id
* @return {Reference} out
*/
NAN_METHOD(GitReference::setTarget) {
NanScope();
if (args.Length() == 0 || !args[0]->IsObject()) {
return NanThrowError("Oid id is required.");
}
git_reference * out = 0;
const git_oid * from_id;
from_id = ObjectWrap::Unwrap<GitOid>(args[0]->ToObject())->GetValue();
int result = git_reference_set_target(
&out
, ObjectWrap::Unwrap<GitReference>(args.This())->GetValue()
, from_id
);
if (result != GIT_OK) {
if (giterr_last()) {
return NanThrowError(giterr_last()->message);
} else {
return NanThrowError("Unknown Error");
}
}
Handle<Value> to;
if (out != NULL) {
to = GitReference::New((void *)out);
} else {
to = NanNull();
}
NanReturnValue(to);
}
#include "../include/functions/copy.h"
/**
* @param {String} new_name
* @param {Number} force
* @param {Reference} callback
*/
NAN_METHOD(GitReference::Rename) {
NanScope();
if (args.Length() == 0 || !args[0]->IsString()) {
return NanThrowError("String new_name is required.");
}
if (args.Length() == 1 || !args[1]->IsInt32()) {
return NanThrowError("Number force is required.");
}
if (args.Length() == 2 || !args[2]->IsFunction()) {
return NanThrowError("Callback is required and must be a Function.");
}
RenameBaton* baton = new RenameBaton;
baton->error_code = GIT_OK;
baton->error = NULL;
baton->ref = ObjectWrap::Unwrap<GitReference>(args.This())->GetValue();
const char * from_new_name;
String::Utf8Value new_name(args[0]->ToString());
from_new_name = strdup(*new_name);
baton->new_name = from_new_name;
int from_force;
from_force = (int) args[1]->ToInt32()->Value();
baton->force = from_force;
NanCallback *callback = new NanCallback(Local<Function>::Cast(args[2]));
RenameWorker *worker = new RenameWorker(baton, callback);
worker->SaveToPersistent("ref", args.This());
if (!args[0]->IsUndefined() && !args[0]->IsNull())
worker->SaveToPersistent("new_name", args[0]->ToObject());
if (!args[1]->IsUndefined() && !args[1]->IsNull())
worker->SaveToPersistent("force", args[1]->ToObject());
NanAsyncQueueWorker(worker);
NanReturnUndefined();
}
void GitReference::RenameWorker::Execute() {
int result = git_reference_rename(
&baton->out,
baton->ref,
baton->new_name,
baton->force
);
baton->error_code = result;
if (result != GIT_OK && giterr_last() != NULL) {
baton->error = git_error_dup(giterr_last());
}
}
void GitReference::RenameWorker::HandleOKCallback() {
TryCatch try_catch;
if (baton->error_code == GIT_OK) {
Handle<Value> to;
if (baton->out != NULL) {
to = GitReference::New((void *)baton->out);
} else {
to = NanNull();
}
Handle<Value> result = to;
Handle<Value> argv[2] = {
NanNull(),
result
};
callback->Call(2, argv);
} else {
if (baton->error) {
Handle<Value> argv[1] = {
NanError(baton->error->message)
};
callback->Call(1, argv);
if (baton->error->message)
free((void *)baton->error->message);
free((void *)baton->error);
} else {
callback->Call(0, NULL);
}
}
if (try_catch.HasCaught()) {
node::FatalException(try_catch);
}
free((void *)baton->new_name);
delete baton;
}
#include "../include/functions/copy.h"
/**
*/
NAN_METHOD(GitReference::Delete) {
NanScope();
if (args.Length() == 0 || !args[0]->IsFunction()) {
return NanThrowError("Callback is required and must be a Function.");
}
DeleteBaton* baton = new DeleteBaton;
baton->error_code = GIT_OK;
baton->error = NULL;
baton->ref = ObjectWrap::Unwrap<GitReference>(args.This())->GetValue();
NanCallback *callback = new NanCallback(Local<Function>::Cast(args[0]));
DeleteWorker *worker = new DeleteWorker(baton, callback);
worker->SaveToPersistent("ref", args.This());
NanAsyncQueueWorker(worker);
NanReturnUndefined();
}
void GitReference::DeleteWorker::Execute() {
int result = git_reference_delete(
baton->ref
);
baton->error_code = result;
if (result != GIT_OK && giterr_last() != NULL) {
baton->error = git_error_dup(giterr_last());
}
}
void GitReference::DeleteWorker::HandleOKCallback() {
TryCatch try_catch;
if (baton->error_code == GIT_OK) {
Handle<Value> result = NanUndefined();
Handle<Value> argv[2] = {
NanNull(),
result
};
callback->Call(2, argv);
} else {
if (baton->error) {
Handle<Value> argv[1] = {
NanError(baton->error->message)
};
callback->Call(1, argv);
if (baton->error->message)
free((void *)baton->error->message);
free((void *)baton->error);
} else {
callback->Call(0, NULL);
}
}
if (try_catch.HasCaught()) {
node::FatalException(try_catch);
}
delete baton;
}
/**
*/
NAN_METHOD(GitReference::IsBranch) {
NanScope();
int result = git_reference_is_branch(
ObjectWrap::Unwrap<GitReference>(args.This())->GetValue()
);
if (result != GIT_OK) {
if (giterr_last()) {
return NanThrowError(giterr_last()->message);
} else {
return NanThrowError("Unknown Error");
}
}
NanReturnUndefined();
}
/**
*/
NAN_METHOD(GitReference::IsRemote) {
NanScope();
int result = git_reference_is_remote(
ObjectWrap::Unwrap<GitReference>(args.This())->GetValue()
);
if (result != GIT_OK) {
if (giterr_last()) {
return NanThrowError(giterr_last()->message);
} else {
return NanThrowError("Unknown Error");
}
}
NanReturnUndefined();
}
/**
* @param {Number} type
* @return {Object} out
*/
NAN_METHOD(GitReference::Peel) {
NanScope();
if (args.Length() == 0 || !args[0]->IsInt32()) {
return NanThrowError("Number type is required.");
}
git_object * out = 0;
git_otype from_type;
from_type = (git_otype) args[0]->ToInt32()->Value();
int result = git_reference_peel(
&out
, ObjectWrap::Unwrap<GitReference>(args.This())->GetValue()
, from_type
);
if (result != GIT_OK) {
if (giterr_last()) {
return NanThrowError(giterr_last()->message);
} else {
return NanThrowError("Unknown Error");
}
}
Handle<Value> to;
if (out != NULL) {
to = GitObject::New((void *)out);
} else {
to = NanNull();
}
NanReturnValue(to);
}
/**
* @param {String} refname
*/
NAN_METHOD(GitReference::IsValidName) {
NanScope();
if (args.Length() == 0 || !args[0]->IsString()) {
return NanThrowError("String refname is required.");
}
const char * from_refname;
String::Utf8Value refname(args[0]->ToString());
from_refname = strdup(*refname);
int result = git_reference_is_valid_name(
from_refname
);
free((void *)from_refname);
if (result != GIT_OK) {
if (giterr_last()) {
return NanThrowError(giterr_last()->message);
} else {
return NanThrowError("Unknown Error");
}
}
NanReturnUndefined();
}
Persistent<Function> GitReference::constructor_template;