-
Notifications
You must be signed in to change notification settings - Fork 699
Expand file tree
/
Copy pathblob.cc
More file actions
executable file
·235 lines (164 loc) · 6.65 KB
/
blob.cc
File metadata and controls
executable file
·235 lines (164 loc) · 6.65 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
/*
* Copyright 2011, Tim Branyen @tbranyen <tim@tabdeveloper.com>
* Dual licensed under the MIT and GPL licenses.
*/
#include <v8.h>
#include <node.h>
#include <node_buffer.h>
#include <string.h>
#include "../vendor/libgit2/include/git2.h"
#include "../include/utils.h"
#include "../include/repo.h"
#include "../include/blob.h"
using namespace v8;
using namespace node;
void GitBlob::Initialize (Handle<v8::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("Blob"));
NODE_SET_PROTOTYPE_METHOD(constructor_template, "lookup", Lookup);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "rawContent", RawContent);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "rawSize", RawSize);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "close", Close);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "createFromFile", CreateFromFile);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "createFromBuffer", CreateFromBuffer);
target->Set(String::NewSymbol("Blob"), constructor_template->GetFunction());
}
git_blob* GitBlob::GetValue() {
return this->blob;
}
void GitBlob::SetValue(git_blob* blob) {
this->blob = blob;
}
int GitBlob::Lookup(git_repository* repo, const git_oid* id) {
return git_blob_lookup(&this->blob, repo, id);
}
const void* GitBlob::RawContent() {
return git_blob_rawcontent(this->blob);
}
int GitBlob::RawSize() {
return git_blob_rawsize(this->blob);
}
void GitBlob::Close() {
git_blob_free(this->blob);
}
int CreateFromFile(git_oid* oid, git_repository* repo, const char* path) {
return git_blob_create_fromdisk(oid, repo, path);
}
int CreateFromBuffer(git_oid* oid, git_repository* repo, const void* buffer, size_t len) {
return git_blob_create_frombuffer(oid, repo, buffer, len);
}
Handle<Value> GitBlob::New(const Arguments& args) {
HandleScope scope;
GitBlob* blob = new GitBlob();
blob->Wrap(args.This());
return scope.Close( args.This() );
}
Handle<Value> GitBlob::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 an Object.")));
}
if(args.Length() == 1 || !args[1]->IsObject()) {
return ThrowException(Exception::Error(String::New("Oid is required and must be an Object.")));
}
if(args.Length() == 2 || !args[2]->IsFunction()) {
return ThrowException(Exception::Error(String::New("Callback is required and must be a Function.")));
}
GitBlob* blob = ObjectWrap::Unwrap<GitBlob>(args.This());
Local<Function> callback = Local<Function>::Cast(args[2]);
lookup_request* ar = new lookup_request();
ar->blob = blob;
ar->repo = ObjectWrap::Unwrap<GitRepo>(args[0]->ToObject());
ar->oid = ObjectWrap::Unwrap<GitOid>(args[1]->ToObject());
ar->callback = Persistent<Function>::New(callback);
blob->Ref();
uv_work_t *req = new uv_work_t;
req->data = ar;
uv_queue_work(uv_default_loop(), req, EIO_Lookup, (uv_after_work_cb)EIO_AfterLookup);
return scope.Close( Undefined() );
}
void GitBlob::EIO_Lookup(uv_work_t* req) {
lookup_request* ar = static_cast<lookup_request* >(req->data);
git_oid oid = ar->oid->GetValue();
ar->err = ar->blob->Lookup(ar->repo->GetValue(), &oid);
}
void GitBlob::EIO_AfterLookup(uv_work_t* req) {
lookup_request* ar = static_cast<lookup_request* >(req->data);
delete req;
ar->blob->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;
}
Handle<Value> GitBlob::RawContent(const Arguments& args) {
HandleScope scope;
GitBlob* blob = ObjectWrap::Unwrap<GitBlob>(args.This());
int rawSize = blob->RawSize();
std::string contents = (const char *)const_cast<void *>(blob->RawContent());
int bufferLength = rawSize;
Buffer* buffer = Buffer::New(const_cast<char *>(contents.c_str()), bufferLength);
Local<Object> fastBuffer;
MAKE_FAST_BUFFER(buffer, fastBuffer);
return scope.Close( fastBuffer );
}
Handle<Value> GitBlob::RawSize(const Arguments& args) {
HandleScope scope;
GitBlob* blob = ObjectWrap::Unwrap<GitBlob>(args.This());
return scope.Close( Integer::New(blob->RawSize()) );
}
Handle<Value> GitBlob::Close(const Arguments& args) {
HandleScope scope;
GitBlob* blob = ObjectWrap::Unwrap<GitBlob>(args.This());
blob->Close();
return scope.Close( Undefined() );
}
Handle<Value> GitBlob::CreateFromFile(const Arguments& args) {
HandleScope scope;
GitBlob* blob = ObjectWrap::Unwrap<GitBlob>(args.This());
if(args.Length() == 0 || !args[0]->IsObject()) {
return ThrowException(Exception::Error(String::New("Oid is required and must be an Object.")));
}
if(args.Length() == 1 || !args[1]->IsObject()) {
return ThrowException(Exception::Error(String::New("Repo is required and must be an Object.")));
}
if(args.Length() == 2 || !args[2]->IsString()) {
return ThrowException(Exception::Error(String::New("Path is required and must be an String.")));
}
GitOid* oid = ObjectWrap::Unwrap<GitOid>(args[0]->ToObject());
GitRepo* repo = ObjectWrap::Unwrap<GitRepo>(args[1]->ToObject());
String::Utf8Value path(args[2]);
git_oid tmp_oid = oid->GetValue();
int err = blob->CreateFromFile(&tmp_oid, repo->GetValue(), *path);
return scope.Close( Integer::New(err) );
}
Handle<Value> GitBlob::CreateFromBuffer(const Arguments& args) {
HandleScope scope;
GitBlob* blob = ObjectWrap::Unwrap<GitBlob>(args.This());
if(args.Length() == 0 || !args[0]->IsObject()) {
return ThrowException(Exception::Error(String::New("Oid is required and must be an Object.")));
}
if(args.Length() == 1 || !args[1]->IsObject()) {
return ThrowException(Exception::Error(String::New("Repo is required and must be an Object.")));
}
if(args.Length() == 2 || !Buffer::HasInstance(args[2])) {
return ThrowException(Exception::Error(String::New("Buffer is required and must be a Buffer.")));
}
GitOid* oid = ObjectWrap::Unwrap<GitOid>(args[0]->ToObject());
GitRepo* repo = ObjectWrap::Unwrap<GitRepo>(args[1]->ToObject());
Local<Object> buffer = args[2]->ToObject();
const void* data = Buffer::Data(buffer);
size_t length = Buffer::Length(buffer);
git_oid tmp_oid = oid->GetValue();
int err = blob->CreateFromBuffer(&tmp_oid, repo->GetValue(), data, length);
return scope.Close( Integer::New(err) );
}
Persistent<FunctionTemplate> GitBlob::constructor_template;