Code

Merge branch 'jl/fetch-submodule-recursive'
[git.git] / builtin / fetch.c
index cbbde2d0ee15623299ded2c74d06a4be5bdcf548..357f3cdbbfd601e2ce3f1261a4d6b30c1257cda4 100644 (file)
@@ -17,7 +17,7 @@
 static const char * const builtin_fetch_usage[] = {
        "git fetch [<options>] [<repository> [<refspec>...]]",
        "git fetch [<options>] <group>",
-       "git fetch --multiple [<options>] [<repository> | <group>]...",
+       "git fetch --multiple [<options>] [(<repository> | <group>)...]",
        "git fetch --all [<options>]",
        NULL
 };
@@ -28,8 +28,14 @@ enum {
        TAGS_SET = 2
 };
 
+enum {
+       RECURSE_SUBMODULES_OFF = 0,
+       RECURSE_SUBMODULES_DEFAULT = 1,
+       RECURSE_SUBMODULES_ON = 2
+};
+
 static int all, append, dry_run, force, keep, multiple, prune, update_head_ok, verbosity;
-static int progress, recurse_submodules;
+static int progress, recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
 static int tags = TAGS_DEFAULT;
 static const char *depth;
 static const char *upload_pack;
@@ -45,8 +51,7 @@ static struct option builtin_fetch_options[] = {
                    "append to .git/FETCH_HEAD instead of overwriting"),
        OPT_STRING(0, "upload-pack", &upload_pack, "PATH",
                   "path to upload pack on remote end"),
-       OPT_BOOLEAN('f', "force", &force,
-                   "force overwrite of local branch"),
+       OPT__FORCE(&force, "force overwrite of local branch"),
        OPT_BOOLEAN('m', "multiple", &multiple,
                    "fetch from multiple remotes"),
        OPT_SET_INT('t', "tags", &tags,
@@ -54,9 +59,10 @@ static struct option builtin_fetch_options[] = {
        OPT_SET_INT('n', NULL, &tags,
                    "do not fetch all tags (--no-tags)", TAGS_UNSET),
        OPT_BOOLEAN('p', "prune", &prune,
-                   "prune tracking branches no longer on remote"),
-       OPT_BOOLEAN(0, "recurse-submodules", &recurse_submodules,
-                   "control recursive fetching of submodules"),
+                   "prune remote-tracking branches no longer on remote"),
+       OPT_SET_INT(0, "recurse-submodules", &recurse_submodules,
+                   "control recursive fetching of submodules",
+                   RECURSE_SUBMODULES_ON),
        OPT_BOOLEAN(0, "dry-run", &dry_run,
                    "dry run"),
        OPT_BOOLEAN('k', "keep", &keep, "keep downloaded pack"),
@@ -104,7 +110,7 @@ static void add_merge_config(struct ref **head,
                        continue;
 
                /*
-                * Not fetched to a tracking branch?  We need to fetch
+                * Not fetched to a remote-tracking branch?  We need to fetch
                 * it anyway to allow this branch's "branch.$name.merge"
                 * to be honored by 'git pull', but we do not have to
                 * fail if branch.$name.merge is misconfigured to point
@@ -152,7 +158,10 @@ static struct ref *get_ref_map(struct transport *transport,
                struct remote *remote = transport->remote;
                struct branch *branch = branch_get(NULL);
                int has_merge = branch_has_merge_config(branch);
-               if (remote && (remote->fetch_refspec_nr || has_merge)) {
+               if (remote &&
+                   (remote->fetch_refspec_nr ||
+                    /* Note: has_merge implies non-NULL branch->remote_name */
+                    (has_merge && !strcmp(branch->remote_name, remote->name)))) {
                        for (i = 0; i < remote->fetch_refspec_nr; i++) {
                                get_fetch_map(remote_refs, &remote->fetch[i], &tail, 0);
                                if (remote->fetch[i].dst &&
@@ -166,6 +175,8 @@ static struct ref *get_ref_map(struct transport *transport,
                         * if the remote we're fetching from is the same
                         * as given in branch.<name>.remote, we add the
                         * ref given in branch.<name>.merge, too.
+                        *
+                        * Note: has_merge implies non-NULL branch->remote_name
                         */
                        if (has_merge &&
                            !strcmp(branch->remote_name, remote->name))
@@ -360,7 +371,7 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
                        what = rm->name + 10;
                }
                else if (!prefixcmp(rm->name, "refs/remotes/")) {
-                       kind = "remote branch";
+                       kind = "remote-tracking branch";
                        what = rm->name + 13;
                }
                else {
@@ -665,10 +676,12 @@ static int do_fetch(struct transport *transport,
 
        for_each_ref(add_existing, &existing_refs);
 
-       if (transport->remote->fetch_tags == 2 && tags != TAGS_UNSET)
-               tags = TAGS_SET;
-       if (transport->remote->fetch_tags == -1)
-               tags = TAGS_UNSET;
+       if (tags == TAGS_DEFAULT) {
+               if (transport->remote->fetch_tags == 2)
+                       tags = TAGS_SET;
+               if (transport->remote->fetch_tags == -1)
+                       tags = TAGS_UNSET;
+       }
 
        if (!transport->get_refs_list || !transport->fetch)
                die("Don't know how to fetch from %s", transport->url);
@@ -795,7 +808,7 @@ static void add_options_to_argv(int *argc, const char **argv)
                argv[(*argc)++] = "--force";
        if (keep)
                argv[(*argc)++] = "--keep";
-       if (recurse_submodules)
+       if (recurse_submodules == RECURSE_SUBMODULES_ON)
                argv[(*argc)++] = "--recurse-submodules";
        if (verbosity >= 2)
                argv[(*argc)++] = "-v";
@@ -933,14 +946,18 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
                }
        }
 
-       if (!result && recurse_submodules) {
+       if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) {
                const char *options[10];
                int num_options = 0;
+               /* Set recursion as default when we already are recursing */
+               if (submodule_prefix[0])
+                       set_config_fetch_recurse_submodules(1);
                gitmodules_config();
                git_config(submodule_config, NULL);
                add_options_to_argv(&num_options, options);
                result = fetch_populated_submodules(num_options, options,
                                                    submodule_prefix,
+                                                   recurse_submodules == RECURSE_SUBMODULES_ON,
                                                    verbosity < 0);
        }