remote.c: untangle error logic in branch_get_upstream

The error-diagnosis logic in branch_get_upstream was copied straight from sha1_name.c in the previous commit. However, because we check all error cases and upfront and then later diagnose them, the logic is a bit tangled. In particular: - if branch->merge[0] is NULL, we may end up dereferencing it for an error message (in practice, it should never be NULL, so this is probably not a triggerable bug). - We may enter the code path because branch->merge[0]->dst is NULL, but we then start our error diagnosis by checking whether our local branch exists. But that is only relevant to diagnosing missing merge config, not a missing tracking ref; our diagnosis may hide the real problem. Instead, let's just use a sequence of "if" blocks to check for each error type, diagnose it, and return NULL. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed May 21, 2015 at 20:46 UTC 1ca41a19323d455cf0028001677f3adfae0d4cc4
1 file changed +13 -6
remote.c
+13 -6
@@ -1721,18 +1721,25 @@ const char *branch_get_upstream(struct branch *branch, struct strbuf *err)
1721 {
1722 if (!branch)
1723 return error_buf(err, _("HEAD does not point to a branch"));
1724 - if (!branch->merge || !branch->merge[0] || !branch->merge[0]->dst) {
1724 +
1725 + if (!branch->merge || !branch->merge[0]) {
1726 + /*
1727 + * no merge config; is it because the user didn't define any,
1728 + * or because it is not a real branch, and get_branch
1729 + * auto-vivified it?
1730 + */
1731 if (!ref_exists(branch->refname))
1732 return error_buf(err, _("no such branch: '%s'"),
1733 branch->name);
1728 - if (!branch->merge)
1729 - return error_buf(err,
1730 - _("no upstream configured for branch '%s'"),
1731 - branch->name);
1734 + return error_buf(err,
1735 + _("no upstream configured for branch '%s'"),
1736 + branch->name);
1737 + }
1738 +
1739 + if (!branch->merge[0]->dst)
1740 return error_buf(err,
1741 _("upstream branch '%s' not stored as a remote-tracking branch"),
1742 branch->merge[0]->src);
1735 - }
1743
1744 return branch->merge[0]->dst;
1745 }