-
Notifications
You must be signed in to change notification settings - Fork 699
Expand file tree
/
Copy pathreference.cc
More file actions
executable file
·144 lines (98 loc) · 3.57 KB
/
reference.cc
File metadata and controls
executable file
·144 lines (98 loc) · 3.57 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
/*
Copyright (c) 2011, Tim Branyen @tbranyen <[email protected]>
*/
#include <v8.h>
#include <node.h>
#include <node_events.h>
#include <string>
#include "../vendor/libgit2/include/git2.h"
#include "../include/repo.h"
#include "../include/reference.h"
#include "../include/oid.h"
using namespace v8;
using namespace node;
void GitReference::Initialize(Handle<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("Ref"));
NODE_SET_PROTOTYPE_METHOD(constructor_template, "oid", Oid);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "lookup", Lookup);
target->Set(String::NewSymbol("Ref"), constructor_template->GetFunction());
}
git_reference* GitReference::GetValue() {
return this->ref;
}
void GitReference::SetValue(git_reference *ref) {
this->ref = ref;
}
int GitReference::Lookup(git_repository* repo, const char* name) {
return git_reference_lookup(&this->ref, repo, name);
}
const git_oid* GitReference::Oid() {
return git_reference_oid(*&this->ref);
}
Handle<Value> GitReference::New(const Arguments& args) {
HandleScope scope;
GitReference *ref = new GitReference();
ref->Wrap(args.This());
return args.This();
}
Handle<Value> GitReference::Lookup(const Arguments& args) {
GitReference *ref = ObjectWrap::Unwrap<GitReference>(args.This());
Local<Function> callback;
HandleScope scope;
if(args.Length() == 0 || !args[0]->IsObject()) {
return ThrowException(Exception::Error(String::New("Repo 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]);
lookup_request *ar = new lookup_request();
ar->ref = ref;
ar->repo = ObjectWrap::Unwrap<GitRepo>(args[0]->ToObject());
String::Utf8Value name(args[1]);
ar->name = *name;
ar->callback = Persistent<Function>::New(callback);
ref->Ref();
eio_custom(EIO_Lookup, EIO_PRI_DEFAULT, EIO_AfterLookup, ar);
ev_ref(EV_DEFAULT_UC);
return Undefined();
}
int GitReference::EIO_Lookup(eio_req *req) {
lookup_request *ar = static_cast<lookup_request *>(req->data);
git_repository* repo = ar->repo->GetValue();
ar->err = ar->ref->Lookup(repo, ar->name.c_str());
return 0;
}
int GitReference::EIO_AfterLookup(eio_req *req) {
HandleScope scope;
lookup_request *ar = static_cast<lookup_request *>(req->data);
ev_unref(EV_DEFAULT_UC);
ar->ref->Unref();
Local<Value> argv[1];
argv[0] = Integer::New(ar->err);
TryCatch try_catch;
ar->callback->Call(Context::GetCurrent()->Global(), 1, argv);
if(try_catch.HasCaught())
FatalException(try_catch);
ar->callback.Dispose();
delete ar;
return 0;
}
Handle<Value> GitReference::Oid(const Arguments& args) {
GitReference *ref = ObjectWrap::Unwrap<GitReference>(args.This());
HandleScope scope;
if(args.Length() == 0 || !args[0]->IsObject()) {
return ThrowException(Exception::Error(String::New("Oid is required and must be an Object.")));
}
GitOid *oid = ObjectWrap::Unwrap<GitOid>(args[0]->ToObject());
oid->SetValue(*const_cast<git_oid *>(ref->Oid()));
return Undefined();
}
Persistent<FunctionTemplate> GitReference::constructor_template;