Skip to content

Commit b28d145

Browse files
committed
repository: temporary add tree diff feature.
proto: Git2\Repository::diff(Git2\Tree $a, Git2\Tree $b, $options = NULL) this will generate `git diff --name-status` format string like this. ```` A 008-02/refs/heads/.gitkeep A 008-02/refs/heads/master D init.php D testrepo.git/HEAD ````
1 parent eb8cd9b commit b28d145

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

repository.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424

2525
#include "php_git2.h"
26+
#include "ext/standard/php_smart_str.h"
2627

2728
PHPAPI zend_class_entry *git2_repository_class_entry;
2829
void php_git2_repository_init(TSRMLS_D);
@@ -527,6 +528,52 @@ PHP_METHOD(git2_repository, checkout)
527528
/* }}} */
528529

529530

531+
static int printer(
532+
void *data,
533+
const git_diff_delta *delta,
534+
const git_diff_range *range,
535+
char usage,
536+
const char *line,
537+
size_t line_len)
538+
{
539+
smart_str *string = (smart_str*)data;
540+
541+
smart_str_appendl(string, line, strlen(line));
542+
return 0;
543+
}
544+
545+
/*
546+
{{{ proto: Git2\Repository::diff(Git2\Tree $a, Git2\Tree $b, $options = NULL)
547+
Experimental
548+
*/
549+
PHP_METHOD(git2_repository, diff)
550+
{
551+
zval *old, *new;
552+
php_git2_tree *m_old, *m_new;
553+
php_git2_repository *m_repository;
554+
git_diff_list *list;
555+
smart_str string = {0};
556+
557+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
558+
"OO", &old, git2_tree_class_entry, &new, git2_tree_class_entry) == FAILURE) {
559+
return;
560+
}
561+
562+
m_repository = PHP_GIT2_GET_OBJECT(php_git2_repository, getThis());
563+
m_old = PHP_GIT2_GET_OBJECT(php_git2_tree, old);
564+
m_new = PHP_GIT2_GET_OBJECT(php_git2_tree, new);
565+
566+
git_diff_tree_to_tree(m_repository->repository, NULL, m_old->tree, m_new->tree, &list);
567+
568+
git_diff_print_compact(list, &string, printer);
569+
smart_str_0(&string);
570+
git_diff_list_free(list);
571+
572+
RETVAL_STRING(string.c, 0);
573+
}
574+
/* }}} */
575+
576+
530577
static zend_function_entry php_git2_repository_methods[] = {
531578
PHP_ME(git2_repository, __construct, arginfo_git2_repository___construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
532579
PHP_ME(git2_repository, isEmpty, NULL, ZEND_ACC_PUBLIC)
@@ -542,6 +589,7 @@ static zend_function_entry php_git2_repository_methods[] = {
542589
PHP_ME(git2_repository, write, arginfo_git2_repository_write, ZEND_ACC_PUBLIC)
543590
PHP_ME(git2_repository, getMergeBase,arginfo_git2_repository_get_merge_base,ZEND_ACC_PUBLIC)
544591
PHP_ME(git2_repository, checkout, arginfo_git2_repository_checkout, ZEND_ACC_PUBLIC)
592+
PHP_ME(git2_repository, diff, NULL, ZEND_ACC_PUBLIC)
545593
#ifdef lookup
546594
#undef lookup
547595
#endif

0 commit comments

Comments
 (0)