branch: add flags and config to inherit tracking

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 committed Dec 20, 2021 at 19:30 UTC d3115660b4cc4b1a32e77ddfd289afde5e4b94d8
16 files changed +205 -23
Documentation/config/branch.txt
+2 -1
@@ -7,7 +7,8 @@ branch.autoSetupMerge::
7 automatic setup is done; `true` -- automatic setup is done when the
8 starting point is a remote-tracking branch; `always` --
9 automatic setup is done when the starting point is either a
10 - local branch or remote-tracking
10 + local branch or remote-tracking branch; `inherit` -- if the starting point
11 + has a tracking configuration, it is copied to the new
12 branch. This option defaults to true.
13
14 branch.autoSetupRebase::
Documentation/git-branch.txt
+17 -7
@@ -16,7 +16,7 @@ SYNOPSIS
16 [--points-at <object>] [--format=<format>]
17 [(-r | --remotes) | (-a | --all)]
18 [--list] [<pattern>...]
19 -'git branch' [--track | --no-track] [-f] <branchname> [<start-point>]
19 +'git branch' [--track [direct|inherit] | --no-track] [-f] <branchname> [<start-point>]
20 'git branch' (--set-upstream-to=<upstream> | -u <upstream>) [<branchname>]
21 'git branch' --unset-upstream [<branchname>]
22 'git branch' (-m | -M) [<oldbranch>] <newbranch>
@@ -206,24 +206,34 @@ This option is only applicable in non-verbose mode.
206 Display the full sha1s in the output listing rather than abbreviating them.
207
208 -t::
209 ---track::
209 +--track [inherit|direct]::
210 When creating a new branch, set up `branch.<name>.remote` and
211 - `branch.<name>.merge` configuration entries to mark the
212 - start-point branch as "upstream" from the new branch. This
211 + `branch.<name>.merge` configuration entries to set "upstream" tracking
212 + configuration for the new branch. This
213 configuration will tell git to show the relationship between the
214 two branches in `git status` and `git branch -v`. Furthermore,
215 it directs `git pull` without arguments to pull from the
216 upstream when the new branch is checked out.
217 +
218 -This behavior is the default when the start point is a remote-tracking branch.
218 +The exact upstream branch is chosen depending on the optional argument:
219 +`--track` or `--track direct` means to use the start-point branch itself as the
220 +upstream; `--track inherit` means to copy the upstream configuration of the
221 +start-point branch.
222 ++
223 +`--track direct` is the default when the start point is a remote-tracking branch.
224 Set the branch.autoSetupMerge configuration variable to `false` if you
225 want `git switch`, `git checkout` and `git branch` to always behave as if `--no-track`
226 were given. Set it to `always` if you want this behavior when the
222 -start-point is either a local or remote-tracking branch.
227 +start-point is either a local or remote-tracking branch. Set it to
228 +`inherit` if you want to copy the tracking configuration from the
229 +branch point.
230 ++
231 +See linkgit:git-pull[1] and linkgit:git-config[1] for additional discussion on
232 +how the `branch.<name>.remote` and `branch.<name>.merge` options are used.
233
234 --no-track::
235 Do not set up "upstream" configuration, even if the
226 - branch.autoSetupMerge configuration variable is true.
236 + branch.autoSetupMerge configuration variable is set.
237
238 --set-upstream::
239 As this option had confusing syntax, it is no longer supported.
Documentation/git-checkout.txt
+1 -1
@@ -156,7 +156,7 @@ of it").
156 linkgit:git-branch[1] for details.
157
158 -t::
159 ---track::
159 +--track [direct|inherit]::
160 When creating a new branch, set up "upstream" configuration. See
161 "--track" in linkgit:git-branch[1] for details.
162 +
Documentation/git-switch.txt
+1 -1
@@ -152,7 +152,7 @@ should result in deletion of the path).
152 attached to a terminal, regardless of `--quiet`.
153
154 -t::
155 ---track::
155 +--track [direct|inherit]::
156 When creating a new branch, set up "upstream" configuration.
157 `-c` is implied. See `--track` in linkgit:git-branch[1] for
158 details.
branch.c
+42 -7
@@ -11,7 +11,7 @@
11
12 struct tracking {
13 struct refspec_item spec;
14 - char *src;
14 + struct string_list *srcs;
15 const char *remote;
16 int matches;
17 };
@@ -22,11 +22,11 @@ static int find_tracked_branch(struct remote *remote, void *priv)
22
23 if (!remote_find_tracking(remote, &tracking->spec)) {
24 if (++tracking->matches == 1) {
25 - tracking->src = tracking->spec.src;
25 + string_list_append(tracking->srcs, tracking->spec.src);
26 tracking->remote = remote->name;
27 } else {
28 free(tracking->spec.src);
29 - FREE_AND_NULL(tracking->src);
29 + string_list_clear(tracking->srcs, 0);
30 }
31 tracking->spec.src = NULL;
32 }
@@ -189,6 +189,34 @@ int install_branch_config(int flag, const char *local, const char *origin,
189 return ret;
190 }
191
192 +static int inherit_tracking(struct tracking *tracking, const char *orig_ref)
193 +{
194 + const char *bare_ref;
195 + struct branch *branch;
196 + int i;
197 +
198 + bare_ref = orig_ref;
199 + skip_prefix(orig_ref, "refs/heads/", &bare_ref);
200 +
201 + branch = branch_get(bare_ref);
202 + if (!branch->remote_name) {
203 + warning(_("asked to inherit tracking from '%s', but no remote is set"),
204 + bare_ref);
205 + return -1;
206 + }
207 +
208 + if (branch->merge_nr < 1 || !branch->merge_name || !branch->merge_name[0]) {
209 + warning(_("asked to inherit tracking from '%s', but no merge configuration is set"),
210 + bare_ref);
211 + return -1;
212 + }
213 +
214 + tracking->remote = xstrdup(branch->remote_name);
215 + for (i = 0; i < branch->merge_nr; i++)
216 + string_list_append(tracking->srcs, branch->merge_name[i]);
217 + return 0;
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
@@ -198,11 +226,15 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,
226 enum branch_track track, int quiet)
227 {
228 struct tracking tracking;
229 + struct string_list tracking_srcs = STRING_LIST_INIT_DUP;
230 int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE;
231
232 memset(&tracking, 0, sizeof(tracking));
233 tracking.spec.dst = (char *)orig_ref;
205 - if (for_each_remote(find_tracked_branch, &tracking))
234 + tracking.srcs = &tracking_srcs;
235 + if (track != BRANCH_TRACK_INHERIT)
236 + for_each_remote(find_tracked_branch, &tracking);
237 + else if (inherit_tracking(&tracking, orig_ref))
238 return;
239
240 if (!tracking.matches)
@@ -210,6 +242,7 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,
242 case BRANCH_TRACK_ALWAYS:
243 case BRANCH_TRACK_EXPLICIT:
244 case BRANCH_TRACK_OVERRIDE:
245 + case BRANCH_TRACK_INHERIT:
246 break;
247 default:
248 return;
@@ -219,11 +252,13 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,
252 die(_("Not tracking: ambiguous information for ref %s"),
253 orig_ref);
254
222 - if (install_branch_config(config_flags, new_ref, tracking.remote,
223 - tracking.src ? tracking.src : orig_ref) < 0)
255 + if (tracking.srcs->nr < 1)
256 + string_list_append(tracking.srcs, orig_ref);
257 + if (install_branch_config_multiple_remotes(config_flags, new_ref,
258 + tracking.remote, tracking.srcs) < 0)
259 exit(-1);
260
226 - free(tracking.src);
261 + string_list_clear(tracking.srcs, 0);
262 }
263
264 int read_branch_desc(struct strbuf *buf, const char *branch_name)
branch.h
+2 -1
@@ -10,7 +10,8 @@ enum branch_track {
10 BRANCH_TRACK_REMOTE,
11 BRANCH_TRACK_ALWAYS,
12 BRANCH_TRACK_EXPLICIT,
13 - BRANCH_TRACK_OVERRIDE
13 + BRANCH_TRACK_OVERRIDE,
14 + BRANCH_TRACK_INHERIT,
15 };
16
17 extern enum branch_track git_branch_track;
builtin/branch.c
+4 -2
@@ -632,8 +632,10 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
632 OPT__VERBOSE(&filter.verbose,
633 N_("show hash and subject, give twice for upstream branch")),
634 OPT__QUIET(&quiet, N_("suppress informational messages")),
635 - OPT_SET_INT('t', "track", &track, N_("set up tracking mode (see git-pull(1))"),
636 - BRANCH_TRACK_EXPLICIT),
635 + OPT_CALLBACK_F('t', "track", &track, "direct|inherit",
636 + N_("set branch tracking configuration"),
637 + PARSE_OPT_OPTARG | PARSE_OPT_LITERAL_ARGHELP,
638 + parse_opt_tracking_mode),
639 OPT_SET_INT_F(0, "set-upstream", &track, N_("do not use"),
640 BRANCH_TRACK_OVERRIDE, PARSE_OPT_HIDDEN),
641 OPT_STRING('u', "set-upstream-to", &new_upstream, N_("upstream"), N_("change the upstream info")),
builtin/checkout.c
+4 -2
@@ -1530,8 +1530,10 @@ static struct option *add_common_switch_branch_options(
1530 {
1531 struct option options[] = {
1532 OPT_BOOL('d', "detach", &opts->force_detach, N_("detach HEAD at named commit")),
1533 - OPT_SET_INT('t', "track", &opts->track, N_("set upstream info for new branch"),
1534 - BRANCH_TRACK_EXPLICIT),
1533 + OPT_CALLBACK_F('t', "track", &opts->track, "direct|inherit",
1534 + N_("set up tracking mode (see git-pull(1))"),
1535 + PARSE_OPT_OPTARG | PARSE_OPT_LITERAL_ARGHELP,
1536 + parse_opt_tracking_mode),
1537 OPT__FORCE(&opts->force, N_("force checkout (throw away local modifications)"),
1538 PARSE_OPT_NOCOMPLETE),
1539 OPT_STRING(0, "orphan", &opts->new_orphan_branch, N_("new-branch"), N_("new unparented branch")),
config.c
+3
@@ -1559,6 +1559,9 @@ static int git_default_branch_config(const char *var, const char *value)
1559 if (value && !strcasecmp(value, "always")) {
1560 git_branch_track = BRANCH_TRACK_ALWAYS;
1561 return 0;
1562 + } else if (value && !strcmp(value, "inherit")) {
1563 + git_branch_track = BRANCH_TRACK_INHERIT;
1564 + return 0;
1565 }
1566 git_branch_track = git_config_bool(var, value);
1567 return 0;
parse-options-cb.c
+16
@@ -1,5 +1,6 @@
1 #include "git-compat-util.h"
2 #include "parse-options.h"
3 +#include "branch.h"
4 #include "cache.h"
5 #include "commit.h"
6 #include "color.h"
@@ -293,3 +294,18 @@ int parse_opt_passthru_argv(const struct option *opt, const char *arg, int unset
294
295 return 0;
296 }
297 +
298 +int parse_opt_tracking_mode(const struct option *opt, const char *arg, int unset)
299 +{
300 + if (unset)
301 + *(enum branch_track *)opt->value = BRANCH_TRACK_NEVER;
302 + else if (!arg || !strcmp(arg, "direct"))
303 + *(enum branch_track *)opt->value = BRANCH_TRACK_EXPLICIT;
304 + else if (!strcmp(arg, "inherit"))
305 + *(enum branch_track *)opt->value = BRANCH_TRACK_INHERIT;
306 + else
307 + return error(_("option `%s' expects \"%s\" or \"%s\""),
308 + "--track", "direct", "inherit");
309 +
310 + return 0;
311 +}
parse-options.h
+2
@@ -302,6 +302,8 @@ enum parse_opt_result parse_opt_unknown_cb(struct parse_opt_ctx_t *ctx,
302 const char *, int);
303 int parse_opt_passthru(const struct option *, const char *, int);
304 int parse_opt_passthru_argv(const struct option *, const char *, int);
305 +/* value is enum branch_track* */
306 +int parse_opt_tracking_mode(const struct option *, const char *, int);
307
308 #define OPT__VERBOSE(var, h) OPT_COUNTUP('v', "verbose", (var), (h))
309 #define OPT__QUIET(var, h) OPT_COUNTUP('q', "quiet", (var), (h))
t/t2017-checkout-orphan.sh
+10 -1
@@ -62,8 +62,17 @@ test_expect_success '--orphan ignores branch.autosetupmerge' '
62 git checkout main &&
63 git config branch.autosetupmerge always &&
64 git checkout --orphan gamma &&
65 - test -z "$(git config branch.gamma.merge)" &&
65 + test_cmp_config "" --default "" branch.gamma.merge &&
66 test refs/heads/gamma = "$(git symbolic-ref HEAD)" &&
67 + test_must_fail git rev-parse --verify HEAD^ &&
68 + git checkout main &&
69 + git config branch.autosetupmerge inherit &&
70 + git checkout --orphan eta &&
71 + test_cmp_config "" --default "" branch.eta.merge &&
72 + test_cmp_config "" --default "" branch.eta.remote &&
73 + echo refs/heads/eta >expected &&
74 + git symbolic-ref HEAD >actual &&
75 + test_cmp expected actual &&
76 test_must_fail git rev-parse --verify HEAD^
77 '
78
t/t2027-checkout-track.sh
+23
@@ -24,4 +24,27 @@ test_expect_success 'checkout --track -b rejects an extra path argument' '
24 test_i18ngrep "cannot be used with updating paths" err
25 '
26
27 +test_expect_success 'checkout --track -b overrides autoSetupMerge=inherit' '
28 + # Set up tracking config on main
29 + test_config branch.main.remote origin &&
30 + test_config branch.main.merge refs/heads/some-branch &&
31 + test_config branch.autoSetupMerge inherit &&
32 + # With --track=inherit, we copy the tracking config from main
33 + git checkout --track=inherit -b b1 main &&
34 + test_cmp_config origin branch.b1.remote &&
35 + test_cmp_config refs/heads/some-branch branch.b1.merge &&
36 + # With branch.autoSetupMerge=inherit, we do the same
37 + git checkout -b b2 main &&
38 + test_cmp_config origin branch.b2.remote &&
39 + test_cmp_config refs/heads/some-branch branch.b2.merge &&
40 + # But --track overrides this
41 + git checkout --track -b b3 main &&
42 + test_cmp_config . branch.b3.remote &&
43 + test_cmp_config refs/heads/main branch.b3.merge &&
44 + # And --track=direct does as well
45 + git checkout --track=direct -b b4 main &&
46 + test_cmp_config . branch.b4.remote &&
47 + test_cmp_config refs/heads/main branch.b4.merge
48 +'
49 +
50 test_done
t/t2060-switch.sh
+28
@@ -107,4 +107,32 @@ test_expect_success 'not switching when something is in progress' '
107 test_must_fail git switch -d @^
108 '
109
110 +test_expect_success 'tracking info copied with autoSetupMerge=inherit' '
111 + # default config does not copy tracking info
112 + git switch -c foo-no-inherit foo &&
113 + test_cmp_config "" --default "" branch.foo-no-inherit.remote &&
114 + test_cmp_config "" --default "" branch.foo-no-inherit.merge &&
115 + # with --track=inherit, we copy tracking info from foo
116 + git switch --track=inherit -c foo2 foo &&
117 + test_cmp_config origin branch.foo2.remote &&
118 + test_cmp_config refs/heads/foo branch.foo2.merge &&
119 + # with autoSetupMerge=inherit, we do the same
120 + test_config branch.autoSetupMerge inherit &&
121 + git switch -c foo3 foo &&
122 + test_cmp_config origin branch.foo3.remote &&
123 + test_cmp_config refs/heads/foo branch.foo3.merge &&
124 + # with --track, we override autoSetupMerge
125 + git switch --track -c foo4 foo &&
126 + test_cmp_config . branch.foo4.remote &&
127 + test_cmp_config refs/heads/foo branch.foo4.merge &&
128 + # and --track=direct does as well
129 + git switch --track=direct -c foo5 foo &&
130 + test_cmp_config . branch.foo5.remote &&
131 + test_cmp_config refs/heads/foo branch.foo5.merge &&
132 + # no tracking info to inherit from main
133 + git switch -c main2 main &&
134 + test_cmp_config "" --default "" branch.main2.remote &&
135 + test_cmp_config "" --default "" branch.main2.merge
136 +'
137 +
138 test_done
t/t3200-branch.sh
+33
@@ -1422,4 +1422,37 @@ test_expect_success 'invalid sort parameter in configuration' '
1422 )
1423 '
1424
1425 +test_expect_success 'tracking info copied with --track=inherit' '
1426 + git branch --track=inherit foo2 my1 &&
1427 + test_cmp_config local branch.foo2.remote &&
1428 + test_cmp_config refs/heads/main branch.foo2.merge
1429 +'
1430 +
1431 +test_expect_success 'tracking info copied with autoSetupMerge=inherit' '
1432 + test_unconfig branch.autoSetupMerge &&
1433 + # default config does not copy tracking info
1434 + git branch foo-no-inherit my1 &&
1435 + test_cmp_config "" --default "" branch.foo-no-inherit.remote &&
1436 + test_cmp_config "" --default "" branch.foo-no-inherit.merge &&
1437 + # with autoSetupMerge=inherit, we copy tracking info from my1
1438 + test_config branch.autoSetupMerge inherit &&
1439 + git branch foo3 my1 &&
1440 + test_cmp_config local branch.foo3.remote &&
1441 + test_cmp_config refs/heads/main branch.foo3.merge &&
1442 + # no tracking info to inherit from main
1443 + git branch main2 main &&
1444 + test_cmp_config "" --default "" branch.main2.remote &&
1445 + test_cmp_config "" --default "" branch.main2.merge
1446 +'
1447 +
1448 +test_expect_success '--track overrides branch.autoSetupMerge' '
1449 + test_config branch.autoSetupMerge inherit &&
1450 + git branch --track=direct foo4 my1 &&
1451 + test_cmp_config . branch.foo4.remote &&
1452 + test_cmp_config refs/heads/my1 branch.foo4.merge &&
1453 + git branch --no-track foo5 my1 &&
1454 + test_cmp_config "" --default "" branch.foo5.remote &&
1455 + test_cmp_config "" --default "" branch.foo5.merge
1456 +'
1457 +
1458 test_done
t/t7201-co.sh
+17
@@ -658,4 +658,21 @@ test_expect_success 'custom merge driver with checkout -m' '
658 test_cmp expect arm
659 '
660
661 +test_expect_success 'tracking info copied with autoSetupMerge=inherit' '
662 + git reset --hard main &&
663 + # default config does not copy tracking info
664 + git checkout -b foo-no-inherit koala/bear &&
665 + test_cmp_config "" --default "" branch.foo-no-inherit.remote &&
666 + test_cmp_config "" --default "" branch.foo-no-inherit.merge &&
667 + # with autoSetupMerge=inherit, we copy tracking info from koala/bear
668 + test_config branch.autoSetupMerge inherit &&
669 + git checkout -b foo koala/bear &&
670 + test_cmp_config origin branch.foo.remote &&
671 + test_cmp_config refs/heads/koala/bear branch.foo.merge &&
672 + # no tracking info to inherit from main
673 + git checkout -b main2 main &&
674 + test_cmp_config "" --default "" branch.main2.remote &&
675 + test_cmp_config "" --default "" branch.main2.merge
676 +'
677 +
678 test_done