@matheusbsbs / matheus-bagliano / commits / 678e484b7d
Fork 0

grep: Add the option '--open-files-in-pager'

This adds an option to open the matching files in the pager, and if the pager happens to be "less" (or "vi") and there is only one grep pattern, it also jumps to the first match right away. The short option was chose as '-O' to avoid clashes with GNU grep's options (as suggested by Junio). So, 'git grep -O abc' is a short form for 'less +/abc $(grep -l abc)' except that it works also with spaces in file names, and it does not start the pager if there was no matching file. [jn: rebased and added tests; with error handling fix from Junio squashed in] Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Jun 12, 2010 at 11:36 UTC 678e484b7d4e6388edeec3470bbbcd206817c148
7 files changed +255 -13
Documentation/git-grep.txt
+8
@@ -14,6 +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]
18 [-z | --null]
19 [-c | --count] [--all-match] [-q | --quiet]
20 [--max-depth <depth>]
@@ -104,6 +105,13 @@ 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::
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
113 + the first match automatically.
114 +
115 -z::
116 --null::
117 Output \0 instead of the character that normally follows a
builtin/grep.c
+73
@@ -11,6 +11,8 @@
11 #include "tree-walk.h"
12 #include "builtin.h"
13 #include "parse-options.h"
14 +#include "string-list.h"
15 +#include "run-command.h"
16 #include "userdiff.h"
17 #include "grep.h"
18 #include "quote.h"
@@ -556,6 +558,33 @@ static int grep_file(struct grep_opt *opt, const char *filename)
558 }
559 }
560
561 +static void append_path(struct grep_opt *opt, const void *data, size_t len)
562 +{
563 + struct string_list *path_list = opt->output_priv;
564 +
565 + if (len == 1 && *(const char *)data == '\0')
566 + return;
567 + string_list_append(xstrndup(data, len), path_list);
568 +}
569 +
570 +static void run_pager(struct grep_opt *opt, const char *prefix)
571 +{
572 + struct string_list *path_list = opt->output_priv;
573 + const char **argv = xmalloc(sizeof(const char *) * (path_list->nr + 1));
574 + int i, status;
575 +
576 + for (i = 0; i < path_list->nr; i++)
577 + argv[i] = path_list->items[i].string;
578 + argv[path_list->nr] = NULL;
579 +
580 + if (prefix && chdir(prefix))
581 + die("Failed to chdir: %s", prefix);
582 + status = run_command_v_opt(argv, RUN_USING_SHELL);
583 + if (status)
584 + exit(status);
585 + free(argv);
586 +}
587 +
588 static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
589 {
590 int hit = 0;
@@ -799,9 +828,11 @@ 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;
832 struct grep_opt opt;
833 struct object_array list = { 0, 0, NULL };
834 const char **paths = NULL;
835 + struct string_list path_list = { NULL, 0, 0, 0 };
836 int i;
837 int dummy;
838 int nongit = 0, use_index = 1;
@@ -885,6 +916,8 @@ 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"),
921 OPT_BOOLEAN(0, "ext-grep", &external_grep_allowed__ignored,
922 "allow calling of grep(1) (ignored by this build)"),
923 { OPTION_CALLBACK, 0, "help-all", &options, NULL, "show usage",
@@ -960,6 +993,20 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
993 argc--;
994 }
995
996 + 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 + }
1008 + }
1009 +
1010 if (!opt.pattern_list)
1011 die("no pattern given.");
1012 if (!opt.fixed && opt.ignore_case)
@@ -1016,6 +1063,30 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
1063 paths[1] = NULL;
1064 }
1065
1066 + if (show_in_pager && (cached || list.nr))
1067 + die("--open-files-in-pager only works on the worktree");
1068 +
1069 + if (show_in_pager && opt.pattern_list && !opt.pattern_list->next) {
1070 + const char *pager = path_list.items[0].string;
1071 + int len = strlen(pager);
1072 +
1073 + if (len > 4 && is_dir_sep(pager[len - 5]))
1074 + pager += len - 4;
1075 +
1076 + if (!strcmp("less", pager) || !strcmp("vi", pager)) {
1077 + struct strbuf buf = STRBUF_INIT;
1078 + strbuf_addf(&buf, "+/%s%s",
1079 + strcmp("less", pager) ? "" : "*",
1080 + opt.pattern_list->pattern);
1081 + string_list_append(buf.buf, &path_list);
1082 + strbuf_detach(&buf, NULL);
1083 + }
1084 + }
1085 +
1086 + if (!show_in_pager)
1087 + setup_pager();
1088 +
1089 +
1090 if (!use_index) {
1091 if (cached)
1092 die("--cached cannot be used with --no-index.");
@@ -1035,6 +1106,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
1106
1107 if (use_threads)
1108 hit |= wait_all();
1109 + if (hit && show_in_pager)
1110 + run_pager(&opt, prefix);
1111 free_grep_patterns(&opt);
1112 return !hit;
1113 }
git.c
+1 -1
@@ -329,7 +329,7 @@ static void handle_internal_command(int argc, const char **argv)
329 { "fsck-objects", cmd_fsck, RUN_SETUP },
330 { "gc", cmd_gc, RUN_SETUP },
331 { "get-tar-commit-id", cmd_get_tar_commit_id },
332 - { "grep", cmd_grep, USE_PAGER },
332 + { "grep", cmd_grep },
333 { "hash-object", cmd_hash_object },
334 { "help", cmd_help },
335 { "index-pack", cmd_index_pack },
t/lib-pager.sh new
+15
@@ -0,0 +1,15 @@
1 +#!/bin/sh
2 +
3 +test_expect_success 'determine default pager' '
4 + test_might_fail git config --unset core.pager &&
5 + less=$(
6 + unset PAGER GIT_PAGER;
7 + git var GIT_PAGER
8 + ) &&
9 + test -n "$less"
10 +'
11 +
12 +if expr "$less" : '^[a-z][a-z]*$' >/dev/null
13 +then
14 + test_set_prereq SIMPLEPAGER
15 +fi
t/t7006-pager.sh
+4 -12
@@ -3,6 +3,7 @@
3 test_description='Test automatic use of a pager.'
4
5 . ./test-lib.sh
6 +. "$TEST_DIRECTORY"/lib-pager.sh
7
8 cleanup_fail() {
9 echo >&2 cleanup failed
@@ -158,21 +159,12 @@ test_expect_success 'color when writing to a file intended for a pager' '
159 colorful colorful.log
160 '
161
161 -test_expect_success 'determine default pager' '
162 - unset PAGER GIT_PAGER &&
163 - test_might_fail git config --unset core.pager ||
164 - cleanup_fail &&
165 -
166 - less=$(git var GIT_PAGER) &&
167 - test -n "$less"
168 -'
169 -
170 -if expr "$less" : '^[a-z][a-z]*$' >/dev/null && test_have_prereq TTY
162 +if test_have_prereq SIMPLEPAGER && test_have_prereq TTY
163 then
172 - test_set_prereq SIMPLEPAGER
164 + test_set_prereq SIMPLEPAGERTTY
165 fi
166
175 -test_expect_success SIMPLEPAGER 'default pager is used by default' '
167 +test_expect_success SIMPLEPAGERTTY 'default pager is used by default' '
168 unset PAGER GIT_PAGER &&
169 test_might_fail git config --unset core.pager &&
170 rm -f default_pager_used ||
t/t7810-grep.sh renamed
t/t7811-grep-open.sh new
+154
@@ -0,0 +1,154 @@
1 +#!/bin/sh
2 +
3 +test_description='git grep --open-files-in-pager
4 +'
5 +
6 +. ./test-lib.sh
7 +. "$TEST_DIRECTORY"/lib-pager.sh
8 +unset PAGER GIT_PAGER
9 +
10 +test_expect_success 'setup' '
11 + test_commit initial grep.h "
12 +enum grep_pat_token {
13 + GREP_PATTERN,
14 + GREP_PATTERN_HEAD,
15 + GREP_PATTERN_BODY,
16 + GREP_AND,
17 + GREP_OPEN_PAREN,
18 + GREP_CLOSE_PAREN,
19 + GREP_NOT,
20 + GREP_OR,
21 +};" &&
22 +
23 + test_commit add-user revision.c "
24 + }
25 + if (seen_dashdash)
26 + read_pathspec_from_stdin(revs, &sb, prune);
27 + strbuf_release(&sb);
28 +}
29 +
30 +static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
31 +{
32 + append_grep_pattern(&revs->grep_filter, ptn, \"command line\", 0, what);
33 +" &&
34 +
35 + mkdir subdir &&
36 + test_commit subdir subdir/grep.c "enum grep_pat_token" &&
37 +
38 + test_commit uninteresting unrelated "hello, world" &&
39 +
40 + echo GREP_PATTERN >untracked
41 +'
42 +
43 +test_expect_success SIMPLEPAGER 'git grep -O' '
44 + cat >$less <<-\EOF &&
45 + #!/bin/sh
46 + printf "%s\n" "$@" >pager-args
47 + EOF
48 + chmod +x $less &&
49 + cat >expect.less <<-\EOF &&
50 + +/*GREP_PATTERN
51 + grep.h
52 + EOF
53 + echo grep.h >expect.notless &&
54 + >empty &&
55 +
56 + PATH=.:$PATH git grep -O GREP_PATTERN >out &&
57 + {
58 + test_cmp expect.less pager-args ||
59 + test_cmp expect.notless pager-args
60 + } &&
61 + test_cmp empty out
62 +'
63 +
64 +test_expect_success 'git grep -O --cached' '
65 + test_must_fail git grep --cached -O GREP_PATTERN >out 2>msg &&
66 + grep open-files-in-pager msg
67 +'
68 +
69 +test_expect_success 'git grep -O --no-index' '
70 + rm -f expect.less pager-args out &&
71 + cat >expect <<-\EOF &&
72 + grep.h
73 + untracked
74 + EOF
75 + >empty &&
76 +
77 + (
78 + GIT_PAGER='\''printf "%s\n" >pager-args'\'' &&
79 + export GIT_PAGER &&
80 + git grep --no-index -O GREP_PATTERN >out
81 + ) &&
82 + test_cmp expect pager-args &&
83 + test_cmp empty out
84 +'
85 +
86 +test_expect_success 'setup: fake "less"' '
87 + cat >less <<-\EOF
88 + #!/bin/sh
89 + printf "%s\n" "$@" >actual
90 + EOF
91 +'
92 +
93 +test_expect_success 'git grep -O jumps to line in less' '
94 + cat >expect <<-\EOF &&
95 + +/*GREP_PATTERN
96 + grep.h
97 + EOF
98 + >empty &&
99 +
100 + GIT_PAGER=./less git grep -O GREP_PATTERN >out &&
101 + test_cmp expect actual &&
102 + test_cmp empty out
103 +'
104 +
105 +test_expect_success 'modified file' '
106 + rm -f actual &&
107 + cat >less <<-\EOF &&
108 + #!/bin/sh
109 + printf "%s\n" "$@" >actual
110 + EOF
111 + chmod +x $less &&
112 + cat >expect <<-\EOF &&
113 + +/*enum grep_pat_token
114 + grep.h
115 + revision.c
116 + subdir/grep.c
117 + unrelated
118 + EOF
119 + >empty &&
120 +
121 + echo "enum grep_pat_token" >unrelated &&
122 + test_when_finished "git checkout HEAD unrelated" &&
123 + GIT_PAGER=./less git grep -F -O "enum grep_pat_token" >out &&
124 + test_cmp expect actual &&
125 + test_cmp empty out
126 +'
127 +
128 +test_expect_success 'run from subdir' '
129 + rm -f actual &&
130 + echo grep.c >expect &&
131 + >empty &&
132 +
133 + (
134 + cd subdir &&
135 + export GIT_PAGER &&
136 + GIT_PAGER='\''printf "%s\n" >../args'\'' &&
137 + git grep -O "enum grep_pat_token" >../out &&
138 + GIT_PAGER="pwd >../dir; :" &&
139 + git grep -O "enum grep_pat_token" >../out2
140 + ) &&
141 + case $(cat dir) in
142 + *subdir)
143 + : good
144 + ;;
145 + *)
146 + false
147 + ;;
148 + esac &&
149 + test_cmp expect args &&
150 + test_cmp empty out &&
151 + test_cmp empty out2
152 +'
153 +
154 +test_done