Skip to content

Commit 07f5b41

Browse files
committed
improve codes
1 parent 5f0b526 commit 07f5b41

File tree

2 files changed

+179
-93
lines changed

2 files changed

+179
-93
lines changed

commit.c

+86-83
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,31 @@
22
#include "php_git2_priv.h"
33
#include "commit.h"
44

5-
/* {{{ proto resource git_commit_lookup(resource $repository, mixed $oid)
6-
*/
5+
/* {{{ proto long git_commit_lookup(resource $repo, string $id)
6+
*/
77
PHP_FUNCTION(git_commit_lookup)
88
{
9-
zval *repository;
10-
php_git2_t *git2, *result;
11-
git_commit *commit;
12-
char *hash;
13-
int hash_len;
14-
int error;
15-
git_oid id;
16-
9+
int result = 0, id_len = 0, error = 0;
10+
git_commit *commit = NULL;
11+
zval *repo = NULL;
12+
php_git2_t *_repo = NULL, *_result = NULL;
13+
char *id = NULL;
14+
git_oid __id = {0};
15+
1716
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
18-
"rs", &repository, &hash, &hash_len) == FAILURE) {
17+
"rs", &repo, &id, &id_len) == FAILURE) {
1918
return;
2019
}
21-
22-
if (git_oid_fromstrn(&id, hash, hash_len) != GIT_OK) {
23-
return;
20+
21+
ZEND_FETCH_RESOURCE(_repo, php_git2_t*, &repo, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
22+
if (git_oid_fromstrn(&__id, id, id_len)) {
23+
RETURN_FALSE;
2424
}
25-
26-
ZEND_FETCH_RESOURCE(git2, php_git2_t*, &repository, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
27-
28-
PHP_GIT2_MAKE_RESOURCE(result);
29-
error = git_commit_lookup(&commit, PHP_GIT2_V(git2, repository), &id);
30-
if (php_git2_check_error(error, "git_commit_lookup" TSRMLS_CC)) {
31-
RETURN_FALSE
25+
result = git_commit_lookup(&commit, PHP_GIT2_V(_repo, repository), &__id);
26+
if (php_git2_make_resource(&_result, PHP_GIT2_TYPE_COMMIT, commit, 0 TSRMLS_CC)) {
27+
RETURN_FALSE;
3228
}
33-
34-
PHP_GIT2_V(result, commit) = commit;
35-
result->type = PHP_GIT2_TYPE_COMMIT;
36-
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
37-
result->should_free_v = 0;
38-
39-
ZVAL_RESOURCE(return_value, result->resource_id);
29+
ZVAL_RESOURCE(return_value, GIT2_RVAL_P(_result));
4030
}
4131
/* }}} */
4232

@@ -64,102 +54,103 @@ PHP_FUNCTION(git_commit_author)
6454
/* }}} */
6555

6656
/* {{{ proto resource git_commit_tree(resource $commit)
67-
*/
57+
*/
6858
PHP_FUNCTION(git_commit_tree)
6959
{
70-
zval *repository;
71-
php_git2_t *git2, *result;
72-
git_commit *commit;
73-
git_tree *tree;
74-
int error;
60+
php_git2_t *result = NULL, *_commit = NULL;
61+
git_tree *tree_out = NULL;
62+
zval *commit = NULL;
63+
int error = 0;
7564

7665
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
7766
"r", &commit) == FAILURE) {
7867
return;
7968
}
8069

81-
ZEND_FETCH_RESOURCE(git2, php_git2_t*, &commit, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
82-
83-
PHP_GIT2_MAKE_RESOURCE(result);
84-
error = git_commit_tree(&tree, PHP_GIT2_V(git2, commit));
70+
ZEND_FETCH_RESOURCE(_commit, php_git2_t*, &commit, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
71+
error = git_commit_tree(&tree_out, PHP_GIT2_V(_commit, commit));
8572
if (php_git2_check_error(error, "git_commit_tree" TSRMLS_CC)) {
86-
RETURN_FALSE
73+
RETURN_FALSE;
8774
}
88-
89-
PHP_GIT2_V(result, tree) = tree;
90-
result->type = PHP_GIT2_TYPE_TREE;
91-
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
92-
result->should_free_v = 0;
93-
94-
ZVAL_RESOURCE(return_value, result->resource_id);
75+
if (php_git2_make_resource(&result, PHP_GIT2_TYPE_TREE, tree_out, 1 TSRMLS_CC)) {
76+
RETURN_FALSE;
77+
}
78+
ZVAL_RESOURCE(return_value, GIT2_RVAL_P(result));
9579
}
80+
/* }}} */
9681

97-
/* {{{ proto resource git_commit_lookup_prefix(repo, id, len)
98-
*/
82+
/* {{{ proto long git_commit_lookup_prefix(resource $repo, string $id, long $len)
83+
*/
9984
PHP_FUNCTION(git_commit_lookup_prefix)
10085
{
101-
zval *repo;
102-
php_git2_t *_repo;
103-
char *id = {0};
104-
int id_len;
105-
long len;
106-
107-
/* TODO(chobie): implement this */
108-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "git_commit_lookup_prefix not implemented yet");
109-
return;
86+
int result = 0, id_len = 0, error = 0;
87+
git_commit *commit = NULL;
88+
zval *repo = NULL;
89+
php_git2_t *_repo = NULL, *_result = NULL;
90+
char *id = NULL;
91+
git_oid __id = {0};
92+
long len = 0;
11093

11194
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
11295
"rsl", &repo, &id, &id_len, &len) == FAILURE) {
11396
return;
11497
}
98+
11599
ZEND_FETCH_RESOURCE(_repo, php_git2_t*, &repo, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
100+
if (git_oid_fromstrn(&__id, id, id_len)) {
101+
RETURN_FALSE;
102+
}
103+
result = git_commit_lookup_prefix(&commit, PHP_GIT2_V(_repo, repository), &__id, len);
104+
if (php_git2_make_resource(&_result, PHP_GIT2_TYPE_COMMIT, commit, 0 TSRMLS_CC)) {
105+
RETURN_FALSE;
106+
}
107+
ZVAL_RESOURCE(return_value, GIT2_RVAL_P(_result));
116108
}
109+
/* }}} */
117110

118-
/* {{{ proto resource git_commit_id(commit)
119-
*/
111+
/* {{{ proto resource git_commit_id(resource $commit)
112+
*/
120113
PHP_FUNCTION(git_commit_id)
121114
{
122-
zval *commit;
123-
php_git2_t *_commit;
124-
char out[GIT2_OID_HEXSIZE] = {0};
125-
const git_oid *id;
115+
const git_oid *result = NULL;
116+
zval *commit = NULL;
117+
php_git2_t *_commit = NULL;
118+
char __result[GIT2_OID_HEXSIZE] = {0};
126119

127120
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
128121
"r", &commit) == FAILURE) {
129122
return;
130123
}
131-
ZEND_FETCH_RESOURCE(_commit, php_git2_t*, &commit, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
132-
133-
id = git_blob_id(PHP_GIT2_V(_commit, commit));
134124

135-
git_oid_fmt(out, id);
136-
RETURN_STRING(out, 1);
125+
ZEND_FETCH_RESOURCE(_commit, php_git2_t*, &commit, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
126+
result = git_commit_id(PHP_GIT2_V(_commit, commit));
127+
git_oid_fmt(__result, result);
128+
RETURN_STRING(__result, 1);
137129
}
130+
/* }}} */
138131

139-
/* {{{ proto resource git_commit_owner(commit)
140-
*/
132+
/* {{{ proto resource git_commit_owner(resource $commit)
133+
*/
141134
PHP_FUNCTION(git_commit_owner)
142135
{
143-
zval *commit;
144-
php_git2_t *_commit, *result;
145-
git_repository *repository;
136+
git_repository *result = NULL;
137+
zval *commit = NULL;
138+
php_git2_t *_commit = NULL, *__result = NULL;
146139

147140
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
148141
"r", &commit) == FAILURE) {
149142
return;
150143
}
151-
ZEND_FETCH_RESOURCE(_commit, php_git2_t*, &commit, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
152-
153-
PHP_GIT2_MAKE_RESOURCE(result);
154-
repository = git_commit_owner(PHP_GIT2_V(_commit, commit));
155-
156-
PHP_GIT2_V(result, repository) = repository;
157-
result->type = PHP_GIT2_TYPE_REPOSITORY;
158-
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
159-
result->should_free_v = 0;
160144

161-
ZVAL_RESOURCE(return_value, result->resource_id);
145+
ZEND_FETCH_RESOURCE(_commit, php_git2_t*, &commit, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
146+
result = git_commit_owner(PHP_GIT2_V(_commit, commit));
147+
if (php_git2_make_resource(&__result, PHP_GIT2_TYPE_COMMIT, result, 1 TSRMLS_CC)) {
148+
RETURN_FALSE;
149+
}
150+
ZVAL_RESOURCE(return_value, GIT2_RVAL_P(__result));
162151
}
152+
/* }}} */
153+
163154

164155
/* {{{ proto resource git_commit_message_encoding(commit)
165156
*/
@@ -177,6 +168,7 @@ PHP_FUNCTION(git_commit_message_encoding)
177168
encoding = git_commit_message_encoding(PHP_GIT2_V(_commit, commit));
178169
RETURN_STRING(encoding, 1);
179170
}
171+
/* }}} */
180172

