Skip to content

Commit 9476094

Browse files
committed
Merge branch 'ba/clone-remote-submodules'
"git clone --recurse-submodules" learned to set up the submodules to ignore commit object names recorded in the superproject gitlink and instead use the commits that happen to be at the tip of the remote-tracking branches from the get-go, by passing the new "--remote-submodules" option. * ba/clone-remote-submodules: clone: add `--remote-submodules` flag
2 parents 6e0b1c6 + 4c69101 commit 9476094

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)
@@ -142,6 +143,8 @@ static struct option builtin_clone_options[] = {
142143
OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
143144
TRANSPORT_FAMILY_IPV6),
144145
OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
146+
OPT_BOOL(0, "remote-submodules", &option_remote_submodules,
147+
N_("any cloned submodules will use their remote-tracking branch")),
145148
OPT_END()
146149
};
147150

@@ -790,6 +793,11 @@ static int checkout(int submodule_progress)
790793
if (option_verbosity < 0)
791794
argv_array_push(&args, "--quiet");
792795

796+
if (option_remote_submodules) {
797+
argv_array_push(&args, "--remote");
798+
argv_array_push(&args, "--no-fetch");
799+
}
800+
793801
err = run_command_v_opt(args.argv, RUN_GIT_CMD);
794802
argv_array_clear(&args);
795803
}

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)