提交 05e73682 编写于 作者: J Junio C Hamano

checkout: report upstream correctly even with loosely defined branch.*.merge

When checking out a branch that is set to build on top of another
branch (often, a remote-tracking branch), "git checkout" reports how
your work relates to the other branch, e.g.

    Your branch is behind 'origin/master', and can be fast-forwarded.

Back when this feature was introduced, this was only done for
branches that build on remote-tracking branches, but 5e6e2b48 (Make
local branches behave like remote branches when --tracked,
2009-04-01) added support to give the same report for branches that
build on other local branches (i.e. branches whose branch.*.remote
variables are set to '.').  Unlike the support for the branches
building on remote-tracking branches, however, this did not take
into account the fact that branch.*.merge configuration is allowed
to record a shortened branch name.

When branch.*.merge is set to 'master' (not 'refs/heads/master'),
i.e. "my branch builds on the local 'master' branch", this caused
"git checkout" to report:

    Your branch is based on 'master', but the upstream is gone.

The upstream is our repository and is definitely not gone, so this
output is nonsense.

The fix is fairly obvious; just like the branch name is DWIMed when
"git pull" merges from the 'master' branch without complaint on such
a branch, the name of the branch the current branch builds upon
needs to be DWIMed the same way.
Signed-off-by: NJunio C Hamano <gitster@pobox.com>
上级 76f8611a
...@@ -1611,6 +1611,27 @@ void set_ref_status_for_push(struct ref *remote_refs, int send_mirror, ...@@ -1611,6 +1611,27 @@ void set_ref_status_for_push(struct ref *remote_refs, int send_mirror,
} }
} }
static void set_merge(struct branch *ret)
{
char *ref;
unsigned char sha1[20];
int i;
ret->merge = xcalloc(ret->merge_nr, sizeof(*ret->merge));
for (i = 0; i < ret->merge_nr; i++) {
ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
ret->merge[i]->src = xstrdup(ret->merge_name[i]);
if (!remote_find_tracking(ret->remote, ret->merge[i]) ||
strcmp(ret->remote_name, "."))
continue;
if (dwim_ref(ret->merge_name[i], strlen(ret->merge_name[i]),
sha1, &ref) == 1)
ret->merge[i]->dst = ref;
else
ret->merge[i]->dst = xstrdup(ret->merge_name[i]);
}
}
struct branch *branch_get(const char *name) struct branch *branch_get(const char *name)
{ {
struct branch *ret; struct branch *ret;
...@@ -1622,17 +1643,8 @@ struct branch *branch_get(const char *name) ...@@ -1622,17 +1643,8 @@ struct branch *branch_get(const char *name)
ret = make_branch(name, 0); ret = make_branch(name, 0);
if (ret && ret->remote_name) { if (ret && ret->remote_name) {
ret->remote = remote_get(ret->remote_name); ret->remote = remote_get(ret->remote_name);
if (ret->merge_nr) { if (ret->merge_nr)
int i; set_merge(ret);
ret->merge = xcalloc(ret->merge_nr, sizeof(*ret->merge));
for (i = 0; i < ret->merge_nr; i++) {
ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
ret->merge[i]->src = xstrdup(ret->merge_name[i]);
if (remote_find_tracking(ret->remote, ret->merge[i])
&& !strcmp(ret->remote_name, "."))
ret->merge[i]->dst = xstrdup(ret->merge_name[i]);
}
}
} }
return ret; return ret;
} }
......
...@@ -185,4 +185,22 @@ test_expect_success 'checkout <branch> -- succeeds, even if a file with the same ...@@ -185,4 +185,22 @@ test_expect_success 'checkout <branch> -- succeeds, even if a file with the same
test_branch_upstream spam repo_c spam test_branch_upstream spam repo_c spam
' '
test_expect_success 'loosely defined local base branch is reported correctly' '
git checkout master &&
git branch strict &&
git branch loose &&
git commit --allow-empty -m "a bit more" &&
test_config branch.strict.remote . &&
test_config branch.loose.remote . &&
test_config branch.strict.merge refs/heads/master &&
test_config branch.loose.merge master &&
git checkout strict | sed -e "s/strict/BRANCHNAME/g" >expect &&
git checkout loose | sed -e "s/loose/BRANCHNAME/g" >actual &&
test_cmp expect actual
'
test_done test_done
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册