]> git.ipfire.org Git - thirdparty/git.git/commitdiff
branch: add flags and config to inherit tracking
authorJosh Steadmon <steadmon@google.com>
Tue, 21 Dec 2021 03:30:23 +0000 (19:30 -0800)
committerJunio C Hamano <gitster@pobox.com>
Tue, 21 Dec 2021 06:40:21 +0000 (22:40 -0800)
It can be helpful when creating a new branch to use the existing
tracking configuration from the branch point. However, there is
currently not a method to automatically do so.

Teach git-{branch,checkout,switch} an "inherit" argument to the
"--track" option. When this is set, creating a new branch will cause the
tracking configuration to default to the configuration of the branch
point, if set.

For example, if branch "main" tracks "origin/main", and we run
`git checkout --track=inherit -b feature main`, then branch "feature"
will track "origin/main". Thus, `git status` will show us how far
ahead/behind we are from origin, and `git pull` will pull from origin.

This is particularly useful when creating branches across many
submodules, such as with `git submodule foreach ...` (or if running with
a patch such as [1], which we use at $job), as it avoids having to
manually set tracking info for each submodule.

Since we've added an argument to "--track", also add "--track=direct" as
another way to explicitly get the original "--track" behavior ("--track"
without an argument still works as well).

Finally, teach branch.autoSetupMerge a new "inherit" option. When this
is set, "--track=inherit" becomes the default behavior.

[1]: https://lore.kernel.org/git/20180927221603.148025-1-sbeller@google.com/

Signed-off-by: Josh Steadmon <steadmon@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
16 files changed:
Documentation/config/branch.txt
Documentation/git-branch.txt
Documentation/git-checkout.txt
Documentation/git-switch.txt
branch.c
branch.h
builtin/branch.c
builtin/checkout.c
config.c
parse-options-cb.c
parse-options.h
t/t2017-checkout-orphan.sh
t/t2027-checkout-track.sh
t/t2060-switch.sh
t/t3200-branch.sh
t/t7201-co.sh

index cc5f3249fc58a39da1580b49ad0145c44ec1544f..55f7522e126c752fd8a33a2d879c001fc7d791de 100644 (file)
@@ -7,7 +7,8 @@ branch.autoSetupMerge::
        automatic setup is done; `true` -- automatic setup is done when the
        starting point is a remote-tracking branch; `always` --
        automatic setup is done when the starting point is either a
-       local branch or remote-tracking
+       local branch or remote-tracking branch; `inherit` -- if the starting point
+       has a tracking configuration, it is copied to the new
        branch. This option defaults to true.
 
 branch.autoSetupRebase::
index 5449767121103299e442560775a54bce5b371a9f..75beea7bac378a727fcd88703fabfbec4e5db8e5 100644 (file)
@@ -16,7 +16,7 @@ SYNOPSIS
        [--points-at <object>] [--format=<format>]
        [(-r | --remotes) | (-a | --all)]
        [--list] [<pattern>...]
-'git branch' [--track | --no-track] [-f] <branchname> [<start-point>]
+'git branch' [--track [direct|inherit] | --no-track] [-f] <branchname> [<start-point>]
 'git branch' (--set-upstream-to=<upstream> | -u <upstream>) [<branchname>]
 'git branch' --unset-upstream [<branchname>]
 'git branch' (-m | -M) [<oldbranch>] <newbranch>
@@ -206,24 +206,34 @@ This option is only applicable in non-verbose mode.
        Display the full sha1s in the output listing rather than abbreviating them.
 
 -t::
---track::
+--track [inherit|direct]::
        When creating a new branch, set up `branch.<name>.remote` and
-       `branch.<name>.merge` configuration entries to mark the
-       start-point branch as "upstream" from the new branch. This
+       `branch.<name>.merge` configuration entries to set "upstream" tracking
+       configuration for the new branch. This
        configuration will tell git to show the relationship between the
        two branches in `git status` and `git branch -v`. Furthermore,
        it directs `git pull` without arguments to pull from the
        upstream when the new branch is checked out.
 +
-This behavior is the default when the start point is a remote-tracking branch.
+The exact upstream branch is chosen depending on the optional argument:
+`--track` or `--track direct` means to use the start-point branch itself as the
+upstream; `--track inherit` means to copy the upstream configuration of the
+start-point branch.
++
+`--track direct` is the default when the start point is a remote-tracking branch.
 Set the branch.autoSetupMerge configuration variable to `false` if you
 want `git switch`, `git checkout` and `git branch` to always behave as if `--no-track`
 were given. Set it to `always` if you want this behavior when the
