Skip to content

Commit 3390e42

Browse files
jonathantanmygitster
authored andcommitted
fetch-pack: support negotiation tip whitelist
During negotiation, fetch-pack eventually reports as "have" lines all commits reachable from all refs. Allow the user to restrict the commits sent in this way by providing a whitelist of tips; only the tips themselves and their ancestors will be sent. Both globs and single objects are supported. This feature is only supported for protocols that support connect or stateless-connect (such as HTTP with protocol v2). This will speed up negotiation when the repository has multiple relatively independent branches (for example, when a repository interacts with multiple repositories, such as with linux-next [1] and torvalds/linux [2]), and the user knows which local branch is likely to have commits in common with the upstream branch they are fetching. [1] https://kernel.googlesource.com/pub/scm/linux/kernel/git/next/linux-next/ [2] https://kernel.googlesource.com/pub/scm/linux/kernel/git/torvalds/linux/ Signed-off-by: Jonathan Tan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ec06283 commit 3390e42

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
@@ -213,6 +213,22 @@ static int next_flush(int stateless_rpc, int count)
213213
return count;
214214
}
215215

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

233-
for_each_ref(rev_list_insert_ref_oid, negotiator);
249+
mark_tips(negotiator, args->negotiation_tips);
234250
for_each_cached_alternate(negotiator, insert_one_alternate_object);
235251

236252
fetching = 0;
@@ -1295,7 +1311,7 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
12951311
else
12961312
state = FETCH_SEND_REQUEST;
12971313

1298-
for_each_ref(rev_list_insert_ref_oid, &negotiator);
1314+
mark_tips(&negotiator, args->negotiation_tips);
12991315
for_each_cached_alternate(&negotiator,
13001316
insert_one_alternate_object);
13011317
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)