]> Pileus Git - ~andy/git/blobdiff - builtin/clone.c
Merge branch 'jk/git-dir-lookup'
[~andy/git] / builtin / clone.c
index 2733fa47ef32034ac5d4d1435837abddf06e9e3c..7559f62bc7a846ecb8f8d085e8bf5c3e6d4df2cd 100644 (file)
@@ -48,7 +48,6 @@ static int option_verbosity;
 static int option_progress;
 static struct string_list option_config;
 static struct string_list option_reference;
-static const char *src_ref_prefix = "refs/heads/";
 
 static int opt_parse_reference(const struct option *opt, const char *arg, int unset)
 {
@@ -108,7 +107,7 @@ static const char *argv_submodule[] = {
 
 static char *get_repo_path(const char *repo, int *is_bundle)
 {
-       static char *suffix[] = { "/.git", ".git", "" };
+       static char *suffix[] = { "/.git", "", ".git/.git", ".git" };
        static char *bundle_suffix[] = { ".bundle", "" };
        struct stat st;
        int i;
@@ -118,7 +117,7 @@ static char *get_repo_path(const char *repo, int *is_bundle)
                path = mkpath("%s%s", repo, suffix[i]);
                if (stat(path, &st))
                        continue;
-               if (S_ISDIR(st.st_mode)) {
+               if (S_ISDIR(st.st_mode) && is_git_directory(path)) {
                        *is_bundle = 0;
                        return xstrdup(absolute_path(path));
                } else if (S_ISREG(st.st_mode) && st.st_size > 8) {
@@ -233,9 +232,6 @@ static int add_one_reference(struct string_list_item *item, void *cb_data)
 {
        char *ref_git;
        struct strbuf alternate = STRBUF_INIT;
-       struct remote *remote;
-       struct transport *transport;
-       const struct ref *extra;
 
        /* Beware: real_path() and mkpath() return static buffer */
        ref_git = xstrdup(real_path(item->string));
@@ -250,14 +246,6 @@ static int add_one_reference(struct string_list_item *item, void *cb_data)
        strbuf_addf(&alternate, "%s/objects", ref_git);
        add_to_alternates_file(alternate.buf);
        strbuf_release(&alternate);
-
-       remote = remote_get(ref_git);
-       transport = transport_get(remote, ref_git);
-       for (extra = transport_get_remote_refs(transport); extra;
-            extra = extra->next)
-               add_extra_ref(extra->name, extra->old_sha1, 0);
-
-       transport_disconnect(transport);
        free(ref_git);
        return 0;
 }
@@ -364,13 +352,8 @@ static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest,
        closedir(dir);
 }
 
-static const struct ref *clone_local(const char *src_repo,
-                                    const char *dest_repo)
+static void clone_local(const char *src_repo, const char *dest_repo)
 {
-       const struct ref *ret;
-       struct remote *remote;
-       struct transport *transport;
-
        if (option_shared) {
                struct strbuf alt = STRBUF_INIT;
                strbuf_addf(&alt, "%s/objects", src_repo);
@@ -386,13 +369,8 @@ static const struct ref *clone_local(const char *src_repo,
                strbuf_release(&dest);
        }
 
-       remote = remote_get(src_repo);
-       transport = transport_get(remote, src_repo);
-       ret = transport_get_remote_refs(transport);
-       transport_disconnect(transport);
        if (0 <= option_verbosity)
                printf(_("done.\n"));
-       return ret;
 }
 
 static const char *junk_work_tree;
@@ -423,6 +401,26 @@ static void remove_junk_on_signal(int signo)
        raise(signo);
 }
 
+static struct ref *find_remote_branch(const struct ref *refs, const char *branch)
+{
+       struct ref *ref;
+       struct strbuf head = STRBUF_INIT;
+       strbuf_addstr(&head, "refs/heads/");
+       strbuf_addstr(&head, branch);
+       ref = find_ref_by_name(refs, head.buf);
+       strbuf_release(&head);
+
+       if (ref)
+               return ref;
+
+       strbuf_addstr(&head, "refs/tags/");
+       strbuf_addstr(&head, branch);
+       ref = find_ref_by_name(refs, head.buf);
+       strbuf_release(&head);
+
+       return ref;
+}
+
 static struct ref *wanted_peer_refs(const struct ref *refs,
                struct refspec *refspec)
 {
@@ -435,19 +433,18 @@ static struct ref *wanted_peer_refs(const struct ref *refs,
 
                if (!option_branch)
                        remote_head = guess_remote_head(head, refs, 0);
-               else {
-                       struct strbuf sb = STRBUF_INIT;
-                       strbuf_addstr(&sb, src_ref_prefix);
-                       strbuf_addstr(&sb, option_branch);
-                       remote_head = find_ref_by_name(refs, sb.buf);
-                       strbuf_release(&sb);
-               }
+               else
+                       remote_head = find_remote_branch(refs, option_branch);
 
                if (!remote_head && option_branch)
                        warning(_("Could not find remote branch %s to clone."),
                                option_branch);
-               else
+               else {
                        get_fetch_map(remote_head, refspec, &tail, 0);
+
+                       /* if --branch=tag, pull the requested tag explicitly */
+                       get_fetch_map(remote_head, tag_refspec, &tail, 0);
+               }
        } else
                get_fetch_map(refs, refspec, &tail, 0);
 
@@ -464,11 +461,10 @@ static void write_remote_refs(const struct ref *local_refs)
        for (r = local_refs; r; r = r->next) {
                if (!r->peer_ref)
                        continue;
-               add_extra_ref(r->peer_ref->name, r->old_sha1, 0);
+               add_packed_ref(r->peer_ref->name, r->old_sha1);
        }
 
        pack_refs(PACK_REFS_ALL);
-       clear_extra_refs();
 }
 
 static void write_followtags(const struct ref *refs, const char *msg)
@@ -493,7 +489,6 @@ static void update_remote_refs(const struct ref *refs,
                               const char *msg)
 {
        if (refs) {
-               clear_extra_refs();
                write_remote_refs(mapped_refs);
                if (option_single_branch)
                        write_followtags(refs, msg);
@@ -512,7 +507,7 @@ static void update_remote_refs(const struct ref *refs,
 static void update_head(const struct ref *our, const struct ref *remote,
                        const char *msg)
 {
-       if (our) {
+       if (our && !prefixcmp(our->name, "refs/heads/")) {
                /* Local default branch link */
                create_symref("HEAD", our->name, NULL);
                if (!option_bare) {
@@ -520,11 +515,15 @@ static void update_head(const struct ref *our, const struct ref *remote,
                        update_ref(msg, "HEAD", our->old_sha1, NULL, 0, DIE_ON_ERR);
                        install_branch_config(0, head, option_origin, our->name);
                }
+       } else if (our) {
+               struct commit *c = lookup_commit_reference(our->old_sha1);
+               /* --branch specifies a non-branch (i.e. tags), detach HEAD */
+               update_ref(msg, "HEAD", c->object.sha1,
+                          NULL, REF_NODEREF, DIE_ON_ERR);
        } else if (remote) {
                /*
                 * We know remote HEAD points to a non-branch, or
-                * HEAD points to a branch but we don't know which one, or
-                * we asked for a specific branch but it did not exist.
+                * HEAD points to a branch but we don't know which one.
                 * Detach HEAD in all these cases.
                 */
                update_ref(msg, "HEAD", remote->old_sha1,
@@ -551,7 +550,10 @@ static int checkout(void)
                          "unable to checkout.\n"));
                return 0;
        }
-       if (strcmp(head, "HEAD")) {
+       if (!strcmp(head, "HEAD")) {
+               if (advice_detached_head)
+                       detach_advice(sha1_to_hex(sha1));
+       } else {
                if (prefixcmp(head, "refs/heads/"))
                        die(_("HEAD not found below refs/heads!"));
        }
@@ -616,10 +618,13 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
        const struct ref *remote_head_points_at;
        const struct ref *our_head_points_at;
        struct ref *mapped_refs;
+       const struct ref *ref;
        struct strbuf key = STRBUF_INIT, value = STRBUF_INIT;
        struct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT;
        struct transport *transport = NULL;
-       int err = 0;
+       const char *src_ref_prefix = "refs/heads/";
+       struct remote *remote;
+       int err = 0, complete_refs_before_fetch = 1;
 
        struct refspec *refspec;
        const char *fetch_pattern;
@@ -773,13 +778,10 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 
        strbuf_reset(&value);
 
-       if (is_local) {
-               refs = clone_local(path, git_dir);
-               mapped_refs = wanted_peer_refs(refs, refspec);
-       } else {
-               struct remote *remote = remote_get(option_origin);
-               transport = transport_get(remote, remote->url[0]);
+       remote = remote_get(option_origin);
+       transport = transport_get(remote, remote->url[0]);
 
+       if (!is_local) {
                if (!transport->get_refs_list || !transport->fetch)
                        die(_("Don't know how to clone %s"), transport->url);
 
@@ -796,39 +798,49 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
                if (option_upload_pack)
                        transport_set_option(transport, TRANS_OPT_UPLOADPACK,
                                             option_upload_pack);
-
-               refs = transport_get_remote_refs(transport);
-               if (refs) {
-                       mapped_refs = wanted_peer_refs(refs, refspec);
-                       transport_fetch_refs(transport, mapped_refs);
-               }
        }
 
+       refs = transport_get_remote_refs(transport);
+
        if (refs) {
+               mapped_refs = wanted_peer_refs(refs, refspec);
+               /*
+                * transport_get_remote_refs() may return refs with null sha-1
+                * in mapped_refs (see struct transport->get_refs_list
+                * comment). In that case we need fetch it early because
+                * remote_head code below relies on it.
+                *
+                * for normal clones, transport_get_remote_refs() should
+                * return reliable ref set, we can delay cloning until after
+                * remote HEAD check.
+                */
+               for (ref = refs; ref; ref = ref->next)
+                       if (is_null_sha1(ref->old_sha1)) {
+                               complete_refs_before_fetch = 0;
+                               break;
+                       }
+
+               if (!is_local && !complete_refs_before_fetch)
+                       transport_fetch_refs(transport, mapped_refs);
+
                remote_head = find_ref_by_name(refs, "HEAD");
                remote_head_points_at =
                        guess_remote_head(remote_head, mapped_refs, 0);
 
                if (option_branch) {
-                       struct strbuf head = STRBUF_INIT;
-                       strbuf_addstr(&head, src_ref_prefix);
-                       strbuf_addstr(&head, option_branch);
                        our_head_points_at =
-                               find_ref_by_name(mapped_refs, head.buf);
-                       strbuf_release(&head);
-
-                       if (!our_head_points_at) {
-                               warning(_("Remote branch %s not found in "
-                                       "upstream %s, using HEAD instead"),
-                                       option_branch, option_origin);
-                               our_head_points_at = remote_head_points_at;
-                       }
+                               find_remote_branch(mapped_refs, option_branch);
+
+                       if (!our_head_points_at)
+                               die(_("Remote branch %s not found in upstream %s"),
+                                   option_branch, option_origin);
                }
                else
                        our_head_points_at = remote_head_points_at;
        }
        else {
                warning(_("You appear to have cloned an empty repository."));
+               mapped_refs = NULL;
                our_head_points_at = NULL;
                remote_head_points_at = NULL;
                remote_head = NULL;
@@ -838,15 +850,18 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
                                              "refs/heads/master");
        }
 
+       if (is_local)
+               clone_local(path, git_dir);
+       else if (refs && complete_refs_before_fetch)
+               transport_fetch_refs(transport, mapped_refs);
+
        update_remote_refs(refs, mapped_refs, remote_head_points_at,
                           branch_top.buf, reflog_msg.buf);
 
        update_head(our_head_points_at, remote_head, reflog_msg.buf);
 
-       if (transport) {
-               transport_unlock_pack(transport);
-               transport_disconnect(transport);
-       }
+       transport_unlock_pack(transport);
+       transport_disconnect(transport);
 
        err = checkout();