All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Add option -b/--branch to clone for select a new HEAD
@ 2009-08-24 20:42 Kirill A. Korinskiy
  2009-08-25  1:57 ` Jeff King
  0 siblings, 1 reply; 21+ messages in thread
From: Kirill A. Korinskiy @ 2009-08-24 20:42 UTC (permalink / raw)
  To: gitster; +Cc: git, Kirill A. Korinskiy

Sometimes (especially on production systems) we need to use only one
remote branch for building software. It really annoying to clone
origin and then swith branch by hand everytime. So this patch provide
functionality to clone remote branch with one command without using
checkout after clone.
---
 Documentation/git-clone.txt |    4 ++++
 builtin-clone.c             |   26 +++++++++++++++++++++++---
 2 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/Documentation/git-clone.txt b/Documentation/git-clone.txt
index 2c63a0f..50446d2 100644
--- a/Documentation/git-clone.txt
+++ b/Documentation/git-clone.txt
@@ -127,6 +127,10 @@ objects from the source repository into a pack in the cloned repository.
 	Instead of using the remote name 'origin' to keep track
 	of the upstream repository, use <name>.
 
+--branch <name>::
+-b <name>::
+	Instead of using the remote HEAD as master, use <name> branch.
+
 --upload-pack <upload-pack>::
 -u <upload-pack>::
 	When given, and the repository to clone from is accessed
diff --git a/builtin-clone.c b/builtin-clone.c
index 32dea74..4420c68 100644
--- a/builtin-clone.c
+++ b/builtin-clone.c
@@ -41,6 +41,7 @@ static int option_quiet, option_no_checkout, option_bare, option_mirror;
 static int option_local, option_no_hardlinks, option_shared;
 static char *option_template, *option_reference, *option_depth;
 static char *option_origin = NULL;
+static char *option_branch = NULL;
 static char *option_upload_pack = "git-upload-pack";
 static int option_verbose;
 
@@ -65,6 +66,8 @@ static struct option builtin_clone_options[] = {
 		   "reference repository"),
 	OPT_STRING('o', "origin", &option_origin, "branch",
 		   "use <branch> instead of 'origin' to track upstream"),
+	OPT_STRING('b', "branch", &option_branch, "branch",
+		   "use <branch> from 'origin' as HEAD"),
 	OPT_STRING('u', "upload-pack", &option_upload_pack, "path",
 		   "path to git-upload-pack on the remote"),
 	OPT_STRING(0, "depth", &option_depth, "depth",
@@ -347,8 +350,8 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 	const char *repo_name, *repo, *work_tree, *git_dir;
 	char *path, *dir;
 	int dest_exists;
-	const struct ref *refs, *head_points_at, *remote_head, *mapped_refs;
-	struct strbuf key = STRBUF_INIT, value = STRBUF_INIT;
+	const struct ref *refs, *head_points_at, *remote_head = NULL, *mapped_refs;
+	struct strbuf key = STRBUF_INIT, value = STRBUF_INIT, branch_head = STRBUF_INIT;
 	struct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT;
 	struct transport *transport = NULL;
 	char *src_ref_prefix = "refs/heads/";
@@ -372,6 +375,9 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 		if (option_origin)
 			die("--bare and --origin %s options are incompatible.",
 			    option_origin);
+		if (option_branch)
+			die("--bare and --branch %s options are incompatible.",
+			    option_branch);
 		option_no_checkout = 1;
 	}
 
@@ -518,7 +524,21 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 
 		mapped_refs = write_remote_refs(refs, refspec, reflog_msg.buf);
 
-		remote_head = find_ref_by_name(refs, "HEAD");
+		if (option_branch) {
+			strbuf_addf(&branch_head, "%s%s", src_ref_prefix, option_branch);
+
+			remote_head = find_ref_by_name(refs, branch_head.buf);
+		}
+
+		if (!remote_head) {
+			if (option_branch)
+				warning("Remote branch %s not found in upstream %s"
+					", using HEAD instead",
+					option_branch, option_origin);
+
+			remote_head = find_ref_by_name(refs, "HEAD");
+		}
+
 		head_points_at = guess_remote_head(remote_head, mapped_refs, 0);
 	}
 	else {
-- 
1.6.2

^ permalink raw reply related	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2009-08-28 12:11 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <87praj90n8.wl%catap@catap.ru>
2009-08-25 19:25 ` [PATCH] Add option -b/--branch to clone for select a new HEAD Kirill A. Korinskiy
2009-08-25 19:27   ` Kirill A. Korinskiy
2009-08-25 21:57     ` Jeff King
2009-08-25 22:42       ` Junio C Hamano
2009-08-25 22:36     ` Björn Steinbrink
     [not found]       ` <87ljl694fd.wl%catap@catap.ru>
2009-08-26 12:16         ` Björn Steinbrink
2009-08-26 14:46           ` Kirill A. Korinskiy
2009-08-26 15:50             ` Björn Steinbrink
2009-08-26 16:10               ` Jeff King
2009-08-26 16:56                 ` Björn Steinbrink
2009-08-26 17:48                   ` Jeff King
2009-08-26 19:05                     ` Jeff King
2009-08-28 10:31   ` Tor Arne Vestbø
2009-08-28 11:05     ` Martin Langhoff
2009-08-28 12:10       ` Julian Phillips
2009-08-24 20:42 Kirill A. Korinskiy
2009-08-25  1:57 ` Jeff King
2009-08-25  2:13   ` Junio C Hamano
2009-08-25 17:20     ` Kirill A. Korinskiy
2009-08-25 19:00       ` Jeff King
2009-08-25 12:30   ` Kirill A. Korinskiy

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.