git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH/RFC] Changing submodule foreach --recursive to be depth-first, --parent option to execute command in supermodule as well
@ 2013-03-04  8:41 Eric Cousineau
  2013-03-04 22:15 ` Jens Lehmann
  0 siblings, 1 reply; 25+ messages in thread
From: Eric Cousineau @ 2013-03-04  8:41 UTC (permalink / raw)
  To: git

[-- Attachment #1: Type: text/plain, Size: 1483 bytes --]

In this patch, foreach --recursive acts depth-first, much like the default
behavior described in the patch by Imram Yousuf in this
post <http://marc.info/?l=git&m=121066084508631&w=2>.
Changes were made so that the submodule "Entering ..." message was right
next to the output generated by the command too.
It also adds the --parent option for executing the command in the
supermodule as well.

I began by adding a --depth option, to preserve the original --recursive
behavior, and the --parent option, and trying to get that to work. However,
I pretty much confused myself for a while trying to straighten that out, so
I just ended up modifying the --recursive behavior.
If the --recursive behavior should be preserved, I could add the --depth
option back and only have --parent affect non-recursive and --depth
recursive behavior.

I had kind-of implemented this behavior with aliases / bash functions
(posted to pastebin <http://pastebin.com/yLHe9XWy>
, spurned by a
question I asked in StackOverflow <http://stackoverflow.com/q/14846967/170413>),
however I would always run into issues with escaping characters when
passing from the bash functions to git aliases (i.e., putting "'ello" as an
test commit message). I also tried out mb14's method from the StackOverflow
post, but I ran into the same issues.
Figured the best way to avoid that was to cut out the extra layers.

I've attached a test script to generate the tree that VonC suggested with
output showing the iteration.

[-- Attachment #2: 0001-area-submodules.patch --]
[-- Type: application/octet-stream, Size: 2196 bytes --]

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


[-- Attachment #3: test.sh --]
[-- Type: application/x-sh, Size: 849 bytes --]

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

end of thread, other threads:[~2013-04-14 18:52 UTC | newest]

Thread overview: 25+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-03-04  8:41 [PATCH/RFC] Changing submodule foreach --recursive to be depth-first, --parent option to execute command in supermodule as well Eric Cousineau
2013-03-04 22:15 ` Jens Lehmann
2013-03-04 23:00   ` Junio C Hamano
2013-03-05  5:37     ` Eric Cousineau
2013-03-05  7:59     ` Heiko Voigt
2013-03-05 16:09       ` Junio C Hamano
2013-03-05 16:42         ` Eric Cousineau
2013-03-05 18:34           ` Junio C Hamano
2013-03-05 20:51             ` Jens Lehmann
2013-03-05 21:17               ` Phil Hord
2013-03-09 18:18                 ` Jens Lehmann
2013-03-11 16:46                   ` Heiko Voigt
2013-03-12 16:01                   ` Phil Hord
2013-03-14  6:30                     ` Eric Cousineau
2013-03-18 21:25                       ` Jens Lehmann
2013-03-26  4:03                         ` Eric Cousineau
2013-04-02 20:14                           ` Jens Lehmann
2013-04-13  4:04                             ` [PATCH] submodule foreach: Added in --post-order=<command> and adjusted code per Jens Lehmann's suggestions eacousineau
     [not found]                               ` <CA+aSAWuK9Yhvx-vO1fUteq-K=xOPgxkyeWeHG3UwZuDHsxLzAw@mail.gmail.com>
2013-04-13  4:11                                 ` Eric Cousineau
2013-04-14 18:52                               ` Jens Lehmann
2013-03-18 21:10                     ` [PATCH/RFC] Changing submodule foreach --recursive to be depth-first, --parent option to execute command in supermodule as well Jens Lehmann
2013-03-26  3:56                       ` Eric Cousineau
2013-03-26  4:36                         ` Eric Cousineau
2013-03-26  5:23                       ` Junio C Hamano
2013-03-26  5:25                         ` Junio C Hamano

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).