diff --git a/branch.c b/branch.c index 22f4f46b96..b5182f2bb7 100644 --- a/branch.c +++ b/branch.c @@ -760,7 +760,15 @@ static int submodule_create_branch(struct repository *r, break; } - strvec_pushl(&child.args, name, start_oid, tracking_name, NULL); + /* + * The tracking name is absent when the start point named no ref. + * Push it separately: strvec_pushl() stops at the first NULL, so + * passing it inline would drop the argument by accident rather + * than by intent. + */ + strvec_pushl(&child.args, name, start_oid, NULL); + if (tracking_name) + strvec_push(&child.args, tracking_name); if ((ret = start_command(&child))) return ret; @@ -840,7 +848,7 @@ void create_branches_recursively(struct repository *r, const char *name, * tedious to determine whether or not tracking was set up in the * superproject. */ - if (track) + if (tracking_name && track) setup_tracking(name, tracking_name, track, quiet); for (i = 0; i < submodule_entry_list.entry_nr; i++) { diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index e7cd3225fa..1eb73de674 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -3336,7 +3336,7 @@ static int module_create_branch(int argc, const char **argv, const char *prefix, OPT_END() }; const char *const usage[] = { - N_("git submodule--helper create-branch [-f|--force] [--create-reflog] [-q|--quiet] [-t|--track] [-n|--dry-run] "), + N_("git submodule--helper create-branch [-f|--force] [--create-reflog] [-q|--quiet] [-t|--track] [-n|--dry-run] []"), NULL }; struct repo_config_values *cfg = repo_config_values(the_repository); @@ -3345,13 +3345,14 @@ static int module_create_branch(int argc, const char **argv, const char *prefix, track = cfg->branch_track; argc = parse_options(argc, argv, prefix, options, usage, 0); - if (argc != 3) + if (argc < 2 || argc > 3) usage_with_options(usage, options); if (!quiet && !dry_run) printf_ln(_("creating branch '%s'"), argv[0]); - create_branches_recursively(the_repository, argv[0], argv[1], argv[2], + create_branches_recursively(the_repository, argv[0], argv[1], + argc > 2 ? argv[2] : NULL, force, reflog, quiet, track, dry_run); return 0; } diff --git a/t/t3207-branch-submodule.sh b/t/t3207-branch-submodule.sh index 4afb2d3198..4dd26c6c0f 100755 --- a/t/t3207-branch-submodule.sh +++ b/t/t3207-branch-submodule.sh @@ -98,6 +98,36 @@ test_expect_success 'should respect submodule.recurse when creating branches' ' ) ' +test_expect_success 'should move a branch to a start point that names no ref' ' + test_when_finished "rm -rf no-submodules" && + git init no-submodules && + ( + cd no-submodules && + test_commit one && + test_commit two && + git remote add origin . && + git config submodule.propagateBranches true && + git config submodule.recurse true && + git branch branch-a HEAD~1 && + oid=$(git rev-parse HEAD) && + git branch -f branch-a "$oid" && + test_cmp_rev HEAD branch-a + ) +' + +test_expect_success 'should recurse into submodules from a start point that names no ref' ' + test_when_finished "reset_test" && + ( + cd super && + oid=$(git rev-parse HEAD) && + git branch --recurse-submodules branch-a "$oid" && + git rev-parse branch-a && + git -C sub rev-parse branch-a && + git -C sub/sub-sub rev-parse branch-a && + git -C second/sub rev-parse branch-a + ) +' + test_expect_success 'should ignore submodule.recurse when not creating branches' ' test_when_finished "reset_test" && (