Add tests and fix bugs for diff whitespace options

Once I added tests for the whitespace handling options of
diff, I realized that there were some bugs.  This fixes
those and adds the new tests into the test suite.
This commit is contained in:
Russell Belfer 2012-02-07 15:30:18 -08:00
parent a2e895be82
commit caf71ec081
11 changed files with 97 additions and 8 deletions

View File

@ -486,9 +486,11 @@ static int set_file_is_binary(
return GIT_SUCCESS; return GIT_SUCCESS;
} }
static void setup_xdiff_config(git_diff_options *opts, xdemitconf_t *cfg) static void setup_xdiff_options(
git_diff_options *opts, xdemitconf_t *cfg, xpparam_t *param)
{ {
memset(cfg, 0, sizeof(xdemitconf_t)); memset(cfg, 0, sizeof(xdemitconf_t));
memset(param, 0, sizeof(xpparam_t));
cfg->ctxlen = cfg->ctxlen =
(!opts || !opts->context_lines) ? 3 : opts->context_lines; (!opts || !opts->context_lines) ? 3 : opts->context_lines;
@ -499,11 +501,11 @@ static void setup_xdiff_config(git_diff_options *opts, xdemitconf_t *cfg)
return; return;
if (opts->flags & GIT_DIFF_IGNORE_WHITESPACE) if (opts->flags & GIT_DIFF_IGNORE_WHITESPACE)
cfg->flags |= XDF_WHITESPACE_FLAGS; param->flags |= XDF_WHITESPACE_FLAGS;
if (opts->flags & GIT_DIFF_IGNORE_WHITESPACE_CHANGE) if (opts->flags & GIT_DIFF_IGNORE_WHITESPACE_CHANGE)
cfg->flags |= XDF_IGNORE_WHITESPACE_CHANGE; param->flags |= XDF_IGNORE_WHITESPACE_CHANGE;
if (opts->flags & GIT_DIFF_IGNORE_WHITESPACE_EOL) if (opts->flags & GIT_DIFF_IGNORE_WHITESPACE_EOL)
cfg->flags |= XDF_IGNORE_WHITESPACE_AT_EOL; param->flags |= XDF_IGNORE_WHITESPACE_AT_EOL;
} }
int git_diff_foreach( int git_diff_foreach(
@ -525,8 +527,7 @@ int git_diff_foreach(
di.hunk_cb = hunk_cb; di.hunk_cb = hunk_cb;
di.line_cb = line_cb; di.line_cb = line_cb;
memset(&xdiff_params, 0, sizeof(xdiff_params)); setup_xdiff_options(&diff->opts, &xdiff_config, &xdiff_params);
setup_xdiff_config(&diff->opts, &xdiff_config);
memset(&xdiff_callback, 0, sizeof(xdiff_callback)); memset(&xdiff_callback, 0, sizeof(xdiff_callback));
xdiff_callback.outf = diff_output_cb; xdiff_callback.outf = diff_output_cb;
xdiff_callback.priv = &di; xdiff_callback.priv = &di;
@ -898,8 +899,7 @@ int git_diff_blobs(
di.hunk_cb = hunk_cb; di.hunk_cb = hunk_cb;
di.line_cb = line_cb; di.line_cb = line_cb;
memset(&xdiff_params, 0, sizeof(xdiff_params)); setup_xdiff_options(options, &xdiff_config, &xdiff_params);
setup_xdiff_config(options, &xdiff_config);
memset(&xdiff_callback, 0, sizeof(xdiff_callback)); memset(&xdiff_callback, 0, sizeof(xdiff_callback));
xdiff_callback.outf = diff_output_cb; xdiff_callback.outf = diff_output_cb;
xdiff_callback.priv = &di; xdiff_callback.priv = &di;

View File

@ -72,9 +72,11 @@ int diff_line_fn(
e->line_ctxt++; e->line_ctxt++;
break; break;
case GIT_DIFF_LINE_ADDITION: case GIT_DIFF_LINE_ADDITION:
case GIT_DIFF_LINE_ADD_EOFNL:
e->line_adds++; e->line_adds++;
break; break;
case GIT_DIFF_LINE_DELETION: case GIT_DIFF_LINE_DELETION:
case GIT_DIFF_LINE_DEL_EOFNL:
e->line_dels++; e->line_dels++;
break; break;
default: default:

View File

@ -84,3 +84,90 @@ void test_diff_tree__0(void)
git_tree_free(b); git_tree_free(b);
git_tree_free(c); git_tree_free(c);
} }
void test_diff_tree__options(void)
{
/* grabbed a couple of commit oids from the history of the attr repo */
const char *a_commit = "6bab5c79cd5140d0";
const char *b_commit = "605812ab7fe421fdd";
const char *c_commit = "f5b0af1fb4f5";
const char *d_commit = "a97cc019851";
git_tree *a = resolve_commit_oid_to_tree(g_repo, a_commit);
git_tree *b = resolve_commit_oid_to_tree(g_repo, b_commit);
git_tree *c = resolve_commit_oid_to_tree(g_repo, c_commit);
git_tree *d = resolve_commit_oid_to_tree(g_repo, d_commit);
git_diff_options opts = {0};
git_diff_list *diff = NULL;
diff_expects exp;
int test_ab_or_cd[] = { 0, 0, 0, 0, 1, 1, 1, 1, 1 };
git_diff_options test_options[] = {
/* a vs b tests */
{ GIT_DIFF_NORMAL, 1, 1, NULL, NULL, {0} },
{ GIT_DIFF_NORMAL, 3, 1, NULL, NULL, {0} },
{ GIT_DIFF_REVERSE, 2, 1, NULL, NULL, {0} },
{ GIT_DIFF_FORCE_TEXT, 2, 1, NULL, NULL, {0} },
/* c vs d tests */
{ GIT_DIFF_NORMAL, 3, 1, NULL, NULL, {0} },
{ GIT_DIFF_IGNORE_WHITESPACE, 3, 1, NULL, NULL, {0} },
{ GIT_DIFF_IGNORE_WHITESPACE_CHANGE, 3, 1, NULL, NULL, {0} },
{ GIT_DIFF_IGNORE_WHITESPACE_EOL, 3, 1, NULL, NULL, {0} },
{ GIT_DIFF_IGNORE_WHITESPACE | GIT_DIFF_REVERSE, 1, 1, NULL, NULL, {0} },
};
/* to generate these values:
* - cd to tests/resources/attr,
* - mv .gitted .git
* - git diff [options] 6bab5c79cd5140d0 605812ab7fe421fdd
* - mv .git .gitted
*/
diff_expects test_expects[] = {
/* a vs b tests */
{ 5, 3, 0, 2, 4, 0, 0, 51, 2, 46, 3 },
{ 5, 3, 0, 2, 4, 0, 0, 53, 4, 46, 3 },
{ 5, 0, 3, 2, 4, 0, 0, 52, 3, 3, 46 },
{ 5, 3, 0, 2, 5, 0, 0, 54, 3, 48, 3 },
/* c vs d tests */
{ 1, 0, 0, 1, 1, 0, 0, 22, 9, 10, 3 },
{ 1, 0, 0, 1, 1, 0, 0, 19, 12, 7, 0 },
{ 1, 0, 0, 1, 1, 0, 0, 20, 11, 8, 1 },
{ 1, 0, 0, 1, 1, 0, 0, 20, 11, 8, 1 },
{ 1, 0, 0, 1, 1, 0, 0, 18, 11, 0, 7 },
{ 0 },
};
int i;
cl_assert(a);
cl_assert(b);
for (i = 0; test_expects[i].files > 0; i++) {
memset(&exp, 0, sizeof(exp)); /* clear accumulator */
opts = test_options[i];
if (test_ab_or_cd[i] == 0)
cl_git_pass(git_diff_tree_to_tree(g_repo, &opts, a, b, &diff));
else
cl_git_pass(git_diff_tree_to_tree(g_repo, &opts, c, d, &diff));
cl_git_pass(git_diff_foreach(
diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn));
cl_assert(exp.files == test_expects[i].files);
cl_assert(exp.file_adds == test_expects[i].file_adds);
cl_assert(exp.file_dels == test_expects[i].file_dels);
cl_assert(exp.file_mods == test_expects[i].file_mods);
cl_assert(exp.hunks == test_expects[i].hunks);
cl_assert(exp.lines == test_expects[i].lines);
cl_assert(exp.line_ctxt == test_expects[i].line_ctxt);
cl_assert(exp.line_adds == test_expects[i].line_adds);
cl_assert(exp.line_dels == test_expects[i].line_dels);
git_diff_list_free(diff);
diff = NULL;
}
git_tree_free(a);
git_tree_free(b);
git_tree_free(c);
git_tree_free(d);
}

Binary file not shown.

Binary file not shown.