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 <bavison@riscosopen.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ben Avison committed May 19, 2019 at 15:26 UTC 4c6910163ab59f334becca39f5a83d3b7a622df4
3 files changed +70 -1
Documentation/git-clone.txt
+8 -1
@@ -15,7 +15,8 @@ SYNOPSIS
15 [--dissociate] [--separate-git-dir <git dir>]
16 [--depth <depth>] [--[no-]single-branch] [--no-tags]
17 [--recurse-submodules[=<pathspec>]] [--[no-]shallow-submodules]
18 - [--jobs <n>] [--] <repository> [<directory>]
18 + [--[no-]remote-submodules] [--jobs <n>] [--] <repository>
19 + [<directory>]
20
21 DESCRIPTION
22 -----------
@@ -260,6 +261,12 @@ or `--mirror` is given)
261 --[no-]shallow-submodules::
262 All submodules which are cloned will be shallow with a depth of 1.
263
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 +
270 --separate-git-dir=<git dir>::
271 Instead of placing the cloned repository where it is supposed
272 to be, place the cloned repository at the specified directory,
builtin/clone.c
+8
@@ -67,6 +67,7 @@ static int max_jobs = -1;
67 static struct string_list option_recurse_submodules = STRING_LIST_INIT_NODUP;
68 static struct list_objects_filter_options filter_options;
69 static struct string_list server_options = STRING_LIST_INIT_NODUP;
70 +static int option_remote_submodules;
71
72 static int recurse_submodules_cb(const struct option *opt,
73 const char *arg, int unset)
@@ -145,6 +146,8 @@ static struct option builtin_clone_options[] = {
146 OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
147 TRANSPORT_FAMILY_IPV6),
148 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")),
151 OPT_END()
152 };
153
@@ -794,6 +797,11 @@ static int checkout(int submodule_progress)
797 if (option_verbosity < 0)
798 argv_array_push(&args, "--quiet");
799
800 + if (option_remote_submodules) {
801 + argv_array_push(&args, "--remote");
802 + argv_array_push(&args, "--no-fetch");
803 + }
804 +
805 err = run_command_v_opt(args.argv, RUN_GIT_CMD);
806 argv_array_clear(&args);
807 }
t/t5617-clone-submodules-remote.sh new
+54
@@ -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