Skip to content

Commit da95e25

Browse files
committed
Merge branch 'gc/branch-recurse-submodules-fix'
A handful of obvious clean-ups around a topic that is already in 'master'. * gc/branch-recurse-submodules-fix: branch.c: simplify advice-and-die sequence branch: rework comments for future developers branch: remove negative exit code branch --set-upstream-to: be consistent when advising branch: give submodule updating advice before exit branch: support more tracking modes when recursing
2 parents 98f6a3a + 6696601 commit da95e25

File tree

3 files changed

+76
-16
lines changed

3 files changed

+76
-16
lines changed

branch.c

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,
233233
struct string_list tracking_srcs = STRING_LIST_INIT_DUP;
234234
int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE;
235235

236+
if (!track)
237+
BUG("asked to set up tracking, but tracking is disallowed");
238+
236239
memset(&tracking, 0, sizeof(tracking));
237240
tracking.spec.dst = (char *)orig_ref;
238241
tracking.srcs = &tracking_srcs;
@@ -260,7 +263,7 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,
260263
string_list_append(tracking.srcs, orig_ref);
261264
if (install_branch_config_multiple_remotes(config_flags, new_ref,
262265
tracking.remote, tracking.srcs) < 0)
263-
exit(-1);
266+
exit(1);
264267

