Merge branch 'rt/maint-clone-single'
authorJunio C Hamano <gitster@pobox.com>
Mon, 1 Oct 2012 19:58:10 +0000 (12:58 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 1 Oct 2012 19:58:10 +0000 (12:58 -0700)
Running "git fetch" in a repository made with "git clone --single"
slurps all the branches, defeating the point of "--single".

* rt/maint-clone-single:
clone --single: limit the fetch refspec to fetched branch

1  2 
Documentation/git-clone.txt
builtin/clone.c
index c1ddd4c2cc78e078a0849fc8fb82f8de0aa7fc9b,e4cc812ee5b4920c2d650223fa0d77469613cd21..6d98ef3d2ab8c8e63e026dbd7e242d3c3bc3c79a
@@@ -29,7 -29,8 +29,8 @@@ currently active branch
  After the clone, a plain `git fetch` without arguments will update
  all the remote-tracking branches, and a `git pull` without
  arguments will in addition merge the remote master branch into the
- current master branch, if any.
+ current master branch, if any (this is untrue when "--single-branch"
+ is given; see below).
  
  This default configuration is achieved by creating references to
  the remote branch heads under `refs/remotes/origin` and
@@@ -46,18 -47,13 +47,18 @@@ OPTION
        mechanism and clones the repository by making a copy of
        HEAD and everything under objects and refs directories.
        The files under `.git/objects/` directory are hardlinked
 -      to save space when possible.  This is now the default when
 -      the source repository is specified with `/path/to/repo`
 -      syntax, so it essentially is a no-op option.  To force
 -      copying instead of hardlinking (which may be desirable
 -      if you are trying to make a back-up of your repository),
 -      but still avoid the usual "git aware" transport
 -      mechanism, `--no-hardlinks` can be used.
 +      to save space when possible.
 ++
 +If the repository is specified as a local path (e.g., `/path/to/repo`),
 +this is the default, and --local is essentially a no-op.  If the
 +repository is specified as a URL, then this flag is ignored (and we
 +never use the local optimizations).  Specifying `--no-local` will
 +override the default when `/path/to/repo` is given, using the regular
 +git transport instead.
 ++
 +To force copying instead of hardlinking (which may be desirable if you
 +are trying to make a back-up of your repository), but still avoid the
 +usual "git aware" transport mechanism, `--no-hardlinks` can be used.
  
  --no-hardlinks::
        Optimize the cloning process from a repository on a
@@@ -152,9 -148,10 +153,10 @@@ objects from the source repository int
  -b <name>::
        Instead of pointing the newly created HEAD to the branch pointed
        to by the cloned repository's HEAD, point to `<name>` branch
-       instead. `--branch` can also take tags and treat them like
-       detached HEAD. In a non-bare repository, this is the branch
-       that will be checked out.
+       instead. In a non-bare repository, this is the branch that will
+       be checked out.
+       `--branch` can also take tags and detaches the HEAD at that commit
+       in the resulting repository.
  
  --upload-pack <upload-pack>::
  -u <upload-pack>::
        clone with the `--depth` option, this is the default, unless
        `--no-single-branch` is given to fetch the histories near the
        tips of all branches.
+       Further fetches into the resulting repository will only update the
+       remote tracking branch for the branch this option was used for the
+       initial cloning.  If the HEAD at the remote did not point at any
+       branch when `--single-branch` clone was made, no remote tracking
+       branch is created.
  
  --recursive::
  --recurse-submodules::
diff --combined builtin/clone.c
index 5a9b2bce2495cac1a343b10300804867e0f067e7,23caf1f00218b0186598d50a7b4bb4dc29110c26..ec2f75b4f3b9d9acfb8aa6b423f3b0fc7fbbafdf
   *
   */
  static const char * const builtin_clone_usage[] = {
 -      "git clone [options] [--] <repo> [<dir>]",
 +      N_("git clone [options] [--] <repo> [<dir>]"),
        NULL
  };
  
  static int option_no_checkout, option_bare, option_mirror, option_single_branch = -1;
 -static int option_local, option_no_hardlinks, option_shared, option_recursive;
 +static int option_local = -1, option_no_hardlinks, option_shared, option_recursive;
  static char *option_template, *option_depth;
  static char *option_origin = NULL;
  static char *option_branch = NULL;
@@@ -61,43 -61,43 +61,43 @@@ static int opt_parse_reference(const st
  static struct option builtin_clone_options[] = {
        OPT__VERBOSITY(&option_verbosity),
        OPT_BOOL(0, "progress", &option_progress,
 -               "force progress reporting"),
 +               N_("force progress reporting")),
        OPT_BOOLEAN('n', "no-checkout", &option_no_checkout,
 -                  "don't create a checkout"),
 -      OPT_BOOLEAN(0, "bare", &option_bare, "create a bare repository"),
 +                  N_("don't create a checkout")),
 +      OPT_BOOLEAN(0, "bare", &option_bare, N_("create a bare repository")),
        { OPTION_BOOLEAN, 0, "naked", &option_bare, NULL,
 -              "create a bare repository",
 +              N_("create a bare repository"),
                PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
        OPT_BOOLEAN(0, "mirror", &option_mirror,
 -                  "create a mirror repository (implies bare)"),
 -      OPT_BOOLEAN('l', "local", &option_local,
 -                  "to clone from a local repository"),
 +                  N_("create a mirror repository (implies bare)")),
 +      OPT_BOOL('l', "local", &option_local,
 +              N_("to clone from a local repository")),
        OPT_BOOLEAN(0, "no-hardlinks", &option_no_hardlinks,
 -                  "don't use local hardlinks, always copy"),
 +                  N_("don't use local hardlinks, always copy")),
        OPT_BOOLEAN('s', "shared", &option_shared,
 -                  "setup as shared repository"),
 +                  N_("setup as shared repository")),
        OPT_BOOLEAN(0, "recursive", &option_recursive,
 -                  "initialize submodules in the clone"),
 +                  N_("initialize submodules in the clone")),
        OPT_BOOLEAN(0, "recurse-submodules", &option_recursive,
 -                  "initialize submodules in the clone"),
 -      OPT_STRING(0, "template", &option_template, "template-directory",
 -                 "directory from which templates will be used"),
 -      OPT_CALLBACK(0 , "reference", &option_reference, "repo",
 -                   "reference repository", &opt_parse_reference),
 -      OPT_STRING('o', "origin", &option_origin, "name",
 -                 "use <name> instead of 'origin' to track upstream"),
 -      OPT_STRING('b', "branch", &option_branch, "branch",
 -                 "checkout <branch> instead of the remote's 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",
 -                  "create a shallow clone of that depth"),
 +                  N_("initialize submodules in the clone")),
 +      OPT_STRING(0, "template", &option_template, N_("template-directory"),
 +                 N_("directory from which templates will be used")),
 +      OPT_CALLBACK(0 , "reference", &option_reference, N_("repo"),
 +                   N_("reference repository"), &opt_parse_reference),
 +      OPT_STRING('o', "origin", &option_origin, N_("name"),
 +                 N_("use <name> instead of 'origin' to track upstream")),
 +      OPT_STRING('b', "branch", &option_branch, N_("branch"),
 +                 N_("checkout <branch> instead of the remote's HEAD")),
 +      OPT_STRING('u', "upload-pack", &option_upload_pack, N_("path"),
 +                 N_("path to git-upload-pack on the remote")),
 +      OPT_STRING(0, "depth", &option_depth, N_("depth"),
 +                  N_("create a shallow clone of that depth")),
        OPT_BOOL(0, "single-branch", &option_single_branch,
 -                  "clone only one branch, HEAD or --branch"),
 -      OPT_STRING(0, "separate-git-dir", &real_git_dir, "gitdir",
 -                 "separate git dir from working tree"),
 -      OPT_STRING_LIST('c', "config", &option_config, "key=value",
 -                      "set config inside the new repository"),
 +                  N_("clone only one branch, HEAD or --branch")),
 +      OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"),
 +                 N_("separate git dir from working tree")),
 +      OPT_STRING_LIST('c', "config", &option_config, N_("key=value"),
 +                      N_("set config inside the new repository")),
        OPT_END()
  };
  
