Skip to content

Commit 0af88c1

Browse files
dschogitster
authored andcommitted
grep -O: allow optional argument specifying the pager (or editor)
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 <[email protected]> Signed-off-by: Jonathan Nieder <[email protected]> Acked-by: Paolo Bonzini <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 678e484 commit 0af88c1

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

Documentation/git-grep.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ SYNOPSIS
1414
[-E | --extended-regexp] [-G | --basic-regexp]
1515
[-F | --fixed-strings] [-n]
1616
[-l | --files-with-matches] [-L | --files-without-match]
17-
[-O | --open-files-in-pager]
17+
[(-O | --open-files-in-pager) [<pager>]]
1818
[-z | --null]
1919
[-c | --count] [--all-match] [-q | --quiet]
2020
[--max-depth <depth>]
@@ -105,8 +105,8 @@ OPTIONS
105105
For better compatibility with 'git diff', `--name-only` is a
106106
synonym for `--files-with-matches`.
107107

108-
-O::
109-
--open-files-in-pager::
108+
-O [<pager>]::
109+
--open-files-in-pager [<pager>]::
110110
Open the matching files in the pager (not the output of 'grep').
111111
If the pager happens to be "less" or "vi", and the user
112112
specified only one pattern, the first file is positioned at

builtin/grep.c

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
828828
int cached = 0;
829829
int seen_dashdash = 0;
830830
int external_grep_allowed__ignored;
831-
int show_in_pager = 0;
831+
const char *show_in_pager = NULL, *default_pager = "dummy";
832832
struct grep_opt opt;
833833
struct object_array list = { 0, 0, NULL };
834834
const char **paths = NULL;
@@ -916,8 +916,9 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
916916
OPT_BOOLEAN(0, "all-match", &opt.all_match,
917917
"show only matches from files that match all patterns"),
918918
OPT_GROUP(""),
919-
OPT_BOOLEAN('O', "open-files-in-pager", &show_in_pager,
920-
"show matching files in the pager"),
919+
{ OPTION_STRING, 'O', "open-files-in-pager", &show_in_pager,
920+
"pager", "show matching files in the pager",
921+
PARSE_OPT_OPTARG, NULL, (intptr_t)default_pager },
921922
OPT_BOOLEAN(0, "ext-grep", &external_grep_allowed__ignored,
922923
"allow calling of grep(1) (ignored by this build)"),
923924
{ OPTION_CALLBACK, 0, "help-all", &options, NULL, "show usage",
@@ -993,18 +994,15 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
993994
argc--;
994995
}
995996

997+
if (show_in_pager == default_pager)
998+
show_in_pager = git_pager(1);
996999
if (show_in_pager) {
997-
const char *pager = git_pager(1);
998-
if (!pager) {
999-
show_in_pager = 0;
1000-
} else {
1001-
opt.name_only = 1;
1002-
opt.null_following_name = 1;
1003-
opt.output_priv = &path_list;
1004-
opt.output = append_path;
1005-
string_list_append(pager, &path_list);
1006-
use_threads = 0;
1007-
}
1000+
opt.name_only = 1;
1001+
opt.null_following_name = 1;
1002+
opt.output_priv = &path_list;
1003+
opt.output = append_path;
1004+
string_list_append(show_in_pager, &path_list);
1005+
use_threads = 0;
10081006
}
10091007

10101008
if (!opt.pattern_list)

t/t7811-grep-open.sh

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,11 @@ test_expect_success 'git grep -O jumps to line in less' '
9999
100100
GIT_PAGER=./less git grep -O GREP_PATTERN >out &&
101101
test_cmp expect actual &&
102-
test_cmp empty out
102+
test_cmp empty out &&
103+
104+
git grep -O./less GREP_PATTERN >out2 &&
105+
test_cmp expect actual &&
106+
test_cmp empty out2
103107
'
104108

105109
test_expect_success 'modified file' '
@@ -135,8 +139,7 @@ test_expect_success 'run from subdir' '
135139
export GIT_PAGER &&
136140
GIT_PAGER='\''printf "%s\n" >../args'\'' &&
137141
git grep -O "enum grep_pat_token" >../out &&
138-
GIT_PAGER="pwd >../dir; :" &&
139-
git grep -O "enum grep_pat_token" >../out2
142+
git grep -O"pwd >../dir; :" "enum grep_pat_token" >../out2
140143
) &&
141144
case $(cat dir) in
142145
*subdir)

0 commit comments

Comments
 (0)