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

[PATCH] submodule foreach: Added in --post-order=<command> and adjusted code per Jens Lehmann's suggestions

From
Eeacousineau <eacousineau@gmail.com>
Date
Apr 13, 2013, 04:04 UTC
Message-ID
<1365825865-12858-1-git-send-email-eacousineau@gmail.com>
In-Reply-To
<515B3C0E.9000802@web.de>
Signed-off-by: eacousineau <eacousineau@gmail.com>
---
I see what you meant by the extra variables, so I've fixed that so the
original flags aren't needed with recursion. Also updated it to not
print the entering command if there is only a post-order command.
Examples:

$ git submodule foreach --recursive --post-order 'echo Goodbye' "echo \"'ello\"" Entering 'b' 'ello Entering 'b/d' 'ello Leaving 'b/d' Goodbye Leaving 'b' Goodbye Entering 'c' 'ello Leaving 'c' Goodbye

$ git submodule foreach --recursive --post-order : Leaving 'b/d' Leaving 'b' Leaving 'c'

 git-submodule.sh | 31 ++++++++++++++++++++++++++-----
 1 file changed, 26 insertions(+), 5 deletions(-)
diff --git a/git-submodule.sh b/git-submodule.sh
index 79bfaac..e08a724 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -11,7 +11,7 @@ USAGE="[--quiet] add [-b <branch>] [-f|--force] [--name <name>] [--reference <re
    or: $dashless [--quiet] deinit [-f|--force] [--] <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
@@ -449,6 +449,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
 			;;
@@ -471,7 +480,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'")"
+			leave_msg="$(eval_gettext "Leaving '\$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/"
@@ -479,13 +490,23 @@ cmd_foreach()
 				# we make $path available to scripts ...
 				path=$sm_path
 				cd "$sm_path" &&
-				eval "$@" &&
+				if test $# -gt 0 -o -z "$post_order"
+				then
+					say "$enter_msg" &&
+					eval "$@" || die "$die_msg"
+				fi &&
 				if test -n "$recursive"
 				then
-					cmd_foreach "--recursive" "$@"
+					# subshell will use parent-scoped values
+					cmd_foreach "$@"
+				fi &&
+				if test -n "$post_order"
+				then
+					say "$leave_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.8.2.1.390.gd4ee029
Previous: Jens LehmannNext: Jens Lehmann
Message 18 of 25 in “Changing submodule foreach --recursive to be depth-first, --parent option to execute command in supermodule as well”
  1. Changing submodule foreach --recursive to be depth-first, --parent option to execute command in supermodule as wellEric Cousineau, Mar 4, 2013
  2. Jens LehmannMar 4, 2013
  3. Junio C HamanoMar 4, 2013
  4. Eric CousineauMar 5, 2013
  5. Heiko VoigtMar 5, 2013
  6. Junio C HamanoMar 5, 2013
  7. Eric CousineauMar 5, 2013
  8. Junio C HamanoMar 5, 2013
  9. Jens LehmannMar 5, 2013
  10. Phil HordMar 5, 2013
  11. Jens LehmannMar 9, 2013
  12. Heiko VoigtMar 11, 2013
  13. Phil HordMar 12, 2013
  14. Eric CousineauMar 14, 2013
  15. Jens LehmannMar 18, 2013
  16. Eric CousineauMar 26, 2013
  17. Jens LehmannApr 2, 2013
  18. submodule foreach: Added in --post-order=<command> and adjusted code per Jens Lehmann's suggestionseacousineau, Apr 13, 2013
  19. Jens LehmannApr 14, 2013
  20. Jens LehmannMar 18, 2013
  21. Eric CousineauMar 26, 2013
  22. Eric CousineauMar 26, 2013
  23. Junio C HamanoMar 26, 2013
  24. Junio C HamanoMar 26, 2013
  25. Eric CousineauApr 13, 2013

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.