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

[PATCH 2/2] grep -O: allow optional argument specifying the pager (or editor)

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Jun 5, 2010, 00:55 UTC
Message-ID
<20100605005552.GC8774@progeny.tock>
In-Reply-To
<20100605005116.GA8774@progeny.tock>
From: Johannes Schindelin <johannes.schindelin@gmx.de>

Suppose you want to edit all files that contain a specific search term. Of course, you can do something totally trivial such as

	git grep -z -e <term> | xargs -0r vi +/<term>
but maybe you are happy that the same will be achieved by
	git grep -Ovi <term>
now.
[jn: rebased and added tests]
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
Things I didn’t think hard about:
 - the exit status
 - the special-casing of "less" and "vi"

Maybe the latter could be improved by also accepting "more" and "vim", which both will accept the +/<term> option.

Thanks for reading.
 Documentation/git-grep.txt |    6 +++---
 builtin/grep.c             |   32 +++++++++++++++-----------------
 t/t7811-grep-open.sh       |   11 +++++++----
 3 files changed, 25 insertions(+), 24 deletions(-)
diff --git a/Documentation/git-grep.txt b/Documentation/git-grep.txt
index 8fdd8e1..d89ec32 100644
--- a/Documentation/git-grep.txt
+++ b/Documentation/git-grep.txt
@@ -14,7 +14,7 @@ SYNOPSIS
 	   [-E | --extended-regexp] [-G | --basic-regexp]
 	   [-F | --fixed-strings] [-n]
 	   [-l | --files-with-matches] [-L | --files-without-match]
-	   [-O | --open-files-in-pager]
+	   [(-O | --open-files-in-pager) [<pager>]]
 	   [-z | --null]
 	   [-c | --count] [--all-match] [-q | --quiet]
 	   [--max-depth <depth>]
@@ -105,8 +105,8 @@ OPTIONS
 	For better compatibility with 'git diff', `--name-only` is a
 	synonym for `--files-with-matches`.
 
--O::
---open-files-in-pager::
+-O [<pager>]::
+--open-files-in-pager [<pager>]::
 	Open the matching files in the pager (not the output of 'grep').
 	If the pager happens to be "less" or "vi", and the user
 	specified only one pattern, the first file is positioned at
diff --git a/builtin/grep.c b/builtin/grep.c
index bc6c2ea..f9551a7 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -584,8 +584,8 @@ static void run_pager(struct grep_opt *opt, const char *prefix)
 		exit(status);
 }
 
