@matheusbsbs / matheus-bagliano / commits / 132f600b06
Fork 0

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 <emilyshaffer@google.com> Acked-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Emily Shaffer committed Feb 20, 2020 at 19:10 UTC 132f600b060c2db7fd3d450dfadb6779a61c648a
5 files changed +49 -5
Documentation/git-submodule.txt
+5 -1
@@ -133,7 +133,7 @@ If you really want to remove a submodule from the repository and commit
133 that use linkgit:git-rm[1] instead. See linkgit:gitsubmodules[7] for removal
134 options.
135
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>...]::
137 +
138 --
139 Update the registered submodules to match what the superproject
@@ -430,6 +430,10 @@ options carefully.
430 Clone new submodules in parallel with as many jobs.
431 Defaults to the `submodule.fetchJobs` option.
432
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 +
437 <path>...::
438 Paths to submodule(s). When specified this will restrict the command
439 to only operate on the submodules found at the specified paths.
builtin/clone.c
+5
@@ -832,6 +832,11 @@ static int checkout(int submodule_progress)
832 argv_array_push(&args, "--no-fetch");
833 }
834
835 + if (option_single_branch >= 0)
836 + argv_array_push(&args, option_single_branch ?
837 + "--single-branch" :
838 + "--no-single-branch");
839 +
840 err = run_command_v_opt(args.argv, RUN_GIT_CMD);
841 argv_array_clear(&args);
842 }
builtin/submodule--helper.c
+18 -2
@@ -1225,7 +1225,7 @@ static int module_deinit(int argc, const char **argv, const char *prefix)
1225
1226 static int clone_submodule(const char *path, const char *gitdir, const char *url,
1227 const char *depth, struct string_list *reference, int dissociate,
1228 - int quiet, int progress)
1228 + int quiet, int progress, int single_branch)
1229 {
1230 struct child_process cp = CHILD_PROCESS_INIT;
1231
@@ -1247,6 +1247,10 @@ static int clone_submodule(const char *path, const char *gitdir, const char *url
1247 argv_array_push(&cp.args, "--dissociate");
1248 if (gitdir && *gitdir)
1249 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");
1254
1255 argv_array_push(&cp.args, "--");
1256 argv_array_push(&cp.args, url);
@@ -1373,6 +1377,7 @@ static int module_clone(int argc, const char **argv, const char *prefix)
1377 struct string_list reference = STRING_LIST_INIT_NODUP;
1378 int dissociate = 0, require_init = 0;
1379 char *sm_alternate = NULL, *error_strategy = NULL;
1380 + int single_branch = -1;
1381
1382 struct option module_clone_options[] = {
1383 OPT_STRING(0, "prefix", &prefix,
@@ -1400,12 +1405,15 @@ static int module_clone(int argc, const char **argv, const char *prefix)
1405 N_("force cloning progress")),
1406 OPT_BOOL(0, "require-init", &require_init,
1407 N_("disallow cloning into non-empty directory")),
1408 + OPT_BOOL(0, "single-branch", &single_branch,
1409 + N_("clone only one branch, HEAD or --branch")),
1410 OPT_END()
1411 };
1412
1413 const char *const git_submodule_helper_usage[] = {
1414 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1415 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1416 + "[--single-branch] "
1417 "--url <url> --path <path>"),
1418 NULL
1419 };
@@ -1438,7 +1446,7 @@ static int module_clone(int argc, const char **argv, const char *prefix)
1446 prepare_possible_alternates(name, &reference);
1447
1448 if (clone_submodule(path, sm_gitdir, url, depth, &reference, dissociate,
1441 - quiet, progress))
1449 + quiet, progress, single_branch))
1450 die(_("clone of '%s' into submodule path '%s' failed"),
1451 url, path);
1452 } else {
@@ -1562,6 +1570,7 @@ struct submodule_update_clone {
1570 const char *depth;
1571 const char *recursive_prefix;
1572 const char *prefix;
1573 + int single_branch;
1574
1575 /* to be consumed by git-submodule.sh */
1576 struct update_clone_data *update_clone;
@@ -1581,6 +1590,7 @@ struct submodule_update_clone {
1590 .update = SUBMODULE_UPDATE_STRATEGY_INIT, \
1591 .recommend_shallow = -1, \
1592 .references = STRING_LIST_INIT_DUP, \
1593 + .single_branch = -1, \
1594 .max_jobs = 1, \
1595 }
1596
@@ -1721,6 +1731,10 @@ static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
1731 argv_array_push(&child->args, "--dissociate");
1732 if (suc->depth)
1733 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");
1738
1739 cleanup:
1740 strbuf_reset(&displaypath_sb);
@@ -1900,6 +1914,8 @@ static int update_clone(int argc, const char **argv, const char *prefix)
1914 N_("force cloning progress")),
1915 OPT_BOOL(0, "require-init", &suc.require_init,
1916 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")),
1919 OPT_END()
1920 };
1921
git-submodule.sh
+9 -1
@@ -10,7 +10,7 @@ USAGE="[--quiet] [--cached]
10 or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
11 or: $dashless [--quiet] init [--] [<path>...]
12 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>...]
14 or: $dashless [--quiet] set-branch (--default|--branch <branch>) [--] <path>
15 or: $dashless [--quiet] set-url [--] <path> <newurl>
16 or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
@@ -47,6 +47,7 @@ custom_name=
47 depth=
48 progress=
49 dissociate=
50 +single_branch=
51
52 die_if_unmatched ()
53 {
@@ -526,6 +527,12 @@ cmd_update()
527 --jobs=*)
528 jobs=$1
529 ;;
530 + --single-branch)
531 + single_branch="--single-branch"
532 + ;;
533 + --no-single-branch)
534 + single_branch="--no-single-branch"
535 + ;;
536 --)
537 shift
538 break
@@ -555,6 +562,7 @@ cmd_update()
562 ${dissociate:+"--dissociate"} \
563 ${depth:+--depth "$depth"} \
564 ${require_init:+--require-init} \
565 + $single_branch \
566 $recommend_shallow \
567 $jobs \
568 -- \
t/t5617-clone-submodules-remote.sh
+12 -1
@@ -14,7 +14,8 @@ test_expect_success 'setup' '
14 cd sub &&
15 git init &&
16 test_commit subcommit1 &&
17 - git tag sub_when_added_to_super
17 + git tag sub_when_added_to_super &&
18 + git branch other
19 ) &&
20 git submodule add "file://$pwd/sub" sub &&
21 git commit -m "add submodule" &&
@@ -51,4 +52,14 @@ test_expect_success 'check the default is --no-remote-submodules' '
52 )
53 '
54
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 +
65 test_done