git-grep: clarification on parameters.
[git] / git-grep.sh
1 #!/bin/sh
2 #
3 # Copyright (c) Linus Torvalds, 2005
4 #
5
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
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)
31                 got_pattern "$2"
32                 shift
33                 ;;
34         -A|-B|-C|-D|-d|-f|-m)
35                 flags=("${flags[@]}" "$1" "$2")
36                 shift
37                 ;;
38         --)
39                 # The rest are git-ls-files paths
40                 shift
41                 break
42                 ;;
43         -*)
44                 flags=("${flags[@]}" "$1")
45                 ;;
46         *)
47                 if [ -z "$no_more_patterns" ]
48                 then
49                         got_pattern "$1"
50                         shift
51                 fi
52                 [ "$1" = -- ] && shift
53                 break
54                 ;;
55         esac
56         shift
57 done
58 [ "$pattern" ] || {
59         usage
60 }
61 git-ls-files -z "${git_flags[@]}" -- "$@" |
62         xargs -0 grep "${flags[@]}" -e "$pattern" --