Skip to content

Commit 9884e67

Browse files
keszybzgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1b4aee9 commit 9884e67

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

sha1_name.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -856,10 +856,14 @@ int interpret_branch_name(const char *name, struct strbuf *buf)
856856
len = cp + tmp_len - name;
857857
cp = xstrndup(name, cp - name);
858858
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);
863867
free(cp);
864868
cp = shorten_unambiguous_ref(upstream->merge[0]->dst, 0);
865869
strbuf_reset(buf);

t/t1507-rev-parse-upstream.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ test_expect_success 'branch@{u} error message when no upstream' '
134134

135135
test_expect_success '@{u} error message when no upstream' '
136136
cat >expect <<-EOF &&
137-
error: No upstream branch found for ${sq}${sq}
137+
error: No upstream branch found for ${sq}master${sq}
138138
fatal: Needed a single revision
139139
EOF
140140
test_must_fail git rev-parse --verify @{u} 2>actual &&
@@ -152,7 +152,7 @@ test_expect_success 'branch@{u} error message with misspelt branch' '
152152

153153
test_expect_success '@{u} error message when not on a branch' '
154154
cat >expect <<-EOF &&
155-
error: No upstream branch found for ${sq}${sq}
155+
error: HEAD does not point to a branch
156156
fatal: Needed a single revision
157157
EOF
158158
git checkout HEAD^0 &&

0 commit comments

Comments
 (0)