Skip to content

Commit adc5423

Browse files
committed
Merge branch 'jh/submodule-foreach'
* jh/submodule-foreach: git clone: Add --recursive to automatically checkout (nested) submodules t7407: Use 'rev-parse --short' rather than bash's substring expansion notation git submodule status: Add --recursive to recurse into nested submodules git submodule update: Introduce --recursive to update nested submodules git submodule foreach: Add --recursive to recurse into nested submodules git submodule foreach: test access to submodule name as '$name' Add selftest for 'git submodule foreach' git submodule: Cleanup usage string and add option parsing to cmd_foreach() git submodule foreach: Provide access to submodule name, as '$name' Conflicts: Documentation/git-submodule.txt git-submodule.sh
2 parents ab36d06 + e7fed18 commit adc5423

File tree

5 files changed

+346
-14
lines changed

5 files changed

+346
-14
lines changed

Documentation/git-clone.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ SYNOPSIS
1212
'git clone' [--template=<template_directory>]
1313
[-l] [-s] [--no-hardlinks] [-q] [-n] [--bare] [--mirror]
1414
[-o <name>] [-u <upload-pack>] [--reference <repository>]
15-
[--depth <depth>] [--] <repository> [<directory>]
15+
[--depth <depth>] [--recursive] [--] <repository> [<directory>]
1616

1717
DESCRIPTION
1818
-----------
@@ -147,6 +147,14 @@ objects from the source repository into a pack in the cloned repository.
147147
with a long history, and would want to send in fixes
148148
as patches.
149149

150+
--recursive::
151+
After the clone is created, initialize all submodules within,
152+
using their default settings. This is equivalent to running
153+
'git submodule update --init --recursive' immediately after
154+
the clone is finished. This option is ignored if the cloned
155+
repository does not have a worktree/checkout (i.e. if any of
156+
`--no-checkout`/`-n`, `--bare`, or `--mirror` is given)
157+
150158
<repository>::
151159
The (possibly remote) repository to clone from. See the
152160
<<URLS,URLS>> section below for more information on specifying

