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

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

From
ECEric Cousineau <eacousineau@gmail.com>
Date
Mar 5, 2013, 05:37 UTC
Message-ID
<51358488.2080005@gmail.com>
In-Reply-To
<7vhakqwz1e.fsf@alter.siamese.dyndns.org>

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

Signed-off-by: Eric Cousineau <eacousineau@gmail.com>
---
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(-)
diff --git a/git-submodule.sh b/git-submodule.sh
index 004c034..652bea0 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] [--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
  }

  #
-- 
1.8.2.rc1.24.g06d67b8.dirty
Previous: Junio C HamanoNext: Heiko Voigt
Message 4 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.