Skip to content

Commit 30bf8d9

Browse files
committed
Merge branch 'jt/fetch-nego-tip'
"git fetch" learned a new option "--negotiation-tip" to limit the set of commits it tells the other end as "have", to reduce wasted bandwidth and cycles, which would be helpful when the receiving repository has a lot of refs that have little to do with the history at the remote it is fetching from. * jt/fetch-nego-tip: fetch-pack: support negotiation tip whitelist
2 parents 84e74c6 + 3390e42 commit 30bf8d9

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
@@ -64,6 +64,7 @@ static int shown_url = 0;
6464
static struct refspec refmap = REFSPEC_INIT_FETCH;
6565
static struct list_objects_filter_options filter_options;
6666
static struct string_list server_options = STRING_LIST_INIT_DUP;
67+
static struct string_list negotiation_tip = STRING_LIST_INIT_NODUP;
6768

6869
static int git_fetch_config(const char *k, const char *v, void *cb)
6970
{
@@ -162,6 +163,8 @@ static struct option builtin_fetch_options[] = {
162163
TRANSPORT_FAMILY_IPV4),
163164
OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
164165
TRANSPORT_FAMILY_IPV6),
166+
OPT_STRING_LIST(0, "negotiation-tip", &negotiation_tip, N_("revision"),
167+
N_("report that we have only objects reachable from this object")),
165168
OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
166169
OPT_END()
167170
};
@@ -1060,6 +1063,40 @@ static void set_option(struct transport *transport, const char *name, const char
10601063
name, transport->url);
10611064
}
10621065

1066+
1067+
static int add_oid(const char *refname, const struct object_id *oid, int flags,
1068+
void *cb_data)
1069+
{
1070+
struct oid_array *oids = cb_data;
1071+
1072+
oid_array_append(oids, oid);
1073+
return 0;
1074+
}
1075+
1076+
static void add_negotiation_tips(struct git_transport_options *smart_options)
1077+
{
1078+
struct oid_array *oids = xcalloc(1, sizeof(*oids));
1079+
int i;
1080+
1081+
for (i = 0; i < negotiation_tip.nr; i++) {
1082+
const char *s = negotiation_tip.items[i].string;
1083+
int old_nr;
1084+
if (!has_glob_specials(s)) {
1085+
struct object_id oid;
1086+
if (get_oid(s, &oid))
1087+
die("%s is not a valid object", s);
1088+
oid_array_append(oids, &oid);
1089+
continue;
1090+
}
1091+
old_nr = oids->nr;
1092+
for_each_glob_ref(add_oid, s, oids);
1093+
if (old_nr == oids->nr)
1094+
warning("Ignoring --negotiation-tip=%s because it does not match any refs",
1095+
s);
1096+
}
1097+
smart_options->negotiation_tips = oids;
1098+
}
1099+
10631100
static struct transport *prepare_transport(struct remote *remote, int deepen)
10641101
{
10651102
struct transport *transport;
@@ -1086,6 +1123,12 @@ static struct transport *prepare_transport(struct remote *remote, int deepen)
10861123
filter_options.filter_spec);
10871124
set_option(transport, TRANS_OPT_FROM_PROMISOR, "1");
10881125
}
1126+
if (negotiation_tip.nr) {
1127+
if (transport->smart_options)
1128+
add_negotiation_tips(transport->smart_options);
1129+
else
1130+
warning("Ignoring --negotiation-tip because the protocol does not support it.");
1131+
}
10891132
return transport;
10901133
}
10911134

fetch-pack.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,22 @@ static int next_flush(int stateless_rpc, int count)
217217
return count;
218218
}
219219

220+
static void mark_tips(struct fetch_negotiator *negotiator,
221+
const struct oid_array *negotiation_tips)
222+
{
223+
int i;
224+
225+
if (!negotiation_tips) {
226+
for_each_ref(rev_list_insert_ref_oid, negotiator);
227+
return;
228+
}
229+
230+
for (i = 0; i < negotiation_tips->nr; i++)
231+
rev_list_insert_ref(negotiator, NULL,
232+
&negotiation_tips->oid[i]);
233+
return;
234+
}
235+
220236
static int find_common(struct fetch_negotiator *negotiator,
221237
struct fetch_pack_args *args,
222238
int fd[2], struct object_id *result_oid,
@@ -234,7 +250,7 @@ static int find_common(struct fetch_negotiator *negotiator,
234250
if (args->stateless_rpc && multi_ack == 1)
235251
die(_("--stateless-rpc requires multi_ack_detailed"));
236252

237-
for_each_ref(rev_list_insert_ref_oid, negotiator);
253+
mark_tips(negotiator, args->negotiation_tips);
238254
for_each_cached_alternate(negotiator, insert_one_alternate_object);
239255

240256
fetching = 0;
@@ -1332,7 +1348,7 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
13321348
else
13331349
state = FETCH_SEND_REQUEST;
13341350

1335-
for_each_ref(rev_list_insert_ref_oid, &negotiator);
1351+
mark_tips(&negotiator, args->negotiation_tips);
13361352
for_each_cached_alternate(&negotiator,
13371353
insert_one_alternate_object);
13381354
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
@@ -686,6 +686,9 @@ static int fetch(struct transport *transport,
686686
transport, "filter",
687687
data->transport_options.filter_options.filter_spec);
688688

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

transport.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ static int fetch_refs_via_pack(struct transport *transport,
320320
args.filter_options = data->options.filter_options;
321321
args.stateless_rpc = transport->stateless_rpc;
322322
args.server_options = transport->server_options;
323+
args.negotiation_tips = data->options.negotiation_tips;
323324

324325
if (!data->got_remote_heads)
325326
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
@@ -36,6 +36,16 @@ struct git_transport_options {
3636
const char *receivepack;
3737
struct push_cas_option *cas;
3838
struct list_objects_filter_options filter_options;
39+
40+
/*
41+
* This is only used during fetch. See the documentation of
42+
* negotiation_tips in struct fetch_pack_args.
43+
*
44+
* This field is only supported by transports that support connect or
45+
* stateless_connect. Set this field directly instead of using
46+
* transport_set_option().
47+
*/
48+
struct oid_array *negotiation_tips;
3949
};
4050

4151
enum transport_family {

0 commit comments

Comments
 (0)