Skip to content

Commit 4eabc43

Browse files
committed
[blob] improve codes
1 parent 38cdbb0 commit 4eabc43

File tree

1 file changed

+128
-58
lines changed

1 file changed

+128
-58
lines changed

blob.c

Lines changed: 128 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,40 @@ PHP_FUNCTION(git_blob_create_frombuffer)
2828
git_oid_fmt(out, &id);
2929
RETURN_STRING(out, 1);
3030
}
31+
/* }}} */
3132

32-
/* {{{ proto resource git_blob_create_fromchunks(resource $repository, string $hintpath, Callable $callback, mixed payload)
33-
*/
33+
/* {{{ proto long git_blob_create_fromchunks(string $id, resource $repo, string $hintpath, Callable $callback, $payload)
34+
*/
3435
PHP_FUNCTION(git_blob_create_fromchunks)
3536
{
37+
int result = 0, id_len = 0, hintpath_len = 0, error = 0;
38+
char *id = NULL, *hintpath = NULL;
39+
git_oid __id = {0};
40+
zval *repo = NULL, *callback = NULL, *payload = NULL;
41+
php_git2_t *_repo = NULL;
42+
zend_fcall_info fci = empty_fcall_info;
43+
zend_fcall_info_cache fcc = empty_fcall_info_cache;
44+
php_git2_cb_t *cb = NULL;
45+
46+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
47+
"srsfz", &id, &id_len, &repo, &hintpath, &hintpath_len, &fci, &fcc, &payload) == FAILURE) {
48+
return;
49+
}
50+
51+
ZEND_FETCH_RESOURCE(_repo, php_git2_t*, &repo, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
52+
if (git_oid_fromstrn(&__id, id, id_len)) {
53+
RETURN_FALSE;
54+
}
55+
if (php_git2_cb_init(&cb, &fci, &fcc, payload TSRMLS_CC)) {
56+
RETURN_FALSE;
57+
}
58+
// TODO(chobie) implement this */
59+
//result = git_blob_create_fromchunks(__id, PHP_GIT2_V(_repo, repository), hintpath, <CHANGEME>, cb);
60+
php_git2_cb_free(cb);
61+
RETURN_LONG(result);
3662
}
63+
/* }}} */
64+
3765

3866
/* {{{ proto resource git_blob_create_fromdisk(resource $repository, string $path)
3967
*/
@@ -62,6 +90,7 @@ PHP_FUNCTION(git_blob_create_fromdisk)
6290
git_oid_fmt(out, &id);
6391
RETURN_STRING(out, 1);
6492
}
93+
/* }}} */
6594

6695
/* {{{ proto resource git_blob_create_fromworkdir(resource $repository, string $relative_path)
6796
*/
@@ -90,32 +119,56 @@ PHP_FUNCTION(git_blob_create_fromworkdir)
90119
git_oid_fmt(out, &id);
91120
RETURN_STRING(out, 1);
92121
}
122+
/* }}} */
93123

94-
/* {{{ proto resource git_blob_filtered_content($blob, $as_path, $check_for_binary_data)
95-
*/
124+
/* {{{ proto resource git_blob_filtered_content(resource $blob, string $as_path, long $check_for_binary_data)
125+
*/
96126
PHP_FUNCTION(git_blob_filtered_content)
97127
{
128+
php_git2_t *result = NULL, *_blob = NULL;
129+
git_buf out = NULL;
130+
zval *blob = NULL;
131+
char *as_path = NULL;
132+
int as_path_len = 0, error = 0;
133+
long check_for_binary_data = 0;
134+
135+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
136+
"rsl", &blob, &as_path, &as_path_len, &check_for_binary_data) == FAILURE) {
137+
return;
138+
}
139+
140+
ZEND_FETCH_RESOURCE(_blob, php_git2_t*, &blob, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
141+
error = git_blob_filtered_content(&out, PHP_GIT2_V(_blob, blob), as_path, check_for_binary_data);
142+
if (php_git2_check_error(error, "git_blob_filtered_content" TSRMLS_CC)) {
143+
RETURN_FALSE;
144+
}
145+
if (php_git2_make_resource(&result, PHP_GIT2_TYPE_BUF, out, 1 TSRMLS_CC)) {
146+
RETURN_FALSE;
147+
}
148+
ZVAL_RESOURCE(return_value, GIT2_RVAL_P(result));
98149
}
150+
/* }}} */
99151

