branch: move --set-upstream-to behavior to dwim_and_setup_tracking()

This commit is preparation for a future commit that will simplify create_branch() so that it always creates a branch. This will allow create_branch() to accept a dry_run parameter (which is needed for "git branch --recurse-submodules"). create_branch() used to always create a branch, but 4fc5006676 (Add branch --set-upstream, 2010-01-18) changed it to also be able to set tracking information without creating a branch. Refactor the code that sets tracking information into its own functions dwim_branch_start() and dwim_and_setup_tracking(). Also change an invocation of create_branch() in cmd_branch() in builtin/branch.c to use dwim_and_setup_tracking(), since that invocation is only for setting tracking information (in "git branch --set-upstream-to"). As of this commit, create_branch() is no longer invoked in a way that does not create branches. Helped-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Glen Choo <chooglen@google.com> Reviewed-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Glen Choo committed Jan 28, 2022 at 16:04 UTC e89f151db13684924feb0cd0a0ca3a13c1d71516
3 files changed +92 -26
branch.c
+67 -20
@@ -218,9 +218,11 @@ static int inherit_tracking(struct tracking *tracking, const char *orig_ref)
218 }
219
220 /*
221 - * This is called when new_ref is branched off of orig_ref, and tries
222 - * to infer the settings for branch.<new_ref>.{remote,merge} from the
223 - * config.
221 + * Used internally to set the branch.<new_ref>.{remote,merge} config
222 + * settings so that branch 'new_ref' tracks 'orig_ref'. Unlike
223 + * dwim_and_setup_tracking(), this does not do DWIM, i.e. "origin/main"
224 + * will not be expanded to "refs/remotes/origin/main", so it is not safe
225 + * for 'orig_ref' to be raw user input.
226 */
227 static void setup_tracking(const char *new_ref, const char *orig_ref,
228 enum branch_track track, int quiet)
@@ -341,31 +343,37 @@ N_("\n"
343 "will track its remote counterpart, you may want to use\n"
344 "\"git push -u\" to set the upstream config as you push.");
345
344 -void create_branch(struct repository *r,
345 - const char *name, const char *start_name,
346 - int force, int clobber_head_ok, int reflog,
347 - int quiet, enum branch_track track)
346 +/**
347 + * DWIMs a user-provided ref to determine the starting point for a
348 + * branch and validates it, where:
349 + *
350 + * - r is the repository to validate the branch for
351 + *
352 + * - start_name is the ref that we would like to test. This is
353 + * expanded with DWIM and assigned to out_real_ref.
354 + *
355 + * - track is the tracking mode of the new branch. If tracking is
356 + * explicitly requested, start_name must be a branch (because
357 + * otherwise start_name cannot be tracked)
358 + *
359 + * - out_oid is an out parameter containing the object_id of start_name
360 + *
361 + * - out_real_ref is an out parameter containing the full, 'real' form
362 + * of start_name e.g. refs/heads/main instead of main
363 + *
364 + */
365 +static void dwim_branch_start(struct repository *r, const char *start_name,
366 + enum branch_track track, char **out_real_ref,
367 + struct object_id *out_oid)
368 {
369 struct commit *commit;
370 struct object_id oid;
371 char *real_ref;
352 - struct strbuf ref = STRBUF_INIT;
353 - int forcing = 0;
354 - int dont_change_ref = 0;
372 int explicit_tracking = 0;
373
374 if (track == BRANCH_TRACK_EXPLICIT || track == BRANCH_TRACK_OVERRIDE)
375 explicit_tracking = 1;
376
360 - if ((track == BRANCH_TRACK_OVERRIDE || clobber_head_ok)
361 - ? validate_branchname(name, &ref)
362 - : validate_new_branchname(name, &ref, force)) {
363 - if (!force)
364 - dont_change_ref = 1;
365 - else
366 - forcing = 1;
367 - }
368 -
377 real_ref = NULL;
378 if (get_oid_mb(start_name, &oid)) {
379 if (explicit_tracking) {
@@ -402,7 +410,37 @@ void create_branch(struct repository *r,
410
411 if ((commit = lookup_commit_reference(r, &oid)) == NULL)
412 die(_("Not a valid branch point: '%s'."), start_name);
405 - oidcpy(&oid, &commit->object.oid);
413 + if (out_real_ref) {
414 + *out_real_ref = real_ref;
415 + real_ref = NULL;
416 + }
417 + if (out_oid)
418 + oidcpy(out_oid, &commit->object.oid);
419 +
420 + FREE_AND_NULL(real_ref);
421 +}
422 +
423 +void create_branch(struct repository *r,
424 + const char *name, const char *start_name,
425 + int force, int clobber_head_ok, int reflog,
426 + int quiet, enum branch_track track)
427 +{
428 + struct object_id oid;
429 + char *real_ref;
430 + struct strbuf ref = STRBUF_INIT;
431 + int forcing = 0;
432 + int dont_change_ref = 0;
433 +
434 + if ((track == BRANCH_TRACK_OVERRIDE || clobber_head_ok)
435 + ? validate_branchname(name, &ref)
436 + : validate_new_branchname(name, &ref, force)) {
437 + if (!force)
438 + dont_change_ref = 1;
439 + else
440 + forcing = 1;
441 + }
442 +
443 + dwim_branch_start(r, start_name, track, &real_ref, &oid);
444
445 if (reflog)
446 log_all_ref_updates = LOG_REFS_NORMAL;
@@ -436,6 +474,15 @@ void create_branch(struct repository *r,
474 free(real_ref);
475 }
476
477 +void dwim_and_setup_tracking(struct repository *r, const char *new_ref,
478 + const char *orig_ref, enum branch_track track,
479 + int quiet)
480 +{
481 + char *real_orig_ref;
482 + dwim_branch_start(r, orig_ref, track, &real_orig_ref, NULL);
483 + setup_tracking(new_ref, real_orig_ref, track, quiet);
484 +}
485 +
486 void remove_merge_branch_state(struct repository *r)
487 {
488 unlink(git_path_merge_head(r));
branch.h
+22
@@ -18,6 +18,28 @@ extern enum branch_track git_branch_track;
18
19 /* Functions for acting on the information about branches. */
20
21 +/**
22 + * Sets branch.<new_ref>.{remote,merge} config settings such that
23 + * new_ref tracks orig_ref according to the specified tracking mode.
24 + *
25 + * - new_ref is the name of the branch that we are setting tracking
26 + * for.
27 + *
28 + * - orig_ref is the name of the ref that is 'upstream' of new_ref.
29 + * orig_ref will be expanded with DWIM so that the config settings
30 + * are in the correct format e.g. "refs/remotes/origin/main" instead
31 + * of "origin/main".
32 + *
33 + * - track is the tracking mode e.g. BRANCH_TRACK_REMOTE causes
34 + * new_ref to track orig_ref directly, whereas BRANCH_TRACK_INHERIT
35 + * causes new_ref to track whatever orig_ref tracks.
36 + *
37 + * - quiet suppresses tracking information.
38 + */
39 +void dwim_and_setup_tracking(struct repository *r, const char *new_ref,
40 + const char *orig_ref, enum branch_track track,
41 + int quiet);
42 +
43 /*
44 * Creates a new branch, where:
45 *
builtin/branch.c
+3 -6
@@ -823,12 +823,9 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
823 if (!ref_exists(branch->refname))
824 die(_("branch '%s' does not exist"), branch->name);
825
826 - /*
827 - * create_branch takes care of setting up the tracking
828 - * info and making sure new_upstream is correct
829 - */
830 - create_branch(the_repository, branch->name, new_upstream,
831 - 0, 0, 0, quiet, BRANCH_TRACK_OVERRIDE);
826 + dwim_and_setup_tracking(the_repository, branch->name,
827 + new_upstream, BRANCH_TRACK_OVERRIDE,
828 + quiet);
829 } else if (unset_upstream) {
830 struct branch *branch = branch_get(argv[0]);
831 struct strbuf buf = STRBUF_INIT;