Skip to content

Commit f6a9467

Browse files
committed
improve codes
1 parent 1176658 commit f6a9467

File tree

2 files changed

+216
-279
lines changed

2 files changed

+216
-279
lines changed

pathspec.c

Lines changed: 37 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
PHP_FUNCTION(git_pathspec_new)
88
{
99
php_git2_t *result = NULL;
10-
git_pathspec *out;
10+
git_pathspec *out = NULL;
1111
zval *pathspec = NULL;
12+
git_strarray _pathspec = {0};
1213
int error = 0;
1314

1415
/* TODO(chobie): generate converter */
@@ -17,20 +18,18 @@ PHP_FUNCTION(git_pathspec_new)
1718
return;
1819
}
1920

21+
php_git2_array_to_strarray(&_pathspec, pathspec TSRMLS_CC);
2022
error = git_pathspec_new(&out, pathspec);
2123
if (php_git2_check_error(error, "git_pathspec_new" TSRMLS_CC)) {
2224
RETURN_FALSE;
2325
}
24-
PHP_GIT2_MAKE_RESOURCE(result);
25-
PHP_GIT2_V(result, pathspec) = out;
26-
result->type = PHP_GIT2_TYPE_PATHSPEC;
27-
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
28-
result->should_free_v = 0;
29-
ZVAL_RESOURCE(return_value, result->resource_id);
26+
if (php_git2_make_resource(&result, PHP_GIT2_TYPE_PATHSPEC, out, 1 TSRMLS_CC)) {
27+
RETURN_FALSE;
28+
}
29+
ZVAL_RESOURCE(return_value, GIT2_RVAL_P(result));
3030
}
3131
/* }}} */
3232

33-
3433
/* {{{ proto void git_pathspec_free(resource $ps)
3534
*/
3635
PHP_FUNCTION(git_pathspec_free)
@@ -44,8 +43,9 @@ PHP_FUNCTION(git_pathspec_free)
4443
}
4544

