Skip to content

Commit 35e22d5

Browse files
committed
Merge branch 'jt/fetch-nego-tip' into ab/fetch-nego
* jt/fetch-nego-tip: fetch-pack: support negotiation tip whitelist
2 parents 42cc748 + 3390e42 commit 35e22d5

File tree

8 files changed

+176
-2
lines changed

8 files changed

+176
-2
lines changed

Documentation/fetch-options.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,22 @@ the current repository has the same history as the source repository.
4242
.git/shallow. This option updates .git/shallow and accept such
4343
refs.
4444

45+
--negotiation-tip=<commit|glob>::
46+
By default, Git will report, to the server, commits reachable
47+
from all local refs to find common commits in an attempt to
48+
reduce the size of the to-be-received packfile. If specified,
49+
Git will only report commits reachable from the given tips.
50+
This is useful to speed up fetches when the user knows which
51+
local ref is likely to have commits in common with the
52+
upstream ref being fetched.
53+
+
54+
This option may be specified more than once; if so, Git will report
55+
commits reachable from any of the given commits.
56+
+
57+
The argument to this option may be a glob on ref names, a ref, or the (possibly
58+
abbreviated) SHA-1 of a commit. Specifying a glob is equivalent to specifying
59+
this option multiple times, one for each matching ref name.
60+
4561
ifndef::git-pull[]
4662
--dry-run::
4763
Show what would be done, without making any changes.

builtin/fetch.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ static int shown_url = 0;
6363
static struct refspec refmap = REFSPEC_INIT_FETCH;
6464
static struct list_objects_filter_options filter_options;
6565
static struct string_list server_options = STRING_LIST_INIT_DUP;
66+
static struct string_list negotiation_tip = STRING_LIST_INIT_NODUP;
6667

6768
static int git_fetch_config(const char *k, const char *v, void *cb)
6869
{
@@ -174,6 +175,8 @@ static struct option builtin_fetch_options[] = {
174175
TRANSPORT_FAMILY_IPV4),
175176
OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
176177
TRANSPORT_FAMILY_IPV6),
178+
OPT_STRING_LIST(0, "negotiation-tip", &negotiation_tip, N_("revision"),
179+
N_("report that we have only objects reachable from this object")),
177180
OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
178181
OPT_END()
179182
};
@@ -1049,6 +1052,40 @@ static void set_option(struct transport *transport, const char *name, const char
10491052
name, transport->url);
10501053
}
10511054

1055+
1056+
static int add_oid(const char *refname, const struct object_id *oid, int flags,
1057+
void *cb_data)
1058+
{
1059+
struct oid_array *oids = cb_data;
1060+
1061+
oid_array_append(oids, oid);
1062+
return 0;
1063+
}
1064+
1065+
static void add_negotiation_tips(struct git_transport_options *smart_options)
1066+
{
1067+
struct oid_array *oids = xcalloc(1, sizeof(*oids));
1068+
int i;
1069+
1070+
for (i = 0; i < negotiation_tip.nr; i++) {
1071+
const char *s = negotiation_tip.items[i].string;
1072+
int old_nr;
1073+
if (!has_glob_specials(s)) {
1074+
struct object_id oid;
1075+
if (get_oid(s, &oid))
1076+
die("%s is not a valid object", s);
1077+
oid_array_append(oids, &oid);
1078+
continue;
1079+
}
1080+
old_nr = oids->nr;
1081+
for_each_glob_ref(add_oid, s, oids);
1082+
if (old_nr == oids->nr)
1083+
warning("Ignoring --negotiation-tip=%s because it does not match any refs",
1084+
s);
1085+
}
1086+
smart_options->negotiation_tips = oids;
1087+
}
1088+
10521089
static struct transport *prepare_transport(struct remote *remote, int deepen)
10531090
{
10541091
struct transport *transport;
@@ -1075,6 +1112,12 @@ static struct transport *prepare_transport(struct remote *remote, int deepen)
10751112
filter_options.filter_spec);
10761113
set_option(transport, TRANS_OPT_FROM_PROMISOR, "1");
10771114
}
1115+
if (negotiation_tip.nr) {
1116+
if (transport->smart_options)
1117+
add_negotiation_tips(transport->smart_options);
1118+
else
1119+
warning("Ignoring --negotiation-tip because the protocol does not support it.");
1120+
}
10781121
return transport;
10791122
}
10801123