-static int grep_cache(struct grep_opt *opt, const char **paths,
-		      int cached, int show_in_pager, const char *prefix)
+static int grep_cache(struct grep_opt *opt, const char **paths, int cached,
+		      const char *show_in_pager, const char *prefix)
 {
 	int hit = 0;
 	int nr;
@@ -707,7 +707,7 @@ static int grep_object(struct grep_opt *opt, const char **paths,
 }
 
 static int grep_directory(struct grep_opt *opt, const char **paths,
-			  int show_in_pager, const char *prefix)
+			  const char *show_in_pager, const char *prefix)
 {
 	struct dir_struct dir;
 	int i, hit = 0;
@@ -816,7 +816,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 	int cached = 0;
 	int seen_dashdash = 0;
 	int external_grep_allowed__ignored;
-	int show_in_pager = 0;
+	const char *show_in_pager = NULL, *default_pager = "dummy";
 	struct grep_opt opt;
 	struct object_array list = { 0, 0, NULL };
 	const char **paths = NULL;
@@ -904,8 +904,9 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 		OPT_BOOLEAN(0, "all-match", &opt.all_match,
 			"show only matches from files that match all patterns"),
 		OPT_GROUP(""),
-		OPT_BOOLEAN('O', "open-files-in-pager", &show_in_pager,
-			"show matching files in the pager"),
+		{ OPTION_STRING, 'O', "open-files-in-pager", &show_in_pager,
+			"pager", "show matching files in the pager",
+			PARSE_OPT_OPTARG, NULL, (intptr_t)default_pager },
 		OPT_BOOLEAN(0, "ext-grep", &external_grep_allowed__ignored,
 			    "allow calling of grep(1) (ignored by this build)"),
 		{ OPTION_CALLBACK, 0, "help-all", &options, NULL, "show usage",
@@ -981,18 +982,15 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 		argc--;
 	}
 
+	if (show_in_pager == default_pager)
+		show_in_pager = git_pager(1);
 	if (show_in_pager) {
-		const char *pager = git_pager(1);
-		if (!pager) {
-			show_in_pager = 0;
-		} else {
-			opt.name_only = 1;
-			opt.null_following_name = 1;
-			opt.output_priv = &path_list;
-			opt.output = append_path;
-			string_list_append(pager, &path_list);
-			use_threads = 0;
-		}
+		opt.name_only = 1;
+		opt.null_following_name = 1;
+		opt.output_priv = &path_list;
+		opt.output = append_path;
+		string_list_append(show_in_pager, &path_list);
+		use_threads = 0;
 	}
 
 	if (!opt.pattern_list)
diff --git a/t/t7811-grep-open.sh b/t/t7811-grep-open.sh
index 72e4023..fdf45b8 100644
--- a/t/t7811-grep-open.sh
+++ b/t/t7811-grep-open.sh
@@ -97,9 +97,13 @@ test_expect_success 'git grep -O jumps to line in less' '
 	EOF
 	>empty &&
 
-	GIT_PAGER=./less git grep -O GREP_PATTERN >out
+	GIT_PAGER=./less git grep -O GREP_PATTERN >out1
 	test_cmp expect actual &&
-	test_cmp empty out
+	test_cmp empty out1 &&
+
+	git grep -O./less GREP_PATTERN >out2 &&
+	test_cmp expect actual &&
+	test_cmp empty out2
 '
 
 test_expect_success 'modified file' '
@@ -135,8 +139,7 @@ test_expect_success 'run from subdir' '
 		export GIT_PAGER &&
 		GIT_PAGER='\''printf "%s\n" >../args'\'' &&
 		git grep -O "enum grep_pat_token" >../out
-		GIT_PAGER="pwd >../dir; :" &&
-		git grep -O "enum grep_pat_token" >../out2
+		git grep -O"pwd >../dir; :" "enum grep_pat_token" >../out2
 	) &&
 	case $(cat dir) in
 	*subdir)
-- 
1.7.1
Previous: Jonathan NiederNext: Johannes Schindelin
Message 4 of 20 in “Teach 'git grep' about --open-files-in-pager=[<pager>]”
  1. 0/2 Teach 'git grep' about --open-files-in-pager=[<pager>]Jonathan Nieder, Jun 5, 2010
  2. 1/2 grep: Add the option '--open-files-in-pager'Jonathan Nieder, Jun 5, 2010
  3. Jonathan NiederJun 5, 2010
  4. 2/2 grep -O: allow optional argument specifying the pager (or editor)Jonathan Nieder, Jun 5, 2010
  5. Johannes SchindelinJun 5, 2010
  6. Jonathan NiederJun 12, 2010
  7. Johannes SchindelinJun 12, 2010
  8. 0/4 Teach 'git grep' about --open-files-in-pager=[<pager>]Jonathan Nieder, Jun 12, 2010
  9. 1/4 grep: refactor grep_objects loop into its own functionJonathan Nieder, Jun 12, 2010
  10. 2/4 Unify code paths of threaded grepsJonathan Nieder, Jun 12, 2010
  11. 3/4 grep: Add the option '--open-files-in-pager'Jonathan Nieder, Jun 12, 2010
  12. 4/4 grep -O: allow optional argument specifying the pager (or editor)Jonathan Nieder, Jun 12, 2010
  13. Paolo BonziniJun 12, 2010
  14. Junio C HamanoJun 13, 2010
  15. Paolo BonziniJun 13, 2010
  16. Jonathan NiederJun 14, 2010
  17. Johannes SchindelinJun 12, 2010
  18. Jonathan NiederJun 12, 2010
  19. Paolo BonziniJun 12, 2010
  20. Jonathan NiederJun 12, 2010

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.