Skip to content

Commit 7dc5916

Browse files
committed
add missing methods for Git2\Commit
int Git2\Commit::getParentCount() Git2\Commit Git2\Commit::getParent([int index]) Git2\Commit Git2\Commit::getParents()
1 parent 4a95e5f commit 7dc5916

3 files changed

Lines changed: 157 additions & 0 deletions

File tree

commit.c

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_git2_commit_create, 0,0,2)
4949
ZEND_ARG_INFO(0, data)
5050
ZEND_END_ARG_INFO()
5151

52+
ZEND_BEGIN_ARG_INFO_EX(arginfo_git2_commit_get_parent, 0,0,1)
53+
ZEND_ARG_INFO(0, index)
54+
ZEND_END_ARG_INFO()
55+
5256
/*
5357
{{{ proto: Git2\Commit::getMessage()
5458
*/
@@ -363,6 +367,113 @@ PHP_METHOD(git2_commit, getTree)
363367
}
364368
/* }}} */
365369

370+
/*
371+
{{{ proto: Git2\Commit::getParents()
372+
*/
373+
PHP_METHOD(git2_commit, getParents)
374+
{
375+
php_git2_commit *m_commit;
376+
unsigned int parent_count = 0;
377+
int error, i = 0;
378+
zval *result;
379+
380+
m_commit = PHP_GIT2_GET_OBJECT(php_git2_commit, getThis());
381+
382+
if (m_commit != NULL) {
383+
if (m_commit->commit == NULL) {
384+
RETURN_FALSE;
385+
}
386+
387+
parent_count = git_commit_parentcount(m_commit->commit);
388+
MAKE_STD_ZVAL(result);
389+
array_init(result);
390+
for (i = 0; i < parent_count; i++) {
391+
git_commit *parent = NULL;
392+
zval *tmp = NULL;
393+
394+
error = git_commit_parent(&parent, m_commit->commit, i);
395+
if (error == GIT_SUCCESS) {
396+
tmp = php_git2_object_new(git_object_owner((git_object *)m_commit->commit), parent TSRMLS_CC);
397+
add_next_index_zval(result, tmp);
398+
}
399+
}
400+
401+
RETVAL_ZVAL(result,0,1);
402+
} else {
403+
RETURN_FALSE;
404+
}
405+
}
406+
/* }}} */
407+
408+
/*
409+
{{{ proto: Git2\Commit::getParent([int index])
410+
*/
411+
PHP_METHOD(git2_commit, getParent)
412+
{
413+
php_git2_commit *m_commit;
414+
unsigned int parent_count = 0;
415+
int error, i = 0;
416+
long index = 0;
417+
zval *result;
418+
git_commit *parent = NULL;
419+
420+
421+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
422+
"|l", &index) == FAILURE) {
423+
return;
424+
}
425+
426+
427+
m_commit = PHP_GIT2_GET_OBJECT(php_git2_commit, getThis());
428+
429+
if (m_commit != NULL) {
430+
if (m_commit->commit == NULL) {
431+
RETURN_FALSE;
432+
}
433+
434+
parent_count = git_commit_parentcount(m_commit->commit);
435+
if (index > (parent_count-1) || index < 0) {
436+
/* @todo: throws invalidargument exception */
437+
RETURN_FALSE;
438+
}
439+
440+
error = git_commit_parent(&parent, m_commit->commit, (unsigned int)index);
441+
if (error == GIT_SUCCESS) {
442+
result = php_git2_object_new(git_object_owner((git_object *)m_commit->commit), parent TSRMLS_CC);
443+
RETVAL_ZVAL(result,0,1);
444+
}
445+
} else {
446+
RETURN_FALSE;
447+
}
448+
}
449+
/* }}} */
450+
451+
/*
452+
{{{ proto: Git2\Commit::getParentCount()
453+
*/
454+
PHP_METHOD(git2_commit, getParentCount)
455+
{
456+
php_git2_commit *m_commit;
457+
unsigned int parent_count = 0;
458+
int error, i = 0;
459+
zval *result;
460+
git_commit *parent = NULL;
461+
462+
m_commit = PHP_GIT2_GET_OBJECT(php_git2_commit, getThis());
463+
464+
if (m_commit != NULL) {
465+
if (m_commit->commit == NULL) {
466+
RETURN_FALSE;
467+
}
468+
469+
parent_count = git_commit_parentcount(m_commit->commit);
470+
RETURN_LONG(parent_count);
471+
} else {
472+
RETURN_FALSE;
473+
}
474+
}
475+
/* }}} */
476+
366477

367478
static zend_function_entry php_git2_commit_methods[] = {
368479
PHP_ME(git2_commit, getMessage, NULL, ZEND_ACC_PUBLIC)
@@ -372,6 +483,9 @@ static zend_function_entry php_git2_commit_methods[] = {
372483
PHP_ME(git2_commit, getCommitter, NULL, ZEND_ACC_PUBLIC)
373484
PHP_ME(git2_commit, getOid, NULL, ZEND_ACC_PUBLIC)
374485
PHP_ME(git2_commit, getTree, NULL, ZEND_ACC_PUBLIC)
486+
PHP_ME(git2_commit, getParentCount, NULL, ZEND_ACC_PUBLIC)
487+
PHP_ME(git2_commit, getParent, arginfo_git2_commit_get_parent, ZEND_ACC_PUBLIC)
488+
PHP_ME(git2_commit, getParents, NULL, ZEND_ACC_PUBLIC)
375489
PHP_ME(git2_commit, create, arginfo_git2_commit_create, ZEND_ACC_STATIC | ZEND_ACC_PUBLIC)
376490
{NULL,NULL,NULL}
377491
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
Check for Git2\Commit::getCommitter
3+
--SKIPIF--
4+
<?php if (!extension_loaded("git2")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
date_default_timezone_set('Asia/Tokyo');
8+
$path = __DIR__ . '/mock/001-01';
9+
10+
$repo = new Git2\Repository($path);
11+
$commit = $repo->lookup("ab68c54212af15d3545c41057e3a8f2f9ff6fd0d");
12+
13+
14+
var_dump($commit->getParentCount());
15+
16+
$path2 = __DIR__ . '/mock/008-02';
17+
$repo = new Git2\Repository($path2);
18+
$commit = $repo->lookup("6e20138dc38f9f626107f1cd3ef0f9838c43defe");
19+
20+
var_dump($commit->getParentCount());
21+
22+
--EXPECT--
23+
int(0)
24+
int(1)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Check for Git2\Commit::getCommitter
3+
--SKIPIF--
4+
<?php if (!extension_loaded("git2")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
date_default_timezone_set('Asia/Tokyo');
8+
9+
$path2 = __DIR__ . '/mock/008-02';
10+
$repo = new Git2\Repository($path2);
11+
$commit = $repo->lookup("6e20138dc38f9f626107f1cd3ef0f9838c43defe");
12+
13+
$parent = $commit->getParent();
14+
var_dump($parent->getOid());
15+
var_dump($parent->getMessage());
16+
--EXPECT--
17+
string(40) "9bb8c853c9ea27609a6bdc48b78cd26a320daf7d"
18+
string(16) "added section 1
19+
"

0 commit comments

Comments
 (0)