-
Notifications
You must be signed in to change notification settings - Fork 78
/
tree.c
488 lines (395 loc) · 12.1 KB
/
tree.c
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#include "php_git2.h"
#include "php_git2_priv.h"
#include "tree.h"
typedef struct tree_walk_cb_t {
zval *payload;
zend_fcall_info *fci;
zend_fcall_info_cache *fcc;
GIT2_TSRMLS_DECL
};
static int tree_walk_cb(const char *root, const git_tree_entry *entry, void *payload)
{
php_git2_t *result;
zval *param_root, *param_rsrc, *retval_ptr = NULL;
struct tree_walk_cb_t *p = (struct tree_walk_cb_t*)payload;
GIT2_TSRMLS_SET(p->tsrm_ls)
Z_ADDREF_P(p->payload);
MAKE_STD_ZVAL(param_root);
MAKE_STD_ZVAL(param_rsrc);
ZVAL_STRING(param_root, root, 1);
php_git2_make_resource(&result, PHP_GIT2_TYPE_TREE_ENTRY, entry, 0 TSRMLS_CC);
zend_list_addref(result->resource_id);
ZVAL_RESOURCE(param_rsrc, result->resource_id);
if (php_git2_call_function_v(p->fci, p->fcc TSRMLS_CC, &retval_ptr, 3, ¶m_root, ¶m_rsrc, &p->payload)) {
zend_list_delete(result->resource_id);
return 0;
}
zval_ptr_dtor(&retval_ptr);
zend_list_delete(result->resource_id);
return 1;
}
static int php_git2_tree_walk_cb_init(struct tree_walk_cb **out, zend_fcall_info *fci, zend_fcall_info_cache *fcc, void *payload TSRMLS_DC)
{
struct tree_walk_cb_t *cb;
cb = (struct tree_walk_cb_t*)emalloc(sizeof(struct tree_walk_cb_t));
if (cb == NULL) {
return 1;
}
cb->payload = payload;
cb->fci = fci;
cb->fcc = fcc;
GIT2_TSRMLS_SET2(cb, TSRMLS_C);
*out = cb;
return 0;
}
static void php_git2_tree_walk_cb_free(struct tree_walk_cb *target)
{
efree(target);
}
/* {{{ proto resource git_tree_entry_byindex(resource $tree, string $name)
*/
PHP_FUNCTION(git_tree_entry_byindex)
{
zval *tree;
php_git2_t *git2, *result;
long index;
const git_tree_entry *entry;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"rl", &tree, &index) == FAILURE) {
return;
}
PHP_GIT2_MAKE_RESOURCE(result);
ZEND_FETCH_RESOURCE(git2, php_git2_t*, &tree, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
entry = git_tree_entry_byindex(PHP_GIT2_V(git2, tree), index);
PHP_GIT2_V(result, tree_entry) = entry;
result->type = PHP_GIT2_TYPE_TREE_ENTRY;
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
result->should_free_v = 0;
ZVAL_RESOURCE(return_value, result->resource_id);
}
/* {{{ proto resource git_tree_entry_byoid(resource $tree, string $oid)
*/
PHP_FUNCTION(git_tree_entry_byoid)
{
zval *tree;
php_git2_t *git2, *result;
char *hash;
int hash_len;
git_oid id;
const git_tree_entry *entry;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"rs", &tree, &hash, &hash_len) == FAILURE) {
return;
}
if (git_oid_fromstrn(&id, hash, hash_len) != GIT_OK) {
return;
}
PHP_GIT2_MAKE_RESOURCE(result);
ZEND_FETCH_RESOURCE(git2, php_git2_t*, &tree, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
entry = git_tree_entry_byoid(PHP_GIT2_V(git2, tree), &id);
PHP_GIT2_V(result, tree_entry) = entry;
result->type = PHP_GIT2_TYPE_TREE_ENTRY;
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
result->should_free_v = 0;
ZVAL_RESOURCE(return_value, result->resource_id);
}
/* {{{ proto resource git_tree_entry_byname(resource $tree, string $name)
*/
PHP_FUNCTION(git_tree_entry_byname)
{
zval *tree;
php_git2_t *git2, *result;
char *name;
int name_len;
const git_tree_entry *entry;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"rs", &tree, &name, &name_len) == FAILURE) {
return;
}
PHP_GIT2_MAKE_RESOURCE(result);
ZEND_FETCH_RESOURCE(git2, php_git2_t*, &tree, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
entry = git_tree_entry_byname(PHP_GIT2_V(git2, tree), name);
PHP_GIT2_V(result, tree_entry) = entry;
result->type = PHP_GIT2_TYPE_TREE_ENTRY;
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
result->should_free_v = 0;
ZVAL_RESOURCE(return_value, result->resource_id);
}
/* {{{ proto array git_tree_entry_bypath(resource $tree, string $path)
*/
PHP_FUNCTION(git_tree_entry_bypath)
{
zval *tree;
php_git2_t *git2, *result;
char *path;
int path_len;
const git_tree_entry *entry;
int error;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"rs", &tree, &path, &path_len) == FAILURE) {
return;
}
PHP_GIT2_MAKE_RESOURCE(result);
ZEND_FETCH_RESOURCE(git2, php_git2_t*, &tree, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
error = git_tree_entry_bypath(&entry, PHP_GIT2_V(git2, tree), path);
PHP_GIT2_V(result, tree_entry) = entry;
result->type = PHP_GIT2_TYPE_TREE_ENTRY;
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
result->should_free_v = 0;
ZVAL_RESOURCE(return_value, result->resource_id);
}
/* {{{ proto string git_tree_entry_id(resource $tree_entry)
*/
PHP_FUNCTION(git_tree_entry_id)
{
zval *tree_entry;
php_git2_t *git2;
char out[GIT2_OID_HEXSIZE] = {0};
const git_oid *id;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"r", &tree_entry) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(git2, php_git2_t*, &tree_entry, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
id = git_tree_entry_id(PHP_GIT2_V(git2, tree_entry));
git_oid_fmt(out, id);
RETURN_STRING(out, 1);
}
/* {{{ proto long git_tree_entry_type(resource $tree_entry)
*/
PHP_FUNCTION(git_tree_entry_type)
{
zval *tree;
php_git2_t *git2;
git_otype type;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"r", &tree) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(git2, php_git2_t*, &tree, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
type = git_tree_entry_type(PHP_GIT2_V(git2, tree));
RETURN_LONG(type);
}
/* {{{ proto string git_tree_entry_name(resource $tree_entry)
*/
PHP_FUNCTION(git_tree_entry_name)
{
zval *tree_entry;
php_git2_t *git2;
const char *name;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"r", &tree_entry) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(git2, php_git2_t*, &tree_entry, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
name = git_tree_entry_name(PHP_GIT2_V(git2, tree_entry));
RETURN_STRING(name, 1);
}
/* {{{ proto long git_tree_entry_count(resource $tree)
*/
PHP_FUNCTION(git_tree_entrycount)
{
zval *tree;
php_git2_t *git2;
size_t count;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"r", &tree) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(git2, php_git2_t*, &tree, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
count = git_tree_entrycount(PHP_GIT2_V(git2, tree));
RETURN_LONG(count);
}
/* {{{ proto long git_tree_entry_filemode(resource $tree_entry)
*/
PHP_FUNCTION(git_tree_entry_filemode)
{
zval *tree_entry;
php_git2_t *git2;
git_filemode_t filemode;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"r", &tree_entry) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(git2, php_git2_t*, &tree_entry, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
filemode = git_tree_entry_filemode(PHP_GIT2_V(git2, tree_entry));
RETURN_LONG(filemode);
}
/* {{{ proto long git_tree_entry_filemode_raw(resource $tree_entry)
*/
PHP_FUNCTION(git_tree_entry_filemode_raw)
{
zval *tree_entry;
php_git2_t *git2;
git_filemode_t filemode;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"r", &tree_entry) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(git2, php_git2_t*, &tree_entry, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
filemode = git_tree_entry_filemode_raw(PHP_GIT2_V(git2, tree_entry));
RETURN_LONG(filemode);
}
/* {{{ proto bool git_tree_entry_cmp(resource $e1, resource $e2)
*/
PHP_FUNCTION(git_tree_entry_cmp)
{
zval *e1, *e2;
php_git2_t *g_e1, *g_e2;
int result;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"rr", &e1, &e2) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(g_e1, php_git2_t*, &e1, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
ZEND_FETCH_RESOURCE(g_e2, php_git2_t*, &e2, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
result = git_tree_entry_cmp(PHP_GIT2_V(g_e1, tree_entry), PHP_GIT2_V(g_e2, tree_entry));
RETURN_LONG(result);
}
/* {{{ proto void git_tree_free(resource $tree_entry)
*/
PHP_FUNCTION(git_tree_free)
{
zval *tree;
php_git2_t *git2;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"r", &tree) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(git2, php_git2_t*, &tree, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
if (git2->should_free_v) {
git_tree_free(PHP_GIT2_V(git2, tree));
git2->should_free_v = 0;
}
zval_ptr_dtor(&tree);
}
/* {{{ proto void git_tree_entry_free(resource $tree_entry)
*/
PHP_FUNCTION(git_tree_entry_free)
{
zval *tree_entry;
php_git2_t *git2;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"r", &tree_entry) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(git2, php_git2_t*, &tree_entry, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
if (git2->should_free_v) {
git_tree_entry_free(PHP_GIT2_V(git2, tree_entry));
git2->should_free_v = 0;
}
zval_ptr_dtor(&tree_entry);
}
/* {{{ proto resource git_tree_entry_dup(resource $tree_entry)
*/
PHP_FUNCTION(git_tree_entry_dup)
{
zval *tree_entry;
php_git2_t *git2, *result;
git_tree_entry *entry;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"r", &tree_entry) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(git2, php_git2_t*, &tree_entry, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
entry = git_tree_entry_dup(PHP_GIT2_V(git2, tree_entry));
PHP_GIT2_MAKE_RESOURCE(result);
PHP_GIT2_V(result, tree_entry) = entry;
result->type = PHP_GIT2_TYPE_TREE_ENTRY;
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
result->should_free_v = 1;
ZVAL_RESOURCE(return_value, result->resource_id);
}
/* {{{ proto string git_tree_id(resource $tree_entry)
*/
PHP_FUNCTION(git_tree_id)
{
zval *tree;
php_git2_t *git2;
char out[GIT2_OID_HEXSIZE] = {0};
const git_oid *id;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"r", &tree) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(git2, php_git2_t*, &tree, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
id = git_tree_id(PHP_GIT2_V(git2, tree));
git_oid_fmt(out, id);
RETURN_STRING(out, 1);
}
/* {{{ proto resource git_tree_lookup(resource $repository, mixed $oid)
*/
PHP_FUNCTION(git_tree_lookup)
{
zval *repository;
php_git2_t *git2, *result;
git_tree *tree;
char *hash;
int hash_len;
int error;
git_oid id;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"rs", &repository, &hash, &hash_len) == FAILURE) {
return;
}
if (git_oid_fromstrn(&id, hash, hash_len) != GIT_OK) {
return;
}
ZEND_FETCH_RESOURCE(git2, php_git2_t*, &repository, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
error = git_tree_lookup(&tree, PHP_GIT2_V(git2, repository), &id);
if (php_git2_check_error(error, "git_commit_lookup" TSRMLS_CC)) {
RETURN_FALSE
}
PHP_GIT2_MAKE_RESOURCE(result);
PHP_GIT2_V(result, tree) = tree;
result->type = PHP_GIT2_TYPE_TREE;
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
result->should_free_v = 0;
ZVAL_RESOURCE(return_value, result->resource_id);
}
/* }}} */
/* {{{ proto resource git_tree_owner(resource $tree)
*/
PHP_FUNCTION(git_tree_owner)
{
git_repository *result = NULL;
zval *tree = NULL;
php_git2_t *_tree = NULL, *__result = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"r", &tree) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(_tree, php_git2_t*, &tree, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
result = git_tree_owner(PHP_GIT2_V(_tree, tree));
if (php_git2_make_resource(&__result, PHP_GIT2_TYPE_TREE, result, 0 TSRMLS_CC)) {
RETURN_FALSE;
}
ZVAL_RESOURCE(return_value, GIT2_RVAL_P(__result));
}
/* }}} */
/* {{{ proto void git_tree_walk(resource $tree, long $mode, Callable $callback, mixed &$payload)
*/
PHP_FUNCTION(git_tree_walk)
{
zval *tree, *payload;
php_git2_t *git2;
zend_fcall_info fci = empty_fcall_info;
zend_fcall_info_cache fcc = empty_fcall_info_cache;
long mode = GIT_TREEWALK_PRE;
struct tree_walk_cb_t *cb;
int error = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"rlf|z", &tree, &mode, &fci, &fcc, &payload) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(git2, php_git2_t*, &tree, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
if (php_git2_tree_walk_cb_init(&cb, &fci, &fcc, payload TSRMLS_CC)) {
RETURN_FALSE;
}
error = git_tree_walk(PHP_GIT2_V(git2, tree), mode, tree_walk_cb, cb);
php_git2_tree_walk_cb_free(cb);
if (php_git2_check_error(error, "git_tree_walk" TSRMLS_CC)) {
RETURN_FALSE
}
}
/* }}} */