sha1_name: fix error message for @{u}

Currently, when no (valid) upstream is configured for a branch, you get an error like: $ git show @{u} error: No upstream configured for branch 'upstream-error' error: No upstream configured for branch 'upstream-error' fatal: ambiguous argument '@{u}': unknown revision or path not in the working tree. Use '--' to separate paths from revisions, like this: 'git <command> [<revision>...] -- [<file>...]' The "error: " line actually appears twice, and the rest of the error message is useless. In sha1_name.c:interpret_branch_name(), there is really no point in processing further if @{u} couldn't be resolved, and we might as well die() instead of returning an error(). After making this change, you get: $ git show @{u} fatal: No upstream configured for branch 'upstream-error' Also tweak a few tests in t1507 to expect this output. This only turns error() that may be called after we know we are dealing with an @{upstream} marker into die(), without touching silent error returns "return -1" from the function. Any caller that wants to handle an error condition itself will not be hurt by this change, unless they want to see the message from error() and then exit silently without giving its own message, which needs to be fixed anyway. Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ramkumar Ramachandra committed May 22, 2013 at 16:09 UTC 17bf4ff3cd3d7fd4b252b81417df8be1b3b2b128
2 files changed +12 -16
sha1_name.c
+7 -6
@@ -1033,14 +1033,15 @@ int interpret_branch_name(const char *name, struct strbuf *buf)
1033 * points to something different than a branch.
1034 */
1035 if (!upstream)
1036 - return error(_("HEAD does not point to a branch"));
1036 + die(_("HEAD does not point to a branch"));
1037 if (!upstream->merge || !upstream->merge[0]->dst) {
1038 if (!ref_exists(upstream->refname))
1039 - return error(_("No such branch: '%s'"), cp);
1040 - if (!upstream->merge)
1041 - return error(_("No upstream configured for branch '%s'"),
1042 - upstream->name);
1043 - return error(
1039 + die(_("No such branch: '%s'"), cp);
1040 + if (!upstream->merge) {
1041 + die(_("No upstream configured for branch '%s'"),
1042 + upstream->name);
1043 + }
1044 + die(
1045 _("Upstream branch '%s' not stored as a remote-tracking branch"),
1046 upstream->merge[0]->src);
1047 }
t/t1507-rev-parse-upstream.sh
+5 -10
@@ -129,8 +129,7 @@ test_expect_success 'branch@{u} works when tracking a local branch' '
129
130 test_expect_success 'branch@{u} error message when no upstream' '
131 cat >expect <<-EOF &&
132 - error: No upstream configured for branch ${sq}non-tracking${sq}
133 - fatal: Needed a single revision
132 + fatal: No upstream configured for branch ${sq}non-tracking${sq}
133 EOF
134 error_message non-tracking@{u} 2>actual &&
135 test_i18ncmp expect actual
@@ -138,8 +137,7 @@ test_expect_success 'branch@{u} error message when no upstream' '
137
138 test_expect_success '@{u} error message when no upstream' '
139 cat >expect <<-EOF &&
141 - error: No upstream configured for branch ${sq}master${sq}
142 - fatal: Needed a single revision
140 + fatal: No upstream configured for branch ${sq}master${sq}
141 EOF
142 test_must_fail git rev-parse --verify @{u} 2>actual &&
143 test_i18ncmp expect actual
@@ -147,8 +145,7 @@ test_expect_success '@{u} error message when no upstream' '
145
146 test_expect_success 'branch@{u} error message with misspelt branch' '
147 cat >expect <<-EOF &&
150 - error: No such branch: ${sq}no-such-branch${sq}
151 - fatal: Needed a single revision
148 + fatal: No such branch: ${sq}no-such-branch${sq}
149 EOF
150 error_message no-such-branch@{u} 2>actual &&
151 test_i18ncmp expect actual
@@ -156,8 +153,7 @@ test_expect_success 'branch@{u} error message with misspelt branch' '
153
154 test_expect_success '@{u} error message when not on a branch' '
155 cat >expect <<-EOF &&
159 - error: HEAD does not point to a branch
160 - fatal: Needed a single revision
156 + fatal: HEAD does not point to a branch
157 EOF
158 git checkout HEAD^0 &&
159 test_must_fail git rev-parse --verify @{u} 2>actual &&
@@ -166,8 +162,7 @@ test_expect_success '@{u} error message when not on a branch' '
162
163 test_expect_success 'branch@{u} error message if upstream branch not fetched' '
164 cat >expect <<-EOF &&
169 - error: Upstream branch ${sq}refs/heads/side${sq} not stored as a remote-tracking branch
170 - fatal: Needed a single revision
165 + fatal: Upstream branch ${sq}refs/heads/side${sq} not stored as a remote-tracking branch
166 EOF
167 error_message bad-upstream@{u} 2>actual &&
168 test_i18ncmp expect actual