Thread (5 messages) flat view 5 messages, 4 authors, 2016-06-15

Re: [PATCH] git-grep: convert from bash to sh

From: Petr Baudis <hidden>
Date: 2016-06-15 22:42:15
Subsystem: the rest · Maintainer: Linus Torvalds

Dear diary, on Sun, Dec 18, 2005 at 02:26:39PM CET, I got a letter
where Timo Hirvonen [off-list ref] said that...
sh does not support arrays so we have to use eval instead.

Signed-off-by: Timo Hirvonen <redacted>
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 <redacted>

---

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.
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help