Skip to content

Commit 5d5a4ef

Browse files
committed
[odb_backend] add read_header callback
1 parent 77de574 commit 5d5a4ef

File tree

3 files changed

+64
-11
lines changed

3 files changed

+64
-11
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ https://docs.google.com/spreadsheet/ccc?key=0AjvShWAWqvfHdDRneEtIUF9GRUZMNVVVR1h
1515
git submodule init && git submodule update
1616
mkdir libgit2/build
1717
cd libgit2/build
18-
cmake -DCMAKE_BUILD_TYPE=Debug -DBUILD_SHARED_LIBS=OFF -DBUILD_CLAR=OFF .
18+
cmake -DCMAKE_BUILD_TYPE=Debug -DBUILD_SHARED_LIBS=OFF -DBUILD_CLAR=OFF ..
1919
cmake --build .
2020
2121
# build php-git2

example/odb_backend.php

+17-6
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,20 @@ class Pool
1313
Pool::$pool[$oid][1],
1414
);
1515
},
16-
"read_prefix" => function(){
17-
echo "Helo WOrld";
18-
16+
"read_prefix" => function($short_oid){
17+
echo "Helo World";
18+
return array(
19+
"Buffer",
20+
"type",
21+
"actual_oid",
22+
);
1923
},
20-
"read_header" => function() {
21-
echo "Helo WOrld";
22-
24+
"read_header" => function($oid) {
25+
echo "\e[32m# read header$oid\e[m\n";
26+
return array(
27+
strlen(Pool::$pool[$oid][0]),
28+
Pool::$pool[$oid][1],
29+
);
2330
},
2431
"write" => function($oid, $buffer, $otype) {
2532
echo "\e[32m# write $oid\e[m\n";
@@ -63,4 +70,8 @@ class Pool
6370
$obj = git_odb_read($odb, $oid);
6471

6572
echo git_odb_object_data($obj);
73+
echo "\n";
74+
75+
$header = git_odb_read_header($odb, $oid);
76+
var_dump($header); // size, otype
6677
exit;

odb.c

+46-4
Original file line numberDiff line numberDiff line change
@@ -156,25 +156,30 @@ PHP_FUNCTION(git_odb_read_header)
156156
{
157157
php_git2_t *result = NULL, *_db = NULL;
158158
size_t len_out = NULL;
159-
zval *type_out = NULL, *db = NULL;
159+
zval *db = NULL, *_result = NULL;
160+
git_otype type_out;
160161
char *id = NULL;
161162
int id_len = 0, error = 0;
162163
git_oid __id = {0};
163164

164165
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
165-
"<git_otype>rs", &type_out, &db, &id, &id_len) == FAILURE) {
166+
"rs", &db, &id, &id_len) == FAILURE) {
166167
return;
167168
}
168169

169170
ZEND_FETCH_RESOURCE(_db, php_git2_t*, &db, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
170171
if (git_oid_fromstrn(&__id, id, id_len)) {
171172
RETURN_FALSE;
172173
}
173-
error = git_odb_read_header(&len_out, type_out, PHP_GIT2_V(_db, odb), &__id);
174+
error = git_odb_read_header(&len_out, &type_out, PHP_GIT2_V(_db, odb), &__id);
174175
if (php_git2_check_error(error, "git_odb_read_header" TSRMLS_CC)) {
175176
RETURN_FALSE;
176177
}
177-
RETURN_LONG(len_out);
178+
MAKE_STD_ZVAL(_result);
179+
array_init(_result);
180+
add_next_index_long(_result, len_out);
181+
add_next_index_long(_result, type_out);
182+
RETURN_ZVAL(_result, 0, 1);
178183
}
179184
/* }}} */
180185

@@ -791,6 +796,39 @@ static int php_git2_odb_backend_read_prefix(git_oid *out_oid,
791796

792797
static int php_git2_odb_backend_read_header(size_t *len_p, git_otype *type_p, git_odb_backend *backend, const git_oid *oid)
793798
{
799+
php_git2_t *result;
800+
php_git2_odb_backend *php_backend = (php_git2_odb_backend*)backend;
801+
zval *param_oid = NULL, *retval_ptr = NULL;
802+
php_git2_multi_cb_t *p = php_backend->multi;
803+
int i = 0, retval = 0;
804+
GIT2_TSRMLS_SET(p->tsrm_ls);
805+
char buf[41] = {0};
806+
807+
git_oid_fmt(buf, oid);
808+
MAKE_STD_ZVAL(param_oid);
809+
ZVAL_STRING(param_oid, buf, 1);
810+
811+
if (php_git2_call_function_v(&p->callbacks[3].fci, &p->callbacks[3].fcc TSRMLS_CC, &retval_ptr, 1, &param_oid)) {
812+
return GIT_EUSER;
813+
}
814+
if (Z_TYPE_P(retval_ptr) == IS_ARRAY) {
815+
zval **value, **otype;
816+
817+
if (zend_hash_num_elements(Z_ARRVAL_P(retval_ptr)) != 2) {
818+
return GIT_ENOTFOUND;
819+
}
820+
821+
zend_hash_get_current_data(Z_ARRVAL_P(retval_ptr), (void **)&value);
822+
*len_p = Z_LVAL_PP(value);
823+
zend_hash_move_forward(Z_ARRVAL_P(retval_ptr));
824+
zend_hash_get_current_data(Z_ARRVAL_P(retval_ptr), (void **)&otype);
825+
*type_p = Z_LVAL_PP(otype);
826+
} else {
827+
retval = GIT_ENOTFOUND;
828+
}
829+
830+
zval_ptr_dtor(&retval_ptr);
831+
return retval;
794832
}
795833
static int php_git2_odb_backend_writestream(git_odb_stream **stream_out, git_odb_backend *_backend, size_t length, git_otype type)
796834
{
@@ -868,6 +906,10 @@ PHP_FUNCTION(git_odb_backend_new)
868906
php_git2_fcall_info_wrapper2(tmp, &write_fci, &write_fcc TSRMLS_CC);
869907
}
870908

909+
tmp = php_git2_read_arrval(callbacks, ZEND_STRS("read_header") TSRMLS_CC);
910+
if (tmp) {
911+
php_git2_fcall_info_wrapper2(tmp, &read_header_fci, &read_header_fcc TSRMLS_CC);
912+
}
871913
tmp = php_git2_read_arrval(callbacks, ZEND_STRS("exists") TSRMLS_CC);
872914
if (tmp) {
873915
php_git2_fcall_info_wrapper2(tmp, &exists_fci, &exists_fcc TSRMLS_CC);

0 commit comments

Comments
 (0)