265268
cleanup:
266269
string_list_clear(&tracking_srcs, 0);
@@ -385,12 +388,10 @@ static void dwim_branch_start(struct repository *r, const char *start_name,
385388
real_ref = NULL;
386389
if (get_oid_mb(start_name, &oid)) {
387390
if (explicit_tracking) {
388-
if (advice_enabled(ADVICE_SET_UPSTREAM_FAILURE)) {
389-
error(_(upstream_missing), start_name);
390-
advise(_(upstream_advice));
391-
exit(1);
392-
}
393-
die(_(upstream_missing), start_name);
391+
int code = die_message(_(upstream_missing), start_name);
392+
advise_if_enabled(ADVICE_SET_UPSTREAM_FAILURE,
393+
_(upstream_advice));
394+
exit(code);
394395
}
395396
die(_("not a valid object name: '%s'"), start_name);
396397
}
@@ -534,8 +535,27 @@ static int submodule_create_branch(struct repository *r,
534535
strvec_push(&child.args, "--quiet");
535536
if (reflog)
536537
strvec_push(&child.args, "--create-reflog");
537-
if (track == BRANCH_TRACK_ALWAYS || track == BRANCH_TRACK_EXPLICIT)
538-
strvec_push(&child.args, "--track");
538+
539+
switch (track) {
540+
case BRANCH_TRACK_NEVER:
541+
strvec_push(&child.args, "--no-track");
542+
break;
543+
case BRANCH_TRACK_ALWAYS:
544+
case BRANCH_TRACK_EXPLICIT:
545+
strvec_push(&child.args, "--track=direct");
546+
break;
547+
case BRANCH_TRACK_OVERRIDE:
548+
BUG("BRANCH_TRACK_OVERRIDE cannot be used when creating a branch.");
549+
break;
550+
case BRANCH_TRACK_INHERIT:
551+
strvec_push(&child.args, "--track=inherit");
552+
break;
553+
case BRANCH_TRACK_UNSPECIFIED:
554+
/* Default for "git checkout". Do not pass --track. */
555+
case BRANCH_TRACK_REMOTE:
556+
/* Default for "git branch". Do not pass --track. */
557+
break;
558+
}
539559

540560
strvec_pushl(&child.args, name, start_oid, tracking_name, NULL);
541561

@@ -585,11 +605,13 @@ void create_branches_recursively(struct repository *r, const char *name,
585605
*/
586606
for (i = 0; i < submodule_entry_list.entry_nr; i++) {
587607
if (submodule_entry_list.entries[i].repo == NULL) {
608+
int code = die_message(
609+
_("submodule '%s': unable to find submodule"),
610+
submodule_entry_list.entries[i].submodule->name);
588611
if (advice_enabled(ADVICE_SUBMODULES_NOT_UPDATED))
589612
advise(_("You may try updating the submodules using 'git checkout %s && git submodule update --init'"),
590613
start_commitish);
591-
die(_("submodule '%s': unable to find submodule"),
592-
submodule_entry_list.entries[i].submodule->name);
614+
exit(code);
593615
}
594616

595617
if (submodule_create_branch(
@@ -614,7 +636,8 @@ void create_branches_recursively(struct repository *r, const char *name,
614636
* tedious to determine whether or not tracking was set up in the
615637
* superproject.
616638
*/
617-
setup_tracking(name, tracking_name, track, quiet);
639+
if (track)
640+
setup_tracking(name, tracking_name, track, quiet);
618641

619642
for (i = 0; i < submodule_entry_list.entry_nr; i++) {
620643
if (submodule_create_branch(

builtin/submodule--helper.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3073,9 +3073,10 @@ static int module_create_branch(int argc, const char **argv, const char *prefix)
30733073
OPT__FORCE(&force, N_("force creation"), 0),
30743074
OPT_BOOL(0, "create-reflog", &reflog,
30753075
N_("create the branch's reflog")),
3076-
OPT_SET_INT('t', "track", &track,
3077-
N_("set up tracking mode (see git-pull(1))"),
3078-
BRANCH_TRACK_EXPLICIT),
3076+
OPT_CALLBACK_F('t', "track", &track, "(direct|inherit)",
3077+
N_("set branch tracking configuration"),
3078+
PARSE_OPT_OPTARG,
3079+
parse_opt_tracking_mode),
30793080
OPT__DRY_RUN(&dry_run,
30803081
N_("show whether the branch would be created")),
30813082
OPT_END()

t/t3207-branch-submodule.sh

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ test_expect_success 'should get fatal error upon branch creation when submodule
260260
)
261261
'
262262

263-
test_expect_success 'should set up tracking of remote-tracking branches' '
263+
test_expect_success 'should set up tracking of remote-tracking branches by default' '
264264
test_when_finished "reset_remote_test" &&
265265
(
266266
cd super-clone &&
@@ -289,4 +289,40 @@ test_expect_success 'should not fail when unable to set up tracking in submodule
289289
)
290290
'
291291

292+
test_expect_success '--track=inherit should set up tracking correctly' '
293+
test_when_finished "reset_remote_test" &&
294+
(
295+
cd super-clone &&
296+
git branch --recurse-submodules branch-a origin/branch-a &&
297+
# Set this manually instead of using branch --set-upstream-to
298+
# to circumvent the "nonexistent upstream" check.
299+
git -C sub config branch.branch-a.remote origin &&
300+
git -C sub config branch.branch-a.merge refs/heads/sub-branch-a &&
301+
git -C sub/sub-sub config branch.branch-a.remote other &&
302+
git -C sub/sub-sub config branch.branch-a.merge refs/heads/sub-sub-branch-a &&
303+
304+
git branch --recurse-submodules --track=inherit branch-b branch-a &&
305+
test_cmp_config origin branch.branch-b.remote &&
306+
test_cmp_config refs/heads/branch-a branch.branch-b.merge &&
307+
test_cmp_config -C sub origin branch.branch-b.remote &&
308+
test_cmp_config -C sub refs/heads/sub-branch-a branch.branch-b.merge &&
309+
test_cmp_config -C sub/sub-sub other branch.branch-b.remote &&
310+
test_cmp_config -C sub/sub-sub refs/heads/sub-sub-branch-a branch.branch-b.merge
311+
)
312+
'
313+
314+
test_expect_success '--no-track should not set up tracking' '
315+
test_when_finished "reset_remote_test" &&
316+
(
317+
cd super-clone &&
318+
git branch --recurse-submodules --no-track branch-a origin/branch-a &&
319+
test_cmp_config "" --default "" branch.branch-a.remote &&
320+
test_cmp_config "" --default "" branch.branch-a.merge &&
321+
test_cmp_config -C sub "" --default "" branch.branch-a.remote &&
322+
test_cmp_config -C sub "" --default "" branch.branch-a.merge &&
323+
test_cmp_config -C sub/sub-sub "" --default "" branch.branch-a.remote &&
324+
test_cmp_config -C sub/sub-sub "" --default "" branch.branch-a.merge
325+
)
326+
'
327+
292328
test_done

0 commit comments

Comments
 (0)