Skip to content

Commit 678e484

Browse files
dschogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 685359c commit 678e484

File tree

7 files changed

+255
-13
lines changed

7 files changed

+255
-13
lines changed

Documentation/git-grep.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +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]
1718
[-z | --null]
1819
[-c | --count] [--all-match] [-q | --quiet]
1920
[--max-depth <depth>]
@@ -104,6 +105,13 @@ OPTIONS
104105
For better compatibility with 'git diff', `--name-only` is a
105106
synonym for `--files-with-matches`.
106107

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+
107115
-z::
108116
--null::
109117
Output \0 instead of the character that normally follows a

builtin/grep.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include "tree-walk.h"
1212
#include "builtin.h"
1313
#include "parse-options.h"
14+
#include "string-list.h"
15+
#include "run-command.h"
1416
#include "userdiff.h"
1517
#include "grep.h"
1618
#include "quote.h"
@@ -556,6 +558,33 @@ static int grep_file(struct grep_opt *opt, const char *filename)
556558
}
557559
}
558560

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+
559588
static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
560589
{
561590
int hit = 0;
@@ -799,9 +828,11 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
799828
int cached = 0;
800829
int seen_dashdash = 0;
801830
int external_grep_allowed__ignored;
831+
int show_in_pager = 0;
802832
struct grep_opt opt;
803833
struct object_array list = { 0, 0, NULL };
804834
const char **paths = NULL;
835+
struct string_list path_list = { NULL, 0, 0, 0 };
805836
int i;
806837
int dummy;
807838
int nongit = 0, use_index = 1;
@@ -885,6 +916,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
885916
OPT_BOOLEAN(0, "all-match", &opt.all_match,
886917
"show only matches from files that match all patterns"),
887918
OPT_GROUP(""),
919+
OPT_BOOLEAN('O', "open-files-in-pager", &show_in_pager,
920+
"show matching files in the pager"),
888921
OPT_BOOLEAN(0, "ext-grep", &external_grep_allowed__ignored,
889922
"allow calling of grep(1) (ignored by this build)"),
890923
{ OPTION_CALLBACK, 0, "help-all", &options, NULL, "show usage",
@@ -960,6 +993,20 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
960993
argc--;
961994
}
962995

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+
9631010
if (!opt.pattern_list)
9641011
die("no pattern given.");
9651012
if (!opt.fixed && opt.ignore_case)
@@ -1016,6 +1063,30 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
10161063
paths[1] = NULL;
10171064
}
10181065

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+
10191090
if (!use_index) {
10201091
if (cached)
10211092
die("--cached cannot be used with --no-index.");
@@ -1035,6 +1106,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
10351106

10361107
if (use_threads)
10371108
hit |= wait_all();
1109+
if (hit && show_in_pager)
1110+
run_pager(&opt, prefix);
10381111
free_grep_patterns(&opt);
10391112
return !hit;
10401113
}

git.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ static void handle_internal_command(int argc, const char **argv)
329329
{ "fsck-objects", cmd_fsck, RUN_SETUP },
330330
{ "gc", cmd_gc, RUN_SETUP },
331331
{ "get-tar-commit-id", cmd_get_tar_commit_id },
332-
{ "grep", cmd_grep, USE_PAGER },
332+
{ "grep", cmd_grep },
333333
{ "hash-object", cmd_hash_object },
334334
{ "help", cmd_help },
335335
{ "index-pack", cmd_index_pack },

t/lib-pager.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
test_description='Test automatic use of a pager.'
44

55
. ./test-lib.sh
6+
. "$TEST_DIRECTORY"/lib-pager.sh
67

78
cleanup_fail() {
89
echo >&2 cleanup failed
@@ -158,21 +159,12 @@ test_expect_success 'color when writing to a file intended for a pager' '
158159
colorful colorful.log
159160
'
160161

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
171163
then
172-
test_set_prereq SIMPLEPAGER
164+
test_set_prereq SIMPLEPAGERTTY
173165
fi
174166

175-
test_expect_success SIMPLEPAGER 'default pager is used by default' '
167+
test_expect_success SIMPLEPAGERTTY 'default pager is used by default' '
176168
unset PAGER GIT_PAGER &&
177169
test_might_fail git config --unset core.pager &&
178170
rm -f default_pager_used ||
File renamed without changes.

t/t7811-grep-open.sh

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

0 commit comments

Comments
 (0)