git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: git@vger.kernel.org
Cc: Johannes Schindelin <Johannes.Schindelin@gmx.de>,
	Jay Soffian <jaysoffian@gmail.com>
Subject: [PATCH 2/3] DWIM "git checkout frotz" to "git checkout -b frotz origin/frotz"
Date: Sun, 18 Oct 2009 01:01:07 -0700	[thread overview]
Message-ID: <7vaazpxha4.fsf_-_@alter.siamese.dyndns.org> (raw)
In-Reply-To: <7vzl7pyvzl.fsf@alter.siamese.dyndns.org> (Junio C. Hamano's message of "Sun\, 18 Oct 2009 00\:58\:06 -0700")

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 <gitster@pobox.com>
---
 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

  parent reply	other threads:[~2009-10-18  8:01 UTC|newest]

Thread overview: 91+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-10-05 20:46 [PATCH/RFC] builtin-checkout: suggest creating local branch when appropriate to do so Jay Soffian
2009-10-05 21:03 ` Sverre Rabbelier
2009-10-05 21:17 ` Johannes Schindelin
2009-10-05 21:26   ` Sverre Rabbelier
2009-10-05 21:57     ` Jay Soffian
2009-10-05 22:00   ` Jay Soffian
2009-10-05 22:45     ` Johannes Schindelin
2009-10-05 22:56   ` Jeff King
2009-10-06  7:32     ` Thomas Rast
2009-10-06  9:16       ` Johannes Schindelin
2009-10-06 11:36         ` Junio C Hamano
2009-10-06 12:02           ` Johannes Schindelin
2009-10-06 20:09             ` Junio C Hamano
2009-10-06  9:12     ` Johannes Schindelin
2009-10-06  9:28       ` Matthieu Moy
2009-10-06  9:41         ` Mikael Magnusson
2009-10-06 10:04           ` Johannes Schindelin
     [not found]           ` <0016e68fd0123a175304754694b4@google.com>
2009-10-06 16:43             ` Eugene Sajine
2009-10-06 20:33               ` Junio C Hamano
2009-10-12  7:49             ` Johannes Schindelin
2009-10-12 18:36               ` Björn Steinbrink
2009-10-12 21:40               ` Thomas Rast
2009-10-12 22:49                 ` Junio C Hamano
2009-10-13  6:36                   ` Thomas Rast
2009-10-13  7:16                     ` Junio C Hamano
2009-10-13  8:44                       ` Junio C Hamano
2009-10-13  8:51                       ` Thomas Rast
2009-10-13  9:24                         ` Junio C Hamano
2009-10-13 21:20                           ` Johannes Schindelin
2009-10-13 21:59                             ` Junio C Hamano
2009-10-13 22:06                             ` Jeff King
2009-10-13 23:22                               ` Johannes Schindelin
2009-10-14  1:05                                 ` Jay Soffian
2009-10-14  3:28                                   ` Junio C Hamano
2009-10-14 12:49                                     ` Jay Soffian
2009-10-14 19:31                                       ` Junio C Hamano
2009-10-25 17:44                                     ` Uri Okrent
2009-10-14  4:31                                 ` Jeff King
2009-10-14  9:56                         ` Thomas Rast
2009-10-14 10:46                           ` Jakub Narebski
2009-10-13  9:32                       ` Johannes Sixt
2009-10-13 18:39                       ` Daniel Barkalow
2009-10-13 20:53                         ` Junio C Hamano
2009-10-13 21:31                           ` Daniel Barkalow
2009-10-13 21:57                             ` Jeff King
2009-10-13 22:46                               ` Junio C Hamano
2009-10-13 23:16                                 ` Johannes Schindelin
2009-10-14  9:33                                   ` Thomas Rast
2009-10-16 11:48                                     ` Johannes Schindelin
2009-10-16 12:07                                       ` Thomas Rast
2009-10-25 17:48                                 ` Uri Okrent
2009-10-26  7:14                                   ` Junio C Hamano
2009-10-13 22:38                             ` Björn Steinbrink
2009-10-18  7:58   ` Junio C Hamano
2009-10-18  8:00     ` [PATCH 1/3] check_filename(): make verify_filename() callable without dying Junio C Hamano
2009-10-18  8:01     ` Junio C Hamano [this message]
2009-10-18 10:34       ` [PATCH 2/3] DWIM "git checkout frotz" to "git checkout -b frotz origin/frotz" Nanako Shiraishi
2009-10-18 12:00         ` Björn Steinbrink
2009-10-18 20:20           ` Nanako Shiraishi
2009-10-18 22:50             ` Junio C Hamano
2009-10-19  5:58             ` Björn Steinbrink
2009-10-18  8:01     ` [PATCH 3/3] git checkout --nodwim Junio C Hamano
2009-10-18 12:40       ` Alex Riesen
2009-10-18 19:53         ` Junio C Hamano
2009-10-18 21:02           ` [PATCH] Use "--no-" prefix to switch off some of checkout dwimmery Alex Riesen
2009-10-18 22:49             ` Junio C Hamano
2009-10-19  6:07               ` Alex Riesen
2009-10-19  6:12                 ` Alex Riesen
2009-10-19  6:16                   ` Junio C Hamano
2009-10-19  7:17                     ` Alex Riesen
2009-10-19  7:25                       ` Junio C Hamano
2009-10-21 17:29           ` [PATCH 3/3] git checkout --nodwim Avery Pennarun
2009-10-21 21:21             ` Nanako Shiraishi
2009-10-21 22:14               ` Junio C Hamano
2009-10-21 22:35                 ` [PATCH] git checkout --no-guess Junio C Hamano
2009-10-21 22:51                   ` Avery Pennarun
2009-10-26 18:17                     ` Jay Soffian
2009-10-26 18:25                       ` Avery Pennarun
2009-10-22  0:27               ` [PATCH 3/3] git checkout --nodwim Johannes Schindelin
2009-10-22  7:09                 ` Erik Faye-Lund
2009-10-23  8:57                 ` Michael J Gruber
2009-10-24  6:35                 ` Junio C Hamano
2009-10-24 14:59                   ` David Roundy
2009-10-24 19:25                     ` Junio C Hamano
2009-10-26 20:12                     ` Johannes Schindelin
2009-10-26 20:40                       ` Avery Pennarun
2009-10-26 21:26                         ` Jeff King
2009-10-26 22:01                           ` Avery Pennarun
2009-10-26 22:14                             ` Jeff King
2009-10-26 22:28                               ` Avery Pennarun
2009-10-05 22:52 ` [PATCH/RFC] builtin-checkout: suggest creating local branch when appropriate to do so Jeff King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7vaazpxha4.fsf_-_@alter.siamese.dyndns.org \
    --to=gitster@pobox.com \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=git@vger.kernel.org \
    --cc=jaysoffian@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).