send-pack: pass negotiation config in push

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>

Derrick Stolee committed May 19, 2026 at 16:24 UTC a6d92c48e4426b88a427a75ed2c20d1daa5dc7f7
5 files changed +70 -7
Documentation/config/remote.adoc
+6
@@ -122,6 +122,9 @@ command-line option. If `--negotiation-restrict` (or its synonym
122 `--negotiation-tip`) is specified on the command line, then the config
123 values are not used.
124 +
125 +These values also influence negotiation during `git push` if
126 +`push.negotiate` is enabled.
127 ++
128 Blank values signal to ignore all previous values, allowing a reset of
129 the list from broader config scenarios.
130
@@ -147,6 +150,9 @@ negotiation algorithm still runs and advertises its own selected commits,
150 but the refs matching `remote.<name>.negotiationInclude` are sent
151 unconditionally on top of those heuristically selected commits.
152 +
153 +These values also influence negotiation during `git push` if
154 +`push.negotiate` is enabled.
155 ++
156 Blank values signal to ignore all previous values, allowing a reset of
157 the list from broader config scenarios.
158
send-pack.c
+30 -7
@@ -433,28 +433,48 @@ static void reject_invalid_nonce(const char *nonce, int len)
433
434 static void get_commons_through_negotiation(struct repository *r,
435 const char *url,
436 + const struct string_list *negotiation_include,
437 + const struct string_list *negotiation_restrict,
438 const struct ref *remote_refs,
439 struct oid_array *commons)
440 {
441 struct child_process child = CHILD_PROCESS_INIT;
442 const struct ref *ref;
443 int len = r->hash_algo->hexsz + 1; /* hash + NL */
442 - int nr_negotiation_tip = 0;
444 + int nr_negotiation = 0;
445
446 child.git_cmd = 1;
447 child.no_stdin = 1;
448 child.out = -1;
449 strvec_pushl(&child.args, "fetch", "--negotiate-only", NULL);
448 - for (ref = remote_refs; ref; ref = ref->next) {
449 - if (!is_null_oid(&ref->new_oid)) {
450 +
451 + if (negotiation_restrict && negotiation_restrict->nr) {
452 + struct string_list_item *item;
453 + for_each_string_list_item(item, negotiation_restrict)
454 strvec_pushf(&child.args, "--negotiation-restrict=%s",
451 - oid_to_hex(&ref->new_oid));
452 - nr_negotiation_tip++;
455 + item->string);
456 + nr_negotiation = negotiation_restrict->nr;
457 + } else {
458 + for (ref = remote_refs; ref; ref = ref->next) {
459 + if (!is_null_oid(&ref->new_oid)) {
460 + strvec_pushf(&child.args, "--negotiation-restrict=%s",
461 + oid_to_hex(&ref->new_oid));
462 + nr_negotiation++;
463 + }
464 }
465 }
466 +
467 + if (negotiation_include && negotiation_include->nr) {
468 + struct string_list_item *item;
469 + for_each_string_list_item(item, negotiation_include)
470 + strvec_pushf(&child.args, "--negotiation-include=%s",
471 + item->string);
472 + nr_negotiation += negotiation_include->nr;
473 + }
474 +
475 strvec_push(&child.args, url);
476
457 - if (!nr_negotiation_tip) {
477 + if (!nr_negotiation) {
478 child_process_clear(&child);
479 return;
480 }
@@ -528,7 +548,10 @@ int send_pack(struct repository *r,
548 repo_config_get_bool(r, "push.negotiate", &push_negotiate);
549 if (push_negotiate) {
550 trace2_region_enter("send_pack", "push_negotiate", r);
531 - get_commons_through_negotiation(r, args->url, remote_refs, &commons);
551 + get_commons_through_negotiation(r, args->url,
552 + args->negotiation_include,
553 + args->negotiation_restrict,
554 + remote_refs, &commons);
555 trace2_region_leave("send_pack", "push_negotiate", r);
556 }
557
send-pack.h
+2
@@ -18,6 +18,8 @@ struct repository;
18
19 struct send_pack_args {
20 const char *url;
21 + const struct string_list *negotiation_include;
22 + const struct string_list *negotiation_restrict;
23 unsigned verbose:1,
24 quiet:1,
25 porcelain:1,
t/t5516-fetch-push.sh
+30
@@ -254,6 +254,36 @@ test_expect_success 'push with negotiation does not attempt to fetch submodules'
254 ! grep "Fetching submodule" err
255 '
256
257 +test_expect_success 'push with negotiation and remote.<name>.negotiationInclude' '
258 + test_when_finished rm -rf negotiation_include &&
259 + mk_empty negotiation_include &&
260 + git push negotiation_include $the_first_commit:refs/remotes/origin/first_commit &&
261 + test_commit -C negotiation_include unrelated_commit &&
262 + git -C negotiation_include config receive.hideRefs refs/remotes/origin/first_commit &&
263 + test_when_finished "rm event" &&
264 + GIT_TRACE2_EVENT="$(pwd)/event" \
265 + git -c protocol.version=2 -c push.negotiate=1 \
266 + -c remote.negotiation_include.negotiationInclude=refs/heads/main \
267 + push negotiation_include refs/heads/main:refs/remotes/origin/main &&
268 + test_grep \"key\":\"total_rounds\" event &&
269 + grep_wrote 2 event # 1 commit, 1 tree
270 +'
271 +
272 +test_expect_success 'push with negotiation and remote.<name>.negotiationRestrict' '
273 + test_when_finished rm -rf negotiation_restrict &&
274 + mk_empty negotiation_restrict &&
275 + git push negotiation_restrict $the_first_commit:refs/remotes/origin/first_commit &&
276 + test_commit -C negotiation_restrict unrelated_commit &&
277 + git -C negotiation_restrict config receive.hideRefs refs/remotes/origin/first_commit &&
278 + test_when_finished "rm event" &&
279 + GIT_TRACE2_EVENT="$(pwd)/event" \
280 + git -c protocol.version=2 -c push.negotiate=1 \
281 + -c remote.negotiation_restrict.negotiationRestrict=refs/heads/main \
282 + push negotiation_restrict refs/heads/main:refs/remotes/origin/main &&
283 + test_grep \"key\":\"total_rounds\" event &&
284 + grep_wrote 2 event # 1 commit, 1 tree
285 +'
286 +
287 test_expect_success 'push without wildcard' '
288 mk_empty testrepo &&
289
transport.c
+2
@@ -921,6 +921,8 @@ static int git_transport_push(struct transport *transport, struct ref *remote_re
921 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
922 args.push_options = transport->push_options;
923 args.url = transport->url;
924 + args.negotiation_include = &transport->remote->negotiation_include;
925 + args.negotiation_restrict = &transport->remote->negotiation_restrict;
926
927 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
928 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;