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

[PATCH] - Added recurse command to git submodule

From
imyousuf@gmail.com <imyousuf@gmail.com>
Date
Jan 9, 2008, 05:51 UTC
Message-ID
<1199857906-26321-1-git-send-email-imyousuf@gmail.com>
From: Imran M Yousuf <imyousuf@smartitengineering.com>
 - The purpose of the recurse command in the git submodule is to recurse
 a command in its submodule at depths specified. For example if one wants
 to do a diff on its project with submodules at once, one can simply do
 git-submodule recurse diff HEAD and would see the diff for all the modules
 it contains. If during recursion it is found that a module has not been
 initialized or updated than the command also initializes and updates them.
 It is to be noted that argument specification to the command intended (in
 above example diff) to recurse will be same as its original command (i.e.
 git diff). If the original command spec changes it will have no effect on
 the recurse command. Depth of recursion can be specified simply by
 mentioning the -d <recursion depth> argument to recurse command. If not
 specified or specified to 0, it will be comprehended to recurse at all
 depth; if a positive number is specified than maximum recursion depth will
 never cross that depth. In order to see some information -v option may be
 used
Signed-off-by: Imran M Yousuf <imyousuf@smartitengineering.com>
---
 git-submodule.sh |  112 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 111 insertions(+), 1 deletions(-)
diff --git a/git-submodule.sh b/git-submodule.sh
index 8a29382..5cb931e 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -4,7 +4,7 @@
 #
 # Copyright (c) 2007 Lars Hjemli
 
-USAGE='[--quiet] [--cached] [add <repo> [-b branch]|status|init|update] [--] [<path>...]'
+USAGE='[[--quiet] [--cached] [add <repo> [-b branch]|status|init|update] [--] [<path>...]|[recurse [-v] [-d <recursion depth>] <command> <arguments> ...]]'
 OPTIONS_SPEC=
 . git-sh-setup
 require_work_tree
@@ -17,6 +17,9 @@ status=
 quiet=
 cached=
 command=
+recurse_verbose=0
+recursion_depth=0
+current_recursion_depth=0
 
 #
 # print stuff on stdout unless -q was specified
@@ -294,6 +297,82 @@ modules_list()
 	done
 }
 
+# Initializes the submodule if already not initialized
+initialize_sub_module() {
+	if [ ! -d "$1"/.git ]; then
+		if [ $recurse_verbose -eq 1 ]; then
+			echo Initializing and updating "$1"
+		fi
+		git-submodule init "$1"; git-submodule update "$1"
+	fi
+}
+
+# This actually traverses the module; checks
+# whether the module is initialized or not.
+# if not initialized, then done so and then the
+# intended command is evaluated. Then it
+# recursively goes into it modules.
+traverse_module() {
+	if [ $recurse_verbose -eq 1 ]; then
+		echo Current recursion depth: "$current_recursion_depth"
+	fi
+	initialize_sub_module "$1"
+	(
+		submod_path="$1"
+		shift
+		cd "$submod_path"
+		if [ $recurse_verbose -eq 1 ]; then
+			echo Working in mod "$submod_path" @ `pwd` with "$@" \("$#"\)
+		fi
+	        git "$@"
+		# Check whether submodules exists only if recursion to that depth
+		# was requested by user
+		if test "$recursion_depth" -eq 0 || test "$current_recursion_depth" -lt "$recursion_depth"
+		then
+			if [ -f .gitmodules ]; then
+		                for mod_path in `sed -n -e 's/path = //p' .gitmodules`; do
+					export current_recursion_depth=$(echo "$current_recursion_depth+1" | bc)
+		                        traverse_module "$mod_path" "$@"
+					export current_recursion_depth=$(echo "$current_recursion_depth-1" | bc)
+		                done
+			fi
+	        fi
+	)
+}
+
+# Propagates or recurses over all the submodules at any
+# depth with any git command, e.g. git-clone, git-status,
+# git-commit etc., with the arguments supplied exactly as
+# it would have been supplied to the command otherwise.
+# This actually starts the recursive propagation
+modules_recurse() {
+	project_home=`pwd`
+	echo Project Home: "$project_home"
+	if [ $recurse_verbose -eq 1 ]; then
+		if [ $recursion_depth = 0 ]; then
+			echo Recursion will go to all depths
+		else
+			echo Recursion depth specified to "$recursion_depth"
+		fi
+	fi
+	if [ -d "$project_home"/.git/ ]; then
+		if [ $recurse_verbose -eq 1 ]; then
+			echo Command to recurse: git "$@"
+		fi
+		git "$@"
+		if [ -f .gitmodules ]; then
+			for mod_path in `sed -n -e 's/path = //p' .gitmodules`; do
+				export current_recursion_depth=1
+				traverse_module $mod_path "$@"
+			done
+		fi
+	else
+		echo "$project_home" not a git repo thus exiting
+		exit
+	fi
+	unset current_recursion_depth
+}
+
 # command specifies the whole function name since 
 # one of theirs prefix is module not modules
 while test $# != 0
@@ -326,6 +405,37 @@ do
 	--cached)
 		command="modules_list"
 		;;
+	recurse)
+		command="modules_$1"
+		repeat=1
+		while test $repeat = 1
+		do
+			case "$2" in
+				-v)
+					recurse_verbose=1
+					shift
+					;;
+				-d)
+					if [ -z $3 ]; then
+						echo No \<recursion depth\> specified
+						usage
+					elif [ `expr "$3" : '[1-9][0-9]*.*'` -eq `expr "$3" : '.*'` ]; then
+						recursion_depth="$3"
+						shift
+						shift
+					else
+						echo \<recursion depth\> not an integer
+						usage
+					fi
+					;;
+				*)
+					repeat=0
+					;;
+			esac
+		done
+		shift
+		break
+		;;
 	--)
 		break
 		;;
-- 
1.5.3.7
Next: Junio C Hamano
Message 1 of 8 in “- Added recurse command to git submodule”
  1. - Added recurse command to git submoduleimyousuf@gmail.com, Jan 9, 2008
  2. Junio C HamanoJan 9, 2008
  3. Imran M YousufJan 9, 2008
  4. Lars HjemliJan 9, 2008
  5. Junio C HamanoJan 9, 2008
  6. Imran M YousufJan 10, 2008
  7. Junio C HamanoJan 10, 2008
  8. Imran M YousufJan 10, 2008

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.