From: "Tor Arne Vestbø" <torarnv@gmail.com>
To: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org
Subject: [PATCH] git-clone: Add option --branch to override initial branch
Date: Mon, 2 Mar 2009 23:11:22 +0100 [thread overview]
Message-ID: <1236031882-2052-1-git-send-email-torarnv@gmail.com> (raw)
The options --branch and -b allow the user to override the initial
branch created and checked out by git-clone. Normally this is the
active branch of the remote repository, which is also the fallback
if the selected branch is not found.
Signed-off-by: Tor Arne Vestbø <torarnv@gmail.com>
---
Documentation/git-clone.txt | 5 +++++
builtin-clone.c | 33 +++++++++++++++++++++++++++++----
2 files changed, 34 insertions(+), 4 deletions(-)
diff --git a/Documentation/git-clone.txt b/Documentation/git-clone.txt
index 95f08b9..e7feb4d 100644
--- a/Documentation/git-clone.txt
+++ b/Documentation/git-clone.txt
@@ -119,6 +119,11 @@ then the cloned repository will become corrupt.
Instead of using the remote name 'origin' to keep track
of the upstream repository, use <name> instead.
+--branch <name>::
+-b <name>::
+ Instead of using the remote repository's active branch as the
+ initial branch, use <name> instead.
+
--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 c338910..601c2c2 100644
--- a/builtin-clone.c
+++ b/builtin-clone.c
@@ -38,6 +38,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;
@@ -66,6 +67,8 @@ static struct option builtin_clone_options[] = {
"path to git-upload-pack on the remote"),
OPT_STRING(0, "depth", &option_depth, "depth",
"create a shallow clone of that depth"),
+ OPT_STRING('b', "branch", &option_branch, "branch",
+ "initial remote branch to check out"),
OPT_END()
};
@@ -372,7 +375,9 @@ 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;
+ const struct ref *refs, *mapped_refs;
+ const struct ref *remote_head = NULL;
+ const struct ref *head_points_at = NULL;
struct strbuf key = STRBUF_INIT, value = STRBUF_INIT;
struct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT;
struct transport *transport = NULL;
@@ -545,12 +550,32 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
mapped_refs = write_remote_refs(refs, &refspec, reflog_msg.buf);
- head_points_at = locate_head(refs, mapped_refs, &remote_head);
+ if (option_branch) {
+ const int offset = 11;
+ const char *branch = option_branch;
+ if (!prefixcmp(branch, "refs/heads/"))
+ branch += offset;
+
+ const struct ref *r;
+ for (r = mapped_refs; r; r = r->next) {
+ if (!strcmp(r->name + offset, branch)) {
+ /* Override initial branch */
+ head_points_at = r;
+ remote_head = r;
+ break;
+ }
+ }
+
+ if (!head_points_at)
+ warning("remote has no branch named '%s', "
+ "falling back to default.", option_branch);
+ }
+
+ if (!head_points_at)
+ head_points_at = locate_head(refs, mapped_refs, &remote_head);
}
else {
warning("You appear to have cloned an empty repository.");
- head_points_at = NULL;
- remote_head = NULL;
option_no_checkout = 1;
if (!option_bare)
install_branch_config("master", option_origin,
--
1.6.2.rc2.16.gf474c.dirty
next reply other threads:[~2009-03-02 22:09 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-03-02 22:11 Tor Arne Vestbø [this message]
2009-03-02 23:48 ` [PATCH] git-clone: Add option --branch to override initial branch Johannes Schindelin
2009-03-03 0:09 ` Junio C Hamano
2009-03-03 0:11 ` Tor Arne Vestbø
2009-03-03 0:33 ` [PATCH v2] " Tor Arne Vestbø
2009-03-03 9:07 ` Johannes Schindelin
2009-03-03 16:47 ` Tor Arne Vestbø
2009-03-03 16:51 ` Junio C Hamano
2009-03-03 17:04 ` Tor Arne Vestbø
2009-03-03 17:07 ` Junio C Hamano
2009-03-04 6:55 ` Junio C Hamano
2009-03-04 8:56 ` Johannes Schindelin
2009-03-04 10:23 ` Tor Arne Vestbø
2009-03-09 14:39 ` Paolo Ciarrocchi
2009-03-09 16:01 ` Felipe Contreras
2009-03-11 8:52 ` Paolo Ciarrocchi
2009-03-12 4:18 ` Miles Bader
2009-03-12 8:48 ` Paolo Ciarrocchi
2009-03-12 9:12 ` Felipe Contreras
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1236031882-2052-1-git-send-email-torarnv@gmail.com \
--to=torarnv@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.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.
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.