Skip to content

Commit c3b172c

Browse files
committed
[pathspec] implement several functions
1 parent 7ecff2f commit c3b172c

9 files changed

+228
-176
lines changed

config.m4

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ if test $PHP_GIT2 != "no"; then
1111
PHP_ADD_INCLUDE([$ext_srcdir/libgit2/include])
1212

1313
# for now
14-
CFLAGS=" $CFLAGS -Wunused-variable -Wpointer-sign -Wimplicit-function-declaration -Winline -Wunused-macros -Wredundant-decls -Wstrict-aliasing=2 -Wswitch-enum -Wdeclaration-after-statement"
14+
CFLAGS=" $CFLAGS -Wunused-variable -Wpointer-sign -Wimplicit-function-declaration -Winline -Wunused-macros -Wredundant-decls -Wstrict-aliasing=2 -Wswitch-enum -Wdeclaration-after-statement -Wl,libgit2/build/libgit2.a"
1515

1616
if test "$PHP_GIT2_DEBUG" != "no"; then
1717
CFLAGS="-g -O0 $CFLAGS"
1818
fi
1919

2020
PHP_ADD_LIBPATH($ext_srcdir/libgit2/build, GIT2_SHARED_LIBADD)
21-
PHP_ADD_LIBRARY(git2,, GIT2_SHARED_LIBADD)
21+
#PHP_ADD_LIBRARY(git2,, GIT2_SHARED_LIBADD)
2222
PHP_SUBST([CFLAGS])
2323

