From e2b6aa5f1b85d5f49f0cf280162cf216e55e1eba Mon Sep 17 00:00:00 2001 From: Jeff King Date: Tue, 2 Apr 2013 15:03:55 -0400 Subject: branch: factor out "upstream is not a branch" error messages This message is duplicated, and is quite long. Let's factor it out, which avoids the repetition and the long lines. It will also make future patches easier as we tweak the message. While we're at it, let's also mark it for translation. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- branch.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'branch.c') diff --git a/branch.c b/branch.c index 2bef1e7e71..1acbd4e6d3 100644 --- a/branch.c +++ b/branch.c @@ -197,6 +197,9 @@ int validate_new_branchname(const char *name, struct strbuf *ref, return 1; } +static const char upstream_not_branch[] = +N_("Cannot setup tracking information; starting point is not a branch."); + void create_branch(const char *head, const char *name, const char *start_name, int force, int reflog, int clobber_head, @@ -231,14 +234,14 @@ void create_branch(const char *head, case 0: /* Not branching from any existing branch */ if (explicit_tracking) - die("Cannot setup tracking information; starting point is not a branch."); + die(_(upstream_not_branch)); break; case 1: /* Unique completion -- good, only if it is a real branch */ if (prefixcmp(real_ref, "refs/heads/") && prefixcmp(real_ref, "refs/remotes/")) { if (explicit_tracking) - die("Cannot setup tracking information; starting point is not a branch."); + die(_(upstream_not_branch)); else real_ref = NULL; } -- cgit v1.2.3 From a5e91c722cceb667ad05a13f9fde150cc1dbe9aa Mon Sep 17 00:00:00 2001 From: Jeff King Date: Tue, 2 Apr 2013 15:04:27 -0400 Subject: branch: improve error message for missing --set-upstream-to ref If we are trying to set the upstream config for a branch, the create_branch function will check both that the name resolves as a ref, and that it is either a local or remote-tracking branch. However, before we do so we run get_sha1 on it to find out whether it resolves at all (since the create_branch function is also used to create actual branches, it wants to know where to start the new branch). This means that if you feed a ref that does not exist to "branch --set-upstream-to", rather than getting a helpful message about tracking, you only get "not a valid object name". Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- branch.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'branch.c') diff --git a/branch.c b/branch.c index 1acbd4e6d3..060e9e3cf8 100644 --- a/branch.c +++ b/branch.c @@ -199,6 +199,8 @@ int validate_new_branchname(const char *name, struct strbuf *ref, static const char upstream_not_branch[] = N_("Cannot setup tracking information; starting point is not a branch."); +static const char upstream_missing[] = +N_("Cannot setup tracking information; starting point does not exist"); void create_branch(const char *head, const char *name, const char *start_name, @@ -227,8 +229,11 @@ void create_branch(const char *head, } real_ref = NULL; - if (get_sha1(start_name, sha1)) + if (get_sha1(start_name, sha1)) { + if (explicit_tracking) + die(_(upstream_missing)); die("Not a valid object name: '%s'.", start_name); + } switch (dwim_ref(start_name, strlen(start_name), sha1, &real_ref)) { case 0: -- cgit v1.2.3 From 1a15d00bb910de0a86981f0d75836feac71d1fe0 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Tue, 2 Apr 2013 15:04:51 -0400 Subject: branch: mention start_name in set-upstream error messages If we refuse a branch operation because the tracking start_name the user gave us is bogus, we just print something like: fatal: Cannot setup tracking information; start point is not a branch If we mention the actual name we tried to use, that may help the user figure out why it didn't work (e.g., if they gave us the arguments in the wrong order). Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- branch.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'branch.c') diff --git a/branch.c b/branch.c index 060e9e3cf8..d6f40015d7 100644 --- a/branch.c +++ b/branch.c @@ -198,9 +198,9 @@ int validate_new_branchname(const char *name, struct strbuf *ref, } static const char upstream_not_branch[] = -N_("Cannot setup tracking information; starting point is not a branch."); +N_("Cannot setup tracking information; starting point '%s' is not a branch."); static const char upstream_missing[] = -N_("Cannot setup tracking information; starting point does not exist"); +N_("Cannot setup tracking information; starting point '%s' does not exist"); void create_branch(const char *head, const char *name, const char *start_name, @@ -231,7 +231,7 @@ void create_branch(const char *head, real_ref = NULL; if (get_sha1(start_name, sha1)) { if (explicit_tracking) - die(_(upstream_missing)); + die(_(upstream_missing), start_name); die("Not a valid object name: '%s'.", start_name); } @@ -239,14 +239,14 @@ void create_branch(const char *head, case 0: /* Not branching from any existing branch */ if (explicit_tracking) - die(_(upstream_not_branch)); + die(_(upstream_not_branch), start_name); break; case 1: /* Unique completion -- good, only if it is a real branch */ if (prefixcmp(real_ref, "refs/heads/") && prefixcmp(real_ref, "refs/remotes/")) { if (explicit_tracking) - die(_(upstream_not_branch)); + die(_(upstream_not_branch), start_name); else real_ref = NULL; } -- cgit v1.2.3 From caa2036b3b4105bfe34f01115e4fb34b78a4db86 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Tue, 2 Apr 2013 15:05:12 -0400 Subject: 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 Signed-off-by: Junio C Hamano --- branch.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'branch.c') diff --git a/branch.c b/branch.c index d6f40015d7..6ae6a4c321 100644 --- a/branch.c +++ b/branch.c @@ -200,7 +200,16 @@ int validate_new_branchname(const char *name, struct strbuf *ref, static const char upstream_not_branch[] = N_("Cannot setup tracking information; starting point '%s' is not a branch."); static const char upstream_missing[] = -N_("Cannot setup tracking information; starting point '%s' does not exist"); +N_("the requested upstream branch '%s' does not exist"); +static const char upstream_advice[] = +N_("\n" +"If you are planning on basing your work on an upstream\n" +"branch that already exists at the remote, you may need to\n" +"run \"git fetch\" to retrieve it.\n" +"\n" +"If you are planning to push out a new local branch that\n" +"will track its remote counterpart, you may want to use\n" +"\"git push -u\" to set the upstream config as you push."); void create_branch(const char *head, const char *name, const char *start_name, @@ -230,8 +239,14 @@ void create_branch(const char *head, real_ref = NULL; if (get_sha1(start_name, sha1)) { - if (explicit_tracking) + if (explicit_tracking) { + if (advice_set_upstream_failure) { + error(_(upstream_missing), start_name); + advise(_(upstream_advice)); + exit(1); + } die(_(upstream_missing), start_name); + } die("Not a valid object name: '%s'.", start_name); } -- cgit v1.2.3