Skip to content

Commit 9ea6935

Browse files
committed
introducing git2 utils
1 parent 577f2fa commit 9ea6935

File tree

6 files changed

+125
-55
lines changed

6 files changed

+125
-55
lines changed

config.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ if test $PHP_GIT2 != "no"; then
2020
PHP_ADD_LIBRARY_WITH_PATH(git2, libgit2/build, GIT2_SHARED_LIBADD)
2121
fi
2222

23-
PHP_NEW_EXTENSION(git2, php_git2.c git2_exception.c git2_repository.c git2_config.c git2_config_entry.c git2_reference.c git2_commit.c git2_remote.c git2_tree.c git2_tree_entry.c git2_blob.c, $ext_shared)
23+
PHP_NEW_EXTENSION(git2, php_git2.c git2_exception.c git2_repository.c git2_config.c git2_config_entry.c git2_reference.c git2_commit.c git2_remote.c git2_tree.c git2_tree_entry.c git2_blob.c git2_php_util.c, $ext_shared)
2424
PHP_SUBST(GIT2_SHARED_LIBADD)
2525
fi

git2_php_util.c

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#include "php_git2.h"
2+
#include "git2_php_util.h"
3+
4+
#define ARRAY_FETCH_LONG(_x) if ((data = zend_hash_str_find(ht, ZEND_STRL(#_x))) != NULL) { \
5+
convert_to_long(data); \
6+
opts->_x = Z_LVAL_P(data); \
7+
}
8+
9+
#define ARRAY_FETCH_BOOL(_x) if ((data = zend_hash_str_find(ht, ZEND_STRL(#_x))) != NULL) { \
10+
convert_to_bool(data); \
11+
opts->_x = Z_BVAL_P(data); \
12+
}
13+
14+
#define ARRAY_FETCH_STRING(_x) if ((data = zend_hash_str_find(ht, ZEND_STRL(#_x))) != NULL) { \
15+
convert_to_string(data); \
16+
opts->_x = Z_STRVAL_P(data); \
17+
}
18+
19+
#define ARRAY_FETCH_OPTIONS(_x, _method) if ((data = zend_hash_str_find(ht, ZEND_STRL(#_x))) != NULL) { \
20+
convert_to_array(data); \
21+
_method(&opts->_x, Z_ARRVAL_P(data)); \
22+
}
23+
24+
#define ARRAY_FETCH_STRARRAY(_x) ARRAY_FETCH_OPTIONS(_x, php_git2_ht_to_strarray)
25+
26+
#define ARRAY_FETCH_CALLBACK(_x, _payload) /* TODO */
27+
28+
void php_git2_ht_to_strarray(git_strarray *out, HashTable *in) {
29+
uint32_t count = zend_array_count(in);
30+
out->count = count;
31+
if (count == 0) return;
32+
33+
out->strings = emalloc(sizeof(char*) * count);
34+
uint32_t cur_pos = 0;
35+
36+
HashPosition position;
37+
zval *data = NULL;
38+
39+
for (zend_hash_internal_pointer_reset_ex(in, &position);
40+
data = zend_hash_get_current_data_ex(in, &position);
41+
zend_hash_move_forward_ex(in, &position)) {
42+
43+
convert_to_string(data);
44+
out->strings[cur_pos] = Z_STRVAL_P(data);
45+
cur_pos += 1;
46+
}
47+
}
48+
49+
void php_git2_strarray_free(git_strarray *a) {
50+
if (a->count > 0) {
51+
efree(a->strings);
52+
}
53+
}
54+
55+
void git2_parse_repository_init_options(git_repository_init_options *opts, HashTable *ht) {
56+
zval *data;
57+
58+
if (ht == NULL) return; // skip if null
59+
60+
ARRAY_FETCH_LONG(flags);
61+
ARRAY_FETCH_LONG(mode);
62+
ARRAY_FETCH_STRING(workdir_path); // check open_basedir?
63+
ARRAY_FETCH_STRING(description);
64+
ARRAY_FETCH_STRING(template_path); // check open_basedir?
65+
ARRAY_FETCH_STRING(initial_head);
66+
ARRAY_FETCH_STRING(origin_url);
67+
}
68+
69+
void git2_parse_clone_options(git_clone_options *opts, HashTable *ht) {
70+
zval *data;
71+
72+
if (ht == NULL) return; // skip if null
73+
74+
ARRAY_FETCH_OPTIONS(checkout_opts, git2_parse_checkout_options);
75+
ARRAY_FETCH_OPTIONS(fetch_opts, git2_parse_fetch_options);
76+
ARRAY_FETCH_BOOL(bare);
77+
ARRAY_FETCH_LONG(local); // one of GIT_CLONE_LOCAL_AUTO, GIT_CLONE_LOCAL, GIT_CLONE_NO_LOCAL, GIT_CLONE_LOCAL_NO_LINKS
78+
ARRAY_FETCH_STRING(checkout_branch);
79+
ARRAY_FETCH_CALLBACK(repository_cb, repository_cb_payload); // TODO + _payload
80+
ARRAY_FETCH_CALLBACK(remote_cb, remote_cb_payload); // TODO + _payload
81+
}
82+
83+
void git2_parse_checkout_options(git_checkout_options *opts, HashTable *ht) {
84+
zval *data;
85+
86+
if (ht == NULL) return; // skip if null
87+
88+
ARRAY_FETCH_LONG(checkout_strategy);
89+
ARRAY_FETCH_BOOL(disable_filters);
90+
ARRAY_FETCH_LONG(dir_mode);
91+
ARRAY_FETCH_LONG(file_mode);
92+
ARRAY_FETCH_LONG(file_open_flags);
93+
ARRAY_FETCH_LONG(notify_flags);
94+
ARRAY_FETCH_CALLBACK(notify_cb, notify_payload);
95+
ARRAY_FETCH_CALLBACK(progress_cb, progress_payload);
96+
ARRAY_FETCH_STRARRAY(paths);
97+
// git_tree *baseline;
98+
// git_index *baseline_index;
99+
ARRAY_FETCH_STRING(target_directory);
100+
ARRAY_FETCH_STRING(ancestor_label);
101+
ARRAY_FETCH_STRING(our_label);
102+
ARRAY_FETCH_STRING(their_label);
103+
ARRAY_FETCH_CALLBACK(perfdata_cb, perfdata_payload);
104+
}
105+

