Add "git grep" helper

Subsystems: kernel build + files below scripts/ (unless maintained elsewhere), the rest

3 messages, 2 authors, 2016-06-15 · open the first message on its own page

Add "git grep" helper

From: Linus Torvalds <torvalds@osdl.org>
Date: 2016-06-15 22:42:05

Very convenient shorthand for

	git-ls-files [file-patterns] | xargs grep <pattern>

which I tend to do all the time.

Yes, it's trivial, but it's really nice. I can do

	git grep '\<some_variable\>' arch/i386 include/asm-i386

and it does exactly what you'd think it does. And since it just uses the 
normal git-ls-files file patterns, you can do things like

	git grep something 'include/*.h'

and it will search all header files under the include/ subdirectory.

Signed-off-by: Linus Torvalds <torvalds@osdl.org>
---
diff --git a/Makefile b/Makefile
--- a/Makefile
+++ b/Makefile
@@ -76,7 +76,7 @@ SCRIPT_SH = \
 	git-tag.sh git-verify-tag.sh git-whatchanged.sh git.sh \
 	git-applymbox.sh git-applypatch.sh \
 	git-merge.sh git-merge-stupid.sh git-merge-octopus.sh \
-	git-merge-resolve.sh
+	git-merge-resolve.sh git-grep.sh
 
 SCRIPT_PERL = \
 	git-archimport.perl git-cvsimport.perl git-relink.perl \
diff --git a/git-grep.sh b/git-grep.sh
new file mode 100755
--- /dev/null
+++ b/git-grep.sh
@@ -0,0 +1,20 @@
+#!/bin/sh
+flags=
+while :; do
+  pattern="$1"
+  case "$pattern" in
+  -i|-I|-a|-E|-H|-h|-l)
+    flags="$flags $pattern"
+    shift
+    ;;
+  -*)
+    echo "unknown flag $pattern" >&2
+    exit 1
+    ;;
+  *)
+    break
+    ;;
+  esac
+done
+shift
+git-ls-files -z "$@" | xargs -0 grep $flags "$pattern"

Re: Add "git grep" helper

From: Morten Welinder <hidden>
Date: 2016-06-15 22:42:06

Cute, but what about grep flags like "-w", "-n", and "-c"?

M.

Re: Add "git grep" helper

From: Linus Torvalds <torvalds@osdl.org>
Date: 2016-06-15 22:42:06


On Mon, 12 Sep 2005, Morten Welinder wrote:
Cute, but what about grep flags like "-w", "-n", and "-c"?
Hey, add them to the flags list. Or just pass in all flags to grep, I
dunno (I was kind of expecting some flags to go to git-ls-files, but that
may or may not make any sense, and my script obviously didn't end up doing
that).

		Linus
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help