Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Patrick Steinhardt authored and Junio C Hamano committed Feb 22, 2016
1 parent b4c8aba commit 27852b2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
50 changes: 36 additions & 14 deletions branch.c
Expand Up @@ -49,7 +49,13 @@ static int should_setup_rebase(const char *origin)
return 0;
}

void install_branch_config(int flag, const char *local, const char *origin, const char *remote)
static const char tracking_advice[] =
N_("\n"
"After fixing the error cause you may try to fix up\n"
"the remote tracking information by invoking\n"
"\"git branch --set-upstream-to=%s%s%s\".");

int install_branch_config(int flag, const char *local, const char *origin, const char *remote)
{
const char *shortname = NULL;
struct strbuf key = STRBUF_INIT;
Expand All @@ -60,20 +66,23 @@ void install_branch_config(int flag, const char *local, const char *origin, cons
&& !origin) {
warning(_("Not setting branch %s as its own upstream."),
local);
return;
return 0;
}

strbuf_addf(&key, "branch.%s.remote", local);
git_config_set(key.buf, origin ? origin : ".");
if (git_config_set(key.buf, origin ? origin : ".") < 0)
goto out_err;

strbuf_reset(&key);
strbuf_addf(&key, "branch.%s.merge", local);
git_config_set(key.buf, remote);
if (git_config_set(key.buf, remote) < 0)
goto out_err;

if (rebasing) {
strbuf_reset(&key);
strbuf_addf(&key, "branch.%s.rebase", local);
git_config_set(key.buf, "true");
if (git_config_set(key.buf, "true") < 0)
goto out_err;
}
strbuf_release(&key);

Expand Down Expand Up @@ -102,23 +111,36 @@ void install_branch_config(int flag, const char *local, const char *origin, cons
local, remote);
}
}

return 0;

out_err:
strbuf_release(&key);
error(_("Unable to write upstream branch configuration"));

advise(_(tracking_advice),
origin ? origin : "",
origin ? "/" : "",
shortname ? shortname : remote);

return -1;
}

/*
* 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
* config.
*/
static int setup_tracking(const char *new_ref, const char *orig_ref,
enum branch_track track, int quiet)
static void setup_tracking(const char *new_ref, const char *orig_ref,
enum branch_track track, int quiet)
{
struct tracking tracking;
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))
return 1;
return;

if (!tracking.matches)
switch (track) {
Expand All @@ -127,18 +149,18 @@ static int setup_tracking(const char *new_ref, const char *orig_ref,
case BRANCH_TRACK_OVERRIDE:
break;
default:
return 1;
return;
}

if (tracking.matches > 1)
return error(_("Not tracking: ambiguous information for ref %s"),
orig_ref);
die(_("Not tracking: ambiguous information for ref %s"),
orig_ref);

install_branch_config(config_flags, new_ref, tracking.remote,
tracking.src ? tracking.src : orig_ref);
if (install_branch_config(config_flags, new_ref, tracking.remote,
tracking.src ? tracking.src : orig_ref) < 0)
exit(-1);

free(tracking.src);
return 0;
}

int read_branch_desc(struct strbuf *buf, const char *branch_name)
Expand Down
3 changes: 2 additions & 1 deletion branch.h
Expand Up @@ -43,9 +43,10 @@ void remove_branch_state(void);
/*
* Configure local branch "local" as downstream to branch "remote"
* from remote "origin". Used by git branch --set-upstream.
* Returns 0 on success.
*/
#define BRANCH_CONFIG_VERBOSE 01
extern void install_branch_config(int flag, const char *local, const char *origin, const char *remote);
extern int install_branch_config(int flag, const char *local, const char *origin, const char *remote);

/*
* Read branch description
Expand Down
9 changes: 8 additions & 1 deletion t/t3200-branch.sh
Expand Up @@ -446,6 +446,13 @@ test_expect_success '--set-upstream-to fails on a non-ref' '
test_must_fail git branch --set-upstream-to HEAD^{}
'

test_expect_success '--set-upstream-to fails on locked config' '
test_when_finished "rm -f .git/config.lock" &&
>.git/config.lock &&
git branch locked &&
test_must_fail git branch --set-upstream-to locked
'

test_expect_success 'use --set-upstream-to modify HEAD' '
test_config branch.master.remote foo &&
test_config branch.master.merge foo &&
Expand Down Expand Up @@ -579,7 +586,7 @@ test_expect_success 'avoid ambiguous track' '
git config remote.ambi1.fetch refs/heads/lalala:refs/heads/master &&
git config remote.ambi2.url lilili &&
git config remote.ambi2.fetch refs/heads/lilili:refs/heads/master &&
git branch all1 master &&
test_must_fail git branch all1 master &&
test -z "$(git config branch.all1.merge)"
'

Expand Down

0 comments on commit 27852b2

Please sign in to comment.