Skip to content

Commit 45d2f75

Browse files
rscharfegitster
authored andcommitted
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>
1 parent 0202c41 commit 45d2f75

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

t/t4051-diff-function-context.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ test_expect_success 'setup' '
6666
mv file.c.new file.c &&
6767
commit_and_tag long_common_tail file.c &&
6868
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+
6978
git checkout initial &&
7079
grep -v "delete me from hello" <file.c >file.c.new &&
7180
mv file.c.new file.c &&
@@ -179,4 +188,20 @@ test_expect_success ' context does not include other functions' '
179188
test $(grep -c "^[ +-].*Begin" changed_hello_appended.diff) -le 2
180189
'
181190

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+
182207
test_done

xdiff/xemit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
248248
if (xche->next) {
249249
long l = XDL_MIN(xche->next->i1,
250250
xe->xdf1.nrec - 1);
251-
if (l <= e1 ||
251+
if (l - xecfg->ctxlen <= e1 ||
252252
get_func_line(xe, xecfg, NULL, l, e1) < 0) {
253253
xche = xche->next;
254254
goto post_context_calculation;

0 commit comments

Comments
 (0)