-
Notifications
You must be signed in to change notification settings - Fork 78
/
helper.c
786 lines (641 loc) · 22.6 KB
/
helper.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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
#include "php_git2.h"
#include "php_git2_priv.h"
#include "helper.h"
static zval* datetime_instantiate(zend_class_entry *pce, zval *object TSRMLS_DC)
{
#if PHP_VERSION_ID <= 50304
Z_TYPE_P(object) = IS_OBJECT;
object_init_ex(object, pce);
Z_SET_REFCOUNT_P(object, 1);
Z_UNSET_ISREF_P(object);
return object;
#else
return php_date_instantiate(pce, object TSRMLS_CC);
#endif
}
int php_git2_check_error(int error_code, const char *action TSRMLS_DC)
{
int result = 0;
const git_error * error;
if (!error_code) {
return result;
}
error = giterr_last();
php_error_docref(NULL TSRMLS_CC, E_WARNING, "WARNING %d %s - %s",
error_code, action, (error && error->message) ? error->message : "???");
result = 1;
return result;
}
zval* php_git2_read_arrval(zval *array, char *name, size_t name_len TSRMLS_DC)
{
zval *result = NULL, **element;
if (zend_hash_find(Z_ARRVAL_P(array), name, name_len, (void**)&element) == SUCCESS) {
result = *element;
}
return result;
}
long php_git2_read_arrval_long2(zval *array, char *name, size_t name_len, long value TSRMLS_DC)
{
zval *tmp;
long result = value;
tmp = php_git2_read_arrval(array, name, name_len TSRMLS_CC);
if (tmp) {
result = Z_LVAL_P(tmp);
}
return result;
}
long php_git2_read_arrval_long(zval *array, char *name, size_t name_len TSRMLS_DC)
{
zval *tmp;
long result = 0;
tmp = php_git2_read_arrval(array, name, name_len TSRMLS_CC);
if (tmp) {
result = Z_LVAL_P(tmp);
}
return result;
}
const char* php_git2_read_arrval_string(zval *array, char *name, size_t name_len TSRMLS_DC)
{
zval *tmp;
const char *result = NULL;
tmp = php_git2_read_arrval(array, name, name_len TSRMLS_CC);
if (tmp) {
result = Z_STRVAL_P(tmp);
}
return result;
}
void php_git2_array_to_signature(git_signature *signature, zval *author TSRMLS_DC)
{
zval *name = NULL, *email = NULL, *time = NULL;
name = php_git2_read_arrval(author, ZEND_STRS("name") TSRMLS_CC);
email = php_git2_read_arrval(author, ZEND_STRS("email") TSRMLS_CC);
time = php_git2_read_arrval(author, ZEND_STRS("time") TSRMLS_CC);
signature->name = Z_STRVAL_P(name);
signature->email = Z_STRVAL_P(email);
//instanceof_function_ex(const zend_class_entry *instance_ce, const zend_class_entry *ce, zend_bool interfaces_only TSRMLS_DC);
if (time != NULL &&
Z_TYPE_P(time) == IS_OBJECT &&
instanceof_function_ex(Z_OBJCE_P(time), php_date_get_date_ce(), 0 TSRMLS_CC)) {
php_date_obj *date;
date = (php_date_obj *)zend_object_store_get_object(time TSRMLS_CC);
signature->when.time = date->time->sse;
signature->when.offset = date->time->z;
}
}
void php_git2_signature_to_array(const git_signature *signature, zval **out TSRMLS_DC)
{
zval *result, *datetime, *timezone;
php_timezone_obj *tzobj;
char time_str[12] = {0};
MAKE_STD_ZVAL(result);
array_init(result);
MAKE_STD_ZVAL(datetime);
MAKE_STD_ZVAL(timezone);
datetime_instantiate(php_date_get_date_ce(), datetime TSRMLS_CC);
snprintf(time_str,12,"%c%ld",'@', signature->when.time);
datetime_instantiate(php_date_get_timezone_ce(), timezone TSRMLS_CC);
tzobj = (php_timezone_obj *) zend_object_store_get_object(timezone TSRMLS_CC);
tzobj->initialized = 1;
tzobj->type = TIMELIB_ZONETYPE_OFFSET;
tzobj->tzi.utc_offset = -signature->when.offset; // doesn't work
/* TODO(chobie): how do i set offset? */
php_date_initialize(zend_object_store_get_object(datetime TSRMLS_CC), time_str, strlen(time_str), NULL, timezone, 0 TSRMLS_CC);
if (signature->name == NULL) {
add_assoc_null_ex(result, ZEND_STRS("name"));
} else {
add_assoc_string_ex(result, ZEND_STRS("name"), signature->name, 1);
}
if (signature->email == NULL) {
add_assoc_null_ex(result, ZEND_STRS("email"));
} else {
add_assoc_string_ex(result, ZEND_STRS("email"), signature->email, 1);
}
add_assoc_zval_ex(result, ZEND_STRS("time"), datetime);
zval_ptr_dtor(&timezone);
*out = result;
}
void php_git2_strarray_to_array(git_strarray *array, zval **out TSRMLS_DC)
{
zval *result;
int i = 0;
MAKE_STD_ZVAL(result);
array_init(result);
for (i = 0; i < array->count; i++) {
add_next_index_string(result, array->strings[i], 1);
}
*out = result;
}
int php_git2_call_function_v(
zend_fcall_info *fci, zend_fcall_info_cache *fcc TSRMLS_DC, zval **retval_ptr_ptr, zend_uint param_count, ...)
{
zval **params = NULL;
va_list ap;
int i = 0;
if (param_count > 0) {
params = emalloc(sizeof(zval*) * param_count);
va_start(ap, param_count);
for (i = 0; i < param_count; i++) {
params[i] = va_arg(ap, zval**);
}
va_end(ap);
} else {
params = NULL;
}
if (ZEND_FCI_INITIALIZED(*fci)) {
fci->params = params;
fci->retval_ptr_ptr = retval_ptr_ptr;
fci->param_count = param_count;
fci->no_separation = 1;
if (zend_call_function(fci, fcc TSRMLS_CC) != SUCCESS) {
if (param_count > 0) {
for (i = 0; i < param_count; i++) {
zval_ptr_dtor(params[i]);
}
efree(params);
}
return 1;
}
zend_fcall_info_args_clear(fci, 0);
}
if (param_count > 0) {
for (i = 0; i < param_count; i++) {
zval_ptr_dtor(params[i]);
}
efree(params);
}
return 0;
}
int php_git2_cb_init(php_git2_cb_t **out, zend_fcall_info *fci, zend_fcall_info_cache *fcc, void *payload TSRMLS_DC)
{
php_git2_cb_t *cb;
cb = (struct php_git2_cb_t*)emalloc(sizeof(php_git2_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;
}
void php_git2_cb_free(php_git2_cb_t *target)
{
efree(target);
}
void php_git2_array_to_strarray(git_strarray *out, zval *array TSRMLS_DC)
{
int elements = 0, i;
HashPosition pos;
zval **value;
if (array == NULL) {
return;
}
if (Z_TYPE_P(array) != IS_ARRAY){
return;
}
if (zend_hash_num_elements(Z_ARRVAL_P(array)) == 0) {
return;
}
elements = zend_hash_num_elements(Z_ARRVAL_P(array));
out->strings = (char**)emalloc(sizeof(char*) * elements);
out->count = elements;
for (i = 0, zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(array), &pos);
zend_hash_get_current_data_ex(Z_ARRVAL_P(array), (void **)&value, &pos) == SUCCESS;
zend_hash_move_forward_ex(Z_ARRVAL_P(array), &pos), i++) {
char *buffer;
Z_STRVAL_PP(value);
buffer = emalloc(sizeof(char*) * Z_STRLEN_PP(value) + 1);
memcpy(buffer, Z_STRVAL_PP(value), Z_STRLEN_PP(value));
buffer[Z_STRLEN_PP(value)] = '\0';
out->strings[i] = buffer;
}
}
void php_git2_strarray_free(git_strarray *out)
{
int i = 0;
for (i = 0; i < out->count; i++) {
efree(out->strings[i]);
}
efree(out->strings);
}
void php_git2_git_checkout_opts_to_array(git_checkout_opts *opts, zval **out TSRMLS_DC)
{
zval *result;
git_checkout_opts tmp = GIT_CHECKOUT_OPTS_INIT;
opts = &tmp;
MAKE_STD_ZVAL(result);
array_init(result);
add_assoc_long_ex(result, ZEND_STRS("version"), opts->version);
add_assoc_long_ex(result, ZEND_STRS("checkout_strategy"), opts->checkout_strategy);
add_assoc_long_ex(result, ZEND_STRS("disable_filters"), opts->disable_filters);
add_assoc_long_ex(result, ZEND_STRS("dir_mode"), opts->dir_mode);
add_assoc_long_ex(result, ZEND_STRS("file_mode"), opts->file_mode);
add_assoc_long_ex(result, ZEND_STRS("file_open_flags"), opts->file_open_flags);
add_assoc_long_ex(result, ZEND_STRS("notify_flags"), opts->notify_flags);
if (opts->notify_payload != NULL) {
} else {
add_assoc_null_ex(result, ZEND_STRS("notify_cb"));
}
if (opts->notify_payload != NULL) {
} else {
add_assoc_null_ex(result, ZEND_STRS("notify_payload"));
}
if (opts->progress_cb != NULL) {
} else {
add_assoc_null_ex(result, ZEND_STRS("progress_cb"));
}
if (opts->progress_payload != NULL) {
} else {
add_assoc_null_ex(result, ZEND_STRS("progress_payload"));
}
if (opts->paths.count > 0) {
zval *paths;
php_git2_strarray_to_array(&opts->paths, &paths TSRMLS_CC);
add_assoc_zval_ex(result, ZEND_STRS("paths"), paths);
} else {
zval *paths;
MAKE_STD_ZVAL(paths);
array_init(paths);
add_assoc_zval_ex(result, ZEND_STRS("paths"), paths);
}
if (opts->baseline != NULL) {
// git_tree
} else {
add_assoc_null_ex(result, ZEND_STRS("baseline"));
}
add_assoc_string_ex(result, ZEND_STRS("target_directory"), (opts->target_directory) ? opts->target_directory : "", 1);
add_assoc_string_ex(result, ZEND_STRS("our_label"), (opts->our_label) ? opts->our_label : "", 1);
add_assoc_string_ex(result, ZEND_STRS("their_label"), (opts->their_label) ? opts->their_label : "", 1);
*out = result;
}
void php_git_git_checkout_opts_free(git_checkout_opts *target TSRMLS_DC)
{
php_git2_cb_t *tmp;
if (target->notify_payload) {
tmp = (php_git2_cb_t*)target->notify_payload;
if (tmp->fci) {
efree(tmp->fci);
}
if (tmp->fcc) {
efree(tmp->fcc);
}
efree(target->notify_payload);
}
if (target->progress_payload) {
tmp = (php_git2_cb_t*)target->progress_payload;
if (tmp->fci) {
efree(tmp->fci);
}
if (tmp->fcc) {
efree(tmp->fcc);
}
efree(target->progress_payload);
}
php_git2_strarray_free(&target->paths);
efree(target);
}
static int php_git2_git_checkout_notify_cb(git_checkout_notify_t why,
const char *path,
const git_diff_file *baseline,
const git_diff_file *target,
const git_diff_file *workdir,
void *payload)
{
/* TODO(chobie): implement callback */
}
void php_git2_git_checkout_progress_cb(const char *path,
size_t completed_steps,
size_t total_steps,
void *payload)
{
zval *param_path, *param_completed_steps, *param_total_steps, *retval_ptr = NULL;
php_git2_cb_t *p = (php_git2_cb_t*)payload;
GIT2_TSRMLS_SET(p->tsrm_ls);
Z_ADDREF_P(p->payload);
MAKE_STD_ZVAL(param_path);
MAKE_STD_ZVAL(param_completed_steps);
MAKE_STD_ZVAL(param_total_steps);
ZVAL_NULL(param_path);
if (path != NULL) {
ZVAL_STRING(param_path, path, 1);
}
ZVAL_LONG(param_completed_steps, completed_steps);
ZVAL_LONG(param_total_steps, total_steps);
SEPARATE_ZVAL_TO_MAKE_IS_REF(&p->payload);
if (php_git2_call_function_v(p->fci, p->fcc TSRMLS_CC, &retval_ptr, 4, ¶m_path, ¶m_completed_steps, ¶m_total_steps, &p->payload)) {
return;
}
zval_ptr_dtor(&retval_ptr);
}
void php_git2_fcall_info_wrapper(zval *target, zend_fcall_info **out_fci, zend_fcall_info_cache **out_fcc TSRMLS_DC)
{
char *is_callable_error = NULL;
zend_fcall_info *fci = NULL;
zend_fcall_info_cache *fcc = NULL;
fci = (zend_fcall_info*)emalloc(sizeof(zend_fcall_info));
fcc = (zend_fcall_info_cache*)emalloc(sizeof(zend_fcall_info_cache));
memcpy(fci, &empty_fcall_info, sizeof(zend_fcall_info));
memcpy(fcc, &empty_fcall_info_cache, sizeof(zend_fcall_info_cache));
if (zend_fcall_info_init(target, 0, fci, fcc, NULL, &is_callable_error TSRMLS_CC) == SUCCESS) {
if (is_callable_error) {
efree(is_callable_error);
}
} else {
fprintf(stderr, "FAILED");
efree(fci);
efree(fcc);
return;
}
*out_fci = fci;
*out_fcc = fcc;
}
void php_git2_fcall_info_wrapper2(zval *target, zend_fcall_info *fci, zend_fcall_info_cache *fcc TSRMLS_DC)
{
char *is_callable_error = NULL;
memcpy(fci, &empty_fcall_info, sizeof(zend_fcall_info));
memcpy(fcc, &empty_fcall_info_cache, sizeof(zend_fcall_info_cache));
if (zend_fcall_info_init(target, 0, fci, fcc, NULL, &is_callable_error TSRMLS_CC) == SUCCESS) {
if (is_callable_error) {
efree(is_callable_error);
}
} else {
fprintf(stderr, "FAILED");
efree(fci);
efree(fcc);
return;
}
}
int php_git2_array_to_git_checkout_opts(git_checkout_opts **out, zval *array TSRMLS_DC)
{
const char *target_directory, *our_label, *their_label;
git_checkout_opts *opts = NULL, def = GIT_CHECKOUT_OPTS_INIT;
php_git2_cb_t *notify_payload = NULL, *progress_payload= NULL;
zval *notify_cb = NULL, *progress_cb = NULL;
opts = (git_checkout_opts*)emalloc(sizeof(struct git_checkout_opts));
memcpy(opts, &def, sizeof(git_checkout_opts));
notify_cb = php_git2_read_arrval(array, ZEND_STRS("notify_cb") TSRMLS_CC);
progress_cb = php_git2_read_arrval(array, ZEND_STRS("progress_cb") TSRMLS_CC);
opts->notify_cb = php_git2_git_checkout_notify_cb;
opts->progress_cb = php_git2_git_checkout_progress_cb;
opts->version = php_git2_read_arrval_long(array, ZEND_STRS("version") TSRMLS_CC);
opts->checkout_strategy = php_git2_read_arrval_long(array, ZEND_STRS("checkout_strategy") TSRMLS_CC);
opts->disable_filters = php_git2_read_arrval_long(array, ZEND_STRS("disable_filters") TSRMLS_CC);
opts->dir_mode = php_git2_read_arrval_long(array, ZEND_STRS("dir_mode") TSRMLS_CC);
opts->file_mode = php_git2_read_arrval_long(array, ZEND_STRS("file_mode") TSRMLS_CC);
opts->file_open_flags = php_git2_read_arrval_long(array, ZEND_STRS("file_open_flags") TSRMLS_CC);
opts->notify_flags = php_git2_read_arrval_long(array, ZEND_STRS("notify_flags") TSRMLS_CC);
//notify_cb
if (Z_TYPE_P(notify_cb) != IS_NULL) {
zend_fcall_info *fci;
zend_fcall_info_cache *fcc;
php_git2_fcall_info_wrapper(notify_cb, &fci, &fcc TSRMLS_CC);
if (php_git2_cb_init(¬ify_payload, fci, fcc,
php_git2_read_arrval(array, ZEND_STRS("notify_payload") TSRMLS_CC) TSRMLS_CC)) {
}
opts->notify_payload = notify_payload;
} else {
opts->notify_cb = NULL;
}
//progress_cb
if (Z_TYPE_P(progress_cb) != IS_NULL) {
zend_fcall_info *fci;
zend_fcall_info_cache *fcc;
php_git2_fcall_info_wrapper(progress_cb, &fci, &fcc TSRMLS_CC);
if (php_git2_cb_init(&progress_payload, fci, fcc,
php_git2_read_arrval(array, ZEND_STRS("progress_payload") TSRMLS_CC) TSRMLS_CC)) {
}
opts->progress_payload = progress_payload;
} else {
opts->progress_cb = NULL;
}
php_git2_array_to_strarray(&opts->paths, php_git2_read_arrval(array, ZEND_STRS("paths") TSRMLS_CC) TSRMLS_CC);
// TODO: assign baseline(git_tree)
target_directory = php_git2_read_arrval_string(array, ZEND_STRS("target_directory") TSRMLS_CC);
our_label = php_git2_read_arrval_string(array, ZEND_STRS("our_label") TSRMLS_CC);
their_label = php_git2_read_arrval_string(array, ZEND_STRS("their_label") TSRMLS_CC);
opts->target_directory = target_directory;
opts->our_label = our_label;
opts->their_label = their_label;
*out = opts;
return 0;
}
int php_git2_multi_cb_init(php_git2_multi_cb_t **out, void *payload TSRMLS_DC, int num_callbacks, ...)
{
php_git2_multi_cb_t *cb;
int i = 0;
va_list ap;
cb = (struct php_git2_multi_cb_t*)emalloc(sizeof(php_git2_multi_cb_t));
if (cb == NULL) {
return 1;
}
cb->payload = payload;
cb->num_callbacks = num_callbacks;
GIT2_TSRMLS_SET2(cb, TSRMLS_C);
cb->callbacks = emalloc(sizeof(php_git2_fcall_t) * num_callbacks);
memset(cb->callbacks, '\0', sizeof(php_git2_fcall_t) * num_callbacks);
va_start(ap, num_callbacks * 2);
for (i = 0; i < num_callbacks; i++) {
memcpy(&cb->callbacks[i].fci, va_arg(ap, zend_fcall_info*), sizeof(zend_fcall_info));
memcpy(&cb->callbacks[i].fcc, va_arg(ap, zend_fcall_info_cache*), sizeof(zend_fcall_info_cache));
}
va_end(ap);
*out = cb;
return 0;
}
void php_git2_multi_cb_free(php_git2_multi_cb_t *target)
{
efree(target->callbacks);
efree(target);
}
void php_git2_diff_line_to_array(git_diff_line *line, zval **out TSRMLS_DC)
{
zval *result;
MAKE_STD_ZVAL(result);
array_init(result);
add_assoc_stringl_ex(result, ZEND_STRS("origin"), &line->origin, 1, 1);
add_assoc_long_ex(result, ZEND_STRS("old_lineno"), line->old_lineno);
add_assoc_long_ex(result, ZEND_STRS("new_lineno"), line->new_lineno);
add_assoc_long_ex(result, ZEND_STRS("num_lines"), line->num_lines);
add_assoc_long_ex(result, ZEND_STRS("content_len"), line->content_len);
add_assoc_long_ex(result, ZEND_STRS("content_offset"), line->content_offset);
add_assoc_stringl_ex(result, ZEND_STRS("content"), line->content, line->content_len, 1);
*out = result;
}
void php_git2_diff_hunk_to_array(git_diff_hunk *hunk, zval **out TSRMLS_DC)
{
zval *result;
MAKE_STD_ZVAL(result);
if (hunk == NULL) {
ZVAL_NULL(result);
} else {
array_init(result);
add_assoc_long_ex(result, ZEND_STRS("old_start"), hunk->old_start);
add_assoc_long_ex(result, ZEND_STRS("old_lines"), hunk->old_lines);
add_assoc_long_ex(result, ZEND_STRS("new_start"), hunk->new_start);
add_assoc_long_ex(result, ZEND_STRS("new_lines"), hunk->new_lines);
add_assoc_stringl_ex(result, ZEND_STRS("header"), hunk->header, 128, 1);
}
*out = result;
}
void php_git2_diff_file_to_array(git_diff_file *file, zval **out TSRMLS_DC)
{
zval *result;
char buf[41] = {0};
MAKE_STD_ZVAL(result);
array_init(result);
git_oid_fmt(buf, &file->oid);
add_assoc_string_ex(result, ZEND_STRS("oid"), buf, 1);
add_assoc_string_ex(result, ZEND_STRS("path"), file->path, 1);
add_assoc_long_ex(result, ZEND_STRS("size"), file->size);
add_assoc_long_ex(result, ZEND_STRS("flags"), file->flags);
add_assoc_long_ex(result, ZEND_STRS("mode"), file->mode);
*out = result;
}
void php_git2_diff_delta_to_array(git_diff_delta *delta, zval **out TSRMLS_DC)
{
zval *result, *old, *new;
MAKE_STD_ZVAL(result);
array_init(result);
add_assoc_long_ex(result, ZEND_STRS("status"), delta->status);
add_assoc_long_ex(result, ZEND_STRS("flags"), delta->flags);
add_assoc_long_ex(result, ZEND_STRS("similarity"), delta->similarity);
add_assoc_long_ex(result, ZEND_STRS("nfiles"), delta->nfiles);
php_git2_diff_file_to_array(&delta->old_file, &old TSRMLS_CC);
php_git2_diff_file_to_array(&delta->new_file, &new TSRMLS_CC);
add_assoc_zval_ex(result, ZEND_STRS("old_file"), old);
add_assoc_zval_ex(result, ZEND_STRS("new_file"), new);
*out = result;
}
void php_git2_array_to_git_diff_options(git_diff_options *options, zval *array TSRMLS_DC)
{
git_diff_options_init(options, GIT_DIFF_OPTIONS_VERSION);
options->version = php_git2_read_arrval_long(array, ZEND_STRS("version") TSRMLS_CC);
options->flags = php_git2_read_arrval_long(array, ZEND_STRS("flags") TSRMLS_CC);
options->ignore_submodules = php_git2_read_arrval_long(array, ZEND_STRS("ignore_submodules") TSRMLS_CC);
php_git2_array_to_strarray(&options->pathspec, php_git2_read_arrval(array, ZEND_STRS("pathspec") TSRMLS_CC) TSRMLS_CC);
// TODO(chobie): support notify cb
options->context_lines = php_git2_read_arrval_long(array, ZEND_STRS("context_lines") TSRMLS_CC);
options->interhunk_lines = php_git2_read_arrval_long(array, ZEND_STRS("interhunk_lines") TSRMLS_CC);
options->oid_abbrev = php_git2_read_arrval_long(array, ZEND_STRS("oid_abbrev") TSRMLS_CC);
options->max_size = php_git2_read_arrval_long(array, ZEND_STRS("max_size") TSRMLS_CC);
options->old_prefix = php_git2_read_arrval_string(array, ZEND_STRS("old_prefix") TSRMLS_CC);
options->new_prefix = php_git2_read_arrval_string(array, ZEND_STRS("new_prefix") TSRMLS_CC);
}
void php_git2_git_diff_options_free(git_diff_options *options)
{
if (options->pathspec.count > 0) {
efree(options->pathspec.strings);
}
}
void php_git2_git_diff_options_to_array(git_diff_options *options, zval **out TSRMLS_DC)
{
zval *result, *pathspec;
MAKE_STD_ZVAL(result);
array_init(result);
add_assoc_long_ex(result, ZEND_STRS("version"), options->version);
add_assoc_long_ex(result, ZEND_STRS("flags"), options->flags);
add_assoc_long_ex(result, ZEND_STRS("ignore_submodules"), options->ignore_submodules);
MAKE_STD_ZVAL(pathspec);
array_init(pathspec);
if (options->pathspec.count > 0) {
} else {
add_assoc_zval_ex(result, ZEND_STRS("pathspec"), pathspec);
}
if (options->notify_cb) {
} else {
add_assoc_null_ex(result, ZEND_STRS("notify_cb"));
}
add_assoc_long_ex(result, ZEND_STRS("context_lines"), options->context_lines);
add_assoc_long_ex(result, ZEND_STRS("interhunk_lines"), options->interhunk_lines);
add_assoc_long_ex(result, ZEND_STRS("oid_abbrev"), options->oid_abbrev);
add_assoc_long_ex(result, ZEND_STRS("max_size"), options->max_size);
if (options->notify_payload) {
} else {
add_assoc_null_ex(result, ZEND_STRS("notify_payload"));
}
if (options->old_prefix) {
add_assoc_string_ex(result, ZEND_STRS("old_prefix"), options->old_prefix, 1);
} else {
add_assoc_null_ex(result, ZEND_STRS("old_prefix"));
}
if (options->new_prefix) {
add_assoc_string_ex(result, ZEND_STRS("new_prefix"), options->new_prefix, 1);
} else {
add_assoc_null_ex(result, ZEND_STRS("new_prefix"));
}
*out = result;
}
int php_git2_git_diff_file_cb(
const git_diff_delta *delta,
float progress,
void *payload)
{
zval *param_delta = NULL, *param_progress = NULL, *retval_ptr = NULL;
php_git2_multi_cb_t *p = (php_git2_multi_cb_t*)payload;
int retval = 0;
GIT2_TSRMLS_SET(p->tsrm_ls)
Z_ADDREF_P(p->payload);
MAKE_STD_ZVAL(param_progress);
ZVAL_DOUBLE(param_progress, progress);
php_git2_diff_delta_to_array(delta, ¶m_delta TSRMLS_CC);
if (php_git2_call_function_v(&p->callbacks[0].fci, &p->callbacks[0].fcc TSRMLS_CC, &retval_ptr, 3, ¶m_delta, ¶m_progress, &p->payload)) {
return GIT_EUSER;
}
retval = Z_LVAL_P(retval_ptr);
zval_ptr_dtor(&retval_ptr);
return retval;
}
int php_git2_git_diff_hunk_cb(
const git_diff_delta *delta,
const git_diff_hunk *hunk,
void *payload)
{
zval *param_delta = NULL, *param_hunk = NULL, *retval_ptr = NULL;
php_git2_multi_cb_t *p = (php_git2_multi_cb_t*)payload;
int retval = 0;
GIT2_TSRMLS_SET(p->tsrm_ls)
Z_ADDREF_P(p->payload);
php_git2_diff_delta_to_array(delta, ¶m_delta TSRMLS_CC);
php_git2_diff_hunk_to_array(hunk, ¶m_hunk TSRMLS_CC);
if (php_git2_call_function_v(&p->callbacks[1].fci, &p->callbacks[1].fcc TSRMLS_CC, &retval_ptr, 3, ¶m_delta, ¶m_hunk, &p->payload)) {
return GIT_EUSER;
}
retval = Z_LVAL_P(retval_ptr);
zval_ptr_dtor(&retval_ptr);
return retval;
}
int php_git2_git_diff_line_cb(
const git_diff_delta *delta,
const git_diff_hunk *hunk,
const git_diff_line *line,
void *payload) {
zval *param_delta = NULL, *param_hunk = NULL, *param_line = NULL, *retval_ptr = NULL;
php_git2_multi_cb_t *p = (php_git2_multi_cb_t*)payload;
int retval = 0;
GIT2_TSRMLS_SET(p->tsrm_ls)
Z_ADDREF_P(p->payload);
php_git2_diff_delta_to_array(delta, ¶m_delta TSRMLS_CC);
php_git2_diff_hunk_to_array(hunk, ¶m_hunk TSRMLS_CC);
php_git2_diff_line_to_array(line, ¶m_line TSRMLS_CC);
if (php_git2_call_function_v(&p->callbacks[2].fci, &p->callbacks[2].fcc TSRMLS_CC, &retval_ptr, 4, ¶m_delta, ¶m_hunk, ¶m_line, &p->payload)) {
return GIT_EUSER;
}
retval = Z_LVAL_P(retval_ptr);
zval_ptr_dtor(&retval_ptr);
return retval;
}
void php_git2_git_transfer_progress_to_array(git_transfer_progress *progress, zval **out TSRMLS_DC)
{
zval *result;
MAKE_STD_ZVAL(result);
array_init(result);
add_assoc_long_ex(result, ZEND_STRS("total_objects"), progress->total_objects);
add_assoc_long_ex(result, ZEND_STRS("indexed_objects"), progress->indexed_objects);
add_assoc_long_ex(result, ZEND_STRS("received_objects"), progress->received_objects);
add_assoc_long_ex(result, ZEND_STRS("local_objects"), progress->local_objects);
add_assoc_long_ex(result, ZEND_STRS("total_deltas"), progress->total_deltas);
add_assoc_long_ex(result, ZEND_STRS("indexed_deltas"), progress->indexed_deltas);
add_assoc_long_ex(result, ZEND_STRS("received_bytes"), progress->received_bytes);
*out = result;
}