Skip to content

Commit faf4e4a

Browse files
committed
[object] implement several functions
1 parent ea6568e commit faf4e4a

File tree

2 files changed

+102
-57
lines changed

2 files changed

+102
-57
lines changed

object.c

+100-57
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,34 @@
77
PHP_FUNCTION(git_object_lookup)
88
{
99
zval *repo;
10-
php_git2_t *_repo;
10+
php_git2_t *_repo, *result;
1111
char *id = {0};
1212
int id_len;
1313
zval *type;
1414
php_git2_t *_type;
15-
16-
/* TODO(chobie): implement this */
17-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "git_object_lookup not implemented yet");
18-
return;
15+
int error = 0;
16+
git_object *object;
17+
git_oid oid;
1918

2019
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
2120
"rsr", &repo, &id, &id_len, &type) == FAILURE) {
2221
return;
2322
}
2423
ZEND_FETCH_RESOURCE(_repo, php_git2_t*, &repo, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
24+
if (git_oid_fromstrn(&oid, id, id_len) != GIT_OK) {
25+
return;
26+
}
27+
error = git_object_lookup(&object, PHP_GIT2_V(_repo, repository), &oid, type);
28+
if (php_git2_check_error(error, "git_object_lookup" TSRMLS_CC)) {
29+
RETURN_FALSE
30+
}
31+
PHP_GIT2_MAKE_RESOURCE(result);
32+
PHP_GIT2_V(result, object) = object;
33+
result->type = PHP_GIT2_TYPE_OBJECT;
34+
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
35+
result->should_free_v = 1;
36+
37+
ZVAL_RESOURCE(return_value, result->resource_id);
2538
}
2639

2740
/* {{{ proto resource git_object_lookup_prefix(repo, id, len, type)
@@ -51,21 +64,31 @@ PHP_FUNCTION(git_object_lookup_prefix)
5164
PHP_FUNCTION(git_object_lookup_bypath)
5265
{
5366
zval *treeish;
54-
php_git2_t *_treeish;
67+
php_git2_t *_treeish, *result;
5568
char *path = {0};
5669
int path_len;
5770
zval *type;
5871
php_git2_t *_type;
59-
60-
/* TODO(chobie): implement this */
61-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "git_object_lookup_bypath not implemented yet");
62-
return;
72+
git_object *object;
73+
int error;
6374

6475
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
6576
"rsr", &treeish, &path, &path_len, &type) == FAILURE) {
6677
return;
6778
}
6879
ZEND_FETCH_RESOURCE(_treeish, php_git2_t*, &treeish, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
80+
// TODO: cast object
81+
error = git_object_lookup_bypath(&object, PHP_GIT2_V(_treeish, tree), path, type);
82+
if (php_git2_check_error(error, "git_object_lookup_bypath" TSRMLS_CC)) {
83+
RETURN_FALSE
84+
}
85+
PHP_GIT2_MAKE_RESOURCE(result);
86+
PHP_GIT2_V(result, object) = object;
87+
result->type = PHP_GIT2_TYPE_OBJECT;
88+
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
89+
result->should_free_v = 0;
90+
91+
ZVAL_RESOURCE(return_value, result->resource_id);
6992
}
7093

7194
/* {{{ proto resource git_object_id(obj)
@@ -74,16 +97,17 @@ PHP_FUNCTION(git_object_id)
7497
{
7598
zval *obj;
7699
php_git2_t *_obj;
77-
78-
/* TODO(chobie): implement this */
79-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "git_object_id not implemented yet");
80-
return;
100+
const git_oid *id;
101+
char buf[41] = {0};
81102

82103
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
83104
"r", &obj) == FAILURE) {
84105
return;
85106
}
86107
ZEND_FETCH_RESOURCE(_obj, php_git2_t*, &obj, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
108+
id = git_object_id(PHP_GIT2_V(_obj, object));
109+
git_oid_fmt(buf, id);
110+
RETURN_STRING(buf, 1);
87111
}
88112

