Skip to content

Commit d311566

Browse files
steadmongitster
authored andcommitted
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/[email protected]/ Signed-off-by: Josh Steadmon <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a3f40ec commit d311566

16 files changed

+205
-23
lines changed

Documentation/config/branch.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ branch.autoSetupMerge::
77
automatic setup is done; `true` -- automatic setup is done when the
88
starting point is a remote-tracking branch; `always` --
99
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
1112
branch. This option defaults to true.
1213

1314
branch.autoSetupRebase::

Documentation/git-branch.txt

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ SYNOPSIS
1616
[--points-at <object>] [--format=<format>]
1717
[(-r | --remotes) | (-a | --all)]
1818
[--list] [<pattern>...]
19-
'git branch' [--track | --no-track] [-f] <branchname> [<start-point>]
19+
'git branch' [--track [direct|inherit] | --no-track] [-f] <branchname> [<start-point>]
2020
'git branch' (--set-upstream-to=<upstream> | -u <upstream>) [<branchname>]
2121
'git branch' --unset-upstream [<branchname>]
2222
'git branch' (-m | -M) [<oldbranch>] <newbranch>
@@ -206,24 +206,34 @@ This option is only applicable in non-verbose mode.
206206
Display the full sha1s in the output listing rather than abbreviating them.
207207

208208
-t::
209-
--track::
209+
--track [inherit|direct]::
210210
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
213213
configuration will tell git to show the relationship between the
214214
two branches in `git status` and `git branch -v`. Furthermore,
215215
it directs `git pull` without arguments to pull from the
216216
upstream when the new branch is checked out.
217217
+
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.
219224
Set the branch.autoSetupMerge configuration variable to `false` if you
220225
want `git switch`, `git checkout` and `git branch` to always behave as if `--no-track`
221226
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.
223233

224234
--no-track::
225235
Do not set up "upstream" configuration, even if the
226-
branch.autoSetupMerge configuration variable is true.
236+
branch.autoSetupMerge configuration variable is set.
227237

228238
--set-upstream::
229239
As this option had confusing syntax, it is no longer supported.

Documentation/git-checkout.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ of it").
156156
linkgit:git-branch[1] for details.
157157

158158
-t::
159-
--track::
159+
--track [direct|inherit]::
160160
When creating a new branch, set up "upstream" configuration. See
161161
"--track" in linkgit:git-branch[1] for details.
162162
+

Documentation/git-switch.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ should result in deletion of the path).
152152
attached to a terminal, regardless of `--quiet`.
153153

154154
-t::
155-
--track::
155+
--track [direct|inherit]::
156156
When creating a new branch, set up "upstream" configuration.
157157
`-c` is implied. See `--track` in linkgit:git-branch[1] for
158158
details.

branch.c

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
struct tracking {
1313
struct refspec_item spec;
14-
char *src;
14+
struct string_list *srcs;
1515
const char *remote;
1616
int matches;
1717
};
@@ -22,11 +22,11 @@ static int find_tracked_branch(struct remote *remote, void *priv)
2222