-start-point is either a local or remote-tracking branch.
+start-point is either a local or remote-tracking branch. Set it to
+`inherit` if you want to copy the tracking configuration from the
+branch point.
++
+See linkgit:git-pull[1] and linkgit:git-config[1] for additional discussion on
+how the `branch.<name>.remote` and `branch.<name>.merge` options are used.
 
 --no-track::
        Do not set up "upstream" configuration, even if the
-       branch.autoSetupMerge configuration variable is true.
+       branch.autoSetupMerge configuration variable is set.
 
 --set-upstream::
        As this option had confusing syntax, it is no longer supported.
index d473c9bf38753ee0786d10e7509a83bde7e58c97..61825b060d380cb0e16668a549a4f834db2a6e87 100644 (file)
@@ -156,7 +156,7 @@ of it").
        linkgit:git-branch[1] for details.
 
 -t::
---track::
+--track [direct|inherit]::
        When creating a new branch, set up "upstream" configuration. See
        "--track" in linkgit:git-branch[1] for details.
 +
index 5c438cd505875841763f0151dfe0a2c1454dfcc5..96dc036ea5ae26fe4ce306a542e86b71af3b10ff 100644 (file)
@@ -152,7 +152,7 @@ should result in deletion of the path).
        attached to a terminal, regardless of `--quiet`.
 
 -t::
---track::
+--track [direct|inherit]::
        When creating a new branch, set up "upstream" configuration.
        `-c` is implied. See `--track` in linkgit:git-branch[1] for
        details.
index 299c8f07f7503d29f5214590c5cb0787411d8b16..a4e4631ef16722c3e360970d52356ee3f9693d35 100644 (file)
--- a/branch.c
+++ b/branch.c
@@ -11,7 +11,7 @@
 
 struct tracking {
        struct refspec_item spec;
-       char *src;
+       struct string_list *srcs;
        const char *remote;
        int matches;
 };
@@ -22,11 +22,11 @@ static int find_tracked_branch(struct remote *remote, void *priv)
 
        if (!remote_find_tracking(remote, &tracking->spec)) {
                if (++tracking->matches == 1) {
-                       tracking->src = tracking->spec.src;
+                       string_list_append(tracking->srcs, tracking->spec.src);
                        tracking->remote = remote->name;
                } else {
                        free(tracking->spec.src);
-                       FREE_AND_NULL(tracking->src);
+                       string_list_clear(tracking->srcs, 0);
                }
                tracking->spec.src = NULL;
        }
@@ -189,6 +189,34 @@ int install_branch_config(int flag, const char *local, const char *origin,
        return ret;
 }
 
