Permalink
Browse files

[pathspec] implement several functions

  • Loading branch information...
chobie committed Jan 17, 2014
1 parent 7ecff2f commit c3b172cda6993b06e8ae6b1bc7f1364968e112c9
Showing with 228 additions and 176 deletions.
  1. +2 −2 config.m4
  2. +6 −147 diff.c
  3. +1 −1 example/diff.php
  4. +8 −0 example/pathspec.php
  5. +147 −0 helper.c
  6. +22 −0 helper.h
  7. +38 −23 patch.c
  8. +3 −2 pathspec.c
  9. +1 −1 repository.c
View
@@ -11,14 +11,14 @@ if test $PHP_GIT2 != "no"; then
PHP_ADD_INCLUDE([$ext_srcdir/libgit2/include])
# for now
CFLAGS=" $CFLAGS -Wunused-variable -Wpointer-sign -Wimplicit-function-declaration -Winline -Wunused-macros -Wredundant-decls -Wstrict-aliasing=2 -Wswitch-enum -Wdeclaration-after-statement"
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"
if test "$PHP_GIT2_DEBUG" != "no"; then
CFLAGS="-g -O0 $CFLAGS"
fi
PHP_ADD_LIBPATH($ext_srcdir/libgit2/build, GIT2_SHARED_LIBADD)
PHP_ADD_LIBRARY(git2,, GIT2_SHARED_LIBADD)
#PHP_ADD_LIBRARY(git2,, GIT2_SHARED_LIBADD)
PHP_SUBST([CFLAGS])
ifdef([PHP_ADD_EXTENSION_DEP],
View
153 diff.c
@@ -2,152 +2,6 @@
#include "php_git2_priv.h"
#include "diff.h"
static void php_git2_array_to_git_diff_options(git_diff_options *options, zval *array TSRMLS_DC)
{
git_diff_options_init(options, GIT_DIFF_OPTIONS_VERSION);
options->version = php_git2_read_arrval_long(array, ZEND_STRS("version") TSRMLS_CC);
options->flags = php_git2_read_arrval_long(array, ZEND_STRS("flags") TSRMLS_CC);
options->ignore_submodules = php_git2_read_arrval_long(array, ZEND_STRS("ignore_submodules") TSRMLS_CC);
php_git2_array_to_strarray(&options->pathspec, php_git2_read_arrval(array, ZEND_STRS("pathspec") TSRMLS_CC) TSRMLS_CC);
// TODO(chobie): support notify cb
options->context_lines = php_git2_read_arrval_long(array, ZEND_STRS("context_lines") TSRMLS_CC);
options->interhunk_lines = php_git2_read_arrval_long(array, ZEND_STRS("interhunk_lines") TSRMLS_CC);
options->oid_abbrev = php_git2_read_arrval_long(array, ZEND_STRS("oid_abbrev") TSRMLS_CC);
options->max_size = php_git2_read_arrval_long(array, ZEND_STRS("max_size") TSRMLS_CC);
options->old_prefix = php_git2_read_arrval_string(array, ZEND_STRS("old_prefix") TSRMLS_CC);
options->new_prefix = php_git2_read_arrval_string(array, ZEND_STRS("new_prefix") TSRMLS_CC);
}
static void php_git2_git_diff_options_free(git_diff_options *options)
{
if (options->pathspec.count > 0) {
efree(options->pathspec.strings);
}
}
static void php_git2_git_diff_options_to_array(git_diff_options *options, zval **out TSRMLS_DC)
{
zval *result, *pathspec;
MAKE_STD_ZVAL(result);
array_init(result);
add_assoc_long_ex(result, ZEND_STRS("version"), options->version);
add_assoc_long_ex(result, ZEND_STRS("flags"), options->flags);
add_assoc_long_ex(result, ZEND_STRS("ignore_submodules"), options->ignore_submodules);
MAKE_STD_ZVAL(pathspec);
array_init(pathspec);
if (options->pathspec.count > 0) {
} else {
add_assoc_zval_ex(result, ZEND_STRS("pathspec"), pathspec);
}
if (options->notify_cb) {
} else {
add_assoc_null_ex(result, ZEND_STRS("notify_cb"));
}
add_assoc_long_ex(result, ZEND_STRS("context_lines"), options->context_lines);
add_assoc_long_ex(result, ZEND_STRS("interhunk_lines"), options->interhunk_lines);
add_assoc_long_ex(result, ZEND_STRS("oid_abbrev"), options->oid_abbrev);
add_assoc_long_ex(result, ZEND_STRS("max_size"), options->max_size);
if (options->notify_payload) {
} else {
add_assoc_null_ex(result, ZEND_STRS("notify_payload"));
}
if (options->old_prefix) {
add_assoc_string_ex(result, ZEND_STRS("old_prefix"), options->old_prefix, 1);
} else {
add_assoc_null_ex(result, ZEND_STRS("old_prefix"));
}
if (options->new_prefix) {
add_assoc_string_ex(result, ZEND_STRS("new_prefix"), options->new_prefix, 1);
} else {
add_assoc_null_ex(result, ZEND_STRS("new_prefix"));
}
*out = result;
}
static int php_git2_git_diff_file_cb(
const git_diff_delta *delta,
float progress,
void *payload)
{
php_git2_t *result;
zval *param_delta = NULL, *param_progress = NULL, *retval_ptr = NULL;
php_git2_multi_cb_t *p = (php_git2_multi_cb_t*)payload;
int i = 0, retval = 0;
GIT2_TSRMLS_SET(p->tsrm_ls)
Z_ADDREF_P(p->payload);
MAKE_STD_ZVAL(param_progress);
ZVAL_DOUBLE(param_progress, progress);
php_git2_diff_delta_to_array(delta, &param_delta TSRMLS_CC);
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)) {
return GIT_EUSER;
}
retval = Z_LVAL_P(retval_ptr);
zval_ptr_dtor(&retval_ptr);
return retval;
}
static int php_git2_git_diff_hunk_cb(
const git_diff_delta *delta,
const git_diff_hunk *hunk,
void *payload)
{
php_git2_t *result;
zval *param_delta = NULL, *param_hunk = NULL, *retval_ptr = NULL;
php_git2_multi_cb_t *p = (php_git2_multi_cb_t*)payload;
int i = 0, retval = 0;
GIT2_TSRMLS_SET(p->tsrm_ls)
Z_ADDREF_P(p->payload);
php_git2_diff_delta_to_array(delta, &param_delta TSRMLS_CC);
php_git2_diff_hunk_to_array(hunk, &param_hunk TSRMLS_CC);
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)) {
return GIT_EUSER;
}
retval = Z_LVAL_P(retval_ptr);
zval_ptr_dtor(&retval_ptr);
return retval;
}
static int php_git2_git_diff_line_cb(
const git_diff_delta *delta,
const git_diff_hunk *hunk,
const git_diff_line *line,
void *payload) {
php_git2_t *result;
zval *param_delta = NULL, *param_hunk = NULL, *param_line = NULL, *retval_ptr = NULL;
php_git2_multi_cb_t *p = (php_git2_multi_cb_t*)payload;
int i = 0, retval = 0;
GIT2_TSRMLS_SET(p->tsrm_ls)
Z_ADDREF_P(p->payload);
php_git2_diff_delta_to_array(delta, &param_delta TSRMLS_CC);
php_git2_diff_hunk_to_array(hunk, &param_hunk TSRMLS_CC);
php_git2_diff_line_to_array(line, &param_line TSRMLS_CC);
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)) {
return GIT_EUSER;
}
retval = Z_LVAL_P(retval_ptr);
zval_ptr_dtor(&retval_ptr);
return retval;
}
/* {{{ proto void git_diff_free(resource $diff)
*/
PHP_FUNCTION(git_diff_free)
@@ -238,7 +92,7 @@ PHP_FUNCTION(git_diff_index_to_workdir)
git_diff *diff = NULL;
zval *repo = NULL, *index = NULL, *opts = NULL;
php_git2_t *_repo = NULL, *_index = NULL, *_diff = NULL;
git_diff_options options = {0};
git_diff_options options = GIT_DIFF_OPTIONS_INIT;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"rra", &repo, &index, &opts) == FAILURE) {
@@ -276,6 +130,11 @@ PHP_FUNCTION(git_diff_tree_to_workdir)
ZEND_FETCH_RESOURCE(_old_tree, php_git2_t*, &old_tree, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
php_git2_array_to_git_diff_options(&options, opts TSRMLS_CC);
result = git_diff_tree_to_workdir(&diff, PHP_GIT2_V(_repo, repository), PHP_GIT2_V(_old_tree, tree), &options);
if (php_git2_check_error(result, "git_diff_tree_to_workdir" TSRMLS_CC)) {
php_git2_git_diff_options_free(&options);
RETURN_FALSE
}
php_git2_git_diff_options_free(&options);
if (php_git2_make_resource(&_result, PHP_GIT2_TYPE_DIFF, diff, 0 TSRMLS_CC)) {
View
@@ -1,7 +1,7 @@
<?php
$repo = git_repository_open(".");
$tree = git_tree_lookup($repo, "e14ccb8e18d632d78ce2f0aeb06597a03f42b237");
$diff = git_diff_tree_to_workdir($repo, $tree, array());
$diff = git_diff_tree_to_workdir($repo, $tree, git_diff_options_init());
$p = array();
git_diff_print($diff, GIT_DIFF_FORMAT_PATCH, function($diff_delta, $diff_hunk, $diff_line, $payload){
View
@@ -0,0 +1,8 @@
<?php
$repo = git_repository_open(".");
$ps = git_pathspec_new(array("*.php"));
$list = git_pathspec_match_workdir($repo, GIT_PATHSPEC_FIND_FAILURES, $ps);
for ($i = 0; $i < git_pathspec_match_list_entrycount($list); $i++) {
$entry = git_pathspec_match_list_entry($list, $i);
printf("%s\n", $entry);
}
View
147 helper.c
@@ -221,6 +221,9 @@ void php_git2_array_to_strarray(git_strarray *out, zval *array TSRMLS_DC)
HashPosition pos;
zval **value;
if (array == NULL) {
return;
}
if (Z_TYPE_P(array) != IS_ARRAY){
return;
}
@@ -591,3 +594,147 @@ void php_git2_diff_delta_to_array(git_diff_delta *delta, zval **out TSRMLS_DC)
*out = result;
}
void php_git2_array_to_git_diff_options(git_diff_options *options, zval *array TSRMLS_DC)
{
git_diff_options_init(options, GIT_DIFF_OPTIONS_VERSION);
options->version = php_git2_read_arrval_long(array, ZEND_STRS("version") TSRMLS_CC);
options->flags = php_git2_read_arrval_long(array, ZEND_STRS("flags") TSRMLS_CC);
options->ignore_submodules = php_git2_read_arrval_long(array, ZEND_STRS("ignore_submodules") TSRMLS_CC);
php_git2_array_to_strarray(&options->pathspec, php_git2_read_arrval(array, ZEND_STRS("pathspec") TSRMLS_CC) TSRMLS_CC);
// TODO(chobie): support notify cb
options->context_lines = php_git2_read_arrval_long(array, ZEND_STRS("context_lines") TSRMLS_CC);
options->interhunk_lines = php_git2_read_arrval_long(array, ZEND_STRS("interhunk_lines") TSRMLS_CC);
options->oid_abbrev = php_git2_read_arrval_long(array, ZEND_STRS("oid_abbrev") TSRMLS_CC);
options->max_size = php_git2_read_arrval_long(array, ZEND_STRS("max_size") TSRMLS_CC);
options->old_prefix = php_git2_read_arrval_string(array, ZEND_STRS("old_prefix") TSRMLS_CC);
options->new_prefix = php_git2_read_arrval_string(array, ZEND_STRS("new_prefix") TSRMLS_CC);
}
void php_git2_git_diff_options_free(git_diff_options *options)
{
if (options->pathspec.count > 0) {
efree(options->pathspec.strings);
}
}
void php_git2_git_diff_options_to_array(git_diff_options *options, zval **out TSRMLS_DC)
{
zval *result, *pathspec;
MAKE_STD_ZVAL(result);
array_init(result);
add_assoc_long_ex(result, ZEND_STRS("version"), options->version);
add_assoc_long_ex(result, ZEND_STRS("flags"), options->flags);
add_assoc_long_ex(result, ZEND_STRS("ignore_submodules"), options->ignore_submodules);
MAKE_STD_ZVAL(pathspec);
array_init(pathspec);
if (options->pathspec.count > 0) {
} else {
add_assoc_zval_ex(result, ZEND_STRS("pathspec"), pathspec);
}
if (options->notify_cb) {
} else {
add_assoc_null_ex(result, ZEND_STRS("notify_cb"));
}
add_assoc_long_ex(result, ZEND_STRS("context_lines"), options->context_lines);
add_assoc_long_ex(result, ZEND_STRS("interhunk_lines"), options->interhunk_lines);
add_assoc_long_ex(result, ZEND_STRS("oid_abbrev"), options->oid_abbrev);
add_assoc_long_ex(result, ZEND_STRS("max_size"), options->max_size);
if (options->notify_payload) {
} else {
add_assoc_null_ex(result, ZEND_STRS("notify_payload"));
}
if (options->old_prefix) {
add_assoc_string_ex(result, ZEND_STRS("old_prefix"), options->old_prefix, 1);
} else {
add_assoc_null_ex(result, ZEND_STRS("old_prefix"));
}
if (options->new_prefix) {
add_assoc_string_ex(result, ZEND_STRS("new_prefix"), options->new_prefix, 1);
} else {
add_assoc_null_ex(result, ZEND_STRS("new_prefix"));
}
*out = result;
}
int php_git2_git_diff_file_cb(
const git_diff_delta *delta,
float progress,
void *payload)
{
php_git2_t *result;
zval *param_delta = NULL, *param_progress = NULL, *retval_ptr = NULL;
php_git2_multi_cb_t *p = (php_git2_multi_cb_t*)payload;
int i = 0, retval = 0;
GIT2_TSRMLS_SET(p->tsrm_ls)
Z_ADDREF_P(p->payload);
MAKE_STD_ZVAL(param_progress);
ZVAL_DOUBLE(param_progress, progress);
php_git2_diff_delta_to_array(delta, &param_delta TSRMLS_CC);
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)) {
return GIT_EUSER;
}
retval = Z_LVAL_P(retval_ptr);
zval_ptr_dtor(&retval_ptr);
return retval;
}
int php_git2_git_diff_hunk_cb(
const git_diff_delta *delta,
const git_diff_hunk *hunk,
void *payload)
{
php_git2_t *result;
zval *param_delta = NULL, *param_hunk = NULL, *retval_ptr = NULL;
php_git2_multi_cb_t *p = (php_git2_multi_cb_t*)payload;
int i = 0, retval = 0;
GIT2_TSRMLS_SET(p->tsrm_ls)
Z_ADDREF_P(p->payload);
php_git2_diff_delta_to_array(delta, &param_delta TSRMLS_CC);
php_git2_diff_hunk_to_array(hunk, &param_hunk TSRMLS_CC);
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)) {
return GIT_EUSER;
}
retval = Z_LVAL_P(retval_ptr);
zval_ptr_dtor(&retval_ptr);
return retval;
}
int php_git2_git_diff_line_cb(
const git_diff_delta *delta,
const git_diff_hunk *hunk,
const git_diff_line *line,
void *payload) {
php_git2_t *result;
zval *param_delta = NULL, *param_hunk = NULL, *param_line = NULL, *retval_ptr = NULL;
php_git2_multi_cb_t *p = (php_git2_multi_cb_t*)payload;
int i = 0, retval = 0;
GIT2_TSRMLS_SET(p->tsrm_ls)
Z_ADDREF_P(p->payload);
php_git2_diff_delta_to_array(delta, &param_delta TSRMLS_CC);
php_git2_diff_hunk_to_array(hunk, &param_hunk TSRMLS_CC);
php_git2_diff_line_to_array(line, &param_line TSRMLS_CC);
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)) {
return GIT_EUSER;
}
retval = Z_LVAL_P(retval_ptr);
zval_ptr_dtor(&retval_ptr);
return retval;
}
View
@@ -70,4 +70,26 @@ void php_git2_diff_hunk_to_array(git_diff_hunk *hunk, zval **out TSRMLS_DC);
void php_git2_diff_file_to_array(git_diff_file *file, zval **out TSRMLS_DC);
void php_git2_diff_delta_to_array(git_diff_delta *delta, zval **out TSRMLS_DC);
void php_git2_array_to_git_diff_options(git_diff_options *options, zval *array TSRMLS_DC);
void php_git2_git_diff_options_free(git_diff_options *options);
void php_git2_git_diff_options_to_array(git_diff_options *options, zval **out TSRMLS_DC);
int php_git2_git_diff_file_cb(
const git_diff_delta *delta,
float progress,
void *payload);
int php_git2_git_diff_hunk_cb(
const git_diff_delta *delta,
const git_diff_hunk *hunk,
void *payload);
int php_git2_git_diff_line_cb(
const git_diff_delta *delta,
const git_diff_hunk *hunk,
const git_diff_line *line,
void *payload);
#endif
Oops, something went wrong.

0 comments on commit c3b172c

Please sign in to comment.