2323
if (!remote_find_tracking(remote, &tracking->spec)) {
2424
if (++tracking->matches == 1) {
25-
tracking->src = tracking->spec.src;
25+
string_list_append(tracking->srcs, tracking->spec.src);
2626
tracking->remote = remote->name;
2727
} else {
2828
free(tracking->spec.src);
29-
FREE_AND_NULL(tracking->src);
29+
string_list_clear(tracking->srcs, 0);
3030
}
3131
tracking->spec.src = NULL;
3232
}
@@ -189,6 +189,34 @@ int install_branch_config(int flag, const char *local, const char *origin,
189189
return ret;
190190
}
191191

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+
192220
/*
193221
* This is called when new_ref is branched off of orig_ref, and tries
194222
* to infer the settings for branch.<new_ref>.{remote,merge} from the
@@ -198,18 +226,23 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,
198226
enum branch_track track, int quiet)
199227
{
200228
struct tracking tracking;
229+
struct string_list tracking_srcs = STRING_LIST_INIT_DUP;
201230
int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE;
202231

203232
memset(&tracking, 0, sizeof(tracking));
204233
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))
206238
return;
207239

208240
if (!tracking.matches)
209241
switch (track) {
210242
case BRANCH_TRACK_ALWAYS:
211243
case BRANCH_TRACK_EXPLICIT:
212244
case BRANCH_TRACK_OVERRIDE:
245+
case BRANCH_TRACK_INHERIT:
213246
break;
214247
default:
215248
return;
@@ -219,11 +252,13 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,
219252
die(_("Not tracking: ambiguous information for ref %s"),
220253
orig_ref);
221254

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)
224259
exit(-1);
225260

226-
free(tracking.src);
261+
string_list_clear(tracking.srcs, 0);
227262
}
228263

229264
int read_branch_desc(struct strbuf *buf, const char *branch_name)

branch.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ enum branch_track {
1010
BRANCH_TRACK_REMOTE,
1111
BRANCH_TRACK_ALWAYS,
1212
BRANCH_TRACK_EXPLICIT,
13-
BRANCH_TRACK_OVERRIDE
13+
BRANCH_TRACK_OVERRIDE,
14+
BRANCH_TRACK_INHERIT,
1415
};
1516

1617
extern enum branch_track git_branch_track;

builtin/branch.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -632,8 +632,10 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
632632
OPT__VERBOSE(&filter.verbose,
633633
N_("show hash and subject, give twice for upstream branch")),
634634
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),
637639
OPT_SET_INT_F(0, "set-upstream", &track, N_("do not use"),
638640
BRANCH_TRACK_OVERRIDE, PARSE_OPT_HIDDEN),
639641
OPT_STRING('u', "set-upstream-to", &new_upstream, N_("upstream"), N_("change the upstream info")),

builtin/checkout.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,8 +1530,10 @@ static struct option *add_common_switch_branch_options(
15301530
{
15311531
struct option options[] = {
15321532
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),
15351537
OPT__FORCE(&opts->force, N_("force checkout (throw away local modifications)"),
15361538
PARSE_OPT_NOCOMPLETE),
15371539
OPT_STRING(0, "orphan", &opts->new_orphan_branch, N_("new-branch"), N_("new unparented branch")),

config.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,6 +1559,9 @@ static int git_default_branch_config(const char *var, const char *value)
15591559
if (value && !strcasecmp(value, "always")) {
15601560
git_branch_track = BRANCH_TRACK_ALWAYS;
15611561
return 0;
1562+
} else if (value && !strcmp(value, "inherit")) {
1563+
git_branch_track = BRANCH_TRACK_INHERIT;
1564+
return 0;
15621565
}
15631566
git_branch_track = git_config_bool(var, value);
15641567
return 0;

parse-options-cb.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "git-compat-util.h"
22
#include "parse-options.h"
3+
#include "branch.h"
34
#include "cache.h"
45
#include "commit.h"
56
#include "color.h"
@@ -293,3 +294,18 @@ int parse_opt_passthru_argv(const struct option *opt, const char *arg, int unset
293294

294295
return 0;
295296
}
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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ enum parse_opt_result parse_opt_unknown_cb(struct parse_opt_ctx_t *ctx,
302302
const char *, int);
303303
int parse_opt_passthru(const struct option *, const char *, int);
304304
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);
305307

306308
#define OPT__VERBOSE(var, h) OPT_COUNTUP('v', "verbose", (var), (h))
307309
#define OPT__QUIET(var, h) OPT_COUNTUP('q', "quiet", (var), (h))

t/t2017-checkout-orphan.sh

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,17 @@ test_expect_success '--orphan ignores branch.autosetupmerge' '
6262
git checkout main &&
6363
git config branch.autosetupmerge always &&
6464
git checkout --orphan gamma &&
65-
test -z "$(git config branch.gamma.merge)" &&
65+
test_cmp_config "" --default "" branch.gamma.merge &&
6666
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 &&
6776
test_must_fail git rev-parse --verify HEAD^
6877
'
6978

t/t2027-checkout-track.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,27 @@ test_expect_success 'checkout --track -b rejects an extra path argument' '
2424
test_i18ngrep "cannot be used with updating paths" err
2525
'
2626

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+
2750
test_done

t/t2060-switch.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,32 @@ test_expect_success 'not switching when something is in progress' '
107107
test_must_fail git switch -d @^
108108
'
109109

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+
110138
test_done

t/t3200-branch.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,4 +1422,37 @@ test_expect_success 'invalid sort parameter in configuration' '
14221422
)
14231423
'
14241424

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+
14251458
test_done

t/t7201-co.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,4 +658,21 @@ test_expect_success 'custom merge driver with checkout -m' '
658658
test_cmp expect arm
659659
'
660660

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+
661678
test_done

0 commit comments

Comments
 (0)