Documentation/git-submodule.txt

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ SYNOPSIS
1111
[verse]
1212
'git submodule' [--quiet] add [-b branch]
1313
[--reference <repository>] [--] <repository> <path>
14-
'git submodule' [--quiet] status [--cached] [--] [<path>...]
14+
'git submodule' [--quiet] status [--cached] [--recursive] [--] [<path>...]
1515
'git submodule' [--quiet] init [--] [<path>...]
1616
'git submodule' [--quiet] update [--init] [-N|--no-fetch] [--rebase]
17-
[--reference <repository>] [--merge] [--] [<path>...]
17+
[--reference <repository>] [--merge] [--recursive] [--] [<path>...]
1818
'git submodule' [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
19-
'git submodule' [--quiet] foreach <command>
19+
'git submodule' [--quiet] foreach [--recursive] <command>
2020
'git submodule' [--quiet] sync [--] [<path>...]
2121

2222

@@ -100,6 +100,9 @@ status::
100100
initialized and `+` if the currently checked out submodule commit
101101
does not match the SHA-1 found in the index of the containing
102102
repository. This command is the default command for 'git-submodule'.
103+
+
104+
If '--recursive' is specified, this command will recurse into nested
105+
submodules, and show their status as well.
103106

104107
init::
105108
Initialize the submodules, i.e. register each submodule name
@@ -122,6 +125,9 @@ update::
122125
If the submodule is not yet initialized, and you just want to use the
123126
setting as stored in .gitmodules, you can automatically initialize the
124127
submodule with the --init option.
128+
+
129+
If '--recursive' is specified, this command will recurse into the
130+
registered submodules, and update any nested submodules within.
125131

126132
summary::
127133
Show commit summary between the given commit (defaults to HEAD) and
@@ -135,12 +141,15 @@ summary::
135141

136142
foreach::
137143
Evaluates an arbitrary shell command in each checked out submodule.
138-
The command has access to the variables $path and $sha1:
144+
The command has access to the variables $name, $path and $sha1:
145+
$name is the name of the relevant submodule section in .gitmodules,
139146
$path is the name of the submodule directory relative to the
140147
superproject, and $sha1 is the commit as recorded in the superproject.
141148
Any submodules defined in the superproject but not checked out are
142149
ignored by this command. Unless given --quiet, foreach prints the name
143150
of each submodule before evaluating the command.
151+
If --recursive is given, submodules are traversed recursively (i.e.
152+
the given shell command is evaluated in nested submodules as well).
144153
A non-zero return from the command in any submodule causes
145154
the processing to terminate. This can be overridden by adding '|| :'
146155
to the end of the command.
@@ -218,6 +227,12 @@ OPTIONS
218227
*NOTE*: Do *not* use this option unless you have read the note
219228
for linkgit:git-clone[1]'s --reference and --shared options carefully.
220229

230+
--recursive::
231+
This option is only valid for foreach, update and status commands.
232+
Traverse submodules recursively. The operation is performed not
233+
only in the submodules of the current repo, but also
234+
in any nested submodules inside those submodules (and so on).
235+
221236
<path>...::
222237
Paths to submodule(s). When specified this will restrict the command
223238
to only operate on the submodules found at the specified paths.

builtin-clone.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ static const char * const builtin_clone_usage[] = {
3838
};
3939

4040
static int option_quiet, option_no_checkout, option_bare, option_mirror;
41-
static int option_local, option_no_hardlinks, option_shared;
41+
static int option_local, option_no_hardlinks, option_shared, option_recursive;
4242
static char *option_template, *option_reference, *option_depth;
4343
static char *option_origin = NULL;
4444
static char *option_upload_pack = "git-upload-pack";
@@ -59,6 +59,8 @@ static struct option builtin_clone_options[] = {
5959
"don't use local hardlinks, always copy"),
6060
OPT_BOOLEAN('s', "shared", &option_shared,
6161
"setup as shared repository"),
62+
OPT_BOOLEAN(0, "recursive", &option_recursive,
63+
"setup as shared repository"),
6264
OPT_STRING(0, "template", &option_template, "path",
6365
"path the template repository"),
6466
OPT_STRING(0, "reference", &option_reference, "repo",
@@ -73,6 +75,10 @@ static struct option builtin_clone_options[] = {
7375
OPT_END()
7476
};
7577

78+
static const char *argv_submodule[] = {
79+
"submodule", "update", "--init", "--recursive", NULL
80+
};
81+
7682
static char *get_repo_path(const char *repo, int *is_bundle)
7783
{
7884
static char *suffix[] = { "/.git", ".git", "" };
@@ -608,6 +614,9 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
608614

609615
err |= run_hook(NULL, "post-checkout", sha1_to_hex(null_sha1),
610616
sha1_to_hex(remote_head->old_sha1), "1", NULL);
617+
618+
if (!err && option_recursive)
619+
err = run_command_v_opt(argv_submodule, RUN_GIT_CMD);
611620
}
612621

613622
strbuf_release(&reflog_msg);

git-submodule.sh

Lines changed: 71 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44
#
55
# Copyright (c) 2007 Lars Hjemli
66

7-
USAGE="[--quiet] [--cached|--files] \
8-
[add [-b branch] <repo> <path>]|[status|init|update [-i|--init] [-N|--no-fetch] [--rebase|--merge]|summary [-n|--summary-limit <n>] [<commit>]] \
9-
[--] [<path>...]|[foreach <command>]|[sync [--] [<path>...]]"
7+
dashless=$(basename "$0" | sed -e 's/-/ /')
8+
USAGE="[--quiet] add [-b branch] [--reference <repository>] [--] <repository> <path>
9+
or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
10+
or: $dashless [--quiet] init [--] [<path>...]
11+
or: $dashless [--quiet] update [--init] [-N|--no-fetch] [--rebase] [--reference <repository>] [--merge] [--recursive] [--] [<path>...]
12+
or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
13+
or: $dashless [--quiet] foreach [--recursive] <command>
14+
or: $dashless [--quiet] sync [--] [<path>...]"
1015
OPTIONS_SPEC=
1116
. git-sh-setup
1217
. git-parse-remote
@@ -19,6 +24,7 @@ cached=
1924
files=
2025
nofetch=
2126
update=
27+
prefix=
2228

2329
# Resolve relative url by appending to parent's url
2430
resolve_relative_url ()
@@ -238,13 +244,43 @@ cmd_add()
238244
#
239245
cmd_foreach()
240246
{
247+
# parse $args after "submodule ... foreach".
248+
while test $# -ne 0
249+
do
250+
case "$1" in
251+
-q|--quiet)
252+
GIT_QUIET=1
253+
;;
254+
--recursive)
255+
recursive=1
256+
;;
257+
-*)
258+
usage
259+
;;
260+
*)
261+
break
262+
;;
263+
esac
264+
shift
265+
done
266+
241267
module_list |
242268
while read mode sha1 stage path
243269
do
244270
if test -e "$path"/.git
245271
then
246-
say "Entering '$path'"
247-
(cd "$path" && eval "$@") ||
272+
say "Entering '$prefix$path'"
273+
name=$(module_name "$path")
274+
(
275+
prefix="$prefix$path/"
276+
unset GIT_DIR
277+
cd "$path" &&
278+
eval "$@" &&
279+
if test -n "$recursive"
280+
then
281+
cmd_foreach "--recursive" "$@"
282+
fi
283+
) ||
248284
die "Stopping at '$path'; script returned non-zero status."
249285
fi
250286
done
@@ -317,6 +353,7 @@ cmd_init()
317353
cmd_update()
318354
{
319355
# parse $args after "submodule ... update".
356+
orig_args="$@"
320357
while test $# -ne 0
321358
do
322359
case "$1" in
@@ -349,6 +386,10 @@ cmd_update()
349386
shift
350387
update="merge"
351388
;;
389+
--recursive)
390+
shift
391+
recursive=1
392+
;;
352393
--)
353394
shift
354395
break
@@ -435,6 +476,12 @@ cmd_update()
435476
die "Unable to $action '$sha1' in submodule path '$path'"
436477
say "Submodule path '$path': $msg '$sha1'"
437478
fi
479+
480+
if test -n "$recursive"
481+
then
482+
(unset GIT_DIR; cd "$path" && cmd_update $orig_args) ||
483+
die "Failed to recurse into submodule path '$path'"
484+
fi
438485
done
439486
}
440487

