git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 3/8] xdiff: handle appended chunks better with -W

From
René Scharfe <l.s.r@web.de>
Date
May 28, 2016, 15:00 UTC
Message-ID
<5749B28C.5010006@web.de>
In-Reply-To
<5749AF59.2070704@web.de>

If lines are added at the end of a file, diff -W shows the whole file. That's because get_func_line() only considers the pre-image and gives up if it sees a record index beyond its end.

Consider the post-image as well to see if the added lines already make up a full function. If it doesn't then search for the previous function line by starting from the bottom of the pre-image, thereby avoiding to confuse get_func_line().

Reuse the existing label called "again", as it's exactly where we need to jump to when we're done handling the pre-context, but rename it to "post_context_calculation" in order to document its new purpose better.

Reported-by: Junio C Hamano <gitster@pobox.com>
Initial-patch-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
 t/t4051-diff-function-context.sh |  2 +-
 xdiff/xemit.c                    | 27 ++++++++++++++++++++++++---
 2 files changed, 25 insertions(+), 4 deletions(-)
diff --git a/t/t4051-diff-function-context.sh b/t/t4051-diff-function-context.sh
index a3adba4..d78461d 100755
--- a/t/t4051-diff-function-context.sh
+++ b/t/t4051-diff-function-context.sh
@@ -131,7 +131,7 @@ test_expect_success ' context includes end' '
 	grep "^[+].*End of second part" extended.diff
 '
 
-test_expect_failure ' context does not include other functions' '
+test_expect_success ' context does not include other functions' '
 	test $(grep -c "^[ +-].*Begin" extended.diff) -le 2
 '
 
diff --git a/xdiff/xemit.c b/xdiff/xemit.c
index 0c87637..969100d 100644
--- a/xdiff/xemit.c
+++ b/xdiff/xemit.c
@@ -171,7 +171,28 @@ int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
 		s2 = XDL_MAX(xch->i2 - xecfg->ctxlen, 0);
 
 		if (xecfg->flags & XDL_EMIT_FUNCCONTEXT) {
-			long fs1 = get_func_line(xe, xecfg, NULL, xch->i1, -1);
+			long fs1, i1 = xch->i1;
+
+			/* Appended chunk? */
+			if (i1 >= xe->xdf1.nrec) {
+				char dummy[1];
+
+				/*
+				 * We don't need additional context if
+				 * a whole function was added.
+				 */
+				if (match_func_rec(&xe->xdf2, xecfg, xch->i2,
+						   dummy, sizeof(dummy)) >= 0)
+					goto post_context_calculation;
+
+				/*
+				 * Otherwise get more context from the
+				 * pre-image.
+				 */
+				i1 = xe->xdf1.nrec - 1;
+			}
+
+			fs1 = get_func_line(xe, xecfg, NULL, i1, -1);
 			if (fs1 < 0)
 				fs1 = 0;
 			if (fs1 < s1) {
@@ -180,7 +201,7 @@ int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
 			}
 		}
 
- again:
+ post_context_calculation:
 		lctx = xecfg->ctxlen;
 		lctx = XDL_MIN(lctx, xe->xdf1.nrec - (xche->i1 + xche->chg1));
 		lctx = XDL_MIN(lctx, xe->xdf2.nrec - (xche->i2 + xche->chg2));
@@ -209,7 +230,7 @@ int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
 				if (l <= e1 ||
 				    get_func_line(xe, xecfg, NULL, l, e1) < 0) {
 					xche = xche->next;
-					goto again;
+					goto post_context_calculation;
 				}
 			}
 		}
-- 
2.8.3
Previous: René ScharfeNext: René Scharfe
Message 22 of 27 in “[Bug?] log -p -W showing the whole file for a patch that adds to the end?”
  1. Junio C HamanoMay 10, 2016
  2. Junio C HamanoMay 10, 2016
  3. René ScharfeMay 12, 2016
  4. René ScharfeMay 21, 2016
  5. 1/5 diff: factor out match_func_rec()René Scharfe, May 21, 2016
  6. 2/5 diff: handle appended chunks better with -WRené Scharfe, May 21, 2016
  7. 3/5 diff: ignore empty lines before added functions with -WRené Scharfe, May 21, 2016
  8. 4/5 diff: don't include common trailing empty lines with -WRené Scharfe, May 21, 2016
  9. 5/5 grep: don't extend context to trailing empty lines with -WRené Scharfe, May 21, 2016
  10. Junio C HamanoMay 24, 2016
  11. René ScharfeMay 26, 2016
  12. Junio C HamanoMay 26, 2016
  13. René ScharfeMay 27, 2016
  14. René ScharfeMay 28, 2016
  15. 1/8 t4051: rewrite, add more testsRené Scharfe, May 28, 2016
  16. Junio C HamanoMay 29, 2016
  17. René ScharfeMay 30, 2016
  18. 1/8 t4051: rewrite, add more testsRené Scharfe, May 31, 2016
  19. René ScharfeMay 31, 2016
  20. Junio C HamanoMay 31, 2016
  21. 2/8 xdiff: factor out match_func_rec()René Scharfe, May 28, 2016
  22. 3/8 xdiff: handle appended chunks better with -WRené Scharfe, May 28, 2016
  23. 4/8 xdiff: ignore empty lines before added functions with -WRené Scharfe, May 28, 2016
  24. 5/8 xdiff: -W: don't include common trailing empty lines in contextRené Scharfe, May 28, 2016
  25. 6/8 xdiff: don't trim common tail with -WRené Scharfe, May 28, 2016
  26. 7/8 t7810: add test for grep -W and trailing empty context linesRené Scharfe, May 28, 2016
  27. 8/8 grep: -W: don't extend context to trailing empty linesRené Scharfe, May 28, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.