fetch-pack.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,22 @@ static int next_flush(int stateless_rpc, int count)
214214
return count;
215215
}
216216

217+
static void mark_tips(struct fetch_negotiator *negotiator,
218+
const struct oid_array *negotiation_tips)
219+
{
220+
int i;
221+
222+
if (!negotiation_tips) {
223+
for_each_ref(rev_list_insert_ref_oid, negotiator);
224+
return;
225+
}
226+
227+
for (i = 0; i < negotiation_tips->nr; i++)
228+
rev_list_insert_ref(negotiator, NULL,
229+
&negotiation_tips->oid[i]);
230+
return;
231+
}
232+
217233
static int find_common(struct fetch_negotiator *negotiator,
218234
struct fetch_pack_args *args,
219235
int fd[2], struct object_id *result_oid,
@@ -231,7 +247,7 @@ static int find_common(struct fetch_negotiator *negotiator,
231247
if (args->stateless_rpc && multi_ack == 1)
232248
die(_("--stateless-rpc requires multi_ack_detailed"));
233249

234-
for_each_ref(rev_list_insert_ref_oid, negotiator);
250+
mark_tips(negotiator, args->negotiation_tips);
235251
for_each_cached_alternate(negotiator, insert_one_alternate_object);
236252

237253
fetching = 0;
@@ -1296,7 +1312,7 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
12961312
else
12971313
state = FETCH_SEND_REQUEST;
12981314

1299-
for_each_ref(rev_list_insert_ref_oid, &negotiator);
1315+
mark_tips(&negotiator, args->negotiation_tips);
13001316
for_each_cached_alternate(&negotiator,
13011317
insert_one_alternate_object);
13021318
break;

fetch-pack.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ struct fetch_pack_args {
1616
const struct string_list *deepen_not;
1717
struct list_objects_filter_options filter_options;
1818
const struct string_list *server_options;
19+
20+
/*
21+
* If not NULL, during packfile negotiation, fetch-pack will send "have"
22+
* lines only with these tips and their ancestors.
23+
*/
24+
const struct oid_array *negotiation_tips;
25+
1926
unsigned deepen_relative:1;
2027
unsigned quiet:1;
2128
unsigned keep_pack:1;

t/t5510-fetch.sh

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,4 +865,82 @@ test_expect_success C_LOCALE_OUTPUT 'fetch compact output' '
865865
test_cmp expect actual
866866
'
867867

868+
setup_negotiation_tip () {
869+
SERVER="$1"
870+
URL="$2"
871+
USE_PROTOCOL_V2="$3"
872+
873+
rm -rf "$SERVER" client trace &&
874+
git init "$SERVER" &&
875+
test_commit -C "$SERVER" alpha_1 &&
876+
test_commit -C "$SERVER" alpha_2 &&
877+
git -C "$SERVER" checkout --orphan beta &&
878+
test_commit -C "$SERVER" beta_1 &&
879+
test_commit -C "$SERVER" beta_2 &&
880+
881+
git clone "$URL" client &&
882+
883+
if test "$USE_PROTOCOL_V2" -eq 1
884+
then
885+
git -C "$SERVER" config protocol.version 2 &&
886+
git -C client config protocol.version 2
887+
fi &&
888+
889+
test_commit -C "$SERVER" beta_s &&
890+
git -C "$SERVER" checkout master &&
891+
test_commit -C "$SERVER" alpha_s &&
892+
git -C "$SERVER" tag -d alpha_1 alpha_2 beta_1 beta_2
893+
}
894+
895+
check_negotiation_tip () {
896+
# Ensure that {alpha,beta}_1 are sent as "have", but not {alpha_beta}_2
897+
ALPHA_1=$(git -C client rev-parse alpha_1) &&
898+
grep "fetch> have $ALPHA_1" trace &&
899+
BETA_1=$(git -C client rev-parse beta_1) &&
900+
grep "fetch> have $BETA_1" trace &&
901+
ALPHA_2=$(git -C client rev-parse alpha_2) &&
902+
! grep "fetch> have $ALPHA_2" trace &&
903+
BETA_2=$(git -C client rev-parse beta_2) &&
904+
! grep "fetch> have $BETA_2" trace
905+
}
906+
907+
test_expect_success '--negotiation-tip limits "have" lines sent' '
908+
setup_negotiation_tip server server 0 &&
909+
GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
910+
--negotiation-tip=alpha_1 --negotiation-tip=beta_1 \
911+
origin alpha_s beta_s &&
912+
check_negotiation_tip
913+
'
914+
915+
test_expect_success '--negotiation-tip understands globs' '
916+
setup_negotiation_tip server server 0 &&
917+
GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
918+
--negotiation-tip=*_1 \
919+
origin alpha_s beta_s &&
920+
check_negotiation_tip
921+
'
922+
923+
test_expect_success '--negotiation-tip understands abbreviated SHA-1' '
924+
setup_negotiation_tip server server 0 &&
925+
GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
926+
--negotiation-tip=$(git -C client rev-parse --short alpha_1) \
927+
--negotiation-tip=$(git -C client rev-parse --short beta_1) \
928+
origin alpha_s beta_s &&
929+
check_negotiation_tip
930+
'
931+
932+
. "$TEST_DIRECTORY"/lib-httpd.sh
933+
start_httpd
934+
935+
test_expect_success '--negotiation-tip limits "have" lines sent with HTTP protocol v2' '
936+
setup_negotiation_tip "$HTTPD_DOCUMENT_ROOT_PATH/server" \
937+
"$HTTPD_URL/smart/server" 1 &&
938+
GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
939+
--negotiation-tip=alpha_1 --negotiation-tip=beta_1 \
940+
origin alpha_s beta_s &&
941+
check_negotiation_tip
942+
'
943+
944+
stop_httpd
945+
868946
test_done

transport-helper.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,9 @@ static int fetch(struct transport *transport,
684684
transport, "filter",
685685
data->transport_options.filter_options.filter_spec);
686686

687+
if (data->transport_options.negotiation_tips)
688+
warning("Ignoring --negotiation-tip because the protocol does not support it.");
689+
687690
if (data->fetch)
688691
return fetch_with_fetch(transport, nr_heads, to_fetch);
689692

transport.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ static int fetch_refs_via_pack(struct transport *transport,
318318
args.filter_options = data->options.filter_options;
319319
args.stateless_rpc = transport->stateless_rpc;
320320
args.server_options = transport->server_options;
321+
args.negotiation_tips = data->options.negotiation_tips;
321322

322323
if (!data->got_remote_heads)
323324
refs_tmp = get_refs_via_connect(transport, 0, NULL);

transport.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ struct git_transport_options {
2525
const char *receivepack;
2626
struct push_cas_option *cas;
2727
struct list_objects_filter_options filter_options;
28+
29+
/*
30+
* This is only used during fetch. See the documentation of
31+
* negotiation_tips in struct fetch_pack_args.
32+
*
33+
* This field is only supported by transports that support connect or
34+
* stateless_connect. Set this field directly instead of using
35+
* transport_set_option().
36+
*/
37+
struct oid_array *negotiation_tips;
2838
};
2939

3040
enum transport_family {

0 commit comments

Comments
 (0)