-
Notifications
You must be signed in to change notification settings - Fork 691
/
repo.cc
executable file
·292 lines (202 loc) · 6.75 KB
/
repo.cc
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
/*
Copyright (c) 2011, Tim Branyen @tbranyen <tim@tabdeveloper.com>
*/
#include <v8.h>
#include <node.h>
#include <node_events.h>
#include <string>
#include "../vendor/libgit2/include/git2.h"
#include "object.h"
#include "repo.h"
#include "commit.h"
using namespace v8;
using namespace node;
void Repo::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("Repo"));
NODE_SET_PROTOTYPE_METHOD(constructor_template, "open", Open);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "lookup", Lookup);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "free", Free);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "init", Init);
target->Set(String::NewSymbol("Repo"), constructor_template->GetFunction());
}
git_repository* Repo::GetValue() {
return this->repo;
}
void Repo::SetValue(git_repository* repo) {
this->repo = repo;
}
int Repo::Open(const char* path) {
return git_repository_open(&this->repo, path);
}
void Repo::Free() {
git_repository_free(this->repo);
}
int Repo::Init(const char* path, bool is_bare) {
git_repository* repo_;
int err = git_repository_init(&repo_, path, is_bare);
if(err == 0) {
this->repo = *&repo_;
}
return err;
}
Handle<Value> Repo::New(const Arguments& args) {
HandleScope scope;
Repo *repo = new Repo();
repo->Wrap(args.This());
return args.This();
}
Handle<Value> Repo::Open(const Arguments& args) {
Repo *repo = ObjectWrap::Unwrap<Repo>(args.This());
Local<Function> callback;
HandleScope scope;
if(args.Length() == 0 || !args[0]->IsString()) {
return ThrowException(Exception::Error(String::New("Path is required and must be a String.")));
}
if(args.Length() == 1 || !args[1]->IsFunction()) {
return ThrowException(Exception::Error(String::New("Callback is required and must be a Function.")));
}
callback = Local<Function>::Cast(args[1]);
open_request *ar = new open_request();
ar->repo = repo;
String::Utf8Value path(args[0]);
ar->path = *path;
ar->callback = Persistent<Function>::New(callback);
repo->Ref();
eio_custom(EIO_Open, EIO_PRI_DEFAULT, EIO_AfterOpen, ar);
ev_ref(EV_DEFAULT_UC);
return Undefined();
}
int Repo::EIO_Open(eio_req *req) {
open_request *ar = static_cast<open_request *>(req->data);
ar->err = ar->repo->Open(ar->path.c_str());
return 0;
}
int Repo::EIO_AfterOpen(eio_req *req) {
HandleScope scope;
open_request *ar = static_cast<open_request *>(req->data);
ev_unref(EV_DEFAULT_UC);
ar->repo->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> Repo::Lookup(const Arguments& args) {
Repo *repo = ObjectWrap::Unwrap<Repo>(args.This());
Local<Function> callback;
HandleScope scope;
if(args.Length() == 0 || !args[0]->IsObject()) {
return ThrowException(Exception::Error(String::New("Object is required and must be a Object.")));
}
if(args.Length() == 1 || !args[1]->IsObject()) {
return ThrowException(Exception::Error(String::New("Repo is required and must be a Object.")));
}
if(args.Length() == 2 || !args[2]->IsObject()) {
return ThrowException(Exception::Error(String::New("Oid is required and must be a Object.")));
}
if(args.Length() == 3 || !args[3]->IsFunction()) {
return ThrowException(Exception::Error(String::New("Callback is required and must be a Function.")));
}
callback = Local<Function>::Cast(args[3]);
lookup_request *ar = new lookup_request();
ar->repo = repo;
ar->callback = Persistent<Function>::New(callback);
repo->Ref();
//eio_custom(EIO_LookupRef, EIO_PRI_DEFAULT, EIO_AfterLookupRef, ar);
//ev_ref(EV_DEFAULT_UC);
return Undefined();
}
int Repo::EIO_Lookup(eio_req *req) {
//lookup_request *ar = static_cast<lookup_request *>(req->data);
//
//String::Utf8Value name(ar->name);
//git_reference *ref;
//
//int err = ar->repo->LookupRef((git_reference **)ref, *name);
//ar->err = Persistent<Value>::New(Integer::New(err));
//
//if(Int32::Cast(*ar->err)->Value() == 0) {
// ar->ref->SetValue(*&ref);
//}
//
//return 0;
}
int Repo::EIO_AfterLookup(eio_req *req) {
//HandleScope scope;
//lookup_request *ar = static_cast<lookupref_request *>(req->data);
//ev_unref(EV_DEFAULT_UC);
//ar->repo->Unref();
//Local<Value> argv[1];
//argv[0] = Number::Cast(*ar->err);
//TryCatch try_catch;
//ar->callback->Call(Context::GetCurrent()->Global(), 1, argv);
//if(try_catch.HasCaught())
// FatalException(try_catch);
//
//ar->err.Dispose();
//ar->name.Dispose();
//ar->callback.Dispose();
//delete ar;
//return 0;
}
Handle<Value> Repo::Free(const Arguments& args) {
Repo *repo = ObjectWrap::Unwrap<Repo>(args.This());
HandleScope scope;
repo->Free();
return Undefined();
}
Handle<Value> Repo::Init(const Arguments& args) {
Repo *repo = ObjectWrap::Unwrap<Repo>(args.This());
Local<Function> callback;
HandleScope scope;
if(args.Length() == 0 || !args[0]->IsString()) {
return ThrowException(Exception::Error(String::New("path is required and must be a String.")));
}
if(args.Length() == 1 || !args[1]->IsBoolean()) {
return ThrowException(Exception::Error(String::New("is_bare is required and must be a Boolean.")));
}
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]);
init_request *ar = new init_request();
ar->repo = repo;
String::Utf8Value path(args[0]);
ar->path = *path;
ar->is_bare = args[1]->ToBoolean()->Value();
ar->callback = Persistent<Function>::New(callback);
repo->Ref();
eio_custom(EIO_Init, EIO_PRI_DEFAULT, EIO_AfterInit, ar);
ev_ref(EV_DEFAULT_UC);
return Undefined();
}
int Repo::EIO_Init(eio_req *req) {
init_request *ar = static_cast<init_request *>(req->data);
ar->err = ar->repo->Init(ar->path.c_str(), ar->is_bare);
return 0;
}
int Repo::EIO_AfterInit(eio_req *req) {
HandleScope scope;
init_request *ar = static_cast<init_request *>(req->data);
ev_unref(EV_DEFAULT_UC);
ar->repo->Unref();
Local<Value> argv[2];
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;
}
Persistent<FunctionTemplate> Repo::constructor_template;