89113
/* {{{ proto resource git_object_type(obj)
@@ -92,34 +116,39 @@ PHP_FUNCTION(git_object_type)
92116
{
93117
zval *obj;
94118
php_git2_t *_obj;
95-
96-
/* TODO(chobie): implement this */
97-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "git_object_type not implemented yet");
98-
return;
119+
const git_otype *type;
99120

100121
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
101122
"r", &obj) == FAILURE) {
102123
return;
103124
}
104125
ZEND_FETCH_RESOURCE(_obj, php_git2_t*, &obj, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
126+
type = git_object_type(PHP_GIT2_V(_obj, object));
127+
RETURN_LONG(type);
105128
}
106129

107130
/* {{{ proto resource git_object_owner(obj)
108131
*/
109132
PHP_FUNCTION(git_object_owner)
110133
{
111134
zval *obj;
112-
php_git2_t *_obj;
113-
114-
/* TODO(chobie): implement this */
115-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "git_object_owner not implemented yet");
116-
return;
135+
php_git2_t *_obj, *result;
136+
git_repository *repository;
117137

118138
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
119139
"r", &obj) == FAILURE) {
120140
return;
121141
}
122142
ZEND_FETCH_RESOURCE(_obj, php_git2_t*, &obj, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
143+
// TODO: consider cast
144+
repository = git_object_owner(PHP_GIT2_V(_obj, object));
145+
PHP_GIT2_MAKE_RESOURCE(result);
146+
PHP_GIT2_V(result, repository) = repository;
147+
result->type = PHP_GIT2_TYPE_REPOSITORY;
148+
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
149+
result->should_free_v = 0;
150+
151+
ZVAL_RESOURCE(return_value, result->resource_id);
123152
}
124153

125154
/* {{{ proto void git_object_free(object)
@@ -129,15 +158,16 @@ PHP_FUNCTION(git_object_free)
129158
zval *object;
130159
php_git2_t *_object;
131160

132-
/* TODO(chobie): implement this */
133-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "git_object_free not implemented yet");
134-
return;
135-
136161
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
137162
"r", &object) == FAILURE) {
138163
return;
139164
}
140165
ZEND_FETCH_RESOURCE(_object, php_git2_t*, &object, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
166+
if (_object->should_free_v) {
167+
git_object_free(PHP_GIT2_V(_object, object));
168+
_object->should_free_v = 0;
169+
}
170+
zval_ptr_dtor(&object);
141171
}
142172

143173
/* {{{ proto resource git_object_type2string(type)
@@ -146,16 +176,16 @@ PHP_FUNCTION(git_object_type2string)
146176
{
147177
zval *type;
148178
php_git2_t *_type;
149-
150-
/* TODO(chobie): implement this */
151-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "git_object_type2string not implemented yet");
152-
return;
179+
const char *result;
153180

154181
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
155182
"r", &type) == FAILURE) {
156183
return;
157184
}
158185
ZEND_FETCH_RESOURCE(_type, php_git2_t*, &type, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
186+
// TODO: consider cast
187+
result = git_object_type2string(PHP_GIT2_V(_type, object));
188+
RETURN_STRING(result, 1);
159189
}
160190

161191
/* {{{ proto resource git_object_string2type(str)
@@ -164,15 +194,14 @@ PHP_FUNCTION(git_object_string2type)
164194
{
165195
char *str = {0};
166196
int str_len;
167-
168-
/* TODO(chobie): implement this */
169-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "git_object_string2type not implemented yet");
170-
return;
197+
git_otype type;
171198

172199
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
173200
"s", &str, &str_len) == FAILURE) {
174201
return;
175202
}
203+
type = git_object_string2type(str);
204+
RETURN_LONG(type);
176205
}
177206

178207
/* {{{ proto long git_object_typeisloose(type)
@@ -181,71 +210,85 @@ PHP_FUNCTION(git_object_typeisloose)
181210
{
182211
zval *type;
183212
php_git2_t *_type;
184-
185-
/* TODO(chobie): implement this */
186-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "git_object_typeisloose not implemented yet");
187-
return;
213+
int error = 0;
188214

