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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
diff --git a/src/diff.c b/src/diff.c
index 9a12aa0..197fe35 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -486,9 +486,11 @@ static int set_file_is_binary(
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(param, 0, sizeof(xpparam_t));
cfg->ctxlen =
(!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;
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)
- cfg->flags |= XDF_IGNORE_WHITESPACE_CHANGE;
+ param->flags |= XDF_IGNORE_WHITESPACE_CHANGE;
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(
@@ -525,8 +527,7 @@ int git_diff_foreach(
di.hunk_cb = hunk_cb;
di.line_cb = line_cb;
- memset(&xdiff_params, 0, sizeof(xdiff_params));
- setup_xdiff_config(&diff->opts, &xdiff_config);
+ setup_xdiff_options(&diff->opts, &xdiff_config, &xdiff_params);
memset(&xdiff_callback, 0, sizeof(xdiff_callback));
xdiff_callback.outf = diff_output_cb;
xdiff_callback.priv = &di;
@@ -898,8 +899,7 @@ int git_diff_blobs(
di.hunk_cb = hunk_cb;
di.line_cb = line_cb;
- memset(&xdiff_params, 0, sizeof(xdiff_params));
- setup_xdiff_config(options, &xdiff_config);
+ setup_xdiff_options(options, &xdiff_config, &xdiff_params);
memset(&xdiff_callback, 0, sizeof(xdiff_callback));
xdiff_callback.outf = diff_output_cb;
xdiff_callback.priv = &di;
diff --git a/tests-clar/diff/diff_helpers.c b/tests-clar/diff/diff_helpers.c
index b32c4bc..80c6480 100644
--- a/tests-clar/diff/diff_helpers.c
+++ b/tests-clar/diff/diff_helpers.c
@@ -72,9 +72,11 @@ int diff_line_fn(
e->line_ctxt++;
break;
case GIT_DIFF_LINE_ADDITION:
+ case GIT_DIFF_LINE_ADD_EOFNL:
e->line_adds++;
break;
case GIT_DIFF_LINE_DELETION:
+ case GIT_DIFF_LINE_DEL_EOFNL:
e->line_dels++;
break;
default:
diff --git a/tests-clar/diff/tree.c b/tests-clar/diff/tree.c
index 836db57..eb4092a 100644
--- a/tests-clar/diff/tree.c
+++ b/tests-clar/diff/tree.c
@@ -84,3 +84,90 @@ void test_diff_tree__0(void)
git_tree_free(b);
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);
+}
diff --git a/tests/resources/attr/.gitted/index b/tests/resources/attr/.gitted/index
index f35d300..19fa99d 100644
Binary files a/tests/resources/attr/.gitted/index and b/tests/resources/attr/.gitted/index differ
diff --git a/tests/resources/attr/.gitted/logs/HEAD b/tests/resources/attr/.gitted/logs/HEAD
index 6d09635..68fcff2 100644
--- a/tests/resources/attr/.gitted/logs/HEAD
+++ b/tests/resources/attr/.gitted/logs/HEAD
@@ -3,3 +3,4 @@
605812ab7fe421fdd325a935d35cb06a9234a7d7 a5d76cad53f66f1312bd995909a5bab3c0820770 Russell Belfer <arrbee@arrbee.com> 1325281762 -0800 commit: more macro tests
a5d76cad53f66f1312bd995909a5bab3c0820770 370fe9ec224ce33e71f9e5ec2bd1142ce9937a6a Russell Belfer <arrbee@arrbee.com> 1327611749 -0800 commit: Updating files so we can do diffs
370fe9ec224ce33e71f9e5ec2bd1142ce9937a6a f5b0af1fb4f5c0cd7aad880711d368a07333c307 Russell Belfer <arrbee@arrbee.com> 1327621027 -0800 commit: Updating test data
+f5b0af1fb4f5c0cd7aad880711d368a07333c307 a97cc019851d401a4f1d091cb91a15890a0dd1ba Russell Belfer <arrbee@arrbee.com> 1328653313 -0800 commit: Some whitespace only changes for testing purposes
diff --git a/tests/resources/attr/.gitted/logs/refs/heads/master b/tests/resources/attr/.gitted/logs/refs/heads/master
index 6d09635..68fcff2 100644
--- a/tests/resources/attr/.gitted/logs/refs/heads/master
+++ b/tests/resources/attr/.gitted/logs/refs/heads/master
@@ -3,3 +3,4 @@
605812ab7fe421fdd325a935d35cb06a9234a7d7 a5d76cad53f66f1312bd995909a5bab3c0820770 Russell Belfer <arrbee@arrbee.com> 1325281762 -0800 commit: more macro tests
a5d76cad53f66f1312bd995909a5bab3c0820770 370fe9ec224ce33e71f9e5ec2bd1142ce9937a6a Russell Belfer <arrbee@arrbee.com> 1327611749 -0800 commit: Updating files so we can do diffs
370fe9ec224ce33e71f9e5ec2bd1142ce9937a6a f5b0af1fb4f5c0cd7aad880711d368a07333c307 Russell Belfer <arrbee@arrbee.com> 1327621027 -0800 commit: Updating test data
+f5b0af1fb4f5c0cd7aad880711d368a07333c307 a97cc019851d401a4f1d091cb91a15890a0dd1ba Russell Belfer <arrbee@arrbee.com> 1328653313 -0800 commit: Some whitespace only changes for testing purposes
diff --git a/tests/resources/attr/.gitted/objects/10/8bb4e7fd7b16490dc33ff7d972151e73d7166e b/tests/resources/attr/.gitted/objects/10/8bb4e7fd7b16490dc33ff7d972151e73d7166e
new file mode 100644
index 0000000..edcf752
Binary files /dev/null and b/tests/resources/attr/.gitted/objects/10/8bb4e7fd7b16490dc33ff7d972151e73d7166e differ
diff --git a/tests/resources/attr/.gitted/objects/6d/968d62c89c7d9ea23a4c9a7b665d017c3d8ffd b/tests/resources/attr/.gitted/objects/6d/968d62c89c7d9ea23a4c9a7b665d017c3d8ffd
new file mode 100644
index 0000000..e832241
Binary files /dev/null and b/tests/resources/attr/.gitted/objects/6d/968d62c89c7d9ea23a4c9a7b665d017c3d8ffd differ
diff --git a/tests/resources/attr/.gitted/objects/a9/7cc019851d401a4f1d091cb91a15890a0dd1ba b/tests/resources/attr/.gitted/objects/a9/7cc019851d401a4f1d091cb91a15890a0dd1ba
new file mode 100644
index 0000000..1a7ec0c
--- /dev/null
+++ b/tests/resources/attr/.gitted/objects/a9/7cc019851d401a4f1d091cb91a15890a0dd1ba
@@ -0,0 +1,2 @@
+xQj0DS[hc;PJ( $qޠ_3oIK+BtI|LgƈŐR4'=qFN64
+J1FrzW[rV6-i7.eVW;X,
mwl|]ṬMɢdRwC[W9sj~Wy
\ No newline at end of file
diff --git a/tests/resources/attr/.gitted/refs/heads/master b/tests/resources/attr/.gitted/refs/heads/master
index 9eca424..7f8bbe3 100644
--- a/tests/resources/attr/.gitted/refs/heads/master
+++ b/tests/resources/attr/.gitted/refs/heads/master
@@ -1 +1 @@
-f5b0af1fb4f5c0cd7aad880711d368a07333c307
+a97cc019851d401a4f1d091cb91a15890a0dd1ba
diff --git a/tests/resources/attr/root_test3 b/tests/resources/attr/root_test3
index c96bbb2..108bb4e 100644
--- a/tests/resources/attr/root_test3
+++ b/tests/resources/attr/root_test3
@@ -1,12 +1,19 @@
Some additional lines
-Down here below the other lines
+
+ Down here below the other lines
+
With even more at the end
-And lots of good stuff
-Anywhere you want
+And lots of good stuff
+
+
+Anywhere you want
+
Don't you think
+
+