]> git.ipfire.org Git - thirdparty/git.git/commitdiff
send-pack: pass negotiation config in push
authorDerrick Stolee <stolee@gmail.com>
Tue, 19 May 2026 16:24:55 +0000 (16:24 +0000)
committerJunio C Hamano <gitster@pobox.com>
Wed, 20 May 2026 02:33:24 +0000 (11:33 +0900)
When push.negotiate is enabled, 'git push' spawns a child 'git fetch
--negotiate-only' process to find common commits.  Pass
--negotiation-include and --negotiation-restrict options from the
'remote.<name>.negotiationInclude' and
'remote.<name>.negotiationRestrict' config keys to this child process.

When negotiationRestrict is configured, it replaces the default
behavior of using all remote refs as negotiation tips. This allows
the user to control which local refs are used for push negotiation.

When negotiationInclude is configured, the specified ref patterns
are passed as --negotiation-include to ensure their tips are always
sent as 'have' lines during push negotiation.

Reviewed-by: Matthew John Cheetham <mjcheetham@outlook.com>
Signed-off-by: Derrick Stolee <stolee@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/config/remote.adoc
send-pack.c
send-pack.h
t/t5516-fetch-push.sh
transport.c

index 1951df154e565dc6e55e8c7a1b18889c230a5270..eb9c8a3c488448e71a6660c08a8aeafa836a8ead 100644 (file)
@@ -122,6 +122,9 @@ command-line option.  If `--negotiation-restrict` (or its synonym
 `--negotiation-tip`) is specified on the command line, then the config
 values are not used.
 +
+These values also influence negotiation during `git push` if
+`push.negotiate` is enabled.
++
 Blank values signal to ignore all previous values, allowing a reset of
 the list from broader config scenarios.
 
@@ -147,6 +150,9 @@ negotiation algorithm still runs and advertises its own selected commits,
 but the refs matching `remote.<name>.negotiationInclude` are sent
 unconditionally on top of those heuristically selected commits.
 +
+These values also influence negotiation during `git push` if
+`push.negotiate` is enabled.
++
 Blank values signal to ignore all previous values, allowing a reset of
 the list from broader config scenarios.
 
