[PATCH 2/3] DWIM "git checkout frotz" to "git checkout -b frotz origin/frotz"

Subsystems: the rest

DORMANTno replies

5 messages, 3 authors, 2016-06-15 · open the first message on its own page

[PATCH 2/3] DWIM "git checkout frotz" to "git checkout -b frotz origin/frotz"

From: Junio C Hamano <hidden>
Date: 2016-06-15 22:47:34

When 'frotz' is not a valid object name nor a tracked filename,
we used to complain and failed this command.  When there is only
one remote that has 'frotz' as one of its tracking branches, we can
DWIM it as a request to create a local branch 'frotz' forking from
the matching remote tracking branch.

Signed-off-by: Junio C Hamano <redacted>
---
 builtin-checkout.c |   60 +++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 57 insertions(+), 3 deletions(-)
diff --git a/builtin-checkout.c b/builtin-checkout.c
index d050c37..fb7e68a 100644
--- a/builtin-checkout.c
+++ b/builtin-checkout.c
@@ -572,6 +572,40 @@ static int interactive_checkout(const char *revision, const char **pathspec,
 	return run_add_interactive(revision, "--patch=checkout", pathspec);
 }
 
+struct tracking_name_data {
+	const char *name;
+	char *remote;
+	int unique;
+};
+
+static int check_tracking_name(const char *refname, const unsigned char *sha1,
+			       int flags, void *cb_data)
+{
+	struct tracking_name_data *cb = cb_data;
+	const char *slash;
+
+	if (prefixcmp(refname, "refs/remotes/"))
+		return 0;
+	slash = strchr(refname + 13, '/');
+	if (!slash || strcmp(slash + 1, cb->name))
+		return 0;
+	if (cb->remote) {
+		cb->unique = 0;
+		return 0;
+	}
+	cb->remote = xstrdup(refname);
+	return 0;
+}
+
+static const char *unique_tracking_name(const char *name)
+{
+	struct tracking_name_data cb_data = { name, NULL, 1 };
+	for_each_ref(check_tracking_name, &cb_data);
+	if (cb_data.unique)
+		return cb_data.remote;
+	free(cb_data.remote);
+	return NULL;
+}
 
 int cmd_checkout(int argc, const char **argv, const char *prefix)
 {
@@ -630,8 +664,6 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 		opts.new_branch = argv0 + 1;
 	}
 
-	if (opts.track == BRANCH_TRACK_UNSPECIFIED)
-		opts.track = git_branch_track;
 	if (conflict_style) {
 		opts.merge = 1; /* implied */
 		git_xmerge_config("merge.conflictstyle", conflict_style, NULL);
@@ -655,6 +687,11 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 	 *   With no paths, if <something> is a commit, that is to
 	 *   switch to the branch or detach HEAD at it.
 	 *
+	 *   With no paths, if <something> is _not_ a commit, no -t nor -b
+	 *   was given, and there is a tracking branch whose name is
+	 *   <something> in one and only one remote, then this is a short-hand
+	 *   to fork local <something> from that remote tracking branch.
+	 *
 	 *   Otherwise <something> shall not be ambiguous.
 	 *   - If it's *only* a reference, treat it like case (1).
 	 *   - If it's only a path, treat it like case (2).
@@ -677,7 +714,20 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 		if (get_sha1(arg, rev)) {
 			if (has_dash_dash)          /* case (1) */
 				die("invalid reference: %s", arg);
-			goto no_reference;          /* case (3 -> 2) */
+			if (!patch_mode &&
+			    opts.track == BRANCH_TRACK_UNSPECIFIED &&
+			    !opts.new_branch &&
+			    !check_filename(NULL, arg) &&
+			    argc == 1) {
+				const char *remote = unique_tracking_name(arg);
+				if (!remote || get_sha1(remote, rev))
+					goto no_reference;
+				opts.new_branch = arg;
+				arg = remote;
+				/* DWIMmed to create local branch */
+			}
+			else
+				goto no_reference;
 		}
 
 		/* we can't end up being in (2) anymore, eat the argument */
@@ -715,6 +765,10 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 	}
 
 no_reference:
