-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit2_reference.c
294 lines (237 loc) · 8.92 KB
/
git2_reference.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
282
283
284
285
286
287
288
289
290
291
292
293
#include "php_git2.h"
#include "git2_exception.h"
#include "git2_repository.h"
#include "git2_reference.h"
#include "git2_commit.h"
#include "git2_tree.h"
#include "git2_blob.h"
static zend_class_entry *php_git2_reference_ce;
static zend_object_handlers php_git2_reference_handler;
typedef struct _git2_reference_object {
zend_object std;
git_reference *ref;
} git2_reference_object_t;
ZEND_BEGIN_ARG_INFO_EX(arginfo_reference_lookup_name, 0, 0, 2)
ZEND_ARG_OBJ_INFO(0, repository, Git2\\Repository, 0)
ZEND_ARG_INFO(0, name)
ZEND_END_ARG_INFO()
static PHP_METHOD(Reference, lookup_name) {
zval *z_repo;
git_repository *repo;
char *name;
size_t name_len;
git2_reference_object_t *intern;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Os", &z_repo, git2_reference_class_entry(), &name, &name_len) == FAILURE)
return;
repo = git2_repository_fetch_from_zval(z_repo);
if (repo == NULL) {
git2_throw_exception(0 TSRMLS_CC, "Parameter must be a valid git repository");
return;
}
object_init_ex(return_value, php_git2_reference_ce);
intern = (git2_reference_object_t*)Z_OBJ_P(return_value);
int res = git_reference_lookup(&intern->ref, repo, name);
if (res != 0) {
git2_throw_last_error();
return;
}
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_reference_dwim, 0, 0, 2)
ZEND_ARG_OBJ_INFO(0, repository, Git2\\Repository, 0)
ZEND_ARG_INFO(0, name)
ZEND_END_ARG_INFO()
static PHP_METHOD(Reference, dwim) {
zval *z_repo;
git_repository *repo;
char *name;
size_t name_len;
git2_reference_object_t *intern;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Os", &z_repo, git2_reference_class_entry(), &name, &name_len) == FAILURE)
return;
repo = git2_repository_fetch_from_zval(z_repo);
if (repo == NULL) {
git2_throw_exception(0 TSRMLS_CC, "Parameter must be a valid git repository");
return;
}
object_init_ex(return_value, php_git2_reference_ce);
intern = (git2_reference_object_t*)Z_OBJ_P(return_value);
int res = git_reference_dwim(&intern->ref, repo, name);
if (res != 0) {
git2_throw_last_error();
return;
}
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_reference_list, 0, 0, 1)
ZEND_ARG_OBJ_INFO(0, repository, Git2\\Repository, 0)
ZEND_END_ARG_INFO()
static int git2_reference_list_callback(git_reference *ref, void *payload) {
git2_reference_object_t *intern;
zval obj;
object_init_ex(&obj, php_git2_reference_ce);
intern = (git2_reference_object_t*)Z_OBJ_P(&obj);
intern->ref = ref;
add_next_index_zval((zval*)payload, &obj);
return 0;
}
static PHP_METHOD(Reference, list) {
zval *z_repo;
git_repository *repo;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &z_repo, git2_reference_class_entry()) == FAILURE)
return;
repo = git2_repository_fetch_from_zval(z_repo);
if (repo == NULL) {
git2_throw_exception(0 TSRMLS_CC, "Parameter must be a valid git repository");
return;
}
array_init(return_value);
int res = git_reference_foreach(repo, git2_reference_list_callback, return_value);
if (res != 0) {
git2_throw_last_error();
return;
}
}
#define GIT2_REFERENCE_FETCH() git2_reference_object_t *intern = (git2_reference_object_t*)Z_OBJ_P(getThis()); \
if (intern->ref == NULL) { \
git2_throw_exception(0 TSRMLS_CC, "Git2\\Reference object in invalid state"); \
return; \
}
#define GIT2_REFERENCE_GET_BOOL(_x) ZEND_BEGIN_ARG_INFO_EX(arginfo_reference_ ## _x, 0, 0, 0) \
ZEND_END_ARG_INFO() \
static PHP_METHOD(Reference, _x) { \
if (zend_parse_parameters_none() == FAILURE) return; \
GIT2_REFERENCE_FETCH(); \
RETURN_BOOL(git_reference_ ## _x(intern->ref)); \
}
#define GIT2_REFERENCE_GET_STRING(_x) ZEND_BEGIN_ARG_INFO_EX(arginfo_reference_ ## _x, 0, 0, 0) \
ZEND_END_ARG_INFO() \
static PHP_METHOD(Reference, _x) { \
if (zend_parse_parameters_none() == FAILURE) return; \
GIT2_REFERENCE_FETCH(); \
const char *res = git_reference_ ## _x(intern->ref); \
if (res == NULL) { RETURN_NULL(); } \
RETURN_STRING(git_reference_ ## _x(intern->ref)); \
}
#define GIT2_REFERENCE_GET_OID(_x) ZEND_BEGIN_ARG_INFO_EX(arginfo_reference_ ## _x, 0, 0, 0) \
ZEND_END_ARG_INFO() \
static PHP_METHOD(Reference, _x) { \
if (zend_parse_parameters_none() == FAILURE) return; \
GIT2_REFERENCE_FETCH(); \
const git_oid *res = git_reference_ ## _x(intern->ref); \
if (res == NULL) { RETURN_NULL(); } \
RETURN_STRINGL((const char*)(res->id), GIT_OID_RAWSZ); \
}
GIT2_REFERENCE_GET_STRING(name)
GIT2_REFERENCE_GET_BOOL(is_branch)
GIT2_REFERENCE_GET_BOOL(is_remote)
GIT2_REFERENCE_GET_BOOL(is_tag)
GIT2_REFERENCE_GET_BOOL(is_note)
GIT2_REFERENCE_GET_STRING(shorthand)
GIT2_REFERENCE_GET_STRING(symbolic_target)
GIT2_REFERENCE_GET_OID(target)
GIT2_REFERENCE_GET_OID(target_peel)
ZEND_BEGIN_ARG_INFO_EX(arginfo_reference_resolve, 0, 0, 0)
ZEND_END_ARG_INFO()
static PHP_METHOD(Reference, resolve) {
if (zend_parse_parameters_none() == FAILURE) return;
GIT2_REFERENCE_FETCH();
git_reference *out;
if (git_reference_resolve(&out, intern->ref) != 0) {
RETURN_FALSE;
}
git2_reference_spawn(return_value, out TSRMLS_CC);
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_reference_peel, 0, 0, 1)
ZEND_ARG_INFO(0, type)
ZEND_END_ARG_INFO()
static PHP_METHOD(Reference, peel) {
long type;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &type) != SUCCESS) {
return;
}
GIT2_REFERENCE_FETCH();
switch(type) {
case GIT_OBJ_COMMIT: case GIT_OBJ_TAG: case GIT_OBJ_TREE: case GIT_OBJ_BLOB: case GIT_OBJ_ANY:
break;
default:
git2_throw_exception(0 TSRMLS_CC, "Invalid type passed");
return;
}
git_object *out;
int res = git_reference_peel(&out, intern->ref, type);
if (res != 0) {
git2_throw_last_error(TSRMLS_C);
return;
}
// TODO find object type, instanciate
switch(git_object_type(out)) {
case GIT_OBJ_COMMIT:
git2_commit_spawn(return_value, (git_commit*)out TSRMLS_CC);
return;
case GIT_OBJ_TREE:
git2_tree_spawn(return_value, (git_tree*)out TSRMLS_CC);
return;
case GIT_OBJ_BLOB:
git2_blob_spawn(return_value, (git_blob*)out TSRMLS_CC);
return;
default:
git2_throw_exception(0 TSRMLS_CC, "Type of object is not implemented");
return;
}
}
void git2_reference_spawn(zval *return_value, git_reference *ref TSRMLS_DC) {
git2_reference_object_t *intern;
object_init_ex(return_value, php_git2_reference_ce);
intern = (git2_reference_object_t*)Z_OBJ_P(return_value);
intern->ref = ref;
}
zend_object *php_git2_reference_create_object(zend_class_entry *class_type TSRMLS_DC) {
git2_reference_object_t *intern = NULL;
intern = emalloc(sizeof(git2_reference_object_t));
memset(intern, 0, sizeof(git2_reference_object_t));
zend_object_std_init(&intern->std, class_type TSRMLS_CC);
object_properties_init(&intern->std, class_type);
intern->std.handlers = &php_git2_reference_handler;
return &intern->std;
}
static void php_git2_reference_free_object(zend_object *object TSRMLS_DC) {
git2_reference_object_t *intern = (git2_reference_object_t*)object;
zend_object_std_dtor(&intern->std TSRMLS_CC);
if (intern->ref) {
git_reference_free(intern->ref);
intern->ref = NULL;
}
// no need with PHP7 to free intern
}
static zend_function_entry git2_reference_methods[] = {
PHP_ME(Reference, lookup_name, arginfo_reference_lookup_name, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_ME(Reference, dwim, arginfo_reference_dwim, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_ME(Reference, list, arginfo_reference_list, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_ME(Reference, name, arginfo_reference_name, ZEND_ACC_PUBLIC)
PHP_ME(Reference, is_branch, arginfo_reference_is_branch, ZEND_ACC_PUBLIC)
PHP_ME(Reference, is_remote, arginfo_reference_is_remote, ZEND_ACC_PUBLIC)
PHP_ME(Reference, is_tag, arginfo_reference_is_tag, ZEND_ACC_PUBLIC)
PHP_ME(Reference, is_note, arginfo_reference_is_note, ZEND_ACC_PUBLIC)
PHP_ME(Reference, shorthand, arginfo_reference_shorthand, ZEND_ACC_PUBLIC)
PHP_ME(Reference, resolve, arginfo_reference_resolve, ZEND_ACC_PUBLIC)
PHP_ME(Reference, target, arginfo_reference_target, ZEND_ACC_PUBLIC)
PHP_ME(Reference, target_peel, arginfo_reference_target_peel, ZEND_ACC_PUBLIC)
PHP_ME(Reference, symbolic_target, arginfo_reference_symbolic_target, ZEND_ACC_PUBLIC)
PHP_ME(Reference, peel, arginfo_reference_peel, ZEND_ACC_PUBLIC)
/* PHP_ME(Reference, __construct, arginfo___construct, ZEND_ACC_PUBLIC) */
{ NULL, NULL, NULL }
};
void git2_reference_init(TSRMLS_D) {
zend_class_entry ce;
INIT_NS_CLASS_ENTRY(ce, "Git2", "Reference", git2_reference_methods);
php_git2_reference_ce = zend_register_internal_class(&ce TSRMLS_CC);
php_git2_reference_ce->create_object = php_git2_reference_create_object;
memcpy(&php_git2_reference_handler, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
php_git2_reference_handler.clone_obj = NULL;
php_git2_reference_handler.free_obj = php_git2_reference_free_object;
// normalize
#define GIT2_REF_CONST(_x) zend_declare_class_constant_long(php_git2_reference_ce, ZEND_STRL(#_x), GIT_REF_ ## _x TSRMLS_CC)
GIT2_REF_CONST(FORMAT_NORMAL);
GIT2_REF_CONST(FORMAT_ALLOW_ONELEVEL);
GIT2_REF_CONST(FORMAT_REFSPEC_PATTERN);
GIT2_REF_CONST(FORMAT_REFSPEC_SHORTHAND);
}