index 3d5d36ba3baa2e80bf09f43a2af6131ea1c83feb..d18e030ce8bbd94acc72286e367f412032315ec3 100644 (file)
@@ -433,28 +433,48 @@ static void reject_invalid_nonce(const char *nonce, int len)
 
 static void get_commons_through_negotiation(struct repository *r,
                                            const char *url,
+                                           const struct string_list *negotiation_include,
+                                           const struct string_list *negotiation_restrict,
                                            const struct ref *remote_refs,
                                            struct oid_array *commons)
 {
        struct child_process child = CHILD_PROCESS_INIT;
        const struct ref *ref;
        int len = r->hash_algo->hexsz + 1; /* hash + NL */
-       int nr_negotiation_tip = 0;
+       int nr_negotiation = 0;
 
        child.git_cmd = 1;
        child.no_stdin = 1;
        child.out = -1;
        strvec_pushl(&child.args, "fetch", "--negotiate-only", NULL);
-       for (ref = remote_refs; ref; ref = ref->next) {
-               if (!is_null_oid(&ref->new_oid)) {
+
+       if (negotiation_restrict && negotiation_restrict->nr) {
+               struct string_list_item *item;
+               for_each_string_list_item(item, negotiation_restrict)
                        strvec_pushf(&child.args, "--negotiation-restrict=%s",
-                                    oid_to_hex(&ref->new_oid));
-                       nr_negotiation_tip++;
+                                    item->string);
+               nr_negotiation = negotiation_restrict->nr;
+       } else {
+               for (ref = remote_refs; ref; ref = ref->next) {
+                       if (!is_null_oid(&ref->new_oid)) {
+                               strvec_pushf(&child.args, "--negotiation-restrict=%s",
+                                            oid_to_hex(&ref->new_oid));
+                               nr_negotiation++;
+                       }
                }
        }
+
+       if (negotiation_include && negotiation_include->nr) {
+               struct string_list_item *item;
+               for_each_string_list_item(item, negotiation_include)
+                       strvec_pushf(&child.args, "--negotiation-include=%s",
+                                    item->string);
+               nr_negotiation += negotiation_include->nr;
+       }
+
        strvec_push(&child.args, url);
 
-       if (!nr_negotiation_tip) {
+       if (!nr_negotiation) {
                child_process_clear(&child);
                return;
        }
@@ -528,7 +548,10 @@ int send_pack(struct repository *r,
        repo_config_get_bool(r, "push.negotiate", &push_negotiate);
        if (push_negotiate) {
                trace2_region_enter("send_pack", "push_negotiate", r);
-               get_commons_through_negotiation(r, args->url, remote_refs, &commons);
+               get_commons_through_negotiation(r, args->url,
+                                              args->negotiation_include,
+                                              args->negotiation_restrict,
+                                              remote_refs, &commons);
                trace2_region_leave("send_pack", "push_negotiate", r);
        }
 
index c5ded2d2006f13c80d1fb097173f5a6a89606630..13850c98bb093a26e95bb3de521a3c96621cb953 100644 (file)
@@ -18,6 +18,8 @@ struct repository;
 
 struct send_pack_args {
        const char *url;
+       const struct string_list *negotiation_include;
+       const struct string_list *negotiation_restrict;
        unsigned verbose:1,
                quiet:1,
                porcelain:1,
index ac8447f21ed9638f5f3724020a9ee6e517def848..177cbc6c751fd217c0bc2de1c43534d989085a5c 100755 (executable)
@@ -254,6 +254,36 @@ test_expect_success 'push with negotiation does not attempt to fetch submodules'
        ! grep "Fetching submodule" err
 '
 
+test_expect_success 'push with negotiation and remote.<name>.negotiationInclude' '
+       test_when_finished rm -rf negotiation_include &&
+       mk_empty negotiation_include &&
+       git push negotiation_include $the_first_commit:refs/remotes/origin/first_commit &&
+       test_commit -C negotiation_include unrelated_commit &&
+       git -C negotiation_include config receive.hideRefs refs/remotes/origin/first_commit &&
+       test_when_finished "rm event" &&
+       GIT_TRACE2_EVENT="$(pwd)/event" \
+               git -c protocol.version=2 -c push.negotiate=1 \
+               -c remote.negotiation_include.negotiationInclude=refs/heads/main \
+               push negotiation_include refs/heads/main:refs/remotes/origin/main &&
+       test_grep \"key\":\"total_rounds\" event &&
+       grep_wrote 2 event # 1 commit, 1 tree
+'
+
+test_expect_success 'push with negotiation and remote.<name>.negotiationRestrict' '
+       test_when_finished rm -rf negotiation_restrict &&
+       mk_empty negotiation_restrict &&
+       git push negotiation_restrict $the_first_commit:refs/remotes/origin/first_commit &&
+       test_commit -C negotiation_restrict unrelated_commit &&
+       git -C negotiation_restrict config receive.hideRefs refs/remotes/origin/first_commit &&
+       test_when_finished "rm event" &&
+       GIT_TRACE2_EVENT="$(pwd)/event" \
+               git -c protocol.version=2 -c push.negotiate=1 \
+               -c remote.negotiation_restrict.negotiationRestrict=refs/heads/main \
+               push negotiation_restrict refs/heads/main:refs/remotes/origin/main &&
+       test_grep \"key\":\"total_rounds\" event &&
+       grep_wrote 2 event # 1 commit, 1 tree
+'
+
 test_expect_success 'push without wildcard' '
        mk_empty testrepo &&
 
index fa54928966e2f213b708a672794a9824b9f506dd..a2d8958cb82ab8b6c79b32de0c95d4dd3ba46e69 100644 (file)
@@ -921,6 +921,8 @@ static int git_transport_push(struct transport *transport, struct ref *remote_re
        args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
        args.push_options = transport->push_options;
        args.url = transport->url;
+       args.negotiation_include = &transport->remote->negotiation_include;
+       args.negotiation_restrict = &transport->remote->negotiation_restrict;
 
        if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
                args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;