git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "René Scharfe" <rene.scharfe@lsrfire.ath.cx>
To: Git Mailing List <git@vger.kernel.org>
Cc: Junio C Hamano <gitster@pobox.com>
Subject: [PATCH 3/6] grep: print context hunk marks between files
Date: Thu, 02 Jul 2009 00:03:44 +0200	[thread overview]
Message-ID: <4A4BDD40.5040302@lsrfire.ath.cx> (raw)
In-Reply-To: <4A4BDC65.80504@lsrfire.ath.cx>

Print a hunk mark before matches from a new file are shown, in addition
to the current behaviour of printing them if lines have been skipped.

The result is easier to read, as (presumably unrelated) matches from
different files are separated by a hunk mark.  GNU grep does the same.

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
---
 builtin-grep.c  |   11 +++++++++++
 grep.c          |    7 ++++++-
 grep.h          |    1 +
 t/t7002-grep.sh |   22 ++++++++++++++++++++++
 4 files changed, 40 insertions(+), 1 deletions(-)

diff --git a/builtin-grep.c b/builtin-grep.c
index 73fc922..48998af 100644
--- a/builtin-grep.c
+++ b/builtin-grep.c
@@ -278,6 +278,17 @@ static int flush_grep(struct grep_opt *opt,
 		argc -= 2;
 	}
 
+	if (opt->pre_context || opt->post_context) {
+		/*
+		 * grep handles hunk marks between files, but we need to
+		 * do that ourselves between multiple calls.
+		 */
+		if (opt->show_hunk_mark)
+			write_or_die(1, "--\n", 3);
+		else
+			opt->show_hunk_mark = 1;
+	}
+
 	status = exec_grep(argc, argv);
 
 	if (kept_0) {
diff --git a/grep.c b/grep.c
index 6ee80f7..4bca759 100644
--- a/grep.c
+++ b/grep.c
@@ -491,7 +491,12 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
 	int rest = eol - bol;
 
 	if (opt->pre_context || opt->post_context) {
-		if (opt->last_shown && lno > opt->last_shown + 1)
+		if (opt->last_shown == 0) {
+			if (opt->show_hunk_mark)
+				fputs("--\n", stdout);
+			else
+				opt->show_hunk_mark = 1;
+		} else if (lno > opt->last_shown + 1)
 			fputs("--\n", stdout);
 	}
 	opt->last_shown = lno;
diff --git a/grep.h b/grep.h
index 0883214..730ffd6 100644
--- a/grep.h
+++ b/grep.h
@@ -85,6 +85,7 @@ struct grep_opt {
 	unsigned pre_context;
 	unsigned post_context;
 	unsigned last_shown;
+	int show_hunk_mark;
 };
 
 extern void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t);
diff --git a/t/t7002-grep.sh b/t/t7002-grep.sh
index 7868af8..155bfdb 100755
--- a/t/t7002-grep.sh
+++ b/t/t7002-grep.sh
@@ -155,6 +155,28 @@ test_expect_success 'grep -e A --and --not -e B' '
 	test_cmp expected actual
 '
 
+cat >expected <<EOF
+y:y yy
+--
+z:zzz
+EOF
+
+# Create 1024 file names that sort between "y" and "z" to make sure
+# the two files are handled by different calls to an external grep.
+# This depends on MAXARGS in builtin-grep.c being 1024 or less.
+c32="0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v"
+test_expect_success 'grep -C1, hunk mark between files' '
+	for a in $c32; do for b in $c32; do : >y-$a$b; done; done &&
+	git add y-?? &&
+	git grep -C1 "^[yz]" >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'grep -C1 --no-ext-grep, hunk mark between files' '
+	git grep -C1 --no-ext-grep "^[yz]" >actual &&
+	test_cmp expected actual
+'
+
 test_expect_success 'log grep setup' '
 	echo a >>file &&
 	test_tick &&
-- 
1.6.3.3

  parent reply	other threads:[~2009-07-01 22:04 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-01 22:00 [PATCH 0/6] grep: add option -p/--show-function, similar to diff's René Scharfe
2009-07-01 22:01 ` [PATCH 1/6] userdiff: add xdiff_clear_find_func() René Scharfe
2009-07-01 22:02 ` [PATCH 2/6] grep: move context hunk mark handling into show_line() René Scharfe
2009-07-01 22:55   ` Junio C Hamano
2009-07-02  3:15     ` René Scharfe
2009-07-02  5:24       ` Junio C Hamano
2009-07-01 22:03 ` René Scharfe [this message]
2009-07-01 22:05 ` [PATCH 4/6] grep: handle pre context lines on demand René Scharfe
2009-07-01 22:06 ` [PATCH 5/6] grep: add option -p/--show-function René Scharfe
2009-07-02  2:35   ` Junio C Hamano
2009-07-02  4:38     ` René Scharfe
2009-07-02  5:27       ` Junio C Hamano
2009-07-02  6:16         ` René Scharfe
2009-07-02 15:42           ` René Scharfe
2009-07-01 22:07 ` [PATCH 6/6] grep -p: support user defined regular expressions René Scharfe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4A4BDD40.5040302@lsrfire.ath.cx \
    --to=rene.scharfe@lsrfire.ath.cx \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).