Skip to content

Commit 2d352b3

Browse files
committed
[odb] add foreach callback
1 parent 1aa8395 commit 2d352b3

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

example/odb_foreach.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
$repo = git_repository_open(".");
3+
$odb = git_repository_odb($repo);
4+
5+
$payload = array();
6+
git_odb_foreach($odb, function($oid, &$payload) {
7+
echo $oid . PHP_EOL;
8+
}, $payload);

odb.c

+30-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,31 @@
22
#include "php_git2_priv.h"
33
#include "odb.h"
44

5+
static int php_git2_git_odb_foreach_cb(const git_oid *id, void *payload)
6+
{
7+
php_git2_t *result;
8+
zval *param_oid, *retval_ptr = NULL;
9+
php_git2_cb_t *p = (php_git2_cb_t*)payload;
10+
int i = 0;
11+
long retval = 0;
12+
char buf[41] = {0};
13+
GIT2_TSRMLS_SET(p->tsrm_ls)
14+
15+
Z_ADDREF_P(p->payload);
16+
MAKE_STD_ZVAL(param_oid);
17+
18+
git_oid_fmt(buf, id);
19+
ZVAL_STRING(param_oid, buf, 1);
20+
21+
if (php_git2_call_function_v(p->fci, p->fcc TSRMLS_CC, &retval_ptr, 2, &param_oid, &p->payload)) {
22+
return GIT_EUSER;
23+
}
24+
25+
retval = Z_LVAL_P(retval_ptr);
26+
zval_ptr_dtor(&retval_ptr);
27+
return retval;
28+
}
29+
530
/* {{{ proto resource git_odb_new()
631
*/
732
PHP_FUNCTION(git_odb_new)
@@ -246,8 +271,7 @@ PHP_FUNCTION(git_odb_foreach)
246271
if (php_git2_cb_init(&cb, &fci, &fcc, payload TSRMLS_CC)) {
247272
RETURN_FALSE;
248273
}
249-
// TODO(chobie): implment callback */
250-
//result = git_odb_foreach(PHP_GIT2_V(_db, odb), <CHANGEME>, cb);
274+
result = git_odb_foreach(PHP_GIT2_V(_db, odb), php_git2_git_odb_foreach_cb, cb);
251275
php_git2_cb_free(cb);
252276
RETURN_LONG(result);
253277
}
@@ -964,6 +988,10 @@ PHP_FUNCTION(git_odb_backend_new)
964988
if (tmp) {
965989
php_git2_fcall_info_wrapper2(tmp, &exists_fci, &exists_fcc TSRMLS_CC);
966990
}
991+
tmp = php_git2_read_arrval(callbacks, ZEND_STRS("foreach") TSRMLS_CC);
992+
if (tmp) {
993+
php_git2_fcall_info_wrapper2(tmp, &foreach_fci, &foreach_fcc TSRMLS_CC);
994+
}
967995

968996

969997
Z_ADDREF_P(callbacks);

0 commit comments

Comments
 (0)