-
Notifications
You must be signed in to change notification settings - Fork 699
Expand file tree
/
Copy pathpatch.cc
More file actions
328 lines (269 loc) · 7.94 KB
/
patch.cc
File metadata and controls
328 lines (269 loc) · 7.94 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
/**
* This code is auto-generated; unless you know what you're doing, do not modify!
**/
#include <v8.h>
#include <node.h>
#include <string.h>
#include "git2.h"
#include "../include/functions/copy.h"
#include "../include/patch.h"
#include "../include/delta.h"
#include "../include/diff_range.h"
using namespace v8;
using namespace node;
GitPatch::GitPatch(git_diff_patch *raw) {
this->raw = raw;
}
GitPatch::~GitPatch() {
git_diff_patch_free(this->raw);
}
void GitPatch::Initialize(Handle<v8::Object> target) {
NanScope();
Local<FunctionTemplate> tpl = FunctionTemplate::New(New);
tpl->InstanceTemplate()->SetInternalFieldCount(1);
tpl->SetClassName(NanSymbol("Patch"));
NODE_SET_PROTOTYPE_METHOD(tpl, "delta", Delta);
NODE_SET_PROTOTYPE_METHOD(tpl, "size", Size);
NODE_SET_PROTOTYPE_METHOD(tpl, "stats", Stats);
NODE_SET_PROTOTYPE_METHOD(tpl, "hunk", Hunk);
NODE_SET_PROTOTYPE_METHOD(tpl, "lines", Lines);
NODE_SET_PROTOTYPE_METHOD(tpl, "line", Line);
NODE_SET_METHOD(tpl, "toString", ToString);
NanAssignPersistent(FunctionTemplate, constructor_template, tpl);
target->Set(String::NewSymbol("Patch"), tpl->GetFunction());
}
NAN_METHOD(GitPatch::New) {
NanScope();
if (args.Length() == 0 || !args[0]->IsExternal()) {
return NanThrowError(String::New("git_diff_patch is required."));
}
GitPatch* object = new GitPatch((git_diff_patch *) External::Cast(*args[0])->Value());
object->Wrap(args.This());
NanReturnValue(args.This());
}
Handle<Value> GitPatch::New(void *raw) {
NanScope();
Handle<Value> argv[1] = { External::New((void *)raw) };
Local<Object> instance;
Local<FunctionTemplate> constructorHandle = NanPersistentToLocal(constructor_template);
instance = constructorHandle->GetFunction()->NewInstance(1, argv);
return scope.Close(instance);
}
git_diff_patch *GitPatch::GetValue() {
return this->raw;
}
/**
* @return {Delta} result
*/
NAN_METHOD(GitPatch::Delta) {
NanScope();
const git_diff_delta * result = git_diff_patch_delta(
ObjectWrap::Unwrap<GitPatch>(args.This())->GetValue()
);
Handle<Value> to;
if (result != NULL) {
result = (const git_diff_delta * )git_diff_delta_dup(result);
}
if (result != NULL) {
to = GitDelta::New((void *)result);
} else {
to = Null();
}
NanReturnValue(to);
}
/**
* @return {Number} result
*/
NAN_METHOD(GitPatch::Size) {
NanScope();
size_t result = git_diff_patch_num_hunks(
ObjectWrap::Unwrap<GitPatch>(args.This())->GetValue()
);
Handle<Value> to;
to = Uint32::New(result);
NanReturnValue(to);
}
/**
* @return {Number} total_context
* @return {Number} total_additions
* @return {Number} total_deletions
*/
NAN_METHOD(GitPatch::Stats) {
NanScope();
size_t total_context = 0;
size_t total_additions = 0;
size_t total_deletions = 0;
int result = git_diff_patch_line_stats(
&total_context
, &total_additions
, &total_deletions
, ObjectWrap::Unwrap<GitPatch>(args.This())->GetValue()
);
if (result != GIT_OK) {
if (giterr_last()) {
return NanThrowError(String::New(giterr_last()->message));
} else {
return NanThrowError(String::New("Unkown Error"));
}
}
Handle<Object> toReturn = Object::New();
Handle<Value> to;
to = Integer::New(total_context);
toReturn->Set(String::NewSymbol("total_context"), to);
to = Integer::New(total_additions);
toReturn->Set(String::NewSymbol("total_additions"), to);
to = Integer::New(total_deletions);
toReturn->Set(String::NewSymbol("total_deletions"), to);
NanReturnValue(toReturn);
}
/**
* @param {Number} hunk_idx
* @return {DiffRange} range
* @return {String} header
* @return {Number} header_len
* @return {Number} lines_in_hunk
*/
NAN_METHOD(GitPatch::Hunk) {
NanScope();
if (args.Length() == 0 || !args[0]->IsUint32()) {
return NanThrowError(String::New("Number hunk_idx is required."));
}
const git_diff_range * range = 0;
const char * header = 0;
size_t header_len = 0;
size_t lines_in_hunk = 0;
size_t from_hunk_idx;
from_hunk_idx = (size_t) args[0]->ToUint32()->Value();
int result = git_diff_patch_get_hunk(
&range
, &header
, &header_len
, &lines_in_hunk
, ObjectWrap::Unwrap<GitPatch>(args.This())->GetValue()
, from_hunk_idx
);
if (result != GIT_OK) {
if (giterr_last()) {
return NanThrowError(String::New(giterr_last()->message));
} else {
return NanThrowError(String::New("Unkown Error"));
}
}
Handle<Object> toReturn = Object::New();
Handle<Value> to;
if (range != NULL) {
range = (const git_diff_range * )git_diff_range_dup(range);
}
if (range != NULL) {
to = GitDiffRange::New((void *)range);
} else {
to = Null();
}
toReturn->Set(String::NewSymbol("range"), to);
to = String::New(header);
toReturn->Set(String::NewSymbol("header"), to);
to = Uint32::New(header_len);
toReturn->Set(String::NewSymbol("headerLength"), to);
to = Uint32::New(lines_in_hunk);
toReturn->Set(String::NewSymbol("lines"), to);
NanReturnValue(toReturn);
}
/**
* @param {Number} hunk_idx
* @return {Number} result
*/
NAN_METHOD(GitPatch::Lines) {
NanScope();
if (args.Length() == 0 || !args[0]->IsUint32()) {
return NanThrowError(String::New("Number hunk_idx is required."));
}
size_t from_hunk_idx;
from_hunk_idx = (size_t) args[0]->ToUint32()->Value();
int result = git_diff_patch_num_lines_in_hunk(
ObjectWrap::Unwrap<GitPatch>(args.This())->GetValue()
, from_hunk_idx
);
Handle<Value> to;
to = Int32::New(result);
NanReturnValue(to);
}
/**
* @param {Number} hunk_idx
* @param {Number} line_of_hunk
* @return {Number} line_origin
* @return {String} content
* @return {Number} content_len
* @return {Number} old_lineno
* @return {Number} new_lineno
*/
NAN_METHOD(GitPatch::Line) {
NanScope();
if (args.Length() == 0 || !args[0]->IsUint32()) {
return NanThrowError(String::New("Number hunk_idx is required."));
}
if (args.Length() == 1 || !args[1]->IsUint32()) {
return NanThrowError(String::New("Number line_of_hunk is required."));
}
char line_origin = 0;
const char * content = 0;
size_t content_len = 0;
int old_lineno = 0;
int new_lineno = 0;
size_t from_hunk_idx;
from_hunk_idx = (size_t) args[0]->ToUint32()->Value();
size_t from_line_of_hunk;
from_line_of_hunk = (size_t) args[1]->ToUint32()->Value();
int result = git_diff_patch_get_line_in_hunk(
&line_origin
, &content
, &content_len
, &old_lineno
, &new_lineno
, ObjectWrap::Unwrap<GitPatch>(args.This())->GetValue()
, from_hunk_idx
, from_line_of_hunk
);
if (result != GIT_OK) {
if (giterr_last()) {
return NanThrowError(String::New(giterr_last()->message));
} else {
return NanThrowError(String::New("Unkown Error"));
}
}
Handle<Object> toReturn = Object::New();
Handle<Value> to;
to = Integer::New(line_origin);
toReturn->Set(String::NewSymbol("lineOrigin"), to);
to = String::New(content, content_len);
toReturn->Set(String::NewSymbol("content"), to);
to = Uint32::New(content_len);
toReturn->Set(String::NewSymbol("length"), to);
to = Int32::New(old_lineno);
toReturn->Set(String::NewSymbol("oldLineNumber"), to);
to = Int32::New(new_lineno);
toReturn->Set(String::NewSymbol("newLineNumber"), to);
NanReturnValue(toReturn);
}
/**
* @return {String} string
*/
NAN_METHOD(GitPatch::ToString) {
NanScope();
char * string = 0;
int result = git_diff_patch_to_str(
&string
, ObjectWrap::Unwrap<GitPatch>(args.This())->GetValue()
);
if (result != GIT_OK) {
if (giterr_last()) {
return NanThrowError(String::New(giterr_last()->message));
} else {
return NanThrowError(String::New("Unkown Error"));
}
}
Handle<Value> to;
to = String::New(string);
free(string);
NanReturnValue(to);
}
Persistent<FunctionTemplate> GitPatch::constructor_template;