100-
/* {{{ proto resource git_blob_free(resource $blob)
101-
*/
152+
/* {{{ proto void git_blob_free(resource $blob)
153+
*/
102154
PHP_FUNCTION(git_blob_free)
103155
{
104-
zval *blob;
105-
php_git2_t *git2;
156+
zval *blob = NULL;
157+
php_git2_t *_blob = NULL;
106158

107159
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
108160
"r", &blob) == FAILURE) {
109161
return;
110162
}
111163

112-
ZEND_FETCH_RESOURCE(git2, php_git2_t*, &blob, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
113-
if (git2->should_free_v) {
114-
git_blob_free(PHP_GIT2_V(git2, blob));
115-
git2->should_free_v = 0;
116-
}
164+
ZEND_FETCH_RESOURCE(_blob, php_git2_t*, &blob, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
165+
if (GIT2_SHOULD_FREE(_blob)) {
166+
git_blob_free(PHP_GIT2_V(_blob, blob));
167+
GIT2_SHOULD_FREE(_blob) = 0;
168+
};
117169
zval_ptr_dtor(&blob);
118170
}
171+
/* }}} */
119172

120173
/* {{{ proto resource git_blob_id(resource $blob)
121174
*/
@@ -137,6 +190,7 @@ PHP_FUNCTION(git_blob_id)
137190
git_oid_fmt(out, id);
138191
RETURN_STRING(out, 1);
139192
}
193+
/* }}} */
140194

141195
/* {{{ proto resource git_blob_is_binary(resource $blob)
142196
*/
@@ -155,75 +209,89 @@ PHP_FUNCTION(git_blob_is_binary)
155209
result = git_blob_is_binary(PHP_GIT2_V(git2, blob));
156210
RETURN_BOOL(result);
157211
}
212+
/* }}} */
158213

159-
/* {{{ proto resource git_blob_lookup(resource $repository, string $oid)
160-
*/
214+
/* {{{ proto long git_blob_lookup(resource $repo, string $id)
215+
*/
161216
PHP_FUNCTION(git_blob_lookup)
162217
{
163-
zval *repository;
164-
php_git2_t *git2, *result;
165-
git_blob *blob;
166-
char *hash;
167-
int hash_len;
168-
int error;
169-
git_oid id;
218+
int result = 0, id_len = 0, error = 0;
219+
git_blob *blob = NULL;
220+
zval *repo = NULL;
221+
php_git2_t *_repo = NULL, *_result = NULL;
222+
char *id = NULL;
223+
git_oid __id = {0};
170224

171225
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
172-
"rs", &repository, &hash, &hash_len) == FAILURE) {
226+
"rs", &repo, &id, &id_len) == FAILURE) {
173227
return;
174228
}
175229

