@matheusbsbs / matheus-bagliano / commits / 5b2bcc7b2d
Fork 0

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>

Junio C Hamano committed Jan 20, 2006 at 15:00 UTC 5b2bcc7b2d546c636f79490655b3347acc91d17f
2 files changed +36 -13
Documentation/git-grep.txt
+15 -4
@@ -8,7 +8,7 @@ git-grep - print lines matching a pattern
8
9 SYNOPSIS
10 --------
11 -'git-grep' [<option>...] <pattern> [<path>...]
11 +'git-grep' [<option>...] [-e] <pattern> [--] [<path>...]
12
13 DESCRIPTION
14 -----------
@@ -18,13 +18,24 @@ containing a match to the given pattern.
18
19 OPTIONS
20 -------
21 +`--`::
22 + Signals the end of options; the rest of the parameters
23 + are <path> limiters.
24 +
25 <option>...::
26 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`.
34
35 <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>`.
39
40 <path>...::
41 Optional paths to limit the set of files to be searched;
git-grep.sh
+21 -9
@@ -3,22 +3,32 @@
3 # Copyright (c) Linus Torvalds, 2005
4 #
5
6 -USAGE='<option>... <pattern> <path>...'
6 +USAGE='[<option>...] [-e] <pattern> [<path>...]'
7 SUBDIRECTORY_OK='Yes'
8 . git-sh-setup
9
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=
20 pattern=
21 flags=()
22 git_flags=()
23 while : ; do
24 case "$1" in
15 - --cached|--deleted|--others|--killed|\
16 - --ignored|--exclude=*|\
25 + -o|--cached|--deleted|--others|--killed|\
26 + --ignored|--modified|--exclude=*|\
27 --exclude-from=*|\--exclude-per-directory=*)
28 git_flags=("${git_flags[@]}" "$1")
29 ;;
30 -e)
21 - pattern="$2"
31 + got_pattern "$2"
32 shift
33 ;;
34 -A|-B|-C|-D|-d|-f|-m)
@@ -26,7 +36,7 @@ while : ; do
36 shift
37 ;;
38 --)
29 - # The rest are git-ls-files paths (or flags)
39 + # The rest are git-ls-files paths
40 shift
41 break
42 ;;
@@ -34,10 +44,12 @@ while : ; do
44 flags=("${flags[@]}" "$1")
45 ;;
46 *)
37 - if [ -z "$pattern" ]; then
38 - pattern="$1"
47 + if [ -z "$no_more_patterns" ]
48 + then
49 + got_pattern "$1"
50 shift
51 fi
52 + [ "$1" = -- ] && shift
53 break
54 ;;
55 esac
@@ -46,5 +58,5 @@ done
58 [ "$pattern" ] || {
59 usage
60 }
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" --