Merge branch 'ps/guess-repo-name-at-root'
authorJunio C Hamano <gitster@pobox.com>
Wed, 19 Aug 2015 21:48:54 +0000 (14:48 -0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 19 Aug 2015 21:48:54 +0000 (14:48 -0700)
"git clone $URL", when cloning from a site whose sole purpose is to
host a single repository (hence, no path after <scheme>://<site>/),
tried to use the site name as the new repository name, but did not
remove username or password when <site> part was of the form
<user>@<pass>:<host>. The code is taught to redact these.

* ps/guess-repo-name-at-root:
clone: abort if no dir name could be guessed
clone: do not use port number as dir name
clone: do not include authentication data in guessed dir

1  2 
builtin/clone.c
diff --combined builtin/clone.c
index bf451995a3c7bc3d8bd322c1a3355d8dc3dd80fa,f60d3271ed32638ca8d1306d236c650b995471a4..5169746b64888b3f8cf9d4fabd6ea52a45fff4d3
@@@ -146,30 -146,68 +146,68 @@@ static char *get_repo_path(const char *
  
  static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
  {
-       const char *end = repo + strlen(repo), *start;
+       const char *end = repo + strlen(repo), *start, *ptr;
        size_t len;
        char *dir;
  
+       /*
+        * Skip scheme.
+        */
+       start = strstr(repo, "://");
+       if (start == NULL)
+               start = repo;
+       else
+               start += 3;
+       /*
+        * Skip authentication data. The stripping does happen
+        * greedily, such that we strip up to the last '@' inside
+        * the host part.
+        */
+       for (ptr = start; ptr < end && !is_dir_sep(*ptr); ptr++) {
+               if (*ptr == '@')
+                       start = ptr + 1;
+       }
        /*
         * Strip trailing spaces, slashes and /.git
         */
-       while (repo < end && (is_dir_sep(end[-1]) || isspace(end[-1])))
+       while (start < end && (is_dir_sep(end[-1]) || isspace(end[-1])))
                end--;
-       if (end - repo > 5 && is_dir_sep(end[-5]) &&
+       if (end - start > 5 && is_dir_sep(end[-5]) &&
            !strncmp(end - 4, ".git", 4)) {
                end -= 5;
-               while (repo < end && is_dir_sep(end[-1]))
+               while (start < 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.
+        * Strip trailing port number if we've got only a
+        * hostname (that is, there is no dir separator but a
+        * colon). This check is required such that we do not
+        * strip URI's like '/foo/bar:2222.git', which should
+        * result in a dir '2222' being guessed due to backwards
+        * compatibility.
+        */
+       if (memchr(start, '/', end - start) == NULL
+           && memchr(start, ':', end - start) != NULL) {
+               ptr = end;
+               while (start < ptr && isdigit(ptr[-1]) && ptr[-1] != ':')
+                       ptr--;
+               if (start < ptr && ptr[-1] == ':')
+                       end = ptr - 1;
+       }
+       /*
+        * Find last component. To remain backwards compatible we
+        * also regard colons as path separators, such that
+        * cloning a repository 'foo:bar.git' would result in a
+        * directory 'bar' being guessed.
         */
-       start = end;
-       while (repo < start && !is_dir_sep(start[-1]) && start[-1] != ':')
-               start--;
+       ptr = end;
+       while (start < ptr && !is_dir_sep(ptr[-1]) && ptr[-1] != ':')
+               ptr--;
+       start = ptr;
  
        /*
         * Strip .{bundle,git}.
        len = end - start;
        strip_suffix_mem(start, &len, is_bundle ? ".bundle" : ".git");
  
+       if (!len || (len == 1 && *start == '/'))
+           die("No directory name could be guessed.\n"
+               "Please specify a directory on the command line");
        if (is_bare)
                dir = xstrfmt("%.*s.git", (int)len, start);
        else
@@@ -278,17 -320,16 +320,17 @@@ static void copy_alternates(struct strb
        struct strbuf line = STRBUF_INIT;
  
        while (strbuf_getline(&line, in, '\n') != EOF) {
 -              char *abs_path, abs_buf[PATH_MAX];
 +              char *abs_path;
                if (!line.len || line.buf[0] == '#')
                        continue;
                if (is_absolute_path(line.buf)) {
                        add_to_alternates_file(line.buf);
                        continue;
                }
 -              abs_path = mkpath("%s/objects/%s", src_repo, line.buf);
 -              normalize_path_copy(abs_buf, abs_path);
 -              add_to_alternates_file(abs_buf);
 +              abs_path = mkpathdup("%s/objects/%s", src_repo, line.buf);
 +              normalize_path_copy(abs_path, abs_path);
 +              add_to_alternates_file(abs_path);
 +              free(abs_path);
        }
        strbuf_release(&line);
        fclose(in);
@@@ -485,26 -526,16 +527,26 @@@ static void write_remote_refs(const str
  {
        const struct ref *r;
  
 -      lock_packed_refs(LOCK_DIE_ON_ERROR);
 +      struct ref_transaction *t;
 +      struct strbuf err = STRBUF_INIT;
 +
 +      t = ref_transaction_begin(&err);
 +      if (!t)
 +              die("%s", err.buf);
  
        for (r = local_refs; r; r = r->next) {
                if (!r->peer_ref)
                        continue;
 -              add_packed_ref(r->peer_ref->name, r->old_sha1);
 +              if (ref_transaction_create(t, r->peer_ref->name, r->old_sha1,
 +                                         0, NULL, &err))
 +                      die("%s", err.buf);
        }
  
 -      if (commit_packed_refs())
 -              die_errno("unable to overwrite old ref-pack file");
 +      if (initial_ref_transaction_commit(t, &err))
 +              die("%s", err.buf);
 +
 +      strbuf_release(&err);
 +      ref_transaction_free(t);
  }
  
  static void write_followtags(const struct ref *refs, const char *msg)