-
Notifications
You must be signed in to change notification settings - Fork 699
Expand file tree
/
Copy pathreference.cc
More file actions
executable file
·180 lines (139 loc) · 5.03 KB
/
reference.cc
File metadata and controls
executable file
·180 lines (139 loc) · 5.03 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
/*
* Copyright 2011, Tim Branyen @tbranyen <tim@tabdeveloper.com>
* @author Michael Robinson @codeofinterest <mike@pagesofinterest.net>
*
* Dual licensed under the MIT and GPL licenses.
*/
#include <v8.h>
#include <node.h>
#include <string>
#include "git2.h"
#include "../include/repo.h"
#include "../include/reference.h"
#include "../include/oid.h"
#include "../include/error.h"
#include "../include/functions/string.h"
#include "../include/functions/utilities.h"
using namespace v8;
using namespace node;
void GitReference::Initialize(Handle<Object> target) {
HandleScope scope;
Local<FunctionTemplate> tpl = FunctionTemplate::New(New);
tpl->InstanceTemplate()->SetInternalFieldCount(1);
tpl->SetClassName(String::NewSymbol("Reference"));
NODE_SET_PROTOTYPE_METHOD(tpl, "oid", Oid);
NODE_SET_PROTOTYPE_METHOD(tpl, "lookup", Lookup);
constructor_template = Persistent<Function>::New(tpl->GetFunction());
target->Set(String::NewSymbol("Reference"), constructor_template);
}
git_reference* GitReference::GetValue() {
return this->ref;
}
void GitReference::SetValue(git_reference *ref) {
this->ref = ref;
}
Handle<Value> GitReference::New(const Arguments& args) {
HandleScope scope;
GitReference *ref = new GitReference();
ref->Wrap(args.This());
return args.This();
}
Handle<Value> GitReference::Oid(const Arguments& args) {
HandleScope scope;
if(args.Length() == 0 || !args[0]->IsFunction()) {
return ThrowException(Exception::Error(String::New("Callback is required and must be a Function.")));
}
OidBaton *baton = new OidBaton;
baton->request.data = baton;
baton->error = NULL;
baton->rawOid = NULL;
baton->rawRef = ObjectWrap::Unwrap<GitReference>(args.This())->GetValue();
baton->callback = Persistent<Function>::New(Local<Function>::Cast(args[0]));
uv_queue_work(uv_default_loop(), &baton->request, OidWork, (uv_after_work_cb)OidAfterWork);
return Undefined();
}
void GitReference::OidWork(uv_work_t* req) {
OidBaton *baton = static_cast<OidBaton *>(req->data);
git_ref_t referenceType = git_reference_type(baton->rawRef);
if (referenceType == GIT_REF_INVALID) {
giterr_set_str(GITERR_INVALID, "Invalid reference type");
baton->error = giterr_last();
return;
}
if (referenceType == GIT_REF_SYMBOLIC) {
int returnCode = git_reference_resolve(&baton->rawRef, baton->rawRef);
if (returnCode != GIT_OK) {
baton->error = giterr_last();
return;
}
}
baton->rawOid = git_reference_target(baton->rawRef);
}
void GitReference::OidAfterWork(uv_work_t* req) {
HandleScope scope;
OidBaton *baton = static_cast<OidBaton *>(req->data);
if (success(baton->error, baton->callback)) {
Handle<Object> oid = GitOid::constructor_template->NewInstance();
GitOid *oidInstance = ObjectWrap::Unwrap<GitOid>(oid);
oidInstance->SetValue(*const_cast<git_oid *>(baton->rawOid));
Handle<Value> argv[2] = {
Local<Value>::New(Null()),
oid
};
TryCatch try_catch;
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
if (try_catch.HasCaught()) {
node::FatalException(try_catch);
}
}
delete req;
}
Handle<Value> GitReference::Lookup(const Arguments& args) {
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.")));
}
LookupBaton *baton = new LookupBaton;
baton->request.data = baton;
baton->ref = ObjectWrap::Unwrap<GitReference>(args.This());
baton->ref->Ref();
baton->error = NULL;
baton->rawRepo = ObjectWrap::Unwrap<GitRepo>(args[0]->ToObject())->GetValue();
baton->name = stringArgToString(args[1]->ToString());
baton->callback = Persistent<Function>::New(Local<Function>::Cast(args[2]));
uv_queue_work(uv_default_loop(), &baton->request, LookupWork, (uv_after_work_cb)LookupAfterWork);
return Undefined();
}
void GitReference::LookupWork(uv_work_t *req) {
LookupBaton *baton = static_cast<LookupBaton *>(req->data);
baton->rawRef = NULL;
int returnCode = git_reference_lookup(&baton->rawRef, baton->rawRepo, baton->name.c_str());
if (returnCode != GIT_OK) {
baton->error = giterr_last();
}
}
void GitReference::LookupAfterWork(uv_work_t *req) {
HandleScope scope;
LookupBaton *baton = static_cast<LookupBaton *>(req->data);
if (success(baton->error, baton->callback)) {
baton->ref->SetValue(baton->rawRef);
Handle<Value> argv[2] = {
Local<Value>::New(Null()),
baton->ref->handle_
};
TryCatch try_catch;
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
if (try_catch.HasCaught()) {
node::FatalException(try_catch);
}
}
baton->ref->Unref();
delete req;
}
Persistent<Function> GitReference::constructor_template;