Skip to content

Commit 86e285d

Browse files
committed
added static method
1 parent 93fe1a6 commit 86e285d

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

blob.c

+41-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ zend_object_value php_git2_blob_new(zend_class_entry *ce TSRMLS_DC)
4444
return retval;
4545
}
4646

47+
ZEND_BEGIN_ARG_INFO_EX(arginfo_git2_blob_create, 0,0,2)
48+
ZEND_ARG_INFO(0, repository)
49+
ZEND_ARG_INFO(0, contents)
50+
ZEND_END_ARG_INFO()
51+
52+
4753

4854
/*
4955
{{{ proto: Git2\Blob::__toString()
@@ -67,9 +73,43 @@ PHP_METHOD(git2_blob, __toString)
6773
}
6874
/* }}} */
6975

76+
/*
77+
{{{ proto: Git2\Blob::create(Git2\Repository $repository, string $contents)
78+
*/
79+
PHP_METHOD(git2_blob, create)
80+
{
81+
char *contents;
82+
int contents_len, error = 0;
83+
zval *repository;
84+
php_git2_repository *m_repository;
85+
char oid_out[GIT_OID_HEXSZ] = {0};
86+
git_oid oid;
87+
88+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
89+
"Os", &repository, git2_repository_class_entry, &contents, &contents_len) == FAILURE) {
90+
return;
91+
}
92+
93+
m_repository = PHP_GIT2_GET_OBJECT(php_git2_repository, repository);
94+
95+
if (m_repository != NULL) {
96+
if (m_repository->repository == NULL) {
97+
RETURN_FALSE;
98+
}
99+
100+
error = git_blob_create_frombuffer(&oid, m_repository->repository, contents, contents_len);
101+
git_oid_fmt(oid_out, &oid);
102+
RETVAL_STRINGL(oid_out,GIT_OID_HEXSZ,1);
103+
} else {
104+
RETURN_FALSE;
105+
}
106+
}
107+
/* }}} */
108+
70109

71110
static zend_function_entry php_git2_blob_methods[] = {
72-
PHP_ME(git2_blob, __toString, NULL, ZEND_ACC_PUBLIC)
111+
PHP_ME(git2_blob, __toString, NULL, ZEND_ACC_PUBLIC)
112+
PHP_ME(git2_blob, create, arginfo_git2_blob_create, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
73113
{NULL,NULL,NULL}
74114
};
75115

tests/004-03-blob_create.phpt

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Check for Git2\Blob::__toString
3+
--SKIPIF--
4+
<?php if (!extension_loaded("git2")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
$path = __DIR__ . '/fixtures/testrepo.git';
8+
$repo = new Git2\Repository($path);
9+
$id = Git2\Blob::create($repo, "Hello World");
10+
11+
if ($id == "5e1c309dae7f45e0f39b1bf3ac3cd9db12e7d689") {
12+
echo "OK" . PHP_EOL;
13+
} else {
14+
echo "FAIL" . PHP_EOL;
15+
}
16+
--EXPECT--
17+
OK

0 commit comments

Comments
 (0)