xdiff: fix merging of hunks with -W context and -u context

If the function context for a hunk (with -W) reaches the beginning of the next hunk then we need to merge these two -- otherwise we'd show some lines twice, which looks strange and even confuses git apply. We already do this checking and merging in xdl_emit_diff(), but forget to consider regular context (with -u or -U). Fix that by merging hunks already if function context of the first one touches or overlaps regular context of the second one. Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Sep 14, 2016 at 18:05 UTC 45d2f75f913c0545be473db1dc2595a1600413f5
2 files changed +26 -1
t/t4051-diff-function-context.sh
+25
@@ -66,6 +66,15 @@ test_expect_success 'setup' '
66 mv file.c.new file.c &&
67 commit_and_tag long_common_tail file.c &&
68
69 + git checkout initial &&
70 + cat "$dir/hello.c" "$dir/dummy.c" >file.c &&
71 + commit_and_tag hello_dummy file.c &&
72 +
73 + # overlap function context of 1st change and -u context of 2nd change
74 + grep -v "delete me from hello" <"$dir/hello.c" >file.c &&
75 + sed 2p <"$dir/dummy.c" >>file.c &&
76 + commit_and_tag changed_hello_dummy file.c &&
77 +
78 git checkout initial &&
79 grep -v "delete me from hello" <file.c >file.c.new &&
80 mv file.c.new file.c &&
@@ -179,4 +188,20 @@ test_expect_success ' context does not include other functions' '
188 test $(grep -c "^[ +-].*Begin" changed_hello_appended.diff) -le 2
189 '
190
191 +check_diff changed_hello_dummy 'changed two consecutive functions'
192 +
193 +test_expect_success ' context includes begin' '
194 + grep "^ .*Begin of hello" changed_hello_dummy.diff &&
195 + grep "^ .*Begin of dummy" changed_hello_dummy.diff
196 +'
197 +
198 +test_expect_success ' context includes end' '
199 + grep "^ .*End of hello" changed_hello_dummy.diff &&
200 + grep "^ .*End of dummy" changed_hello_dummy.diff
201 +'
202 +
203 +test_expect_success ' overlapping hunks are merged' '
204 + test $(grep -c "^@@" changed_hello_dummy.diff) -eq 1
205 +'
206 +
207 test_done
xdiff/xemit.c
+1 -1
@@ -248,7 +248,7 @@ int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
248 if (xche->next) {
249 long l = XDL_MIN(xche->next->i1,
250 xe->xdf1.nrec - 1);
251 - if (l <= e1 ||
251 + if (l - xecfg->ctxlen <= e1 ||
252 get_func_line(xe, xecfg, NULL, l, e1) < 0) {
253 xche = xche->next;
254 goto post_context_calculation;