Code

git-submodule: replace duplicated code with a module_list function
[git.git] / git-submodule.sh
index 9228f56bee7fd82d530a6865f1f8669fd9b99d8f..2a3a197d1057dcc73570a9946d5e5d8d9bbcc959 100755 (executable)
@@ -6,7 +6,7 @@
 
 USAGE="[--quiet] [--cached] \
 [add <repo> [-b branch] <path>]|[status|init|update [-i|--init]|summary [-n|--summary-limit <n>] [<commit>]] \
-[--] [<path>...]"
+[--] [<path>...]|[foreach <command>]"
 OPTIONS_SPEC=
 . git-sh-setup
 require_work_tree
@@ -53,6 +53,15 @@ resolve_relative_url ()
        echo "$remoteurl/$url"
 }
 
+#
+# Get submodule info for registered submodules
+# $@ = path to limit submodule list
+#
+module_list()
+{
+       git ls-files --stage -- "$@" | grep '^160000 '
+}
+
 #
 # Map submodule path to submodule name
 #
@@ -198,6 +207,26 @@ cmd_add()
        die "Failed to register submodule '$path'"
 }
 
+#
+# Execute an arbitrary command sequence in each checked out
+# submodule
+#
+# $@ = command to execute
+#
+cmd_foreach()
+{
+       module_list |
+       while read mode sha1 stage path
+       do
+               if test -e "$path"/.git
+               then
+                       say "Entering '$path'"
+                       (cd "$path" && eval "$@") ||
+                       die "Stopping at '$path'; script returned non-zero status."
+               fi
+       done
+}
+
 #
 # Register submodules in .git/config
 #
@@ -226,7 +255,7 @@ cmd_init()
                shift
        done
 
-       git ls-files --stage -- "$@" | grep '^160000 ' |
+       module_list "$@" |
        while read mode sha1 stage path
        do
                # Skip already registered paths
@@ -264,6 +293,7 @@ cmd_update()
        do
                case "$1" in
                -q|--quiet)
+                       shift
                        quiet=1
                        ;;
                -i|--init)
@@ -281,10 +311,9 @@ cmd_update()
                        break
                        ;;
                esac
-               shift
        done
 
-       git ls-files --stage -- "$@" | grep '^160000 ' |
+       module_list "$@" |
        while read mode sha1 stage path
        do
                name=$(module_name "$path") || exit
@@ -549,7 +578,7 @@ cmd_status()
                shift
        done
 
-       git ls-files --stage -- "$@" | grep '^160000 ' |
+       module_list "$@" |
        while read mode sha1 stage path
        do
                name=$(module_name "$path") || exit
@@ -583,7 +612,7 @@ cmd_status()
 while test $# != 0 && test -z "$command"
 do
        case "$1" in
-       add | init | update | status | summary)
+       add | foreach | init | update | status | summary)
                command=$1
                ;;
        -q|--quiet)