Provide branch name in error message when using @{u}

When using @{u} or @{upstream} it is common to omit the branch name, implying current branch. If the upstream is not configured, the error message was "No upstream branch found for ''". When resolving '@{u}', branch_get() is called, which almost always returns a description of a branch. This allows us to use a branch name in the error message, even if the user said something like '@{u}'. The only case when branch_get() returns NULL is when HEAD points to so something which is not a branch. Of course this also means that no upstream is configured, but it is better to directly say that HEAD does not point to a branch. Signed-off-by: Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Zbigniew Jędrzejewski-Szmek committed Apr 14, 2012 at 09:54 UTC 9884e67f9d9b511592b981572cfbb30cc50aec61
2 files changed +10 -6
sha1_name.c
+8 -4
@@ -856,10 +856,14 @@ int interpret_branch_name(const char *name, struct strbuf *buf)
856 len = cp + tmp_len - name;
857 cp = xstrndup(name, cp - name);
858 upstream = branch_get(*cp ? cp : NULL);
859 - if (!upstream
860 - || !upstream->merge
861 - || !upstream->merge[0]->dst)
862 - return error("No upstream branch found for '%s'", cp);
859 + /*
860 + * Upstream can be NULL only if cp refers to HEAD and HEAD
861 + * points to something different than a branch.
862 + */
863 + if (!upstream)
864 + return error("HEAD does not point to a branch");
865 + if (!upstream->merge || !upstream->merge[0]->dst)
866 + return error("No upstream branch found for '%s'", upstream->name);
867 free(cp);
868 cp = shorten_unambiguous_ref(upstream->merge[0]->dst, 0);
869 strbuf_reset(buf);
t/t1507-rev-parse-upstream.sh
+2 -2
@@ -134,7 +134,7 @@ test_expect_success 'branch@{u} error message when no upstream' '
134
135 test_expect_success '@{u} error message when no upstream' '
136 cat >expect <<-EOF &&
137 - error: No upstream branch found for ${sq}${sq}
137 + error: No upstream branch found for ${sq}master${sq}
138 fatal: Needed a single revision
139 EOF
140 test_must_fail git rev-parse --verify @{u} 2>actual &&
@@ -152,7 +152,7 @@ test_expect_success 'branch@{u} error message with misspelt branch' '
152
153 test_expect_success '@{u} error message when not on a branch' '
154 cat >expect <<-EOF &&
155 - error: No upstream branch found for ${sq}${sq}
155 + error: HEAD does not point to a branch
156 fatal: Needed a single revision
157 EOF
158 git checkout HEAD^0 &&