Skip to content

Commit ed0be32

Browse files
committed
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 <mcheetham@outlook.com> Signed-off-by: Derrick Stolee <stolee@gmail.com>
1 parent e571411 commit ed0be32

5 files changed

Lines changed: 70 additions & 7 deletions

File tree

Documentation/config/remote.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ command-line option. If `--negotiation-restrict` (or its synonym
122122
`--negotiation-tip`) is specified on the command line, then the config
123123
values are not used.
124124
+
125+
These values also influence negotiation during `git push` if
126+
`push.negotiate` is enabled.
127+
+
125128
Blank values signal to ignore all previous values, allowing a reset of
126129
the list from broader config scenarios.
127130

@@ -147,6 +150,9 @@ negotiation algorithm still runs and advertises its own selected commits,
147150
but the refs matching `remote.<name>.negotiationInclude` are sent
148151
unconditionally on top of those heuristically selected commits.
149152
+
153+
These values also influence negotiation during `git push` if
154+
`push.negotiate` is enabled.
155+
+
150156
Blank values signal to ignore all previous values, allowing a reset of
151157
the list from broader config scenarios.
152158

send-pack.c

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -433,28 +433,48 @@ static void reject_invalid_nonce(const char *nonce, int len)
433433

434434
static void get_commons_through_negotiation(struct repository *r,
435435
const char *url,
436+
const struct string_list *negotiation_include,
437+
const struct string_list *negotiation_restrict,
436438
const struct ref *remote_refs,
437439
struct oid_array *commons)
438440
{
439441
struct child_process child = CHILD_PROCESS_INIT;
440442
const struct ref *ref;
441443
int len = r->hash_algo->hexsz + 1; /* hash + NL */
442-
int nr_negotiation_tip = 0;
444+
int nr_negotiation = 0;
443445

444446
child.git_cmd = 1;
445447
child.no_stdin = 1;
446448
child.out = -1;
447449
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)
450454
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+
}
453464
}
454465
}
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+
455475
strvec_push(&child.args, url);
456476

457-
if (!nr_negotiation_tip) {
477+
if (!nr_negotiation) {
458478
child_process_clear(&child);
459479
return;
460480
}
@@ -528,7 +548,10 @@ int send_pack(struct repository *r,
528548
repo_config_get_bool(r, "push.negotiate", &push_negotiate);
529549
if (push_negotiate) {
530550
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);
532555
trace2_region_leave("send_pack", "push_negotiate", r);
533556
}
534557

send-pack.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ struct repository;
1818

1919
struct send_pack_args {
2020
const char *url;
21+
const struct string_list *negotiation_include;
22+
const struct string_list *negotiation_restrict;
2123
unsigned verbose:1,
2224
quiet:1,
2325
porcelain:1,

t/t5516-fetch-push.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,36 @@ test_expect_success 'push with negotiation does not attempt to fetch submodules'
254254
! grep "Fetching submodule" err
255255
'
256256

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+
257287
test_expect_success 'push without wildcard' '
258288
mk_empty testrepo &&
259289

transport.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,8 @@ static int git_transport_push(struct transport *transport, struct ref *remote_re
921921
args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
922922
args.push_options = transport->push_options;
923923
args.url = transport->url;
924+
args.negotiation_include = &transport->remote->negotiation_include;
925+
args.negotiation_restrict = &transport->remote->negotiation_restrict;
924926

925927
if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
926928
args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;

0 commit comments

Comments
 (0)