Skip to content

Commit 0f9eade

Browse files
committed
[revparse] improve codes
1 parent 9f8c803 commit 0f9eade

File tree

1 file changed

+24
-36
lines changed

1 file changed

+24
-36
lines changed

revparse.c

+24-36
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66
*/
77
PHP_FUNCTION(git_revparse_single)
88
{
9-
php_git2_t *result = NULL;
9+
php_git2_t *result = NULL, *_repo = NULL;
1010
git_object *out = NULL;
1111
zval *repo = NULL;
12-
php_git2_t *_repo = NULL;
1312
char *spec = NULL;
14-
int spec_len = 0;
15-
int error = 0;
13+
int spec_len = 0, error = 0;
1614

1715
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
1816
"rs", &repo, &spec, &spec_len) == FAILURE) {
@@ -24,15 +22,14 @@ PHP_FUNCTION(git_revparse_single)
2422
if (php_git2_check_error(error, "git_revparse_single" TSRMLS_CC)) {
2523
RETURN_FALSE;
2624
}
27-
PHP_GIT2_MAKE_RESOURCE(result);
28-
PHP_GIT2_V(result, object) = out;
29-
result->type = PHP_GIT2_TYPE_OBJECT;
30-
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
31-
result->should_free_v = 0;
32-
ZVAL_RESOURCE(return_value, result->resource_id);
25+
if (php_git2_make_resource(&result, PHP_GIT2_TYPE_OBJECT, out, 0 TSRMLS_CC)) {
26+
RETURN_FALSE;
27+
}
28+
ZVAL_RESOURCE(return_value, GIT2_RVAL_P(result));
3329
}
3430
/* }}} */
3531

32+
3633
/* {{{ proto array git_revparse_ext(resource $repo, string $spec)
3734
*/
3835
PHP_FUNCTION(git_revparse_ext)
@@ -58,23 +55,19 @@ PHP_FUNCTION(git_revparse_ext)
5855
RETURN_FALSE;
5956
}
6057

61-
PHP_GIT2_MAKE_RESOURCE(result);
62-
PHP_GIT2_MAKE_RESOURCE(result2);
6358
MAKE_STD_ZVAL(array);
6459
MAKE_STD_ZVAL(a);
6560
MAKE_STD_ZVAL(b);
6661

67-
PHP_GIT2_V(result, object) = object_out;
68-
result->type = PHP_GIT2_TYPE_OBJECT;
69-
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
70-
result->should_free_v = 0;
71-
ZVAL_RESOURCE(a, result->resource_id);
62+
if (php_git2_make_resource(&result, PHP_GIT2_TYPE_OBJECT, object_out, 0 TSRMLS_CC)) {
63+
RETURN_FALSE;
64+
}
65+
ZVAL_RESOURCE(a, GIT2_RVAL_P(result));
7266

73-
PHP_GIT2_V(result2, reference) = reference_out;
74-
result2->type = PHP_GIT2_TYPE_REFERENCE;
75-
result2->resource_id = PHP_GIT2_LIST_INSERT(result2, git2_resource_handle);
76-
result2->should_free_v = 0;
77-
ZVAL_RESOURCE(b, result->resource_id);
67+
if (php_git2_make_resource(&result2, PHP_GIT2_TYPE_REFERENCE, reference_out, 0 TSRMLS_CC)) {
68+
RETURN_FALSE;
69+
}
70+
ZVAL_RESOURCE(b, GIT2_RVAL_P(result2));
7871

7972
array_init(array);
8073
add_next_index_zval(array, a);
@@ -108,20 +101,15 @@ PHP_FUNCTION(git_revparse)
108101
RETURN_FALSE;
109102
}
110103
MAKE_STD_ZVAL(result);
111-
MAKE_STD_ZVAL(from);
112-
MAKE_STD_ZVAL(to);
113-
114-
PHP_GIT2_V(_from, object) = revspec.from;
115-
_from->type = PHP_GIT2_TYPE_OBJECT;
116-
_from->resource_id = PHP_GIT2_LIST_INSERT(_from, git2_resource_handle);
117-
_from->should_free_v = 0;
118-
ZVAL_RESOURCE(from, _from->resource_id);
119-
120-
PHP_GIT2_V(_to, object) = revspec.to;
121-
_to->type = PHP_GIT2_TYPE_OBJECT;
122-
_to->resource_id = PHP_GIT2_LIST_INSERT(_to, git2_resource_handle);
123-
_to->should_free_v = 0;
124-
ZVAL_RESOURCE(to, _to->resource_id);
104+
if (php_git2_make_resource(&_from, PHP_GIT2_TYPE_OBJECT, revspec.from, 0 TSRMLS_CC)) {
105+
RETURN_FALSE;
106+
}
107+
ZVAL_RESOURCE(from, GIT2_RVAL_P(_from));
108+
109+
if (php_git2_make_resource(&_to, PHP_GIT2_TYPE_OBJECT, revspec.to, 0 TSRMLS_CC)) {
110+
RETURN_FALSE;
111+
}
112+
ZVAL_RESOURCE(to, GIT2_RVAL_P(_to));
125113

126114
array_init(result);
127115
add_assoc_zval_ex(result, ZEND_STRS("from"), from);

0 commit comments

Comments
 (0)