Skip to content

Commit 1176658

Browse files
committed
improve codes
1 parent 0f9eade commit 1176658

File tree

3 files changed

+53
-74
lines changed

3 files changed

+53
-74
lines changed

cred.c

+19-32
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,8 @@ PHP_FUNCTION(git_cred_userpass_plaintext_new)
2828
{
2929
php_git2_t *result = NULL;
3030
git_cred *out = NULL;
31-
char *username = NULL;
32-
int username_len = 0;
33-
char *password = NULL;
34-
int password_len = 0;
35-
int error = 0;
31+
char *username = NULL, *password = NULL;
32+
int username_len = 0, password_len = 0, error = 0;
3633

3734
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
3835
"ss", &username, &username_len, &password, &password_len) == FAILURE) {
@@ -43,30 +40,22 @@ PHP_FUNCTION(git_cred_userpass_plaintext_new)
4340
if (php_git2_check_error(error, "git_cred_userpass_plaintext_new" TSRMLS_CC)) {
4441
RETURN_FALSE;
4542
}
46-
PHP_GIT2_MAKE_RESOURCE(result);
47-
PHP_GIT2_V(result, cred) = out;
48-
result->type = PHP_GIT2_TYPE_CRED;
49-
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
50-
result->should_free_v = 0;
51-
ZVAL_RESOURCE(return_value, result->resource_id);
43+
if (php_git2_make_resource(&result, PHP_GIT2_TYPE_CRED, out, 0 TSRMLS_CC)) {
44+
RETURN_FALSE;
45+
}
46+
ZVAL_RESOURCE(return_value, GIT2_RVAL_P(result));
5247
}
5348
/* }}} */
5449

50+
5551
/* {{{ proto resource git_cred_ssh_key_new(string $username, string $publickey, string $privatekey, string $passphrase)
5652
*/
5753
PHP_FUNCTION(git_cred_ssh_key_new)
5854
{
5955
php_git2_t *result = NULL;
6056
git_cred *out = NULL;
61-
char *username = NULL;
62-
int username_len = 0;
63-
char *publickey = NULL;
64-
int publickey_len = 0;
65-
char *privatekey = NULL;
66-
int privatekey_len = 0;
67-
char *passphrase = NULL;
68-
int passphrase_len = 0;
69-
int error = 0;
57+
char *username = NULL, *publickey = NULL, *privatekey = NULL, *passphrase = NULL;
58+
int username_len = 0, publickey_len = 0, privatekey_len = 0, passphrase_len = 0, error = 0;
7059

7160
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
7261
"ssss", &username, &username_len, &publickey, &publickey_len, &privatekey, &privatekey_len, &passphrase, &passphrase_len) == FAILURE) {
@@ -77,16 +66,15 @@ PHP_FUNCTION(git_cred_ssh_key_new)
7766
if (php_git2_check_error(error, "git_cred_ssh_key_new" TSRMLS_CC)) {
7867
RETURN_FALSE;
7968
}
80-
PHP_GIT2_MAKE_RESOURCE(result);
81-
PHP_GIT2_V(result, cred) = out;
82-
result->type = PHP_GIT2_TYPE_CRED;
83-
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
84-
result->should_free_v = 0;
85-
ZVAL_RESOURCE(return_value, result->resource_id);
69+
if (php_git2_make_resource(&result, PHP_GIT2_TYPE_CRED, out, 0 TSRMLS_CC)) {
70+
RETURN_FALSE;
71+
}
72+
ZVAL_RESOURCE(return_value, GIT2_RVAL_P(result));
8673
}
8774
/* }}} */
8875

8976

77+
9078
/* {{{ proto resource git_cred_ssh_custom_new(username, publickey, publickey_len, sign_fn, sign_data)
9179
*/
9280
PHP_FUNCTION(git_cred_ssh_custom_new)
@@ -121,16 +109,15 @@ PHP_FUNCTION(git_cred_default_new)
121109
if (php_git2_check_error(error, "git_cred_default_new" TSRMLS_CC)) {
122110
RETURN_FALSE;
123111
}
124-
PHP_GIT2_MAKE_RESOURCE(result);
125-
PHP_GIT2_V(result, cred) = out;
126-
result->type = PHP_GIT2_TYPE_CRED;
127-
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
128-
result->should_free_v = 0;
129-
ZVAL_RESOURCE(return_value, result->resource_id);
112+
if (php_git2_make_resource(&result, PHP_GIT2_TYPE_CRED, out, 0 TSRMLS_CC)) {
113+
RETURN_FALSE;
114+
}
115+
ZVAL_RESOURCE(return_value, GIT2_RVAL_P(result));
130116
}
131117
/* }}} */
132118

133119

