]> git.ipfire.org Git - thirdparty/git.git/commitdiff
fetch: add --negotiation-include option for negotiation
authorDerrick Stolee <stolee@gmail.com>
Wed, 22 Apr 2026 15:25:44 +0000 (15:25 +0000)
committerJunio C Hamano <gitster@pobox.com>
Wed, 22 Apr 2026 23:10:33 +0000 (16:10 -0700)
Add a new --negotiation-include option to 'git fetch', which ensures
that certain ref tips are always sent as 'have' lines during fetch
negotiation, regardless of what the negotiation algorithm selects.

This is useful when the repository has a large number of references, so
the normal negotiation algorithm truncates the list. This is especially
important in repositories with long parallel commit histories. For
example, a repo could have a 'dev' branch for development and a
'release' branch for released versions. If the 'dev' branch isn't
selected for negotiation, then it's not a big deal because there are
many in-progress development branches with a shared history. However, if
'release' is not selected for negotiation, then the server may think
that this is the first time the client has asked for that reference,
causing a full download of its parallel commit history (and any extra
data that may be unique to that branch). This is based on a real example
where certain fetches would grow to 60+ GB when a release branch
updated.

This option is a complement to --negotiation-restrict, which reduces the
negotiation ref set to a specific list. In the earlier example, using
--negotiation-restrict to focus the negotiation to 'dev' and 'release'
would avoid those problematic downloads, but would still not allow
advertising potentially-relevant user brances. In this way, the
'include' version solves the problem I mention while allowing
negotiation to pick other references opportunistically. The two options
can also be combined to allow the best of both worlds.

The argument may be an exact ref name or a glob pattern. Non-existent
refs are silently ignored. This behavior is also updated in the ref matching
logic for the related --negotiation-restrict option to match.

The implementation outputs the requested objects as haves before the
negotiation algorithm kicks in and performs a priority-queue walk from the
tip commits. In order to avoid duplicates, we mark the requested objects as
COMMON so they (and their descendants) are not output by the negotiator. The
negotiator still outputs at least one have before a round is flushed, when
the server could ACK to stop the negotiation.

Also add --negotiation-include to 'git pull' passthrough options.

Signed-off-by: Derrick Stolee <stolee@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/fetch-options.adoc
builtin/fetch.c
builtin/pull.c
fetch-pack.c
fetch-pack.h
t/t5510-fetch.sh
transport.c
transport.h

index c07b85499fafe9d3e2aa5af69e26363a07174923..decc7f6abd81156f893bd089549f8254dc206c9d 100644 (file)
@@ -73,6 +73,25 @@ See also the `fetch.negotiationAlgorithm` and `push.negotiate`
 configuration variables documented in linkgit:git-config[1], and the
 `--negotiate-only` option below.
 
+`--negotiation-include=<revision>`::
+       Ensure that the given ref tip is always sent as a "have" line
+       during fetch negotiation, regardless of what the negotiation
+       algorithm selects.  This is useful to guarantee that common
+       history reachable from specific refs is always considered, even
+       when `--negotiation-restrict` restricts the set of tips or when
+       the negotiation algorithm would otherwise skip them.
++
+This option may be specified more than once; if so, each ref is sent
+unconditionally.
++
+The argument may be an exact ref name (e.g. `refs/heads/release`) or a
+glob pattern (e.g. `refs/heads/release/{asterisk}`).  The pattern syntax
+is the same as for `--negotiation-restrict`.
++
+If `--negotiation-restrict` is used, the have set is first restricted by
+that option and then increased to include the tips specified by
+`--negotiation-include`.
+
 `--negotiate-only`::
        Do not fetch anything from the server, and instead print the
        ancestors of the provided `--negotiation-tip=` arguments,
index a1960e3e0cf28fa8f059ee620cfbe2af2b95d9cc..ef50e2fbe9a9d16ab03e19a19e8c062d3474ca12 100644 (file)
@@ -99,6 +99,7 @@ static struct transport *gsecondary;
 static struct refspec refmap = REFSPEC_INIT_FETCH;
 static struct string_list server_options = STRING_LIST_INIT_DUP;
 static struct string_list negotiation_restrict = STRING_LIST_INIT_NODUP;
