git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] - Added recurse command to git submodule
@ 2008-01-09  5:51 imyousuf
  2008-01-09  8:38 ` Junio C Hamano
  0 siblings, 1 reply; 8+ messages in thread
From: imyousuf @ 2008-01-09  5:51 UTC (permalink / raw)
  To: git; +Cc: gitster, Imran M Yousuf

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

^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2008-01-10  4:51 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-01-09  5:51 [PATCH] - Added recurse command to git submodule imyousuf
2008-01-09  8:38 ` Junio C Hamano
2008-01-09  8:55   ` Imran M Yousuf
2008-01-09 10:42   ` Lars Hjemli
2008-01-09 20:26     ` Junio C Hamano
2008-01-10  3:27       ` Imran M Yousuf
2008-01-10  4:09         ` Junio C Hamano
2008-01-10  4:50           ` Imran M Yousuf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).