@@ -656,6 +703,7 @@ cmd_summary() {
656703
cmd_status()
657704
{
658705
# parse $args after "submodule ... status".
706+
orig_args="$@"
659707
while test $# -ne 0
660708
do
661709
case "$1" in
@@ -665,6 +713,9 @@ cmd_status()
665713
--cached)
666714
cached=1
667715
;;
716+
--recursive)
717+
recursive=1
718+
;;
668719
--)
669720
shift
670721
break
@@ -684,22 +735,34 @@ cmd_status()
684735
do
685736
name=$(module_name "$path") || exit
686737
url=$(git config submodule."$name".url)
738+
displaypath="$prefix$path"
687739
if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
688740
then
689-
say "-$sha1 $path"
741+
say "-$sha1 $displaypath"
690742
continue;
691743
fi
692744
set_name_rev "$path" "$sha1"
693745
if git diff-files --quiet -- "$path"
694746
then
695-
say " $sha1 $path$revname"
747+
say " $sha1 $displaypath$revname"
696748
else
697749
if test -z "$cached"
698750
then
699751
sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD)
700752
set_name_rev "$path" "$sha1"
701753
fi
702-
say "+$sha1 $path$revname"
754+
say "+$sha1 $displaypath$revname"
755+
fi
756+
757+
if test -n "$recursive"
758+
then
759+
(
760+
prefix="$displaypath/"
761+
unset GIT_DIR
762+
cd "$path" &&
763+
cmd_status $orig_args
764+
) ||
765+
die "Failed to recurse into submodule path '$path'"
703766
fi
704767
done
705768
}

0 commit comments

Comments
 (0)