Skip to content

Commit 88dccb6

Browse files
committed
Merge branch 'jk/set-upstream-error-cases'
The handing by "git branch --set-upstream-to" against various forms of errorneous inputs were suboptimal. * jk/set-upstream-error-cases: branch: give advice when tracking start-point is missing branch: mention start_name in set-upstream error messages branch: improve error message for missing --set-upstream-to ref branch: factor out "upstream is not a branch" error messages t3200: test --set-upstream-to with bogus refs
2 parents 9a11f13 + caa2036 commit 88dccb6

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

advice.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ int advice_commit_before_merge = 1;
1313
int advice_resolve_conflict = 1;
1414
int advice_implicit_identity = 1;
1515
int advice_detached_head = 1;
16+
int advice_set_upstream_failure = 1;
1617

1718
static struct {
1819
const char *name;
@@ -31,6 +32,7 @@ static struct {
3132
{ "resolveconflict", &advice_resolve_conflict },
3233
{ "implicitidentity", &advice_implicit_identity },
3334
{ "detachedhead", &advice_detached_head },
35+
{ "setupstreamfailure", &advice_set_upstream_failure },
3436

3537
/* make this an alias for backward compatibility */
3638
{ "pushnonfastforward", &advice_push_update_rejected }

advice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ extern int advice_commit_before_merge;
1616
extern int advice_resolve_conflict;
1717
extern int advice_implicit_identity;
1818
extern int advice_detached_head;
19+
extern int advice_set_upstream_failure;
1920

2021
int git_default_advice_config(const char *var, const char *value);
2122
void advise(const char *advice, ...);

branch.c

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,20 @@ int validate_new_branchname(const char *name, struct strbuf *ref,
197197
return 1;
198198
}
199199

200+
static const char upstream_not_branch[] =
201+
N_("Cannot setup tracking information; starting point '%s' is not a branch.");
202+
static const char upstream_missing[] =
203+
N_("the requested upstream branch '%s' does not exist");
204+
static const char upstream_advice[] =
205+
N_("\n"
206+
"If you are planning on basing your work on an upstream\n"
207+
"branch that already exists at the remote, you may need to\n"
208+
"run \"git fetch\" to retrieve it.\n"
209+
"\n"
210+
"If you are planning to push out a new local branch that\n"
211+
"will track its remote counterpart, you may want to use\n"
212+
"\"git push -u\" to set the upstream config as you push.");
213+
200214
void create_branch(const char *head,
201215
const char *name, const char *start_name,
202216
int force, int reflog, int clobber_head,
@@ -224,21 +238,30 @@ void create_branch(const char *head,
224238
}
225239

226240
real_ref = NULL;
227-
if (get_sha1(start_name, sha1))
241+
if (get_sha1(start_name, sha1)) {
242+
if (explicit_tracking) {
243+
if (advice_set_upstream_failure) {
244+
error(_(upstream_missing), start_name);
245+
advise(_(upstream_advice));
246+
exit(1);
247+
}
248+
die(_(upstream_missing), start_name);
249+
}
228250
die("Not a valid object name: '%s'.", start_name);
251+
}
229252

230253
switch (dwim_ref(start_name, strlen(start_name), sha1, &real_ref)) {
231254
case 0:
232255
/* Not branching from any existing branch */
233256
if (explicit_tracking)
234-
die("Cannot setup tracking information; starting point is not a branch.");
257+
die(_(upstream_not_branch), start_name);
235258
break;
236259
case 1:
237260
/* Unique completion -- good, only if it is a real branch */
238261
if (prefixcmp(real_ref, "refs/heads/") &&
239262
prefixcmp(real_ref, "refs/remotes/")) {
240263
if (explicit_tracking)
241-
die("Cannot setup tracking information; starting point is not a branch.");
264+
die(_(upstream_not_branch), start_name);
242265
else
243266
real_ref = NULL;
244267
}

t/t3200-branch.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,18 @@ test_expect_success '--set-upstream-to fails on detached HEAD' '
409409
git checkout -
410410
'
411411

412+
test_expect_success '--set-upstream-to fails on a missing dst branch' '
413+
test_must_fail git branch --set-upstream-to master does-not-exist
414+
'
415+
416+
test_expect_success '--set-upstream-to fails on a missing src branch' '
417+
test_must_fail git branch --set-upstream-to does-not-exist master
418+
'
419+
420+
test_expect_success '--set-upstream-to fails on a non-ref' '
421+
test_must_fail git branch --set-upstream-to HEAD^{}
422+
'
423+
412424
test_expect_success 'use --set-upstream-to modify HEAD' '
413425
test_config branch.master.remote foo &&
414426
test_config branch.master.merge foo &&

0 commit comments

Comments
 (0)