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

[RFC PATCH 1/2] contrib: add git-submodule-grep.sh

From
Chris Packham <judge.packham@gmail.com>
Date
Sep 23, 2010, 21:17 UTC
Message-ID
<1285276627-7907-2-git-send-email-judge.packham@gmail.com>
In-Reply-To
<1285276627-7907-1-git-send-email-judge.packham@gmail.com>
A simple wrapper around 'git grep' that is submodule aware.

The advantage of this over "git submodule foreach 'git grep <pattern>'" is that the results are presented with a path relative to the current directory. Also this script will work from any subdirectory of any submodule in the current superproject.

Signed-off-by: Chris Packham <judge.packham@gmail.com>
---
 contrib/git-submodule-grep.sh |   35 +++++++++++++++++++++++++++++++++++
 1 files changed, 35 insertions(+), 0 deletions(-)
 create mode 100755 contrib/git-submodule-grep.sh
diff --git a/contrib/git-submodule-grep.sh b/contrib/git-submodule-grep.sh
new file mode 100755
index 0000000..407fdb0
--- /dev/null
+++ b/contrib/git-submodule-grep.sh
@@ -0,0 +1,35 @@
+#!/bin/sh
+# Copyright (c) 2010, Chris Packham
+# Wrapper for git grep that is submodule aware
+
+SUBDIRECTORY_OK="yes"
+. git-sh-setup
+
+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)
+}
+
+# we don't do anything with the arg but we should check that we
+# have something to pass to git grep
+test $# -lt 1 && die "fatal: no pattern given."
+
+prefix="$(submodule_root_relative)" || exit 1
+(cd "./$prefix"; \
+	git submodule --quiet foreach \
+		"git --no-pager grep $@ | sed s\"|.*|$prefix\$path/&|\" || true") \
+			| git_pager
-- 
1.7.3.dirty
Previous: Chris PackhamNext: Chris Packham
Message 2 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.