From 851d65fcfb8f49131428a57fc318af7b56416430 Mon Sep 17 00:00:00 2001
From: eacousineau <eacousineau@gmail.com>
Date: Mon, 4 Mar 2013 01:08:07 -0600
Subject: [PATCH] area: submodules Make foreach --recursive do depth-first.
 Make foreach --parent [--recursive] execute command in toplevel supermodule

Signed-off-by: Eric Cousineau <eacousineau@gmail.com>
---
 git-submodule.sh | 31 ++++++++++++++++++++++++++-----
 1 file changed, 26 insertions(+), 5 deletions(-)

diff --git a/git-submodule.sh b/git-submodule.sh
index 004c034..721c959 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -434,6 +434,9 @@ Use -f if you really want to add it." >&2
 cmd_foreach()
 {
 	# parse $args after "submodule ... foreach".
+	# Gratuitous local's to prevent recursive bleeding
+	local parent=
+	local recursive=
 	while test $# -ne 0
 	do
 		case "$1" in
@@ -443,6 +446,10 @@ cmd_foreach()
 		--recursive)
 			recursive=1
 			;;
+		--parent)
+			# Execute command in parent, after children commands are executed
+			parent=1
+			;;
 		-*)
 			usage
 			;;
@@ -464,8 +471,8 @@ cmd_foreach()
 	do
 		die_if_unmatched "$mode"
 		if test -e "$sm_path"/.git
-		then
-			say "$(eval_gettext "Entering '\$prefix\$sm_path'")"
+		then 
+			local message="$(eval_gettext "Entering '\$prefix\$sm_path'")"
 			name=$(module_name "$sm_path")
 			(
 				prefix="$prefix$sm_path/"
@@ -473,15 +480,29 @@ cmd_foreach()
 				# we make $path available to scripts ...
 				path=$sm_path
 				cd "$sm_path" &&
-				eval "$@" &&
 				if test -n "$recursive"
 				then
 					cmd_foreach "--recursive" "$@"
-				fi
+				fi &&
+				(
+					# Put message here so it stays somewhat tidy -- hopefully OK since prefixes are included
+					say "$message"
+					eval "$@"
+				)
 			) <&3 3<&- ||
 			die "$(eval_gettext "Stopping at '\$sm_path'; script returned non-zero status.")"
 		fi
-	done
+	done &&
+	(
+		if test -n "$parent"
+		then
+			name=$(basename "$toplevel")
+			clear_local_git_env
+			path=.
+			say "$(eval_gettext "Entering '\$name'")" # Not sure of proper thing here
+			eval "$@" || die "$(eval_gettext "Stopping at supermodule; script returned non-zero status.")"
+		fi
+	)
 }
 
 #
-- 
1.8.2.rc1.24.g06d67b8