4645
ZEND_FETCH_RESOURCE(_ps, php_git2_t*, &ps, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
47-
if (_ps->should_free_v) {
46+
if (GIT2_SHOULD_FREE(_ps)) {
4847
git_pathspec_free(PHP_GIT2_V(_ps, pathspec));
48+
GIT2_SHOULD_FREE(_ps) = 0;
4949
};
5050
zval_ptr_dtor(&ps);
5151
}
@@ -56,13 +56,11 @@ PHP_FUNCTION(git_pathspec_free)
5656
*/
5757
PHP_FUNCTION(git_pathspec_matches_path)
5858
{
59-
int result = 0;
59+
int result = 0, path_len = 0, error = 0;
6060
zval *ps = NULL;
6161
php_git2_t *_ps = NULL;
6262
long flags = 0;
6363
char *path = NULL;
64-
int path_len = 0;
65-
int error = 0;
6664

6765
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
6866
"rls", &ps, &flags, &path, &path_len) == FAILURE) {
@@ -80,13 +78,10 @@ PHP_FUNCTION(git_pathspec_matches_path)
8078
*/
8179
PHP_FUNCTION(git_pathspec_match_workdir)
8280
{
83-
php_git2_t *result = NULL;
81+
php_git2_t *result = NULL, *_repo = NULL, *_ps = NULL;
8482
git_pathspec_match_list *out = NULL;
85-
zval *repo = NULL;
86-
php_git2_t *_repo = NULL;
83+
zval *repo = NULL, *ps = NULL;
8784
long flags = 0;
88-
zval *ps = NULL;
89-
php_git2_t *_ps = NULL;
9085
int error = 0;
9186

9287
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
@@ -100,27 +95,21 @@ PHP_FUNCTION(git_pathspec_match_workdir)
10095
if (php_git2_check_error(error, "git_pathspec_match_workdir" TSRMLS_CC)) {
10196
RETURN_FALSE;
10297
}
103-
PHP_GIT2_MAKE_RESOURCE(result);
104-
PHP_GIT2_V(result, pathspec_match_list) = out;
105-
result->type = PHP_GIT2_TYPE_PATHSPEC_MATCH_LIST;
106-
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
107-
result->should_free_v = 0;
108-
ZVAL_RESOURCE(return_value, result->resource_id);
98+
if (php_git2_make_resource(&result, PHP_GIT2_TYPE_PATHSPEC_MATCH_LIST, out, 0 TSRMLS_CC)) {
99+
RETURN_FALSE;
100+
}
101+
ZVAL_RESOURCE(return_value, GIT2_RVAL_P(result));
109102
}
110103
/* }}} */
111104

112-
113105
/* {{{ proto resource git_pathspec_match_index(resource $index, long $flags, resource $ps)
114106
*/
115107
PHP_FUNCTION(git_pathspec_match_index)
116108
{
117-
php_git2_t *result = NULL;
109+
php_git2_t *result = NULL, *_index = NULL, *_ps = NULL;
118110
git_pathspec_match_list *out = NULL;
119-
zval *index = NULL;
120-
php_git2_t *_index = NULL;
111+
zval *index = NULL, *ps = NULL;
121112
long flags = 0;
122-
zval *ps = NULL;
123-
php_git2_t *_ps = NULL;
124113
int error = 0;
125114

126115
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
@@ -134,27 +123,21 @@ PHP_FUNCTION(git_pathspec_match_index)
134123
if (php_git2_check_error(error, "git_pathspec_match_index" TSRMLS_CC)) {
135124
RETURN_FALSE;
136125
}
137-
PHP_GIT2_MAKE_RESOURCE(result);
138-
PHP_GIT2_V(result, pathspec_match_list) = out;
139-
result->type = PHP_GIT2_TYPE_PATHSPEC_MATCH_LIST;
140-
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
141-
result->should_free_v = 0;
142-
ZVAL_RESOURCE(return_value, result->resource_id);
126+
if (php_git2_make_resource(&result, PHP_GIT2_TYPE_PATHSPEC_MATCH_LIST, out, 1 TSRMLS_CC)) {
127+
RETURN_FALSE;
128+
}
129+
ZVAL_RESOURCE(return_value, GIT2_RVAL_P(result));
143130
}
144131
/* }}} */
145132

146-
147133
/* {{{ proto resource git_pathspec_match_tree(resource $tree, long $flags, resource $ps)
148134
*/
149135
PHP_FUNCTION(git_pathspec_match_tree)
150136
{
151-
php_git2_t *result = NULL;
137+
php_git2_t *result = NULL, *_tree = NULL, *_ps = NULL;
152138
git_pathspec_match_list *out = NULL;
153-
zval *tree = NULL;
154-
php_git2_t *_tree = NULL;
139+
zval *tree = NULL, *ps = NULL;
155140
long flags = 0;
156-
zval *ps = NULL;
157-
php_git2_t *_ps = NULL;
158141
int error = 0;
159142

160143
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
@@ -168,27 +151,21 @@ PHP_FUNCTION(git_pathspec_match_tree)
168151
if (php_git2_check_error(error, "git_pathspec_match_tree" TSRMLS_CC)) {
169152
RETURN_FALSE;
170153
}
171-
PHP_GIT2_MAKE_RESOURCE(result);
172-
PHP_GIT2_V(result, pathspec_match_list) = out;
173-
result->type = PHP_GIT2_TYPE_PATHSPEC_MATCH_LIST;
174-
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
175-
result->should_free_v = 0;
176-
ZVAL_RESOURCE(return_value, result->resource_id);
154+
if (php_git2_make_resource(&result, PHP_GIT2_TYPE_PATHSPEC_MATCH_LIST, out, 1 TSRMLS_CC)) {
155+
RETURN_FALSE;
156+
}
157+
ZVAL_RESOURCE(return_value, GIT2_RVAL_P(result));
177158
}
178159
/* }}} */
179160

180-
181161
/* {{{ proto resource git_pathspec_match_diff(resource $diff, long $flags, resource $ps)
182162
*/
183163
PHP_FUNCTION(git_pathspec_match_diff)
184164
{
185-
php_git2_t *result = NULL;
165+
php_git2_t *result = NULL, *_diff = NULL, *_ps = NULL;
186166
git_pathspec_match_list *out = NULL;
187-
zval *diff = NULL;
188-
php_git2_t *_diff = NULL;
167+
zval *diff = NULL, *ps = NULL;
189168
long flags = 0;
190-
zval *ps = NULL;
191-
php_git2_t *_ps = NULL;
192169
int error = 0;
193170

194171
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
@@ -202,16 +179,13 @@ PHP_FUNCTION(git_pathspec_match_diff)
202179
if (php_git2_check_error(error, "git_pathspec_match_diff" TSRMLS_CC)) {
203180
RETURN_FALSE;
204181
}
205-
PHP_GIT2_MAKE_RESOURCE(result);
206-
PHP_GIT2_V(result, pathspec_match_list) = out;
207-
result->type = PHP_GIT2_TYPE_PATHSPEC_MATCH_LIST;
208-
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
209-
result->should_free_v = 0;
210-
ZVAL_RESOURCE(return_value, result->resource_id);
182+
if (php_git2_make_resource(&result, PHP_GIT2_TYPE_PATHSPEC_MATCH_LIST, out, 0 TSRMLS_CC)) {
183+
RETURN_FALSE;
184+
}
185+
ZVAL_RESOURCE(return_value, GIT2_RVAL_P(result));
211186
}
212187
/* }}} */
213188

214-
215189
/* {{{ proto void git_pathspec_match_list_free(resource $m)
216190
*/
217191
PHP_FUNCTION(git_pathspec_match_list_free)
@@ -225,8 +199,9 @@ PHP_FUNCTION(git_pathspec_match_list_free)
225199
}
226200

227201
ZEND_FETCH_RESOURCE(_m, php_git2_t*, &m, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
228-
if (_m->should_free_v) {
202+
if (GIT2_SHOULD_FREE(_m)) {
229203
git_pathspec_match_list_free(PHP_GIT2_V(_m, pathspec_match_list));
204+
GIT2_SHOULD_FREE(_m) = 0;
230205
};
231206
zval_ptr_dtor(&m);
232207
}
@@ -278,7 +253,7 @@ PHP_FUNCTION(git_pathspec_match_list_entry)
278253
*/
279254
PHP_FUNCTION(git_pathspec_match_list_diff_entry)
280255
{
281-
const git_diff_delta *result = NULL;
256+
const git_diff_delta *result = NULL;
282257
zval *m = NULL;
283258
php_git2_t *_m = NULL;
284259
long pos = 0;

0 commit comments

Comments
 (0)