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: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Oct 14, 2014 at 14:42 UTC 05e73682cd28eeaf6d949badefd85b94244fca3f
2 files changed +41 -11
remote.c
+23 -11
@@ -1611,6 +1611,27 @@ void set_ref_status_for_push(struct ref *remote_refs, int send_mirror,
1611 }
1612 }
1613
1614 +static void set_merge(struct branch *ret)
1615 +{
1616 + char *ref;
1617 + unsigned char sha1[20];
1618 + int i;
1619 +
1620 + ret->merge = xcalloc(ret->merge_nr, sizeof(*ret->merge));
1621 + for (i = 0; i < ret->merge_nr; i++) {
1622 + ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
1623 + ret->merge[i]->src = xstrdup(ret->merge_name[i]);
1624 + if (!remote_find_tracking(ret->remote, ret->merge[i]) ||
1625 + strcmp(ret->remote_name, "."))
1626 + continue;
1627 + if (dwim_ref(ret->merge_name[i], strlen(ret->merge_name[i]),
1628 + sha1, &ref) == 1)
1629 + ret->merge[i]->dst = ref;
1630 + else
1631 + ret->merge[i]->dst = xstrdup(ret->merge_name[i]);
1632 + }
1633 +}
1634 +
1635 struct branch *branch_get(const char *name)
1636 {
1637 struct branch *ret;
@@ -1622,17 +1643,8 @@ struct branch *branch_get(const char *name)
1643 ret = make_branch(name, 0);
1644 if (ret && ret->remote_name) {
1645 ret->remote = remote_get(ret->remote_name);
1625 - if (ret->merge_nr) {
1626 - int i;
1627 - ret->merge = xcalloc(ret->merge_nr, sizeof(*ret->merge));
1628 - for (i = 0; i < ret->merge_nr; i++) {
1629 - ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
1630 - ret->merge[i]->src = xstrdup(ret->merge_name[i]);
1631 - if (remote_find_tracking(ret->remote, ret->merge[i])
1632 - && !strcmp(ret->remote_name, "."))
1633 - ret->merge[i]->dst = xstrdup(ret->merge_name[i]);
1634 - }
1635 - }
1646 + if (ret->merge_nr)
1647 + set_merge(ret);
1648 }
1649 return ret;
1650 }
t/t2024-checkout-dwim.sh
+18
@@ -185,4 +185,22 @@ test_expect_success 'checkout <branch> -- succeeds, even if a file with the same
185 test_branch_upstream spam repo_c spam
186 '
187
188 +test_expect_success 'loosely defined local base branch is reported correctly' '
189 +
190 + git checkout master &&
191 + git branch strict &&
192 + git branch loose &&
193 + git commit --allow-empty -m "a bit more" &&
194 +
195 + test_config branch.strict.remote . &&
196 + test_config branch.loose.remote . &&
197 + test_config branch.strict.merge refs/heads/master &&
198 + test_config branch.loose.merge master &&
199 +
200 + git checkout strict | sed -e "s/strict/BRANCHNAME/g" >expect &&
201 + git checkout loose | sed -e "s/loose/BRANCHNAME/g" >actual &&
202 +
203 + test_cmp expect actual
204 +'
205 +
206 test_done