Skip to content

Commit ed1e8cf

Browse files
committed
[repository] add callback
1 parent ba610c6 commit ed1e8cf

File tree

2 files changed

+103
-27
lines changed

2 files changed

+103
-27
lines changed

repository.c

+101-25
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,72 @@
22
#include "php_git2_priv.h"
33
#include "repository.h"
44

5+
static int php_git2_repository_fetchhead_foreach_cb(const char *ref_name,
6+
const char *remote_url,
7+
const git_oid *oid,
8+
unsigned int is_merge,
9+
void *payload)
10+
{
11+
php_git2_t *result;
12+
zval *param_ref_name, *param_remote_url, *param_oid, *param_is_merge, *retval_ptr = NULL;
13+
php_git2_cb_t *p = (php_git2_cb_t*)payload;
14+
int i = 0;
15+
long retval = 0;
16+
char _oid[41] = {0};
17+
GIT2_TSRMLS_SET(p->tsrm_ls)
18+
19+
git_oid_fmt(_oid, oid);
20+
21+
Z_ADDREF_P(p->payload);
22+
MAKE_STD_ZVAL(param_ref_name);
23+
MAKE_STD_ZVAL(param_remote_url);
24+
MAKE_STD_ZVAL(param_oid);
25+
MAKE_STD_ZVAL(param_is_merge);
26+
ZVAL_STRING(param_ref_name, ref_name, 1);
27+
ZVAL_STRING(param_remote_url, remote_url, 1);
28+
ZVAL_STRING(param_oid, _oid, 1);
29+
ZVAL_BOOL(param_is_merge, is_merge);
30+
31+
if (php_git2_call_function_v(p->fci, p->fcc TSRMLS_CC, &retval_ptr, 5,
32+
&param_ref_name,
33+
&param_remote_url,
34+
&param_oid,
35+
&param_is_merge,
36+
&p->payload)) {
37+
return GIT_EUSER;
38+
}
39+
40+
retval = Z_LVAL_P(retval_ptr);
41+
zval_ptr_dtor(&retval_ptr);
42+
return retval;
43+
}
44+
45+
static int php_git2_repository_mergehead_foreach_cb(const git_oid *oid,
46+
void *payload)
47+
{
48+
php_git2_t *result;
49+
zval *param_oid, *retval_ptr = NULL;
50+
php_git2_cb_t *p = (php_git2_cb_t*)payload;
51+
int i = 0;
52+
long retval = 0;
53+
char _oid[41] = {0};
54+
GIT2_TSRMLS_SET(p->tsrm_ls)
55+
56+
git_oid_fmt(_oid, oid);
57+
58+
Z_ADDREF_P(p->payload);
59+
MAKE_STD_ZVAL(param_oid);
60+
ZVAL_STRING(param_oid, _oid, 1);
61+
62+
if (php_git2_call_function_v(p->fci, p->fcc TSRMLS_CC, &retval_ptr, 2, &param_oid, &p->payload)) {
63+
return GIT_EUSER;
64+
}
65+
66+
retval = Z_LVAL_P(retval_ptr);
67+
zval_ptr_dtor(&retval_ptr);
68+
return retval;
69+
}
70+
571
/* {{{ proto resource git_repository_new()
672
*/
773
PHP_FUNCTION(git_repository_new)
@@ -601,48 +667,58 @@ PHP_FUNCTION(git_repository_merge_cleanup)
601667
}
602668
/* }}} */
603669

604-
605-
/* {{{ proto long git_repository_fetchhead_foreach(repo, callback, payload)
606-
*/
670+
/* {{{ proto long git_repository_fetchhead_foreach(resource $repo, Callable $callback, $payload)
671+
*/
607672
PHP_FUNCTION(git_repository_fetchhead_foreach)
608673
{
609-
zval *repo;
610-
php_git2_t *_repo;
611-
zval *callback;
612-
php_git2_t *_callback;
613-
zval *payload;
614-
615-
/* TODO(chobie): implement this */
616-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "git_repository_fetchhead_foreach not implemented yet");
617-
return;
674+
int result = 0, error = 0;
675+
zval *repo = NULL, *callback = NULL, *payload = NULL;
676+
php_git2_t *_repo = NULL;
677+
zend_fcall_info fci = empty_fcall_info;
678+
zend_fcall_info_cache fcc = empty_fcall_info_cache;
679+
php_git2_cb_t *cb = NULL;
618680

