Skip to content

Commit 65a27a1

Browse files
committed
added Git2\Repository::init method
1 parent e927f41 commit 65a27a1

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

repository.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_git2_repository___construct, 0,0,1)
4949
ZEND_ARG_INFO(0, repository_path)
5050
ZEND_END_ARG_INFO()
5151

52+
ZEND_BEGIN_ARG_INFO_EX(arginfo_git2_repository_init, 0,0,1)
53+
ZEND_ARG_INFO(0, isBare)
54+
ZEND_END_ARG_INFO()
55+
5256
/*
5357
{{{ proto: Git2\Repsotiroy::__construct(string $path)
5458
*/
@@ -175,12 +179,47 @@ PHP_METHOD(git2_repository, getWorkdir)
175179
/* }}} */
176180

177181

182+
/*
183+
{{{ proto: Git2\Repsotiroy::init(string $path [, bool isBare])
184+
*/
185+
PHP_METHOD(git2_repository, init)
186+
{
187+
char *path;
188+
int ret, path_len = 0;
189+
zend_bool is_bare = 0;
190+
git_repository *repository;
191+
192+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
193+
"s|b", &path, &path_len, &is_bare) == FAILURE) {
194+
return;
195+
}
196+
197+
ret = git_repository_init(&repository, path, is_bare);
198+
if (ret == GIT_SUCCESS) {
199+
zval *object;
200+
php_git2_repository *m_repository;
201+
202+
MAKE_STD_ZVAL(object);
203+
object_init_ex(object, git2_repository_class_entry);
204+
m_repository = PHP_GIT2_GET_OBJECT(php_git2_repository, object);
205+
m_repository->repository = repository;
206+
207+
RETVAL_ZVAL(object,0,1);
208+
} else {
209+
/* @todo: throws an runtime exception */
210+
RETURN_FALSE;
211+
}
212+
}
213+
/* }}} */
214+
215+
178216
static zend_function_entry php_git2_repository_methods[] = {
179217
PHP_ME(git2_repository, __construct, arginfo_git2_repository___construct, ZEND_ACC_PUBLIC)
180218
PHP_ME(git2_repository, isEmpty, NULL, ZEND_ACC_PUBLIC)
181219
PHP_ME(git2_repository, isBare, NULL, ZEND_ACC_PUBLIC)
182220
PHP_ME(git2_repository, getPath, NULL, ZEND_ACC_PUBLIC)
183221
PHP_ME(git2_repository, getWorkdir, NULL, ZEND_ACC_PUBLIC)
222+
PHP_ME(git2_repository, init, arginfo_git2_repository_init, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
184223
{NULL,NULL,NULL}
185224
};
186225

tests/001-06-repository_init.phpt

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
--TEST--
2+
Check for Git2\Repository::init
3+
--SKIPIF--
4+
<?php if (!extension_loaded("git2")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
$bare_path = __DIR__ . "/mock/001-06/bare";
8+
if (file_exists($bare_path)) {
9+
`rm -rf {$bare_path}`;
10+
}
11+
$repo = Git2\Repository::init($bare_path,true);
12+
13+
$expected_files = array(
14+
"HEAD",
15+
"config",
16+
"objects",
17+
"objects/info",
18+
"objects/pack",
19+
"refs",
20+
"refs/heads",
21+
"refs/tags",
22+
);
23+
24+
if (file_exists($bare_path)) {
25+
echo "OK" . PHP_EOL;
26+
foreach ($expected_files as $file) {
27+
if (file_exists($bare_path . "/{$file}")) {
28+
echo "OK" . PHP_EOL;
29+
} else {
30+
echo "NG: missing {$file}" . PHP_EOL;
31+
}
32+
}
33+
34+
`rm -rf {$bare_path}`;
35+
}
36+
echo "END_BARE" . PHP_EOL;
37+
38+
$normal_path = __DIR__ . "/mock/001-06/normal";
39+
if (file_exists($normal_path)) {
40+
`rm -rf {$normal_path}`;
41+
}
42+
$repo = Git2\Repository::init($normal_path);
43+
44+
$expected_files = array(
45+
".git",
46+
".git/HEAD",
47+
".git/config",
48+
".git/objects",
49+
".git/objects/info",
50+
".git/objects/pack",
51+
".git/refs",
52+
".git/refs/heads",
53+
".git/refs/tags",
54+
);
55+
56+
if (file_exists($normal_path)) {
57+
echo "OK" . PHP_EOL;
58+
foreach ($expected_files as $file) {
59+
if (file_exists($normal_path . "/{$file}")) {
60+
echo "OK" . PHP_EOL;
61+
} else {
62+
echo "NG: missing {$file}" . PHP_EOL;
63+
}
64+
}
65+
66+
`rm -rf {$normal_path}`;
67+
}
68+
69+
--EXPECT--
70+
OK
71+
OK
72+
OK
73+
OK
74+
OK
75+
OK
76+
OK
77+
OK
78+
OK
79+
END_BARE
80+
OK
81+
OK
82+
OK
83+
OK
84+
OK
85+
OK
86+
OK
87+
OK
88+
OK
89+
OK

0 commit comments

Comments
 (0)