Re: [PATCH] git-grep: convert from bash to sh
- From
Petr Baudis <pasky@suse.cz>
- Date
- Dec 18, 2005, 14:56 UTC
- Message-ID
- <20051218145621.GX22159@pasky.or.cz>
- In-Reply-To
- <20051218152639.5c14bc26.tihirvon@gmail.com>
Dear diary, on Sun, Dec 18, 2005 at 02:26:39PM CET, I got a letter where Timo Hirvonen <tihirvon@gmail.com> said that...
> sh does not support arrays so we have to use eval instead. > > Signed-off-by: Timo Hirvonen <tihirvon@gmail.com>
This version also makes it work properly with patterns containing quotes and backslashes (not so unusual when you grep for C strings).
Signed-off-by: Petr Baudis <pasky@suse.cz>
---
I'm kind of sensitive to this stuff. I still passionately hate scp making me to double-quote remote filenames. It's just evil.
diff --git a/git-grep.sh b/git-grep.sh index 2ed8e95..7e9e5bf 100755 --- a/git-grep.sh +++ b/git-grep.sh @@ -8,21 +8,21 @@ SUBDIRECTORY_OK='Yes' . git-sh-setup pattern= -flags=() -git_flags=() +flags= +git_flags= while : ; do case "$1" in --cached|--deleted|--others|--killed|\ --ignored|--exclude=*|\ --exclude-from=*|\--exclude-per-directory=*) - git_flags=("${git_flags[@]}" "$1") + git_flags="$git_flags '$1'" ;; -e) pattern="$2" shift ;; -A|-B|-C|-D|-d|-f|-m) - flags=("${flags[@]}" "$1" "$2") + flags="$flags '$1' '$2'" shift ;; --) @@ -31,7 +31,7 @@ while : ; do break ;; -*) - flags=("${flags[@]}" "$1") + flags="$flags '$1'" ;; *) if [ -z "$pattern" ]; then @@ -46,5 +46,6 @@ done [ "$pattern" ] || { usage } -git-ls-files -z "${git_flags[@]}" "$@" | - xargs -0 grep "${flags[@]}" -e "$pattern" +pattern="$(echo "$pattern" | sed 's/[\\"]/\\&/g')" +eval git-ls-files -z "$git_flags" '"$@"' | + eval xargs -0 grep "$flags" -e '"$pattern"'
-- Petr "Pasky" Baudis Stuff: http://pasky.or.cz/ VI has two modes: the one in which it beeps and the one in which it doesn't.