Code

rebase -i: When an 'edit' stops, mention the commit
[git.git] / builtin-clone.c
index 71909520710ab046fe04723f5a25ac9a53374025..352224591f3be1eaff858b1fe10dac8f852f32c2 100644 (file)
@@ -18,6 +18,7 @@
 #include "transport.h"
 #include "strbuf.h"
 #include "dir.h"
+#include "pack-refs.h"
 
 /*
  * Overall FIXMEs:
@@ -28,7 +29,7 @@
  *
  */
 static const char * const builtin_clone_usage[] = {
-       "git-clone [options] [--] <repo> [<dir>]",
+       "git clone [options] [--] <repo> [<dir>]",
        NULL
 };
 
@@ -94,35 +95,38 @@ static char *get_repo_path(const char *repo, int *is_bundle)
 
 static char *guess_dir_name(const char *repo, int is_bundle)
 {
-       const char *p, *start, *end, *limit;
-       int after_slash_or_colon;
-
-       /* Guess dir name from repository: strip trailing '/',
-        * strip trailing '[:/]*.{git,bundle}', strip leading '.*[/:]'. */
-
-       after_slash_or_colon = 1;
-       limit = repo + strlen(repo);
-       start = repo;
-       end = limit;
-       for (p = repo; p < limit; p++) {
-               const char *prefix = is_bundle ? ".bundle" : ".git";
-               if (!prefixcmp(p, prefix)) {
-                       if (!after_slash_or_colon)
-                               end = p;
-                       p += strlen(prefix) - 1;
-               } else if (!prefixcmp(p, ".bundle")) {
-                       if (!after_slash_or_colon)
-                               end = p;
-                       p += 7;
-               } else if (*p == '/' || *p == ':') {
-                       if (end == limit)
-                               end = p;
-                       after_slash_or_colon = 1;
-               } else if (after_slash_or_colon) {
-                       start = p;
-                       end = limit;
-                       after_slash_or_colon = 0;
-               }
+       const char *end = repo + strlen(repo), *start;
+
+       /*
+        * Strip trailing slashes and /.git
+        */
+       while (repo < end && is_dir_sep(end[-1]))
+               end--;
+       if (end - repo > 5 && is_dir_sep(end[-5]) &&
+           !strncmp(end - 4, ".git", 4)) {
+               end -= 5;
+               while (repo < end && is_dir_sep(end[-1]))
+                       end--;
+       }
+
+       /*
+        * Find last component, but be prepared that repo could have
+        * the form  "remote.example.com:foo.git", i.e. no slash
+        * in the directory part.
+        */
+       start = end;
+       while (repo < start && !is_dir_sep(start[-1]) && start[-1] != ':')
+               start--;
+
+       /*
+        * Strip .{bundle,git}.
+        */
+       if (is_bundle) {
+               if (end - start > 7 && !strncmp(end - 7, ".bundle", 7))
+                       end -= 7;
+       } else {
+               if (end - start > 4 && !strncmp(end - 4, ".git", 4))
+                       end -= 4;
        }
 
        return xstrndup(start, end - start);
@@ -321,8 +325,11 @@ static struct ref *write_remote_refs(const struct ref *refs,
        get_fetch_map(refs, tag_refspec, &tail, 0);
 
        for (r = local_refs; r; r = r->next)
-               update_ref(reflog,
-                          r->peer_ref->name, r->old_sha1, NULL, 0, DIE_ON_ERR);
+               add_extra_ref(r->peer_ref->name, r->old_sha1, 0);
+
+       pack_refs(PACK_REFS_ALL);
+       clear_extra_refs();
+
        return local_refs;
 }
 
@@ -337,6 +344,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
        const struct ref *refs, *head_points_at, *remote_head, *mapped_refs;
        char branch_top[256], key[256], value[256];
        struct strbuf reflog_msg;
+       struct transport *transport = NULL;
 
        struct refspec refspec;
 
@@ -400,6 +408,9 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 
        if (!option_bare) {
                junk_work_tree = work_tree;
+               if (safe_create_leading_directories_const(work_tree) < 0)
+                       die("could not create leading directories of '%s'",
+                                       work_tree);
                if (mkdir(work_tree, 0755))
                        die("could not create work tree dir '%s'.", work_tree);
                set_git_work_tree(work_tree);
@@ -410,11 +421,19 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 
        setenv(CONFIG_ENVIRONMENT, xstrdup(mkpath("%s/config", git_dir)), 1);
 
+       if (safe_create_leading_directories_const(git_dir) < 0)
+               die("could not create leading directories of '%s'", git_dir);
        set_git_dir(make_absolute_path(git_dir));
 
-       fprintf(stderr, "Initialize %s\n", git_dir);
        init_db(option_template, option_quiet ? INIT_DB_QUIET : 0);
 
+       /*
+        * At this point, the config exists, so we do not need the
+        * environment variable.  We actually need to unset it, too, to
+        * re-enable parsing of the global configs.
+        */
+       unsetenv(CONFIG_ENVIRONMENT);
+
        if (option_reference)
                setup_reference(git_dir);
 
@@ -447,7 +466,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
                refs = clone_local(path, git_dir);
        else {
                struct remote *remote = remote_get(argv[0]);
-               struct transport *transport = transport_get(remote, argv[0]);
+               transport = transport_get(remote, remote->url[0]);
 
                if (!transport->get_refs_list || !transport->fetch)
                        die("Don't know how to clone %s", transport->url);
@@ -517,6 +536,9 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
                option_no_checkout = 1;
        }
 
+       if (transport)
+               transport_unlock_pack(transport);
+
        if (!option_no_checkout) {
                struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
                struct unpack_trees_options opts;