+static struct string_list negotiation_include = STRING_LIST_INIT_NODUP;
 
 struct fetch_config {
        enum display_format display_format;
@@ -1547,10 +1548,14 @@ static void add_negotiation_restrict_tips(struct git_transport_options *smart_op
                int old_nr;
                if (!has_glob_specials(s)) {
                        struct object_id oid;
+
+                       /* Ignore missing reference. */
                        if (repo_get_oid(the_repository, s, &oid))
-                               die(_("%s is not a valid object"), s);
+                               continue;
+                       /* Fail on missing object pointed by ref. */
                        if (!odb_has_object(the_repository->objects, &oid, 0))
                                die(_("the object %s does not exist"), s);
+
                        oid_array_append(oids, &oid);
                        continue;
                }
@@ -1615,6 +1620,13 @@ static struct transport *prepare_transport(struct remote *remote, int deepen,
                        strbuf_release(&config_name);
                }
        }
+       if (negotiation_include.nr) {
+               if (transport->smart_options)
+                       transport->smart_options->negotiation_include = &negotiation_include;
+               else
+                       warning(_("ignoring %s because the protocol does not support it"),
+                               "--negotiation-include");
+       }
        return transport;
 }
 
@@ -2582,6 +2594,8 @@ int cmd_fetch(int argc,
                OPT_STRING_LIST(0, "negotiation-restrict", &negotiation_restrict, N_("revision"),
                                N_("report that we have only objects reachable from this object")),
                OPT_ALIAS(0, "negotiation-tip", "negotiation-restrict"),
+               OPT_STRING_LIST(0, "negotiation-include", &negotiation_include, N_("revision"),
+                               N_("ensure this ref is always sent as a negotiation have")),
                OPT_BOOL(0, "negotiate-only", &negotiate_only,
                         N_("do not fetch a packfile; instead, print ancestors of negotiation tips")),
                OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
index 821cc6699a142f954432586f87290d98a274ff12..86c85b60efd34872f5b100aaef5ad6c05dd86ddd 100644 (file)
@@ -1002,6 +1002,9 @@ int cmd_pull(int argc,
                OPT_PASSTHRU_ARGV(0, "negotiation-restrict", &opt_fetch, N_("revision"),
                        N_("report that we have only objects reachable from this object"),
                        0),
+               OPT_PASSTHRU_ARGV(0, "negotiation-include", &opt_fetch, N_("revision"),
+                       N_("ensure this ref is always sent as a negotiation have"),
+                       0),
                OPT_BOOL(0, "show-forced-updates", &opt_show_forced_updates,
                         N_("check for forced-updates on all updated branches")),
                OPT_PASSTHRU(0, "set-upstream", &set_upstream, NULL,
index baf239adf98db3715462a20c3809d20523de886d..8b080b0080453db77f33b557dbc03d1be0b9b4dd 100644 (file)
@@ -25,6 +25,7 @@
 #include "oidset.h"
 #include "packfile.h"
 #include "odb.h"
+#include "object-name.h"
 #include "path.h"
 #include "connected.h"
 #include "fetch-negotiator.h"
@@ -332,6 +333,48 @@ static void send_filter(struct fetch_pack_args *args,
        }
 }
 
+static int add_oid_to_oidset(const struct reference *ref, void *cb_data)
+{
+       struct oidset *set = cb_data;
+       if (!odb_has_object(the_repository->objects, ref->oid, 0))
+               die(_("the object %s does not exist"), oid_to_hex(ref->oid));
+       oidset_insert(set, ref->oid);
+       return 0;
+}
+
+static void resolve_negotiation_include(const struct string_list *negotiation_include,
+                                       struct oidset *result)
+{
+       struct string_list_item *item;
+
+       if (!negotiation_include || !negotiation_include->nr)
+               return;
+
+       for_each_string_list_item(item, negotiation_include) {
+               if (!has_glob_specials(item->string)) {
+                       struct object_id oid;
+
+                       /* Ignore missing reference. */
+                       if (repo_get_oid(the_repository, item->string, &oid))
+                               continue;
+
+                       /* Fail on missing object pointed by ref. */
+                       if (!odb_has_object(the_repository->objects, &oid, 0))
+                               die(_("the object %s does not exist"),
+                                   item->string);
+
+                       oidset_insert(result, &oid);
+               } else {
+                       struct refs_for_each_ref_options opts = {
+                               .pattern = item->string,
+                       };
+                       refs_for_each_ref_ext(
+                               get_main_ref_store(the_repository),
+                               add_oid_to_oidset, result, &opts);
+               }
+       }
+}
+
 static int find_common(struct fetch_negotiator *negotiator,
                       struct fetch_pack_args *args,
                       int fd[2], struct object_id *result_oid,
@@ -347,6 +390,7 @@ static int find_common(struct fetch_negotiator *negotiator,
        struct strbuf req_buf = STRBUF_INIT;
        size_t state_len = 0;
        struct packet_reader reader;
+       struct oidset negotiation_include_oids = OIDSET_INIT;
 
        if (args->stateless_rpc && multi_ack == 1)
                die(_("the option '%s' requires '%s'"), "--stateless-rpc", "multi_ack_detailed");
@@ -474,6 +518,33 @@ static int find_common(struct fetch_negotiator *negotiator,
        trace2_region_enter("fetch-pack", "negotiation_v0_v1", the_repository);
        flushes = 0;
        retval = -1;
+
+       /* Send unconditional haves from --negotiation-include */
+       resolve_negotiation_include(args->negotiation_include,
+                                   &negotiation_include_oids);
+       if (oidset_size(&negotiation_include_oids)) {
+               struct oidset_iter iter;
+               oidset_iter_init(&negotiation_include_oids, &iter);
+
+               while ((oid = oidset_iter_next(&iter))) {
+                       struct commit *commit;
+                       packet_buf_write(&req_buf, "have %s\n",
+                                        oid_to_hex(oid));
+                       print_verbose(args, "have %s", oid_to_hex(oid));
+                       count++;
+
+                       /*
+                        * If this is a commit, then mark as COMMON to
+                        * avoid the negotiator also outputting it as
+                        * a have.
+                        */
+                       commit = lookup_commit(the_repository, oid);
+                       if (commit &&
+                           !repo_parse_commit(the_repository, commit))
+                               commit->object.flags |= COMMON;
+               }
+       }
+
        while ((oid = negotiator->next(negotiator))) {
                packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
                print_verbose(args, "have %s", oid_to_hex(oid));
@@ -584,6 +655,7 @@ done:
                flushes++;
        }
        strbuf_release(&req_buf);
+       oidset_clear(&negotiation_include_oids);
 
        if (!got_ready || !no_done)
                consume_shallow_list(args, &reader);
@@ -1305,12 +1377,26 @@ static void add_common(struct strbuf *req_buf, struct oidset *common)
 
 static int add_haves(struct fetch_negotiator *negotiator,
                     struct strbuf *req_buf,
-                    int *haves_to_send)
+                    int *haves_to_send,
+                    struct oidset *negotiation_include_oids)
 {
        int haves_added = 0;
        const struct object_id *oid;
 
+       /* Send unconditional haves from --negotiation-include */
+       if (negotiation_include_oids) {
+               struct oidset_iter iter;
+               oidset_iter_init(negotiation_include_oids, &iter);
+
+               while ((oid = oidset_iter_next(&iter)))
+                       packet_buf_write(req_buf, "have %s\n",
+                                        oid_to_hex(oid));
+       }
+
        while ((oid = negotiator->next(negotiator))) {
+               if (negotiation_include_oids &&
+                   oidset_contains(negotiation_include_oids, oid))
+                       continue;
                packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
                if (++haves_added >= *haves_to_send)
                        break;
@@ -1358,7 +1444,8 @@ static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
                              struct fetch_pack_args *args,
                              const struct ref *wants, struct oidset *common,
                              int *haves_to_send, int *in_vain,
-                             int sideband_all, int seen_ack)
+                             int sideband_all, int seen_ack,
+                             struct oidset *negotiation_include_oids)
 {
        int haves_added;
        int done_sent = 0;
@@ -1413,7 +1500,8 @@ static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
        /* Add all of the common commits we've found in previous rounds */
        add_common(&req_buf, common);
 
-       haves_added = add_haves(negotiator, &req_buf, haves_to_send);
+       haves_added = add_haves(negotiator, &req_buf, haves_to_send,
+                              negotiation_include_oids);
        *in_vain += haves_added;
        trace2_data_intmax("negotiation_v2", the_repository, "haves_added", haves_added);
        trace2_data_intmax("negotiation_v2", the_repository, "in_vain", *in_vain);
@@ -1657,6 +1745,7 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
        struct ref *ref = copy_ref_list(orig_ref);
        enum fetch_state state = FETCH_CHECK_LOCAL;
        struct oidset common = OIDSET_INIT;
+       struct oidset negotiation_include_oids = OIDSET_INIT;
        struct packet_reader reader;
        int in_vain = 0, negotiation_started = 0;
        int negotiation_round = 0;
@@ -1729,6 +1818,8 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
                                state = FETCH_SEND_REQUEST;
 
                        mark_tips(negotiator, args->negotiation_restrict_tips);
+                       resolve_negotiation_include(args->negotiation_include,
+                                                   &negotiation_include_oids);
                        for_each_cached_alternate(negotiator,
                                                  insert_one_alternate_object);
                        break;
@@ -1747,7 +1838,8 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
                                               &common,
                                               &haves_to_send, &in_vain,
                                               reader.use_sideband,
-                                              seen_ack)) {
+                                              seen_ack,
+                                              &negotiation_include_oids)) {
                                trace2_region_leave_printf("negotiation_v2", "round",
                                                           the_repository, "%d",
                                                           negotiation_round);
@@ -1883,6 +1975,7 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
                negotiator->release(negotiator);
 
        oidset_clear(&common);
+       oidset_clear(&negotiation_include_oids);
        return ref;
 }
 
@@ -2181,12 +2274,14 @@ void negotiate_using_fetch(const struct oid_array *negotiation_restrict_tips,
                           const struct string_list *server_options,
                           int stateless_rpc,
                           int fd[],
-                          struct oidset *acked_commits)
+                          struct oidset *acked_commits,
+                          const struct string_list *negotiation_include)
 {
        struct fetch_negotiator negotiator;
        struct packet_reader reader;
        struct object_array nt_object_array = OBJECT_ARRAY_INIT;
        struct strbuf req_buf = STRBUF_INIT;
+       struct oidset negotiation_include_oids = OIDSET_INIT;
        int haves_to_send = INITIAL_FLUSH;
        int in_vain = 0;
        int seen_ack = 0;
@@ -2197,6 +2292,9 @@ void negotiate_using_fetch(const struct oid_array *negotiation_restrict_tips,
        fetch_negotiator_init(the_repository, &negotiator);
        mark_tips(&negotiator, negotiation_restrict_tips);
 
+       resolve_negotiation_include(negotiation_include,
+                                   &negotiation_include_oids);
+
        packet_reader_init(&reader, fd[0], NULL, 0,
                           PACKET_READ_CHOMP_NEWLINE |
                           PACKET_READ_DIE_ON_ERR_PACKET);
@@ -2221,7 +2319,8 @@ void negotiate_using_fetch(const struct oid_array *negotiation_restrict_tips,
 
                packet_buf_write(&req_buf, "wait-for-done");
 
-               haves_added = add_haves(&negotiator, &req_buf, &haves_to_send);
+               haves_added = add_haves(&negotiator, &req_buf, &haves_to_send,
+                                      &negotiation_include_oids);
                in_vain += haves_added;
                if (!haves_added || (seen_ack && in_vain >= MAX_IN_VAIN))
                        last_iteration = 1;
@@ -2273,6 +2372,7 @@ void negotiate_using_fetch(const struct oid_array *negotiation_restrict_tips,
 
        clear_common_flag(acked_commits);
        object_array_clear(&nt_object_array);
+       oidset_clear(&negotiation_include_oids);
        negotiator.release(&negotiator);
        strbuf_release(&req_buf);
 }
index 6c70c942c2f001a7df5fa32bc6970b950e9fd90e..32ae94d0b47bae903b02971a2a854686ee78a657 100644 (file)
@@ -23,6 +23,13 @@ struct fetch_pack_args {
         */
        const struct oid_array *negotiation_restrict_tips;
 
+       /*
+        * If non-empty, ref patterns whose tips should always be sent
+        * as "have" lines during negotiation, regardless of what the
+        * negotiation algorithm selects.
+        */
+       const struct string_list *negotiation_include;
+
        unsigned deepen_relative:1;
        unsigned quiet:1;
        unsigned keep_pack:1;
@@ -93,7 +100,8 @@ void negotiate_using_fetch(const struct oid_array *negotiation_restrict_tips,
                           const struct string_list *server_options,
                           int stateless_rpc,
                           int fd[],
-                          struct oidset *acked_commits);
+                          struct oidset *acked_commits,
+                          const struct string_list *negotiation_include);
 
 /*
  * Print an appropriate error message for each sought ref that wasn't
index eff3ce8e2de89cbc513eb26396098e0307b7655e..4316f8d4eaf67c5fc375d1a4619d6a1c0f4e5fbe 100755 (executable)
@@ -1511,6 +1511,72 @@ test_expect_success 'CLI --negotiation-restrict overrides remote config' '
        test_grep ! "fetch> have $BETA_1" trace
 '
 
+test_expect_success '--negotiation-include includes configured refs as haves' '
+       test_when_finished rm -f trace &&
+       setup_negotiation_tip server server 0 &&
+
+       GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
+               --negotiation-restrict=alpha_1 \
+               --negotiation-include=refs/tags/beta_1 \
+               origin alpha_s beta_s &&
+
+       ALPHA_1=$(git -C client rev-parse alpha_1) &&
+       test_grep "fetch> have $ALPHA_1" trace &&
+       BETA_1=$(git -C client rev-parse beta_1) &&
+       test_grep "fetch> have $BETA_1" trace
+'
+
+test_expect_success '--negotiation-include works with glob patterns' '
+       test_when_finished rm -f trace &&
+       setup_negotiation_tip server server 0 &&
+
+       GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
+               --negotiation-restrict=alpha_1 \
+               --negotiation-include="refs/tags/beta_*" \
+               origin alpha_s beta_s &&
+
+       BETA_1=$(git -C client rev-parse beta_1) &&
+       test_grep "fetch> have $BETA_1" trace &&
+       BETA_2=$(git -C client rev-parse beta_2) &&
+       test_grep "fetch> have $BETA_2" trace
+'
+
+test_expect_success '--negotiation-include is additive with negotiation' '
+       test_when_finished rm -f trace &&
+       setup_negotiation_tip server server 0 &&
+
+       GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
+               --negotiation-include=refs/tags/beta_1 \
+               origin alpha_s beta_s &&
+
+       BETA_1=$(git -C client rev-parse beta_1) &&
+       test_grep "fetch> have $BETA_1" trace
+'
+
+test_expect_success '--negotiation-include ignores non-existent refs silently' '
+       setup_negotiation_tip server server 0 &&
+
+       git -C client fetch --quiet \
+               --negotiation-restrict=alpha_1 \
+               --negotiation-include=refs/tags/nonexistent \
+               origin alpha_s beta_s 2>err &&
+       test_must_be_empty err
+'
+
+test_expect_success '--negotiation-include avoids duplicates with negotiator' '
+       test_when_finished rm -f trace &&
+       setup_negotiation_tip server server 0 &&
+
+       ALPHA_1=$(git -C client rev-parse alpha_1) &&
+       GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
+               --negotiation-restrict=alpha_1 \
+               --negotiation-include=refs/tags/alpha_1 \
+               origin alpha_s beta_s &&
+
+       test_grep "fetch> have $ALPHA_1" trace >matches &&
+       test_line_count = 1 matches
+'
+
 test_expect_success SYMLINKS 'clone does not get confused by a D/F conflict' '
        git init df-conflict &&
        (
index a3051f6733633d2d63841952e85f247fab51c279..8a2d8adffc21316d53a78b4d0679ccefda157630 100644 (file)
@@ -464,6 +464,7 @@ static int fetch_refs_via_pack(struct transport *transport,
        args.stateless_rpc = transport->stateless_rpc;
        args.server_options = transport->server_options;
        args.negotiation_restrict_tips = data->options.negotiation_restrict_tips;
+       args.negotiation_include = data->options.negotiation_include;
        args.reject_shallow_remote = transport->smart_options->reject_shallow;
 
        if (!data->finished_handshake) {
@@ -495,7 +496,8 @@ static int fetch_refs_via_pack(struct transport *transport,
                                              transport->server_options,
                                              transport->stateless_rpc,
                                              data->fd,
-                                             data->options.acked_commits);
+                                             data->options.acked_commits,
+                                             data->options.negotiation_include);
                        ret = 0;
                }
                goto cleanup;
index cdeb33c16f82f6e94917f10dea62605cce31a551..6092775a27039070df643de6ca6a47c6e0b25406 100644 (file)
@@ -48,6 +48,12 @@ struct git_transport_options {
         */
        struct oid_array *negotiation_restrict_tips;
 
+       /*
+        * If non-empty, ref patterns whose tips should always be sent
+        * as "have" lines during negotiation.
+        */
+       const struct string_list *negotiation_include;
+
        /*
         * If allocated, whenever transport_fetch_refs() is called, add known
         * common commits to this oidset instead of fetching any packfiles.