Skip to content

Commit caa2036

Browse files
peffgitster
authored andcommitted
branch: give advice when tracking start-point is missing
If the user requests to --set-upstream-to a branch that does not exist, then either: 1. It was a typo. 2. They thought the branch should exist. In case (1), there is not much we can do beyond showing the name we tried to use. For case (2), though, we can help to guide them through common workflows. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1a15d00 commit caa2036

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
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: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,16 @@ int validate_new_branchname(const char *name, struct strbuf *ref,
200200
static const char upstream_not_branch[] =
201201
N_("Cannot setup tracking information; starting point '%s' is not a branch.");
202202
static const char upstream_missing[] =
203-
N_("Cannot setup tracking information; starting point '%s' does not exist");
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.");
204213

205214
void create_branch(const char *head,
206215
const char *name, const char *start_name,
@@ -230,8 +239,14 @@ void create_branch(const char *head,
230239

231240
real_ref = NULL;
232241
if (get_sha1(start_name, sha1)) {
233-
if (explicit_tracking)
242+
if (explicit_tracking) {
243+
if (advice_set_upstream_failure) {
244+
error(_(upstream_missing), start_name);
245+
advise(_(upstream_advice));
246+
exit(1);
247+
}
234248
die(_(upstream_missing), start_name);
249+
}
235250
die("Not a valid object name: '%s'.", start_name);
236251
}
237252

0 commit comments

Comments
 (0)