{"thread":{"id":"44084","subject":"[PATCH] xdiff: fix merging of hunks with -W context and -u context","startedAt":"2016-09-14T16:05:52Z","lastAt":"2016-09-14T16:05:52Z","messageCount":1,"participants":["René Scharfe"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"301911","messageId":"a6f5a3cb-74d3-6072-a611-8dd63b902057@web.de","threadId":"44084","inReplyTo":null,"subject":"[PATCH] xdiff: fix merging of hunks with -W context and -u context","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2016-09-14T16:05:27Z","receivedAt":"2016-09-14T16:05:52Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"If the function context for a hunk (with -W) reaches the beginning of\nthe next hunk then we need to merge these two -- otherwise we'd show\nsome lines twice, which looks strange and even confuses git apply.  We\nalready do this checking and merging in xdl_emit_diff(), but forget to\nconsider regular context (with -u or -U).\n\nFix that by merging hunks already if function context of the first one\ntouches or overlaps regular context of the second one.\n\nSigned-off-by: Rene Scharfe <l.s.r@web.de>\n---\n t/t4051-diff-function-context.sh | 25 +++++++++++++++++++++++++\n xdiff/xemit.c                    |  2 +-\n 2 files changed, 26 insertions(+), 1 deletion(-)\n\ndiff --git a/t/t4051-diff-function-context.sh b/t/t4051-diff-function-context.sh\nindex b79b877..6154acb 100755\n--- a/t/t4051-diff-function-context.sh\n+++ b/t/t4051-diff-function-context.sh\n@@ -67,6 +67,15 @@ test_expect_success 'setup' '\n \tcommit_and_tag long_common_tail file.c &&\n \n \tgit checkout initial &&\n+\tcat \"$dir/hello.c\" \"$dir/dummy.c\" >file.c &&\n+\tcommit_and_tag hello_dummy file.c &&\n+\n+\t# overlap function context of 1st change and -u context of 2nd change\n+\tgrep -v \"delete me from hello\" <\"$dir/hello.c\" >file.c &&\n+\tsed 2p <\"$dir/dummy.c\" >>file.c &&\n+\tcommit_and_tag changed_hello_dummy file.c &&\n+\n+\tgit checkout initial &&\n \tgrep -v \"delete me from hello\" <file.c >file.c.new &&\n \tmv file.c.new file.c &&\n \tcat \"$dir/appended1.c\" >>file.c &&\n@@ -179,4 +188,20 @@ test_expect_success ' context does not include other functions' '\n \ttest $(grep -c \"^[ +-].*Begin\" changed_hello_appended.diff) -le 2\n '\n \n+check_diff changed_hello_dummy 'changed two consecutive functions'\n+\n+test_expect_success ' context includes begin' '\n+\tgrep \"^ .*Begin of hello\" changed_hello_dummy.diff &&\n+\tgrep \"^ .*Begin of dummy\" changed_hello_dummy.diff\n+'\n+\n+test_expect_success ' context includes end' '\n+\tgrep \"^ .*End of hello\" changed_hello_dummy.diff &&\n+\tgrep \"^ .*End of dummy\" changed_hello_dummy.diff\n+'\n+\n+test_expect_success ' overlapping hunks are merged' '\n+\ttest $(grep -c \"^@@\" changed_hello_dummy.diff) -eq 1\n+'\n+\n test_done\ndiff --git a/xdiff/xemit.c b/xdiff/xemit.c\nindex b52b4b9..7389ce4 100644\n--- a/xdiff/xemit.c\n+++ b/xdiff/xemit.c\n@@ -239,7 +239,7 @@ int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,\n \t\t\tif (xche->next) {\n \t\t\t\tlong l = XDL_MIN(xche->next->i1,\n \t\t\t\t\t\t xe->xdf1.nrec - 1);\n-\t\t\t\tif (l <= e1 ||\n+\t\t\t\tif (l - xecfg->ctxlen <= e1 ||\n \t\t\t\t    get_func_line(xe, xecfg, NULL, l, e1) < 0) {\n \t\t\t\t\txche = xche->next;\n \t\t\t\t\tgoto post_context_calculation;\n-- \n2.10.0\n\n"}]}