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>
Josh Steadmon committedDec 20, 2021 at 19:30 UTCd3115660b4cc4b1a32e77ddfd289afde5e4b94d8
16 files changed+205-20
Documentation/config/branch.txt
+2-1
index cc5f3249fc..55f7522e12 100644--- a/Documentation/config/branch.txt+++ b/Documentation/config/branch.txt@@ -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::
Documentation/git-branch.txt
+17-6
index 5449767121..75beea7bac 100644--- a/Documentation/git-branch.txt+++ b/Documentation/git-branch.txt@@ -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.
Documentation/git-checkout.txt
+1
index d473c9bf38..61825b060d 100644--- a/Documentation/git-checkout.txt+++ b/Documentation/git-checkout.txt@@ -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. +
Documentation/git-switch.txt
+1
index 5c438cd505..96dc036ea5 100644--- a/Documentation/git-switch.txt+++ b/Documentation/git-switch.txt@@ -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.
branch.c
+42-7
index 299c8f07f7..a4e4631ef1 100644--- 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)