189215
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
190216
"r", &type) == FAILURE) {
191217
return;
192218
}
193219
ZEND_FETCH_RESOURCE(_type, php_git2_t*, &type, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
220+
error = git_object_typeisloose(PHP_GIT2_V(_type, object));
221+
if (php_git2_check_error(error, "git_object_typeisloose" TSRMLS_CC)) {
222+
RETURN_FALSE
223+
}
224+
RETURN_TRUE;
194225
}
195226

196227
/* {{{ proto resource git_object__size(type)
197228
*/
198229
PHP_FUNCTION(git_object__size)
199230
{
200-
zval *type;
201-
php_git2_t *_type;
202-
203-
/* TODO(chobie): implement this */
204-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "git_object__size not implemented yet");
205-
return;
231+
long type;
232+
size_t size;
206233

207234
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
208-
"r", &type) == FAILURE) {
235+
"l", &type) == FAILURE) {
209236
return;
210237
}
211-
ZEND_FETCH_RESOURCE(_type, php_git2_t*, &type, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
238+
size = git_object__size(type);
239+
RETURN_LONG(size);
212240
}
213241

214242
/* {{{ proto resource git_object_peel(object, target_type)
215243
*/
216244
PHP_FUNCTION(git_object_peel)
217245
{
218246
zval *object;
219-
php_git2_t *_object;
247+
php_git2_t *_object, *result;
220248
zval *target_type;
221249
php_git2_t *_target_type;
222-
223-
/* TODO(chobie): implement this */
224-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "git_object_peel not implemented yet");
225-
return;
250+
int error = 0;
251+
git_object *out;
226252

227253
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
228254
"rr", &object, &target_type) == FAILURE) {
229255
return;
230256
}
231257
ZEND_FETCH_RESOURCE(_object, php_git2_t*, &object, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
258+
error = git_object_peel(&out, PHP_GIT2_V(_object, object), target_type);
259+
if (php_git2_check_error(error, "git_object_peel" TSRMLS_CC)) {
260+
RETURN_FALSE
261+
}
262+
PHP_GIT2_MAKE_RESOURCE(result);
263+
PHP_GIT2_V(result, object) = object;
264+
result->type = PHP_GIT2_TYPE_OBJECT;
265+
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
266+
result->should_free_v = 1;
267+
268+
ZVAL_RESOURCE(return_value, result->resource_id);
232269
}
233270

234271
/* {{{ proto resource git_object_dup(source)
235272
*/
236273
PHP_FUNCTION(git_object_dup)
237274
{
238275
zval *source;
239-
php_git2_t *_source;
240-
241-
/* TODO(chobie): implement this */
242-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "git_object_dup not implemented yet");
243-
return;
276+
php_git2_t *_source, *result;
277+
git_object *dest;
278+
int error = 0;
244279

245280
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
246281
"r", &source) == FAILURE) {
247282
return;
248283
}
249284
ZEND_FETCH_RESOURCE(_source, php_git2_t*, &source, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
285+
error = git_object_dup(&dest, PHP_GIT2_V(_source, object));
286+
PHP_GIT2_MAKE_RESOURCE(result);
287+
PHP_GIT2_V(result, object) = dest;
288+
result->type = PHP_GIT2_TYPE_OBJECT;
289+
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
290+
result->should_free_v = 1;
291+
292+
ZVAL_RESOURCE(return_value, result->resource_id);
250293
}
251294

php_git2.c

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ void static destruct_git2(zend_rsrc_list_entry *rsrc TSRMLS_DC)
6666
git_reference_free(PHP_GIT2_V(resource, reference));
6767
case PHP_GIT2_TYPE_CONFIG:
6868
git_config_free(PHP_GIT2_V(resource, config));
69+
case PHP_GIT2_TYPE_OBJECT:
70+
git_object_free(PHP_GIT2_V(resource, object));
6971
default:
7072
break;
7173
}

0 commit comments

Comments
 (0)