Skip to content

Commit b60e455

Browse files
committed
improve transport
1 parent e20f10d commit b60e455

File tree

2 files changed

+62
-79
lines changed

2 files changed

+62
-79
lines changed

transport.c

+28-32
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,6 @@
22
#include "php_git2_priv.h"
33
#include "transport.h"
44

5-
/* {{{ proto resource git_transport_new(resource $owner, string $url)
6-
*/
7-
PHP_FUNCTION(git_transport_new)
8-
{
9-
php_git2_t *result = NULL;
10-
git_transport *out = NULL;
11-
zval *owner = NULL;
12-
php_git2_t *_owner = NULL;
13-
char *url = NULL;
14-
int url_len = 0;
15-
int error = 0;
16-
17-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
18-
"rs", &owner, &url, &url_len) == FAILURE) {
19-
return;
20-
}
21-
22-
ZEND_FETCH_RESOURCE(_owner, php_git2_t*, &owner, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
23-
error = git_transport_new(&out, PHP_GIT2_V(_owner, remote), url);
24-
if (php_git2_check_error(error, "git_transport_new" TSRMLS_CC)) {
25-
RETURN_FALSE;
26-
}
27-
PHP_GIT2_MAKE_RESOURCE(result);
28-
PHP_GIT2_V(result, transport) = out;
29-
result->type = PHP_GIT2_TYPE_TRANSPORT;
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);
33-
}
34-
/* }}} */
35-
36-
//typedef int (*git_transport_cb)(git_transport **out, git_remote *owner, void *param);
375
static int php_git2_transport_cb(git_transport **out, git_remote *owner, void *param)
386
{
397
php_git2_t *result;
@@ -62,6 +30,34 @@ static int php_git2_transport_cb(git_transport **out, git_remote *owner, void *p
6230
return retval;
6331
}
6432

33+
34+
/* {{{ proto resource git_transport_new(resource $owner, string $url)
35+
*/
36+
PHP_FUNCTION(git_transport_new)
37+
{
38+
php_git2_t *result = NULL, *_owner = NULL;
39+
git_transport *out = NULL;
40+
zval *owner = NULL;
41+
char *url = NULL;
42+
int url_len = 0, error = 0;
43+
44+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
45+
"rs", &owner, &url, &url_len) == FAILURE) {
46+
return;
47+
}
48+
49+
ZEND_FETCH_RESOURCE(_owner, php_git2_t*, &owner, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
50+
error = git_transport_new(&out, PHP_GIT2_V(_owner, remote), url);
51+
if (php_git2_check_error(error, "git_transport_new" TSRMLS_CC)) {
52+
RETURN_FALSE;
53+
}
54+
if (php_git2_make_resource(&result, PHP_GIT2_TYPE_TRANSPORT, out, 1 TSRMLS_CC)) {
55+
RETURN_FALSE;
56+
}
57+
ZVAL_RESOURCE(return_value, GIT2_RVAL_P(result));
58+
}
59+
/* }}} */
60+
6561
/* {{{ proto long git_transport_register(string $prefix, $priority, Callable $cb, $param)
6662
*/
6763
PHP_FUNCTION(git_transport_register)

treebuilder.c

