Skip to content

Commit 4c69101

Browse files
bavisongitster
authored andcommitted
clone: add --remote-submodules flag
When using `git clone --recurse-submodules` there was previously no way to pass a `--remote` switch to the implicit `git submodule update` command for any use case where you want the submodules to be checked out on their remote-tracking branch rather than with the SHA-1 recorded in the superproject. This patch rectifies this situation. It actually passes `--no-fetch` to `git submodule update` as well on the grounds they the submodule has only just been cloned, so fetching from the remote again only serves to slow things down. Signed-off-by: Ben Avison <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ab15ad1 commit 4c69101

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

Documentation/git-clone.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ SYNOPSIS
1515
[--dissociate] [--separate-git-dir <git dir>]
1616
[--depth <depth>] [--[no-]single-branch] [--no-tags]
1717
[--recurse-submodules[=<pathspec>]] [--[no-]shallow-submodules]
18-
[--jobs <n>] [--] <repository> [<directory>]
18+
[--[no-]remote-submodules] [--jobs <n>] [--] <repository>
19+
[<directory>]
1920

2021
DESCRIPTION
2122
-----------
@@ -260,6 +261,12 @@ or `--mirror` is given)
260261
--[no-]shallow-submodules::
261262
All submodules which are cloned will be shallow with a depth of 1.
262263

264+
--[no-]remote-submodules::
265+
All submodules which are cloned will use the status of the submodule’s
266+
remote-tracking branch to update the submodule, rather than the
267+
superproject’s recorded SHA-1. Equivalent to passing `--remote` to
268+
`git submodule update`.
269+
263270
--separate-git-dir=<git dir>::
264271
Instead of placing the cloned repository where it is supposed
265272
to be, place the cloned repository at the specified directory,

builtin/clone.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ static int max_jobs = -1;
6767
static struct string_list option_recurse_submodules = STRING_LIST_INIT_NODUP;
6868
static struct list_objects_filter_options filter_options;
6969
static struct string_list server_options = STRING_LIST_INIT_NODUP;
70+
static int option_remote_submodules;
7071

7172
static int recurse_submodules_cb(const struct option *opt,
7273
const char *arg, int unset)
@@ -145,6 +146,8 @@ static struct option builtin_clone_options[] = {
145146
OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
146147
TRANSPORT_FAMILY_IPV6),
147148
OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
149+
OPT_BOOL(0, "remote-submodules", &option_remote_submodules,
150+
N_("any cloned submodules will use their remote-tracking branch")),
148151
OPT_END()
149152
};
150153

@@ -794,6 +797,11 @@ static int checkout(int submodule_progress)
794797
if (option_verbosity < 0)
795798
argv_array_push(&args, "--quiet");
796799

800+
if (option_remote_submodules) {
801+
argv_array_push(&args, "--remote");
802+
argv_array_push(&args, "--no-fetch");
803+
}
804+
797805
err = run_command_v_opt(args.argv, RUN_GIT_CMD);
798806
argv_array_clear(&args);
799807
}

t/t5617-clone-submodules-remote.sh

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/bin/sh
2+
3+
test_description='Test cloning repos with submodules using remote-tracking branches'
4+
5+
. ./test-lib.sh
6+
7+
pwd=$(pwd)
8+
9+
test_expect_success 'setup' '
10+
git checkout -b master &&
11+
test_commit commit1 &&
12+
mkdir sub &&
13+
(
14+
cd sub &&
15+
git init &&
16+
test_commit subcommit1 &&
17+
git tag sub_when_added_to_super
18+
) &&
19+
git submodule add "file://$pwd/sub" sub &&
20+
git commit -m "add submodule" &&
21+
(
22+
cd sub &&
23+
test_commit subcommit2
24+
)
25+
'
26+
27+
test_expect_success 'clone with --no-remote-submodules' '
28+
test_when_finished "rm -rf super_clone" &&
29+
git clone --recurse-submodules --no-remote-submodules "file://$pwd/." super_clone &&
30+
(
31+
cd super_clone/sub &&
32+
git diff --exit-code sub_when_added_to_super
33+
)
34+
'
35+
36+
test_expect_success 'clone with --remote-submodules' '
37+
test_when_finished "rm -rf super_clone" &&
38+
git clone --recurse-submodules --remote-submodules "file://$pwd/." super_clone &&
39+
(
40+
cd super_clone/sub &&
41+
git diff --exit-code remotes/origin/master
42+
)
43+
'
44+
45+
test_expect_success 'check the default is --no-remote-submodules' '
46+
test_when_finished "rm -rf super_clone" &&
47+
git clone --recurse-submodules "file://$pwd/." super_clone &&
48+
(
49+
cd super_clone/sub &&
50+
git diff --exit-code sub_when_added_to_super
51+
)
52+
'
53+
54+
test_done

0 commit comments

Comments
 (0)