Skip to content

Commit 132f600

Browse files
nasamuffingitster
authored andcommitted
clone: pass --single-branch during --recurse-submodules
Previously, performing "git clone --recurse-submodules --single-branch" resulted in submodules cloning all branches even though the superproject cloned only one branch. Pipe --single-branch through the submodule helper framework to make it to 'clone' later on. Signed-off-by: Emily Shaffer <[email protected]> Acked-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4731957 commit 132f600

File tree

5 files changed

+49
-5
lines changed

5 files changed

+49
-5
lines changed

Documentation/git-submodule.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ If you really want to remove a submodule from the repository and commit
133133
that use linkgit:git-rm[1] instead. See linkgit:gitsubmodules[7] for removal
134134
options.
135135

136-
update [--init] [--remote] [-N|--no-fetch] [--[no-]recommend-shallow] [-f|--force] [--checkout|--rebase|--merge] [--reference <repository>] [--depth <depth>] [--recursive] [--jobs <n>] [--] [<path>...]::
136+
update [--init] [--remote] [-N|--no-fetch] [--[no-]recommend-shallow] [-f|--force] [--checkout|--rebase|--merge] [--reference <repository>] [--depth <depth>] [--recursive] [--jobs <n>] [--[no-]single-branch] [--] [<path>...]::
137137
+
138138
--
139139
Update the registered submodules to match what the superproject
@@ -430,6 +430,10 @@ options carefully.
430430
Clone new submodules in parallel with as many jobs.
431431
Defaults to the `submodule.fetchJobs` option.
432432

433+
--[no-]single-branch::
434+
This option is only valid for the update command.
435+
Clone only one branch during update: HEAD or one specified by --branch.
436+
433437
<path>...::
434438
Paths to submodule(s). When specified this will restrict the command
435439
to only operate on the submodules found at the specified paths.

builtin/clone.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,11 @@ static int checkout(int submodule_progress)
832832
argv_array_push(&args, "--no-fetch");
833833
}
834834

835+
if (option_single_branch >= 0)
836+
argv_array_push(&args, option_single_branch ?
837+
"--single-branch" :
838+
"--no-single-branch");
839+
835840
err = run_command_v_opt(args.argv, RUN_GIT_CMD);
836841
argv_array_clear(&args);
837842
}

builtin/submodule--helper.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,7 +1225,7 @@ static int module_deinit(int argc, const char **argv, const char *prefix)
12251225

12261226
static int clone_submodule(const char *path, const char *gitdir, const char *url,
12271227
const char *depth, struct string_list *reference, int dissociate,
1228-
int quiet, int progress)
1228+
int quiet, int progress, int single_branch)
12291229
{
12301230
struct child_process cp = CHILD_PROCESS_INIT;
12311231

@@ -1247,6 +1247,10 @@ static int clone_submodule(const char *path, const char *gitdir, const char *url
12471247
argv_array_push(&cp.args, "--dissociate");
12481248
if (gitdir && *gitdir)
12491249
argv_array_pushl(&cp.args, "--separate-git-dir", gitdir, NULL);
1250+
if (single_branch >= 0)
1251+
argv_array_push(&cp.args, single_branch ?
1252+
"--single-branch" :
1253+
"--no-single-branch");
12501254

12511255
argv_array_push(&cp.args, "--");
12521256
argv_array_push(&cp.args, url);
@@ -1373,6 +1377,7 @@ static int module_clone(int argc, const char **argv, const char *prefix)
13731377
struct string_list reference = STRING_LIST_INIT_NODUP;
13741378
int dissociate = 0, require_init = 0;
13751379
char *sm_alternate = NULL, *error_strategy = NULL;
1380+
int single_branch = -1;
13761381

13771382
struct option module_clone_options[] = {
13781383
OPT_STRING(0, "prefix", &prefix,
@@ -1400,12 +1405,15 @@ static int module_clone(int argc, const char **argv, const char *prefix)
14001405
N_("force cloning progress")),
14011406
OPT_BOOL(0, "require-init", &require_init,
14021407
N_("disallow cloning into non-empty directory")),
1408+
OPT_BOOL(0, "single-branch", &single_branch,
1409+
N_("clone only one branch, HEAD or --branch")),
14031410
OPT_END()
14041411
};
14051412

14061413
const char *const git_submodule_helper_usage[] = {
14071414
N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
14081415
"[--reference <repository>] [--name <name>] [--depth <depth>] "
1416+
"[--single-branch] "
14091417
"--url <url> --path <path>"),
14101418
NULL
14111419
};
@@ -1438,7 +1446,7 @@ static int module_clone(int argc, const char **argv, const char *prefix)
14381446
prepare_possible_alternates(name, &reference);
14391447

14401448
if (clone_submodule(path, sm_gitdir, url, depth, &reference, dissociate,
1441-
quiet, progress))
1449+
quiet, progress, single_branch))
14421450
die(_("clone of '%s' into submodule path '%s' failed"),
14431451
url, path);
14441452
} else {
@@ -1562,6 +1570,7 @@ struct submodule_update_clone {
15621570
const char *depth;
15631571
const char *recursive_prefix;
15641572
const char *prefix;
1573+
int single_branch;
15651574

15661575
/* to be consumed by git-submodule.sh */
15671576
struct update_clone_data *update_clone;
@@ -1581,6 +1590,7 @@ struct submodule_update_clone {
15811590
.update = SUBMODULE_UPDATE_STRATEGY_INIT, \
15821591
.recommend_shallow = -1, \
15831592
.references = STRING_LIST_INIT_DUP, \
1593+
.single_branch = -1, \
15841594
.max_jobs = 1, \
15851595
}
15861596

@@ -1721,6 +1731,10 @@ static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
17211731
argv_array_push(&child->args, "--dissociate");
17221732
if (suc->depth)
17231733
argv_array_push(&child->args, suc->depth);
1734+
if (suc->single_branch >= 0)
1735+
argv_array_push(&child->args, suc->single_branch ?
1736+
"--single-branch" :
1737+
"--no-single-branch");
17241738

17251739
cleanup:
17261740
strbuf_reset(&displaypath_sb);
@@ -1900,6 +1914,8 @@ static int update_clone(int argc, const char **argv, const char *prefix)
19001914
N_("force cloning progress")),
19011915
OPT_BOOL(0, "require-init", &suc.require_init,
19021916
N_("disallow cloning into non-empty directory")),
1917+
OPT_BOOL(0, "single-branch", &suc.single_branch,
1918+
N_("clone only one branch, HEAD or --branch")),
19031919
OPT_END()
19041920
};
19051921