181173
/* {{{ proto resource git_commit_message(commit)
182174
*/
@@ -194,6 +186,7 @@ PHP_FUNCTION(git_commit_message)
194186
message = git_commit_message(PHP_GIT2_V(_commit, commit));
195187
RETURN_STRING(message, 1);
196188
}
189+
/* }}} */
197190

198191
/* {{{ proto resource git_commit_message_raw(commit)
199192
*/
@@ -212,6 +205,7 @@ PHP_FUNCTION(git_commit_message_raw)
212205
message = git_commit_message_raw(PHP_GIT2_V(_commit, commit));
213206
RETURN_STRING(message, 1);
214207
}
208+
/* }}} */
215209

216210
/* {{{ proto resource git_commit_time(commit)
217211
*/
@@ -231,6 +225,7 @@ PHP_FUNCTION(git_commit_time)
231225
/* NOTE(chobie) should this return as a string? */
232226
RETURN_LONG(time);
233227
}
228+
/* }}} */
234229

235230
/* {{{ proto long git_commit_time_offset(commit)
236231
*/
@@ -248,6 +243,7 @@ PHP_FUNCTION(git_commit_time_offset)
248243
result = git_commit_time_offset(PHP_GIT2_V(_commit, commit));
249244
RETURN_LONG(result);
250245
}
246+
/* }}} */
251247

