Skip to content

Commit 4fe6eff

Browse files
committed
modified Git2\Config::get
1 parent ce96323 commit 4fe6eff

File tree

8 files changed

+115
-3
lines changed

8 files changed

+115
-3
lines changed

Diff for: config.c

+53-3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,52 @@ typedef struct{
6060
git_config *config;
6161
} php_git2_config_foreach_t;
6262

63+
static int php_git2_config_resolve(zval **result, const char *var_name, zval *m_config)
64+
{
65+
TSRMLS_FETCH();
66+
HashTable *hash;
67+
zval *entry, *tmp_result, **target_offset;
68+
char *current_key, *tmp_value, *savedptr, *k;
69+
int error = 0;
70+
71+
entry = zend_read_property(git2_config_class_entry, m_config,"configs",sizeof("configs")-1, 0 TSRMLS_CC);
72+
73+
tmp_value = estrdup(var_name);
74+
current_key = php_strtok_r(tmp_value, ".", &savedptr);
75+
while (current_key != NULL) {
76+
k = current_key;
77+
current_key = php_strtok_r(NULL, ".", &savedptr);
78+
79+
if (current_key != NULL && k != NULL) {
80+
if (zend_hash_exists(Z_ARRVAL_P(entry), k, strlen(k)+1)) {
81+
if (zend_hash_find(Z_ARRVAL_P(entry), k, strlen(k)+1, (void **)&target_offset) == SUCCESS) {
82+
entry = *target_offset;
83+
}
84+
} else {
85+
target_offset = NULL;
86+
}
87+
} else {
88+
if (k != NULL) {
89+
if (zend_hash_find(Z_ARRVAL_P(entry), k, strlen(k)+1, (void **)&target_offset) != SUCCESS) {
90+
target_offset = NULL;
91+
}
92+
}
93+
}
94+
}
95+
efree(tmp_value);
96+
97+
if (target_offset != NULL) {
98+
MAKE_STD_ZVAL(tmp_result);
99+
ZVAL_ZVAL(tmp_result, *target_offset,1,0);
100+
} else {
101+
MAKE_STD_ZVAL(tmp_result);
102+
ZVAL_NULL(tmp_result);
103+
}
104+
*result = tmp_result;
105+
106+
return error;
107+
}
108+
63109
static int php_git2_config_foreach(const char *var_name, const char *value, void *payload)
64110
{
65111
HashTable *hash;
@@ -150,15 +196,19 @@ PHP_METHOD(git2_config, get)
150196
int error, key_len = 0;
151197
const char *value;
152198
php_git2_config *m_config;
199+
zval *result;
153200

154201
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
155202
"s", &key, &key_len) == FAILURE) {
156203
return;
157204
}
158205

159-
m_config = PHP_GIT2_GET_OBJECT(php_git2_config, getThis());
160-
error = git_config_get_string(m_config->config, key, &value);
161-
RETVAL_STRING(value, 1);
206+
if (key_len < 1) {
207+
RETURN_FALSE;
208+
}
209+
210+
php_git2_config_resolve(&result, (const char *)key, getThis());
211+
RETVAL_ZVAL(result,0,1);
162212
}
163213
/* }}} */
164214

Diff for: tests/00a-01-config___construct.phpt

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
Check for Git2\Config::__construct
3+
--SKIPIF--
4+
<?php if (!extension_loaded("git2")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
$path = __DIR__ . '/fixtures/testrepo.git/config';
8+
$config = new Git2\Config($path);
9+
var_dump($config);
10+
--EXPECT--
11+
object(Git2\Config)#1 (1) {
12+
["configs"]=>
13+
array(1) {
14+
["core"]=>
15+
array(4) {
16+
["repositoryformatversion"]=>
17+
string(1) "0"
18+
["filemode"]=>
19+
string(4) "true"
20+
["bare"]=>
21+
string(4) "true"
22+
["ignorecase"]=>
23+
string(4) "true"
24+
}
25+
}
26+
}

Diff for: tests/00a-02-config_get.phpt

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
Check for Git2\Config::get
3+
--SKIPIF--
4+
<?php if (!extension_loaded("git2")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
$path = __DIR__ . '/fixtures/testrepo.git/config';
8+
$config = new Git2\Config($path);
9+
echo $config->get("core.repositoryformatversion") . PHP_EOL;
10+
echo $config->get("core.filemode") . PHP_EOL;
11+
echo $config->get("core.bare") . PHP_EOL;
12+
echo $config->get("core.ignorecase") . PHP_EOL;
13+
echo ($config->get("core") == array("repositoryformatversion"=>"0","filemode"=>"true","bare"=>"true","ignorecase"=>"true")) ? "OK\n" : "FAIL\n";
14+
echo ($config->get("") == false) ? "OK\n" : "FAIL\n";
15+
echo ($config->get("core.uhi") == false) ? "OK\n" : "FAIL\n";
16+
--EXPECT--
17+
0
18+
true
19+
true
20+
true
21+
OK
22+
OK
23+
OK

Diff for: tests/fixtures/testrepo.git/HEAD

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref: refs/heads/master

Diff for: tests/fixtures/testrepo.git/config

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[core]
2+
repositoryformatversion = 0
3+
filemode = true
4+
bare = true
5+
ignorecase = true

Diff for: tests/fixtures/testrepo.git/description

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Unnamed repository; edit this file 'description' to name the repository.

Diff for: tests/fixtures/testrepo.git/info/exclude

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# git ls-files --others --exclude-from=.git/info/exclude
2+
# Lines that start with '#' are comments.
3+
# For a project mostly in C, the following would be a good set of
4+
# exclude patterns (uncomment them if you want to use them):
5+
# *.[oa]
6+
# *~

Diff for: tests/fixtures/testrepo.git/refs/heads/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)