2424
ifdef([PHP_ADD_EXTENSION_DEP],

diff.c

+6-147
Original file line numberDiff line numberDiff line change
@@ -2,152 +2,6 @@
22
#include "php_git2_priv.h"
33
#include "diff.h"
44

5-
6-
static void php_git2_array_to_git_diff_options(git_diff_options *options, zval *array TSRMLS_DC)
7-
{
8-
git_diff_options_init(options, GIT_DIFF_OPTIONS_VERSION);
9-
10-
options->version = php_git2_read_arrval_long(array, ZEND_STRS("version") TSRMLS_CC);
11-
options->flags = php_git2_read_arrval_long(array, ZEND_STRS("flags") TSRMLS_CC);
12-
options->ignore_submodules = php_git2_read_arrval_long(array, ZEND_STRS("ignore_submodules") TSRMLS_CC);
13-
14-
php_git2_array_to_strarray(&options->pathspec, php_git2_read_arrval(array, ZEND_STRS("pathspec") TSRMLS_CC) TSRMLS_CC);
15-
// TODO(chobie): support notify cb
16-
17-
18-
options->context_lines = php_git2_read_arrval_long(array, ZEND_STRS("context_lines") TSRMLS_CC);
19-
options->interhunk_lines = php_git2_read_arrval_long(array, ZEND_STRS("interhunk_lines") TSRMLS_CC);
20-
options->oid_abbrev = php_git2_read_arrval_long(array, ZEND_STRS("oid_abbrev") TSRMLS_CC);
21-
options->max_size = php_git2_read_arrval_long(array, ZEND_STRS("max_size") TSRMLS_CC);
22-
options->old_prefix = php_git2_read_arrval_string(array, ZEND_STRS("old_prefix") TSRMLS_CC);
23-
options->new_prefix = php_git2_read_arrval_string(array, ZEND_STRS("new_prefix") TSRMLS_CC);
24-
}
25-
26-
static void php_git2_git_diff_options_free(git_diff_options *options)
27-
{
28-
if (options->pathspec.count > 0) {
29-
efree(options->pathspec.strings);
30-
}
31-
}
32-
33-
static void php_git2_git_diff_options_to_array(git_diff_options *options, zval **out TSRMLS_DC)
34-
{
35-
zval *result, *pathspec;
36-
37-
MAKE_STD_ZVAL(result);
38-
array_init(result);
39-
add_assoc_long_ex(result, ZEND_STRS("version"), options->version);
40-
add_assoc_long_ex(result, ZEND_STRS("flags"), options->flags);
41-
add_assoc_long_ex(result, ZEND_STRS("ignore_submodules"), options->ignore_submodules);
42-
43-
MAKE_STD_ZVAL(pathspec);
44-
array_init(pathspec);
45-
if (options->pathspec.count > 0) {
46-
} else {
47-
add_assoc_zval_ex(result, ZEND_STRS("pathspec"), pathspec);
48-
}
49-
50-
if (options->notify_cb) {
51-
} else {
52-
add_assoc_null_ex(result, ZEND_STRS("notify_cb"));
53-
}
54-
55-
add_assoc_long_ex(result, ZEND_STRS("context_lines"), options->context_lines);
56-
add_assoc_long_ex(result, ZEND_STRS("interhunk_lines"), options->interhunk_lines);
57-
add_assoc_long_ex(result, ZEND_STRS("oid_abbrev"), options->oid_abbrev);
58-
add_assoc_long_ex(result, ZEND_STRS("max_size"), options->max_size);
59-
if (options->notify_payload) {
60-
} else {
61-
add_assoc_null_ex(result, ZEND_STRS("notify_payload"));
62-
}
63-
if (options->old_prefix) {
64-
add_assoc_string_ex(result, ZEND_STRS("old_prefix"), options->old_prefix, 1);
65-
} else {
66-
add_assoc_null_ex(result, ZEND_STRS("old_prefix"));
67-
}
68-
if (options->new_prefix) {
69-
add_assoc_string_ex(result, ZEND_STRS("new_prefix"), options->new_prefix, 1);
70-
} else {
71-
add_assoc_null_ex(result, ZEND_STRS("new_prefix"));
72-
}
73-
74-
*out = result;
75-
}
76-
77-
78-
static int php_git2_git_diff_file_cb(
79-
const git_diff_delta *delta,
80-
float progress,
81-
void *payload)
82-
{
83-
php_git2_t *result;
84-
zval *param_delta = NULL, *param_progress = NULL, *retval_ptr = NULL;
85-
php_git2_multi_cb_t *p = (php_git2_multi_cb_t*)payload;
86-
int i = 0, retval = 0;
87-
GIT2_TSRMLS_SET(p->tsrm_ls)
88-
89-
Z_ADDREF_P(p->payload);
90-
MAKE_STD_ZVAL(param_progress);
91-
ZVAL_DOUBLE(param_progress, progress);
92-
php_git2_diff_delta_to_array(delta, &param_delta TSRMLS_CC);
93-
if (php_git2_call_function_v(&p->callbacks[0].fci, &p->callbacks[0].fcc TSRMLS_CC, &retval_ptr, 3, &param_delta, &param_progress, &p->payload)) {
94-
return GIT_EUSER;
95-
}
96-
retval = Z_LVAL_P(retval_ptr);
97-
zval_ptr_dtor(&retval_ptr);
98-
99-
return retval;
100-
}
101-
102-
static int php_git2_git_diff_hunk_cb(
103-
const git_diff_delta *delta,
104-
const git_diff_hunk *hunk,
105-
void *payload)
106-
{
107-
php_git2_t *result;
108-
zval *param_delta = NULL, *param_hunk = NULL, *retval_ptr = NULL;
109-
php_git2_multi_cb_t *p = (php_git2_multi_cb_t*)payload;
110-
int i = 0, retval = 0;
111-
GIT2_TSRMLS_SET(p->tsrm_ls)
112-
113-
Z_ADDREF_P(p->payload);
114-
php_git2_diff_delta_to_array(delta, &param_delta TSRMLS_CC);
115-
php_git2_diff_hunk_to_array(hunk, &param_hunk TSRMLS_CC);
116-
117-
if (php_git2_call_function_v(&p->callbacks[1].fci, &p->callbacks[1].fcc TSRMLS_CC, &retval_ptr, 3, &param_delta, &param_hunk, &p->payload)) {
118-
return GIT_EUSER;
119-
}
120-
121-
retval = Z_LVAL_P(retval_ptr);
122-
zval_ptr_dtor(&retval_ptr);
123-
return retval;
124-
}
125-
126-
static int php_git2_git_diff_line_cb(
127-
const git_diff_delta *delta,
128-
const git_diff_hunk *hunk,
129-
const git_diff_line *line,
130-
void *payload) {
131-
php_git2_t *result;
132-
zval *param_delta = NULL, *param_hunk = NULL, *param_line = NULL, *retval_ptr = NULL;
133-
php_git2_multi_cb_t *p = (php_git2_multi_cb_t*)payload;
134-
int i = 0, retval = 0;
135-
GIT2_TSRMLS_SET(p->tsrm_ls)
136-
137-
Z_ADDREF_P(p->payload);
138-
php_git2_diff_delta_to_array(delta, &param_delta TSRMLS_CC);
139-
php_git2_diff_hunk_to_array(hunk, &param_hunk TSRMLS_CC);
140-
php_git2_diff_line_to_array(line, &param_line TSRMLS_CC);
141-
142-
if (php_git2_call_function_v(&p->callbacks[2].fci, &p->callbacks[2].fcc TSRMLS_CC, &retval_ptr, 4, &param_delta, &param_hunk, &param_line, &p->payload)) {
143-
return GIT_EUSER;
144-
}
145-
146-
retval = Z_LVAL_P(retval_ptr);
147-
zval_ptr_dtor(&retval_ptr);
148-
return retval;
149-
}
150-
1515
/* {{{ proto void git_diff_free(resource $diff)
1526
*/
1537
PHP_FUNCTION(git_diff_free)
@@ -238,7 +92,7 @@ PHP_FUNCTION(git_diff_index_to_workdir)
23892
git_diff *diff = NULL;
23993
zval *repo = NULL, *index = NULL, *opts = NULL;
24094
php_git2_t *_repo = NULL, *_index = NULL, *_diff = NULL;
241-
git_diff_options options = {0};
95+
git_diff_options options = GIT_DIFF_OPTIONS_INIT;
24296

24397
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
24498
"rra", &repo, &index, &opts) == FAILURE) {
@@ -276,6 +130,11 @@ PHP_FUNCTION(git_diff_tree_to_workdir)
276130
ZEND_FETCH_RESOURCE(_old_tree, php_git2_t*, &old_tree, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
277131
php_git2_array_to_git_diff_options(&options, opts TSRMLS_CC);
278132
result = git_diff_tree_to_workdir(&diff, PHP_GIT2_V(_repo, repository), PHP_GIT2_V(_old_tree, tree), &options);
133+
if (php_git2_check_error(result, "git_diff_tree_to_workdir" TSRMLS_CC)) {
134+
php_git2_git_diff_options_free(&options);
135+
RETURN_FALSE
136+
}
137+
279138
php_git2_git_diff_options_free(&options);
280139

281140
if (php_git2_make_resource(&_result, PHP_GIT2_TYPE_DIFF, diff, 0 TSRMLS_CC)) {

example/diff.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
$repo = git_repository_open(".");
33
$tree = git_tree_lookup($repo, "e14ccb8e18d632d78ce2f0aeb06597a03f42b237");
4-
$diff = git_diff_tree_to_workdir($repo, $tree, array());
4+
$diff = git_diff_tree_to_workdir($repo, $tree, git_diff_options_init());
55

66
$p = array();
77
git_diff_print($diff, GIT_DIFF_FORMAT_PATCH, function($diff_delta, $diff_hunk, $diff_line, $payload){

example/pathspec.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
$repo = git_repository_open(".");
3+
$ps = git_pathspec_new(array("*.php"));
4+
$list = git_pathspec_match_workdir($repo, GIT_PATHSPEC_FIND_FAILURES, $ps);
5+
for ($i = 0; $i < git_pathspec_match_list_entrycount($list); $i++) {
6+
$entry = git_pathspec_match_list_entry($list, $i);
7+
printf("%s\n", $entry);
8+
}

helper.c

+147
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ void php_git2_array_to_strarray(git_strarray *out, zval *array TSRMLS_DC)
221221
HashPosition pos;
222222
zval **value;
223223

224+
if (array == NULL) {
225+
return;
226+
}
224227
if (Z_TYPE_P(array) != IS_ARRAY){
225228
return;
226229
}
@@ -591,3 +594,147 @@ void php_git2_diff_delta_to_array(git_diff_delta *delta, zval **out TSRMLS_DC)
591594

592595
*out = result;
593596
}
597+
598+
void php_git2_array_to_git_diff_options(git_diff_options *options, zval *array TSRMLS_DC)
599+
{
600+
git_diff_options_init(options, GIT_DIFF_OPTIONS_VERSION);
601+
602+
options->version = php_git2_read_arrval_long(array, ZEND_STRS("version") TSRMLS_CC);
603+
options->flags = php_git2_read_arrval_long(array, ZEND_STRS("flags") TSRMLS_CC);
604+
options->ignore_submodules = php_git2_read_arrval_long(array, ZEND_STRS("ignore_submodules") TSRMLS_CC);
605+
606+
php_git2_array_to_strarray(&options->pathspec, php_git2_read_arrval(array, ZEND_STRS("pathspec") TSRMLS_CC) TSRMLS_CC);
607+
// TODO(chobie): support notify cb
608+
609+
options->context_lines = php_git2_read_arrval_long(array, ZEND_STRS("context_lines") TSRMLS_CC);
610+
options->interhunk_lines = php_git2_read_arrval_long(array, ZEND_STRS("interhunk_lines") TSRMLS_CC);
611+
options->oid_abbrev = php_git2_read_arrval_long(array, ZEND_STRS("oid_abbrev") TSRMLS_CC);
612+
options->max_size = php_git2_read_arrval_long(array, ZEND_STRS("max_size") TSRMLS_CC);
613+
options->old_prefix = php_git2_read_arrval_string(array, ZEND_STRS("old_prefix") TSRMLS_CC);
614+
options->new_prefix = php_git2_read_arrval_string(array, ZEND_STRS("new_prefix") TSRMLS_CC);
615+
}
616+
617+
void php_git2_git_diff_options_free(git_diff_options *options)
618+
{
619+
if (options->pathspec.count > 0) {
620+
efree(options->pathspec.strings);
621+
}
622+
}
623+
624+
void php_git2_git_diff_options_to_array(git_diff_options *options, zval **out TSRMLS_DC)
625+
{
626+
zval *result, *pathspec;
627+
628+
MAKE_STD_ZVAL(result);
629+
array_init(result);
630+
add_assoc_long_ex(result, ZEND_STRS("version"), options->version);
631+
add_assoc_long_ex(result, ZEND_STRS("flags"), options->flags);
632+
add_assoc_long_ex(result, ZEND_STRS("ignore_submodules"), options->ignore_submodules);
633+
634+
MAKE_STD_ZVAL(pathspec);
635+
array_init(pathspec);
636+
if (options->pathspec.count > 0) {
637+
} else {
638+
add_assoc_zval_ex(result, ZEND_STRS("pathspec"), pathspec);
639+
}
640+
641+
if (options->notify_cb) {
642+
} else {
643+
add_assoc_null_ex(result, ZEND_STRS("notify_cb"));
644+
}
645+
646+
add_assoc_long_ex(result, ZEND_STRS("context_lines"), options->context_lines);
647+
add_assoc_long_ex(result, ZEND_STRS("interhunk_lines"), options->interhunk_lines);
648+
add_assoc_long_ex(result, ZEND_STRS("oid_abbrev"), options->oid_abbrev);
649+
add_assoc_long_ex(result, ZEND_STRS("max_size"), options->max_size);
650+
if (options->notify_payload) {
651+
} else {
652+
add_assoc_null_ex(result, ZEND_STRS("notify_payload"));
653+
}
654+
if (options->old_prefix) {
655+
add_assoc_string_ex(result, ZEND_STRS("old_prefix"), options->old_prefix, 1);
656+
} else {
657+
add_assoc_null_ex(result, ZEND_STRS("old_prefix"));
658+
}
659+
if (options->new_prefix) {
660+
add_assoc_string_ex(result, ZEND_STRS("new_prefix"), options->new_prefix, 1);
661+
} else {
662+
add_assoc_null_ex(result, ZEND_STRS("new_prefix"));
663+
}
664+
665+
*out = result;
666+
}
667+
668+
int php_git2_git_diff_file_cb(
669+
const git_diff_delta *delta,
670+
float progress,
671+
void *payload)
672+
{
673+
php_git2_t *result;
674+
zval *param_delta = NULL, *param_progress = NULL, *retval_ptr = NULL;
675+
php_git2_multi_cb_t *p = (php_git2_multi_cb_t*)payload;
676+
int i = 0, retval = 0;
677+
GIT2_TSRMLS_SET(p->tsrm_ls)
678+
679+
Z_ADDREF_P(p->payload);
680+
MAKE_STD_ZVAL(param_progress);
681+
ZVAL_DOUBLE(param_progress, progress);
682+
php_git2_diff_delta_to_array(delta, &param_delta TSRMLS_CC);
683+
if (php_git2_call_function_v(&p->callbacks[0].fci, &p->callbacks[0].fcc TSRMLS_CC, &retval_ptr, 3, &param_delta, &param_progress, &p->payload)) {
684+
return GIT_EUSER;
685+
}
686+
retval = Z_LVAL_P(retval_ptr);
687+
zval_ptr_dtor(&retval_ptr);
688+
689+
return retval;
690+
}
691+
692+
int php_git2_git_diff_hunk_cb(
693+
const git_diff_delta *delta,
694+
const git_diff_hunk *hunk,
695+
void *payload)
696+
{
697+
php_git2_t *result;
698+
zval *param_delta = NULL, *param_hunk = NULL, *retval_ptr = NULL;
699+
php_git2_multi_cb_t *p = (php_git2_multi_cb_t*)payload;
700+
int i = 0, retval = 0;
701+
GIT2_TSRMLS_SET(p->tsrm_ls)
702+
703+
Z_ADDREF_P(p->payload);
704+
php_git2_diff_delta_to_array(delta, &param_delta TSRMLS_CC);
705+
php_git2_diff_hunk_to_array(hunk, &param_hunk TSRMLS_CC);
706+
707+
if (php_git2_call_function_v(&p->callbacks[1].fci, &p->callbacks[1].fcc TSRMLS_CC, &retval_ptr, 3, &param_delta, &param_hunk, &p->payload)) {
708+
return GIT_EUSER;
709+
}
710+
711+
retval = Z_LVAL_P(retval_ptr);
712+
zval_ptr_dtor(&retval_ptr);
713+
return retval;
714+
}
715+
716+
int php_git2_git_diff_line_cb(
717+
const git_diff_delta *delta,
718+
const git_diff_hunk *hunk,
719+
const git_diff_line *line,
720+
void *payload) {
721+
php_git2_t *result;
722+
zval *param_delta = NULL, *param_hunk = NULL, *param_line = NULL, *retval_ptr = NULL;
723+
php_git2_multi_cb_t *p = (php_git2_multi_cb_t*)payload;
724+
int i = 0, retval = 0;
725+
GIT2_TSRMLS_SET(p->tsrm_ls)
726+
727+
Z_ADDREF_P(p->payload);
728+
php_git2_diff_delta_to_array(delta, &param_delta TSRMLS_CC);
729+
php_git2_diff_hunk_to_array(hunk, &param_hunk TSRMLS_CC);
730+
php_git2_diff_line_to_array(line, &param_line TSRMLS_CC);
731+
732+
if (php_git2_call_function_v(&p->callbacks[2].fci, &p->callbacks[2].fcc TSRMLS_CC, &retval_ptr, 4, &param_delta, &param_hunk, &param_line, &p->payload)) {
733+
return GIT_EUSER;
734+
}
735+
736+
retval = Z_LVAL_P(retval_ptr);
737+
zval_ptr_dtor(&retval_ptr);
738+
return retval;
739+
}
740+

helper.h

+22
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,26 @@ void php_git2_diff_hunk_to_array(git_diff_hunk *hunk, zval **out TSRMLS_DC);
7070
void php_git2_diff_file_to_array(git_diff_file *file, zval **out TSRMLS_DC);
7171

7272
void php_git2_diff_delta_to_array(git_diff_delta *delta, zval **out TSRMLS_DC);
73+
74+
void php_git2_array_to_git_diff_options(git_diff_options *options, zval *array TSRMLS_DC);
75+
76+
void php_git2_git_diff_options_free(git_diff_options *options);
77+
78+
void php_git2_git_diff_options_to_array(git_diff_options *options, zval **out TSRMLS_DC);
79+
80+
int php_git2_git_diff_file_cb(
81+
const git_diff_delta *delta,
82+
float progress,
83+
void *payload);
84+
85+
int php_git2_git_diff_hunk_cb(
86+
const git_diff_delta *delta,
87+
const git_diff_hunk *hunk,
88+
void *payload);
89+
int php_git2_git_diff_line_cb(
90+
const git_diff_delta *delta,
91+
const git_diff_hunk *hunk,
92+
const git_diff_line *line,
93+
void *payload);
94+
7395
#endif

0 commit comments

Comments
 (0)