forked from libgit2/php-git
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git2.c
281 lines (241 loc) · 7.62 KB
/
git2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* The MIT License
*
* Copyright (c) 2010 - 2012 Shuhei Tanuma
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "php_git2.h"
#include "ext/standard/info.h"
void php_git2_repository_init(TSRMLS_D);
void php_git2_commit_init(TSRMLS_D);
void php_git2_blob_init(TSRMLS_D);
void php_git2_tree_init(TSRMLS_D);
void php_git2_tree_builder_init(TSRMLS_D);
void php_git2_signature_init(TSRMLS_D);
void php_git2_walker_init(TSRMLS_D);
void php_git2_reference_init(TSRMLS_D);
void php_git2_config_init(TSRMLS_D);
void php_git2_remote_init(TSRMLS_D);
void php_git2_tag_init(TSRMLS_D);
void php_git2_odb_init(TSRMLS_D);
void php_git2_odb_object_init(TSRMLS_D);
void php_git2_tree_entry_init(TSRMLS_D);
void php_git2_index_entry_init(TSRMLS_D);
void php_git2_backend_init(TSRMLS_D);
void php_git2_index_init(TSRMLS_D);
int php_git2_call_user_function_v(zval **retval, zval *obj, char *method, unsigned int method_len, unsigned int param_count, ...)
{
va_list ap;
size_t i;
int error = 0;
zval **params, *method_name, *ret= NULL;
TSRMLS_FETCH();
if (param_count > 0) {
params = emalloc(sizeof(zval**) * param_count);
va_start(ap, param_count);
for (i = 0; i < param_count; i ++) {
params[i] = va_arg(ap, zval*);
}
va_end(ap);
} else {
params = NULL;
}
MAKE_STD_ZVAL(ret);
MAKE_STD_ZVAL(method_name);
ZVAL_NULL(ret);
ZVAL_STRINGL(method_name, method, method_len, 1);
error = call_user_function(NULL, &obj, method_name, ret, param_count, params TSRMLS_CC);
if (param_count > 0) {
for (i = 0; i < param_count; i++) {
if (params[i] != NULL) {
zval_ptr_dtor(¶ms[i]);
}
}
efree(params);
params = NULL;
}
*retval = ret;
zval_ptr_dtor(&method_name);
return error;
}
zval* php_git2_object_new(git_repository *repository, git_object *object TSRMLS_DC)
{
zval *result = NULL;
MAKE_STD_ZVAL(result);
switch (git_object_type(object)) {
case GIT_OBJ_TAG: {
php_git2_tag *m_obj = NULL;
object_init_ex(result, git2_tag_class_entry);
m_obj = PHP_GIT2_GET_OBJECT(php_git2_tag, result);
m_obj->tag = (git_tag*)object;
break;
}
case GIT_OBJ_COMMIT: {
php_git2_commit *m_obj = NULL;
object_init_ex(result, git2_commit_class_entry);
m_obj = PHP_GIT2_GET_OBJECT(php_git2_commit, result);
m_obj->commit = (git_commit*)object;
break;
}
case GIT_OBJ_BLOB: {
php_git2_blob *m_obj = NULL;
object_init_ex(result, git2_blob_class_entry);
m_obj = PHP_GIT2_GET_OBJECT(php_git2_blob, result);
m_obj->blob = (git_blob*)object;
break;
}
case GIT_OBJ_TREE: {
php_git2_tree *m_obj = NULL;
unsigned int numbers = 0;
int i = 0;
zval *m_array;
object_init_ex(result, git2_tree_class_entry);
m_obj = PHP_GIT2_GET_OBJECT(php_git2_tree, result);
m_obj->tree = (git_tree*)object;
m_obj->repository = repository;
numbers = git_tree_entrycount(m_obj->tree);
MAKE_STD_ZVAL(m_array);
array_init(m_array);
for (i = 0;i < numbers; i++) {
const char *entry_name = {0};
char entry_oid[GIT_OID_HEXSZ+1] = {0};
const git_tree_entry *entry;
const git_oid *oid = NULL;
zval *m_entry = NULL;
entry = git_tree_entry_byindex(m_obj->tree, i);
entry_name = git_tree_entry_name(entry);
oid = git_tree_entry_id(entry);
git_oid_fmt(entry_oid, oid);
MAKE_STD_ZVAL(m_entry);
object_init_ex(m_entry, git2_tree_entry_class_entry);
add_property_stringl_ex(m_entry, "name", sizeof("name"), (const char *)entry_name, strlen(entry_name), 1 TSRMLS_CC);
add_property_stringl_ex(m_entry, "oid", sizeof("oid"), (const char *)entry_oid, strlen(entry_oid), 1 TSRMLS_CC);
add_property_long_ex(m_entry, "attributes", sizeof("attributes"), git_tree_entry_filemode(entry) TSRMLS_CC);
add_next_index_zval(m_array, m_entry);
}
add_property_zval_ex(result, "entries",sizeof("entries"),m_array TSRMLS_CC);
zval_ptr_dtor(&m_array);
break;
}
case GIT_OBJ_BAD:
case GIT_OBJ_ANY:
case GIT_OBJ__EXT1:
case GIT_OBJ__EXT2:
case GIT_OBJ_OFS_DELTA:
case GIT_OBJ_REF_DELTA:
default:
break;
}
return result;
}
zval* php_git_read_protected_property(zend_class_entry *scope, zval *object, char *name, int name_length TSRMLS_DC)
{
zval **data;
char *key;
int length;
zend_mangle_property_name(&key,&length,"*",1,name,name_length,0);
if (zend_hash_find(Z_OBJPROP_P(object),key,length,(void **)&data) != SUCCESS) {
data = NULL;
}
efree(key);
return *data;
}
int php_git2_add_protected_property_string_ex(zval *object, char *name, int name_length, char *data, zend_bool duplicate TSRMLS_DC)
{
zval *tmp;
char *key;
int length;
MAKE_STD_ZVAL(tmp);
ZVAL_STRING(tmp,data,duplicate);
zend_mangle_property_name(&key, &length, "*",1,name,name_length,0);
zend_hash_update(Z_OBJPROP_P(object),key,length,&tmp,sizeof(zval *),NULL);
efree(key);
return SUCCESS;
}
int php_git2_add_protected_property_zval_ex(zval *object, char *name, int name_length, zval *data, zend_bool duplicate TSRMLS_DC)
{
zval *tmp;
char *key;
int length;
MAKE_STD_ZVAL(tmp);
ZVAL_ZVAL(tmp,data,duplicate,0);
zend_mangle_property_name(&key, &length, "*",1,name,name_length,0);
zend_hash_update(Z_OBJPROP_P(object),key,length,&tmp,sizeof(zval *),NULL);
efree(key);
return SUCCESS;
}
PHP_MINIT_FUNCTION(git2)
{
php_git2_repository_init(TSRMLS_C);
php_git2_commit_init(TSRMLS_C);
php_git2_blob_init(TSRMLS_C);
php_git2_tree_init(TSRMLS_C);
php_git2_tree_builder_init(TSRMLS_C);
php_git2_tree_entry_init(TSRMLS_C);
php_git2_signature_init(TSRMLS_C);
php_git2_walker_init(TSRMLS_C);
php_git2_reference_init(TSRMLS_C);
php_git2_index_entry_init(TSRMLS_C);
php_git2_index_init(TSRMLS_C);
php_git2_config_init(TSRMLS_C);
php_git2_remote_init(TSRMLS_C);
php_git2_tag_init(TSRMLS_C);
php_git2_odb_init(TSRMLS_C);
php_git2_odb_object_init(TSRMLS_C);
php_git2_backend_init(TSRMLS_C);
return SUCCESS;
}
PHP_MINFO_FUNCTION(git2)
{
php_printf("PHP libgit2 Extension\n");
php_info_print_table_start();
php_info_print_table_row(2,"Version", PHP_GIT2_EXTVER "-dev");
php_info_print_table_row(2,"libgit2 version", LIBGIT2_VERSION);
php_info_print_table_end();
}
PHP_RINIT_FUNCTION(git2)
{
git_threads_init();
return SUCCESS;
}
PHP_RSHUTDOWN_FUNCTION(git2)
{
git_threads_shutdown();
return SUCCESS;
}
zend_module_entry git2_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
PHP_GIT2_EXTNAME,
NULL, /* Functions */
PHP_MINIT(git2), /* MINIT */
NULL, /* MSHUTDOWN */
PHP_RINIT(git2), /* RINIT */
PHP_RSHUTDOWN(git2),/* RSHUTDOWN */
PHP_MINFO(git2), /* MFINFO */
#if ZEND_MODULE_API_NO >= 20010901
PHP_GIT2_EXTVER,
#endif
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_GIT2
ZEND_GET_MODULE(git2)
#endif