From f9a482e62b079cdf9e80889011b6f41f3c17f0c2 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Mon, 26 Mar 2012 19:51:01 -0400 Subject: checkout: suppress tracking message with "-q" Like the "switched to..." message (which is already suppressed by "-q"), this message is purely informational. Let's silence it if the user asked us to be quiet. This patch is slightly more than a one-liner, because we have to teach create_branch to propagate the flag all the way down to install_branch_config. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- branch.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'branch.c') diff --git a/branch.c b/branch.c index 9971820a18..eccdaf9392 100644 --- a/branch.c +++ b/branch.c @@ -101,9 +101,10 @@ void install_branch_config(int flag, const char *local, const char *origin, cons * config. */ static int setup_tracking(const char *new_ref, const char *orig_ref, - enum branch_track track) + enum branch_track track, int quiet) { struct tracking tracking; + int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE; if (strlen(new_ref) > 1024 - 7 - 7 - 1) return error("Tracking not set up: name too long: %s", @@ -128,7 +129,7 @@ static int setup_tracking(const char *new_ref, const char *orig_ref, return error("Not tracking: ambiguous information for ref %s", orig_ref); - install_branch_config(BRANCH_CONFIG_VERBOSE, new_ref, tracking.remote, + install_branch_config(config_flags, new_ref, tracking.remote, tracking.src ? tracking.src : orig_ref); free(tracking.src); @@ -191,7 +192,7 @@ int validate_new_branchname(const char *name, struct strbuf *ref, void create_branch(const char *head, const char *name, const char *start_name, int force, int reflog, int clobber_head, - enum branch_track track) + int quiet, enum branch_track track) { struct ref_lock *lock = NULL; struct commit *commit; @@ -260,7 +261,7 @@ void create_branch(const char *head, start_name); if (real_ref && track) - setup_tracking(ref.buf+11, real_ref, track); + setup_tracking(ref.buf+11, real_ref, track, quiet); if (!dont_change_ref) if (write_ref_sha1(lock, sha1, msg) < 0) -- cgit v1.2.1 From d53a35032a67fde5b59c8a6a66e0466837cbaf1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Thu, 7 Jun 2012 19:05:10 +0700 Subject: Remove i18n legos in notifying new branch tracking setup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- branch.c | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) (limited to 'branch.c') diff --git a/branch.c b/branch.c index eccdaf9392..2bef1e7e71 100644 --- a/branch.c +++ b/branch.c @@ -74,25 +74,33 @@ void install_branch_config(int flag, const char *local, const char *origin, cons strbuf_addf(&key, "branch.%s.rebase", local); git_config_set(key.buf, "true"); } + strbuf_release(&key); if (flag & BRANCH_CONFIG_VERBOSE) { - strbuf_reset(&key); - - strbuf_addstr(&key, origin ? "remote" : "local"); - - /* Are we tracking a proper "branch"? */ - if (remote_is_branch) { - strbuf_addf(&key, " branch %s", shortname); - if (origin) - strbuf_addf(&key, " from %s", origin); - } + if (remote_is_branch && origin) + printf(rebasing ? + "Branch %s set up to track remote branch %s from %s by rebasing.\n" : + "Branch %s set up to track remote branch %s from %s.\n", + local, shortname, origin); + else if (remote_is_branch && !origin) + printf(rebasing ? + "Branch %s set up to track local branch %s by rebasing.\n" : + "Branch %s set up to track local branch %s.\n", + local, shortname); + else if (!remote_is_branch && origin) + printf(rebasing ? + "Branch %s set up to track remote ref %s by rebasing.\n" : + "Branch %s set up to track remote ref %s.\n", + local, remote); + else if (!remote_is_branch && !origin) + printf(rebasing ? + "Branch %s set up to track local ref %s by rebasing.\n" : + "Branch %s set up to track local ref %s.\n", + local, remote); else - strbuf_addf(&key, " ref %s", remote); - printf("Branch %s set up to track %s%s.\n", - local, key.buf, - rebasing ? " by rebasing" : ""); + die("BUG: impossible combination of %d and %p", + remote_is_branch, origin); } - strbuf_release(&key); } /* -- cgit v1.2.1 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.1 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.1 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.1 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.1 From bc554df8c90e3592e8cd70460032b524fb6f03bc Mon Sep 17 00:00:00 2001 From: Jiang Xin Date: Tue, 16 Apr 2013 11:37:50 +0800 Subject: i18n: branch: mark strings for translation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jiang Xin Reviewed-by: Nguyễn Thái Ngọc Duy Reviewed-by: Jonathan Nieder Signed-off-by: Junio C Hamano --- branch.c | 54 +++++++++++++++++++++++++++--------------------------- 1 file changed, 27 insertions(+), 27 deletions(-) (limited to 'branch.c') diff --git a/branch.c b/branch.c index 6ae6a4c321..97c72bfe70 100644 --- a/branch.c +++ b/branch.c @@ -57,7 +57,7 @@ void install_branch_config(int flag, const char *local, const char *origin, cons if (remote_is_branch && !strcmp(local, shortname) && !origin) { - warning("Not setting branch %s as its own upstream.", + warning(_("Not setting branch %s as its own upstream."), local); return; } @@ -78,25 +78,25 @@ void install_branch_config(int flag, const char *local, const char *origin, cons if (flag & BRANCH_CONFIG_VERBOSE) { if (remote_is_branch && origin) - printf(rebasing ? - "Branch %s set up to track remote branch %s from %s by rebasing.\n" : - "Branch %s set up to track remote branch %s from %s.\n", - local, shortname, origin); + printf_ln(rebasing ? + _("Branch %s set up to track remote branch %s from %s by rebasing.") : + _("Branch %s set up to track remote branch %s from %s."), + local, shortname, origin); else if (remote_is_branch && !origin) - printf(rebasing ? - "Branch %s set up to track local branch %s by rebasing.\n" : - "Branch %s set up to track local branch %s.\n", - local, shortname); + printf_ln(rebasing ? + _("Branch %s set up to track local branch %s by rebasing.") : + _("Branch %s set up to track local branch %s."), + local, shortname); else if (!remote_is_branch && origin) - printf(rebasing ? - "Branch %s set up to track remote ref %s by rebasing.\n" : - "Branch %s set up to track remote ref %s.\n", - local, remote); + printf_ln(rebasing ? + _("Branch %s set up to track remote ref %s by rebasing.") : + _("Branch %s set up to track remote ref %s."), + local, remote); else if (!remote_is_branch && !origin) - printf(rebasing ? - "Branch %s set up to track local ref %s by rebasing.\n" : - "Branch %s set up to track local ref %s.\n", - local, remote); + printf_ln(rebasing ? + _("Branch %s set up to track local ref %s by rebasing.") : + _("Branch %s set up to track local ref %s."), + local, remote); else die("BUG: impossible combination of %d and %p", remote_is_branch, origin); @@ -115,7 +115,7 @@ static int setup_tracking(const char *new_ref, const char *orig_ref, int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE; if (strlen(new_ref) > 1024 - 7 - 7 - 1) - return error("Tracking not set up: name too long: %s", + return error(_("Tracking not set up: name too long: %s"), new_ref); memset(&tracking, 0, sizeof(tracking)); @@ -134,7 +134,7 @@ static int setup_tracking(const char *new_ref, const char *orig_ref, } if (tracking.matches > 1) - return error("Not tracking: ambiguous information for ref %s", + return error(_("Not tracking: ambiguous information for ref %s"), orig_ref); install_branch_config(config_flags, new_ref, tracking.remote, @@ -179,12 +179,12 @@ int validate_new_branchname(const char *name, struct strbuf *ref, int force, int attr_only) { if (strbuf_check_branch_ref(ref, name)) - die("'%s' is not a valid branch name.", name); + die(_("'%s' is not a valid branch name."), name); if (!ref_exists(ref->buf)) return 0; else if (!force && !attr_only) - die("A branch named '%s' already exists.", ref->buf + strlen("refs/heads/")); + die(_("A branch named '%s' already exists."), ref->buf + strlen("refs/heads/")); if (!attr_only) { const char *head; @@ -192,7 +192,7 @@ int validate_new_branchname(const char *name, struct strbuf *ref, head = resolve_ref_unsafe("HEAD", sha1, 0, NULL); if (!is_bare_repository() && head && !strcmp(head, ref->buf)) - die("Cannot force update the current branch."); + die(_("Cannot force update the current branch.")); } return 1; } @@ -247,7 +247,7 @@ void create_branch(const char *head, } die(_(upstream_missing), start_name); } - die("Not a valid object name: '%s'.", start_name); + die(_("Not a valid object name: '%s'."), start_name); } switch (dwim_ref(start_name, strlen(start_name), sha1, &real_ref)) { @@ -267,18 +267,18 @@ void create_branch(const char *head, } break; default: - die("Ambiguous object name: '%s'.", start_name); + die(_("Ambiguous object name: '%s'."), start_name); break; } if ((commit = lookup_commit_reference(sha1)) == NULL) - die("Not a valid branch point: '%s'.", start_name); + die(_("Not a valid branch point: '%s'."), start_name); hashcpy(sha1, commit->object.sha1); if (!dont_change_ref) { lock = lock_any_ref_for_update(ref.buf, NULL, 0); if (!lock) - die_errno("Failed to lock ref for update"); + die_errno(_("Failed to lock ref for update")); } if (reflog) @@ -296,7 +296,7 @@ void create_branch(const char *head, if (!dont_change_ref) if (write_ref_sha1(lock, sha1, msg) < 0) - die_errno("Failed to write ref"); + die_errno(_("Failed to write ref")); strbuf_release(&ref); free(real_ref); -- cgit v1.2.1 From 41c21f22d0fc06f1f22489621980396aea9f7a62 Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Sun, 21 Apr 2013 23:52:05 +0200 Subject: branch.c: Validate tracking branches with refspecs instead of refs/remotes/* The current code for validating tracking branches (e.g. the argument to the -t/--track option) hardcodes refs/heads/* and refs/remotes/* as the potential locations for tracking branches. This works with the refspecs created by "git clone" or "git remote add", but is suboptimal in other cases: - If "refs/remotes/foo/bar" exists without any association to a remote (i.e. there is no remote named "foo", or no remote with a refspec that matches "refs/remotes/foo/bar"), then it is impossible to set up a valid upstream config that tracks it. Currently, the code defaults to using "refs/remotes/foo/bar" from repo "." as the upstream, which works, but is probably not what the user had in mind when running "git branch baz --track foo/bar". - If the user has tweaked the fetch refspec for a remote to put its remote-tracking branches outside of refs/remotes/*, e.g. by running git config remote.foo.fetch "+refs/heads/*:refs/foo_stuff/*" then the current code will refuse to use its remote-tracking branches as --track arguments, since they do not match refs/remotes/*. This patch removes the "refs/remotes/*" requirement for upstream branches, and replaces it with explicit checking of the refspecs for each remote to determine whether a given --track argument is a valid remote-tracking branch. This solves both of the above problems, since the matching refspec guarantees that there is a both a remote name and a remote branch name that can be used for the upstream config. However, this means that refs located within refs/remotes/* without a corresponding remote/refspec will no longer be usable as upstreams. The few existing tests which depended on this behavioral quirk has already been fixed in the preceding patches. This patch fixes the last remaining test failure in t2024-checkout-dwim. Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- branch.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'branch.c') diff --git a/branch.c b/branch.c index 6ae6a4c321..beaf11d97c 100644 --- a/branch.c +++ b/branch.c @@ -197,6 +197,21 @@ int validate_new_branchname(const char *name, struct strbuf *ref, return 1; } +static int check_tracking_branch(struct remote *remote, void *cb_data) +{ + char *tracking_branch = cb_data; + struct refspec query; + memset(&query, 0, sizeof(struct refspec)); + query.dst = tracking_branch; + return !(remote_find_tracking(remote, &query) || + prefixcmp(query.src, "refs/heads/")); +} + +static int validate_remote_tracking_branch(char *ref) +{ + return !for_each_remote(check_tracking_branch, ref); +} + static const char upstream_not_branch[] = N_("Cannot setup tracking information; starting point '%s' is not a branch."); static const char upstream_missing[] = @@ -259,7 +274,7 @@ void create_branch(const char *head, case 1: /* Unique completion -- good, only if it is a real branch */ if (prefixcmp(real_ref, "refs/heads/") && - prefixcmp(real_ref, "refs/remotes/")) { + validate_remote_tracking_branch(real_ref)) { if (explicit_tracking) die(_(upstream_not_branch), start_name); else -- cgit v1.2.1