120+
134121
/* {{{ proto resource git_cred_userpass(url, user_from_url, allowed_types, payload)
135122
*/
136123
PHP_FUNCTION(git_cred_userpass)

merge.c

+14-21
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,9 @@ PHP_FUNCTION(git_merge_head_from_ref)
8383
if (php_git2_check_error(error, "git_merge_head_from_ref" TSRMLS_CC)) {
8484
RETURN_FALSE;
8585
}
86-
PHP_GIT2_MAKE_RESOURCE(result);
87-
PHP_GIT2_V(result, merge_head) = out;
88-
result->type = PHP_GIT2_TYPE_MERGE_HEAD;
89-
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
90-
result->should_free_v = 0;
86+
if (php_git2_make_resource(&result, PHP_GIT2_TYPE_MERGE_HEAD, out, 0 TSRMLS_CC)) {
87+
RETURN_FALSE;
88+
}
9189
ZVAL_RESOURCE(return_value, result->resource_id);
9290
}
9391
/* }}} */
@@ -158,11 +156,9 @@ PHP_FUNCTION(git_merge_head_from_oid)
158156
if (php_git2_check_error(error, "git_merge_head_from_oid" TSRMLS_CC)) {
159157
RETURN_FALSE;
160158
}
161-
PHP_GIT2_MAKE_RESOURCE(result);
162-
PHP_GIT2_V(result, merge_head) = out;
163-
result->type = PHP_GIT2_TYPE_MERGE_HEAD;
164-
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
165-
result->should_free_v = 0;
159+
if (php_git2_make_resource(&result, PHP_GIT2_TYPE_MERGE_HEAD, out, 0 TSRMLS_CC)) {
160+
RETURN_FALSE;
161+
}
166162
ZVAL_RESOURCE(return_value, result->resource_id);
167163
}
168164
/* }}} */
@@ -220,11 +216,9 @@ PHP_FUNCTION(git_merge_trees)
220216
if (php_git2_check_error(error, "git_merge_trees" TSRMLS_CC)) {
221217
RETURN_FALSE;
222218
}
223-
PHP_GIT2_MAKE_RESOURCE(result);
224-
PHP_GIT2_V(result, index) = out;
225-
result->type = PHP_GIT2_TYPE_INDEX;
226-
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
227-
result->should_free_v = 0;
219+
if (php_git2_make_resource(&result, PHP_GIT2_TYPE_INDEX, out, 0 TSRMLS_CC)) {
220+
RETURN_FALSE;
221+
}
228222
ZVAL_RESOURCE(return_value, result->resource_id);
229223
}
230224
/* }}} */
@@ -255,11 +249,9 @@ PHP_FUNCTION(git_merge)
255249
if (php_git2_check_error(error, "git_merge" TSRMLS_CC)) {
256250
RETURN_FALSE;
257251
}
258-
PHP_GIT2_MAKE_RESOURCE(result);
259-
PHP_GIT2_V(result, merge_result) = out;
260-
result->type = PHP_GIT2_TYPE_MERGE_RESULT;
261-
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
262-
result->should_free_v = 0;
252+
if (php_git2_make_resource(&result, PHP_GIT2_TYPE_MERGE_RESULT, out, 0 TSRMLS_CC)) {
253+
RETURN_FALSE;
254+
}
263255
ZVAL_RESOURCE(return_value, result->resource_id);
264256
}
265257
/* }}} */
@@ -346,8 +338,9 @@ PHP_FUNCTION(git_merge_result_free)
346338
}
347339

348340
ZEND_FETCH_RESOURCE(_merge_result, php_git2_t*, &merge_result, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
349-
if (_merge_result->should_free_v) {
341+
if (GIT2_SHOULD_FREE(_merge_result)) {
350342
git_merge_result_free(PHP_GIT2_V(_merge_result, merge_result));
343+
GIT2_SHOULD_FREE(_merge_result) = 0;
351344
};
352345
zval_ptr_dtor(&merge_result);
353346
}

odb.c

+20-21
Original file line numberDiff line numberDiff line change
@@ -241,31 +241,32 @@ PHP_FUNCTION(git_odb_foreach)
241241
if (php_git2_cb_init(&cb, &fci, &fcc, payload TSRMLS_CC)) {
242242
RETURN_FALSE;
243243
}
244+
// TODO(chobie): implment callback */
244245
//result = git_odb_foreach(PHP_GIT2_V(_db, odb), <CHANGEME>, cb);
245246
php_git2_cb_free(cb);
246247
RETURN_LONG(result);
247248
}
248249
/* }}} */
249250

