Skip to content

Commit 5b2bcc7

Browse files
author
Junio C Hamano
committed
git-grep: clarification on parameters.
We forgot to make sure that there is no more than one pattern parameter. Also when looking for files in a directory called '--others', it passed that path limiter without preceding the end-of-options marker '--' to underlying git-ls-files, which misunderstood it as one of its options instead. $ git grep --others -e Meta/Make Meta $ git grep -o -e Meta/Make Meta $ git grep -o Meta/Make Meta look for a string "Meta/Make" from untracked files in Meta/ directory. $ git grep Meta/Make --others looks for the same string from tracked files in ./--others directory. On the other hand, $ git grep -e Meta/Make --others does not have a freestanding pattern, so everybody is parameter and there is no path specifier. It looks for the string in all the untracked files without any path limiter. [jc: updated with usability enhancements and documentation cleanups from Sean.] Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent 0bdd79a commit 5b2bcc7

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

Documentation/git-grep.txt

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ git-grep - print lines matching a pattern
88

99
SYNOPSIS
1010
--------
11-
'git-grep' [<option>...] <pattern> [<path>...]
11+
'git-grep' [<option>...] [-e] <pattern> [--] [<path>...]
1212

1313
DESCRIPTION
1414
-----------
@@ -18,13 +18,24 @@ containing a match to the given pattern.
1818

1919
OPTIONS
2020
-------
21+
`--`::
22+
Signals the end of options; the rest of the parameters
23+
are <path> limiters.
24+
2125
<option>...::
2226
Either an option to pass to `grep` or `git-ls-files`.
23-
Some `grep` options, such as `-C` and `-m`, that take
24-
parameters are known to `git-grep`.
27+
28+
The following are the specific `git-ls-files` options
29+
that may be given: `-o`, `--cached`, `--deleted`, `--others`,
30+
`--killed`, `--ignored`, `--modified`, `--exclude=*`,
31+
`--exclude-from=*`, and `--exclude-per-directory=*`.
32+
33+
All other options will be passed to `grep`.
2534

2635
<pattern>::
27-
The pattern to look for.
36+
The pattern to look for. The first non option is taken
37+
as the pattern; if your pattern begins with a dash, use
38+
`-e <pattern>`.
2839

2940
<path>...::
3041
Optional paths to limit the set of files to be searched;

git-grep.sh

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,53 @@
33
# Copyright (c) Linus Torvalds, 2005
44
#
55

6-
USAGE='<option>... <pattern> <path>...'
6+
USAGE='[<option>...] [-e] <pattern> [<path>...]'
77
SUBDIRECTORY_OK='Yes'
88
. git-sh-setup
99

10+
got_pattern () {
11+
if [ -z "$no_more_patterns" ]
12+
then
13+
pattern="$1" no_more_patterns=yes
14+
else
15+
die "git-grep: do not specify more than one pattern"
16+
fi
17+
}
18+
19+
no_more_patterns=
1020
pattern=
1121
flags=()
1222
git_flags=()
1323
while : ; do
1424
case "$1" in
15-
--cached|--deleted|--others|--killed|\
16-
--ignored|--exclude=*|\
25+
-o|--cached|--deleted|--others|--killed|\
26+
--ignored|--modified|--exclude=*|\
1727
--exclude-from=*|\--exclude-per-directory=*)
1828
git_flags=("${git_flags[@]}" "$1")
1929
;;
2030
-e)
21-
pattern="$2"
31+
got_pattern "$2"
2232
shift
2333
;;
2434
-A|-B|-C|-D|-d|-f|-m)
2535
flags=("${flags[@]}" "$1" "$2")
2636
shift
2737
;;
2838
--)
29-
# The rest are git-ls-files paths (or flags)
39+
# The rest are git-ls-files paths
3040
shift
3141
break
3242
;;
3343
-*)
3444
flags=("${flags[@]}" "$1")
3545
;;
3646
*)
37-
if [ -z "$pattern" ]; then
38-
pattern="$1"
47+
if [ -z "$no_more_patterns" ]
48+
then
49+
got_pattern "$1"
3950
shift
4051
fi
52+
[ "$1" = -- ] && shift
4153
break
4254
;;
4355
esac
@@ -46,5 +58,5 @@ done
4658
[ "$pattern" ] || {
4759
usage
4860
}
49-
git-ls-files -z "${git_flags[@]}" "$@" |
50-
xargs -0 grep "${flags[@]}" -e "$pattern"
61+
git-ls-files -z "${git_flags[@]}" -- "$@" |
62+
xargs -0 grep "${flags[@]}" -e "$pattern" --

0 commit comments

Comments
 (0)