forked from libgit2/php-git
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php_git2.h
328 lines (276 loc) · 9.73 KB
/
php_git2.h
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
* 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.
*/
#ifndef PHP_GIT2_H
# define PHP_GIT2_H
# define PHP_GIT2_EXTNAME "git2"
# define PHP_GIT2_EXTVER "0.2.1"
# ifdef HAVE_CONFIG_H
# include "config.h"
# endif
# include "php.h"
# include "Zend/zend_interfaces.h"
# include "Zend/zend_exceptions.h"
# include "ext/spl/spl_exceptions.h"
# include "ext/standard/php_string.h"
# include <date/php_date.h>
# include <git2.h>
# include <git2/errors.h>
# include <git2/odb_backend.h>
extern zend_module_entry git2_module_entry;
# define phpext_git2_ptr &git_module_entry;
# define PHP_GIT2_NS "Git2"
extern PHPAPI zend_class_entry *git2_repository_class_entry;
extern PHPAPI zend_class_entry *git2_commit_class_entry;
extern PHPAPI zend_class_entry *git2_blob_class_entry;
extern PHPAPI zend_class_entry *git2_tree_class_entry;
extern PHPAPI zend_class_entry *git2_tree_builder_class_entry;
extern PHPAPI zend_class_entry *git2_tree_entry_class_entry;
extern PHPAPI zend_class_entry *git2_signature_class_entry;
extern PHPAPI zend_class_entry *git2_walker_class_entry;
extern PHPAPI zend_class_entry *git2_reference_class_entry;
extern PHPAPI zend_class_entry *git2_index_class_entry;
extern PHPAPI zend_class_entry *git2_index_entry_class_entry;
extern PHPAPI zend_class_entry *git2_config_class_entry;
extern PHPAPI zend_class_entry *git2_remote_class_entry;
extern PHPAPI zend_class_entry *git2_tag_class_entry;
extern PHPAPI zend_class_entry *git2_odb_class_entry;
extern PHPAPI zend_class_entry *git2_odb_object_class_entry;
extern PHPAPI zend_class_entry *git2_backend_class_entry;
typedef struct{
zend_object zo;
git_repository *repository;
} php_git2_repository;
typedef struct{
zend_object zo;
git_commit *commit;
} php_git2_commit;
typedef struct{
zend_object zo;
git_blob *blob;
} php_git2_blob;
typedef struct{
zend_object zo;
git_tree *tree;
unsigned int offset;
git_repository *repository;
} php_git2_tree;
typedef struct{
zend_object zo;
git_tree_entry *entry;
} php_git2_tree_entry;
typedef struct{
zend_object zo;
git_treebuilder *builder;
} php_git2_tree_builder;
typedef struct{
zend_object zo;
git_signature *signature;
} php_git2_signature;
typedef struct{
zend_object zo;
git_reference *reference;
} php_git2_reference;
typedef struct php_git2_vector{
size_t _alloc_size;
size_t length;
void **contents;
} php_git2_vector;
typedef struct{
zend_object zo;
git_revwalk *walker;
git_oid *current;
php_git2_vector vector;
git_repository *repository;
int dirty;
} php_git2_walker;
typedef struct{
zend_object zo;
git_index *index;
unsigned int offset;
} php_git2_index;
typedef struct{
zend_object zo;
git_index_entry *entry;
} php_git2_index_entry;
typedef struct{
zend_object zo;
git_config *config;
} php_git2_config;
typedef struct{
zend_object zo;
git_remote *remote;
} php_git2_remote;
typedef struct{
zend_object zo;
git_tag *tag;
} php_git2_tag;
typedef struct{
zend_object zo;
git_odb *odb;
} php_git2_odb;
typedef struct{
zend_object zo;
git_odb_object *object;
} php_git2_odb_object;
typedef struct{
zend_object zo;
struct git_odb_backend *backend;
} php_git2_backend;
typedef struct{
git_odb_backend parent;
zval *self;
} php_git2_backend_internal;
# define PHP_GIT2_GET_OBJECT(STRUCT_NAME, OBJECT) (STRUCT_NAME *) zend_object_store_get_object(OBJECT TSRMLS_CC);
# if ZEND_MODULE_API_NO >= 20100525
# define PHP_GIT2_STD_CREATE_OBJECT(STRUCT_NAME) \
STRUCT_NAME *object;\
\
object = ecalloc(1, sizeof(*object));\
zend_object_std_init(&object->zo, ce TSRMLS_CC);\
object_properties_init(&object->zo, ce);\
\
retval.handle = zend_objects_store_put(object,\
(zend_objects_store_dtor_t)zend_objects_destroy_object,\
(zend_objects_free_object_storage_t) STRUCT_NAME##_free_storage ,\
NULL TSRMLS_CC);\
retval.handlers = zend_get_std_object_handlers();
# else
# define PHP_GIT2_STD_CREATE_OBJECT(STRUCT_NAME) \
STRUCT_NAME *object;\
zval *tmp = NULL;\
\
object = ecalloc(1, sizeof(*object));\
zend_object_std_init(&object->zo, ce TSRMLS_CC);\
zend_hash_copy(object->zo.properties, &ce->default_properties, (copy_ctor_func_t)zval_add_ref, (void *)&tmp, sizeof(zval *)); \
\
retval.handle = zend_objects_store_put(object,\
(zend_objects_store_dtor_t)zend_objects_destroy_object,\
(zend_objects_free_object_storage_t) STRUCT_NAME##_free_storage ,\
NULL TSRMLS_CC);\
retval.handlers = zend_get_std_object_handlers();
# endif
extern zval* php_git_read_protected_property(zend_class_entry *scope, zval *object, char *name, int name_length TSRMLS_DC);
extern int php_git2_add_protected_property_string_ex(zval *object, char *name, int name_length, char *data, zend_bool duplicate TSRMLS_DC);
extern int php_git2_add_protected_property_zval_ex(zval *object, char *name, int name_length, zval *data, zend_bool duplicate TSRMLS_DC);
extern zval* php_git2_object_new(git_repository *repository, git_object *object TSRMLS_DC);
extern int php_git2_call_user_function_v(zval **retval, zval *obj, char *method, unsigned int method_len, unsigned int param_count, ...);
extern inline void php_git2_create_signature(zval *object, char *name, int name_len, char *email, int email_len, zval *date TSRMLS_DC);
static zval* php_git2_date_instantiate(zend_class_entry *pce, zval *object TSRMLS_DC)
{
#if PHP_VERSION_ID <= 50304
Z_TYPE_P(object) = IS_OBJECT;
object_init_ex(object, pce);
Z_SET_REFCOUNT_P(object, 1);
Z_UNSET_ISREF_P(object);
return object;
#else
return php_date_instantiate(pce, object TSRMLS_CC);
#endif
}
static void inline php_git2_create_signature_from_commit(zval **object, git_commit *commit, int type TSRMLS_DC)
{
zval *ret, *date;
char time_str[12] = {0};
const git_signature *author;
php_git2_signature *m_signature;
if (type == 0) {
author = git_commit_author(commit);
} else {
author = git_commit_committer(commit);
}
MAKE_STD_ZVAL(ret);
MAKE_STD_ZVAL(date);
object_init_ex(ret,git2_signature_class_entry);
m_signature = PHP_GIT2_GET_OBJECT(php_git2_signature, ret);
add_property_string_ex(ret,"name",sizeof("name"), author->name,1 TSRMLS_CC);
add_property_string_ex(ret,"email",sizeof("email"),author->email,1 TSRMLS_CC);
php_git2_date_instantiate(php_date_get_date_ce(), date TSRMLS_CC);
snprintf(time_str,12,"%c%ld",'@',author->when.time);
#if PHP_VERSION_ID <= 50304
{
zval *tmp;
MAKE_STD_ZVAL(tmp);
ZVAL_STRING(tmp, time_str,1);
zend_call_method_with_1_params(&date, php_date_get_date_ce(), NULL, "__construct", NULL, tmp);
zval_ptr_dtor(&tmp);
}
#else
php_date_initialize(zend_object_store_get_object(date TSRMLS_CC), time_str, strlen(time_str), NULL, NULL, 0 TSRMLS_CC);
#endif
add_property_zval(ret,"time",date);
zval_ptr_dtor(&date);
*object = ret;
}
static inline void create_tree_entry_from_entry(zval **object, git_tree_entry *entry, git_repository *repository)
{
TSRMLS_FETCH();
char buf[GIT_OID_HEXSZ+1] = {0};
const git_oid *oid;
php_git2_tree_entry *m_entry;
MAKE_STD_ZVAL(*object);
object_init_ex(*object, git2_tree_entry_class_entry);
m_entry = PHP_GIT2_GET_OBJECT(php_git2_tree_entry, *object);
m_entry->entry = entry;
oid = git_tree_entry_id(entry);
git_oid_tostr(buf,GIT_OID_HEXSZ+1,oid);
add_property_string(*object, "name", (char *)git_tree_entry_name(entry), 1);
add_property_string(*object, "oid", buf, 1);
add_property_long(*object, "attributes", git_tree_entry_filemode(entry));
}
static inline void php_git2_create_index_entry(zval **object, git_index_entry *entry TSRMLS_DC)
{
zval *tmp = NULL;
char oid_out[GIT_OID_HEXSZ] = {0};
MAKE_STD_ZVAL(tmp);
object_init_ex(tmp, git2_index_entry_class_entry);
git_oid_fmt(oid_out, &entry->oid);
add_property_string_ex(tmp, "path", sizeof("path"), entry->path, 1 TSRMLS_CC);
add_property_stringl_ex(tmp, "oid", sizeof("oid"), oid_out,GIT_OID_HEXSZ, 1 TSRMLS_CC);
add_property_long(tmp, "dev", entry->dev);
add_property_long(tmp, "ino", entry->ino);
add_property_long(tmp, "mode", entry->mode);
add_property_long(tmp, "uid", entry->uid);
add_property_long(tmp, "gid", entry->gid);
add_property_long(tmp, "file_size", entry->file_size);
add_property_long(tmp, "flags", entry->flags);
add_property_long(tmp, "flags_extended", entry->flags_extended);
add_property_long(tmp, "ctime", entry->ctime.seconds);
add_property_long(tmp, "mtime", entry->mtime.seconds);
*object = tmp;
}
#define PHP_GIT2_EXCEPTION_CHECK(errorcode) \
if (errorcode < 0) { \
zend_throw_exception_ex(NULL, 0 TSRMLS_CC,"%d\n(error code %d) at %s:%d", giterr_last(), errorcode, __FILE__, __LINE__); \
giterr_clear(); \
return; \
} \
static inline void php_git2_exception_check(int errorcode TSRMLS_DC)
{
if (errorcode < 0) {
zend_throw_exception_ex(NULL, 0 TSRMLS_CC,"%d\n(error code %d)", giterr_last(), errorcode);
giterr_clear();
return;
}
}
#endif /* PHP_GIT2_H */