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

Improve "git grep" flags handling

This allows any arbitrary flags to "grep", and knows about the few special grep flags that take an argument too. It also allows some flags for git-ls-files, although their usefulness is questionable. With this, something line git grep -w -1 pattern works, without the script enumerating every possible flag. [jc: this is the version Linus sent out after I showed him a barf-o-meter test version that avoids shell arrays. He must have typed this version blindly, since he said: I'm not barfing, but that's probably because my brain just shut down and is desperately trying to gouge my eyes out with a spoon. I slightly fixed it to catch the remaining arguments meant to be given git-ls-files.] Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Linus Torvalds committed Sep 12, 2005 at 16:46 UTC 5d9d831a51dc7847ad40fac77f9a072aa2d1c0bd
1 file changed +41 -23
git-grep.sh
+41 -23
@@ -1,25 +1,43 @@
1 #!/bin/sh
2 -flags=
3 -while :; do
4 - pattern="$1"
5 - case "$pattern" in
6 - -i|-I|-a|-E|-H|-h|-l)
7 - flags="$flags $pattern"
8 - shift
9 - ;;
10 - -e)
11 - pattern="$2"
12 - shift
13 - break
14 - ;;
15 - -*)
16 - echo "unknown flag $pattern" >&2
17 - exit 1
18 - ;;
19 - *)
20 - break
21 - ;;
22 - esac
2 +#
3 +# Copyright (c) Linus Torvalds, 2005
4 +#
5 +
6 +pattern=
7 +flags=()
8 +git_flags=()
9 +while : ; do
10 + case "$1" in
11 + --cached|--deleted|--others|--killed|\
12 + --ignored|--exclude=*|\
13 + --exclude-from=*|\--exclude-per-directory=*)
14 + git_flags=("${git_flags[@]}" "$1")
15 + ;;
16 + -e)
17 + pattern="$2"
18 + shift
19 + ;;
20 + -A|-B|-C|-D|-d|-f|-m)
21 + flags=("${flags[@]}" "$1" "$2")
22 + shift
23 + ;;
24 + --)
25 + # The rest are git-ls-files paths (or flags)
26 + shift
27 + break
28 + ;;
29 + -*)
30 + flags=("${flags[@]}" "$1")
31 + ;;
32 + *)
33 + if [ -z "$pattern" ]; then
34 + pattern="$1"
35 + shift
36 + fi
37 + break
38 + ;;
39 + esac
40 + shift
41 done
24 -shift
25 -git-ls-files -z "$@" | xargs -0 grep $flags -e "$pattern"
42 +git-ls-files -z "${git_flags[@]}" "$@" |
43 + xargs -0 grep "${flags[@]}" "$pattern"