619681
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
620-
"rrz", &repo, &callback, &payload) == FAILURE) {
682+
"rfz", &repo, &fci, &fcc, &payload) == FAILURE) {
621683
return;
622684
}
685+
623686
ZEND_FETCH_RESOURCE(_repo, php_git2_t*, &repo, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
687+
if (php_git2_cb_init(&cb, &fci, &fcc, payload TSRMLS_CC)) {
688+
RETURN_FALSE;
689+
}
690+
result = git_repository_fetchhead_foreach(PHP_GIT2_V(_repo, repository), php_git2_repository_fetchhead_foreach_cb, cb);
691+
php_git2_cb_free(cb);
692+
RETURN_LONG(result);
624693
}
694+
/* }}} */
625695

626-
/* {{{ proto long git_repository_mergehead_foreach(repo, callback, payload)
627-
*/
696+
/* {{{ proto long git_repository_mergehead_foreach(resource $repo, Callable $callback, $payload)
697+
*/
628698
PHP_FUNCTION(git_repository_mergehead_foreach)
629699
{
630-
zval *repo;
631-
php_git2_t *_repo;
632-
zval *callback;
633-
php_git2_t *_callback;
634-
zval *payload;
635-
636-
/* TODO(chobie): implement this */
637-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "git_repository_mergehead_foreach not implemented yet");
638-
return;
700+
int result = 0, error = 0;
701+
zval *repo = NULL, *callback = NULL, *payload = NULL;
702+
php_git2_t *_repo = NULL;
703+
zend_fcall_info fci = empty_fcall_info;
704+
zend_fcall_info_cache fcc = empty_fcall_info_cache;
705+
php_git2_cb_t *cb = NULL;
639706

640707
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
641-
"rrz", &repo, &callback, &payload) == FAILURE) {
708+
"rfz", &repo, &fci, &fcc, &payload) == FAILURE) {
642709
return;
643710
}
711+
644712
ZEND_FETCH_RESOURCE(_repo, php_git2_t*, &repo, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
713+
if (php_git2_cb_init(&cb, &fci, &fcc, payload TSRMLS_CC)) {
714+
RETURN_FALSE;
715+
}
716+
result = git_repository_mergehead_foreach(PHP_GIT2_V(_repo, repository), php_git2_repository_mergehead_foreach_cb, cb);
717+
php_git2_cb_free(cb);
718+
RETURN_LONG(result);
645719
}
720+
/* }}} */
721+
646722

647723
/* {{{ proto resource git_repository_hashfile(repo, path, type, as_path)
648724
*/

repository.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,13 @@ ZEND_END_ARG_INFO()
138138
ZEND_BEGIN_ARG_INFO_EX(arginfo_git_repository_fetchhead_foreach, 0, 0, 3)
139139
ZEND_ARG_INFO(0, repo)
140140
ZEND_ARG_INFO(0, callback)
141-
ZEND_ARG_INFO(0, payload)
141+
ZEND_ARG_INFO(1, payload)
142142
ZEND_END_ARG_INFO()
143143

144144
ZEND_BEGIN_ARG_INFO_EX(arginfo_git_repository_mergehead_foreach, 0, 0, 3)
145145
ZEND_ARG_INFO(0, repo)
146146
ZEND_ARG_INFO(0, callback)
147-
ZEND_ARG_INFO(0, payload)
147+
ZEND_ARG_INFO(1, payload)
148148
ZEND_END_ARG_INFO()
149149

150150
ZEND_BEGIN_ARG_INFO_EX(arginfo_git_repository_hashfile, 0, 0, 4)

0 commit comments

Comments
 (0)