+static int inherit_tracking(struct tracking *tracking, const char *orig_ref)
+{
+       const char *bare_ref;
+       struct branch *branch;
+       int i;
+
+       bare_ref = orig_ref;
+       skip_prefix(orig_ref, "refs/heads/", &bare_ref);
+
+       branch = branch_get(bare_ref);
+       if (!branch->remote_name) {
+               warning(_("asked to inherit tracking from '%s', but no remote is set"),
+                       bare_ref);
+               return -1;
+       }
+
+       if (branch->merge_nr < 1 || !branch->merge_name || !branch->merge_name[0]) {
+               warning(_("asked to inherit tracking from '%s', but no merge configuration is set"),
+                       bare_ref);
+               return -1;
+       }
+
+       tracking->remote = xstrdup(branch->remote_name);
+       for (i = 0; i < branch->merge_nr; i++)
+               string_list_append(tracking->srcs, branch->merge_name[i]);
+       return 0;
+}
+
 /*
  * This is called when new_ref is branched off of orig_ref, and tries
  * to infer the settings for branch.<new_ref>.{remote,merge} from the
@@ -198,11 +226,15 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,
                           enum branch_track track, int quiet)
 {
        struct tracking tracking;
+       struct string_list tracking_srcs = STRING_LIST_INIT_DUP;
        int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE;
 
        memset(&tracking, 0, sizeof(tracking));
        tracking.spec.dst = (char *)orig_ref;
-       if (for_each_remote(find_tracked_branch, &tracking))
+       tracking.srcs = &tracking_srcs;
+       if (track != BRANCH_TRACK_INHERIT)
+               for_each_remote(find_tracked_branch, &tracking);
+       else if (inherit_tracking(&tracking, orig_ref))
                return;
 
        if (!tracking.matches)
@@ -210,6 +242,7 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,
                case BRANCH_TRACK_ALWAYS:
                case BRANCH_TRACK_EXPLICIT:
                case BRANCH_TRACK_OVERRIDE:
+               case BRANCH_TRACK_INHERIT:
                        break;
                default:
                        return;
@@ -219,11 +252,13 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,
                die(_("Not tracking: ambiguous information for ref %s"),
                    orig_ref);
 
-       if (install_branch_config(config_flags, new_ref, tracking.remote,
-                             tracking.src ? tracking.src : orig_ref) < 0)
+       if (tracking.srcs->nr < 1)
+               string_list_append(tracking.srcs, orig_ref);
+       if (install_branch_config_multiple_remotes(config_flags, new_ref,
+                               tracking.remote, tracking.srcs) < 0)
                exit(-1);
 
-       free(tracking.src);
+       string_list_clear(tracking.srcs, 0);
 }
 
 int read_branch_desc(struct strbuf *buf, const char *branch_name)
index df0be61506fd36a0bfb63a911ba532b5db495767..815dcd40c0761104eba38215d04232e7f601ab1d 100644 (file)
--- a/branch.h
+++ b/branch.h
@@ -10,7 +10,8 @@ enum branch_track {
        BRANCH_TRACK_REMOTE,
        BRANCH_TRACK_ALWAYS,
        BRANCH_TRACK_EXPLICIT,
-       BRANCH_TRACK_OVERRIDE
+       BRANCH_TRACK_OVERRIDE,
+       BRANCH_TRACK_INHERIT,
 };
 
 extern enum branch_track git_branch_track;
index 0b7ed82654af1fd52170e7a27fcc3f69fd2f4825..1890afd4e57432349541e6b451b257b53720770a 100644 (file)
@@ -632,8 +632,10 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
                OPT__VERBOSE(&filter.verbose,
                        N_("show hash and subject, give twice for upstream branch")),
                OPT__QUIET(&quiet, N_("suppress informational messages")),
-               OPT_SET_INT('t', "track",  &track, N_("set up tracking mode (see git-pull(1))"),
-                       BRANCH_TRACK_EXPLICIT),
+               OPT_CALLBACK_F('t', "track",  &track, "direct|inherit",
+                       N_("set branch tracking configuration"),
+                       PARSE_OPT_OPTARG | PARSE_OPT_LITERAL_ARGHELP,
+                       parse_opt_tracking_mode),
                OPT_SET_INT_F(0, "set-upstream", &track, N_("do not use"),
                        BRANCH_TRACK_OVERRIDE, PARSE_OPT_HIDDEN),
                OPT_STRING('u', "set-upstream-to", &new_upstream, N_("upstream"), N_("change the upstream info")),
index cbf73b8c9f65ae2fdd1d96265bd4972aeaa9bd68..8d511aa6b70635073fbd1e99c5feafb3c03e2cc8 100644 (file)
@@ -1530,8 +1530,10 @@ static struct option *add_common_switch_branch_options(
 {
        struct option options[] = {
                OPT_BOOL('d', "detach", &opts->force_detach, N_("detach HEAD at named commit")),
-               OPT_SET_INT('t', "track",  &opts->track, N_("set upstream info for new branch"),
-                       BRANCH_TRACK_EXPLICIT),
+               OPT_CALLBACK_F('t', "track",  &opts->track, "direct|inherit",
+                       N_("set up tracking mode (see git-pull(1))"),
+                       PARSE_OPT_OPTARG | PARSE_OPT_LITERAL_ARGHELP,
+                       parse_opt_tracking_mode),
                OPT__FORCE(&opts->force, N_("force checkout (throw away local modifications)"),
                           PARSE_OPT_NOCOMPLETE),
                OPT_STRING(0, "orphan", &opts->new_orphan_branch, N_("new-branch"), N_("new unparented branch")),
index 2dcbe901b6b7a05f56a66056ac79d9b729454382..532732fc3355250164e5e841d0bf73d7f8a34375 100644 (file)
--- a/config.c
+++ b/config.c
@@ -1559,6 +1559,9 @@ static int git_default_branch_config(const char *var, const char *value)
                if (value && !strcasecmp(value, "always")) {
                        git_branch_track = BRANCH_TRACK_ALWAYS;
                        return 0;
+               } else if (value && !strcmp(value, "inherit")) {
+                       git_branch_track = BRANCH_TRACK_INHERIT;
+                       return 0;
                }
                git_branch_track = git_config_bool(var, value);
                return 0;
index 3c811e1e4a7e135cb302b807d15725816730f3b3..d346dbe2100ddceb53f8ce149ad4ed794b772f53 100644 (file)
@@ -1,5 +1,6 @@
 #include "git-compat-util.h"
 #include "parse-options.h"
+#include "branch.h"
 #include "cache.h"
 #include "commit.h"
 #include "color.h"
@@ -293,3 +294,18 @@ int parse_opt_passthru_argv(const struct option *opt, const char *arg, int unset
 
        return 0;
 }
+
+int parse_opt_tracking_mode(const struct option *opt, const char *arg, int unset)
+{
+       if (unset)
+               *(enum branch_track *)opt->value = BRANCH_TRACK_NEVER;
+       else if (!arg || !strcmp(arg, "direct"))
+               *(enum branch_track *)opt->value = BRANCH_TRACK_EXPLICIT;
+       else if (!strcmp(arg, "inherit"))
+               *(enum branch_track *)opt->value = BRANCH_TRACK_INHERIT;
+       else
+               return error(_("option `%s' expects \"%s\" or \"%s\""),
+                            "--track", "direct", "inherit");
+
+       return 0;
+}
index 13405472ee82c9028cb441e20535631bf2993f33..47ba8dcbac164cb75e1172c976c6a700753337d2 100644 (file)
@@ -302,6 +302,8 @@ enum parse_opt_result parse_opt_unknown_cb(struct parse_opt_ctx_t *ctx,
                                           const char *, int);
 int parse_opt_passthru(const struct option *, const char *, int);
 int parse_opt_passthru_argv(const struct option *, const char *, int);
+/* value is enum branch_track* */
+int parse_opt_tracking_mode(const struct option *, const char *, int);
 
 #define OPT__VERBOSE(var, h)  OPT_COUNTUP('v', "verbose", (var), (h))
 #define OPT__QUIET(var, h)    OPT_COUNTUP('q', "quiet",   (var), (h))
