Merge branch 'rt/maint-clone-single' into maint
authorJunio C Hamano <gitster@pobox.com>
Mon, 8 Oct 2012 18:34:02 +0000 (11:34 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 8 Oct 2012 18:34:02 +0000 (11:34 -0700)
A repository created with "git clone --single" had its fetch
refspecs set up just like a clone without "--single", leading the
subsequent "git fetch" to slurp all the other branches, defeating
the whole point of specifying "only this branch".

* 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 e314b0b6d2133b09307dc09fa256577e6cdba03a,23caf1f00218b0186598d50a7b4bb4dc29110c26..0d663e34f78431280eb7f79d581262a7c28820d3
@@@ -38,7 -38,7 +38,7 @@@ static const char * const builtin_clone
  };
  
  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;
@@@ -70,8 -70,8 +70,8 @@@ static struct option builtin_clone_opti
                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"),
 +      OPT_BOOL('l', "local", &option_local,
 +              "to clone from a local repository"),
        OPT_BOOLEAN(0, "no-hardlinks", &option_no_hardlinks,
                    "don't use local hardlinks, always copy"),
        OPT_BOOLEAN('s', "shared", &option_shared,
@@@ -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."));
  
        }
  
        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)