Skip to content

Commit 27852b2

Browse files
pks-tgitster
authored andcommitted
branch: report errors in tracking branch setup
When setting up a new tracking branch fails due to issues with the configuration file we do not report any errors to the user and pretend setting the tracking branch succeeded. Setting up the tracking branch is handled by the `install_branch_config` function. We do not want to simply die there as the function is not only invoked when explicitly setting upstream information with `git branch --set-upstream-to=`, but also by `git push --set-upstream` and `git clone`. While it is reasonable to die in the explict first case, we would lose information in the latter two cases, so we only print the error message but continue the program as usual. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b4c8aba commit 27852b2

File tree

3 files changed

+46
-16
lines changed

3 files changed

+46
-16
lines changed

branch.c

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ static int should_setup_rebase(const char *origin)
4949
return 0;
5050
}
5151

52-
void install_branch_config(int flag, const char *local, const char *origin, const char *remote)
52+
static const char tracking_advice[] =
53+
N_("\n"
54+
"After fixing the error cause you may try to fix up\n"
55+
"the remote tracking information by invoking\n"
56+
"\"git branch --set-upstream-to=%s%s%s\".");
57+
58+
int install_branch_config(int flag, const char *local, const char *origin, const char *remote)
5359
{
5460
const char *shortname = NULL;
5561
struct strbuf key = STRBUF_INIT;
@@ -60,20 +66,23 @@ void install_branch_config(int flag, const char *local, const char *origin, cons
6066
&& !origin) {
6167
warning(_("Not setting branch %s as its own upstream."),
6268
local);
63-
return;
69+
return 0;
6470
}
6571

6672
strbuf_addf(&key, "branch.%s.remote", local);
67-
git_config_set(key.buf, origin ? origin : ".");
73+
if (git_config_set(key.buf, origin ? origin : ".") < 0)
74+
goto out_err;
6875

6976
strbuf_reset(&key);
7077
strbuf_addf(&key, "branch.%s.merge", local);
71-
git_config_set(key.buf, remote);
78+
if (git_config_set(key.buf, remote) < 0)
79+
goto out_err;
7280

7381
if (rebasing) {
7482
strbuf_reset(&key);
7583
strbuf_addf(&key, "branch.%s.rebase", local);
76-
git_config_set(key.buf, "true");
84+
if (git_config_set(key.buf, "true") < 0)
85+
goto out_err;
7786
}
7887
strbuf_release(&key);
7988

@@ -102,23 +111,36 @@ void install_branch_config(int flag, const char *local, const char *origin, cons
102111
local, remote);
103112
}
104113
}
114+
115+
return 0;
116+
117+
out_err:
118+
strbuf_release(&key);
119+
error(_("Unable to write upstream branch configuration"));
120+
121+
advise(_(tracking_advice),
122+
origin ? origin : "",
123+
origin ? "/" : "",
124+
shortname ? shortname : remote);
125+
126+
return -1;
105127
}
106128

107129
/*
108130
* This is called when new_ref is branched off of orig_ref, and tries
109131
* to infer the settings for branch.<new_ref>.{remote,merge} from the
110132
* config.
111133
*/
112-
static int setup_tracking(const char *new_ref, const char *orig_ref,
113-
enum branch_track track, int quiet)
134+
static void setup_tracking(const char *new_ref, const char *orig_ref,
135+
enum branch_track track, int quiet)
114136
{
115137
struct tracking tracking;
116138
int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE;
117139

118140
memset(&tracking, 0, sizeof(tracking));
119141
tracking.spec.dst = (char *)orig_ref;
120142
if (for_each_remote(find_tracked_branch, &tracking))
121-
return 1;
143+
return;
122144

123145
if (!tracking.matches)
124146
switch (track) {
@@ -127,18 +149,18 @@ static int setup_tracking(const char *new_ref, const char *orig_ref,
127149
case BRANCH_TRACK_OVERRIDE:
128150
break;
129151
default:
130-
return 1;
152+
return;
131153
}
132154

133155
if (tracking.matches > 1)
134-
return error(_("Not tracking: ambiguous information for ref %s"),
135-
orig_ref);
156+
die(_("Not tracking: ambiguous information for ref %s"),
157+
orig_ref);
136158

137-
install_branch_config(config_flags, new_ref, tracking.remote,
138-
tracking.src ? tracking.src : orig_ref);
159+
if (install_branch_config(config_flags, new_ref, tracking.remote,
160+
tracking.src ? tracking.src : orig_ref) < 0)
161+
exit(-1);
139162

140163
free(tracking.src);
141-
return 0;
142164
}
143165

144166
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
@@ -43,9 +43,10 @@ void remove_branch_state(void);
4343
/*
4444
* Configure local branch "local" as downstream to branch "remote"
4545
* from remote "origin". Used by git branch --set-upstream.
46+
* Returns 0 on success.
4647
*/
4748
#define BRANCH_CONFIG_VERBOSE 01
48-
extern void install_branch_config(int flag, const char *local, const char *origin, const char *remote);
49+
extern int install_branch_config(int flag, const char *local, const char *origin, const char *remote);
4950

5051
/*
5152
* Read branch description

t/t3200-branch.sh

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,13 @@ test_expect_success '--set-upstream-to fails on a non-ref' '
446446
test_must_fail git branch --set-upstream-to HEAD^{}
447447
'
448448

449+
test_expect_success '--set-upstream-to fails on locked config' '
450+
test_when_finished "rm -f .git/config.lock" &&
451+
>.git/config.lock &&
452+
git branch locked &&
453+
test_must_fail git branch --set-upstream-to locked
454+
'
455+
449456
test_expect_success 'use --set-upstream-to modify HEAD' '
450457
test_config branch.master.remote foo &&
451458
test_config branch.master.merge foo &&
@@ -579,7 +586,7 @@ test_expect_success 'avoid ambiguous track' '
579586
git config remote.ambi1.fetch refs/heads/lalala:refs/heads/master &&
580587
git config remote.ambi2.url lilili &&
581588
git config remote.ambi2.fetch refs/heads/lilili:refs/heads/master &&
582-
git branch all1 master &&
589+
test_must_fail git branch all1 master &&
583590
test -z "$(git config branch.all1.merge)"
584591
'
585592

0 commit comments

Comments
 (0)