Skip to content

Commit 7a66238

Browse files
committed
[repository] implement git_repository_init_ext
1 parent c3b172c commit 7a66238

File tree

3 files changed

+82
-5
lines changed

3 files changed

+82
-5
lines changed

php_git2.c

+1
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ static zend_function_entry php_git2_functions[] = {
366366
PHP_FE(git_repository_state, arginfo_git_repository_state)
367367
PHP_FE(git_repository_set_namespace, arginfo_git_repository_set_namespace)
368368
PHP_FE(git_repository_is_shallow, arginfo_git_repository_is_shallow)
369+
PHP_FE(git_repository_init_options_new, NULL)
369370

370371
/* index */
371372
PHP_FE(git_index_open, arginfo_git_index_open)

repository.c

+77-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,67 @@
22
#include "php_git2_priv.h"
33
#include "repository.h"
44

5+
static void php_git2_array_to_git_repository_init_options(git_repository_init_options *opts, zval *array TSRMLS_DC)
6+
{
7+
long lval;
8+
9+
lval = php_git2_read_arrval_long(array, ZEND_STRS("version") TSRMLS_CC);
10+
if (lval > 0) {
11+
opts->version = lval;
12+
}
13+
opts->flags = php_git2_read_arrval_long(array, ZEND_STRS("flags") TSRMLS_CC);
14+
opts->mode = php_git2_read_arrval_long(array, ZEND_STRS("mode") TSRMLS_CC);
15+
16+
opts->workdir_path = php_git2_read_arrval_string(array, ZEND_STRS("workdir_path") TSRMLS_CC);
17+
opts->description = php_git2_read_arrval_string(array, ZEND_STRS("description") TSRMLS_CC);
18+
opts->template_path = php_git2_read_arrval_string(array, ZEND_STRS("template_path") TSRMLS_CC);
19+
opts->initial_head = php_git2_read_arrval_string(array, ZEND_STRS("initial_head") TSRMLS_CC);
20+
opts->origin_url = php_git2_read_arrval_string(array, ZEND_STRS("origin_url") TSRMLS_CC);
21+
}
22+
23+
static void php_git2_git_repository_init_options_to_array(git_repository_init_options *opts, zval **out TSRMLS_DC)
24+
{
25+
zval *result;
26+
MAKE_STD_ZVAL(result);
27+
array_init(result);
28+
29+
add_assoc_long_ex(result, ZEND_STRS("version"), opts->version);
30+
add_assoc_long_ex(result, ZEND_STRS("flags"), opts->flags);
31+
add_assoc_long_ex(result, ZEND_STRS("mode"), opts->mode);
32+
33+
if (opts->workdir_path != NULL) {
34+
add_assoc_string_ex(result, ZEND_STRS("workdir_path"), opts->workdir_path, 1);
35+
} else {
36+
add_assoc_null_ex(result, ZEND_STRS("workdir_path"));
37+
}
38+
39+
if (opts->workdir_path != NULL) {
40+
add_assoc_string_ex(result, ZEND_STRS("description"), opts->description, 1);
41+
} else {
42+
add_assoc_null_ex(result, ZEND_STRS("description"));
43+
}
44+
45+
if (opts->workdir_path != NULL) {
46+
add_assoc_string_ex(result, ZEND_STRS("template_path"), opts->template_path, 1);
47+
} else {
48+
add_assoc_null_ex(result, ZEND_STRS("template_path"));
49+
}
50+
51+
if (opts->workdir_path != NULL) {
52+
add_assoc_string_ex(result, ZEND_STRS("initial_head"), opts->initial_head, 1);
53+
} else {
54+
add_assoc_null_ex(result, ZEND_STRS("initial_head"));
55+
}
56+
57+
if (opts->workdir_path != NULL) {
58+
add_assoc_string_ex(result, ZEND_STRS("origin_url"), opts->origin_url, 1);
59+
} else {
60+
add_assoc_null_ex(result, ZEND_STRS("origin_url"));
61+
}
62+
*out = result;
63+
}
64+
65+
566
static int php_git2_repository_fetchhead_foreach_cb(const char *ref_name,
667
const char *remote_url,
768
const git_oid *oid,
@@ -310,25 +371,24 @@ PHP_FUNCTION(git_repository_free)
310371
}
311372
/* }}} */
312373

313-
314374
/* {{{ proto resource git_repository_init_ext(string $repo_path, $opts)
315375
*/
316376
PHP_FUNCTION(git_repository_init_ext)
317377
{
378+
git_repository_init_options options = GIT_REPOSITORY_INIT_OPTIONS_INIT;
318379
php_git2_t *result = NULL;
319380
git_repository *out = NULL;
320381
char *repo_path = NULL;
321-
int repo_path_len = 0;
382+
int repo_path_len = 0, error = 0;
322383
zval *opts = NULL;
323-
int error = 0;
324384

325-
/* TODO(chobie): generate converter */
326385
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
327386
"sa", &repo_path, &repo_path_len, &opts) == FAILURE) {
328387
return;
329388
}
330389

331-
error = git_repository_init_ext(&out, repo_path, opts);
390+
php_git2_array_to_git_repository_init_options(&options, opts TSRMLS_CC);
391+
error = git_repository_init_ext(&out, repo_path, &options);
332392
if (php_git2_check_error(error, "git_repository_init_ext" TSRMLS_CC)) {
333393
RETURN_FALSE;
334394
}
@@ -876,4 +936,16 @@ PHP_FUNCTION(git_repository_is_shallow)
876936
is_shallow = git_repository_is_shallow(PHP_GIT2_V(_repo, repository));
877937
RETURN_LONG(is_shallow);
878938
}
939+
/* }}} */
940+
941+
/* {{{ proto array git_repository_init_options_new()
942+
*/
943+
PHP_FUNCTION(git_repository_init_options_new)
944+
{
945+
git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
946+
zval *result;
947+
948+
php_git2_git_repository_init_options_to_array(&opts, &result TSRMLS_CC);
949+
RETURN_ZVAL(result, 0, 1);
950+
}
879951
/* }}} */

repository.h

+4
Original file line numberDiff line numberDiff line change
@@ -317,4 +317,8 @@ PHP_FUNCTION(git_repository_set_namespace);
317317
*/
318318
PHP_FUNCTION(git_repository_is_shallow);
319319

320+
/* {{{ proto array git_repository_init_options_new()
321+
*/
322+
PHP_FUNCTION(git_repository_init_options_new);
323+
320324
#endif

0 commit comments

Comments
 (0)