Merge branch 'tg/push-all-in-mirror-forbidden'
[gitweb.git] / builtin / fetch.c
index 717dd14e896117c56101f7f56df0261576c323d0..9b27ae968199a75e62a617b649b56788e501e076 100644 (file)
@@ -23,6 +23,8 @@
 #include "packfile.h"
 #include "list-objects-filter-options.h"
 #include "commit-reach.h"
+#include "branch.h"
+#include "promisor-remote.h"
 
 #define FORCED_UPDATES_DELAY_WARNING_IN_MS (10 * 1000)
 
@@ -50,7 +52,8 @@ static int fetch_prune_tags_config = -1; /* unspecified */
 static int prune_tags = -1; /* unspecified */
 #define PRUNE_TAGS_BY_DEFAULT 0 /* do we prune tags by default? */
 
-static int all, append, dry_run, force, keep, multiple, update_head_ok, verbosity, deepen_relative;
+static int all, append, dry_run, force, keep, multiple, update_head_ok;
+static int verbosity, deepen_relative, set_upstream;
 static int progress = -1;
 static int enable_auto_gc = 1;
 static int tags = TAGS_DEFAULT, unshallow, update_shallow, deepen;
@@ -123,6 +126,8 @@ static struct option builtin_fetch_options[] = {
        OPT__VERBOSITY(&verbosity),
        OPT_BOOL(0, "all", &all,
                 N_("fetch from all remotes")),
+       OPT_BOOL(0, "set-upstream", &set_upstream,
+                N_("set upstream for git pull/fetch")),
        OPT_BOOL('a', "append", &append,
                 N_("append to .git/FETCH_HEAD instead of overwriting")),
        OPT_STRING(0, "upload-pack", &upload_pack, N_("path"),
@@ -1238,13 +1243,10 @@ static struct transport *prepare_transport(struct remote *remote, int deepen)
        if (update_shallow)
                set_option(transport, TRANS_OPT_UPDATE_SHALLOW, "yes");
        if (filter_options.choice) {
-               struct strbuf expanded_filter_spec = STRBUF_INIT;
-               expand_list_objects_filter_spec(&filter_options,
-                                               &expanded_filter_spec);
-               set_option(transport, TRANS_OPT_LIST_OBJECTS_FILTER,
-                          expanded_filter_spec.buf);
+               const char *spec =
+                       expand_list_objects_filter_spec(&filter_options);
+               set_option(transport, TRANS_OPT_LIST_OBJECTS_FILTER, spec);
                set_option(transport, TRANS_OPT_FROM_PROMISOR, "1");
-               strbuf_release(&expanded_filter_spec);
        }
        if (negotiation_tip.nr) {
                if (transport->smart_options)
@@ -1367,6 +1369,51 @@ static int do_fetch(struct transport *transport,
                retcode = 1;
                goto cleanup;
        }
+
+       if (set_upstream) {
+               struct branch *branch = branch_get("HEAD");
+               struct ref *rm;
+               struct ref *source_ref = NULL;
+
+               /*
+                * We're setting the upstream configuration for the
+                * current branch. The relevent upstream is the
+                * fetched branch that is meant to be merged with the
+                * current one, i.e. the one fetched to FETCH_HEAD.
+                *
+                * When there are several such branches, consider the
+                * request ambiguous and err on the safe side by doing
+                * nothing and just emit a warning.
+                */
+               for (rm = ref_map; rm; rm = rm->next) {
+                       if (!rm->peer_ref) {
+                               if (source_ref) {
+                                       warning(_("multiple branch detected, incompatible with --set-upstream"));
+                                       goto skip;
+                               } else {
+                                       source_ref = rm;
+                               }
+                       }
+               }
+               if (source_ref) {
+                       if (!strcmp(source_ref->name, "HEAD") ||
+                           starts_with(source_ref->name, "refs/heads/"))
+                               install_branch_config(0,
+                                                     branch->name,
+                                                     transport->remote->name,
+                                                     source_ref->name);
+                       else if (starts_with(source_ref->name, "refs/remotes/"))
+                               warning(_("not setting upstream for a remote remote-tracking branch"));
+                       else if (starts_with(source_ref->name, "refs/tags/"))
+                               warning(_("not setting upstream for a remote tag"));
+                       else
+                               warning(_("unknown branch type"));
+               } else {
+                       warning(_("no source branch found.\n"
+                               "you need to specify exactly one branch with the --set-upstream option."));
+               }
+       }
+ skip:
        free_refs(ref_map);
 
        /* if neither --no-tags nor --tags was specified, do automated tag
@@ -1510,37 +1557,27 @@ static inline void fetch_one_setup_partial(struct remote *remote)
         * If no prior partial clone/fetch and the current fetch DID NOT
         * request a partial-fetch, do a normal fetch.
         */
-       if (!repository_format_partial_clone && !filter_options.choice)
+       if (!has_promisor_remote() && !filter_options.choice)
                return;
 
        /*
-        * If this is the FIRST partial-fetch request, we enable partial
-        * on this repo and remember the given filter-spec as the default
-        * for subsequent fetches to this remote.
+        * If this is a partial-fetch request, we enable partial on
+        * this repo if not already enabled and remember the given
+        * filter-spec as the default for subsequent fetches to this
+        * remote.
         */
-       if (!repository_format_partial_clone && filter_options.choice) {
+       if (filter_options.choice) {
                partial_clone_register(remote->name, &filter_options);
                return;
        }
 
-       /*
-        * We are currently limited to only ONE promisor remote and only
-        * allow partial-fetches from the promisor remote.
-        */
-       if (strcmp(remote->name, repository_format_partial_clone)) {
-               if (filter_options.choice)
-                       die(_("--filter can only be used with the remote "
-                             "configured in extensions.partialClone"));
-               return;
-       }
-
        /*
         * Do a partial-fetch from the promisor remote using either the
         * explicitly given filter-spec or inherit the filter-spec from
         * the config.
         */
        if (!filter_options.choice)
-               partial_clone_get_default_filter_spec(&filter_options);
+               partial_clone_get_default_filter_spec(&filter_options, remote->name);
        return;
 }
 
@@ -1661,7 +1698,7 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
        if (depth || deepen_since || deepen_not.nr)
                deepen = 1;
 
-       if (filter_options.choice && !repository_format_partial_clone)
+       if (filter_options.choice && !has_promisor_remote())
                die("--filter can only be used when extensions.partialClone is set");
 
        if (all) {
@@ -1695,7 +1732,7 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
        }
 
        if (remote) {
-               if (filter_options.choice || repository_format_partial_clone)
+               if (filter_options.choice || has_promisor_remote())
                        fetch_one_setup_partial(remote);
                result = fetch_one(remote, argc, argv, prune_tags_ok);
        } else {