176-
if (git_oid_fromstrn(&id, hash, hash_len) != GIT_OK) {
177-
return;
230+
ZEND_FETCH_RESOURCE(_repo, php_git2_t*, &repo, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
231+
if (git_oid_fromstrn(&__id, id, id_len)) {
232+
RETURN_FALSE;
178233
}
179-
180-
ZEND_FETCH_RESOURCE(git2, php_git2_t*, &repository, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
181-
182-
error = git_blob_lookup(&blob, PHP_GIT2_V(git2, repository), &id);
183-
if (php_git2_check_error(error, "git_blob_lookup" TSRMLS_CC)) {
184-
RETURN_FALSE
234+
result = git_blob_lookup(&blob, PHP_GIT2_V(_repo, repository), &__id);
235+
if (php_git2_make_resource(&_result, PHP_GIT2_TYPE_BLOB, result, 0 TSRMLS_CC)) {
236+
RETURN_FALSE;
185237
}
186-
187-
PHP_GIT2_MAKE_RESOURCE(result);
188-
PHP_GIT2_V(result, blob) = blob;
189-
result->type = PHP_GIT2_TYPE_BLOB;
190-
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
191-
result->should_free_v = 0;
192-
193-
ZVAL_RESOURCE(return_value, result->resource_id);
238+
ZVAL_RESOURCE(return_value, GIT2_RVAL_P(_result));
194239
}
240+
/* }}} */
195241

196-
/* {{{ proto resource git_blob_lookup_prefix(resource $blob, string $oid)
197-
*/
242+
243+
/* {{{ proto long git_blob_lookup_prefix(resource $repo, string $id, long $len)
244+
*/
198245
PHP_FUNCTION(git_blob_lookup_prefix)
199246
{
247+
int result = 0, id_len = 0, error = 0;
248+
git_blob *blob = NULL;
249+
zval *repo = NULL;
250+
php_git2_t *_repo = NULL, *_result = NULL;
251+
char *id = NULL;
252+
git_oid __id = {0};
253+
long len = 0;
254+
255+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
256+
"rsl", &repo, &id, &id_len, &len) == FAILURE) {
257+
return;
258+
}
259+
260+
ZEND_FETCH_RESOURCE(_repo, php_git2_t*, &repo, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
261+
if (git_oid_fromstrn(&__id, id, id_len)) {
262+
RETURN_FALSE;
263+
}
264+
result = git_blob_lookup_prefix(&blob, PHP_GIT2_V(_repo, repository), &__id, len);
265+
if (php_git2_make_resource(&_result, PHP_GIT2_TYPE_BLOB, blob, 0 TSRMLS_CC)) {
266+
RETURN_FALSE;
267+
}
268+
ZVAL_RESOURCE(return_value, GIT2_RVAL_P(_result));
200269
}
270+
/* }}} */
201271

202-
/* {{{ proto resource git_blob_owner(resource $blob, string $oid)
203-
*/
272+
273+
/* {{{ proto resource git_blob_owner(resource $blob)
274+
*/
204275
PHP_FUNCTION(git_blob_owner)
205276
{
206-
zval *blob;
207-
php_git2_t *git2, *result;
208-
git_repository *repository;
277+
git_repository *result = NULL;
278+
zval *blob = NULL;
279+
php_git2_t *_blob = NULL, *__result = NULL;
209280

210281
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
211282
"r", &blob) == FAILURE) {
212283
return;
213284
}
214285

215-
ZEND_FETCH_RESOURCE(git2, php_git2_t*, &blob, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
216-
217-
PHP_GIT2_MAKE_RESOURCE(result);
218-
repository = git_blob_owner(PHP_GIT2_V(git2, blob));
219-
220-
PHP_GIT2_V(result, repository) = repository;
221-
result->type = PHP_GIT2_TYPE_REPOSITORY;
222-
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
223-
result->should_free_v = 0;
224-
225-
ZVAL_RESOURCE(return_value, result->resource_id);
286+
ZEND_FETCH_RESOURCE(_blob, php_git2_t*, &blob, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
287+
result = git_blob_owner(PHP_GIT2_V(_blob, blob));
288+
if (php_git2_make_resource(&__result, PHP_GIT2_TYPE_BLOB, result, 0 TSRMLS_CC)) {
289+
RETURN_FALSE;
290+
}
291+
ZVAL_RESOURCE(return_value, GIT2_RVAL_P(__result));
226292
}
293+
/* }}} */
294+
227295

228296
/* {{{ proto resource git_blob_rawcontent(resource $blob)
229297
*/
@@ -248,6 +316,7 @@ PHP_FUNCTION(git_blob_rawcontent)
248316
size = git_blob_rawsize(PHP_GIT2_V(git2, blob));
249317
RETURN_STRINGL(buffer, size, 1);
250318
}
319+
/* }}} */
251320

252321
/* {{{ proto resource git_blob_rawsize(resource $blob, string $oid)
253322
*/
@@ -265,4 +334,5 @@ PHP_FUNCTION(git_blob_rawsize)
265334
ZEND_FETCH_RESOURCE(git2, php_git2_t*, &blob, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
266335
size = git_blob_rawsize(PHP_GIT2_V(git2, blob));
267336
RETURN_LONG(size);
268-
}
337+
}
338+
/* }}} */

0 commit comments

Comments
 (0)