forked from libgit2/php-git
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.c
495 lines (424 loc) · 13.4 KB
/
repository.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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
* 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"
PHPAPI zend_class_entry *git2_repository_class_entry;
void php_git2_repository_init(TSRMLS_D);
static zend_object_handlers git2_repository_object_handlers;
static void php_git2_repository_free_storage(php_git2_repository *object TSRMLS_DC)
{
if (object->repository != NULL) {
git_repository_free(object->repository);
object->repository = NULL;
}
zend_object_std_dtor(&object->zo TSRMLS_CC);
efree(object);
}
zend_object_value php_git2_repository_new(zend_class_entry *ce TSRMLS_DC)
{
zend_object_value retval;
PHP_GIT2_STD_CREATE_OBJECT(php_git2_repository);
retval.handlers = &git2_repository_object_handlers;
return retval;
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_git2_repository___construct, 0,0,1)
ZEND_ARG_INFO(0, repository_path)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_git2_repository_init, 0,0,1)
ZEND_ARG_INFO(0, isBare)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_git2_repository_discover, 0,0,3)
ZEND_ARG_INFO(0, path)
ZEND_ARG_INFO(0, across_fs)
ZEND_ARG_INFO(0, ceiling_dirs)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_git2_repository_exists, 0,0,1)
ZEND_ARG_INFO(0, exists)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_git2_repository_write, 0,0,2)
ZEND_ARG_INFO(0, contents)
ZEND_ARG_INFO(0, type)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_git2_repository_hash, 0,0,2)
ZEND_ARG_INFO(0, contents)
ZEND_ARG_INFO(0, type)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_git2_repository_lookup, 0,0,1)
ZEND_ARG_INFO(0, lookup)
ZEND_ARG_INFO(0, type)
ZEND_END_ARG_INFO()
static int php_git2_repository_initialize(zval *object, git_repository *repository TSRMLS_DC)
{
zval *odb;
php_git2_repository *m_repository;
php_git2_odb *m_odb;
m_repository = PHP_GIT2_GET_OBJECT(php_git2_repository, object);
m_repository->repository = repository;
MAKE_STD_ZVAL(odb);
object_init_ex(odb,git2_odb_class_entry);
m_odb = PHP_GIT2_GET_OBJECT(php_git2_odb, odb);
git_repository_odb(&m_odb->odb,repository);
add_property_string(object, "path", git_repository_path(repository),1);
add_property_zval(object, "odb", odb);
zval_ptr_dtor(&odb);
}
/*
{{{ proto: Git2\Repsotiroy::__construct(string $path)
*/
PHP_METHOD(git2_repository, __construct)
{
const char *repository_path = NULL;
int repository_path_len, ret = 0;
git_repository *repository;
php_git2_repository *m_repository;
zval *odb;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"|s", &repository_path, &repository_path_len) == FAILURE) {
return;
}
if (repository_path_len > 0) {
ret = git_repository_open(&repository, repository_path);
PHP_GIT2_EXCEPTION_CHECK(ret);
php_git2_repository_initialize(getThis(), repository TSRMLS_CC);
} else {
m_repository->repository = NULL;
}
}
/* }}} */
/*
{{{ proto: Git2\Repsotiroy::isEmpty()
*/
PHP_METHOD(git2_repository, isEmpty)
{
git_repository *repository;
php_git2_repository *m_repository;
m_repository = PHP_GIT2_GET_OBJECT(php_git2_repository, getThis());
if (m_repository->repository != NULL) {
if (git_repository_is_empty(m_repository->repository)) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
} else {
/* @todo: throws an exectpion */
}
}
/* }}} */
/*
{{{ proto: Git2\Repsotiroy::isBare()
*/
PHP_METHOD(git2_repository, isBare)
{
git_repository *repository;
php_git2_repository *m_repository;
m_repository = PHP_GIT2_GET_OBJECT(php_git2_repository, getThis());
if (m_repository->repository != NULL) {
if (git_repository_is_bare(m_repository->repository) == 1) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
} else {
/* @todo: throws an exectpion */
}
}
/* }}} */
/*
{{{ proto: Git2\Repsotiroy::getPath()
*/
PHP_METHOD(git2_repository, getPath)
{
git_repository *repository;
php_git2_repository *m_repository;
const char *path = NULL;
zval *m_path = NULL;
m_repository = PHP_GIT2_GET_OBJECT(php_git2_repository, getThis());
if (m_repository->repository != NULL) {
path = git_repository_path(m_repository->repository);
if (path != NULL) {
MAKE_STD_ZVAL(m_path);
ZVAL_STRING(m_path, path, 1);
RETVAL_ZVAL(m_path,0,1);
}
} else {
/* @todo: throws an exectpion */
}
}
/* }}} */
/*
{{{ proto: Git2\Repsotiroy::getWorkdir()
*/
PHP_METHOD(git2_repository, getWorkdir)
{
git_repository *repository;
php_git2_repository *m_repository;
const char *path = NULL;
zval *m_path = NULL;
m_repository = PHP_GIT2_GET_OBJECT(php_git2_repository, getThis());
if (m_repository->repository != NULL) {
path = git_repository_workdir(m_repository->repository);
if (path != NULL) {
MAKE_STD_ZVAL(m_path);
ZVAL_STRING(m_path, path, 1);
RETVAL_ZVAL(m_path,0,1);
}
} else {
/* @todo: throws an exectpion */
}
}
/* }}} */
/*
{{{ proto: Git2\Repsotiroy::init(string $path [, bool isBare])
*/
PHP_METHOD(git2_repository, init)
{
char *path;
int ret, path_len = 0;
zend_bool is_bare = 0;
git_repository *repository;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"s|b", &path, &path_len, &is_bare) == FAILURE) {
return;
}
ret = git_repository_init(&repository, path, is_bare);
if (ret == GIT_SUCCESS) {
zval *object;
MAKE_STD_ZVAL(object);
object_init_ex(object, git2_repository_class_entry);
php_git2_repository_initialize(object, repository TSRMLS_CC);
RETVAL_ZVAL(object,0,1);
} else {
php_git2_exception_check(ret TSRMLS_CC);
RETURN_FALSE;
}
}
/* }}} */
/*
{{{ proto: Git2\Repsotiroy::headDetached()
A repository's HEAD is detached when it points directly to a commit instead of a branch.
*/
PHP_METHOD(git2_repository, headDetached)
{
git_repository *repository;
php_git2_repository *m_repository;
m_repository = PHP_GIT2_GET_OBJECT(php_git2_repository, getThis());
if (m_repository->repository != NULL) {
if (git_repository_head_detached(m_repository->repository) == 1) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
} else {
/* @todo: throws an exectpion */
}
}
/* }}} */
/*
{{{ proto: Git2\Repsotiroy::headOrphan()
An orphan branch is one named from HEAD but which doesn't exist in
the refs namespace, because it doesn't have any commit to point to.
*/
PHP_METHOD(git2_repository, headOrphan)
{
git_repository *repository;
php_git2_repository *m_repository;
m_repository = PHP_GIT2_GET_OBJECT(php_git2_repository, getThis());
if (m_repository->repository != NULL) {
if (git_repository_head_orphan(m_repository->repository) == 1) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
} else {
/* @todo: throws an exectpion */
}
}
/* }}} */
/*
{{{ proto: Git2\Repsotiroy::discover(string $path[, bool across_fs, string ceiling_dirs])
*/
PHP_METHOD(git2_repository, discover)
{
char path_buffer[1024] = {0};
size_t path_size = 1024;
zend_bool across_fs = 1;
char *start_path, *ceiling_dirs = NULL;
int start_path_len, ceiling_dirs_len = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"s|bs", &start_path, &start_path_len, &across_fs, &ceiling_dirs, & ceiling_dirs_len) == FAILURE) {
return;
}
if (git_repository_discover(path_buffer,path_size,(const char *)start_path,(int)across_fs, (const char *)ceiling_dirs) == GIT_SUCCESS) {
RETVAL_STRING(path_buffer, 1);
} else {
RETURN_FALSE;
}
}
/* }}} */
/*
{{{ proto: Git2\Repsotiroy::exists(string $sha1)
*/
PHP_METHOD(git2_repository, exists)
{
char *hash;
int error, hash_len = 0;
git_odb *odb;
git_oid id;
php_git2_repository *m_repository;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"s", &hash, &hash_len) == FAILURE) {
return;
}
m_repository = PHP_GIT2_GET_OBJECT(php_git2_repository, getThis());
error = git_repository_odb(&odb, m_repository->repository);
PHP_GIT2_EXCEPTION_CHECK(error);
if (git_oid_fromstr(&id, hash) != GIT_SUCCESS) {
RETURN_FALSE;
}
if (git_odb_exists(odb, &id) == 1) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/* }}} */
/*
{{{ proto: Git2\Repsotiroy::lookup(string $sha1[, int type = GIT_OBJ_ANY])
*/
PHP_METHOD(git2_repository, lookup)
{
char *hash;
int error, hash_len = 0;
git_odb *odb;
git_oid id;
git_object *object;
long type = GIT_OBJ_ANY;
php_git2_repository *m_repository;
zval *result = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"s|l", &hash, &hash_len, type) == FAILURE) {
return;
}
m_repository = PHP_GIT2_GET_OBJECT(php_git2_repository, getThis());
error = git_repository_odb(&odb, m_repository->repository);
PHP_GIT2_EXCEPTION_CHECK(error);
if (git_oid_fromstrn(&id, hash, hash_len) != GIT_SUCCESS) {
RETURN_FALSE;
}
if (hash_len < GIT_OID_HEXSZ) {
error = git_object_lookup_prefix(&object, m_repository->repository, &id, hash_len, (git_otype)type);
} else {
error = git_object_lookup(&object, m_repository->repository, &id, (git_otype)type);
}
result = php_git2_object_new(m_repository->repository, object TSRMLS_CC);
RETVAL_ZVAL(result,0,1);
}
/* }}} */
/*
{{{ proto: Git2\Repsotiroy::write(string $contents, int $type)
*/
PHP_METHOD(git2_repository, write)
{
char *contents;
int contents_len = 0;
long type = 0;
git_odb_stream *stream;
git_odb *odb;
git_oid oid;
char oid_out[GIT_OID_HEXSZ+1];
int error = 0;
php_git2_repository *m_repository;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"sl", &contents, &contents_len, &type) == FAILURE) {
return;
}
m_repository = PHP_GIT2_GET_OBJECT(php_git2_repository, getThis());
error = git_repository_odb(&odb, m_repository->repository);
PHP_GIT2_EXCEPTION_CHECK(error);
error = git_odb_open_wstream(&stream, odb, contents_len, (git_otype)type);
PHP_GIT2_EXCEPTION_CHECK(error);
error = stream->write(stream, contents, contents_len);
PHP_GIT2_EXCEPTION_CHECK(error);
error = stream->finalize_write(&oid, stream);
PHP_GIT2_EXCEPTION_CHECK(error);
git_oid_fmt(oid_out, &oid);
RETURN_STRINGL(oid_out,GIT_OID_HEXSZ,1);
}
/* }}} */
/*
{{{ proto: Git2\Repsotiroy::hash(string $contents, int $type)
*/
PHP_METHOD(git2_repository, hash)
{
char *contents;
int contents_len = 0;
long type = 0;
git_odb *odb;
git_oid oid;
char oid_out[GIT_OID_HEXSZ+1];
int error = 0;
php_git2_repository *m_repository;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"sl", &contents, &contents_len, &type) == FAILURE) {
return;
}
m_repository = PHP_GIT2_GET_OBJECT(php_git2_repository, getThis());
error = git_repository_odb(&odb, m_repository->repository);
PHP_GIT2_EXCEPTION_CHECK(error);
error = git_odb_hash(&oid, contents,contents_len, type);
PHP_GIT2_EXCEPTION_CHECK(error);
git_oid_fmt(oid_out, &oid);
RETURN_STRINGL(oid_out,GIT_OID_HEXSZ,1);
}
/* }}} */
static zend_function_entry php_git2_repository_methods[] = {
PHP_ME(git2_repository, __construct, arginfo_git2_repository___construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(git2_repository, isEmpty, NULL, ZEND_ACC_PUBLIC)
PHP_ME(git2_repository, isBare, NULL, ZEND_ACC_PUBLIC)
PHP_ME(git2_repository, getPath, NULL, ZEND_ACC_PUBLIC)
PHP_ME(git2_repository, getWorkdir, NULL, ZEND_ACC_PUBLIC)
PHP_ME(git2_repository, headDetached,NULL, ZEND_ACC_PUBLIC)
PHP_ME(git2_repository, headOrphan, NULL, ZEND_ACC_PUBLIC)
PHP_ME(git2_repository, init, arginfo_git2_repository_init, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(git2_repository, discover, arginfo_git2_repository_discover, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(git2_repository, exists, arginfo_git2_repository_exists, ZEND_ACC_PUBLIC)
PHP_ME(git2_repository, hash, arginfo_git2_repository_hash, ZEND_ACC_PUBLIC)
PHP_ME(git2_repository, write, arginfo_git2_repository_write, ZEND_ACC_PUBLIC)
#ifdef lookup
#undef lookup
#endif
PHP_ME(git2_repository, lookup, arginfo_git2_repository_lookup, ZEND_ACC_PUBLIC)
#ifndef lookup
#define lookup php_lookup
#endif
{NULL,NULL,NULL}
};
void php_git2_repository_init(TSRMLS_D)
{
zend_class_entry ce;
INIT_NS_CLASS_ENTRY(ce, PHP_GIT2_NS, "Repository", php_git2_repository_methods);
git2_repository_class_entry = zend_register_internal_class(&ce TSRMLS_CC);
git2_repository_class_entry->create_object = php_git2_repository_new;
memcpy(&git2_repository_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
git2_repository_object_handlers.clone_obj = NULL;
}