@matheusbsbs / matheus-bagliano / commits / 19a31f9c1a
Fork 0

git-submodule - Add 'foreach' subcommand

submodule foreach <command-list> will execute the list of commands in each currently checked out submodule directory. The list of commands is arbitrary as long as it is acceptable to sh. The variables '$path' and '$sha1' are availble to the command-list, defining the submodule path relative to the superproject and the submodules's commitID as recorded in the superproject (this may be different than HEAD in the submodule). This utility is inspired by a number of threads on the mailing list looking for ways to better integrate submodules in a tree and work with them as a unit. This could include fetching a new branch in each from a given source, or possibly checking out a given named branch in each. Currently, there is no consensus as to what additional commands should be implemented in the porcelain, requiring all users whose needs exceed that of git-submodule to do their own scripting. The foreach command is intended to support such scripting, and in particular does no error checking and produces no output, thus allowing end users complete control over any information printed out and over what constitutes an error. The processing does terminate if the command-list returns an error, but processing can easily be forced for all submodules be terminating the list with ';true'. Signed-off-by: Mark Levedahl <mlevedahl@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Mark Levedahl committed Aug 10, 2008 at 19:10 UTC 19a31f9c1a6b18abd8a7f20d616516afca36a6a3
2 files changed +39 -2
Documentation/git-submodule.txt
+17
@@ -14,6 +14,7 @@ SYNOPSIS
14 'git submodule' [--quiet] init [--] [<path>...]
15 'git submodule' [--quiet] update [--init] [--] [<path>...]
16 'git submodule' [--quiet] summary [--summary-limit <n>] [commit] [--] [<path>...]
17 +'git submodule' [--quiet] foreach <command>
18
19
20 DESCRIPTION
@@ -123,6 +124,22 @@ summary::
124 in the submodule between the given super project commit and the
125 index or working tree (switched by --cached) are shown.
126
127 +foreach::
128 + Evaluates an arbitrary shell command in each checked out submodule.
129 + The command has access to the variables $path and $sha1:
130 + $path is the name of the submodule directory relative to the
131 + superproject, and $sha1 is the commit as recorded in the superproject.
132 + Any submodules defined in the superproject but not checked out are
133 + ignored by this command. Unless given --quiet, foreach prints the name
134 + of each submodule before evaluating the command.
135 + A non-zero return from the command in any submodule causes
136 + the processing to terminate. This can be overridden by adding '|| :'
137 + to the end of the command.
138 ++
139 +As an example, "git submodule foreach 'echo $path `git rev-parse HEAD`' will
140 +show the path and currently checked out commit for each submodule.
141 +
142 +
143 OPTIONS
144 -------
145 -q::
git-submodule.sh
+22 -2
@@ -6,7 +6,7 @@
6
7 USAGE="[--quiet] [--cached] \
8 [add <repo> [-b branch] <path>]|[status|init|update [-i|--init]|summary [-n|--summary-limit <n>] [<commit>]] \
9 -[--] [<path>...]"
9 +[--] [<path>...]|[foreach <command>]"
10 OPTIONS_SPEC=
11 . git-sh-setup
12 require_work_tree
@@ -198,6 +198,26 @@ cmd_add()
198 die "Failed to register submodule '$path'"
199 }
200
201 +#
202 +# Execute an arbitrary command sequence in each checked out
203 +# submodule
204 +#
205 +# $@ = command to execute
206 +#
207 +cmd_foreach()
208 +{
209 + git ls-files --stage | grep '^160000 ' |
210 + while read mode sha1 stage path
211 + do
212 + if test -e "$path"/.git
213 + then
214 + say "Entering '$path'"
215 + (cd "$path" && eval "$@") ||
216 + die "Stopping at '$path'; script returned non-zero status."
217 + fi
218 + done
219 +}
220 +
221 #
222 # Register submodules in .git/config
223 #
@@ -583,7 +603,7 @@ cmd_status()
603 while test $# != 0 && test -z "$command"
604 do
605 case "$1" in
586 - add | init | update | status | summary)
606 + add | foreach | init | update | status | summary)
607 command=$1
608 ;;
609 -q|--quiet)