Skip to content

Commit 1cf6860

Browse files
committed
reference: fix #36 add Git2\Reference::create method
now, you can create a branch like this. ```` $repo = new Git2\Repository(".git"); $branch_name = "refs/heads/feature/create_branch"; $oid_or_symbolic_ref = "b4fd0a854ae8a6a02c89ab08e3e2f41bd4832ff1"; $force = false; $ref = Git2\Reference::create($repo, $branch_name, $oid_or_symbolic_ref, $force); ```` this API throws \RuntimeException when failed.
1 parent 5531c47 commit 1cf6860

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

reference.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ zend_object_value php_git2_reference_new(zend_class_entry *ce TSRMLS_DC)
4343
return retval;
4444
}
4545

46+
ZEND_BEGIN_ARG_INFO_EX(arginfo_git2_reference_create, 0,0,4)
47+
ZEND_ARG_INFO(0, repository)
48+
ZEND_ARG_INFO(0, name)
49+
ZEND_ARG_INFO(0, target)
50+
ZEND_ARG_INFO(0, force)
51+
ZEND_END_ARG_INFO()
52+
4653
ZEND_BEGIN_ARG_INFO_EX(arginfo_git2_reference_lookup, 0,0,2)
4754
ZEND_ARG_INFO(0, repository)
4855
ZEND_ARG_INFO(0, path)
@@ -162,6 +169,50 @@ PHP_METHOD(git2_reference, resolve)
162169
/* }}} */
163170

164171

172+
/*
173+
{{{ proto: Git2\Reference::create(Git2\Repository $repo, string $name, string $oid_or_symbolic_ref[, bool $force = false])
174+
*/
175+
PHP_METHOD(git2_reference, create)
176+
{
177+
zval *repository = NULL;
178+
php_git2_repository *m_repository;
179+
php_git2_reference *m_reference;
180+
char *name, *target;
181+
int error = 0, target_len = 0, name_len = 0;
182+
zend_bool force = 0;
183+
git_reference *ref;
184+
git_oid oid;
185+
zval *object;
186+
187+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
188+
"Oss|b", &repository, git2_repository_class_entry, &name, &name_len, &target, &target_len, &force) == FAILURE) {
189+
return;
190+
}
191+
192+
m_repository = PHP_GIT2_GET_OBJECT(php_git2_repository, repository);
193+
194+
if (git_oid_fromstr(&oid, target) == GIT_OK) {
195+
error = git_reference_create_oid(&ref, m_repository->repository, name, &oid, (int)force);
196+
} else {
197+
error = git_reference_create_symbolic(&ref, m_repository->repository, name, target, (int)force);
198+
}
199+
200+
if (error != GIT_OK) {
201+
const git_error *err;
202+
203+
err = giterr_last();
204+
zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC, "Git2\\Refernce::create failed: %s (%d)", err->message, err->klass);
205+
}
206+
207+
MAKE_STD_ZVAL(object);
208+
object_init_ex(object, git2_reference_class_entry);
209+
m_reference = PHP_GIT2_GET_OBJECT(php_git2_reference, object);
210+
m_reference->reference = ref;
211+
212+
RETURN_ZVAL(object, 0, 1);
213+
}
214+
/* }}} */
215+
165216

166217
typedef struct {
167218
unsigned int type;
@@ -264,6 +315,7 @@ static zend_function_entry php_git2_reference_methods[] = {
264315
PHP_ME(git2_reference, getName, NULL, ZEND_ACC_PUBLIC)
265316
PHP_ME(git2_reference, getBaseName,NULL,ZEND_ACC_PUBLIC)
266317
PHP_ME(git2_reference, resolve, NULL, ZEND_ACC_PUBLIC)
318+
PHP_ME(git2_reference, create, arginfo_git2_reference_create, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
267319
PHP_ME(git2_reference, each, arginfo_git2_reference_each, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
268320
#ifdef lookup
269321
#undef lookup

0 commit comments

Comments
 (0)