250-
/* {{{ proto resource git_odb_write(resource $odb, $data, long $len, $type)
251+
/* {{{ proto resource git_odb_write(resource $odb, $data, $type)
251252
*/
252253
PHP_FUNCTION(git_odb_write)
253254
{
254255
php_git2_t *result = NULL, *_odb = NULL;
255256
git_oid out = {0};
256-
zval *odb = NULL, *type = NULL;
257+
zval *odb = NULL;
257258
zval *data = NULL;
258-
long len = 0;
259-
int error = 0;
259+
long type = 0;
260+
int error = 0, data_len = 0;
260261
char buf[41] = {0};
261262

262263
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
263-
"r<void>l<git_otype>", &odb, &data, &len, &type) == FAILURE) {
264+
"rsl", &odb, &data, &data_len, &type) == FAILURE) {
264265
return;
265266
}
266267

267268
ZEND_FETCH_RESOURCE(_odb, php_git2_t*, &odb, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
268-
error = git_odb_write(&out, PHP_GIT2_V(_odb, odb), data, len, type);
269+
error = git_odb_write(&out, PHP_GIT2_V(_odb, odb), data, data_len, type);
269270
if (php_git2_check_error(error, "git_odb_write" TSRMLS_CC)) {
270271
RETURN_FALSE;
271272
}
@@ -280,12 +281,12 @@ PHP_FUNCTION(git_odb_open_wstream)
280281
{
281282
php_git2_t *result = NULL, *_db = NULL;
282283
git_odb_stream *out = NULL;
283-
zval *db = NULL, *type = NULL;
284-
long size = 0;
284+
zval *db = NULL;
285+
long size = 0, type = 0;
285286
int error = 0;
286287

287288
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
288-
"rl<git_otype>", &db, &size, &type) == FAILURE) {
289+
"rll", &db, &size, &type) == FAILURE) {
289290
return;
290291
}
291292

@@ -301,23 +302,22 @@ PHP_FUNCTION(git_odb_open_wstream)
301302
}
302303
/* }}} */
303304

304-
/* {{{ proto long git_odb_stream_write(resource $stream, string $buffer, long $len)
305+
/* {{{ proto long git_odb_stream_write(resource $stream, string $buffer)
305306
*/
306307
PHP_FUNCTION(git_odb_stream_write)
307308
{
308309
int result = 0, buffer_len = 0, error = 0;
309310
zval *stream = NULL;
310311
php_git2_t *_stream = NULL;
311312
char *buffer = NULL;
312-
long len = 0;
313313

314314
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
315-
"rsl", &stream, &buffer, &buffer_len, &len) == FAILURE) {
315+
"rs", &stream, &buffer, &buffer_len) == FAILURE) {
316316
return;
317317
}
318318

319319
ZEND_FETCH_RESOURCE(_stream, php_git2_t*, &stream, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
320-
result = git_odb_stream_write(PHP_GIT2_V(_stream, odb_stream), buffer, len);
320+
result = git_odb_stream_write(PHP_GIT2_V(_stream, odb_stream), buffer, buffer_len);
321321
RETURN_LONG(result);
322322
}
323323
/* }}} */
@@ -452,23 +452,22 @@ PHP_FUNCTION(git_odb_write_pack)
452452
}
453453
/* }}} */
454454

455-
/* {{{ proto resource git_odb_hash( $data, long $len, $type)
455+
/* {{{ proto resource git_odb_hash(string $data, long $type)
456456
*/
457457
PHP_FUNCTION(git_odb_hash)
458458
{
459459
php_git2_t *result = NULL;
460460
git_oid out = {0};
461461
zval *data = NULL;
462-
long len = 0;
463-
zval *type = NULL;
464-
int error = 0;
462+
int error = 0, data_len = 0;
463+
long type = 0;
465464
char buf[41] = {0};
466465

467466
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
468-
"<void>l<git_otype>", &data, &len, &type) == FAILURE) {
467+
"sl", &data, &data_len, &type) == FAILURE) {
469468
return;
470469
}
471-
error = git_odb_hash(&out, data, len, type);
470+
error = git_odb_hash(&out, data, data_len, type);
472471
if (php_git2_check_error(error, "git_odb_hash" TSRMLS_CC)) {
473472
RETURN_FALSE;
474473
}
@@ -485,11 +484,11 @@ PHP_FUNCTION(git_odb_hashfile)
485484
git_oid out = {0};
486485
char *path = NULL;
487486
int path_len = 0, error = 0;
488-
zval *type = NULL;
489487
char buf[41] = {0};
488+
long type = 0;
490489

491490
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
492-
"s<git_otype>", &path, &path_len, &type) == FAILURE) {
491+
"sl", &path, &path_len, &type) == FAILURE) {
493492
return;
494493
}
495494
error = git_odb_hashfile(&out, path, type);

0 commit comments

Comments
 (0)