Skip to content

Commit 3a429d0

Browse files
peffgitster
authored andcommitted
remote.c: report specific errors from branch_get_upstream
When the previous commit introduced the branch_get_upstream helper, there was one call-site that could not be converted: the one in sha1_name.c, which gives detailed error messages for each possible failure. Let's teach the helper to optionally report these specific errors. This lets us convert another callsite, and means we can use the helper in other locations that want to give the same error messages. The logic and error messages come straight from sha1_name.c, with the exception that we start each error with a lowercase letter, as is our usual style (note that a few tests need updated as a result). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a9f9f8c commit 3a429d0

File tree

7 files changed

+48
-30
lines changed

7 files changed

+48
-30
lines changed

builtin/branch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ static int branch_merged(int kind, const char *name,
123123

124124
if (kind == REF_LOCAL_BRANCH) {
125125
struct branch *branch = branch_get(name);
126-
const char *upstream = branch_get_upstream(branch);
126+
const char *upstream = branch_get_upstream(branch, NULL);
127127
unsigned char sha1[20];
128128

129129
if (upstream &&

builtin/for-each-ref.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ static void populate_value(struct refinfo *ref)
664664
continue;
665665
branch = branch_get(ref->refname + 11);
666666

667-
refname = branch_get_upstream(branch);
667+
refname = branch_get_upstream(branch, NULL);
668668
if (!refname)
669669
continue;
670670
} else if (starts_with(name, "color:")) {

builtin/log.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1632,7 +1632,7 @@ int cmd_cherry(int argc, const char **argv, const char *prefix)
16321632
break;
16331633
default:
16341634
current_branch = branch_get(NULL);
1635-
upstream = branch_get_upstream(current_branch);
1635+
upstream = branch_get_upstream(current_branch, NULL);
16361636
if (!upstream) {
16371637
fprintf(stderr, _("Could not find a tracked"
16381638
" remote branch, please"

remote.c

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,10 +1705,35 @@ int branch_merge_matches(struct branch *branch,
17051705
return refname_match(branch->merge[i]->src, refname);
17061706
}
17071707

1708-
const char *branch_get_upstream(struct branch *branch)
1708+
__attribute((format (printf,2,3)))
1709+
static const char *error_buf(struct strbuf *err, const char *fmt, ...)
17091710
{
1710-
if (!branch || !branch->merge || !branch->merge[0])
1711-
return NULL;
1711+
if (err) {
1712+
va_list ap;
1713+
va_start(ap, fmt);
1714+
strbuf_vaddf(err, fmt, ap);
1715+
va_end(ap);
1716+
}
1717+
return NULL;
1718+
}
1719+
1720+
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) {
1725+
if (!ref_exists(branch->refname))
1726+
return error_buf(err, _("no such branch: '%s'"),
1727+
branch->name);
1728+
if (!branch->merge)
1729+
return error_buf(err,
1730+
_("no upstream configured for branch '%s'"),
1731+
branch->name);
1732+
return error_buf(err,
1733+
_("upstream branch '%s' not stored as a remote-tracking branch"),
1734+
branch->merge[0]->src);
1735+
}
1736+
17121737
return branch->merge[0]->dst;
17131738
}
17141739

@@ -1921,7 +1946,7 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs)
19211946
int rev_argc;
19221947

19231948
/* Cannot stat unless we are marked to build on top of somebody else. */
1924-
base = branch_get_upstream(branch);
1949+
base = branch_get_upstream(branch, NULL);
19251950
if (!base)
19261951
return 0;
19271952

remote.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,12 @@ int branch_merge_matches(struct branch *, int n, const char *);
222222
* Return the fully-qualified refname of the tracking branch for `branch`.
223223
* I.e., what "branch@{upstream}" would give you. Returns NULL if no
224224
* upstream is defined.
225+
*
226+
* If `err` is not NULL and no upstream is defined, a more specific error
227+
* message is recorded there (if the function does not return NULL, then
228+
* `err` is not touched).
225229
*/
226-
const char *branch_get_upstream(struct branch *branch);
230+
const char *branch_get_upstream(struct branch *branch, struct strbuf *err);
227231

228232
/* Flags to match_refs. */
229233
enum match_refs_flags {

sha1_name.c

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,27 +1059,16 @@ static const char *get_upstream_branch(const char *branch_buf, int len)
10591059
{
10601060
char *branch = xstrndup(branch_buf, len);
10611061
struct branch *upstream = branch_get(*branch ? branch : NULL);
1062+
struct strbuf err = STRBUF_INIT;
1063+
const char *ret;
10621064

1063-
/*
1064-
* Upstream can be NULL only if branch refers to HEAD and HEAD
1065-
* points to something different than a branch.
1066-
*/
1067-
if (!upstream)
1068-
die(_("HEAD does not point to a branch"));
1069-
if (!upstream->merge || !upstream->merge[0]->dst) {
1070-
if (!ref_exists(upstream->refname))
1071-
die(_("No such branch: '%s'"), branch);
1072-
if (!upstream->merge) {
1073-
die(_("No upstream configured for branch '%s'"),
1074-
upstream->name);
1075-
}
1076-
die(
1077-
_("Upstream branch '%s' not stored as a remote-tracking branch"),
1078-
upstream->merge[0]->src);
1079-
}
10801065
free(branch);
10811066

1082-
return upstream->merge[0]->dst;
1067+
ret = branch_get_upstream(upstream, &err);
1068+
if (!ret)
1069+
die("%s", err.buf);
1070+
1071+
return ret;
10831072
}
10841073

10851074
static int interpret_upstream_mark(const char *name, int namelen,

t/t1507-rev-parse-upstream.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,23 +150,23 @@ test_expect_success 'branch@{u} works when tracking a local branch' '
150150

151151
test_expect_success 'branch@{u} error message when no upstream' '
152152
cat >expect <<-EOF &&
153-
fatal: No upstream configured for branch ${sq}non-tracking${sq}
153+
fatal: no upstream configured for branch ${sq}non-tracking${sq}
154154
EOF
155155
error_message non-tracking@{u} 2>actual &&
156156
test_i18ncmp expect actual
157157
'
158158

159159
test_expect_success '@{u} error message when no upstream' '
160160
cat >expect <<-EOF &&
161-
fatal: No upstream configured for branch ${sq}master${sq}
161+
fatal: no upstream configured for branch ${sq}master${sq}
162162
EOF
163163
test_must_fail git rev-parse --verify @{u} 2>actual &&
164164
test_i18ncmp expect actual
165165
'
166166

167167
test_expect_success 'branch@{u} error message with misspelt branch' '
168168
cat >expect <<-EOF &&
169-
fatal: No such branch: ${sq}no-such-branch${sq}
169+
fatal: no such branch: ${sq}no-such-branch${sq}
170170
EOF
171171
error_message no-such-branch@{u} 2>actual &&
172172
test_i18ncmp expect actual
@@ -183,7 +183,7 @@ test_expect_success '@{u} error message when not on a branch' '
183183

184184
test_expect_success 'branch@{u} error message if upstream branch not fetched' '
185185
cat >expect <<-EOF &&
186-
fatal: Upstream branch ${sq}refs/heads/side${sq} not stored as a remote-tracking branch
186+
fatal: upstream branch ${sq}refs/heads/side${sq} not stored as a remote-tracking branch
187187
EOF
188188
error_message bad-upstream@{u} 2>actual &&
189189
test_i18ncmp expect actual

0 commit comments

Comments
 (0)