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

[PATCH 3/5] diff: ignore empty lines before added functions with -W

From
René Scharfe <l.s.r@web.de>
Date
May 21, 2016, 18:46 UTC
Message-ID
<5740ACF5.4060107@web.de>
In-Reply-To
<5740AC28.6010202@web.de>

If a new function and a preceding empty line is appended, diff -W shows the previous function in full in order to provide context for that empty line. In most languages empty lines between sections are not interesting in and off themselves and showing a whole extra function for them is not what we want.

Skip empty lines when checking of the appended chunk starts with a function line, thereby avoiding to extend the context just for them.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
 xdiff/xemit.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)
diff --git a/xdiff/xemit.c b/xdiff/xemit.c
index 969100d..087956a 100644
--- a/xdiff/xemit.c
+++ b/xdiff/xemit.c
@@ -155,6 +155,18 @@ static long get_func_line(xdfenv_t *xe, xdemitconf_t const *xecfg,
 	return -1;
 }
 
+static int is_empty_rec(xdfile_t *xdf, xdemitconf_t const *xecfg, long ri)
+{
+	const char *rec;
+	long len = xdl_get_rec(xdf, ri, &rec);
+
+	while (len > 0 && isspace(*rec)) {
+		rec++;
+		len--;
+	}
+	return !len;
+}
+
 int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
 		  xdemitconf_t const *xecfg) {
 	long s1, s2, e1, e2, lctx;
@@ -176,12 +188,18 @@ int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
 			/* Appended chunk? */
 			if (i1 >= xe->xdf1.nrec) {
 				char dummy[1];
+				long i2 = xch->i2;
 
 				/*
 				 * We don't need additional context if
-				 * a whole function was added.
+				 * a whole function was added, possibly
+				 * starting with empty lines.
 				 */
-				if (match_func_rec(&xe->xdf2, xecfg, xch->i2,
+				while (i2 < xe->xdf2.nrec &&
+				       is_empty_rec(&xe->xdf2, xecfg, i2))
+					i2++;
+				if (i2 < xe->xdf2.nrec &&
+				    match_func_rec(&xe->xdf2, xecfg, i2,
 						   dummy, sizeof(dummy)) >= 0)
 					goto post_context_calculation;
 
-- 
2.8.3
Previous: René ScharfeNext: René Scharfe
Message 7 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.