Skip to content

Commit 56d7fe6

Browse files
committed
added blob test
1 parent 470e032 commit 56d7fe6

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

git2_blob.c

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,23 +100,13 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_blob_rawcontent, 0, 0, 0)
100100
ZEND_END_ARG_INFO()
101101

102102
static PHP_METHOD(Blob, rawcontent) {
103-
zend_bool is_push;
104-
103+
if (zend_parse_parameters_none() == FAILURE) return;
105104
GIT2_BLOB_FETCH();
106105

107-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &is_push) == FAILURE) {
108-
return;
109-
}
110-
111-
// TODO handle callbacks
112-
int res = git_blob_connect(intern->blob, is_push ? GIT_DIRECTION_PUSH : GIT_DIRECTION_FETCH, NULL, NULL);
113-
114-
if (res == 0) {
115-
RETURN_TRUE;
116-
}
106+
const void *res = git_blob_rawcontent(intern->blob);
107+
git_off_t len = git_blob_rawsize(intern->blob);
117108

118-
git2_throw_last_error();
119-
RETURN_FALSE;
109+
RETURN_STRINGL((const char*)res, len);
120110
}
121111

122112
void git2_blob_spawn(zval **return_value, git_blob *blob TSRMLS_DC) {
@@ -157,6 +147,7 @@ static void php_git2_blob_free_object(zend_object *object TSRMLS_DC) {
157147
#define PHP_GIT2_BLOB_ME_P(_x) PHP_ME(Blob, _x, arginfo_blob_##_x, ZEND_ACC_PUBLIC)
158148

159149
static zend_function_entry git2_blob_methods[] = {
150+
PHP_ME(Blob, lookup_oid, arginfo_blob_lookup_oid, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
160151
// PHP_ME(Blob, create_fromworkdir, arginfo_blob_create_fromworkdir, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
161152
// PHP_ME(Blob, create_fromdisk, arginfo_blob_create_fromdisk, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
162153
PHP_GIT2_BLOB_ME_P(id)

git2_tests/000_base.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,21 @@
3030
var_dump(bin2hex($tree->id()));
3131
var_dump($tree->entrycount());
3232

33-
$cb = function($root, $entry) {
34-
echo " + $root".$entry->name()." (".bin2hex($entry->id()).")\n";
33+
$cb = function($root, $entry) use ($repo) {
34+
$fullname = $root.$entry->name();
35+
echo " + $fullname (".bin2hex($entry->id()).")\n";
36+
if ($fullname == 'README.md') {
37+
// fetch contents, compare with actual README.md (will fail if uncommitted changes exist in file)
38+
$blob = Git2\Blob::lookup_oid($repo, $entry->id());
39+
$data = $blob->rawcontent();
40+
$actual_data = file_get_contents(dirname(__DIR__).'/README.md');
41+
if ($data == $actual_data) {
42+
echo " `- TEST OK\n";
43+
} else {
44+
echo " `- TEST FAILED ! (data different, uncommitted changes?)\n";
45+
exit(1);
46+
}
47+
}
3548
return 0;
3649
};
3750

0 commit comments

Comments
 (0)