git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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.
Previous: Timo HirvonenNext: Junio C Hamano
Message 2 of 5 in “git-grep: convert from bash to sh”
  1. git-grep: convert from bash to shTimo Hirvonen, Dec 18, 2005
  2. Petr BaudisDec 18, 2005
  3. Junio C HamanoDec 18, 2005
  4. Linus TorvaldsDec 18, 2005
  5. Timo HirvonenDec 18, 2005

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.