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

[RFC PATCH 2/2] submodule: add grep command

From
Chris Packham <judge.packham@gmail.com>
Date
Sep 23, 2010, 21:17 UTC
Message-ID
<1285276627-7907-3-git-send-email-judge.packham@gmail.com>
In-Reply-To
<1285276627-7907-1-git-send-email-judge.packham@gmail.com>

This adds a 'grep' command to the 'git submodule' commands. Internally it is basically a wrapper for "git submodule foreach 'git grep <pattern>'" but does have the advantage of supplying results with a path relative to the current directory.

Unlike contrib/git-submodule-grep.sh this can only be run from the root of a submodule or the root of the superproject.

Signed-off-by: Chris Packham <judge.packham@gmail.com>
---
 git-sh-setup.sh  |   19 +++++++++++++++++++
 git-submodule.sh |   41 +++++++++++++++++++++++++++++++++++++++--
 2 files changed, 58 insertions(+), 2 deletions(-)
diff --git a/git-sh-setup.sh b/git-sh-setup.sh
index 6131670..2a1d585 100644
--- a/git-sh-setup.sh
+++ b/git-sh-setup.sh
@@ -145,6 +145,25 @@ require_work_tree () {
 	die "fatal: $0 cannot be used without a working tree."
 }
 
+submodule_root_relative()
+{
+	ceiling="$HOME"
+	while test ! -e "$cdup.gitmodules"; do
+		cdup="$cdup../"
+		if test "$(cd $cdup && pwd)" == "$ceiling"; then
+			echo >&2 "fatal: failed to find superproject root."
+			echo >&2 "       stopped searching at $ceiling".
+			exit 1
+		fi
+	done
+    echo "$cdup"
+}
+
+submodule_root()
+{
+    (cd "$(submodule_root_relative)" && pwd)
+}
+
 get_author_ident_from_commit () {
 	pick_author_script='
 	/^author /{
diff --git a/git-submodule.sh b/git-submodule.sh
index 9ebbab7..3edaa49 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -11,7 +11,8 @@ USAGE="[--quiet] add [-b branch] [-f|--force] [--reference <repository>] [--] <r
    or: $dashless [--quiet] update [--init] [-N|--no-fetch] [--rebase] [--reference <repository>] [--merge] [--recursive] [--] [<path>...]
    or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
    or: $dashless [--quiet] foreach [--recursive] <command>
-   or: $dashless [--quiet] sync [--] [<path>...]"
+   or: $dashless [--quiet] sync [--] [<path>...]
+   or: $dashless [--quiet] grep [--] [-n] [<pattern>]"
 OPTIONS_SPEC=
 . git-sh-setup
 . git-parse-remote
@@ -849,6 +850,42 @@ cmd_sync()
 		fi
 	done
 }
+#
+# Search for a pattern within all submodules
+# This is a fairly simple wrapper using foreach and git grep,
+# arguments could be passed to git grep but for now we only
+# understand -n
+#
+cmd_grep()
+{
+	grep_args=""
+	while test $# -ne 0
+	do
+		case "$1" in
+		-n)
+			grep_args="$1"
+			shift
+			;;
+		--)
+			shift
+			break
+			;;
+		-*)
+			usage
+			;;
+		*)
+			break
+			;;
+		esac
+	done
+	test $# -lt 1 && die "fatal: no pattern given"
+
+	prefix="$(submodule_root_relative)" || exit 1
+	(cd "./$prefix"; \
+		cmd_foreach "git --no-pager grep $grep_args -- $@ | \
+			sed s\"|.*|$prefix\$path/&|\" || true") \
+				| git_pager
+}
 
 # This loop parses the command line arguments to find the
 # subcommand name to dispatch.  Parsing of the subcommand specific
@@ -859,7 +896,7 @@ cmd_sync()
 while test $# != 0 && test -z "$command"
 do
 	case "$1" in
-	add | foreach | init | update | status | summary | sync)
+	add | foreach | init | update | status | summary | sync | grep)
 		command=$1
 		;;
 	-q|--quiet)
-- 
1.7.3.dirty
Previous: Chris PackhamNext: Heiko Voigt
Message 3 of 6 in “submodule aware grep”
  1. 0/2 submodule aware grepChris Packham, Sep 23, 2010
  2. 1/2 contrib: add git-submodule-grep.shChris Packham, Sep 23, 2010
  3. 2/2 submodule: add grep commandChris Packham, Sep 23, 2010
  4. Heiko VoigtSep 24, 2010
  5. Chris PackhamSep 24, 2010
  6. Jens LehmannSep 24, 2010

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.