+34-47
Original file line numberDiff line numberDiff line change
@@ -36,38 +36,35 @@ static int php_git2_treebuilder_filter_cb(const git_tree_entry *entry, void *pay
3636

3737

3838
/* {{{ proto resource git_treebuilder_create([resource $source])
39-
*/
39+
*/
4040
PHP_FUNCTION(git_treebuilder_create)
4141
{
42+
php_git2_t *result = NULL, *_source = NULL;
43+
git_treebuilder *out = NULL;
4244
zval *source = NULL;
43-
php_git2_t *_source, *result;
44-
git_treebuilder *builder;
4545
git_tree *tree = NULL;
4646
int error = 0;
4747

4848
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
4949
"|r", &source) == FAILURE) {
5050
return;
5151
}
52-
5352
if (source != NULL) {
54-
ZEND_FETCH_RESOURCE(_source, php_git2_t*, &source, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
5553
tree = PHP_GIT2_V(_source, tree);
5654
}
5755

58-
error = git_treebuilder_create(&builder, tree);
56+
ZEND_FETCH_RESOURCE(_source, php_git2_t*, &source, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
57+
error = git_treebuilder_create(&out, tree);
5958
if (php_git2_check_error(error, "git_treebuilder_create" TSRMLS_CC)) {
6059
RETURN_FALSE;
6160
}
62-
63-
PHP_GIT2_MAKE_RESOURCE(result);
64-
PHP_GIT2_V(result, treebuilder) = builder;
65-
result->type = PHP_GIT2_TYPE_TREEBUILDER;
66-
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
67-
result->should_free_v = 1;
68-
69-
ZVAL_RESOURCE(return_value, result->resource_id);
61+
if (php_git2_make_resource(&result, PHP_GIT2_TYPE_TREEBUILDER, out, 1 TSRMLS_CC)) {
62+
RETURN_FALSE;
63+
}
64+
ZVAL_RESOURCE(return_value, GIT2_RVAL_P(result));
7065
}
66+
/* }}} */
67+
7168

7269
/* {{{ proto void git_treebuilder_clear(bld)
7370
*/
@@ -135,57 +132,47 @@ PHP_FUNCTION(git_treebuilder_get)
135132
ZEND_FETCH_RESOURCE(_bld, php_git2_t*, &bld, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
136133
entry = git_treebuilder_get(PHP_GIT2_V(_bld, treebuilder), filename);
137134
if (entry != NULL) {
138-
PHP_GIT2_MAKE_RESOURCE(result);
139-
PHP_GIT2_V(result, tree_entry) = entry;
140-
result->type = PHP_GIT2_TYPE_TREE_ENTRY;
141-
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
142-
result->should_free_v = 0;
143-
135+
if (php_git2_make_resource(&result, PHP_GIT2_TYPE_TREE_ENTRY, entry, 0 TSRMLS_CC)) {
136+
RETURN_FALSE;
137+
}
144138
ZVAL_RESOURCE(return_value, result->resource_id);
145139
} else {
146140
RETURN_FALSE;
147141
}
148142
}
149143

150-
/* {{{ proto resource git_treebuilder_insert(bld, filename, id, filemode)
151-
*/
144+
/* {{{ proto resource git_treebuilder_insert(resource $bld, string $filename, string $id, $filemode)
145+
*/
152146
PHP_FUNCTION(git_treebuilder_insert)
153147
{
154-
zval *bld;
155-
php_git2_t *_bld, *result;
156-
char *filename = {0};
157-
int filename_len;
158-
char *id = {0};
159-
int id_len;
160-
long filemode;
161-
const git_tree_entry *entry;
162-
int error = 0;
163-
git_oid oid;
148+
php_git2_t *result = NULL, *_bld = NULL;
149+
git_tree_entry *out = NULL;
150+
zval *bld = NULL;
151+
char *filename = NULL, *id = NULL;
152+
int filename_len = 0, id_len = 0, error = 0;
153+
git_oid __id = {0};
154+
long filemode = 0;
164155

165156
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
166157
"rssl", &bld, &filename, &filename_len, &id, &id_len, &filemode) == FAILURE) {
167158
return;
168159
}
169160

170-
if (git_oid_fromstrn(&oid, id, id_len) != GIT_OK) {
171-
return;
172-
}
173-
174161
ZEND_FETCH_RESOURCE(_bld, php_git2_t*, &bld, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
175-
error = git_treebuilder_insert(&entry, PHP_GIT2_V(_bld, treebuilder), filename, &oid, filemode);
176-
177-
if (php_git2_check_error(error, "git_treebuilder_write" TSRMLS_CC)) {
162+
if (git_oid_fromstrn(&__id, id, id_len)) {
178163
RETURN_FALSE;
179164
}
180-
181-
PHP_GIT2_MAKE_RESOURCE(result);
182-
PHP_GIT2_V(result, tree_entry) = entry;
183-
result->type = PHP_GIT2_TYPE_TREE_ENTRY;
184-
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
185-
result->should_free_v = 0;
186-
187-
ZVAL_RESOURCE(return_value, result->resource_id);
165+
error = git_treebuilder_insert(&out, PHP_GIT2_V(_bld, treebuilder), filename, &__id, filemode);
166+
if (php_git2_check_error(error, "git_treebuilder_insert" TSRMLS_CC)) {
167+
RETURN_FALSE;
168+
}
169+
if (php_git2_make_resource(&result, PHP_GIT2_TYPE_TREE_ENTRY, out, 1 TSRMLS_CC)) {
170+
RETURN_FALSE;
171+
}
172+
ZVAL_RESOURCE(return_value, GIT2_RVAL_P(result));
188173
}
174+
/* }}} */
175+
189176

190177
/* {{{ proto long git_treebuilder_remove(bld, filename)
191178
*/

0 commit comments

Comments
 (0)