git2_php_util.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef GIT2_PHP_UTIL_H
2+
#define GIT2_PHP_UTIL_H
3+
4+
void php_git2_ht_to_strarray(git_strarray *out, HashTable *in);
5+
void php_git2_strarray_free(git_strarray *a);
6+
7+
void git2_parse_repository_init_options(git_repository_init_options *opts, HashTable *ht);
8+
void git2_parse_clone_options(git_clone_options *opts, HashTable *ht);
9+
void git2_parse_checkout_options(git_checkout_options *opts, HashTable *ht);
10+
void git2_parse_fetch_options(git_fetch_options *opts, HashTable *ht);
11+
void git2_parse_checkout_options(git_checkout_options *opts, HashTable *ht);
12+
13+
#endif /* GIT2_PHP_UTIL_H */

git2_repository.c

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "git2_repository.h"
33
#include "git2_config.h"
44
#include "git2_reference.h"
5+
#include "git2_php_util.h"
56

67
static zend_class_entry *php_git2_repository_ce;
78
static zend_object_handlers php_git2_repository_handler;
@@ -103,26 +104,15 @@ static PHP_METHOD(Repository, init_ext) {
103104
char *path;
104105
size_t path_len;
105106
HashTable *opts;
106-
zval *data;
107107
git2_repository_object_t *intern;
108108

109109
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sh", &path, &path_len, &opts) == FAILURE)
110110
return;
111111

112112
git_repository_init_options opts_libgit2 = GIT_REPOSITORY_INIT_OPTIONS_INIT;
113-
if ((data = zend_hash_str_find(opts, ZEND_STRL("flags"))) != NULL) {
114-
convert_to_long(data); // is it safe to call convert_to_long() here?
115-
opts_libgit2.flags = Z_LVAL_P(data);
116-
}
117-
118-
if ((data = zend_hash_str_find(opts, ZEND_STRL("mode"))) != NULL) {
119-
convert_to_long(data); // is it safe to call convert_to_long() here?
120-
opts_libgit2.mode = Z_LVAL_P(data);
121-
}
122-
// TODO: workdir_path description template_path initial_head origin_url
113+
git2_parse_repository_init_options(&opts_libgit2, opts);
123114

124115
object_init_ex(return_value, php_git2_repository_ce);
125-
126116
intern = (git2_repository_object_t*)Z_OBJ_P(return_value);
127117

128118
int res = git_repository_init_ext(&intern->repo, path, &opts_libgit2);
@@ -150,17 +140,7 @@ static PHP_METHOD(Repository, clone) {
150140
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|h", &url, &url_len, &local_path, &local_path_len, &opts) == FAILURE)
151141
return;
152142

153-
if (opts != NULL) {
154-
// TODO hnadle options
155-
if ((data = zend_hash_str_find(opts, ZEND_STRL("bare"))) != NULL) {
156-
convert_to_bool(data);
157-
opts_libgit2.bare = Z_BVAL_P(data);
158-
}
159-
if ((data = zend_hash_str_find(opts, ZEND_STRL("checkout_branch"))) != NULL) {
160-
convert_to_string(data);
161-
opts_libgit2.checkout_branch = Z_STRVAL_P(data);
162-
}
163-
}
143+
git2_parse_clone_options(&opts_libgit2, opts);
164144

165145
object_init_ex(return_value, php_git2_repository_ce);
166146
intern = (git2_repository_object_t*)Z_OBJ_P(return_value);
@@ -280,16 +260,17 @@ ZEND_END_ARG_INFO()
280260

281261
static PHP_METHOD(Repository, checkout_head) {
282262
HashTable *opts = NULL;
263+
git_checkout_options opts_libgit2 = GIT_CHECKOUT_OPTIONS_INIT;
283264

284265
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|H", &opts) == FAILURE) {
285266
RETURN_FALSE;
286267
}
287268

288269
GIT2_REPOSITORY_FETCH();
289270

290-
// TODO handle opts
271+
git2_parse_checkout_options(&opts_libgit2, opts);
291272

292-
int res = git_checkout_head(intern->repo, NULL);
273+
int res = git_checkout_head(intern->repo, &opts_libgit2);
293274

294275
if (res != 0) {
295276
git2_throw_last_error();

php_git2.c

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,6 @@
1414
static zend_class_entry *php_git2_base_ce;
1515
static zend_object_handlers php_git2_base_handler;
1616

17-
void php_git2_ht_to_strarray(git_strarray *out, HashTable *in) {
18-
uint32_t count = zend_array_count(in);
19-
out->count = count;
20-
if (count == 0) return;
21-
22-
out->strings = emalloc(sizeof(char*) * count);
23-
uint32_t cur_pos = 0;
24-
25-
HashPosition position;
26-
zval *data = NULL;
27-
28-
for (zend_hash_internal_pointer_reset_ex(in, &position);
29-
data = zend_hash_get_current_data_ex(in, &position);
30-
zend_hash_move_forward_ex(in, &position)) {
31-
32-
convert_to_string(data);
33-
out->strings[cur_pos] = Z_STRVAL_P(data);
34-
cur_pos += 1;
35-
}
36-
}
37-
38-
void php_git2_strarray_free(git_strarray *a) {
39-
if (a->count > 0) {
40-
efree(a->strings);
41-
}
42-
}
43-
4417
static zend_function_entry git2_base_methods[] = {
4518
{ NULL, NULL, NULL }
4619
};

php_git2.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ extern zend_module_entry git2_module_entry;
3636
#define PHP_GIT2_API
3737
#endif
3838

39-
void php_git2_ht_to_strarray(git_strarray *out, HashTable *in);
40-
void php_git2_strarray_free(git_strarray *a);
4139
//PHP_GIT2_API zend_class_entry *php_git2_get_repository(void);
4240

4341

0 commit comments

Comments
 (0)