Subject: Re: [PATCH/RFC] Changing submodule foreach --recursive to be depth-first, --parent option to execute command in supermodule as well

From: Eric Cousineau <[email-masked]>
Date: Mon, 25 Mar 2013 23:03:16 -0500

From 2c2923ada809d671828aa58dcda05a1b71222b70 Mon Sep 17 00:00:00 2001
From: Eric Cousineau <[email-masked]>
Date: Mon, 25 Mar 2013 22:27:06 -0500
Subject: [PATCH] submodule-foreach: Added in --post-order=<command> and
  adjusted code per Jens Lehmann's suggestion

Signed-off-by: Eric Cousineau <[email-masked]>
---
Updated the usage line.
I had put the locals in there before because I think I was having 
trouble with resolving some
of the variables in nested submodules, but now that I've taken them out 
they seem to work fine.
I also changed the message for the post-order to say "Exiting".

I did not have a chance to look into why I couldn't group the 
--post-order stuff into a string
when passing it on to submodule. I can look at it later on though.

Now the output is as follows:

$ git submodule foreach --recursive --post-order 'echo Post $name' 'echo 
Pre $path'
Entering 'b'
Pre b
Entering 'b/d'
Pre d
Exiting 'b/d'
Post d
Exiting 'b'
Post b
Entering 'c'
Pre c
Exiting 'c'
Post c

  git-submodule.sh |   35 ++++++++++++++++++++++++++++++-----
  1 file changed, 30 insertions(+), 5 deletions(-)

diff --git a/git-submodule.sh b/git-submodule.sh
index 004c034..4c9923a 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -10,7 +10,7 @@ USAGE="[--quiet] add [-b <branch>] [-f|--force] 
[--name <name>] [--reference <re
     or: $dashless [--quiet] init [--] [<path>...]
     or: $dashless [--quiet] update [--init] [--remote] [-N|--no-fetch] 
[-f|--force] [--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] foreach [--recursive] [--post-order 
<command>] <command>
     or: $dashless [--quiet] sync [--recursive] [--] [<path>...]"
  OPTIONS_SPEC=
  . git-sh-setup
@@ -434,6 +434,8 @@ Use -f if you really want to add it." >&2
  cmd_foreach()
  {
  	# parse $args after "submodule ... foreach".
+	recursive=
+	post_order=
  	while test $# -ne 0
  	do
  		case "$1" in
@@ -443,6 +445,15 @@ cmd_foreach()
  		--recursive)
  			recursive=1
  			;;
+		--post-order)
+			test "$#" = "1" && usage
+			post_order="$2"
+			shift
+			;;
+		--post-order=*)
+			# Will skip empty commands
+			post_order=${1#*=}
+			;;
  		-*)
  			usage
  			;;
@@ -465,7 +476,9 @@ cmd_foreach()
  		die_if_unmatched "$mode"
  		if test -e "$sm_path"/.git
  		then
-			say "$(eval_gettext "Entering '\$prefix\$sm_path'")"
+			enter_msg="$(eval_gettext "Entering '\$prefix\$sm_path'")"
+			exit_msg="$(eval_gettext "Exiting '\$prefix\$sm_path'")"
+			die_msg="$(eval_gettext "Stopping at '\$sm_path'; script returned 
non-zero status.")"
  			name=$(module_name "$sm_path")
  			(
  				prefix="$prefix$sm_path/"
@@ -473,13 +486,25 @@ cmd_foreach()
  				# we make $path available to scripts ...
  				path=$sm_path
  				cd "$sm_path" &&
-				eval "$@" &&
+				say "$enter_msg" &&
+				eval "$@" || die "$die_msg" &&
  				if test -n "$recursive"
  				then
-					cmd_foreach "--recursive" "$@"
+					if test -n "$post_order"
+					then
+						# tried keeping flags as a variable, but was having difficulty
+						cmd_foreach --recursive --post-order "$post_order" "$@"
+					else
+						cmd_foreach --recursive "$@"
+					fi
+				fi &&
+				if test -n "$post_order"
+				then
+					say "$exit_msg" &&
+					eval "$post_order" || die "$die_msg"
  				fi
  			) <&3 3<&- ||
-			die "$(eval_gettext "Stopping at '\$sm_path'; script returned 
non-zero status.")"
+			die "$die_msg"
  		fi
  	done
  }
-- 
1.7.9.5