@matheusbsbs / matheus-bagliano / commits / 0af88c15e2
Fork 0

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 <johannes.schindelin@gmx.de> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Acked-by: Paolo Bonzini <bonzini@gnu.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Jun 12, 2010 at 11:39 UTC 0af88c15e2eb0a680c3797da8d8b97636b797f66
3 files changed +21 -20
Documentation/git-grep.txt
+3 -3
@@ -14,7 +14,7 @@ SYNOPSIS
14 [-E | --extended-regexp] [-G | --basic-regexp]
15 [-F | --fixed-strings] [-n]
16 [-l | --files-with-matches] [-L | --files-without-match]
17 - [-O | --open-files-in-pager]
17 + [(-O | --open-files-in-pager) [<pager>]]
18 [-z | --null]
19 [-c | --count] [--all-match] [-q | --quiet]
20 [--max-depth <depth>]
@@ -105,8 +105,8 @@ OPTIONS
105 For better compatibility with 'git diff', `--name-only` is a
106 synonym for `--files-with-matches`.
107
108 --O::
109 ---open-files-in-pager::
108 +-O [<pager>]::
109 +--open-files-in-pager [<pager>]::
110 Open the matching files in the pager (not the output of 'grep').
111 If the pager happens to be "less" or "vi", and the user
112 specified only one pattern, the first file is positioned at
builtin/grep.c
+12 -14
@@ -828,7 +828,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
828 int cached = 0;
829 int seen_dashdash = 0;
830 int external_grep_allowed__ignored;
831 - int show_in_pager = 0;
831 + const char *show_in_pager = NULL, *default_pager = "dummy";
832 struct grep_opt opt;
833 struct object_array list = { 0, 0, NULL };
834 const char **paths = NULL;
@@ -916,8 +916,9 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
916 OPT_BOOLEAN(0, "all-match", &opt.all_match,
917 "show only matches from files that match all patterns"),
918 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 },
922 OPT_BOOLEAN(0, "ext-grep", &external_grep_allowed__ignored,
923 "allow calling of grep(1) (ignored by this build)"),
924 { OPTION_CALLBACK, 0, "help-all", &options, NULL, "show usage",
@@ -993,18 +994,15 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
994 argc--;
995 }
996
997 + if (show_in_pager == default_pager)
998 + show_in_pager = git_pager(1);
999 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;
1006 }
1007
1008 if (!opt.pattern_list)
t/t7811-grep-open.sh
+6 -3
@@ -99,7 +99,11 @@ test_expect_success 'git grep -O jumps to line in less' '
99
100 GIT_PAGER=./less git grep -O GREP_PATTERN >out &&
101 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
107 '
108
109 test_expect_success 'modified file' '
@@ -135,8 +139,7 @@ test_expect_success 'run from subdir' '
139 export GIT_PAGER &&
140 GIT_PAGER='\''printf "%s\n" >../args'\'' &&
141 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
143 ) &&
144 case $(cat dir) in
145 *subdir)