7
7
PHP_FUNCTION (git_object_lookup )
8
8
{
9
9
zval * repo ;
10
- php_git2_t * _repo ;
10
+ php_git2_t * _repo , * result ;
11
11
char * id = {0 };
12
12
int id_len ;
13
13
zval * type ;
14
14
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 ;
19
18
20
19
if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC ,
21
20
"rsr" , & repo , & id , & id_len , & type ) == FAILURE ) {
22
21
return ;
23
22
}
24
23
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 );
25
38
}
26
39
27
40
/* {{{ proto resource git_object_lookup_prefix(repo, id, len, type)
@@ -51,21 +64,31 @@ PHP_FUNCTION(git_object_lookup_prefix)
51
64
PHP_FUNCTION (git_object_lookup_bypath )
52
65
{
53
66
zval * treeish ;
54
- php_git2_t * _treeish ;
67
+ php_git2_t * _treeish , * result ;
55
68
char * path = {0 };
56
69
int path_len ;
57
70
zval * type ;
58
71
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 ;
63
74
64
75
if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC ,
65
76
"rsr" , & treeish , & path , & path_len , & type ) == FAILURE ) {
66
77
return ;
67
78
}
68
79
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 );
69
92
}
70
93
71
94
/* {{{ proto resource git_object_id(obj)
@@ -74,16 +97,17 @@ PHP_FUNCTION(git_object_id)
74
97
{
75
98
zval * obj ;
76
99
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 };
81
102
82
103
if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC ,
83
104
"r" , & obj ) == FAILURE ) {
84
105
return ;
85
106
}
86
107
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 );
87
111
}
88
112
89
113
/* {{{ proto resource git_object_type(obj)
@@ -92,34 +116,39 @@ PHP_FUNCTION(git_object_type)
92
116
{
93
117
zval * obj ;
94
118
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 ;
99
120
100
121
if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC ,
101
122
"r" , & obj ) == FAILURE ) {
102
123
return ;
103
124
}
104
125
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 );
105
128
}
106
129
107
130
/* {{{ proto resource git_object_owner(obj)
108
131
*/
109
132
PHP_FUNCTION (git_object_owner )
110
133
{
111
134
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 ;
117
137
118
138
if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC ,
119
139
"r" , & obj ) == FAILURE ) {
120
140
return ;
121
141
}
122
142
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 );
123
152
}
124
153
125
154
/* {{{ proto void git_object_free(object)
@@ -129,15 +158,16 @@ PHP_FUNCTION(git_object_free)
129
158
zval * object ;
130
159
php_git2_t * _object ;
131
160
132
- /* TODO(chobie): implement this */
133
- php_error_docref (NULL TSRMLS_CC , E_WARNING , "git_object_free not implemented yet" );
134
- return ;
135
-
136
161
if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC ,
137
162
"r" , & object ) == FAILURE ) {
138
163
return ;
139
164
}
140
165
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 );
141
171
}
142
172
143
173
/* {{{ proto resource git_object_type2string(type)
@@ -146,16 +176,16 @@ PHP_FUNCTION(git_object_type2string)
146
176
{
147
177
zval * type ;
148
178
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 ;
153
180
154
181
if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC ,
155
182
"r" , & type ) == FAILURE ) {
156
183
return ;
157
184
}
158
185
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 );
159
189
}
160
190
161
191
/* {{{ proto resource git_object_string2type(str)
@@ -164,15 +194,14 @@ PHP_FUNCTION(git_object_string2type)
164
194
{
165
195
char * str = {0 };
166
196
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 ;
171
198
172
199
if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC ,
173
200
"s" , & str , & str_len ) == FAILURE ) {
174
201
return ;
175
202
}
203
+ type = git_object_string2type (str );
204
+ RETURN_LONG (type );
176
205
}
177
206
178
207
/* {{{ proto long git_object_typeisloose(type)
@@ -181,71 +210,85 @@ PHP_FUNCTION(git_object_typeisloose)
181
210
{
182
211
zval * type ;
183
212
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 ;
188
214
189
215
if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC ,
190
216
"r" , & type ) == FAILURE ) {
191
217
return ;
192
218
}
193
219
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 ;
194
225
}
195
226
196
227
/* {{{ proto resource git_object__size(type)
197
228
*/
198
229
PHP_FUNCTION (git_object__size )
199
230
{
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 ;
206
233
207
234
if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC ,
208
- "r " , & type ) == FAILURE ) {
235
+ "l " , & type ) == FAILURE ) {
209
236
return ;
210
237
}
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 );
212
240
}
213
241
214
242
/* {{{ proto resource git_object_peel(object, target_type)
215
243
*/
216
244
PHP_FUNCTION (git_object_peel )
217
245
{
218
246
zval * object ;
219
- php_git2_t * _object ;
247
+ php_git2_t * _object , * result ;
220
248
zval * target_type ;
221
249
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 ;
226
252
227
253
if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC ,
228
254
"rr" , & object , & target_type ) == FAILURE ) {
229
255
return ;
230
256
}
231
257
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 );
232
269
}
233
270
234
271
/* {{{ proto resource git_object_dup(source)
235
272
*/
236
273
PHP_FUNCTION (git_object_dup )
237
274
{
238
275
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 ;
244
279
245
280
if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC ,
246
281
"r" , & source ) == FAILURE ) {
247
282
return ;
248
283
}
249
284
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 );
250
293
}
251
294
0 commit comments