252248
/* {{{ proto resource git_commit_committer(commit)
253249
*/
@@ -268,6 +264,7 @@ PHP_FUNCTION(git_commit_committer)
268264
php_git2_signature_to_array(committer, &result TSRMLS_CC);
269265
RETURN_ZVAL(result, 0, 1);
270266
}
267+
/* }}} */
271268

272269
/* {{{ proto resource git_commit_raw_header(commit)
273270
*/
@@ -286,6 +283,7 @@ PHP_FUNCTION(git_commit_raw_header)
286283

287284
RETURN_STRING(header, 1);
288285
}
286+
/* }}} */
289287

290288
/* {{{ proto resource git_commit_tree_id(commit)
291289
*/
@@ -306,6 +304,7 @@ PHP_FUNCTION(git_commit_tree_id)
306304
git_oid_fmt(out, id);
307305
RETURN_STRING(out, 1);
308306
}
307+
/* }}} */
309308

310309
/* {{{ proto resource git_commit_parentcount(commit)
311310
*/
@@ -324,6 +323,7 @@ PHP_FUNCTION(git_commit_parentcount)
324323
count = git_commit_parentcount(PHP_GIT2_V(_commit, commit));
325324
RETURN_LONG(count);
326325
}
326+
/* }}} */
327327

328328
/* {{{ proto resource git_commit_parent(commit, n)
329329
*/
@@ -353,6 +353,7 @@ PHP_FUNCTION(git_commit_parent)
353353

354354
ZVAL_RESOURCE(return_value, result->resource_id);
355355
}
356+
/* }}} */
356357

357358
/* {{{ proto resource git_commit_parent_id(commit, n)
358359
*/
@@ -374,6 +375,7 @@ PHP_FUNCTION(git_commit_parent_id)
374375

375376
RETURN_STRING(out, 1);
376377
}
378+
/* }}} */
377379

378380
/* {{{ proto resource git_commit_nth_gen_ancestor(commit, n)
379381
*/
@@ -405,6 +407,7 @@ PHP_FUNCTION(git_commit_nth_gen_ancestor)
405407
ZVAL_RESOURCE(return_value, result->resource_id);
406408

407409
}
410+
/* }}} */
408411

409412
/* {{{ proto resource git_commit_create(
410413
resource $repo, string $update_ref, array $author, array $committer,

0 commit comments

Comments
 (0)