From 8a3e5ecdaa9fc45d7af58eb95bba0f39ca625a8d Mon Sep 17 00:00:00 2001 From: Jeff King Date: Tue, 2 Apr 2013 15:02:53 -0400 Subject: [PATCH 1/5] t3200: test --set-upstream-to with bogus refs These tests pass with the current code, but let's make sure we don't accidentally break the behavior in the future. Note that our tests expect failure when we try to set the upstream to or from a missing branch. Technically we are just munging config here, so we do not need the refs to exist. But seeing that they do exist is a good check that the user has not made a typo. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- t/t3200-branch.sh | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh index b08c9f2295..09f65f8358 100755 --- a/t/t3200-branch.sh +++ b/t/t3200-branch.sh @@ -409,6 +409,18 @@ test_expect_success '--set-upstream-to fails on detached HEAD' ' git checkout - ' +test_expect_success '--set-upstream-to fails on a missing dst branch' ' + test_must_fail git branch --set-upstream-to master does-not-exist +' + +test_expect_success '--set-upstream-to fails on a missing src branch' ' + test_must_fail git branch --set-upstream-to does-not-exist master +' + +test_expect_success '--set-upstream-to fails on a non-ref' ' + test_must_fail git branch --set-upstream-to HEAD^{} +' + test_expect_success 'use --set-upstream-to modify HEAD' ' test_config branch.master.remote foo && test_config branch.master.merge foo && From e2b6aa5f1b85d5f49f0cf280162cf216e55e1eba Mon Sep 17 00:00:00 2001 From: Jeff King Date: Tue, 2 Apr 2013 15:03:55 -0400 Subject: [PATCH 2/5] 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(-) 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; } From a5e91c722cceb667ad05a13f9fde150cc1dbe9aa Mon Sep 17 00:00:00 2001 From: Jeff King Date: Tue, 2 Apr 2013 15:04:27 -0400 Subject: [PATCH 3/5] 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(-) 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: From 1a15d00bb910de0a86981f0d75836feac71d1fe0 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Tue, 2 Apr 2013 15:04:51 -0400 Subject: [PATCH 4/5] 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(-) 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; } From caa2036b3b4105bfe34f01115e4fb34b78a4db86 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Tue, 2 Apr 2013 15:05:12 -0400 Subject: [PATCH 5/5] 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 --- advice.c | 2 ++ advice.h | 1 + branch.c | 19 +++++++++++++++++-- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/advice.c b/advice.c index 3bc86260b8..a8deee6e64 100644 --- a/advice.c +++ b/advice.c @@ -13,6 +13,7 @@ int advice_commit_before_merge = 1; int advice_resolve_conflict = 1; int advice_implicit_identity = 1; int advice_detached_head = 1; +int advice_set_upstream_failure = 1; static struct { const char *name; @@ -31,6 +32,7 @@ static struct { { "resolveconflict", &advice_resolve_conflict }, { "implicitidentity", &advice_implicit_identity }, { "detachedhead", &advice_detached_head }, + { "setupstreamfailure", &advice_set_upstream_failure }, /* make this an alias for backward compatibility */ { "pushnonfastforward", &advice_push_update_rejected } diff --git a/advice.h b/advice.h index af0c983c68..94caa32f92 100644 --- a/advice.h +++ b/advice.h @@ -16,6 +16,7 @@ extern int advice_commit_before_merge; extern int advice_resolve_conflict; extern int advice_implicit_identity; extern int advice_detached_head; +extern int advice_set_upstream_failure; int git_default_advice_config(const char *var, const char *value); void advise(const char *advice, ...); 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); }