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

[PATCH 3/4] grep: move code to print hunk markers after heading

From
Mark Lodato <lodatom@gmail.com>
Date
Mar 26, 2012, 02:41 UTC
Message-ID
<1332729705-9283-4-git-send-email-lodatom@gmail.com>
In-Reply-To
<1332729705-9283-1-git-send-email-lodatom@gmail.com>

This commit does not have any effect on the output of the program. The purpose is to distinguish in the code between two kinds of hunk separators: those that occur before the heading and those that occur after. This step is necessary for the next commit, which will modify only the latter tyep of hunk separators.

Signed-off-by: Mark Lodato <lodatom@gmail.com>
---
 grep.c |   25 +++++++++++++------------
 1 file changed, 13 insertions(+), 12 deletions(-)
diff --git a/grep.c b/grep.c
index 190139c..14e0480 100644
--- a/grep.c
+++ b/grep.c
@@ -739,25 +739,26 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
 {
 	int rest = eol - bol;
 	char *line_color = NULL;
+	int show_hunk = (opt->pre_context || opt->post_context ||
+			 opt->funcbody);
 
-	if (opt->file_break && opt->last_shown == 0) {
-		if (opt->show_hunk_mark)
-			opt->output(opt, "\n", 1);
-	} else if (opt->pre_context || opt->post_context || opt->funcbody) {
-		if (opt->last_shown == 0) {
-			if (opt->show_hunk_mark) {
-				output_color(opt, "--", 2, opt->color_sep);
-				opt->output(opt, "\n", 1);
-			}
-		} else if (lno > opt->last_shown + 1) {
+	if (opt->show_hunk_mark && opt->last_shown == 0 &&
+	    (opt->file_break || (opt->heading && show_hunk))) {
+		if (opt->heading && !opt->file_break)
 			output_color(opt, "--", 2, opt->color_sep);
-			opt->output(opt, "\n", 1);
-		}
+		opt->output(opt, "\n", 1);
+		show_hunk = 0;
 	}
 	if (opt->heading && opt->last_shown == 0) {
 		output_color(opt, name, strlen(name), opt->color_filename);
 		opt->output(opt, "\n", 1);
 	}
+	if (show_hunk &&
+	    ((opt->last_shown == 0 && opt->show_hunk_mark) ||
+	     (opt->last_shown != 0 && lno > opt->last_shown + 1))) {
+		output_color(opt, "--", 2, opt->color_sep);
+		opt->output(opt, "\n", 1);
+	}
 	opt->last_shown = lno;
 
 	if (!opt->heading && opt->pathname) {
-- 
1.7.9.2
Previous: Mark LodatoNext: Mark Lodato
Message 4 of 15 in “grep: add more information to hunk separators”
  1. 0/4 grep: add more information to hunk separatorsMark Lodato, Mar 26, 2012
  2. 1/4 grep doc: add --break / --heading / -W to synopsisMark Lodato, Mar 26, 2012
  3. 2/4 add tests for grep --heading with contextMark Lodato, Mar 26, 2012
  4. 3/4 grep: move code to print hunk markers after headingMark Lodato, Mar 26, 2012
  5. 4/4 grep: add --hunk-heading optionMark Lodato, Mar 26, 2012
  6. Junio C HamanoMar 26, 2012
  7. René ScharfeMar 26, 2012
  8. Junio C HamanoMar 26, 2012
  9. Bert WesargMar 26, 2012
  10. René ScharfeMar 26, 2012
  11. Junio C HamanoMar 26, 2012
  12. René ScharfeMar 26, 2012
  13. Junio C HamanoMar 26, 2012
  14. 5/4 move sane_truncate_line to utf8_truncate_lineMark Lodato, Mar 27, 2012
  15. 6/4 add grep.hunkHeadingFunction optionMark Lodato, Mar 27, 2012

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.