-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
Johannes Schindelin
authored and
Junio C Hamano
committed
Jun 13, 2010
1 parent
685359c
commit 678e484
Showing
7 changed files
with
255 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/bin/sh | ||
|
||
test_expect_success 'determine default pager' ' | ||
test_might_fail git config --unset core.pager && | ||
less=$( | ||
unset PAGER GIT_PAGER; | ||
git var GIT_PAGER | ||
) && | ||
test -n "$less" | ||
' | ||
|
||
if expr "$less" : '^[a-z][a-z]*$' >/dev/null | ||
then | ||
test_set_prereq SIMPLEPAGER | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
#!/bin/sh | ||
|
||
test_description='git grep --open-files-in-pager | ||
' | ||
|
||
. ./test-lib.sh | ||
. "$TEST_DIRECTORY"/lib-pager.sh | ||
unset PAGER GIT_PAGER | ||
|
||
test_expect_success 'setup' ' | ||
test_commit initial grep.h " | ||
enum grep_pat_token { | ||
GREP_PATTERN, | ||
GREP_PATTERN_HEAD, | ||
GREP_PATTERN_BODY, | ||
GREP_AND, | ||
GREP_OPEN_PAREN, | ||
GREP_CLOSE_PAREN, | ||
GREP_NOT, | ||
GREP_OR, | ||
};" && | ||
test_commit add-user revision.c " | ||
} | ||
if (seen_dashdash) | ||
read_pathspec_from_stdin(revs, &sb, prune); | ||
strbuf_release(&sb); | ||
} | ||
static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what) | ||
{ | ||
append_grep_pattern(&revs->grep_filter, ptn, \"command line\", 0, what); | ||
" && | ||
mkdir subdir && | ||
test_commit subdir subdir/grep.c "enum grep_pat_token" && | ||
test_commit uninteresting unrelated "hello, world" && | ||
echo GREP_PATTERN >untracked | ||
' | ||
|
||
test_expect_success SIMPLEPAGER 'git grep -O' ' | ||
cat >$less <<-\EOF && | ||
#!/bin/sh | ||
printf "%s\n" "$@" >pager-args | ||
EOF | ||
chmod +x $less && | ||
cat >expect.less <<-\EOF && | ||
+/*GREP_PATTERN | ||
grep.h | ||
EOF | ||
echo grep.h >expect.notless && | ||
>empty && | ||
PATH=.:$PATH git grep -O GREP_PATTERN >out && | ||
{ | ||
test_cmp expect.less pager-args || | ||
test_cmp expect.notless pager-args | ||
} && | ||
test_cmp empty out | ||
' | ||
|
||
test_expect_success 'git grep -O --cached' ' | ||
test_must_fail git grep --cached -O GREP_PATTERN >out 2>msg && | ||
grep open-files-in-pager msg | ||
' | ||
|
||
test_expect_success 'git grep -O --no-index' ' | ||
rm -f expect.less pager-args out && | ||
cat >expect <<-\EOF && | ||
grep.h | ||
untracked | ||
EOF | ||
>empty && | ||
( | ||
GIT_PAGER='\''printf "%s\n" >pager-args'\'' && | ||
export GIT_PAGER && | ||
git grep --no-index -O GREP_PATTERN >out | ||
) && | ||
test_cmp expect pager-args && | ||
test_cmp empty out | ||
' | ||
|
||
test_expect_success 'setup: fake "less"' ' | ||
cat >less <<-\EOF | ||
#!/bin/sh | ||
printf "%s\n" "$@" >actual | ||
EOF | ||
' | ||
|
||
test_expect_success 'git grep -O jumps to line in less' ' | ||
cat >expect <<-\EOF && | ||
+/*GREP_PATTERN | ||
grep.h | ||
EOF | ||
>empty && | ||
GIT_PAGER=./less git grep -O GREP_PATTERN >out && | ||
test_cmp expect actual && | ||
test_cmp empty out | ||
' | ||
|
||
test_expect_success 'modified file' ' | ||
rm -f actual && | ||
cat >less <<-\EOF && | ||
#!/bin/sh | ||
printf "%s\n" "$@" >actual | ||
EOF | ||
chmod +x $less && | ||
cat >expect <<-\EOF && | ||
+/*enum grep_pat_token | ||
grep.h | ||
revision.c | ||
subdir/grep.c | ||
unrelated | ||
EOF | ||
>empty && | ||
echo "enum grep_pat_token" >unrelated && | ||
test_when_finished "git checkout HEAD unrelated" && | ||
GIT_PAGER=./less git grep -F -O "enum grep_pat_token" >out && | ||
test_cmp expect actual && | ||
test_cmp empty out | ||
' | ||
|
||
test_expect_success 'run from subdir' ' | ||
rm -f actual && | ||
echo grep.c >expect && | ||
>empty && | ||
( | ||
cd 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 | ||
) && | ||
case $(cat dir) in | ||
*subdir) | ||
: good | ||
;; | ||
*) | ||
false | ||
;; | ||
esac && | ||
test_cmp expect args && | ||
test_cmp empty out && | ||
test_cmp empty out2 | ||
' | ||
|
||
test_done |