index 88d6992a5e1f95978e15ab5fc5d1fad00e2b890a..4d689bd377c269770879356fbcf7bc6efecca115 100755 (executable)
@@ -62,8 +62,17 @@ test_expect_success '--orphan ignores branch.autosetupmerge' '
        git checkout main &&
        git config branch.autosetupmerge always &&
        git checkout --orphan gamma &&
-       test -z "$(git config branch.gamma.merge)" &&
+       test_cmp_config "" --default "" branch.gamma.merge &&
        test refs/heads/gamma = "$(git symbolic-ref HEAD)" &&
+       test_must_fail git rev-parse --verify HEAD^ &&
+       git checkout main &&
+       git config branch.autosetupmerge inherit &&
+       git checkout --orphan eta &&
+       test_cmp_config "" --default "" branch.eta.merge &&
+       test_cmp_config "" --default "" branch.eta.remote &&
+       echo refs/heads/eta >expected &&
+       git symbolic-ref HEAD >actual &&
+       test_cmp expected actual &&
        test_must_fail git rev-parse --verify HEAD^
 '
 
index 4453741b966dc5607cf4350b53cd440cbcab942d..dca35aa3e3e264002703932e56add4322b475fcf 100755 (executable)
@@ -24,4 +24,27 @@ test_expect_success 'checkout --track -b rejects an extra path argument' '
        test_i18ngrep "cannot be used with updating paths" err
 '
 
+test_expect_success 'checkout --track -b overrides autoSetupMerge=inherit' '
+       # Set up tracking config on main
+       test_config branch.main.remote origin &&
+       test_config branch.main.merge refs/heads/some-branch &&
+       test_config branch.autoSetupMerge inherit &&
+       # With --track=inherit, we copy the tracking config from main
+       git checkout --track=inherit -b b1 main &&
+       test_cmp_config origin branch.b1.remote &&
+       test_cmp_config refs/heads/some-branch branch.b1.merge &&
+       # With branch.autoSetupMerge=inherit, we do the same
+       git checkout -b b2 main &&
+       test_cmp_config origin branch.b2.remote &&
+       test_cmp_config refs/heads/some-branch branch.b2.merge &&
+       # But --track overrides this
+       git checkout --track -b b3 main &&
+       test_cmp_config . branch.b3.remote &&
+       test_cmp_config refs/heads/main branch.b3.merge &&
+       # And --track=direct does as well
+       git checkout --track=direct -b b4 main &&
+       test_cmp_config . branch.b4.remote &&
+       test_cmp_config refs/heads/main branch.b4.merge
+'
+
 test_done
index 9bc6a3aa5cd9afbae1bb8b136f2e6e834b09b103..ebb961be293ef935d88c3ad3b8056725d248684b 100755 (executable)
@@ -107,4 +107,32 @@ test_expect_success 'not switching when something is in progress' '
        test_must_fail git switch -d @^
 '
 
