Skip to content

Commit 7cccf59

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-require and --negotiation-restrict options from the 'remote.<name>.negotiationRequire' 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 negotiationRequire is configured, the specified ref patterns are passed as --negotiation-require to ensure their tips are always sent as 'have' lines during push negotiation. This change also updates the use of --negotiation-tip into --negotiation-restrict now that the new synonym exists. Signed-off-by: Derrick Stolee <stolee@gmail.com>
1 parent 081f904 commit 7cccf59

File tree

4 files changed

+62
-6
lines changed

4 files changed

+62
-6
lines changed

send-pack.c

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,8 @@ 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_require,
437+
const struct string_list *negotiation_restrict,
436438
const struct ref *remote_refs,
437439
struct oid_array *commons)
438440
{
@@ -445,13 +447,30 @@ static void get_commons_through_negotiation(struct repository *r,
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-
strvec_pushf(&child.args, "--negotiation-tip=%s",
451-
oid_to_hex(&ref->new_oid));
452-
nr_negotiation_tip++;
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",
455+
item->string);
456+
nr_negotiation_tip = 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-tip=%s",
461+
oid_to_hex(&ref->new_oid));
462+
nr_negotiation_tip++;
463+
}
453464
}
454465
}
466+
467+
if (negotiation_require && negotiation_require->nr) {
468+
struct string_list_item *item;
469+
for_each_string_list_item(item, negotiation_require)
470+
strvec_pushf(&child.args, "--negotiation-require=%s",
471+
item->string);
472+
}
473+
455474
strvec_push(&child.args, url);
456475

457476
if (!nr_negotiation_tip) {
@@ -528,7 +547,10 @@ int send_pack(struct repository *r,
528547
repo_config_get_bool(r, "push.negotiate", &push_negotiate);
529548
if (push_negotiate) {
530549
trace2_region_enter("send_pack", "push_negotiate", r);
531-
get_commons_through_negotiation(r, args->url, remote_refs, &commons);
550+
get_commons_through_negotiation(r, args->url,
551+
args->negotiation_require,
552+
args->negotiation_restrict,
553+
remote_refs, &commons);
532554
trace2_region_leave("send_pack", "push_negotiate", r);
533555
}
534556

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_require;
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>.negotiationRequire' '
258+
test_when_finished rm -rf negotiation_require &&
259+
mk_empty negotiation_require &&
260+
git push negotiation_require $the_first_commit:refs/remotes/origin/first_commit &&
261+
test_commit -C negotiation_require unrelated_commit &&
262+
git -C negotiation_require 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_require.negotiationRequire=refs/heads/main \
267+
push negotiation_require 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_require = &transport->remote->negotiation_require;
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)