Skip to content

Commit 4ed3863

Browse files
committed
Merge branch 'rs/xdiff-merge-overlapping-hunks-for-W-context'
"git diff -W" output needs to extend the context backward to include the header line of the current function and also forward to include the body of the entire current function up to the header line of the next one. This process may have to merge to adjacent hunks, but the code forgot to do so in some cases. * rs/xdiff-merge-overlapping-hunks-for-W-context: xdiff: fix merging of hunks with -W context and -u context
2 parents e9c6d3d + 45d2f75 commit 4ed3863

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
@@ -239,7 +239,7 @@ int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
239239
if (xche->next) {
240240
long l = XDL_MIN(xche->next->i1,
241241
xe->xdf1.nrec - 1);
242-
if (l <= e1 ||
242+
if (l - xecfg->ctxlen <= e1 ||
243243
get_func_line(xe, xecfg, NULL, l, e1) < 0) {
244244
xche = xche->next;
245245
goto post_context_calculation;

0 commit comments

Comments
 (0)