|
| 1 | +#include "php_git2.h" |
| 2 | +#include "git2_exception.h" |
| 3 | +#include "git2_blob.h" |
| 4 | +#include "git2_repository.h" |
| 5 | + |
| 6 | +static zend_class_entry *php_git2_blob_ce; |
| 7 | +static zend_object_handlers php_git2_blob_handler; |
| 8 | + |
| 9 | +typedef struct _git2_blob_object { |
| 10 | + zend_object std; |
| 11 | + git_blob *blob; |
| 12 | +} git2_blob_object_t; |
| 13 | + |
| 14 | +ZEND_BEGIN_ARG_INFO_EX(arginfo_blob_lookup_oid, 0, 0, 2) |
| 15 | + ZEND_ARG_OBJ_INFO(0, repository, Git2\\Repository, 0) |
| 16 | + ZEND_ARG_INFO(0, oid) |
| 17 | +ZEND_END_ARG_INFO() |
| 18 | + |
| 19 | +static PHP_METHOD(Blob, lookup_oid) { |
| 20 | + zval *z_repo; |
| 21 | + git_repository *repo; |
| 22 | + char *oid; |
| 23 | + size_t oid_len; |
| 24 | + git2_blob_object_t *intern; |
| 25 | + git_oid id; |
| 26 | + |
| 27 | + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Os", &z_repo, git2_reference_class_entry(), &oid, &oid_len) == FAILURE) { |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + repo = git2_repository_fetch_from_zval(z_repo); |
| 32 | + if (repo == NULL) { |
| 33 | + git2_throw_exception(0 TSRMLS_CC, "Parameter must be a valid git repository"); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + if (oid_len != 20) { |
| 38 | + git2_throw_exception(0 TSRMLS_CC, "oid must be 20 bytes long"); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + memcpy(&id, oid, oid_len); |
| 43 | + |
| 44 | + object_init_ex(return_value, php_git2_blob_ce); |
| 45 | + intern = (git2_blob_object_t*)Z_OBJ_P(return_value); |
| 46 | + int res = git_blob_lookup(&intern->blob, repo, &id); |
| 47 | + |
| 48 | + if (res != 0) { |
| 49 | + git2_throw_last_error(); |
| 50 | + return; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +#define GIT2_BLOB_FETCH() git2_blob_object_t *intern = (git2_blob_object_t*)Z_OBJ_P(getThis()); \ |
| 55 | + if (intern->blob == NULL) { \ |
| 56 | + git2_throw_exception(0 TSRMLS_CC, "Git2\\Blob object in invalid state"); \ |
| 57 | + return; \ |
| 58 | + } |
| 59 | + |
| 60 | +#define GIT2_BLOB_GET_LONG(_x) ZEND_BEGIN_ARG_INFO_EX(arginfo_blob_ ## _x, 0, 0, 0) \ |
| 61 | + ZEND_END_ARG_INFO() \ |
| 62 | + static PHP_METHOD(Blob, _x) { \ |
| 63 | + if (zend_parse_parameters_none() == FAILURE) return; \ |
| 64 | + GIT2_BLOB_FETCH(); \ |
| 65 | + RETURN_LONG(git_blob_ ## _x(intern->blob)); \ |
| 66 | + } |
| 67 | + |
| 68 | +#define GIT2_BLOB_GET_BOOL(_x) ZEND_BEGIN_ARG_INFO_EX(arginfo_blob_ ## _x, 0, 0, 0) \ |
| 69 | + ZEND_END_ARG_INFO() \ |
| 70 | + static PHP_METHOD(Blob, _x) { \ |
| 71 | + if (zend_parse_parameters_none() == FAILURE) return; \ |
| 72 | + GIT2_BLOB_FETCH(); \ |
| 73 | + RETURN_BOOL(git_blob_ ## _x(intern->blob)); \ |
| 74 | + } |
| 75 | + |
| 76 | +#define GIT2_BLOB_GET_STRING(_x) ZEND_BEGIN_ARG_INFO_EX(arginfo_blob_ ## _x, 0, 0, 0) \ |
| 77 | + ZEND_END_ARG_INFO() \ |
| 78 | + static PHP_METHOD(Blob, _x) { \ |
| 79 | + if (zend_parse_parameters_none() == FAILURE) return; \ |
| 80 | + GIT2_BLOB_FETCH(); \ |
| 81 | + const char *res = git_blob_ ## _x(intern->blob); \ |
| 82 | + if (res == NULL) { RETURN_NULL(); } \ |
| 83 | + RETURN_STRING(res); \ |
| 84 | + } |
| 85 | + |
| 86 | +#define GIT2_BLOB_GET_OID(_x) ZEND_BEGIN_ARG_INFO_EX(arginfo_blob_ ## _x, 0, 0, 0) \ |
| 87 | + ZEND_END_ARG_INFO() \ |
| 88 | + static PHP_METHOD(Blob, _x) { \ |
| 89 | + if (zend_parse_parameters_none() == FAILURE) return; \ |
| 90 | + GIT2_BLOB_FETCH(); \ |
| 91 | + const git_oid *res = git_blob_ ## _x(intern->blob); \ |
| 92 | + if (res == NULL) { RETURN_NULL(); } \ |
| 93 | + RETURN_STRINGL((const char*)(res->id), GIT_OID_RAWSZ); \ |
| 94 | + } |
| 95 | + |
| 96 | +GIT2_BLOB_GET_OID(id) |
| 97 | +GIT2_BLOB_GET_BOOL(is_binary) |
| 98 | + |
| 99 | +ZEND_BEGIN_ARG_INFO_EX(arginfo_blob_rawcontent, 0, 0, 0) |
| 100 | +ZEND_END_ARG_INFO() |
| 101 | + |
| 102 | +static PHP_METHOD(Blob, rawcontent) { |
| 103 | + zend_bool is_push; |
| 104 | + |
| 105 | + GIT2_BLOB_FETCH(); |
| 106 | + |
| 107 | + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &is_push) == FAILURE) { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + // TODO handle callbacks |
| 112 | + int res = git_blob_connect(intern->blob, is_push ? GIT_DIRECTION_PUSH : GIT_DIRECTION_FETCH, NULL, NULL); |
| 113 | + |
| 114 | + if (res == 0) { |
| 115 | + RETURN_TRUE; |
| 116 | + } |
| 117 | + |
| 118 | + git2_throw_last_error(); |
| 119 | + RETURN_FALSE; |
| 120 | +} |
| 121 | + |
| 122 | +void git2_blob_spawn(zval **return_value, git_blob *blob TSRMLS_DC) { |
| 123 | + git2_blob_object_t *intern; |
| 124 | + |
| 125 | + object_init_ex(*return_value, php_git2_blob_ce); |
| 126 | + intern = (git2_blob_object_t*)Z_OBJ_P(*return_value); |
| 127 | + intern->blob = blob; |
| 128 | +} |
| 129 | + |
| 130 | +zend_object *php_git2_blob_create_object(zend_class_entry *class_type TSRMLS_DC) { |
| 131 | + git2_blob_object_t *intern = NULL; |
| 132 | + |
| 133 | + intern = emalloc(sizeof(git2_blob_object_t)); |
| 134 | + memset(intern, 0, sizeof(git2_blob_object_t)); |
| 135 | + |
| 136 | + zend_object_std_init(&intern->std, class_type TSRMLS_CC); |
| 137 | + object_properties_init(&intern->std, class_type); |
| 138 | + |
| 139 | + intern->std.handlers = &php_git2_blob_handler; |
| 140 | + |
| 141 | + return &intern->std; |
| 142 | +} |
| 143 | + |
| 144 | +static void php_git2_blob_free_object(zend_object *object TSRMLS_DC) { |
| 145 | + git2_blob_object_t *intern = (git2_blob_object_t*)object; |
| 146 | + |
| 147 | + zend_object_std_dtor(&intern->std TSRMLS_CC); |
| 148 | + |
| 149 | + if (intern->blob) { |
| 150 | + git_blob_free(intern->blob); |
| 151 | + intern->blob = NULL; |
| 152 | + } |
| 153 | + |
| 154 | + // no need with PHP7 to free intern |
| 155 | +} |
| 156 | + |
| 157 | +#define PHP_GIT2_BLOB_ME_P(_x) PHP_ME(Blob, _x, arginfo_blob_##_x, ZEND_ACC_PUBLIC) |
| 158 | + |
| 159 | +static zend_function_entry git2_blob_methods[] = { |
| 160 | +// PHP_ME(Blob, create_fromworkdir, arginfo_blob_create_fromworkdir, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) |
| 161 | +// PHP_ME(Blob, create_fromdisk, arginfo_blob_create_fromdisk, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) |
| 162 | + PHP_GIT2_BLOB_ME_P(id) |
| 163 | + PHP_GIT2_BLOB_ME_P(is_binary) |
| 164 | + PHP_GIT2_BLOB_ME_P(rawcontent) |
| 165 | +/* PHP_ME(Blob, __construct, arginfo___construct, ZEND_ACC_PUBLIC) */ |
| 166 | + { NULL, NULL, NULL } |
| 167 | +}; |
| 168 | + |
| 169 | +void git2_blob_init(TSRMLS_D) { |
| 170 | + zend_class_entry ce; |
| 171 | + |
| 172 | + INIT_NS_CLASS_ENTRY(ce, "Git2", "Blob", git2_blob_methods); |
| 173 | + php_git2_blob_ce = zend_register_internal_class(&ce TSRMLS_CC); |
| 174 | + php_git2_blob_ce->create_object = php_git2_blob_create_object; |
| 175 | + |
| 176 | + memcpy(&php_git2_blob_handler, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); |
| 177 | + php_git2_blob_handler.clone_obj = NULL; |
| 178 | + php_git2_blob_handler.free_obj = php_git2_blob_free_object; |
| 179 | +} |
| 180 | + |
0 commit comments