Skip to content

Commit 1ca41a1

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3a429d0 commit 1ca41a1

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

remote.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,18 +1721,25 @@ const char *branch_get_upstream(struct branch *branch, struct strbuf *err)
17211721
{
17221722
if (!branch)
17231723
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+
*/
17251731
if (!ref_exists(branch->refname))
17261732
return error_buf(err, _("no such branch: '%s'"),
17271733
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)
17321740
return error_buf(err,
17331741
_("upstream branch '%s' not stored as a remote-tracking branch"),
17341742
branch->merge[0]->src);
1735-
}
17361743

17371744
return branch->merge[0]->dst;
17381745
}

0 commit comments

Comments
 (0)