Changing submodule foreach --recursive to be depth-first, --parent option to execute command in supermodule as well
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.
From 851d65fcfb8f49131428a57fc318af7b56416430 Mon Sep 17 00:00:00 2001
From: eacousineau <[email protected]>
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
--- git-submodule.sh | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-)
git-submodule.sh +26 -5
@@ -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 + ) } #
Signature · 1 line
1.8.2.rc1.24.g06d67b8
Please don't attach your patches, see Documentation/SubmittingPatches on how to post patches to this list.
Am 04.03.2013 09:41, schrieb Eric Cousineau:
Quoted 9 lines
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.From reading the linked pages I assume a valid use case you have is:
git submodule foreach --recursive 'git add -A && git commit ...'
This will currently not work because the depth first algorithm of foreach will execute the command /before/ recursing deeper. You'd need it to execute the command /after/ returning from the deeper level (which is what your patch seems to be about).
Quoted 7 lines
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 would rather not change default behavior without having a *very* good reason to do so (and I'm not sure what you need the --depth option for).
What we currently get from your example is:
Entering 'a'
Entering 'a/b'
Entering 'a/b/d'
Entering 'a/c'
Entering 'b'
Entering 'b/d'
Entering 'c'
Entering 'd'
Me thinks this is what most users would expect of a recursion, enter each level before descending into the next.
For your use case you'd need to have:
Entering 'a/b/d'
Entering 'a/b'
Entering 'a/c'
Entering 'a'
Entering 'b/d'
Entering 'b'
Entering 'c'
Entering 'd'
(Please note that this is still depth-first)
I won't object to adding an option to foreach that will execute the command after recursing (but I'm not convinced --parent is a very good name for that).
Quoted 9 lines
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.
It seems to be really hard to do what you have in mind with shell commands or aliases, which is a good point for adding such an option to foreach. But I don't see a reason why we would want to change the current default, which is what your RFC proposes.
Quoted 40 lines · Jens Lehmann
Please don't attach your patches, see
Documentation/SubmittingPatcheson how to post patches to this list.Am 04.03.2013 09:41, schrieb Eric Cousineau:
Quoted 7 lines
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.From reading the linked pages I assume a valid use case you have is:
git submodule foreach --recursive 'git add -A && git commit ...'
This will currently not work because the depth first algorithm of foreach will execute the command /before/ recursing deeper. You'd need it to execute the command /after/ returning from the deeper level (which is what your patch seems to be about).
...
What we currently get from your example is:
Entering 'a'
Entering 'a/b'
Entering 'a/b/d'
...
Entering 'c'
Entering 'd'
Me thinks this is what most users would expect of a recursion, enter each level before descending into the next.For your use case you'd need to have:
Entering 'a/b/d'
Entering 'a/b'
Entering 'a/c'
...
Entering 'c'
Entering 'd'
(Please note that this is still depth-first)
Quoted 2 lines
I won't object to adding an option to foreach that will execute the command after recursing (but I'm not convinced --parent is a very good name for that).
Are you comparing pre-order vs post-order traversal?
Both can be useful depending on what you are trying to achieve. You need a pre-order traversal (i.e. you "visit" and perform some action on the node and then descend into its children) if you need to do some preparation before you visit deeper levels; you need a post-order traversal (i.e. you "visit" and perform some action on the node after you have done all its children) if you know you will be readly only after you are done with all your children.
You can throw in in-order traversal to the mix (i.e. you "visit" and perform some action on the node after visiting some but not all of your children and then continue visiting the remainder of your children), but I do not know what practical value you would get out of it.
So if you want a single boolean to toggle between the current behaviour and the other one, it would be --post-order. But you may at least want to consider pros and cons of allowing users to give two separate commands, one for the pre-order visitation (which is the current "command") and the other for the post-order visitation. Being able to run both might turn out to be useful.
Quoted 6 lines · Junio C Hamano
So if you want a single boolean to toggle between the current behaviour and the other one, it would be --post-order. But you may at least want to consider pros and cons of allowing users to give two separate commands, one for the pre-order visitation (which is the current "command") and the other for the post-order visitation. Being able to run both might turn out to be useful.
I second that. Having a --post-order=<command/script> switch will give us much more flexibility. For ease of use we could allow --post-order without command to switch the meaning of the main command.
So a final solution would have these switches:
git submodule foreach ... [--pre-order[=<command>]] [--post-order[=<command>]] [<command>]
If only --pre-order without argument is given the command will be executed pre-order. If only --post-order the command will be executed post-order. If both are given its an error and so on...
There are some combinations we would need to catch as errors but this design should allow a step by step implementation:
1. just the --post-order switch
2. --post-order with argument switch
3. --pre-order (including argument) for symmetry of usage
Cheers Heiko
Quoted 20 lines · Heiko Voigt
Quoted 6 lines · Junio C Hamano
So if you want a single boolean to toggle between the current behaviour and the other one, it would be --post-order. But you may at least want to consider pros and cons of allowing users to give two separate commands, one for the pre-order visitation (which is the current "command") and the other for the post-order visitation. Being able to run both might turn out to be useful.
I second that. Having a --post-order=<command/script> switch will give us much more flexibility. For ease of use we could allow --post-order without command to switch the meaning of the main command.
So a final solution would have these switches:
git submodule foreach ... [--pre-order[=<command>]] [--post-order[=<command>]] [<command>]
If only --pre-order without argument is given the command will be executed pre-order. If only --post-order the command will be executed post-order. If both are given its an error and so on...
Quoted 6 lines
There are some combinations we would need to catch as errors but this design should allow a step by step implementation:
1. just the --post-order switch
2. --post-order with argument switch
3. --pre-order (including argument) for symmetry of usage
Yeah, I think I can agree with that direction, and Eric's patch could be that first step of the three-step progression, without painting us into a corner we cannot get out of when we want to advance to 2 and 3 later.
I was more interested in the design aspect and I didn't look at the actual patch text, though.
Quoted 29 lines · Junio C Hamano
Quoted 20 lines · Heiko Voigt
Quoted 6 lines · Junio C Hamano
So if you want a single boolean to toggle between the current behaviour and the other one, it would be --post-order. But you may at least want to consider pros and cons of allowing users to give two separate commands, one for the pre-order visitation (which is the current "command") and the other for the post-order visitation. Being able to run both might turn out to be useful.
I second that. Having a --post-order=<command/script> switch will give us much more flexibility. For ease of use we could allow --post-order without command to switch the meaning of the main command.
So a final solution would have these switches:
git submodule foreach ... [--pre-order[=<command>]] [--post-order[=<command>]] [<command>]
If only --pre-order without argument is given the command will be executed pre-order. If only --post-order the command will be executed post-order. If both are given its an error and so on...
Quoted 6 lines
There are some combinations we would need to catch as errors but this design should allow a step by step implementation:
1. just the --post-order switch
2. --post-order with argument switch
3. --pre-order (including argument) for symmetry of usage
Quoted 7 lines
Yeah, I think I can agree with that direction, and Eric's patch could be that first step of the three-step progression, without painting us into a corner we cannot get out of when we want to advance to 2 and 3 later.
I was more interested in the design aspect and I didn't look at the actual patch text, though.
Would these be the correct behaviors of Heiko's implementation?
git submodule foreach # Empty command, pre-order
git submodule foreach --pre-order # Same behavior
git submodule foreach --post-order # Empty command, post-order git submodule foreach 'frotz' # Do 'frotz' pre-order in each submodule git submodule foreach --post-order 'frotz' # Do 'frotz' post-order in each submodule
git submodule foreach --pre-order='frotz' --post-order='shimmy' # Do 'frotz' pre-order and 'shimmy' post-order in each submodule git submodule foreach --post-order='shimmy' 'frotz' # Invalid usage of the command
git submodule foreach --post-order --pre-order #
It should not be too hard to have this functionality affect the --include-super command as well.
And would it be worth it to abstract this traversal to expose it to other commands, such as 'update', to consolidate the code some? I think Imram was doing something like that in his post.
- Eric
Quoted 1 line · Eric Cousineau
Would these be the correct behaviors of Heiko's implementation?
I do not think Heiko already has an implementation, but let's try to see how each example makes sense.
Quoted 3 lines
git submodule foreach # Empty command, pre-order
git submodule foreach --pre-order # Same behavior git submodule foreach --post-order # Empty command, post-order
OK. The last one shows "I am here" output differently from the other two, but otherwise they are all no-op.
Quoted 1 line
git submodule foreach 'frotz' # Do 'frotz' pre-order in each submodule
OK. And it would be the same if you said either one of:
git submodule foreach --pre-order 'frotz' git submodule foreach --pre-order='frotz'
Quoted 2 lines
git submodule foreach --post-order 'frotz' # Do 'frotz' post-order in each submodule
OK.
Quoted 2 lines
git submodule foreach --pre-order='frotz' --post-order='shimmy' # Do 'frotz' pre-order and 'shimmy' post-order in each submodule
OK.
Quoted 2 lines
git submodule foreach --post-order='shimmy' 'frotz' # Invalid usage of the command
I would expect this to behave exactly the same as:
git submodule foreach \
--post-order=shimmy \
--pre-order=frotzQuoted 1 line
git submodule foreach --post-order --pre-order #
I expect it to behave exactly the same as:
git submodule foreach --post-order=: --pre-order=:
Quoted 2 lines
It should not be too hard to have this functionality affect the --include-super command as well.
I would assume that
git submodule foreach --pre-order=A --post-order=B --include-super
would be identical to running
A &&
git submodule foreach --pre-order=A --post-order=B &&
BI am not entirely convinced we would want --include-super in the first place, though. It does not belong to "submodule foreach"; it is doing something _outside_ the submoudules.
Am 05.03.2013 19:34, schrieb Junio C Hamano:
Quoted 40 lines
Quoted 1 line · Eric Cousineau
Would these be the correct behaviors of Heiko's implementation?
I do not think Heiko already has an implementation, but let's try to see how each example makes sense.
Quoted 3 lines
git submodule foreach # Empty command, pre-order
git submodule foreach --pre-order # Same behavior git submodule foreach --post-order # Empty command, post-orderOK. The last one shows "I am here" output differently from the other two, but otherwise they are all no-op.
Quoted 1 line
git submodule foreach 'frotz' # Do 'frotz' pre-order in each submodule
OK. And it would be the same if you said either one of:
git submodule foreach --pre-order 'frotz' git submodule foreach --pre-order='frotz'Quoted 2 lines
git submodule foreach --post-order 'frotz' # Do 'frotz' post-order in each submodule
OK.
Quoted 2 lines
git submodule foreach --pre-order='frotz' --post-order='shimmy' # Do 'frotz' pre-order and 'shimmy' post-order in each submodule
OK.
Quoted 2 lines
git submodule foreach --post-order='shimmy' 'frotz' # Invalid usage of the command
I would expect this to behave exactly the same as:
git submodule foreach \ --post-order=shimmy \ --pre-order=frotz
Quoted 5 lines
Quoted 1 line
git submodule foreach --post-order --pre-order #
I expect it to behave exactly the same as:
git submodule foreach --post-order=: --pre-order=:
I'd favor to just drop the --pre-order option and do this:
foreach [--recursive] [--post-order <command>] [<command>]
Me thinks pre-order is a sane default and we shouldn't add an explicit option for that. And even with current Git you can simply give no command at all and it'll show you all the submodules it enters without doing anything in them, so we'd only need to add the --post-order handling anyway (and fix the synopsis by adding square brackets around the command while at it, as that is optional).
Quoted 9 lines
Quoted 2 lines
It should not be too hard to have this functionality affect the --include-super command as well.
I would assume that
git submodule foreach --pre-order=A --post-order=B --include-superwould be identical to running
Quoted 7 lines
A && git submodule foreach --pre-order=A --post-order=B && BI am not entirely convinced we would want --include-super in the first place, though. It does not belong to "submodule foreach"; it is doing something _outside_ the submoudules.
I totally agree with that. First, adding --include-super does not belong into the --post-order patch at all, as that is a different topic (even though it belongs to the same use case Eric has). Also the reason why we are thinking about adding the --post-order option IMO cuts the other way for --include-super: It is so easy to do that yourself I'm not convinced we should add an extra option to foreach for that, especially as it has nothing to do with submodules. So I think we should just drop --include-super.
Quoted 7 lines · Jens Lehmann
Am 05.03.2013 19:34, schrieb Junio C Hamano:
Quoted 5 lines
Quoted 1 line · Eric Cousineau
...
I am not entirely convinced we would want --include-super in the first place, though. It does not belong to "submodule foreach"; it is doing something _outside_ the submoudules.
Quoted 8 lines
I totally agree with that. First, adding --include-super does not belong into the --post-order patch at all, as that is a different topic (even though it belongs to the same use case Eric has). Also the reason why we are thinking about adding the --post-order option IMO cuts the other way for --include-super: It is so easy to do that yourself I'm not convinced we should add an extra option to foreach for that, especially as it has nothing to do with submodules. So I think we should just drop --include-super.
I agree it should not be part of this commit, but I've often found
myself in need of an --include-super switch. To me,
git-submodule-foreach means "visit all my .git repos in this project and execute $cmd". It's a pity that the super-project is considered a second-class citizen in this regard.
I have to do this sometimes:
${cmd} && git submodule foreach --recursive '${cmd}'I often forget the first part in scripts, though, and I've seen others do it too. I usually create a function for it in git-heavy scripts.
In a shell, it usually goes like this:
git submodule foreach --recursive '${cmd}'
<up><home><del>{30-ish}<end><backspace><enter>It'd be easier if I could just include a switch for this, and maybe even create an alias for it. But maybe this is different command altogether.
Am 05.03.2013 22:17, schrieb Phil Hord:
Quoted 17 lines · Jens Lehmann
Quoted 7 lines
Am 05.03.2013 19:34, schrieb Junio C Hamano:
Quoted 5 lines
Quoted 1 line · Eric Cousineau
...
I am not entirely convinced we would want --include-super in the first place, though. It does not belong to "submodule foreach"; it is doing something _outside_ the submoudules.
Quoted 8 lines
I totally agree with that. First, adding --include-super does not belong into the --post-order patch at all, as that is a different topic (even though it belongs to the same use case Eric has). Also the reason why we are thinking about adding the --post-order option IMO cuts the other way for --include-super: It is so easy to do that yourself I'm not convinced we should add an extra option to foreach for that, especially as it has nothing to do with submodules. So I think we should just drop --include-super.
Quoted 5 lines
I agree it should not be part of this commit, but I've often found
myself in need of an --include-super switch. To me,
git-submodule-foreach means "visit all my .git repos in this project and execute $cmd". It's a pity that the super-project is considered a second-class citizen in this regard.
Hmm, for me the super-project is a very natural second-class citizen to "git *submodule* foreach". But also I understand that sometimes the user wants to apply a command to superproject and submodules alike (I just recently did exactly that with "git gc" on our build server).
Quoted 7 lines
I have to do this sometimes:
${cmd} && git submodule foreach --recursive '${cmd}'I often forget the first part in scripts, though, and I've seen others do it too. I usually create a function for it in git-heavy scripts.
Quoted 8 lines
In a shell, it usually goes like this:
git submodule foreach --recursive '${cmd}' <up><home><del>{30-ish}<end><backspace><enter>It'd be easier if I could just include a switch for this, and maybe even create an alias for it. But maybe this is different command altogether.
Are you sure you wouldn't forget to provide such a switch too? ;-)
I'm still not convinced we should add a new switch, as it can easily be achieved by adding "${cmd} &&" to your scripts. And on the command line you could use an alias like this one to achieve that:
[alias]
recurse = !sh -c \"$@ && git submodule foreach --recursive $@\"
Quoted 10 lines · Jens Lehmann
Am 05.03.2013 22:17, schrieb Phil Hord:
Quoted 8 lines
In a shell, it usually goes like this:
git submodule foreach --recursive '${cmd}' <up><home><del>{30-ish}<end><backspace><enter>It'd be easier if I could just include a switch for this, and maybe even create an alias for it. But maybe this is different command altogether.
Quoted 8 lines
Are you sure you wouldn't forget to provide such a switch too? ;-)
I'm still not convinced we should add a new switch, as it can easily be achieved by adding "${cmd} &&" to your scripts. And on the command line you could use an alias like this one to achieve that:
[alias]
recurse = !sh -c \"$@ && git submodule foreach --recursive $@\"
I also think it would be useful to have a switch (or even configuration) to include the superproject.
The following (quite typical) use cases come to my mind:
# Assuming some not yet existing configuration values git config submodule.recursive true
git config submodule.includeSuper true
# commit your work over the whole tree into one branch git submodule foreach git checkout -b hv/my-super-cool-feature git submodule foreach --post-order git commit -a -m "DRAFT: finished work for today" git submodule foreach git push hvoigt hv/my-super-cool-feature
# cleanup git submodule foreach git clean -xfd
# reset git submodule foreach git reset --hard
...
Assuming you have a submodule heavy project and you work on multiple submodules including the superproject. These are quite typical commands you would use during development of your feature I imagine. Once you are finished you need to get your feature upstream by the individual submodule rules.
On a feature branch during development there is nothing wrong in simply doing full cross-submodule project commits.
At some point we will probably extend the above commands with a --recurse-submodules switch but until then this is a good substitute so why not have a --include-super maybe even as a configuration option ?
Cheers Heiko
Quoted 38 lines · Jens Lehmann
Am 05.03.2013 22:17, schrieb Phil Hord:
Quoted 17 lines · Jens Lehmann
Quoted 7 lines
Am 05.03.2013 19:34, schrieb Junio C Hamano:
Quoted 5 lines
Quoted 1 line · Eric Cousineau
...
I am not entirely convinced we would want --include-super in the first place, though. It does not belong to "submodule foreach"; it is doing something _outside_ the submoudules.
Quoted 8 lines
I totally agree with that. First, adding --include-super does not belong into the --post-order patch at all, as that is a different topic (even though it belongs to the same use case Eric has). Also the reason why we are thinking about adding the --post-order option IMO cuts the other way for --include-super: It is so easy to do that yourself I'm not convinced we should add an extra option to foreach for that, especially as it has nothing to do with submodules. So I think we should just drop --include-super.
Quoted 5 lines
I agree it should not be part of this commit, but I've often found
myself in need of an --include-super switch. To me,
git-submodule-foreach means "visit all my .git repos in this project and execute $cmd". It's a pity that the super-project is considered a second-class citizen in this regard.Hmm, for me the super-project is a very natural second-class citizen to "git *submodule* foreach". But also I understand that sometimes the user wants to apply a command to superproject and submodules alike (I just recently did exactly that with "git gc" on our build server).
Quoted 9 lines
I have to do this sometimes:
${cmd} && git submodule foreach --recursive '${cmd}'I often forget the first part in scripts, though, and I've seen others do it too. I usually create a function for it in git-heavy scripts.
In a shell, it usually goes like this:
Quoted 8 lines
Quoted 6 lines
git submodule foreach --recursive '${cmd}' <up><home><del>{30-ish}<end><backspace><enter>It'd be easier if I could just include a switch for this, and maybe even create an alias for it. But maybe this is different command altogether.
Are you sure you wouldn't forget to provide such a switch too? ;-)
No. However, when I remember to add the switch, my shell history will remember it for me. This does not happen naturally for me in the "<up><home><del>{30-ish}..." workflow.
I also hope this switch grows up into a configuration option someday. Or maybe a completely different command, like I said before; because I actually think it could be dangerous as a configuration option since it would have drastic consequences for users executing scripts or commands in other users' environments.
Quoted 6 lines
I'm still not convinced we should add a new switch, as it can easily be achieved by adding "${cmd} &&" to your scripts. And on the command line you could use an alias like this one to achieve that:
[alias]
recurse = !sh -c \"$@ && git submodule foreach --recursive $@\"
Yes, making the feature itself a 2nd-class citizen. :-)
But this alias also denies me the benefit of the --post-order option. For 'git recurse git push', for example, I wouldn't want the superproject push to occur first; I would want it to occur last after the submodules have been successfully pushed.
I agree this should go in some other commit, but I do not think it is so trivial it should never be considered as a feature for git. That's all I'm trying to say.
Phil
From 59fb432e17a1aae9de26bbaaca7f09cc7f03b471 Mon Sep 17 00:00:00 2001
From: Eric Cousineau <[email protected]>
Date: Thu, 14 Mar 2013 01:19:53 -0500
Subject: [PATCH] submodule-foreach: Added in --post-order=<command> per Jens
Lehmann's suggestion
---
Made the scope of the patch only relate to --post-order. Would we want to rename this to just --post=<command> ?
Anywho, here it is running in a test setup, where the structure is:
a
- b
- - d
- c
$ git submodule foreach --recursive --post-order 'echo Post $name' 'echo Pre $path' Entering 'b' Pre b Entering 'b/d' Pre d Entering 'b/d' Post d Entering 'b' Post b Entering 'c' Pre c Entering 'c' Post c
An interesting note is that it fails with 'git submodule foreach --post-order', but not 'git submodule foreach --post-order=', since it simply interprets that as an empty command. If that is important, I could add in a check for $# when parsing the argument for --post-order=*.
git-submodule.sh | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-)
git-submodule.sh +0 -0
@@ -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".
+ # Gratuitous (empty) local's to prevent recursive bleeding
+ local 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
;;
@@ -453,7 +464,7 @@ cmd_foreach()
shift
done- toplevel=$(pwd)
+ local toplevel=$(pwd)
# dup stdin so that it can be restored when running the external
# command in the subshell (and a recursive call to this function)
@@ -465,18 +476,36 @@ cmd_foreach()
die_if_unmatched "$mode"
if test -e "$sm_path"/.git
then
- say "$(eval_gettext "Entering '\$prefix\$sm_path'")"
+ local name prefix path message epitaph
+ message="$(eval_gettext "Entering '\$prefix\$sm_path'")"
+ epitaph="$(eval_gettext "Stopping at '\$sm_path'; script returned non-zero status.")"
name=$(module_name "$sm_path")
(
prefix="$prefix$sm_path/"
clear_local_git_env
# we make $path available to scripts ...
path=$sm_path
+
+ sm_eval() {
+ say "$message"
+ eval "$@" || die "$epitaph"
+ }
+
cd "$sm_path" &&
- eval "$@" &&
+ sm_eval "$@" &&
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
+ sm_eval "$post_order"
fi
) <&3 3<&- ||
die "$(eval_gettext "Stopping at '\$sm_path'; script
returned non-zero status.")"Signature · 1 line
1.8.2.rc1.24.g06d67b8.dirty
Thanks, just a quick review before I find some time do take a deeper look.
Am 14.03.2013 07:30, schrieb Eric Cousineau:
Quoted 10 lines
From
59fb432e17a1aae9de26bbaaca7f09cc7f03b471Mon Sep 17 00:00:00 2001
From: Eric Cousineau <[email protected]>
Date: Thu, 14 Mar 2013 01:19:53 -0500
Subject: [PATCH] submodule-foreach: Added in --post-order=<command> per Jens
Lehmann's suggestionSigned-off-by Eric Cousineau <[email protected]>---
Made the scope of the patch only relate to --post-order. Would we want to rename this to just --post=<command> ?
Hmm, while having no strong preference on that, "post order" looks more like the correct term describing what we do here.
Quoted 6 lines
Anywho, here it is running in a test setup, where the structure is:
a
- b
- - d
- c
Quoted 13 lines
$ git submodule foreach --recursive --post-order 'echo Post $name' 'echo Pre $path' Entering 'b' Pre b Entering 'b/d' Pre d Entering 'b/d' Post d Entering 'b' Post b Entering 'c' Pre c Entering 'c' Post c
Looking good.
Quoted 11 lines
An interesting note is that it fails with 'git submodule foreach --post-order', but not 'git submodule foreach --post-order=', since it simply interprets that as an empty command. If that is important, I could add in a check for $# when parsing the argument for --post-order=*.
git-submodule.sh | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-)git-submodule.sh +0
-0
git-submodule.sh USAGE="[--quiet] add [-b <branch>] [-f|--force] [--name <name>] [--reference <re · line 13
Quoted 5 lines
git-submodule.sh +1
-1@@ -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>
Maybe drop the '=' from the description (see --reference for an example)?
git-submodule.sh Use -f if you really want to add it." >&2 · line 438
Quoted 9 lines
git-submodule.sh +2
-0or: $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". + # Gratuitous (empty) local's to prevent recursive bleeding + local recursive= post_order=
Wouldn't it be sufficient to add "post_order=" to the top of the file where "recursive" is already initialized? Or am I missing something here?
Quoted 20 lines
git-submodule.sh +9
-0while 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 ;;
git-submodule.sh cmd_foreach() · line 467
Quoted 5 lines
git-submodule.sh +1
-1@@ -453,7 +464,7 @@ cmd_foreach() shift done - toplevel=$(pwd) + local toplevel=$(pwd)
Why do you have to add the "local" keyword here?
git-submodule.sh cmd_foreach() · line 479
Quoted 8 lines
git-submodule.sh +1
-1# dup stdin so that it can be restored when running the external # command in the subshell (and a recursive call to this function)@@ -465,18 +476,36 @@ cmd_foreach() die_if_unmatched "$mode" if test -e "$sm_path"/.git then - say "$(eval_gettext "Entering '\$prefix\$sm_path'")" + local name prefix path message epitaph
Same here?
Quoted 13 lines
git-submodule.sh +7
-0+ message="$(eval_gettext "Entering '\$prefix\$sm_path'")" + epitaph="$(eval_gettext "Stopping at '\$sm_path'; script returned non-zero status.")" name=$(module_name "$sm_path") ( prefix="$prefix$sm_path/" clear_local_git_env # we make $path available to scripts ... path=$sm_path + + sm_eval() { + say "$message" + eval "$@" || die "$epitaph" + }
git-submodule.sh cmd_foreach() · line 500
Quoted 10 lines
git-submodule.sh +5
-2+ cd "$sm_path" && - eval "$@" && + sm_eval "$@" && if test -n "$recursive" then - cmd_foreach "--recursive" "$@" + if test -n "$post_order" + then + # Tried keeping flags as a variable, but was having difficulty
Maybe because you set the "post_order" variable to empty at the beginning of this function? If I read that right moving that initialization to the top of the file could get rid of the if here?
Quoted 11 lines
git-submodule.sh +8
-0+ cmd_foreach --recursive --post-order "$post_order" "$@" + else + cmd_foreach --recursive "$@" + fi + fi && + if test -n "$post_order" + then + sm_eval "$post_order" fi ) <&3 3<&- || die "$(eval_gettext "Stopping at '\$sm_path'; script returned non-zero status.")"
From 2c2923ada809d671828aa58dcda05a1b71222b70 Mon Sep 17 00:00:00 2001
From: Eric Cousineau <[email protected]>
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
---
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(-)
git-submodule.sh +0 -0
@@ -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
}Signature · 1 line
1.7.9.5
Seems were getting closer, some comments from a quick read of your patch below.
Am 26.03.2013 05:03, schrieb Eric Cousineau:
Quoted 6 lines
From
2c2923ada809d671828aa58dcda05a1b71222b70Mon Sep 17 00:00:00 2001
From: Eric Cousineau <[email protected]>
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
Quoted 6 lines
Signed-off-by Eric Cousineau <[email protected]>---
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".
That's better than "Stopping", but while I'm not a native speaker I'd propose to use "Leaving" as the opposite of "Entering".
Quoted 36 lines
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 cgit-submodule.sh | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-)git-submodule.sh +1
-1@@ -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
git-submodule.sh Use -f if you really want to add it." >&2 · line 438
Quoted 5 lines
git-submodule.sh +2
-0@@ -434,6 +434,8 @@ Use -f if you really want to add it." >&2 cmd_foreach() { # parse $args after "submodule ... foreach". + recursive= + post_order=
I'm still not sure we need that here, in fact the problem you have with the cmd_foreach invocation below might just be because you reset these variables here instead of once at the top of this file.
Quoted 59 lines
git-submodule.sh +27
-4while 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 }
PATCH 1/1 submodule foreach: Added in --post-order=<command> and adjusted code per Jens Lehmann's suggestions
---
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(-)
git-submodule.sh +26 -5
@@ -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 }
Signature · 1 line
1.8.2.1.390.gd4ee029
A message that is not in the archive (sent off-list or not yet received): CA+aSAWuK9Yhvx-vO1fUteq-K=xOPgxkyeWeHG3UwZuDHsxLzAw@mail.gmail.com
Re: [PATCH] submodule foreach: Added in --post-order=<command> and adjusted code per Jens Lehmann's suggestions
Had accidentally sent this as HTML, resending as plain-text.
Quoted 114 lines · Eric Cousineau
Oops... I tried out using git-send-email adding in the Message-Id, but forgot to change the title as well. My bad.
This was in response to:
[PATCH/RFC] Changing submodule foreach --recursive to be depth-first, --parent option to execute command in supermodule as well
Message-Id: <[email protected]>- Eric
Quoted 101 lines · eacousineau
Signed-off-by eacousineau <[email protected]>---
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(-)git-submodule.sh +26
-6@@ -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
Re: [PATCH] submodule foreach: Added in --post-order=<command> and adjusted code per Jens Lehmann's suggestions
In reply to eacousineauAm 13.04.2013 06:04, schrieb eacousineau:
Quoted 4 lines
Signed-off-by eacousineau <[email protected]>---
I see what you meant by the extra variables, so I've fixed that so the original flags aren't needed with recursion.
Thanks, the code is looking much better now and you nicely described the changes you made since the last version. A few comments though:
I think the subject line should read:
[PATCH v2] submodule foreach: Add --post-order option
We use the imperative form, also the adjustments are a normal part of the review process and don't need to be mentioned explicitly in the title, just show the version of your iteration by adding "v2" after the word "PATCH" (and of course the next iteration will be "v3" ;-).
The commit message is not explaining what you did and why you did it, please see the "Describe your changes well." section in Documentation/SubmittingPatches on how to do that.
And you'll also want to add the new option to the man page in Documentation/git-submodule.txt.
Quoted 2 lines
Also updated it to not
print the entering command if there is only a post-order command.
Good idea.
Quoted 16 lines
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
Quoted 4 lines
$ git submodule foreach --recursive --post-order : Leaving 'b/d' Leaving 'b' Leaving 'c'
Makes sense to me. These two examples should be getting a test case each in t/t7407-submodule-foreach.sh.
Quoted 33 lines
git-submodule.sh | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-)git-submodule.sh +10
-1@@ -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 ;;
git-submodule.sh cmd_foreach() · line 484
Quoted 6 lines
git-submodule.sh +2
-1@@ -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'")"
I don't think we gain much by putting enter_msg and leave_msg into their own variables as they are only used once, I'd prefer to see these messages inlined.
git-submodule.sh cmd_foreach() · line 485
Quoted 1 line
git-submodule.sh +1
-0+ die_msg="$(eval_gettext "Stopping at '\$sm_path'; script returned non-zero status.")"
I think there is a \$prefix missing in front of the \$sm_path here (see enter_msg and leave_msg). As you only copied that message you can simply say in the commit message "While at it also fix a missing prefix in the die message" at the end of the last paragraph.
Quoted 8 lines
git-submodule.sh +0
-1name=$(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 "$@" &&
git-submodule.sh cmd_foreach() · line 501
Quoted 10 lines
git-submodule.sh +7
-1+ 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 "$@"
You should at least state that you dropped the --recursive here on purpose, just add that to the "While at it ..." sentence. And I suspect the comment above is more a reminder for yourself, we could drop that too.
Quoted 13 lines
git-submodule.sh +6
-1+ 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 }
Am 12.03.2013 17:01, schrieb Phil Hord:
Quoted 46 lines · Jens Lehmann
Quoted 45 lines
Am 05.03.2013 22:17, schrieb Phil Hord:
Quoted 17 lines · Jens Lehmann
Quoted 7 lines
Am 05.03.2013 19:34, schrieb Junio C Hamano:
Quoted 5 lines
Quoted 1 line · Eric Cousineau
...
I am not entirely convinced we would want --include-super in the first place, though. It does not belong to "submodule foreach"; it is doing something _outside_ the submoudules.
Quoted 8 lines
I totally agree with that. First, adding --include-super does not belong into the --post-order patch at all, as that is a different topic (even though it belongs to the same use case Eric has). Also the reason why we are thinking about adding the --post-order option IMO cuts the other way for --include-super: It is so easy to do that yourself I'm not convinced we should add an extra option to foreach for that, especially as it has nothing to do with submodules. So I think we should just drop --include-super.
Quoted 5 lines
I agree it should not be part of this commit, but I've often found
myself in need of an --include-super switch. To me,
git-submodule-foreach means "visit all my .git repos in this project and execute $cmd". It's a pity that the super-project is considered a second-class citizen in this regard.Hmm, for me the super-project is a very natural second-class citizen to "git *submodule* foreach". But also I understand that sometimes the user wants to apply a command to superproject and submodules alike (I just recently did exactly that with "git gc" on our build server).
Quoted 15 lines
I have to do this sometimes:
${cmd} && git submodule foreach --recursive '${cmd}'I often forget the first part in scripts, though, and I've seen others do it too. I usually create a function for it in git-heavy scripts.
In a shell, it usually goes like this:
git submodule foreach --recursive '${cmd}' <up><home><del>{30-ish}<end><backspace><enter>It'd be easier if I could just include a switch for this, and maybe even create an alias for it. But maybe this is different command altogether.
Quoted 5 lines
Quoted 1 line
Are you sure you wouldn't forget to provide such a switch too? ;-)
No. However, when I remember to add the switch, my shell history will remember it for me. This does not happen naturally for me in the "<up><home><del>{30-ish}..." workflow.
I started to use '&&' in my daily shell work for exactly that reason:
that the bash history remembers groups of two or more commands for me.
Quoted 5 lines
I also hope this switch grows up into a configuration option someday. Or maybe a completely different command, like I said before; because I actually think it could be dangerous as a configuration option since it would have drastic consequences for users executing scripts or commands in other users' environments.
I agree on the possible problems a configuration option introduces.
Quoted 7 lines
Quoted 6 lines
I'm still not convinced we should add a new switch, as it can easily be achieved by adding "${cmd} &&" to your scripts. And on the command line you could use an alias like this one to achieve that:
[alias]
recurse = !sh -c \"$@ && git submodule foreach --recursive $@\"
Quoted 6 lines
Yes, making the feature itself a 2nd-class citizen. :-)
But this alias also denies me the benefit of the --post-order option. For 'git recurse git push', for example, I wouldn't want the superproject push to occur first; I would want it to occur last after the submodules have been successfully pushed.
[alias]
recurse-post = !sh -c \"git submodule foreach --recursive --post-order $@ && $@\"
;-)
Quoted 3 lines
I agree this should go in some other commit, but I do not think it is so trivial it should never be considered as a feature for git. That's all I'm trying to say.
I am not against adding such a functionality to Git, I'm just not convinced "git submodule foreach" is the right command for that. I suspect the "git for-each-repo" Lars proposed earlier this year might be a better choice, as that could also recurse into other repos which aren't registered as submodules. And a "for-each-repo" to me looks like a command which could include the superproject too (at least when told to do so with an option).
Quoted 56 lines · Jens Lehmann
Am 12.03.2013 17:01, schrieb Phil Hord:
Quoted 46 lines · Jens Lehmann
Quoted 45 lines
Am 05.03.2013 22:17, schrieb Phil Hord:
Quoted 17 lines · Jens Lehmann
Quoted 7 lines
Am 05.03.2013 19:34, schrieb Junio C Hamano:
Quoted 5 lines
Quoted 1 line · Eric Cousineau
...
I am not entirely convinced we would want --include-super in the first place, though. It does not belong to "submodule foreach"; it is doing something _outside_ the submoudules.
Quoted 8 lines
I totally agree with that. First, adding --include-super does not belong into the --post-order patch at all, as that is a different topic (even though it belongs to the same use case Eric has). Also the reason why we are thinking about adding the --post-order option IMO cuts the other way for --include-super: It is so easy to do that yourself I'm not convinced we should add an extra option to foreach for that, especially as it has nothing to do with submodules. So I think we should just drop --include-super.
Quoted 5 lines
I agree it should not be part of this commit, but I've often found
myself in need of an --include-super switch. To me,
git-submodule-foreach means "visit all my .git repos in this project and execute $cmd". It's a pity that the super-project is considered a second-class citizen in this regard.Hmm, for me the super-project is a very natural second-class citizen to "git *submodule* foreach". But also I understand that sometimes the user wants to apply a command to superproject and submodules alike (I just recently did exactly that with "git gc" on our build server).
Quoted 15 lines
I have to do this sometimes:
${cmd} && git submodule foreach --recursive '${cmd}'I often forget the first part in scripts, though, and I've seen others do it too. I usually create a function for it in git-heavy scripts.
In a shell, it usually goes like this:
git submodule foreach --recursive '${cmd}' <up><home><del>{30-ish}<end><backspace><enter>It'd be easier if I could just include a switch for this, and maybe even create an alias for it. But maybe this is different command altogether.
Quoted 5 lines
Quoted 1 line
Are you sure you wouldn't forget to provide such a switch too? ;-)
No. However, when I remember to add the switch, my shell history will remember it for me. This does not happen naturally for me in the "<up><home><del>{30-ish}..." workflow.
I started to use '&&' in my daily shell work for exactly that reason:
that the bash history remembers groups of two or more commands for me.
Quoted 15 lines
Quoted 5 lines
I also hope this switch grows up into a configuration option someday. Or maybe a completely different command, like I said before; because I actually think it could be dangerous as a configuration option since it would have drastic consequences for users executing scripts or commands in other users' environments.
I agree on the possible problems a configuration option introduces.
Quoted 7 lines
Quoted 6 lines
I'm still not convinced we should add a new switch, as it can easily be achieved by adding "${cmd} &&" to your scripts. And on the command line you could use an alias like this one to achieve that:
[alias]
recurse = !sh -c \"$@ && git submodule foreach --recursive $@\"
I tried this and the 'recurse-post' alias, but could not get it to function as it does inside of 'git submodule foreach'. I also tried out some different escaping methods, but nothing seemed to work. I've added the examples below.
Quoted 15 lines
Quoted 6 lines
Yes, making the feature itself a 2nd-class citizen. :-)
But this alias also denies me the benefit of the --post-order option. For 'git recurse git push', for example, I wouldn't want the superproject push to occur first; I would want it to occur last after the submodules have been successfully pushed.
[alias]
recurse-post = !sh -c \"git submodule foreach --recursive --post-order $@ && $@\";-)
Quoted 3 lines
I agree this should go in some other commit, but I do not think it is so trivial it should never be considered as a feature for git. That's all I'm trying to say.
Quoted 8 lines
I am not against adding such a functionality to Git, I'm just not convinced "git submodule foreach" is the right command for that. I suspect the "git for-each-repo" Lars proposed earlier this year might be a better choice, as that could also recurse into other repos which aren't registered as submodules. And a "for-each-repo" to me looks like a command which could include the superproject too (at least when told to do so with an option).
Here are the aliases I am using:
[alias]
recurse = !sh -c \"$@ && git submodule foreach --recursive $@\" recurse-post = !sh -c \"git submodule foreach --recursive --post-order $@ && $@\" fer = !sh -c \"eval \\\"$@\\\" && git submodule foreach --recursive \\\"$@\\\"\" ferpo = !sh -c \"git submodule foreach --recursive --post-order \\\"$@\\\" && eval \\\"$@\\\"\" fers = !sh -c \"eval '$@' && git submodule foreach --recursive '$@'\" ferpos = !sh -c \"git submodule foreach --recursive --post-order '$@' && eval '$@'\"
And these are the results I get with the following example:
$ cmd="echo \"'ello world: \$PWD\"" $ eval "$cmd" 'ello world: /tmp/a $ git submodule foreach --recursive "$cmd" Entering 'b' 'ello world: /tmp/a/b Entering 'b/d' 'ello world: /tmp/a/b/d Entering 'c' 'ello world: /tmp/a/c $ git submodule foreach --recursive --post-order "$cmd" "$cmd" Entering 'b' 'ello world: /tmp/a/b Entering 'b/d' 'ello world: /tmp/a/b/d Exiting 'b/d' 'ello world: /tmp/a/b/d Exiting 'b' 'ello world: /tmp/a/b Entering 'c' 'ello world: /tmp/a/c Exiting 'c' 'ello world: /tmp/a/c $ git recurse "$cmd" 'ello world: /tmp/a Entering 'b' /home/eacousineau/local/lib/git/libexec/git-core/git-submodule: 1: eval: Syntax error: Unterminated quoted string Stopping at 'b'; script returned non-zero status. $ git recurse-post "$cmd" Entering 'b' /home/eacousineau/local/lib/git/libexec/git-core/git-submodule: 1: eval: Syntax error: Unterminated quoted string Stopping at 'b'; script returned non-zero status. $ git fer "$cmd" ello world: /tmp/a Entering 'b' ello world: /tmp/a Entering 'b/d' ello world: /tmp/a Entering 'c' ello world: /tmp/a $ git ferpo "$cmd" Entering 'b' /home/eacousineau/local/lib/git/libexec/git-core/git-submodule: 1: eval: world:: not found Stopping at 'b'; script returned non-zero status. Stopping at 'b'; script returned non-zero status. $ git fers "$cmd" ello world: /tmp/a' && git submodule foreach --recursive 'echo ello world: /tmp/a $ git ferpos "$cmd" Entering 'b' /home/eacousineau/local/lib/git/libexec/git-core/git-submodule: 1: eval: Syntax error: Unterminated quoted string Stopping at 'b'; script returned non-zero status.
The problem is trying to escape with double-quotes, where the single-quotes are evaluated
as a shell token thing and not as a string argument, versus single-quotes, where you cannot (easily) escape single quotes inside of it (though please correct me if I'm wrong!).
It seems the best solution would be to have it as a script to allow recursion to occur in the scope of one script, like submodule foreach.
I understand now why it does not fit in the scope of 'git submodule', though, so I could implement it as a *very* lightweight stand-in for Lars's "git for-each-repo" via some copy-and-paste :P
- Eric
Quoted 4 lines · Eric Cousineau
Quoted 3 lines · Jens Lehmann
Am 12.03.2013 17:01, schrieb Phil Hord:
Quoted 2 lines · Jens Lehmann
Quoted 1 line
Am 05.03.2013 22:17, schrieb Phil Hord:
...
Quoted 109 lines
Quoted 10 lines
I agree on the possible problems a configuration option introduces.
Quoted 7 lines
Quoted 6 lines
I'm still not convinced we should add a new switch, as it can easily be achieved by adding "${cmd} &&" to your scripts. And on the command line you could use an alias like this one to achieve that:
[alias]
recurse = !sh -c \"$@ && git submodule foreach --recursive $@\"I tried this and the 'recurse-post' alias, but could not get it to function as it does inside of 'git submodule foreach'. I also tried out some different escaping methods, but nothing seemed to work. I've added the examples below.
Quoted 15 lines
Quoted 6 lines
Yes, making the feature itself a 2nd-class citizen. :-)
But this alias also denies me the benefit of the --post-order option. For 'git recurse git push', for example, I wouldn't want the superproject push to occur first; I would want it to occur last after the submodules have been successfully pushed.
[alias]
recurse-post = !sh -c \"git submodule foreach --recursive --post-order $@ && $@\";-)
Quoted 3 lines
I agree this should go in some other commit, but I do not think it is so trivial it should never be considered as a feature for git. That's all I'm trying to say.
Quoted 8 lines
I am not against adding such a functionality to Git, I'm just not convinced "git submodule foreach" is the right command for that. I suspect the "git for-each-repo" Lars proposed earlier this year might be a better choice, as that could also recurse into other repos which aren't registered as submodules. And a "for-each-repo" to me looks like a command which could include the superproject too (at least when told to do so with an option).
Here are the aliases I am using:
[alias]
recurse = !sh -c \"$@ && git submodule foreach --recursive $@\" recurse-post = !sh -c \"git submodule foreach --recursive --post-order $@ && $@\" fer = !sh -c \"eval \\\"$@\\\" && git submodule foreach --recursive \\\"$@\\\"\" ferpo = !sh -c \"git submodule foreach --recursive --post-order \\\"$@\\\" && eval \\\"$@\\\"\" fers = !sh -c \"eval '$@' && git submodule foreach --recursive '$@'\" ferpos = !sh -c \"git submodule foreach --recursive --post-order '$@' && eval '$@'\"And these are the results I get with the following example:
$ cmd="echo \"'ello world: \$PWD\"" $ eval "$cmd" 'ello world: /tmp/a $ git submodule foreach --recursive "$cmd" Entering 'b' 'ello world: /tmp/a/b Entering 'b/d' 'ello world: /tmp/a/b/d Entering 'c' 'ello world: /tmp/a/c $ git submodule foreach --recursive --post-order "$cmd" "$cmd" Entering 'b' 'ello world: /tmp/a/b Entering 'b/d' 'ello world: /tmp/a/b/d Exiting 'b/d' 'ello world: /tmp/a/b/d Exiting 'b' 'ello world: /tmp/a/b Entering 'c' 'ello world: /tmp/a/c Exiting 'c' 'ello world: /tmp/a/c $ git recurse "$cmd" 'ello world: /tmp/a Entering 'b' /home/eacousineau/local/lib/git/libexec/git-core/git-submodule: 1: eval: Syntax error: Unterminated quoted string Stopping at 'b'; script returned non-zero status. $ git recurse-post "$cmd" Entering 'b' /home/eacousineau/local/lib/git/libexec/git-core/git-submodule: 1: eval: Syntax error: Unterminated quoted string Stopping at 'b'; script returned non-zero status. $ git fer "$cmd" ello world: /tmp/a Entering 'b' ello world: /tmp/a Entering 'b/d' ello world: /tmp/a Entering 'c' ello world: /tmp/a $ git ferpo "$cmd" Entering 'b' /home/eacousineau/local/lib/git/libexec/git-core/git-submodule: 1: eval: world:: not found Stopping at 'b'; script returned non-zero status. Stopping at 'b'; script returned non-zero status. $ git fers "$cmd" ello world: /tmp/a' && git submodule foreach --recursive 'echo ello world: /tmp/a $ git ferpos "$cmd" Entering 'b' /home/eacousineau/local/lib/git/libexec/git-core/git-submodule: 1: eval: Syntax error: Unterminated quoted string Stopping at 'b'; script returned non-zero status.The problem is trying to escape with double-quotes, where the single-quotes are evaluated
as a shell token thing and not as a string argument, versus single-quotes, where you cannot (easily) escape single quotes inside of it (though please correct me if I'm wrong!).
It seems the best solution would be to have it as a script to allow recursion to occur in the scope of one script, like submodule foreach.
Quoted 5 lines
I understand now why it does not fit in the scope of 'git submodule', though, so I could implement it as a *very* lightweight stand-in for Lars's "git for-each-repo" via some copy-and-paste :P
- Eric
Put together a script with the --include-super functionality, named it 'git-fer.sh' to start. Posted as a Gist: https://gist.github.com/eacousineau/5243161
That test case:
$ git-fer --include-super --recursive --post-order "$cmd" "$cmd" Entering supermodule 'a' 'ello world: /tmp/a Entering 'b' 'ello world: /tmp/a/b Entering 'b/d' 'ello world: /tmp/a/b/d Exiting 'b/d' 'ello world: /tmp/a/b/d Exiting 'b' 'ello world: /tmp/a/b Entering 'c' 'ello world: /tmp/a/c Exiting 'c' 'ello world: /tmp/a/c Exiting supermodule 'a' 'ello world: /tmp/a
Quoted 19 lines · Jens Lehmann
Am 12.03.2013 17:01, schrieb Phil Hord:
Quoted 18 lines · Jens Lehmann
Quoted 17 lines
Am 05.03.2013 22:17, schrieb Phil Hord:
Quoted 16 lines · Jens Lehmann
Quoted 15 lines
Am 05.03.2013 19:34, schrieb Junio C Hamano:
Quoted 5 lines
Quoted 1 line · Eric Cousineau
...
I am not entirely convinced we would want --include-super in the first place, though. It does not belong to "submodule foreach"; it is doing something _outside_ the submoudules.
I totally agree with that. First, adding --include-super does not belong into the --post-order patch at all, as that is a different topic (even though it belongs to the same use case Eric has). Also the reason why we are thinking about adding the --post-order option IMO cuts the other way for --include-super: It is so easy to do that yourself I'm not convinced we should add an extra option to foreach for that, especially as it has nothing to do with submodules. So I think we should just drop --include-super.
FWIW, after thinking about it a bit more and especially after thinking about the nested submodule layout, I changed my mind.
The reasoning is very simple. In short, your "top-level" may be somebody else's submodule.
If you have a project A, that has a submodule B & C that in turn have submodules D, E & F, G, like this:
A
/ \
B C
/ \ / \
D E F Gyou may want your "submodule foreach [--post-order]" that is run at the top-level to visit B D E C F G (or D E B F G C). A is not a submodule, and it may be rational to do without --also-toplevel option from the point of view of yourself.
But if "submodule foreach [--post-order] B" run at the top-level visits B D E (or D E B), wouldn't it be more natural if you had a way to optionally make this
cd B && submodule foreach [--post-order]
visit the same modules in the same way? The story is the same if your top-level project A is bound at a path in somebody else's project as a submodule. His "submodulle foreach" will visit your top-level A while visiting the hierarchy of your submodules (and other submodules he has as your siblings).
I do not know if foreach should visit your top-level by default; changing that may be too late and too disruptive. But I think an optional "I want this traversal to also visit the top" would not be so _wrong_ even at the conceptual level.
Of course, it may make the implementation simpler, too ;-) foreach could just scan the immediate submodules, chdir into each of them and then run the equivalent foreach with --also-toplevel option, with the same --post-order (or --pre-order) option.
Quoted 5 lines · Junio C Hamano
Jens Lehmann <[email protected]> writes:
...Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
I totally agree with that. First, adding --include-super does not belong into the --post-order patch at all, as that is a different topic (even though it belongs to the same use case Eric has).
I forgot to say that I haven't changed my mind about this part. "Do we visit the top-level?" is an orthogonal and different topic and is better done in a patch separate from the one for --post-order.
Thanks.
git-submodule.sh: In foreach, make '-post-order' yield post-order traversal and '--include-super' execute commands at the top-level supermodule, with both of these options compatible with '--recursive'.
---
Sorry about missing the part about not included MIME attachments, hope this is in a better format now. Jens, I changed the '--parent' option to '--include-super' which is hopefully less vague. Junio, you made an excellent point about both being useful. In particular, I overlooked the case for doing a submodule pull / update (if, for whatever reason, it is more convenient than a submodule update, maybe for merging). In that case, you might want to initialize new submodules and ignore the old ones, instead of wasting time on them with a post-order traversal pull.
I've implemented your suggestions to have a boolean '--post-order' option, and made the '--include-super' option compatible with it. This way, the original behavior of 'foreach' is preserved.
I've updated the test and uploaded it to pastebin: http://pastebin.com/BgZNzFpi
git-submodule.sh | 102 +++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 77 insertions(+), 25 deletions(-)
git-submodule.sh +0 -0
@@ -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] [--include-super] [--post-order] <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".
+ # Gratuitous (empty) local's to prevent recursive bleeding
+ local include_super= recursive= post_order=
while test $# -ne 0
do
case "$1" in
@@ -443,6 +445,12 @@ cmd_foreach()
--recursive)
recursive=1
;;
+ --post-order)
+ post_order=1
+ ;;
+ --include-super)
+ include_super=1
+ ;;
-*)
usage
;;
@@ -453,35 +461,79 @@ cmd_foreach()
shift
done- toplevel=$(pwd) + if test -n "$recursive" + then + local recursive_flags="--recursive" + if test -n "$post_order" + then
+ recursive_flags="$recursive_flags --post-order"
+ fi
+ fi
+
+ local toplevel=$(pwd)
# dup stdin so that it can be restored when running the external
# command in the subshell (and a recursive call to this function)
exec 3<&0
+
+ # Use nested functions
+ super_eval() {
+ 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.")"
+ }- module_list | - while read mode sha1 stage sm_path - do - die_if_unmatched "$mode" - if test -e "$sm_path"/.git - then - say "$(eval_gettext "Entering '\$prefix\$sm_path'")" - name=$(module_name "$sm_path") - ( - prefix="$prefix$sm_path/" - clear_local_git_env - # we make $path available to scripts ... - path=$sm_path - cd "$sm_path" && - eval "$@" && - if test -n "$recursive" - then
- cmd_foreach "--recursive" "$@"
- fi
- ) <&3 3<&- ||
- die "$(eval_gettext "Stopping at '\$sm_path'; script returned non-zero status.")"
- fi
- done + if test -n "$include_super" -a -z "$post_order" + then + super_eval "$@" + fi && + ( + module_list | + while read mode sha1 stage sm_path + do + die_if_unmatched "$mode" + if test -e "$sm_path"/.git + then + local name prefix path message epitaph + message="$(eval_gettext "Entering '\$prefix\$sm_path'")" + epitaph="$(eval_gettext "Stopping at '\$sm_path'; script returned non-zero status.")" + name=$(module_name "$sm_path") + ( + prefix="$prefix$sm_path/" + clear_local_git_env + # we make $path available to scripts ... + path=$sm_path
+
+ sm_eval() {
+ say "$message"
+ eval "$@" || die "$epitaph"
+ }
+
+ cd "$sm_path" && + if test -z "$post_order" + then + sm_eval "$@" + fi && + if test -n "$recursive" + then + cmd_foreach $recursive_flags "$@" + fi && + if test -n "$post_order" + then + sm_eval "$@" + fi + # Since the (...) seems to limit exit's scope, make sure to kill things here if something goes awry + # (the `|| exit 1` at the end) + ) <&3 3<&- || exit 1 + fi + done + ) && + if test -n "$include_super" -a -n "$post_order" + then
+ super_eval "$@"
+ fi
}
#
Signature · 1 line
1.8.2.rc1.24.g06d67b8.dirty