+test_expect_success 'tracking info copied with autoSetupMerge=inherit' '
+       # default config does not copy tracking info
+       git switch -c foo-no-inherit foo &&
+       test_cmp_config "" --default "" branch.foo-no-inherit.remote &&
+       test_cmp_config "" --default "" branch.foo-no-inherit.merge &&
+       # with --track=inherit, we copy tracking info from foo
+       git switch --track=inherit -c foo2 foo &&
+       test_cmp_config origin branch.foo2.remote &&
+       test_cmp_config refs/heads/foo branch.foo2.merge &&
+       # with autoSetupMerge=inherit, we do the same
+       test_config branch.autoSetupMerge inherit &&
+       git switch -c foo3 foo &&
+       test_cmp_config origin branch.foo3.remote &&
+       test_cmp_config refs/heads/foo branch.foo3.merge &&
+       # with --track, we override autoSetupMerge
+       git switch --track -c foo4 foo &&
+       test_cmp_config . branch.foo4.remote &&
+       test_cmp_config refs/heads/foo branch.foo4.merge &&
+       # and --track=direct does as well
+       git switch --track=direct -c foo5 foo &&
+       test_cmp_config . branch.foo5.remote &&
+       test_cmp_config refs/heads/foo branch.foo5.merge &&
+       # no tracking info to inherit from main
+       git switch -c main2 main &&
+       test_cmp_config "" --default "" branch.main2.remote &&
+       test_cmp_config "" --default "" branch.main2.merge
+'
+
 test_done
index bcc1acaa786e7da44599cfde9dc2cb67210ea4c3..a74b2e06a146e322f7978f6a4e4b5c97736b40e6 100755 (executable)
@@ -1422,4 +1422,37 @@ test_expect_success 'invalid sort parameter in configuration' '
        )
 '
 
+test_expect_success 'tracking info copied with --track=inherit' '
+       git branch --track=inherit foo2 my1 &&
+       test_cmp_config local branch.foo2.remote &&
+       test_cmp_config refs/heads/main branch.foo2.merge
+'
+
+test_expect_success 'tracking info copied with autoSetupMerge=inherit' '
+       test_unconfig branch.autoSetupMerge &&
+       # default config does not copy tracking info
+       git branch foo-no-inherit my1 &&
+       test_cmp_config "" --default "" branch.foo-no-inherit.remote &&
+       test_cmp_config "" --default "" branch.foo-no-inherit.merge &&
+       # with autoSetupMerge=inherit, we copy tracking info from my1
+       test_config branch.autoSetupMerge inherit &&
+       git branch foo3 my1 &&
+       test_cmp_config local branch.foo3.remote &&
+       test_cmp_config refs/heads/main branch.foo3.merge &&
+       # no tracking info to inherit from main
+       git branch main2 main &&
+       test_cmp_config "" --default "" branch.main2.remote &&
+       test_cmp_config "" --default "" branch.main2.merge
+'
+
+test_expect_success '--track overrides branch.autoSetupMerge' '
+       test_config branch.autoSetupMerge inherit &&
+       git branch --track=direct foo4 my1 &&
+       test_cmp_config . branch.foo4.remote &&
+       test_cmp_config refs/heads/my1 branch.foo4.merge &&
+       git branch --no-track foo5 my1 &&
+       test_cmp_config "" --default "" branch.foo5.remote &&
+       test_cmp_config "" --default "" branch.foo5.merge
+'
+
 test_done
index b7ba1c3268e32935ca62f0e3f562a80c99f84481..61ad47b0c18d231c60327acda04348895947809c 100755 (executable)
@@ -658,4 +658,21 @@ test_expect_success 'custom merge driver with checkout -m' '
        test_cmp expect arm
 '
 
+test_expect_success 'tracking info copied with autoSetupMerge=inherit' '
+       git reset --hard main &&
+       # default config does not copy tracking info
+       git checkout -b foo-no-inherit koala/bear &&
+       test_cmp_config "" --default "" branch.foo-no-inherit.remote &&
+       test_cmp_config "" --default "" branch.foo-no-inherit.merge &&
+       # with autoSetupMerge=inherit, we copy tracking info from koala/bear
+       test_config branch.autoSetupMerge inherit &&
+       git checkout -b foo koala/bear &&
+       test_cmp_config origin branch.foo.remote &&
+       test_cmp_config refs/heads/koala/bear branch.foo.merge &&
+       # no tracking info to inherit from main
+       git checkout -b main2 main &&
+       test_cmp_config "" --default "" branch.main2.remote &&
+       test_cmp_config "" --default "" branch.main2.merge
+'
+
 test_done