git-submodule.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ USAGE="[--quiet] [--cached]
1010
or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
1111
or: $dashless [--quiet] init [--] [<path>...]
1212
or: $dashless [--quiet] deinit [-f|--force] (--all| [--] <path>...)
13-
or: $dashless [--quiet] update [--init] [--remote] [-N|--no-fetch] [-f|--force] [--checkout|--merge|--rebase] [--[no-]recommend-shallow] [--reference <repository>] [--recursive] [--] [<path>...]
13+
or: $dashless [--quiet] update [--init] [--remote] [-N|--no-fetch] [-f|--force] [--checkout|--merge|--rebase] [--[no-]recommend-shallow] [--reference <repository>] [--recursive] [--[no-]single-branch] [--] [<path>...]
1414
or: $dashless [--quiet] set-branch (--default|--branch <branch>) [--] <path>
1515
or: $dashless [--quiet] set-url [--] <path> <newurl>
1616
or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
@@ -47,6 +47,7 @@ custom_name=
4747
depth=
4848
progress=
4949
dissociate=
50+
single_branch=
5051

5152
die_if_unmatched ()
5253
{
@@ -526,6 +527,12 @@ cmd_update()
526527
--jobs=*)
527528
jobs=$1
528529
;;
530+
--single-branch)
531+
single_branch="--single-branch"
532+
;;
533+
--no-single-branch)
534+
single_branch="--no-single-branch"
535+
;;
529536
--)
530537
shift
531538
break
@@ -555,6 +562,7 @@ cmd_update()
555562
${dissociate:+"--dissociate"} \
556563
${depth:+--depth "$depth"} \
557564
${require_init:+--require-init} \
565+
$single_branch \
558566
$recommend_shallow \
559567
$jobs \
560568
-- \

t/t5617-clone-submodules-remote.sh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ test_expect_success 'setup' '
1414
cd sub &&
1515
git init &&
1616
test_commit subcommit1 &&
17-
git tag sub_when_added_to_super
17+
git tag sub_when_added_to_super &&
18+
git branch other
1819
) &&
1920
git submodule add "file://$pwd/sub" sub &&
2021
git commit -m "add submodule" &&
@@ -51,4 +52,14 @@ test_expect_success 'check the default is --no-remote-submodules' '
5152
)
5253
'
5354

55+
test_expect_success 'clone with --single-branch' '
56+
test_when_finished "rm -rf super_clone" &&
57+
git clone --recurse-submodules --single-branch "file://$pwd/." super_clone &&
58+
(
59+
cd super_clone/sub &&
60+
git rev-parse --verify origin/master &&
61+
test_must_fail git rev-parse --verify origin/other
62+
)
63+
'
64+
5465
test_done

0 commit comments

Comments
 (0)