forked from RogerGee/php-git2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php-callback.h
855 lines (734 loc) · 23.3 KB
/
php-callback.h
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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
/*
* php-callback.h
*
* Copyright (C) Roger P. Gee
*/
#ifndef PHPGIT2_PHP_CALLBACK_H
#define PHPGIT2_PHP_CALLBACK_H
#include "php-type.h"
namespace php_git2
{
int php_git2_invoke_callback(
zval* obj,
zval* func,
zval* ret,
int paramCount,
zval params[]);
// Provide a type that contains an array of zvals converted from primative
// values.
template<unsigned Count>
class zval_array
{
public:
zval_array()
{
for (unsigned i = 0;i < Count;++i) {
ZVAL_NULL(params + i);
}
}
~zval_array()
{
for (unsigned i = 0;i < Count;++i) {
zval_ptr_dtor(params + i);
}
}
// Create a member function for assigning to the zvals from primative
// values. The assignment may be recursive to allow multiple
// assignments. We create partial specializations for the kinds of
// values we are interested in.
template<unsigned I>
void assign()
{
// The base case does nothing.
}
template<unsigned I,typename... Ts>
void assign(long h,Ts&&... ts)
{
ZVAL_LONG(params + I,static_cast<zend_long>(h));
assign<I+1>(std::forward<Ts>(ts)...);
}
template<unsigned I,typename... Ts>
void assign(int h,Ts&&... ts)
{
ZVAL_LONG(params + I,static_cast<zend_long>(h));
assign<I+1>(std::forward<Ts>(ts)...);
}
template<unsigned I,typename... Ts>
void assign(unsigned int h,Ts&&... ts)
{
ZVAL_LONG(params + I,static_cast<zend_long>(h));
assign<I+1>(std::forward<Ts>(ts)...);
}
template<unsigned I,typename... Ts>
void assign(size_t sz,Ts&&... ts)
{
ZVAL_LONG(params + I,static_cast<zend_long>(sz));
assign<I+1>(std::forward<Ts>(ts)...);
}
template<unsigned I,typename... Ts>
void assign(double h,Ts&&... ts)
{
ZVAL_DOUBLE(params + I,h);
assign<I+1>(std::forward<Ts>(ts)...);
}
template<unsigned I,typename... Ts>
void assign(bool b,Ts&&... ts)
{
ZVAL_BOOL(params + I,b);
assign<I+1>(std::forward<Ts>(ts)...);
}
template<unsigned I,typename... Ts>
void assign(const char* str,Ts&&... ts)
{
if (str == nullptr) {
ZVAL_NULL(params + I);
}
else {
ZVAL_STRING(params + I,str);
}
assign<I+1>(std::forward<Ts>(ts)...);
}
template<unsigned I,typename... Ts>
void assign(const void* a,size_t& b,Ts&&... ts)
{
ZVAL_STRINGL(params + I,(const char*)a,b);
assign<I+1>(std::forward<Ts>(ts)...);
}
template<unsigned I,typename... Ts>
void assign(zval* h,Ts&&... ts)
{
if (h != nullptr) {
zval_ptr_dtor(params + I);
ZVAL_COPY(params + I,h);
}
assign<I+1>(std::forward<Ts>(ts)...);
}
int call(zval* func,zval* ret)
{
return php_git2_invoke_callback(nullptr,func,ret,Count,params);
}
int call(zval* obj,zval* func,zval* ret)
{
return php_git2_invoke_callback(obj,func,ret,Count,params);
}
void make_ref(unsigned index)
{
ZVAL_MAKE_REF(¶ms[index]);
}
void unref(unsigned index)
{
ZVAL_UNREF(¶ms[index]);
}
zval* operator [](unsigned index)
{
return ¶ms[index];
}
const zval* operator [](unsigned index) const
{
return params + index;
}
private:
zval params[Count];
};
/**
* Synchronous Callbacks
*
* A synchronous callback is one that is executed by libgit2 within a single
* library call. As such, we can allocate the callback data on the
* stack. The object holds two zvals: one to represent the PHP userspace
* function (i.e. a callable) and another to represent any payload
* data. Payload data may be omitted if desired.
*/
class php_callback_base:
public php_value_base
{
public:
php_callback_base();
~php_callback_base();
void* byval_git2()
{
return this;
}
zval* get_payload()
{
return &data;
}
void set_members(zval* zvpFunc,zval* zvpPayload)
{
parse_with_context(zvpFunc,"callback");
parse_with_context(zvpPayload,"payload");
}
protected:
virtual void parse_impl(zval* zvp,int argno);
// NOTE: Callable is stored in 'value' member from base class.
zval data; // payload
};
using php_callback_sync = php_callback_base;
// Provide a variant that allows a NULL callback.
class php_callback_sync_nullable:
public php_callback_sync
{
private:
virtual void parse_impl(zval* zvp,int argno);
};
/**
* Asynchronous Callbacks
*
* An asynchronous callback is a wrapper that allows a callback to persist
* so that it can be executed out-of-line. We implement this by allocating a
* php_callback_sync instance dynamically. This means that the async
* callback object (which is allocated on the stack) goes away while the
* sync callback object persists. To avoid leaks during the lifetime of the
* request, the async object is also a connector type so users can attach an
* asynchronous callback object to a resource or object. In this way the
* lifetime of the callback is the same as the lifetime of the
* resource/object. The user must free the structure by manually calling its
* destructor and then calling efree() on the structure.
*/
template<typename CallbackSyncType = php_callback_sync>
class php_callback_async_base:
public php_value_base
{
public:
php_callback_base* get_callback() const
{
return cb;
}
void* byval_git2()
{
return cb->byval_git2();
}
protected:
CallbackSyncType* cb;
private:
virtual void parse_impl(zval* zvp,int argno)
{
return cb->parse(zvp,argno);
}
};
template<typename GitResource,typename CallbackSyncType = php_callback_sync>
class php_callback_async:
public php_callback_async_base<CallbackSyncType>
{
public:
// Connect to an arbitrary resource type. It must be a newly created
// resource (i.e. resource ref).
using connect_t = php_resource_ref<GitResource>;
using target_t = void*;
php_callback_async(connect_t& conn)
{
// Allocate php_callback_sync object.
cb = new (emalloc(sizeof(CallbackSyncType))) CallbackSyncType;
// Assign php_callback_sync object to resource object. It must have
// a member called 'cb' to which we have access.
conn.get_object()->cb = cb;
}
private:
using php_callback_async_base<CallbackSyncType>::cb;
};
// This alternate form of php_callback_async works for a non-resource
// type. This means it doesn't have to perform a lookup on an underlying
// resource object. Instead it uses the connected object directly.
template<typename ConnectType,typename CallbackSyncType = php_callback_sync>
class php_callback_async_ex:
public php_callback_async_base<CallbackSyncType>
{
public:
using connect_t = ConnectType;
using target_t = void*;
php_callback_async_ex(connect_t& conn)
{
// Allocate CallbackSyncType object. Assign a new CallbackSyncType
// object to the connected object. It must have a member called 'cb'
// to which we have access.
cb = new (emalloc(sizeof(CallbackSyncType))) CallbackSyncType;
conn.cb = cb;
}
private:
using php_callback_async_base<CallbackSyncType>::cb;
};
// This alternate form of php_callback_async writes the callback to the
// resource object at the end of its lifetime. This allows assignment to an
// existing resource.
template<typename GitResource,typename CallbackSyncType = php_callback_sync>
class php_callback_async_existing:
public php_callback_async_base<CallbackSyncType>
{
public:
// Connect to an arbitrary resource type.
using connect_t = php_resource<GitResource>;
typedef void* target_t;
php_callback_async_existing(connect_t& conn):
stor(conn)
{
// Allocate CallbackSyncType object.
cb = new (emalloc(sizeof(CallbackSyncType))) CallbackSyncType;
}
~php_callback_async_existing()
{
// Assign CallbackSyncType object to resource object. It must have
// a member called 'cb' to which we have access. This is run here in
// the destructor to allow the resource wrapper's zval to get
// assigned properly so we can access the GitResource.
GitResource* rsrc = stor.get_object();
if (rsrc->cb == nullptr) {
rsrc->cb = cb;
}
}
private:
connect_t& stor;
using php_callback_async_base<CallbackSyncType>::cb;
};
// Define a type to represent a callback function handler. All this does is
// forward the function address to a git2 call.
template<typename CallbackFunc>
class php_callback_handler
{
public:
constexpr typename CallbackFunc::type byval_git2() const
{
// Return the static address of the wrapped callback function.
return &CallbackFunc::callback;
}
};
// Create connector types that allows callback handlers to be nullable. The
// connector type connects to an object that accesses a php_callback_base
// object (either directly or indirectly).
template<typename CallbackFunc>
class php_callback_handler_nullable_connector:
public php_callback_handler<CallbackFunc>
{
public:
using connect_t = php_callback_base;
using target_t = typename CallbackFunc::type;
php_callback_handler_nullable_connector(connect_t& obj):
conn(obj)
{
}
typename CallbackFunc::type byval_git2()
{
// Return null if the callback zval is NULL.
if (Z_TYPE_P(conn.get_value()) == IS_NULL) {
return nullptr;
}
return php_callback_handler<CallbackFunc>::byval_git2();
}
private:
connect_t& conn;
};
template<typename CallbackFunc,typename CallbackAsyncType>
class php_callback_handler_nullable_connector_async:
public php_callback_handler<CallbackFunc>
{
public:
using connect_t = CallbackAsyncType;
using target_t = typename CallbackFunc::type;
php_callback_handler_nullable_connector_async(connect_t& obj):
conn(obj)
{
}
typename CallbackFunc::type byval_git2()
{
php_callback_base* cb = conn.get_callback();
// Return null if the callback zval is NULL.
if (Z_TYPE_P(cb->get_value()) == IS_NULL) {
return nullptr;
}
return php_callback_handler<CallbackFunc>::byval_git2();
}
private:
connect_t& conn;
};
// Here we define all the callbacks used by the library. Some are specific
// to a particular operation whereas others are more generic.
struct packbuilder_foreach_callback
{
typedef int (*type)(void*,size_t,void*);
static int callback(void* buf,size_t size,void* payload);
};
struct transfer_progress_callback
{
typedef int (*type)(const git_transfer_progress* stats,void* payload);
static int callback(const git_transfer_progress* stats,void* payload);
};
struct odb_foreach_callback
{
typedef int (*type)(const git_oid* oid,void* payload);
static int callback(const git_oid* oid,void* payload);
};
struct treewalk_callback
{
typedef int (*type)(const char* root,const git_tree_entry* entry,void* payload);
static int callback(const char* root,const git_tree_entry* entry,void* payload);
};
struct commit_parent_callback
{
// Create a sync callback derivative for storing a git_oid to use as the
// callback return value. This will be allocated on the stack for the
// duration of calls to the callback.
struct sync_callback:
php_callback_sync
{
git_oid oidbuf;
};
typedef const git_oid* (*type)(size_t idx,void* payload);
static const git_oid* callback(size_t idx,void* payload);
};
struct reference_foreach_callback
{
typedef git_reference_foreach_cb type;
static int callback(git_reference* ref,void* payload);
};
struct reference_foreach_name_callback
{
typedef git_reference_foreach_name_cb type;
static int callback(const char* name,void* payload);
};
struct packbuilder_progress_callback
{
typedef int (*type)(int stage,uint32_t current,uint32_t total,void* payload);
static int callback(int stage,uint32_t current,uint32_t total,void* payload);
};
struct config_foreach_callback
{
typedef git_config_foreach_cb type;
static int callback(const git_config_entry* entry,void* payload);
};
struct tag_foreach_callback
{
typedef git_tag_foreach_cb type;
static int callback(const char* name,git_oid* oid,void* payload);
};
struct repository_create_callback
{
typedef git_repository_create_cb type;
static int callback(git_repository** out,const char* path,int bare,void* payload);
};
struct checkout_progress_callback
{
typedef git_checkout_progress_cb type;
static void callback(
const char* path,
size_t completed_steps,
size_t total_steps,
void* payload);
};
struct git_diff_options_callback_info
{
php_callback_sync notifyCallback;
php_callback_sync progressCallback;
};
struct diff_notify_callback
{
typedef git_diff_notify_cb type;
static int callback(
const git_diff* diff_so_far,
const git_diff_delta* delta_to_add,
const char* matched_pathspec,
void* payload /* git_diff_options_callback_info */);
};
struct diff_progress_callback
{
typedef git_diff_progress_cb type;
static int callback(
const git_diff* diff_so_far,
const char* old_path,
const char* new_path,
void* payload /* git_diff_options_callback_info */);
};
struct git_diff_callback_info
{
git_diff_callback_info(zval* zvp):
zpayload(zvp)
{
}
php_callback_sync* fileCallback;
php_callback_sync* binaryCallback;
php_callback_sync* hunkCallback;
php_callback_sync* lineCallback;
zval* zpayload;
};
struct diff_file_callback
{
typedef git_diff_file_cb type;
static int callback(const git_diff_delta* delta,float progress,void* payload);
};
struct diff_binary_callback
{
typedef git_diff_binary_cb type;
static int callback(
const git_diff_delta* delta,
const git_diff_binary* binary,
void* payload);
};
struct diff_hunk_callback
{
typedef git_diff_hunk_cb type;
static int callback(
const git_diff_delta* delta,
const git_diff_hunk* hunk,
void* payload);
};
struct diff_line_callback
{
typedef git_diff_line_cb type;
static int callback(
const git_diff_delta* delta,
const git_diff_hunk* hunk,
const git_diff_line* line,
void* payload);
};
struct index_matched_path_callback
{
typedef git_index_matched_path_cb type;
static int callback(
const char* path,
const char* matched_pathspec,
void* payload);
};
struct revwalk_hide_callback
{
typedef git_revwalk_hide_cb type;
static int callback(
const git_oid* commit_id,
void* payload);
};
struct trace_callback
{
typedef git_trace_callback type;
static void callback(
git_trace_level_t level,
const char* msg);
};
struct attr_foreach_callback
{
typedef git_attr_foreach_cb type;
static int callback(
const char* name,
const char* value,
void* payload);
};
struct status_callback
{
typedef git_status_cb type;
static int callback(
const char* path,
unsigned int status_flags,
void* payload);
};
struct note_foreach_callback
{
typedef git_note_foreach_cb type;
static int callback(
const git_oid* blob_id,
const git_oid* annotated_object_id,
void* payload);
};
struct stash_callback
{
typedef git_stash_cb type;
static int callback(
size_t index,
const char* message,
const git_oid* stash_id,
void* payload);
};
struct stash_apply_progress_callback
{
typedef git_stash_apply_progress_cb type;
static int callback(
git_stash_apply_progress_t progress,
void* payload);
};
struct cred_acquire_callback
{
typedef git_cred_acquire_cb type;
static int callback(
git_cred** cred,
const char* url,
const char* username_from_url,
unsigned int allowed_types,
void* payload);
};
struct transport_certificate_check_callback
{
typedef git_transport_certificate_check_cb type;
static int callback(
git_cert* cert,
int valid,
const char* host,
void* payload);
};
struct git_remote_callbacks_info
{
php_callback_sync transportMessageCallback;
php_callback_sync completionCallback;
php_callback_sync credAcquireCallback;
php_callback_sync transportCertificateCheckCallback;
php_callback_sync transferProgressCallback;
php_callback_sync updateTipsCallback;
php_callback_sync packbuilderProgressCallback;
php_callback_sync pushTransferProgressCallback;
php_callback_sync pushUpdateReferenceCallback;
php_callback_sync pushNegotiationCallback;
//php_callback_sync transportCallback;
};
struct remote_transport_message_callback
{
typedef git_transport_message_cb type;
static int callback(
const char* str,
int len,
void* payload);
};
struct remote_completion_callback
{
typedef int (*type)(git_remote_completion_type,void*);
static int callback(
git_remote_completion_type type,
void* payload);
};
struct remote_cred_acquire_callback
{
typedef git_cred_acquire_cb type;
static int callback(
git_cred** cred,
const char* url,
const char* username_from_url,
unsigned int allowed_types,
void* payload);
};
struct remote_transport_certificate_check_callback
{
typedef git_transport_certificate_check_cb type;
static int callback(
git_cert* cert,
int valid,
const char* host,
void* payload);
};
struct remote_transfer_progress_callback
{
typedef git_transfer_progress_cb type;
static int callback(
const git_transfer_progress* stats,
void* payload);
};
struct remote_update_tips_callback
{
typedef int (*type)(const char*,const git_oid*,const git_oid*,void*);
static int callback(
const char* refname,
const git_oid* a,
const git_oid* b,
void* payload);
};
struct remote_packbuilder_progress_callback
{
typedef git_packbuilder_progress type;
static int callback(
int stage,
uint32_t current,
uint32_t total,
void* payload);
};
struct remote_push_transfer_progress_callback
{
typedef git_push_transfer_progress type;
static int callback(
unsigned int current,
unsigned int total,
size_t bytes,
void* payload);
};
struct remote_push_update_reference_callback
{
typedef int (*type)(const char*,const char*,void*);
static int callback(
const char* refname,
const char* status,
void* payload);
};
struct remote_push_negotiation_callback
{
typedef git_push_negotiation type;
static int callback(
const git_push_update** updates,
size_t len,
void *payload);
};
struct git_proxy_callbacks_info
{
php_callback_sync credAcquireCallback;
php_callback_sync transportCertificateCheckCallback;
};
struct proxy_cred_acquire_callback
{
typedef git_cred_acquire_cb type;
static int callback(
git_cred** cred,
const char* url,
const char* username_from_url,
unsigned int allowed_types,
void* payload);
};
struct proxy_transport_certificate_check_callback
{
typedef git_transport_certificate_check_cb type;
static int callback(
git_cert* cert,
int valid,
const char* host,
void* payload);
};
struct remote_create_callback
{
typedef git_remote_create_cb type;
static int callback(
git_remote** out,
git_repository* repo,
const char* name,
const char* url,
void* payload);
};
struct repository_fetchhead_foreach_callback
{
typedef git_repository_fetchhead_foreach_cb type;
static int callback(
const char* ref_name,
const char* remote_url,
const git_oid* oid,
unsigned int is_merge,
void* payload);
};
struct repository_mergehead_foreach_callback
{
typedef git_repository_mergehead_foreach_cb type;
static int callback(
const git_oid* oid,
void* payload);
};
struct treebuilder_filter_callback
{
typedef git_treebuilder_filter_cb type;
static int callback(
const git_tree_entry* entry,
void* payload);
};
struct submodule_foreach_callback
{
typedef git_submodule_cb type;
static int callback(
git_submodule* sm,
const char* name,
void* payload);
};
} // namespace php_git2
#endif
/*
* Local Variables:
* mode:c++
* indent-tabs-mode:nil
* tab-width:4
* End:
*/