Skip to content

Commit

Permalink
Merge branch 'ba/clone-remote-submodules'
Browse files Browse the repository at this point in the history
"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
  • Loading branch information
gitster committed Jun 17, 2019
2 parents 6e0b1c6 + 4c69101 commit 9476094
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
9 changes: 8 additions & 1 deletion Documentation/git-clone.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ SYNOPSIS
[--dissociate] [--separate-git-dir <git dir>]
[--depth <depth>] [--[no-]single-branch] [--no-tags]
[--recurse-submodules[=<pathspec>]] [--[no-]shallow-submodules]
[--jobs <n>] [--] <repository> [<directory>]
[--[no-]remote-submodules] [--jobs <n>] [--] <repository>
[<directory>]

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

--[no-]remote-submodules::
All submodules which are cloned will use the status of the submodule’s
remote-tracking branch to update the submodule, rather than the
superproject’s recorded SHA-1. Equivalent to passing `--remote` to
`git submodule update`.

--separate-git-dir=<git dir>::
Instead of placing the cloned repository where it is supposed
to be, place the cloned repository at the specified directory,
Expand Down
8 changes: 8 additions & 0 deletions builtin/clone.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ static int max_jobs = -1;
static struct string_list option_recurse_submodules = STRING_LIST_INIT_NODUP;
static struct list_objects_filter_options filter_options;
static struct string_list server_options = STRING_LIST_INIT_NODUP;
static int option_remote_submodules;

static int recurse_submodules_cb(const struct option *opt,
const char *arg, int unset)
Expand Down Expand Up @@ -142,6 +143,8 @@ static struct option builtin_clone_options[] = {
OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
TRANSPORT_FAMILY_IPV6),
OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
OPT_BOOL(0, "remote-submodules", &option_remote_submodules,
N_("any cloned submodules will use their remote-tracking branch")),
OPT_END()
};

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

if (option_remote_submodules) {
argv_array_push(&args, "--remote");
argv_array_push(&args, "--no-fetch");
}

err = run_command_v_opt(args.argv, RUN_GIT_CMD);
argv_array_clear(&args);
}
Expand Down
54 changes: 54 additions & 0 deletions t/t5617-clone-submodules-remote.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/sh

test_description='Test cloning repos with submodules using remote-tracking branches'

. ./test-lib.sh

pwd=$(pwd)

test_expect_success 'setup' '
git checkout -b master &&
test_commit commit1 &&
mkdir sub &&
(
cd sub &&
git init &&
test_commit subcommit1 &&
git tag sub_when_added_to_super
) &&
git submodule add "file://$pwd/sub" sub &&
git commit -m "add submodule" &&
(
cd sub &&
test_commit subcommit2
)
'

test_expect_success 'clone with --no-remote-submodules' '
test_when_finished "rm -rf super_clone" &&
git clone --recurse-submodules --no-remote-submodules "file://$pwd/." super_clone &&
(
cd super_clone/sub &&
git diff --exit-code sub_when_added_to_super
)
'

test_expect_success 'clone with --remote-submodules' '
test_when_finished "rm -rf super_clone" &&
git clone --recurse-submodules --remote-submodules "file://$pwd/." super_clone &&
(
cd super_clone/sub &&
git diff --exit-code remotes/origin/master
)
'

test_expect_success 'check the default is --no-remote-submodules' '
test_when_finished "rm -rf super_clone" &&
git clone --recurse-submodules "file://$pwd/." super_clone &&
(
cd super_clone/sub &&
git diff --exit-code sub_when_added_to_super
)
'

test_done

0 comments on commit 9476094

Please sign in to comment.