+
+	if (opts.track == BRANCH_TRACK_UNSPECIFIED)
+		opts.track = git_branch_track;
+
 	if (argc) {
 		const char **pathspec = get_pathspec(prefix, argv);
 
-- 
1.6.5.1.95.g09fbd

Re: [PATCH 2/3] DWIM "git checkout frotz" to "git checkout -b frotz origin/frotz"

From: Nanako Shiraishi <hidden>
Date: 2016-06-15 22:47:34

Quoting Junio C Hamano [off-list ref]
When 'frotz' is not a valid object name nor a tracked filename,
we used to complain and failed this command.  When there is only
one remote that has 'frotz' as one of its tracking branches, we can
DWIM it as a request to create a local branch 'frotz' forking from
the matching remote tracking branch.
In the subject you used 'git checkout -b frotz origin/frotz'. Did you forget to say '-t'?

-- 
Nanako Shiraishi
http://ivory.ap.teacup.com/nanako3/

Re: [PATCH 2/3] DWIM "git checkout frotz" to "git checkout -b frotz origin/frotz"

From: Björn Steinbrink <hidden>
Date: 2016-06-15 22:47:34

On 2009.10.18 19:34:48 +0900, Nanako Shiraishi wrote:
Quoting Junio C Hamano [off-list ref]
quoted
When 'frotz' is not a valid object name nor a tracked filename,
we used to complain and failed this command.  When there is only
one remote that has 'frotz' as one of its tracking branches, we can
DWIM it as a request to create a local branch 'frotz' forking from
the matching remote tracking branch.
In the subject you used 'git checkout -b frotz origin/frotz'. Did you
forget to say '-t'?
Hm, the DWIMmery only triggers when opts.track is
BRANCH_TRACK_UNSPECIFIED, i.e. -t was not used. And it doesn't change
opts.track when it DWIMs, so it respects branch.autosetupmerge, which
would be overriden by -t. So it seems correct that -t is not in there.

Björn

Re: [PATCH 2/3] DWIM "git checkout frotz" to "git checkout -b frotz origin/frotz"

From: Nanako Shiraishi <hidden>
Date: 2016-06-15 22:47:34

Quoting Björn Steinbrink [off-list ref] writes:
On 2009.10.18 19:34:48 +0900, Nanako Shiraishi wrote:
quoted
Quoting Junio C Hamano [off-list ref]
quoted
When 'frotz' is not a valid object name nor a tracked filename,
we used to complain and failed this command.  When there is only
one remote that has 'frotz' as one of its tracking branches, we can
DWIM it as a request to create a local branch 'frotz' forking from
the matching remote tracking branch.
In the subject you used 'git checkout -b frotz origin/frotz'. Did you
forget to say '-t'?
Hm, the DWIMmery only triggers when opts.track is
BRANCH_TRACK_UNSPECIFIED, i.e. -t was not used. And it doesn't change
opts.track when it DWIMs, so it respects branch.autosetupmerge, which
would be overriden by -t. So it seems correct that -t is not in there.
I see.

A user who always wants tracking can set the config option and use 
the new "git checkout frotz" shortcut, but a user who usually 
doesn't want tracking doesn't have the config option and when he 
wants tracking only for this new branch he can explicitly say "git 
checkout -t origin/frotz", right?

-- 
Nanako Shiraishi
http://ivory.ap.teacup.com/nanako3/

Re: [PATCH 2/3] DWIM "git checkout frotz" to "git checkout -b frotz origin/frotz"

From: Björn Steinbrink <hidden>
Date: 2016-06-15 22:47:34

On 2009.10.19 05:20:43 +0900, Nanako Shiraishi wrote:
A user who always wants tracking can set the config option and use 
the new "git checkout frotz" shortcut, but a user who usually 
doesn't want tracking doesn't have the config option and when he 
wants tracking only for this new branch he can explicitly say "git 
checkout -t origin/frotz", right?
Well, branch.autosetupmerge has three possible values.
 - true: Do the upstream setup when starting from remote tracking
   branches
 - always: Also do the upstream setup when starting from a local branch
   head
 - false: Don't do any upstream setup

The default is "true", which should catch the "git checkout frotz"
shortcut, as that selects a remote tracking branch as the starting
point. So the user doesn't have to change any config setting to have
that act as if -t was given.

Only if we doesn't want "git checkout frotz" to not do the upstream
setup, he needs to set branch.autosetupmerge to false.

And falling back to "git checkout --track/--no-track origin/frotz" he
can override whatever config setting he has.

Björn
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help