@@@ -236,7 -236,7 +236,7 @@@ static int add_one_reference(struct str
        /* Beware: real_path() and mkpath() return static buffer */
        ref_git = xstrdup(real_path(item->string));
        if (is_directory(mkpath("%s/.git/objects", ref_git))) {
 -              char *ref_git_git = xstrdup(mkpath("%s/.git", ref_git));
 +              char *ref_git_git = mkpathdup("%s/.git", ref_git);
                free(ref_git);
                ref_git = ref_git_git;
        } else if (!is_directory(mkpath("%s/objects", ref_git)))
@@@ -342,7 -342,7 +342,7 @@@ static void copy_or_link_directory(stru
                if (!option_no_hardlinks) {
                        if (!link(src->buf, dest->buf))
                                continue;
 -                      if (option_local)
 +                      if (option_local > 0)
                                die_errno(_("failed to create link '%s'"), dest->buf);
                        option_no_hardlinks = 1;
                }
@@@ -610,6 -610,54 +610,54 @@@ static void write_config(struct string_
        }
  }
  
+ static void write_refspec_config(const char* src_ref_prefix,
+               const struct ref* our_head_points_at,
+               const struct ref* remote_head_points_at, struct strbuf* branch_top)
+ {
+       struct strbuf key = STRBUF_INIT;
+       struct strbuf value = STRBUF_INIT;
+       if (option_mirror || !option_bare) {
+               if (option_single_branch && !option_mirror) {
+                       if (option_branch) {
+                               if (strstr(our_head_points_at->name, "refs/tags/"))
+                                       strbuf_addf(&value, "+%s:%s", our_head_points_at->name,
+                                               our_head_points_at->name);
+                               else
+                                       strbuf_addf(&value, "+%s:%s%s", our_head_points_at->name,
+                                               branch_top->buf, option_branch);
+                       } else if (remote_head_points_at) {
+                               strbuf_addf(&value, "+%s:%s%s", remote_head_points_at->name,
+                                               branch_top->buf,
+                                               skip_prefix(remote_head_points_at->name, "refs/heads/"));
+                       }
+                       /*
+                        * otherwise, the next "git fetch" will
+                        * simply fetch from HEAD without updating
+                        * any remote tracking branch, which is what
+                        * we want.
+                        */
+               } else {
+                       strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top->buf);
+               }
+               /* Configure the remote */
+               if (value.len) {
+                       strbuf_addf(&key, "remote.%s.fetch", option_origin);
+                       git_config_set_multivar(key.buf, value.buf, "^$", 0);
+                       strbuf_reset(&key);
+                       if (option_mirror) {
+                               strbuf_addf(&key, "remote.%s.mirror", option_origin);
+                               git_config_set(key.buf, "true");
+                               strbuf_reset(&key);
+                       }
+               }
+       }
+       strbuf_release(&key);
+       strbuf_release(&value);
+ }
  int cmd_clone(int argc, const char **argv, const char *prefix)
  {
        int is_bundle = 0, is_local;
                die(_("repository '%s' does not exist"), repo_name);
        else
                repo = repo_name;
 -      is_local = path && !is_bundle;
 +      is_local = option_local != 0 && path && !is_bundle;
        if (is_local && option_depth)
                warning(_("--depth is ignored in local clones; use file:// instead."));
  
                git_dir = xstrdup(dir);
        else {
                work_tree = dir;
 -              git_dir = xstrdup(mkpath("%s/.git", dir));
 +              git_dir = mkpathdup("%s/.git", dir);
        }
  
        if (!option_bare) {
        }
  
        strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top.buf);
-       if (option_mirror || !option_bare) {
-               /* Configure the remote */
-               strbuf_addf(&key, "remote.%s.fetch", option_origin);
-               git_config_set_multivar(key.buf, value.buf, "^$", 0);
-               strbuf_reset(&key);
-               if (option_mirror) {
-                       strbuf_addf(&key, "remote.%s.mirror", option_origin);
-                       git_config_set(key.buf, "true");
-                       strbuf_reset(&key);
-               }
-       }
        strbuf_addf(&key, "remote.%s.url", option_origin);
        git_config_set(key.buf, repo);
        strbuf_reset(&key);
                                              "refs/heads/master");
        }
  
+       write_refspec_config(src_ref_prefix, our_head_points_at,
+                       remote_head_points_at, &branch_top);
        if (is_local)
                clone_local(path, git_dir);
        else if (refs && complete_refs_before_fetch)