-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit2_tree.c
257 lines (202 loc) · 6.81 KB
/
git2_tree.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
#include "php_git2.h"
#include "git2_exception.h"
#include "git2_repository.h"
#include "git2_tree.h"
#include "git2_tree_entry.h"
static zend_class_entry *php_git2_tree_ce;
static zend_object_handlers php_git2_tree_handler;
typedef struct _git2_tree_object {
zend_object std;
git_tree *tree;
} git2_tree_object_t;
ZEND_BEGIN_ARG_INFO_EX(arginfo_tree_lookup_oid, 0, 0, 2)
ZEND_ARG_OBJ_INFO(0, repository, Git2\\Repository, 0)
ZEND_ARG_INFO(0, oid)
ZEND_END_ARG_INFO()
// somehow it seems we can't use just "lookup" as method name, it'll become php_lookup
static PHP_METHOD(Tree, lookup_oid) {
zval *repo;
char *oid;
size_t oid_len;
git_oid id;
git_repository *git_repo;
git2_tree_object_t *intern;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Os", &repo, git2_reference_class_entry(), &oid, &oid_len) != SUCCESS) {
return;
}
git_repo = git2_repository_fetch_from_zval(repo);
if (git_repo == NULL) {
git2_throw_exception(0 TSRMLS_CC, "Parameter must be a valid git repository");
return;
}
if (oid_len != 20) {
git2_throw_exception(0 TSRMLS_CC, "A git oid must be exactly 20 bytes");
return;
}
memcpy(&id, oid, oid_len);
object_init_ex(return_value, php_git2_tree_ce);
intern = (git2_tree_object_t*)Z_OBJ_P(return_value);
int res = git_tree_lookup(&intern->tree, git_repo, &id);
if (res != 0) {
git2_throw_last_error(TSRMLS_C);
}
}
#define GIT2_TREE_FETCH() git2_tree_object_t *intern = (git2_tree_object_t*)Z_OBJ_P(getThis()); \
if (intern->tree == NULL) { \
git2_throw_exception(0 TSRMLS_CC, "Git2\\Tree object in invalid state"); \
return; \
}
#define GIT2_TREE_GET_LONG(_x) ZEND_BEGIN_ARG_INFO_EX(arginfo_tree_ ## _x, 0, 0, 0) \
ZEND_END_ARG_INFO() \
static PHP_METHOD(Tree, _x) { \
if (zend_parse_parameters_none() == FAILURE) return; \
GIT2_TREE_FETCH(); \
RETURN_LONG(git_tree_ ## _x(intern->tree)); \
}
#define GIT2_TREE_GET_OID(_x) ZEND_BEGIN_ARG_INFO_EX(arginfo_tree_ ## _x, 0, 0, 0) \
ZEND_END_ARG_INFO() \
static PHP_METHOD(Tree, _x) { \
if (zend_parse_parameters_none() == FAILURE) return; \
GIT2_TREE_FETCH(); \
const git_oid *res = git_tree_ ## _x(intern->tree); \
if (res == NULL) { RETURN_NULL(); } \
RETURN_STRINGL((const char*)(res->id), GIT_OID_RAWSZ); \
}
GIT2_TREE_GET_OID(id)
GIT2_TREE_GET_LONG(entrycount)
struct git2_treewalk_payload {
zval *this;
zval *callback_data;
zend_fcall_info callback_i;
zend_fcall_info_cache callback_ic;
zend_bool aborted;
};
static int git2_treewalk_cb(const char *root, const git_tree_entry *entry, void *payload) {
int error;
zval argv[3]; // root, entry, callback_data
zval retval;
struct git2_treewalk_payload *p = payload;
ZVAL_STRING(&argv[0], root);
git2_tree_entry_spawn_ephemeral(&argv[1], entry);
if (p->callback_data) {
ZVAL_COPY_VALUE(&argv[2], p->callback_data);
p->callback_i.param_count = 3;
} else {
p->callback_i.param_count = 2;
}
p->callback_i.retval = &retval;
p->callback_i.params = argv;
error = zend_call_function(&p->callback_i, &p->callback_ic);
if (error == FAILURE) {
p->aborted = 1; // failure of zend_call_function will already throw something (TODO confirm if this is indeed the case)
return -1; // causes end of walk
} else if (!Z_ISUNDEF(retval)) {
convert_to_long(&retval);
error = Z_LVAL(retval);
zval_ptr_dtor(&retval);
if (error < 0)
p->aborted = 1;
} else {
error = 0;
}
zval_ptr_dtor(&argv[0]);
// zval_ptr_dtor(&argv[1]);
// zval_ptr_dtor(&argv[2]); // ?
return error;
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_tree_walk, 0, 0, 2)
ZEND_ARG_INFO(0, mode)
ZEND_ARG_INFO(0, callback)
ZEND_ARG_INFO(1, callback_data)
ZEND_END_ARG_INFO()
static PHP_METHOD(Tree, walk) {
long mode;
struct git2_treewalk_payload p;
p.this = getThis();
p.callback_data = NULL;
p.aborted = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lf|z", &mode, &p.callback_i, &p.callback_ic, &p.callback_data) != SUCCESS) {
return;
}
GIT2_TREE_FETCH();
switch(mode) {
case GIT_TREEWALK_PRE: case GIT_TREEWALK_POST:
break;
default:
git2_throw_exception(0 TSRMLS_CC, "Invalid mode provided, should be Git2::TREEWALK_PRE or Git2::TREEWALK_POST");
return;
}
int res = git_tree_walk(intern->tree, mode, git2_treewalk_cb, &p);
if (p.aborted) {
RETURN_FALSE; // no point throwing an exception if aborted
}
if (res != 0) {
git2_throw_last_error();
return;
}
RETURN_TRUE;
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_tree_entry_bypath, 0, 0, 1)
ZEND_ARG_INFO(0, path)
ZEND_END_ARG_INFO()
static PHP_METHOD(Tree, entry_bypath) {
char *path;
size_t path_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &path, &path_len) != SUCCESS) {
return;
}
GIT2_TREE_FETCH();
git_tree_entry *out;
int res = git_tree_entry_bypath(&out, intern->tree, path);
if (res != 0) {
git2_throw_last_error();
return;
}
git2_tree_entry_spawn(return_value, out TSRMLS_CC);
}
void git2_tree_spawn(zval *return_value, git_tree *tree TSRMLS_DC) {
git2_tree_object_t *intern;
object_init_ex(return_value, php_git2_tree_ce);
intern = (git2_tree_object_t*)Z_OBJ_P(return_value);
intern->tree = tree;
}
zend_object *php_git2_tree_create_object(zend_class_entry *class_type TSRMLS_DC) {
git2_tree_object_t *intern = NULL;
intern = emalloc(sizeof(git2_tree_object_t));
memset(intern, 0, sizeof(git2_tree_object_t));
zend_object_std_init(&intern->std, class_type TSRMLS_CC);
object_properties_init(&intern->std, class_type);
intern->std.handlers = &php_git2_tree_handler;
return &intern->std;
}
static void php_git2_tree_free_object(zend_object *object TSRMLS_DC) {
git2_tree_object_t *intern = (git2_tree_object_t*)object;
zend_object_std_dtor(&intern->std TSRMLS_CC);
if (intern->tree) {
git_tree_free(intern->tree);
intern->tree = NULL;
}
// no need with PHP7 to free intern
}
static zend_function_entry git2_tree_methods[] = {
PHP_ME(Tree, lookup_oid, arginfo_tree_id, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_ME(Tree, id, arginfo_tree_id, ZEND_ACC_PUBLIC)
PHP_ME(Tree, entrycount, arginfo_tree_entrycount, ZEND_ACC_PUBLIC)
PHP_ME(Tree, walk, arginfo_tree_walk, ZEND_ACC_PUBLIC)
PHP_ME(Tree, entry_bypath, arginfo_tree_entry_bypath, ZEND_ACC_PUBLIC)
/* PHP_ME(Tree, __construct, arginfo___construct, ZEND_ACC_PUBLIC) */
{ NULL, NULL, NULL }
};
void git2_tree_init(TSRMLS_D) {
zend_class_entry ce;
INIT_NS_CLASS_ENTRY(ce, "Git2", "Tree", git2_tree_methods);
php_git2_tree_ce = zend_register_internal_class(&ce TSRMLS_CC);
php_git2_tree_ce->create_object = php_git2_tree_create_object;
memcpy(&php_git2_tree_handler, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
php_git2_tree_handler.clone_obj = NULL;
php_git2_tree_handler.free_obj = php_git2_tree_free_object;
// normalize
#define GIT2_TREE_CONST(_x, _y) zend_declare_class_constant_long(php_git2_tree_ce, ZEND_STRL(#_x), _y TSRMLS_CC)
GIT2_TREE_CONST(WALK_PRE, GIT_TREEWALK_PRE);
GIT2_TREE_CONST(WALK_POST, GIT_TREEWALK_POST);
}