]>
Commit | Line | Data |
---|---|---|
745f7a8c | 1 | #include "cache.h" |
a49d2834 | 2 | #include "repository.h" |
b2141fc1 | 3 | #include "config.h" |
697cc8ef | 4 | #include "lockfile.h" |
745f7a8c NTND |
5 | #include "refs.h" |
6 | #include "pkt-line.h" | |
7 | #include "commit.h" | |
8 | #include "tag.h" | |
d807c4a0 | 9 | #include "exec-cmd.h" |
745f7a8c NTND |
10 | #include "pack.h" |
11 | #include "sideband.h" | |
12 | #include "fetch-pack.h" | |
13 | #include "remote.h" | |
14 | #include "run-command.h" | |
47a59185 | 15 | #include "connect.h" |
745f7a8c NTND |
16 | #include "transport.h" |
17 | #include "version.h" | |
fe299ec5 | 18 | #include "oid-array.h" |
fdb69d33 | 19 | #include "oidset.h" |
0abe14f6 | 20 | #include "packfile.h" |
cbd53a21 | 21 | #include "object-store.h" |
cf1e7c07 | 22 | #include "connected.h" |
ec062838 | 23 | #include "fetch-negotiator.h" |
1362df0d | 24 | #include "fsck.h" |
120ad2b0 | 25 | #include "shallow.h" |
9c1e657a JT |
26 | #include "commit-reach.h" |
27 | #include "commit-graph.h" | |
2a4aed42 | 28 | #include "sigchain.h" |
6fc9fec0 | 29 | #include "mergesort.h" |
745f7a8c NTND |
30 | |
31 | static int transfer_unpack_limit = -1; | |
32 | static int fetch_unpack_limit = -1; | |
33 | static int unpack_limit = 100; | |
34 | static int prefer_ofs_delta = 1; | |
35 | static int no_done; | |
508ea882 | 36 | static int deepen_since_ok; |
a45a2600 | 37 | static int deepen_not_ok; |
745f7a8c NTND |
38 | static int fetch_fsck_objects = -1; |
39 | static int transfer_fsck_objects = -1; | |
40 | static int agent_supported; | |
640d8b72 | 41 | static int server_supports_filtering; |
1e905bbc | 42 | static int advertise_sid; |
cac4b8e2 | 43 | static struct shallow_lock shallow_lock; |
6035d6aa | 44 | static const char *alternate_shallow_file; |
3745e269 | 45 | static struct fsck_options fsck_options = FSCK_OPTIONS_MISSING_GITMODULES; |
1362df0d | 46 | static struct strbuf fsck_msg_types = STRBUF_INIT; |
dd4b732d | 47 | static struct string_list uri_protocols = STRING_LIST_INIT_DUP; |
745f7a8c | 48 | |
208acbfb | 49 | /* Remember to update object flag allocation in object.h */ |
745f7a8c | 50 | #define COMPLETE (1U << 0) |
ec062838 | 51 | #define ALTERNATE (1U << 1) |
9c1e657a JT |
52 | #define COMMON (1U << 6) |
53 | #define REACH_SCRATCH (1U << 7) | |
745f7a8c NTND |
54 | |
55 | /* | |
56 | * After sending this many "have"s if we do not get any new ACK , we | |
57 | * give up traversing our history. | |
58 | */ | |
59 | #define MAX_IN_VAIN 256 | |
60 | ||
d30fe89c | 61 | static int multi_ack, use_sideband; |
7199c093 FM |
62 | /* Allow specifying sha1 if it is a ref tip. */ |
63 | #define ALLOW_TIP_SHA1 01 | |
68ee6289 FM |
64 | /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */ |
65 | #define ALLOW_REACHABLE_SHA1 02 | |
7199c093 | 66 | static unsigned int allow_unadvertised_object_request; |
745f7a8c | 67 | |
0d789a5b NTND |
68 | __attribute__((format (printf, 2, 3))) |
69 | static inline void print_verbose(const struct fetch_pack_args *args, | |
70 | const char *fmt, ...) | |
71 | { | |
72 | va_list params; | |
73 | ||
74 | if (!args->verbose) | |
75 | return; | |
76 | ||
77 | va_start(params, fmt); | |
78 | vfprintf(stderr, fmt, params); | |
79 | va_end(params); | |
80 | fputc('\n', stderr); | |
81 | } | |
82 | ||
41a078c6 JK |
83 | struct alternate_object_cache { |
84 | struct object **items; | |
85 | size_t nr, alloc; | |
86 | }; | |
87 | ||
bdf4276c | 88 | static void cache_one_alternate(const struct object_id *oid, |
41a078c6 JK |
89 | void *vcache) |
90 | { | |
91 | struct alternate_object_cache *cache = vcache; | |
109cd76d | 92 | struct object *obj = parse_object(the_repository, oid); |
41a078c6 JK |
93 | |
94 | if (!obj || (obj->flags & ALTERNATE)) | |
95 | return; | |
96 | ||
97 | obj->flags |= ALTERNATE; | |
98 | ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc); | |
99 | cache->items[cache->nr++] = obj; | |
100 | } | |
101 | ||
ec062838 JT |
102 | static void for_each_cached_alternate(struct fetch_negotiator *negotiator, |
103 | void (*cb)(struct fetch_negotiator *, | |
d30fe89c | 104 | struct object *)) |
41a078c6 JK |
105 | { |
106 | static int initialized; | |
107 | static struct alternate_object_cache cache; | |
108 | size_t i; | |
109 | ||
110 | if (!initialized) { | |
111 | for_each_alternate_ref(cache_one_alternate, &cache); | |
112 | initialized = 1; | |
113 | } | |
114 | ||
115 | for (i = 0; i < cache.nr; i++) | |
ec062838 | 116 | cb(negotiator, cache.items[i]); |
745f7a8c NTND |
117 | } |
118 | ||
a6e65fb3 ÆAB |
119 | static struct commit *deref_without_lazy_fetch_extended(const struct object_id *oid, |
120 | int mark_tags_complete, | |
121 | enum object_type *type, | |
122 | unsigned int oi_flags) | |
5c3b801d | 123 | { |
a6e65fb3 | 124 | struct object_info info = { .typep = type }; |
62b5a35a PS |
125 | struct commit *commit; |
126 | ||
127 | commit = lookup_commit_in_graph(the_repository, oid); | |
128 | if (commit) | |
129 | return commit; | |
5c3b801d JT |
130 | |
131 | while (1) { | |
132 | if (oid_object_info_extended(the_repository, oid, &info, | |
a6e65fb3 | 133 | oi_flags)) |
5c3b801d | 134 | return NULL; |
a6e65fb3 | 135 | if (*type == OBJ_TAG) { |
5c3b801d JT |
136 | struct tag *tag = (struct tag *) |
137 | parse_object(the_repository, oid); | |
138 | ||
139 | if (!tag->tagged) | |
140 | return NULL; | |
141 | if (mark_tags_complete) | |
142 | tag->object.flags |= COMPLETE; | |
143 | oid = &tag->tagged->oid; | |
144 | } else { | |
145 | break; | |
146 | } | |
147 | } | |
3e5e6c6e | 148 | |
a6e65fb3 | 149 | if (*type == OBJ_COMMIT) { |
3e5e6c6e PS |
150 | struct commit *commit = lookup_commit(the_repository, oid); |
151 | if (!commit || repo_parse_commit(the_repository, commit)) | |
152 | return NULL; | |
153 | return commit; | |
154 | } | |
155 | ||
5c3b801d JT |
156 | return NULL; |
157 | } | |
158 | ||
a6e65fb3 ÆAB |
159 | |
160 | static struct commit *deref_without_lazy_fetch(const struct object_id *oid, | |
161 | int mark_tags_complete) | |
162 | { | |
163 | enum object_type type; | |
164 | unsigned flags = OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_QUICK; | |
165 | return deref_without_lazy_fetch_extended(oid, mark_tags_complete, | |
166 | &type, flags); | |
167 | } | |
168 | ||
ec062838 | 169 | static int rev_list_insert_ref(struct fetch_negotiator *negotiator, |
d30fe89c | 170 | const struct object_id *oid) |
745f7a8c | 171 | { |
5c3b801d | 172 | struct commit *c = deref_without_lazy_fetch(oid, 0); |
745f7a8c | 173 | |
5c3b801d JT |
174 | if (c) |
175 | negotiator->add_tip(negotiator, c); | |
745f7a8c NTND |
176 | return 0; |
177 | } | |
178 | ||
5cf88fd8 | 179 | static int rev_list_insert_ref_oid(const char *refname UNUSED, |
63e14ee2 | 180 | const struct object_id *oid, |
5cf88fd8 | 181 | int flag UNUSED, |
63e14ee2 | 182 | void *cb_data) |
745f7a8c | 183 | { |
5c3b801d | 184 | return rev_list_insert_ref(cb_data, oid); |
745f7a8c NTND |
185 | } |
186 | ||
187 | enum ack_type { | |
188 | NAK = 0, | |
189 | ACK, | |
190 | ACK_continue, | |
191 | ACK_common, | |
192 | ACK_ready | |
193 | }; | |
194 | ||
01f9ec64 MS |
195 | static void consume_shallow_list(struct fetch_pack_args *args, |
196 | struct packet_reader *reader) | |
745f7a8c | 197 | { |
79891cb9 | 198 | if (args->stateless_rpc && args->deepen) { |
745f7a8c NTND |
199 | /* If we sent a depth we will get back "duplicate" |
200 | * shallow and unshallow commands every time there | |
201 | * is a block of have lines exchanged. | |
202 | */ | |
01f9ec64 MS |
203 | while (packet_reader_read(reader) == PACKET_READ_NORMAL) { |
204 | if (starts_with(reader->line, "shallow ")) | |
745f7a8c | 205 | continue; |
01f9ec64 | 206 | if (starts_with(reader->line, "unshallow ")) |
745f7a8c | 207 | continue; |
1dd73e20 | 208 | die(_("git fetch-pack: expected shallow list")); |
745f7a8c | 209 | } |
01f9ec64 MS |
210 | if (reader->status != PACKET_READ_FLUSH) |
211 | die(_("git fetch-pack: expected a flush packet after shallow list")); | |
745f7a8c NTND |
212 | } |
213 | } | |
214 | ||
01f9ec64 MS |
215 | static enum ack_type get_ack(struct packet_reader *reader, |
216 | struct object_id *result_oid) | |
745f7a8c | 217 | { |
74543a04 | 218 | int len; |
82e56767 | 219 | const char *arg; |
745f7a8c | 220 | |
01f9ec64 | 221 | if (packet_reader_read(reader) != PACKET_READ_NORMAL) |
bc9d4dc5 | 222 | die(_("git fetch-pack: expected ACK/NAK, got a flush packet")); |
01f9ec64 MS |
223 | len = reader->pktlen; |
224 | ||
225 | if (!strcmp(reader->line, "NAK")) | |
745f7a8c | 226 | return NAK; |
01f9ec64 | 227 | if (skip_prefix(reader->line, "ACK ", &arg)) { |
f6af19a9 | 228 | const char *p; |
229 | if (!parse_oid_hex(arg, result_oid, &p)) { | |
230 | len -= p - reader->line; | |
82e56767 | 231 | if (len < 1) |
030e9dd6 | 232 | return ACK; |
f6af19a9 | 233 | if (strstr(p, "continue")) |
745f7a8c | 234 | return ACK_continue; |
f6af19a9 | 235 | if (strstr(p, "common")) |
745f7a8c | 236 | return ACK_common; |
f6af19a9 | 237 | if (strstr(p, "ready")) |
745f7a8c NTND |
238 | return ACK_ready; |
239 | return ACK; | |
240 | } | |
241 | } | |
01f9ec64 | 242 | die(_("git fetch-pack: expected ACK/NAK, got '%s'"), reader->line); |
745f7a8c NTND |
243 | } |
244 | ||
245 | static void send_request(struct fetch_pack_args *args, | |
246 | int fd, struct strbuf *buf) | |
247 | { | |
248 | if (args->stateless_rpc) { | |
249 | send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX); | |
250 | packet_flush(fd); | |
37c80012 JK |
251 | } else { |
252 | if (write_in_full(fd, buf->buf, buf->len) < 0) | |
253 | die_errno(_("unable to write to remote")); | |
254 | } | |
745f7a8c NTND |
255 | } |
256 | ||
ec062838 | 257 | static void insert_one_alternate_object(struct fetch_negotiator *negotiator, |
d30fe89c | 258 | struct object *obj) |
745f7a8c | 259 | { |
5c3b801d | 260 | rev_list_insert_ref(negotiator, &obj->oid); |
745f7a8c NTND |
261 | } |
262 | ||
263 | #define INITIAL_FLUSH 16 | |
264 | #define PIPESAFE_FLUSH 32 | |
da470981 | 265 | #define LARGE_FLUSH 16384 |
745f7a8c | 266 | |
685fbd32 | 267 | static int next_flush(int stateless_rpc, int count) |
745f7a8c | 268 | { |
685fbd32 | 269 | if (stateless_rpc) { |
da470981 JT |
270 | if (count < LARGE_FLUSH) |
271 | count <<= 1; | |
272 | else | |
273 | count = count * 11 / 10; | |
274 | } else { | |
275 | if (count < PIPESAFE_FLUSH) | |
276 | count <<= 1; | |
277 | else | |
278 | count += PIPESAFE_FLUSH; | |
279 | } | |
745f7a8c NTND |
280 | return count; |
281 | } | |
282 | ||
3390e42a JT |
283 | static void mark_tips(struct fetch_negotiator *negotiator, |
284 | const struct oid_array *negotiation_tips) | |
285 | { | |
286 | int i; | |
287 | ||
288 | if (!negotiation_tips) { | |
5c3b801d | 289 | for_each_rawref(rev_list_insert_ref_oid, negotiator); |
3390e42a JT |
290 | return; |
291 | } | |
292 | ||
293 | for (i = 0; i < negotiation_tips->nr; i++) | |
5c3b801d | 294 | rev_list_insert_ref(negotiator, &negotiation_tips->oid[i]); |
3390e42a JT |
295 | return; |
296 | } | |
297 | ||
1007557a JT |
298 | static void send_filter(struct fetch_pack_args *args, |
299 | struct strbuf *req_buf, | |
300 | int server_supports_filter) | |
301 | { | |
302 | if (args->filter_options.choice) { | |
303 | const char *spec = | |
304 | expand_list_objects_filter_spec(&args->filter_options); | |
305 | if (server_supports_filter) { | |
306 | print_verbose(args, _("Server supports filter")); | |
307 | packet_buf_write(req_buf, "filter %s", spec); | |
308 | trace2_data_string("fetch", the_repository, | |
309 | "filter/effective", spec); | |
310 | } else { | |
311 | warning("filtering not recognized by server, ignoring"); | |
312 | trace2_data_string("fetch", the_repository, | |
313 | "filter/unsupported", spec); | |
314 | } | |
315 | } else { | |
316 | trace2_data_string("fetch", the_repository, | |
317 | "filter/none", ""); | |
318 | } | |
319 | } | |
320 | ||
ec062838 | 321 | static int find_common(struct fetch_negotiator *negotiator, |
d30fe89c | 322 | struct fetch_pack_args *args, |
1b283377 | 323 | int fd[2], struct object_id *result_oid, |
745f7a8c NTND |
324 | struct ref *refs) |
325 | { | |
326 | int fetching; | |
327 | int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval; | |
a29263cf | 328 | int negotiation_round = 0, haves = 0; |
1b283377 | 329 | const struct object_id *oid; |
745f7a8c NTND |
330 | unsigned in_vain = 0; |
331 | int got_continue = 0; | |
332 | int got_ready = 0; | |
333 | struct strbuf req_buf = STRBUF_INIT; | |
334 | size_t state_len = 0; | |
01f9ec64 | 335 | struct packet_reader reader; |
745f7a8c NTND |
336 | |
337 | if (args->stateless_rpc && multi_ack == 1) | |
6fa00ee8 | 338 | die(_("the option '%s' requires '%s'"), "--stateless-rpc", "multi_ack_detailed"); |
745f7a8c | 339 | |
01f9ec64 | 340 | packet_reader_init(&reader, fd[0], NULL, 0, |
2d103c31 MS |
341 | PACKET_READ_CHOMP_NEWLINE | |
342 | PACKET_READ_DIE_ON_ERR_PACKET); | |
01f9ec64 | 343 | |
9dfa8dbe JT |
344 | mark_tips(negotiator, args->negotiation_tips); |
345 | for_each_cached_alternate(negotiator, insert_one_alternate_object); | |
745f7a8c NTND |
346 | |
347 | fetching = 0; | |
348 | for ( ; refs ; refs = refs->next) { | |
1b283377 | 349 | struct object_id *remote = &refs->old_oid; |
745f7a8c NTND |
350 | const char *remote_hex; |
351 | struct object *o; | |
352 | ||
4dfd0925 RC |
353 | if (!args->refetch) { |
354 | /* | |
355 | * If that object is complete (i.e. it is an ancestor of a | |
356 | * local ref), we tell them we have it but do not have to | |
357 | * tell them about its ancestors, which they already know | |
358 | * about. | |
359 | * | |
360 | * We use lookup_object here because we are only | |
361 | * interested in the case we *know* the object is | |
362 | * reachable and we have already scanned it. | |
363 | */ | |
364 | if (((o = lookup_object(the_repository, remote)) != NULL) && | |
365 | (o->flags & COMPLETE)) { | |
366 | continue; | |
367 | } | |
745f7a8c NTND |
368 | } |
369 | ||
1b283377 | 370 | remote_hex = oid_to_hex(remote); |
745f7a8c NTND |
371 | if (!fetching) { |
372 | struct strbuf c = STRBUF_INIT; | |
373 | if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed"); | |
374 | if (multi_ack == 1) strbuf_addstr(&c, " multi_ack"); | |
375 | if (no_done) strbuf_addstr(&c, " no-done"); | |
376 | if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k"); | |
377 | if (use_sideband == 1) strbuf_addstr(&c, " side-band"); | |
cccf74e2 | 378 | if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative"); |
745f7a8c NTND |
379 | if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack"); |
380 | if (args->no_progress) strbuf_addstr(&c, " no-progress"); | |
381 | if (args->include_tag) strbuf_addstr(&c, " include-tag"); | |
382 | if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta"); | |
508ea882 | 383 | if (deepen_since_ok) strbuf_addstr(&c, " deepen-since"); |
a45a2600 | 384 | if (deepen_not_ok) strbuf_addstr(&c, " deepen-not"); |
745f7a8c NTND |
385 | if (agent_supported) strbuf_addf(&c, " agent=%s", |
386 | git_user_agent_sanitized()); | |
1e905bbc JS |
387 | if (advertise_sid) |
388 | strbuf_addf(&c, " session-id=%s", trace2_session_id()); | |
640d8b72 JH |
389 | if (args->filter_options.choice) |
390 | strbuf_addstr(&c, " filter"); | |
745f7a8c NTND |
391 | packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf); |
392 | strbuf_release(&c); | |
393 | } else | |
394 | packet_buf_write(&req_buf, "want %s\n", remote_hex); | |
395 | fetching++; | |
396 | } | |
397 | ||
398 | if (!fetching) { | |
399 | strbuf_release(&req_buf); | |
400 | packet_flush(fd[1]); | |
401 | return 1; | |
402 | } | |
403 | ||
c8813487 | 404 | if (is_repository_shallow(the_repository)) |
1a30f5a2 | 405 | write_shallow_commits(&req_buf, 1, NULL); |
745f7a8c NTND |
406 | if (args->depth > 0) |
407 | packet_buf_write(&req_buf, "deepen %d", args->depth); | |
508ea882 | 408 | if (args->deepen_since) { |
dddbad72 | 409 | timestamp_t max_age = approxidate(args->deepen_since); |
cb71f8bd | 410 | packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age); |
508ea882 | 411 | } |
a45a2600 NTND |
412 | if (args->deepen_not) { |
413 | int i; | |
414 | for (i = 0; i < args->deepen_not->nr; i++) { | |
415 | struct string_list_item *s = args->deepen_not->items + i; | |
416 | packet_buf_write(&req_buf, "deepen-not %s", s->string); | |
417 | } | |
418 | } | |
1007557a | 419 | send_filter(args, &req_buf, server_supports_filtering); |
745f7a8c NTND |
420 | packet_buf_flush(&req_buf); |
421 | state_len = req_buf.len; | |
422 | ||
79891cb9 | 423 | if (args->deepen) { |
ae021d87 | 424 | const char *arg; |
1b283377 | 425 | struct object_id oid; |
745f7a8c NTND |
426 | |
427 | send_request(args, fd[1], &req_buf); | |
01f9ec64 MS |
428 | while (packet_reader_read(&reader) == PACKET_READ_NORMAL) { |
429 | if (skip_prefix(reader.line, "shallow ", &arg)) { | |
1b283377 | 430 | if (get_oid_hex(arg, &oid)) |
01f9ec64 | 431 | die(_("invalid shallow line: %s"), reader.line); |
19143f13 | 432 | register_shallow(the_repository, &oid); |
745f7a8c NTND |
433 | continue; |
434 | } | |
01f9ec64 | 435 | if (skip_prefix(reader.line, "unshallow ", &arg)) { |
1b283377 | 436 | if (get_oid_hex(arg, &oid)) |
01f9ec64 | 437 | die(_("invalid unshallow line: %s"), reader.line); |
d0229abd | 438 | if (!lookup_object(the_repository, &oid)) |
01f9ec64 | 439 | die(_("object not found: %s"), reader.line); |
745f7a8c | 440 | /* make sure that it is parsed as shallow */ |
109cd76d | 441 | if (!parse_object(the_repository, &oid)) |
01f9ec64 | 442 | die(_("error in object: %s"), reader.line); |
e92b848c | 443 | if (unregister_shallow(&oid)) |
01f9ec64 | 444 | die(_("no shallow found: %s"), reader.line); |
745f7a8c NTND |
445 | continue; |
446 | } | |
01f9ec64 | 447 | die(_("expected shallow/unshallow, got %s"), reader.line); |
745f7a8c NTND |
448 | } |
449 | } else if (!args->stateless_rpc) | |
450 | send_request(args, fd[1], &req_buf); | |
451 | ||
452 | if (!args->stateless_rpc) { | |
453 | /* If we aren't using the stateless-rpc interface | |
454 | * we don't need to retain the headers. | |
455 | */ | |
456 | strbuf_setlen(&req_buf, 0); | |
457 | state_len = 0; | |
458 | } | |
459 | ||
5fc31180 | 460 | trace2_region_enter("fetch-pack", "negotiation_v0_v1", the_repository); |
745f7a8c NTND |
461 | flushes = 0; |
462 | retval = -1; | |
ec062838 | 463 | while ((oid = negotiator->next(negotiator))) { |
1b283377 | 464 | packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid)); |
465 | print_verbose(args, "have %s", oid_to_hex(oid)); | |
745f7a8c | 466 | in_vain++; |
a29263cf | 467 | haves++; |
745f7a8c NTND |
468 | if (flush_at <= ++count) { |
469 | int ack; | |
470 | ||
a29263cf JS |
471 | negotiation_round++; |
472 | trace2_region_enter_printf("negotiation_v0_v1", "round", | |
473 | the_repository, "%d", | |
474 | negotiation_round); | |
475 | trace2_data_intmax("negotiation_v0_v1", the_repository, | |
476 | "haves_added", haves); | |
477 | trace2_data_intmax("negotiation_v0_v1", the_repository, | |
478 | "in_vain", in_vain); | |
479 | haves = 0; | |
745f7a8c NTND |
480 | packet_buf_flush(&req_buf); |
481 | send_request(args, fd[1], &req_buf); | |
482 | strbuf_setlen(&req_buf, state_len); | |
483 | flushes++; | |
685fbd32 | 484 | flush_at = next_flush(args->stateless_rpc, count); |
745f7a8c NTND |
485 | |
486 | /* | |
487 | * We keep one window "ahead" of the other side, and | |
488 | * will wait for an ACK only on the next one | |
489 | */ | |
490 | if (!args->stateless_rpc && count == INITIAL_FLUSH) | |
491 | continue; | |
492 | ||
01f9ec64 | 493 | consume_shallow_list(args, &reader); |
745f7a8c | 494 | do { |
01f9ec64 | 495 | ack = get_ack(&reader, result_oid); |
0d789a5b | 496 | if (ack) |
1dd73e20 | 497 | print_verbose(args, _("got %s %d %s"), "ack", |
1b283377 | 498 | ack, oid_to_hex(result_oid)); |
745f7a8c NTND |
499 | switch (ack) { |
500 | case ACK: | |
a29263cf JS |
501 | trace2_region_leave_printf("negotiation_v0_v1", "round", |
502 | the_repository, "%d", | |
503 | negotiation_round); | |
745f7a8c NTND |
504 | flushes = 0; |
505 | multi_ack = 0; | |
506 | retval = 0; | |
507 | goto done; | |
508 | case ACK_common: | |
509 | case ACK_ready: | |
510 | case ACK_continue: { | |
511 | struct commit *commit = | |
c1f5eb49 SB |
512 | lookup_commit(the_repository, |
513 | result_oid); | |
d093bc75 | 514 | int was_common; |
3a2a1dc1 | 515 | |
745f7a8c | 516 | if (!commit) |
1b283377 | 517 | die(_("invalid commit %s"), oid_to_hex(result_oid)); |
ec062838 | 518 | was_common = negotiator->ack(negotiator, commit); |
745f7a8c NTND |
519 | if (args->stateless_rpc |
520 | && ack == ACK_common | |
d093bc75 | 521 | && !was_common) { |
745f7a8c NTND |
522 | /* We need to replay the have for this object |
523 | * on the next RPC request so the peer knows | |
524 | * it is in common with us. | |
525 | */ | |
1b283377 | 526 | const char *hex = oid_to_hex(result_oid); |
745f7a8c NTND |
527 | packet_buf_write(&req_buf, "have %s\n", hex); |
528 | state_len = req_buf.len; | |
a29263cf | 529 | haves++; |
06b3d386 JT |
530 | /* |
531 | * Reset in_vain because an ack | |
532 | * for this commit has not been | |
533 | * seen. | |
534 | */ | |
535 | in_vain = 0; | |
536 | } else if (!args->stateless_rpc | |
537 | || ack != ACK_common) | |
538 | in_vain = 0; | |
745f7a8c | 539 | retval = 0; |
745f7a8c | 540 | got_continue = 1; |
21bcf6e4 | 541 | if (ack == ACK_ready) |
745f7a8c | 542 | got_ready = 1; |
745f7a8c NTND |
543 | break; |
544 | } | |
545 | } | |
546 | } while (ack); | |
547 | flushes--; | |
a29263cf JS |
548 | trace2_region_leave_printf("negotiation_v0_v1", "round", |
549 | the_repository, "%d", | |
550 | negotiation_round); | |
745f7a8c | 551 | if (got_continue && MAX_IN_VAIN < in_vain) { |
1dd73e20 | 552 | print_verbose(args, _("giving up")); |
745f7a8c NTND |
553 | break; /* give up */ |
554 | } | |
21bcf6e4 JT |
555 | if (got_ready) |
556 | break; | |
745f7a8c NTND |
557 | } |
558 | } | |
559 | done: | |
5fc31180 | 560 | trace2_region_leave("fetch-pack", "negotiation_v0_v1", the_repository); |
a29263cf JS |
561 | trace2_data_intmax("negotiation_v0_v1", the_repository, "total_rounds", |
562 | negotiation_round); | |
745f7a8c NTND |
563 | if (!got_ready || !no_done) { |
564 | packet_buf_write(&req_buf, "done\n"); | |
565 | send_request(args, fd[1], &req_buf); | |
566 | } | |
1dd73e20 | 567 | print_verbose(args, _("done")); |
745f7a8c NTND |
568 | if (retval != 0) { |
569 | multi_ack = 0; | |
570 | flushes++; | |
571 | } | |
572 | strbuf_release(&req_buf); | |
573 | ||
ff62eca7 | 574 | if (!got_ready || !no_done) |
01f9ec64 | 575 | consume_shallow_list(args, &reader); |
745f7a8c | 576 | while (flushes || multi_ack) { |
01f9ec64 | 577 | int ack = get_ack(&reader, result_oid); |
745f7a8c | 578 | if (ack) { |
1dd73e20 | 579 | print_verbose(args, _("got %s (%d) %s"), "ack", |
1b283377 | 580 | ack, oid_to_hex(result_oid)); |
745f7a8c NTND |
581 | if (ack == ACK) |
582 | return 0; | |
583 | multi_ack = 1; | |
584 | continue; | |
585 | } | |
586 | flushes--; | |
587 | } | |
588 | /* it is no error to fetch into a completely empty repo */ | |
589 | return count ? retval : 0; | |
590 | } | |
591 | ||
592 | static struct commit_list *complete; | |
593 | ||
1b283377 | 594 | static int mark_complete(const struct object_id *oid) |
745f7a8c | 595 | { |
5c3b801d JT |
596 | struct commit *commit = deref_without_lazy_fetch(oid, 1); |
597 | ||
598 | if (commit && !(commit->object.flags & COMPLETE)) { | |
599 | commit->object.flags |= COMPLETE; | |
600 | commit_list_insert(commit, &complete); | |
745f7a8c NTND |
601 | } |
602 | return 0; | |
603 | } | |
604 | ||
5cf88fd8 | 605 | static int mark_complete_oid(const char *refname UNUSED, |
63e14ee2 | 606 | const struct object_id *oid, |
5cf88fd8 ÆAB |
607 | int flag UNUSED, |
608 | void *cb_data UNUSED) | |
f8ee4d85 | 609 | { |
1b283377 | 610 | return mark_complete(oid); |
f8ee4d85 MH |
611 | } |
612 | ||
745f7a8c | 613 | static void mark_recent_complete_commits(struct fetch_pack_args *args, |
dddbad72 | 614 | timestamp_t cutoff) |
745f7a8c NTND |
615 | { |
616 | while (complete && cutoff <= complete->item->date) { | |
1dd73e20 | 617 | print_verbose(args, _("Marking %s as complete"), |
0d789a5b | 618 | oid_to_hex(&complete->item->object.oid)); |
745f7a8c NTND |
619 | pop_most_recent_commit(&complete, COMPLETE); |
620 | } | |
621 | } | |
622 | ||
fdb69d33 JT |
623 | static void add_refs_to_oidset(struct oidset *oids, struct ref *refs) |
624 | { | |
625 | for (; refs; refs = refs->next) | |
626 | oidset_insert(oids, &refs->old_oid); | |
627 | } | |
628 | ||
bf73282c RS |
629 | static int is_unmatched_ref(const struct ref *ref) |
630 | { | |
631 | struct object_id oid; | |
632 | const char *p; | |
633 | return ref->match_status == REF_NOT_MATCHED && | |
634 | !parse_oid_hex(ref->name, &oid, &p) && | |
635 | *p == '\0' && | |
636 | oideq(&oid, &ref->old_oid); | |
637 | } | |
638 | ||
745f7a8c | 639 | static void filter_refs(struct fetch_pack_args *args, |
f2db854d JH |
640 | struct ref **refs, |
641 | struct ref **sought, int nr_sought) | |
745f7a8c NTND |
642 | { |
643 | struct ref *newlist = NULL; | |
644 | struct ref **newtail = &newlist; | |
fdb69d33 | 645 | struct ref *unmatched = NULL; |
745f7a8c | 646 | struct ref *ref, *next; |
fdb69d33 | 647 | struct oidset tip_oids = OIDSET_INIT; |
f2db854d | 648 | int i; |
22a16465 RS |
649 | int strict = !(allow_unadvertised_object_request & |
650 | (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1)); | |
745f7a8c | 651 | |
f2db854d | 652 | i = 0; |
745f7a8c NTND |
653 | for (ref = *refs; ref; ref = next) { |
654 | int keep = 0; | |
655 | next = ref->next; | |
f2db854d | 656 | |
50e19a83 | 657 | if (starts_with(ref->name, "refs/") && |
34066f06 JK |
658 | check_refname_format(ref->name, 0)) { |
659 | /* | |
660 | * trash or a peeled value; do not even add it to | |
661 | * unmatched list | |
662 | */ | |
663 | free_one_ref(ref); | |
664 | continue; | |
665 | } else { | |
f2db854d JH |
666 | while (i < nr_sought) { |
667 | int cmp = strcmp(ref->name, sought[i]->name); | |
745f7a8c NTND |
668 | if (cmp < 0) |
669 | break; /* definitely do not have it */ | |
670 | else if (cmp == 0) { | |
671 | keep = 1; /* definitely have it */ | |
d56583de | 672 | sought[i]->match_status = REF_MATCHED; |
745f7a8c | 673 | } |
f2db854d | 674 | i++; |
745f7a8c | 675 | } |
745f7a8c | 676 | |
e9502c0a JK |
677 | if (!keep && args->fetch_all && |
678 | (!args->deepen || !starts_with(ref->name, "refs/tags/"))) | |
679 | keep = 1; | |
680 | } | |
745f7a8c NTND |
681 | |
682 | if (keep) { | |
683 | *newtail = ref; | |
684 | ref->next = NULL; | |
685 | newtail = &ref->next; | |
686 | } else { | |
fdb69d33 JT |
687 | ref->next = unmatched; |
688 | unmatched = ref; | |
745f7a8c NTND |
689 | } |
690 | } | |
691 | ||
22a16465 RS |
692 | if (strict) { |
693 | for (i = 0; i < nr_sought; i++) { | |
694 | ref = sought[i]; | |
695 | if (!is_unmatched_ref(ref)) | |
696 | continue; | |
697 | ||
698 | add_refs_to_oidset(&tip_oids, unmatched); | |
699 | add_refs_to_oidset(&tip_oids, newlist); | |
700 | break; | |
701 | } | |
702 | } | |
703 | ||
6e7b66ee | 704 | /* Append unmatched requests to the list */ |
d56583de | 705 | for (i = 0; i < nr_sought; i++) { |
d56583de | 706 | ref = sought[i]; |
bf73282c | 707 | if (!is_unmatched_ref(ref)) |
d56583de | 708 | continue; |
6e7b66ee | 709 | |
22a16465 | 710 | if (!strict || oidset_contains(&tip_oids, &ref->old_oid)) { |
d56583de | 711 | ref->match_status = REF_MATCHED; |
c3c17bf1 JK |
712 | *newtail = copy_ref(ref); |
713 | newtail = &(*newtail)->next; | |
d56583de MM |
714 | } else { |
715 | ref->match_status = REF_UNADVERTISED_NOT_ALLOWED; | |
6e7b66ee JH |
716 | } |
717 | } | |
fdb69d33 JT |
718 | |
719 | oidset_clear(&tip_oids); | |
259eddde | 720 | free_refs(unmatched); |
fdb69d33 | 721 | |
745f7a8c NTND |
722 | *refs = newlist; |
723 | } | |
724 | ||
ec062838 | 725 | static void mark_alternate_complete(struct fetch_negotiator *unused, |
d30fe89c | 726 | struct object *obj) |
745f7a8c | 727 | { |
1b283377 | 728 | mark_complete(&obj->oid); |
745f7a8c NTND |
729 | } |
730 | ||
024aa469 TI |
731 | struct loose_object_iter { |
732 | struct oidset *loose_object_set; | |
733 | struct ref *refs; | |
734 | }; | |
735 | ||
34c29034 JT |
736 | /* |
737 | * Mark recent commits available locally and reachable from a local ref as | |
9dfa8dbe | 738 | * COMPLETE. |
34c29034 JT |
739 | * |
740 | * The cutoff time for recency is determined by this heuristic: it is the | |
741 | * earliest commit time of the objects in refs that are commits and that we know | |
742 | * the commit time of. | |
743 | */ | |
ec062838 | 744 | static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator, |
d30fe89c | 745 | struct fetch_pack_args *args, |
34c29034 | 746 | struct ref **refs) |
745f7a8c NTND |
747 | { |
748 | struct ref *ref; | |
a1c6d7c1 | 749 | int old_save_commit_buffer = save_commit_buffer; |
dddbad72 | 750 | timestamp_t cutoff = 0; |
745f7a8c | 751 | |
4dfd0925 RC |
752 | if (args->refetch) |
753 | return; | |
754 | ||
745f7a8c NTND |
755 | save_commit_buffer = 0; |
756 | ||
9e5afdf9 | 757 | trace2_region_enter("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL); |
745f7a8c | 758 | for (ref = *refs; ref; ref = ref->next) { |
6fd1cc8f PS |
759 | struct commit *commit; |
760 | ||
761 | commit = lookup_commit_in_graph(the_repository, &ref->old_oid); | |
762 | if (!commit) { | |
763 | struct object *o; | |
745f7a8c | 764 | |
6fd1cc8f | 765 | if (!has_object_file_with_flags(&ref->old_oid, |
6462d5eb | 766 | OBJECT_INFO_QUICK | |
6fd1cc8f PS |
767 | OBJECT_INFO_SKIP_FETCH_OBJECT)) |
768 | continue; | |
769 | o = parse_object(the_repository, &ref->old_oid); | |
770 | if (!o || o->type != OBJ_COMMIT) | |
771 | continue; | |
772 | ||
773 | commit = (struct commit *)o; | |
774 | } | |
745f7a8c | 775 | |
9e5afdf9 EC |
776 | /* |
777 | * We already have it -- which may mean that we were | |
745f7a8c NTND |
778 | * in sync with the other side at some time after |
779 | * that (it is OK if we guess wrong here). | |
780 | */ | |
6fd1cc8f PS |
781 | if (!cutoff || cutoff < commit->date) |
782 | cutoff = commit->date; | |
745f7a8c | 783 | } |
9e5afdf9 | 784 | trace2_region_leave("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL); |
745f7a8c | 785 | |
9e5afdf9 EC |
786 | /* |
787 | * This block marks all local refs as COMPLETE, and then recursively marks all | |
788 | * parents of those refs as COMPLETE. | |
789 | */ | |
790 | trace2_region_enter("fetch-pack", "mark_complete_local_refs", NULL); | |
12f19a98 | 791 | if (!args->deepen) { |
5c3b801d | 792 | for_each_rawref(mark_complete_oid, NULL); |
12f19a98 JT |
793 | for_each_cached_alternate(NULL, mark_alternate_complete); |
794 | commit_list_sort_by_date(&complete); | |
795 | if (cutoff) | |
796 | mark_recent_complete_commits(args, cutoff); | |
797 | } | |
9e5afdf9 | 798 | trace2_region_leave("fetch-pack", "mark_complete_local_refs", NULL); |
745f7a8c | 799 | |
12f19a98 JT |
800 | /* |
801 | * Mark all complete remote refs as common refs. | |
802 | * Don't mark them common yet; the server has to be told so first. | |
803 | */ | |
9e5afdf9 | 804 | trace2_region_enter("fetch-pack", "mark_common_remote_refs", NULL); |
12f19a98 | 805 | for (ref = *refs; ref; ref = ref->next) { |
5c3b801d | 806 | struct commit *c = deref_without_lazy_fetch(&ref->old_oid, 0); |
745f7a8c | 807 | |
5c3b801d | 808 | if (!c || !(c->object.flags & COMPLETE)) |
12f19a98 | 809 | continue; |
745f7a8c | 810 | |
5c3b801d | 811 | negotiator->known_common(negotiator, c); |
745f7a8c | 812 | } |
9e5afdf9 | 813 | trace2_region_leave("fetch-pack", "mark_common_remote_refs", NULL); |
745f7a8c | 814 | |
34c29034 JT |
815 | save_commit_buffer = old_save_commit_buffer; |
816 | } | |
817 | ||
818 | /* | |
819 | * Returns 1 if every object pointed to by the given remote refs is available | |
820 | * locally and reachable from a local ref, and 0 otherwise. | |
821 | */ | |
822 | static int everything_local(struct fetch_pack_args *args, | |
823 | struct ref **refs) | |
824 | { | |
825 | struct ref *ref; | |
826 | int retval; | |
745f7a8c NTND |
827 | |
828 | for (retval = 1, ref = *refs; ref ; ref = ref->next) { | |
1b283377 | 829 | const struct object_id *remote = &ref->old_oid; |
745f7a8c NTND |
830 | struct object *o; |
831 | ||
d0229abd | 832 | o = lookup_object(the_repository, remote); |
745f7a8c NTND |
833 | if (!o || !(o->flags & COMPLETE)) { |
834 | retval = 0; | |
1b283377 | 835 | print_verbose(args, "want %s (%s)", oid_to_hex(remote), |
0d789a5b | 836 | ref->name); |
745f7a8c NTND |
837 | continue; |
838 | } | |
1b283377 | 839 | print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote), |
0d789a5b | 840 | ref->name); |
745f7a8c | 841 | } |
a1c6d7c1 | 842 | |
745f7a8c NTND |
843 | return retval; |
844 | } | |
845 | ||
5cf88fd8 | 846 | static int sideband_demux(int in UNUSED, int out, void *data) |
745f7a8c NTND |
847 | { |
848 | int *xd = data; | |
9ff18faf | 849 | int ret; |
745f7a8c | 850 | |
9ff18faf | 851 | ret = recv_sideband("fetch-pack", xd[0], out); |
745f7a8c NTND |
852 | close(out); |
853 | return ret; | |
854 | } | |
855 | ||
9d7fa3be CC |
856 | static void create_promisor_file(const char *keep_name, |
857 | struct ref **sought, int nr_sought) | |
5374a290 JT |
858 | { |
859 | struct strbuf promisor_name = STRBUF_INIT; | |
860 | int suffix_stripped; | |
5374a290 JT |
861 | |
862 | strbuf_addstr(&promisor_name, keep_name); | |
863 | suffix_stripped = strbuf_strip_suffix(&promisor_name, ".keep"); | |
864 | if (!suffix_stripped) | |
865 | BUG("name of pack lockfile should end with .keep (was '%s')", | |
866 | keep_name); | |
867 | strbuf_addstr(&promisor_name, ".promisor"); | |
868 | ||
33add2ad | 869 | write_promisor_file(promisor_name.buf, sought, nr_sought); |
5374a290 JT |
870 | |
871 | strbuf_release(&promisor_name); | |
872 | } | |
873 | ||
5476e1ef JT |
874 | static void parse_gitmodules_oids(int fd, struct oidset *gitmodules_oids) |
875 | { | |
876 | int len = the_hash_algo->hexsz + 1; /* hash + NL */ | |
877 | ||
878 | do { | |
879 | char hex_hash[GIT_MAX_HEXSZ + 1]; | |
880 | int read_len = read_in_full(fd, hex_hash, len); | |
881 | struct object_id oid; | |
882 | const char *end; | |
883 | ||
884 | if (!read_len) | |
885 | return; | |
886 | if (read_len != len) | |
887 | die("invalid length read %d", read_len); | |
888 | if (parse_oid_hex(hex_hash, &oid, &end) || *end != '\n') | |
889 | die("invalid hash"); | |
890 | oidset_insert(gitmodules_oids, &oid); | |
891 | } while (1); | |
892 | } | |
893 | ||
1f6cf450 ÆAB |
894 | static void add_index_pack_keep_option(struct strvec *args) |
895 | { | |
896 | char hostname[HOST_NAME_MAX + 1]; | |
897 | ||
898 | if (xgethostname(hostname, sizeof(hostname))) | |
899 | xsnprintf(hostname, sizeof(hostname), "localhost"); | |
900 | strvec_pushf(args, "--keep=fetch-pack %"PRIuMAX " on %s", | |
901 | (uintmax_t)getpid(), hostname); | |
902 | } | |
903 | ||
ece9aea2 | 904 | /* |
b664e9ff JT |
905 | * If packfile URIs were provided, pass a non-NULL pointer to index_pack_args. |
906 | * The strings to pass as the --index-pack-arg arguments to http-fetch will be | |
907 | * stored there. (It must be freed by the caller.) | |
ece9aea2 | 908 | */ |
745f7a8c | 909 | static int get_pack(struct fetch_pack_args *args, |
9da69a65 | 910 | int xd[2], struct string_list *pack_lockfiles, |
b664e9ff | 911 | struct strvec *index_pack_args, |
5476e1ef JT |
912 | struct ref **sought, int nr_sought, |
913 | struct oidset *gitmodules_oids) | |
745f7a8c NTND |
914 | { |
915 | struct async demux; | |
745f7a8c | 916 | int do_keep = args->keep_pack; |
984a43b9 JK |
917 | const char *cmd_name; |
918 | struct pack_header header; | |
919 | int pass_header = 0; | |
d3180279 | 920 | struct child_process cmd = CHILD_PROCESS_INIT; |
5476e1ef | 921 | int fsck_objects = 0; |
c6807a40 | 922 | int ret; |
745f7a8c NTND |
923 | |
924 | memset(&demux, 0, sizeof(demux)); | |
925 | if (use_sideband) { | |
926 | /* xd[] is talking with upload-pack; subprocess reads from | |
927 | * xd[0], spits out band#2 to stderr, and feeds us band#1 | |
928 | * through demux->out. | |
929 | */ | |
930 | demux.proc = sideband_demux; | |
931 | demux.data = xd; | |
932 | demux.out = -1; | |
df857572 | 933 | demux.isolate_sigpipe = 1; |
745f7a8c | 934 | if (start_async(&demux)) |
1dd73e20 | 935 | die(_("fetch-pack: unable to fork off sideband demultiplexer")); |
745f7a8c NTND |
936 | } |
937 | else | |
938 | demux.out = xd[0]; | |
939 | ||
2aec3bc4 | 940 | if (!args->keep_pack && unpack_limit && !index_pack_args) { |
745f7a8c NTND |
941 | |
942 | if (read_pack_header(demux.out, &header)) | |
1dd73e20 | 943 | die(_("protocol error: bad pack header")); |
984a43b9 | 944 | pass_header = 1; |
745f7a8c NTND |
945 | if (ntohl(header.hdr_entries) < unpack_limit) |
946 | do_keep = 0; | |
947 | else | |
948 | do_keep = 1; | |
949 | } | |
950 | ||
6035d6aa | 951 | if (alternate_shallow_file) { |
ef8d7ac4 JK |
952 | strvec_push(&cmd.args, "--shallow-file"); |
953 | strvec_push(&cmd.args, alternate_shallow_file); | |
6035d6aa NTND |
954 | } |
955 | ||
5476e1ef JT |
956 | if (fetch_fsck_objects >= 0 |
957 | ? fetch_fsck_objects | |
958 | : transfer_fsck_objects >= 0 | |
959 | ? transfer_fsck_objects | |
960 | : 0) | |
961 | fsck_objects = 1; | |
962 | ||
963 | if (do_keep || args->from_promisor || index_pack_args || fsck_objects) { | |
964 | if (pack_lockfiles || fsck_objects) | |
745f7a8c | 965 | cmd.out = -1; |
984a43b9 | 966 | cmd_name = "index-pack"; |
ef8d7ac4 JK |
967 | strvec_push(&cmd.args, cmd_name); |
968 | strvec_push(&cmd.args, "--stdin"); | |
745f7a8c | 969 | if (!args->quiet && !args->no_progress) |
ef8d7ac4 | 970 | strvec_push(&cmd.args, "-v"); |
745f7a8c | 971 | if (args->use_thin_pack) |
ef8d7ac4 | 972 | strvec_push(&cmd.args, "--fix-thin"); |
1f6cf450 ÆAB |
973 | if ((do_keep || index_pack_args) && (args->lock_pack || unpack_limit)) |
974 | add_index_pack_keep_option(&cmd.args); | |
b664e9ff | 975 | if (!index_pack_args && args->check_self_contained_and_connected) |
ef8d7ac4 | 976 | strvec_push(&cmd.args, "--check-self-contained-and-connected"); |
dd4b732d JT |
977 | else |
978 | /* | |
979 | * We cannot perform any connectivity checks because | |
980 | * not all packs have been downloaded; let the caller | |
981 | * have this responsibility. | |
982 | */ | |
983 | args->check_self_contained_and_connected = 0; | |
1b03df5f JT |
984 | |
985 | if (args->from_promisor) | |
986 | /* | |
9d7fa3be | 987 | * create_promisor_file() may be called afterwards but |
1b03df5f JT |
988 | * we still need index-pack to know that this is a |
989 | * promisor pack. For example, if transfer.fsckobjects | |
990 | * is true, index-pack needs to know that .gitmodules | |
991 | * is a promisor object (so that it won't complain if | |
992 | * it is missing). | |
993 | */ | |
ef8d7ac4 | 994 | strvec_push(&cmd.args, "--promisor"); |
745f7a8c NTND |
995 | } |
996 | else { | |
984a43b9 | 997 | cmd_name = "unpack-objects"; |
ef8d7ac4 | 998 | strvec_push(&cmd.args, cmd_name); |
745f7a8c | 999 | if (args->quiet || args->no_progress) |
ef8d7ac4 | 1000 | strvec_push(&cmd.args, "-q"); |
c6807a40 | 1001 | args->check_self_contained_and_connected = 0; |
745f7a8c | 1002 | } |
984a43b9 JK |
1003 | |
1004 | if (pass_header) | |
ef8d7ac4 | 1005 | strvec_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32, |
f6d8942b | 1006 | ntohl(header.hdr_version), |
984a43b9 | 1007 | ntohl(header.hdr_entries)); |
5476e1ef | 1008 | if (fsck_objects) { |
b664e9ff | 1009 | if (args->from_promisor || index_pack_args) |
98a2ea46 JT |
1010 | /* |
1011 | * We cannot use --strict in index-pack because it | |
1012 | * checks both broken objects and links, but we only | |
1013 | * want to check for broken objects. | |
1014 | */ | |
ef8d7ac4 | 1015 | strvec_push(&cmd.args, "--fsck-objects"); |
98a2ea46 | 1016 | else |
ef8d7ac4 | 1017 | strvec_pushf(&cmd.args, "--strict%s", |
f6d8942b | 1018 | fsck_msg_types.buf); |
98a2ea46 | 1019 | } |
745f7a8c | 1020 | |
b664e9ff JT |
1021 | if (index_pack_args) { |
1022 | int i; | |
1023 | ||
1024 | for (i = 0; i < cmd.args.nr; i++) | |
1025 | strvec_push(index_pack_args, cmd.args.v[i]); | |
1026 | } | |
1027 | ||
2a4aed42 JK |
1028 | sigchain_push(SIGPIPE, SIG_IGN); |
1029 | ||
745f7a8c NTND |
1030 | cmd.in = demux.out; |
1031 | cmd.git_cmd = 1; | |
1032 | if (start_command(&cmd)) | |
1dd73e20 | 1033 | die(_("fetch-pack: unable to fork off %s"), cmd_name); |
5476e1ef JT |
1034 | if (do_keep && (pack_lockfiles || fsck_objects)) { |
1035 | int is_well_formed; | |
1036 | char *pack_lockfile = index_pack_lockfile(cmd.out, &is_well_formed); | |
1037 | ||
1038 | if (!is_well_formed) | |
1039 | die(_("fetch-pack: invalid index-pack output")); | |
6031af38 RS |
1040 | if (pack_lockfile) |
1041 | string_list_append_nodup(pack_lockfiles, pack_lockfile); | |
5476e1ef | 1042 | parse_gitmodules_oids(cmd.out, gitmodules_oids); |
745f7a8c NTND |
1043 | close(cmd.out); |
1044 | } | |
1045 | ||
37cb1dd6 JL |
1046 | if (!use_sideband) |
1047 | /* Closed by start_command() */ | |
1048 | xd[0] = -1; | |
1049 | ||
c6807a40 NTND |
1050 | ret = finish_command(&cmd); |
1051 | if (!ret || (args->check_self_contained_and_connected && ret == 1)) | |
1052 | args->self_contained_and_connected = | |
1053 | args->check_self_contained_and_connected && | |
1054 | ret == 0; | |
1055 | else | |
1dd73e20 | 1056 | die(_("%s failed"), cmd_name); |
745f7a8c | 1057 | if (use_sideband && finish_async(&demux)) |
1dd73e20 | 1058 | die(_("error in sideband demultiplexer")); |
5374a290 | 1059 | |
2a4aed42 JK |
1060 | sigchain_pop(SIGPIPE); |
1061 | ||
5374a290 JT |
1062 | /* |
1063 | * Now that index-pack has succeeded, write the promisor file using the | |
1064 | * obtained .keep filename if necessary | |
1065 | */ | |
9da69a65 | 1066 | if (do_keep && pack_lockfiles && pack_lockfiles->nr && args->from_promisor) |
9d7fa3be | 1067 | create_promisor_file(pack_lockfiles->items[0].string, sought, nr_sought); |
5374a290 | 1068 | |
745f7a8c NTND |
1069 | return 0; |
1070 | } | |
1071 | ||
6fc9fec0 RS |
1072 | static int ref_compare_name(const struct ref *a, const struct ref *b) |
1073 | { | |
1074 | return strcmp(a->name, b->name); | |
1075 | } | |
1076 | ||
1077 | DEFINE_LIST_SORT(static, sort_ref_list, struct ref, next); | |
1078 | ||
f2db854d JH |
1079 | static int cmp_ref_by_name(const void *a_, const void *b_) |
1080 | { | |
1081 | const struct ref *a = *((const struct ref **)a_); | |
1082 | const struct ref *b = *((const struct ref **)b_); | |
1083 | return strcmp(a->name, b->name); | |
1084 | } | |
1085 | ||
745f7a8c NTND |
1086 | static struct ref *do_fetch_pack(struct fetch_pack_args *args, |
1087 | int fd[2], | |
1088 | const struct ref *orig_ref, | |
f2db854d | 1089 | struct ref **sought, int nr_sought, |
beea4152 | 1090 | struct shallow_info *si, |
9da69a65 | 1091 | struct string_list *pack_lockfiles) |
745f7a8c | 1092 | { |
aaf633c2 | 1093 | struct repository *r = the_repository; |
745f7a8c | 1094 | struct ref *ref = copy_ref_list(orig_ref); |
1b283377 | 1095 | struct object_id oid; |
745f7a8c NTND |
1096 | const char *agent_feature; |
1097 | int agent_len; | |
603960b5 JT |
1098 | struct fetch_negotiator negotiator_alloc; |
1099 | struct fetch_negotiator *negotiator; | |
1100 | ||
9dfa8dbe | 1101 | negotiator = &negotiator_alloc; |
4dfd0925 RC |
1102 | if (args->refetch) { |
1103 | fetch_negotiator_init_noop(negotiator); | |
1104 | } else { | |
1105 | fetch_negotiator_init(r, negotiator); | |
1106 | } | |
745f7a8c NTND |
1107 | |
1108 | sort_ref_list(&ref, ref_compare_name); | |
9ed0d8d6 | 1109 | QSORT(sought, nr_sought, cmp_ref_by_name); |
745f7a8c | 1110 | |
0e042971 NTND |
1111 | if ((agent_feature = server_feature_value("agent", &agent_len))) { |
1112 | agent_supported = 1; | |
1113 | if (agent_len) | |
1114 | print_verbose(args, _("Server version is %.*s"), | |
1115 | agent_len, agent_feature); | |
1116 | } | |
1117 | ||
1e905bbc JS |
1118 | if (!server_supports("session-id")) |
1119 | advertise_sid = 0; | |
1120 | ||
5a88583b NTND |
1121 | if (server_supports("shallow")) |
1122 | print_verbose(args, _("Server supports %s"), "shallow"); | |
aaf633c2 | 1123 | else if (args->depth > 0 || is_repository_shallow(r)) |
1dd73e20 | 1124 | die(_("Server does not support shallow clients")); |
a45a2600 | 1125 | if (args->depth > 0 || args->deepen_since || args->deepen_not) |
79891cb9 | 1126 | args->deepen = 1; |
745f7a8c | 1127 | if (server_supports("multi_ack_detailed")) { |
0778b293 | 1128 | print_verbose(args, _("Server supports %s"), "multi_ack_detailed"); |
745f7a8c NTND |
1129 | multi_ack = 2; |
1130 | if (server_supports("no-done")) { | |
0778b293 | 1131 | print_verbose(args, _("Server supports %s"), "no-done"); |
745f7a8c NTND |
1132 | if (args->stateless_rpc) |
1133 | no_done = 1; | |
1134 | } | |
1135 | } | |
1136 | else if (server_supports("multi_ack")) { | |
0778b293 | 1137 | print_verbose(args, _("Server supports %s"), "multi_ack"); |
745f7a8c NTND |
1138 | multi_ack = 1; |
1139 | } | |
1140 | if (server_supports("side-band-64k")) { | |
0778b293 | 1141 | print_verbose(args, _("Server supports %s"), "side-band-64k"); |
745f7a8c NTND |
1142 | use_sideband = 2; |
1143 | } | |
1144 | else if (server_supports("side-band")) { | |
0778b293 | 1145 | print_verbose(args, _("Server supports %s"), "side-band"); |
745f7a8c NTND |
1146 | use_sideband = 1; |
1147 | } | |
6e7b66ee | 1148 | if (server_supports("allow-tip-sha1-in-want")) { |
0778b293 | 1149 | print_verbose(args, _("Server supports %s"), "allow-tip-sha1-in-want"); |
7199c093 | 1150 | allow_unadvertised_object_request |= ALLOW_TIP_SHA1; |
6e7b66ee | 1151 | } |
68ee6289 | 1152 | if (server_supports("allow-reachable-sha1-in-want")) { |
0778b293 | 1153 | print_verbose(args, _("Server supports %s"), "allow-reachable-sha1-in-want"); |
68ee6289 FM |
1154 | allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1; |
1155 | } | |
5a88583b NTND |
1156 | if (server_supports("thin-pack")) |
1157 | print_verbose(args, _("Server supports %s"), "thin-pack"); | |
1158 | else | |
745f7a8c | 1159 | args->use_thin_pack = 0; |
5a88583b NTND |
1160 | if (server_supports("no-progress")) |
1161 | print_verbose(args, _("Server supports %s"), "no-progress"); | |
1162 | else | |
745f7a8c | 1163 | args->no_progress = 0; |
5a88583b NTND |
1164 | if (server_supports("include-tag")) |
1165 | print_verbose(args, _("Server supports %s"), "include-tag"); | |
1166 | else | |
745f7a8c | 1167 | args->include_tag = 0; |
0d789a5b | 1168 | if (server_supports("ofs-delta")) |
0778b293 | 1169 | print_verbose(args, _("Server supports %s"), "ofs-delta"); |
0d789a5b | 1170 | else |
745f7a8c NTND |
1171 | prefer_ofs_delta = 0; |
1172 | ||
640d8b72 JH |
1173 | if (server_supports("filter")) { |
1174 | server_supports_filtering = 1; | |
0778b293 | 1175 | print_verbose(args, _("Server supports %s"), "filter"); |
640d8b72 JH |
1176 | } else if (args->filter_options.choice) { |
1177 | warning("filtering not recognized by server, ignoring"); | |
1178 | } | |
1179 | ||
5a88583b NTND |
1180 | if (server_supports("deepen-since")) { |
1181 | print_verbose(args, _("Server supports %s"), "deepen-since"); | |
508ea882 | 1182 | deepen_since_ok = 1; |
5a88583b | 1183 | } else if (args->deepen_since) |
508ea882 | 1184 | die(_("Server does not support --shallow-since")); |
5a88583b NTND |
1185 | if (server_supports("deepen-not")) { |
1186 | print_verbose(args, _("Server supports %s"), "deepen-not"); | |
a45a2600 | 1187 | deepen_not_ok = 1; |
5a88583b | 1188 | } else if (args->deepen_not) |
a45a2600 | 1189 | die(_("Server does not support --shallow-exclude")); |
5a88583b NTND |
1190 | if (server_supports("deepen-relative")) |
1191 | print_verbose(args, _("Server supports %s"), "deepen-relative"); | |
1192 | else if (args->deepen_relative) | |
cccf74e2 | 1193 | die(_("Server does not support --deepen")); |
48bf1415 | 1194 | if (!server_supports_hash(the_hash_algo->name, NULL)) |
1195 | die(_("Server does not support this repository's object format")); | |
745f7a8c | 1196 | |
9dfa8dbe JT |
1197 | mark_complete_and_common_ref(negotiator, args, &ref); |
1198 | filter_refs(args, &ref, sought, nr_sought); | |
4dfd0925 | 1199 | if (!args->refetch && everything_local(args, &ref)) { |
9dfa8dbe JT |
1200 | packet_flush(fd[1]); |
1201 | goto all_done; | |
745f7a8c | 1202 | } |
603960b5 | 1203 | if (find_common(negotiator, args, fd, &oid, ref) < 0) |
745f7a8c NTND |
1204 | if (!args->keep_pack) |
1205 | /* When cloning, it is not unusual to have | |
1206 | * no common commit. | |
1207 | */ | |
1dd73e20 | 1208 | warning(_("no common commits")); |
745f7a8c NTND |
1209 | |
1210 | if (args->stateless_rpc) | |
1211 | packet_flush(fd[1]); | |
79891cb9 | 1212 | if (args->deepen) |
1a30f5a2 NTND |
1213 | setup_alternate_shallow(&shallow_lock, &alternate_shallow_file, |
1214 | NULL); | |
4fe788b1 LL |
1215 | else if (si->nr_ours || si->nr_theirs) { |
1216 | if (args->reject_shallow_remote) | |
1217 | die(_("source repository is shallow, reject to clone.")); | |
beea4152 | 1218 | alternate_shallow_file = setup_temporary_shallow(si->shallow); |
4fe788b1 | 1219 | } else |
6da8bdcb | 1220 | alternate_shallow_file = NULL; |
5476e1ef | 1221 | if (get_pack(args, fd, pack_lockfiles, NULL, sought, nr_sought, |
3745e269 | 1222 | &fsck_options.gitmodules_found)) |
1dd73e20 | 1223 | die(_("git fetch-pack: fetch failed.")); |
3745e269 ÆAB |
1224 | if (fsck_finish(&fsck_options)) |
1225 | die("fsck failed"); | |
745f7a8c NTND |
1226 | |
1227 | all_done: | |
603960b5 JT |
1228 | if (negotiator) |
1229 | negotiator->release(negotiator); | |
745f7a8c NTND |
1230 | return ref; |
1231 | } | |
1232 | ||
f7e20501 BW |
1233 | static void add_shallow_requests(struct strbuf *req_buf, |
1234 | const struct fetch_pack_args *args) | |
1235 | { | |
00624d60 | 1236 | if (is_repository_shallow(the_repository)) |
f7e20501 BW |
1237 | write_shallow_commits(req_buf, 1, NULL); |
1238 | if (args->depth > 0) | |
1239 | packet_buf_write(req_buf, "deepen %d", args->depth); | |
1240 | if (args->deepen_since) { | |
1241 | timestamp_t max_age = approxidate(args->deepen_since); | |
1242 | packet_buf_write(req_buf, "deepen-since %"PRItime, max_age); | |
1243 | } | |
1244 | if (args->deepen_not) { | |
1245 | int i; | |
1246 | for (i = 0; i < args->deepen_not->nr; i++) { | |
1247 | struct string_list_item *s = args->deepen_not->items + i; | |
1248 | packet_buf_write(req_buf, "deepen-not %s", s->string); | |
1249 | } | |
1250 | } | |
5056cf4a JT |
1251 | if (args->deepen_relative) |
1252 | packet_buf_write(req_buf, "deepen-relative\n"); | |
f7e20501 BW |
1253 | } |
1254 | ||
9dfa8dbe | 1255 | static void add_wants(const struct ref *wants, struct strbuf *req_buf) |
685fbd32 | 1256 | { |
73302051 BW |
1257 | int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0); |
1258 | ||
685fbd32 BW |
1259 | for ( ; wants ; wants = wants->next) { |
1260 | const struct object_id *remote = &wants->old_oid; | |
685fbd32 BW |
1261 | struct object *o; |
1262 | ||
1263 | /* | |
1264 | * If that object is complete (i.e. it is an ancestor of a | |
1265 | * local ref), we tell them we have it but do not have to | |
1266 | * tell them about its ancestors, which they already know | |
1267 | * about. | |
1268 | * | |
1269 | * We use lookup_object here because we are only | |
1270 | * interested in the case we *know* the object is | |
1271 | * reachable and we have already scanned it. | |
1272 | */ | |
9dfa8dbe | 1273 | if (((o = lookup_object(the_repository, remote)) != NULL) && |
685fbd32 BW |
1274 | (o->flags & COMPLETE)) { |
1275 | continue; | |
1276 | } | |
1277 | ||
73302051 BW |
1278 | if (!use_ref_in_want || wants->exact_oid) |
1279 | packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote)); | |
1280 | else | |
1281 | packet_buf_write(req_buf, "want-ref %s\n", wants->name); | |
685fbd32 BW |
1282 | } |
1283 | } | |
1284 | ||
1285 | static void add_common(struct strbuf *req_buf, struct oidset *common) | |
1286 | { | |
1287 | struct oidset_iter iter; | |
1288 | const struct object_id *oid; | |
1289 | oidset_iter_init(common, &iter); | |
1290 | ||
1291 | while ((oid = oidset_iter_next(&iter))) { | |
1292 | packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid)); | |
1293 | } | |
1294 | } | |
1295 | ||
ec062838 JT |
1296 | static int add_haves(struct fetch_negotiator *negotiator, |
1297 | struct strbuf *req_buf, | |
57c3451b | 1298 | int *haves_to_send) |
685fbd32 | 1299 | { |
685fbd32 BW |
1300 | int haves_added = 0; |
1301 | const struct object_id *oid; | |
1302 | ||
ec062838 | 1303 | while ((oid = negotiator->next(negotiator))) { |
685fbd32 BW |
1304 | packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid)); |
1305 | if (++haves_added >= *haves_to_send) | |
1306 | break; | |
1307 | } | |
1308 | ||
685fbd32 BW |
1309 | /* Increase haves to send on next round */ |
1310 | *haves_to_send = next_flush(1, *haves_to_send); | |
1311 | ||
57c3451b | 1312 | return haves_added; |
685fbd32 BW |
1313 | } |
1314 | ||
6871d0ce JT |
1315 | static void write_fetch_command_and_capabilities(struct strbuf *req_buf, |
1316 | const struct string_list *server_options) | |
685fbd32 | 1317 | { |
4b831208 | 1318 | const char *hash_name; |
685fbd32 BW |
1319 | |
1320 | if (server_supports_v2("fetch", 1)) | |
6871d0ce | 1321 | packet_buf_write(req_buf, "command=fetch"); |
685fbd32 | 1322 | if (server_supports_v2("agent", 0)) |
6871d0ce | 1323 | packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized()); |
1e905bbc | 1324 | if (advertise_sid && server_supports_v2("session-id", 0)) |
6871d0ce JT |
1325 | packet_buf_write(req_buf, "session-id=%s", trace2_session_id()); |
1326 | if (server_options && server_options->nr && | |
5e3548ef BW |
1327 | server_supports_v2("server-option", 1)) { |
1328 | int i; | |
6871d0ce JT |
1329 | for (i = 0; i < server_options->nr; i++) |
1330 | packet_buf_write(req_buf, "server-option=%s", | |
1331 | server_options->items[i].string); | |
5e3548ef | 1332 | } |
685fbd32 | 1333 | |
4b831208 | 1334 | if (server_feature_v2("object-format", &hash_name)) { |
1335 | int hash_algo = hash_algo_by_name(hash_name); | |
1336 | if (hash_algo_by_ptr(the_hash_algo) != hash_algo) | |
1337 | die(_("mismatched algorithms: client %s; server %s"), | |
1338 | the_hash_algo->name, hash_name); | |
6871d0ce | 1339 | packet_buf_write(req_buf, "object-format=%s", the_hash_algo->name); |
4b831208 | 1340 | } else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1) { |
1341 | die(_("the server does not support algorithm '%s'"), | |
1342 | the_hash_algo->name); | |
1343 | } | |
6871d0ce JT |
1344 | packet_buf_delim(req_buf); |
1345 | } | |
1346 | ||
1347 | static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out, | |
1348 | struct fetch_pack_args *args, | |
1349 | const struct ref *wants, struct oidset *common, | |
1350 | int *haves_to_send, int *in_vain, | |
1351 | int sideband_all, int seen_ack) | |
1352 | { | |
1353 | int haves_added; | |
1354 | int done_sent = 0; | |
1355 | struct strbuf req_buf = STRBUF_INIT; | |
1356 | ||
1357 | write_fetch_command_and_capabilities(&req_buf, args->server_options); | |
4b831208 | 1358 | |
685fbd32 BW |
1359 | if (args->use_thin_pack) |
1360 | packet_buf_write(&req_buf, "thin-pack"); | |
1361 | if (args->no_progress) | |
1362 | packet_buf_write(&req_buf, "no-progress"); | |
1363 | if (args->include_tag) | |
1364 | packet_buf_write(&req_buf, "include-tag"); | |
1365 | if (prefer_ofs_delta) | |
1366 | packet_buf_write(&req_buf, "ofs-delta"); | |
0bbc0bc5 JT |
1367 | if (sideband_all) |
1368 | packet_buf_write(&req_buf, "sideband-all"); | |
685fbd32 | 1369 | |
f7e20501 BW |
1370 | /* Add shallow-info and deepen request */ |
1371 | if (server_supports_feature("fetch", "shallow", 0)) | |
1372 | add_shallow_requests(&req_buf, args); | |
00624d60 | 1373 | else if (is_repository_shallow(the_repository) || args->deepen) |
f7e20501 BW |
1374 | die(_("Server does not support shallow requests")); |
1375 | ||
ba95710a | 1376 | /* Add filter */ |
1007557a JT |
1377 | send_filter(args, &req_buf, |
1378 | server_supports_feature("fetch", "filter", 0)); | |
ba95710a | 1379 | |
dd4b732d JT |
1380 | if (server_supports_feature("fetch", "packfile-uris", 0)) { |
1381 | int i; | |
1382 | struct strbuf to_send = STRBUF_INIT; | |
1383 | ||
1384 | for (i = 0; i < uri_protocols.nr; i++) { | |
1385 | const char *s = uri_protocols.items[i].string; | |
1386 | ||
1387 | if (!strcmp(s, "https") || !strcmp(s, "http")) { | |
1388 | if (to_send.len) | |
1389 | strbuf_addch(&to_send, ','); | |
1390 | strbuf_addstr(&to_send, s); | |
1391 | } | |
1392 | } | |
1393 | if (to_send.len) { | |
1394 | packet_buf_write(&req_buf, "packfile-uris %s", | |
1395 | to_send.buf); | |
1396 | strbuf_release(&to_send); | |
1397 | } | |
1398 | } | |
1399 | ||
685fbd32 | 1400 | /* add wants */ |
9dfa8dbe | 1401 | add_wants(wants, &req_buf); |
685fbd32 | 1402 | |
9dfa8dbe JT |
1403 | /* Add all of the common commits we've found in previous rounds */ |
1404 | add_common(&req_buf, common); | |
685fbd32 | 1405 | |
57c3451b JT |
1406 | haves_added = add_haves(negotiator, &req_buf, haves_to_send); |
1407 | *in_vain += haves_added; | |
a29263cf JS |
1408 | trace2_data_intmax("negotiation_v2", the_repository, "haves_added", haves_added); |
1409 | trace2_data_intmax("negotiation_v2", the_repository, "in_vain", *in_vain); | |
57c3451b JT |
1410 | if (!haves_added || (seen_ack && *in_vain >= MAX_IN_VAIN)) { |
1411 | /* Send Done */ | |
1412 | packet_buf_write(&req_buf, "done\n"); | |
1413 | done_sent = 1; | |
1414 | } | |
685fbd32 BW |
1415 | |
1416 | /* Send request */ | |
1417 | packet_buf_flush(&req_buf); | |
37c80012 JK |
1418 | if (write_in_full(fd_out, req_buf.buf, req_buf.len) < 0) |
1419 | die_errno(_("unable to write request to remote")); | |
685fbd32 BW |
1420 | |
1421 | strbuf_release(&req_buf); | |
57c3451b | 1422 | return done_sent; |
685fbd32 BW |
1423 | } |
1424 | ||
1425 | /* | |
1426 | * Processes a section header in a server's response and checks if it matches | |
1427 | * `section`. If the value of `peek` is 1, the header line will be peeked (and | |
1428 | * not consumed); if 0, the line will be consumed and the function will die if | |
1429 | * the section header doesn't match what was expected. | |
1430 | */ | |
1431 | static int process_section_header(struct packet_reader *reader, | |
1432 | const char *section, int peek) | |
1433 | { | |
7709acf7 | 1434 | int ret = 0; |
685fbd32 | 1435 | |
7709acf7 JT |
1436 | if (packet_reader_peek(reader) == PACKET_READ_NORMAL && |
1437 | !strcmp(reader->line, section)) | |
1438 | ret = 1; | |
685fbd32 BW |
1439 | |
1440 | if (!peek) { | |
7709acf7 JT |
1441 | if (!ret) { |
1442 | if (reader->line) | |
1443 | die(_("expected '%s', received '%s'"), | |
1444 | section, reader->line); | |
1445 | else | |
1446 | die(_("expected '%s'"), section); | |
1447 | } | |
685fbd32 BW |
1448 | packet_reader_read(reader); |
1449 | } | |
1450 | ||
1451 | return ret; | |
1452 | } | |
1453 | ||
81025703 JT |
1454 | static int process_ack(struct fetch_negotiator *negotiator, |
1455 | struct packet_reader *reader, | |
1456 | struct object_id *common_oid, | |
1457 | int *received_ready) | |
685fbd32 | 1458 | { |
685fbd32 BW |
1459 | while (packet_reader_read(reader) == PACKET_READ_NORMAL) { |
1460 | const char *arg; | |
1461 | ||
1462 | if (!strcmp(reader->line, "NAK")) | |
1463 | continue; | |
1464 | ||
1465 | if (skip_prefix(reader->line, "ACK ", &arg)) { | |
81025703 | 1466 | if (!get_oid_hex(arg, common_oid)) { |
685fbd32 | 1467 | struct commit *commit; |
81025703 | 1468 | commit = lookup_commit(the_repository, common_oid); |
603960b5 JT |
1469 | if (negotiator) |
1470 | negotiator->ack(negotiator, commit); | |
685fbd32 | 1471 | } |
81025703 | 1472 | return 1; |
685fbd32 BW |
1473 | } |
1474 | ||
1475 | if (!strcmp(reader->line, "ready")) { | |
81025703 | 1476 | *received_ready = 1; |
685fbd32 BW |
1477 | continue; |
1478 | } | |
1479 | ||
bbb19a8b | 1480 | die(_("unexpected acknowledgment line: '%s'"), reader->line); |
685fbd32 BW |
1481 | } |
1482 | ||
1483 | if (reader->status != PACKET_READ_FLUSH && | |
1484 | reader->status != PACKET_READ_DELIM) | |
bbb19a8b | 1485 | die(_("error processing acks: %d"), reader->status); |
685fbd32 | 1486 | |
5400b2a2 JT |
1487 | /* |
1488 | * If an "acknowledgments" section is sent, a packfile is sent if and | |
1489 | * only if "ready" was sent in this section. The other sections | |
1490 | * ("shallow-info" and "wanted-refs") are sent only if a packfile is | |
1491 | * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH | |
1492 | * otherwise. | |
1493 | */ | |
81025703 | 1494 | if (*received_ready && reader->status != PACKET_READ_DELIM) |
3d3c23b3 BS |
1495 | /* |
1496 | * TRANSLATORS: The parameter will be 'ready', a protocol | |
1497 | * keyword. | |
1498 | */ | |
1499 | die(_("expected packfile to be sent after '%s'"), "ready"); | |
81025703 | 1500 | if (!*received_ready && reader->status != PACKET_READ_FLUSH) |
3d3c23b3 BS |
1501 | /* |
1502 | * TRANSLATORS: The parameter will be 'ready', a protocol | |
1503 | * keyword. | |
1504 | */ | |
1505 | die(_("expected no other sections to be sent after no '%s'"), "ready"); | |
5400b2a2 | 1506 | |
81025703 | 1507 | return 0; |
685fbd32 BW |
1508 | } |
1509 | ||
f7e20501 | 1510 | static void receive_shallow_info(struct fetch_pack_args *args, |
1339078f JT |
1511 | struct packet_reader *reader, |
1512 | struct oid_array *shallows, | |
1513 | struct shallow_info *si) | |
f7e20501 | 1514 | { |
1339078f | 1515 | int unshallow_received = 0; |
bd0b42ae | 1516 | |
f7e20501 BW |
1517 | process_section_header(reader, "shallow-info", 0); |
1518 | while (packet_reader_read(reader) == PACKET_READ_NORMAL) { | |
1519 | const char *arg; | |
1520 | struct object_id oid; | |
1521 | ||
1522 | if (skip_prefix(reader->line, "shallow ", &arg)) { | |
1523 | if (get_oid_hex(arg, &oid)) | |
1524 | die(_("invalid shallow line: %s"), reader->line); | |
1339078f | 1525 | oid_array_append(shallows, &oid); |
f7e20501 BW |
1526 | continue; |
1527 | } | |
1528 | if (skip_prefix(reader->line, "unshallow ", &arg)) { | |
1529 | if (get_oid_hex(arg, &oid)) | |
1530 | die(_("invalid unshallow line: %s"), reader->line); | |
d0229abd | 1531 | if (!lookup_object(the_repository, &oid)) |
f7e20501 BW |
1532 | die(_("object not found: %s"), reader->line); |
1533 | /* make sure that it is parsed as shallow */ | |
109cd76d | 1534 | if (!parse_object(the_repository, &oid)) |
f7e20501 BW |
1535 | die(_("error in object: %s"), reader->line); |
1536 | if (unregister_shallow(&oid)) | |
1537 | die(_("no shallow found: %s"), reader->line); | |
1339078f | 1538 | unshallow_received = 1; |
f7e20501 BW |
1539 | continue; |
1540 | } | |
1541 | die(_("expected shallow/unshallow, got %s"), reader->line); | |
1542 | } | |
1543 | ||
1544 | if (reader->status != PACKET_READ_FLUSH && | |
1545 | reader->status != PACKET_READ_DELIM) | |
bbb19a8b | 1546 | die(_("error processing shallow info: %d"), reader->status); |
f7e20501 | 1547 | |
1339078f JT |
1548 | if (args->deepen || unshallow_received) { |
1549 | /* | |
1550 | * Treat these as shallow lines caused by our depth settings. | |
1551 | * In v0, these lines cannot cause refs to be rejected; do the | |
1552 | * same. | |
1553 | */ | |
1554 | int i; | |
1555 | ||
1556 | for (i = 0; i < shallows->nr; i++) | |
1557 | register_shallow(the_repository, &shallows->oid[i]); | |
bd0b42ae JT |
1558 | setup_alternate_shallow(&shallow_lock, &alternate_shallow_file, |
1559 | NULL); | |
1560 | args->deepen = 1; | |
1339078f JT |
1561 | } else if (shallows->nr) { |
1562 | /* | |
1563 | * Treat these as shallow lines caused by the remote being | |
1564 | * shallow. In v0, remote refs that reach these objects are | |
1565 | * rejected (unless --update-shallow is set); do the same. | |
1566 | */ | |
1567 | prepare_shallow_info(si, shallows); | |
4fe788b1 LL |
1568 | if (si->nr_ours || si->nr_theirs) { |
1569 | if (args->reject_shallow_remote) | |
1570 | die(_("source repository is shallow, reject to clone.")); | |
1339078f JT |
1571 | alternate_shallow_file = |
1572 | setup_temporary_shallow(si->shallow); | |
4fe788b1 | 1573 | } else |
1339078f | 1574 | alternate_shallow_file = NULL; |
380ebab2 | 1575 | } else { |
1576 | alternate_shallow_file = NULL; | |
bd0b42ae | 1577 | } |
f7e20501 BW |
1578 | } |
1579 | ||
b7643009 JT |
1580 | static int cmp_name_ref(const void *name, const void *ref) |
1581 | { | |
1582 | return strcmp(name, (*(struct ref **)ref)->name); | |
1583 | } | |
1584 | ||
e2842b39 JT |
1585 | static void receive_wanted_refs(struct packet_reader *reader, |
1586 | struct ref **sought, int nr_sought) | |
73302051 BW |
1587 | { |
1588 | process_section_header(reader, "wanted-refs", 0); | |
1589 | while (packet_reader_read(reader) == PACKET_READ_NORMAL) { | |
1590 | struct object_id oid; | |
1591 | const char *end; | |
b7643009 | 1592 | struct ref **found; |
73302051 BW |
1593 | |
1594 | if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ') | |
bbb19a8b | 1595 | die(_("expected wanted-ref, got '%s'"), reader->line); |
73302051 | 1596 | |
b7643009 JT |
1597 | found = bsearch(end, sought, nr_sought, sizeof(*sought), |
1598 | cmp_name_ref); | |
1599 | if (!found) | |
bbb19a8b | 1600 | die(_("unexpected wanted-ref: '%s'"), reader->line); |
b7643009 | 1601 | oidcpy(&(*found)->old_oid, &oid); |
73302051 BW |
1602 | } |
1603 | ||
1604 | if (reader->status != PACKET_READ_DELIM) | |
bbb19a8b | 1605 | die(_("error processing wanted refs: %d"), reader->status); |
73302051 BW |
1606 | } |
1607 | ||
dd4b732d JT |
1608 | static void receive_packfile_uris(struct packet_reader *reader, |
1609 | struct string_list *uris) | |
1610 | { | |
1611 | process_section_header(reader, "packfile-uris", 0); | |
1612 | while (packet_reader_read(reader) == PACKET_READ_NORMAL) { | |
1613 | if (reader->pktlen < the_hash_algo->hexsz || | |
1614 | reader->line[the_hash_algo->hexsz] != ' ') | |
1615 | die("expected '<hash> <uri>', got: %s\n", reader->line); | |
1616 | ||
1617 | string_list_append(uris, reader->line); | |
1618 | } | |
1619 | if (reader->status != PACKET_READ_DELIM) | |
1620 | die("expected DELIM"); | |
1621 | } | |
1622 | ||
685fbd32 BW |
1623 | enum fetch_state { |
1624 | FETCH_CHECK_LOCAL = 0, | |
1625 | FETCH_SEND_REQUEST, | |
1626 | FETCH_PROCESS_ACKS, | |
1627 | FETCH_GET_PACK, | |
1628 | FETCH_DONE, | |
1629 | }; | |
1630 | ||
9c1e657a | 1631 | static void do_check_stateless_delimiter(int stateless_rpc, |
b0df0c16 DL |
1632 | struct packet_reader *reader) |
1633 | { | |
9c1e657a | 1634 | check_stateless_delimiter(stateless_rpc, reader, |
b0df0c16 DL |
1635 | _("git fetch-pack: expected response end packet")); |
1636 | } | |
1637 | ||
685fbd32 BW |
1638 | static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args, |
1639 | int fd[2], | |
1640 | const struct ref *orig_ref, | |
1641 | struct ref **sought, int nr_sought, | |
1339078f JT |
1642 | struct oid_array *shallows, |
1643 | struct shallow_info *si, | |
9da69a65 | 1644 | struct string_list *pack_lockfiles) |
685fbd32 | 1645 | { |
aaf633c2 | 1646 | struct repository *r = the_repository; |
685fbd32 BW |
1647 | struct ref *ref = copy_ref_list(orig_ref); |
1648 | enum fetch_state state = FETCH_CHECK_LOCAL; | |
1649 | struct oidset common = OIDSET_INIT; | |
1650 | struct packet_reader reader; | |
5fc31180 | 1651 | int in_vain = 0, negotiation_started = 0; |
a29263cf | 1652 | int negotiation_round = 0; |
685fbd32 | 1653 | int haves_to_send = INITIAL_FLUSH; |
603960b5 JT |
1654 | struct fetch_negotiator negotiator_alloc; |
1655 | struct fetch_negotiator *negotiator; | |
4fa3f00a | 1656 | int seen_ack = 0; |
81025703 JT |
1657 | struct object_id common_oid; |
1658 | int received_ready = 0; | |
dd4b732d JT |
1659 | struct string_list packfile_uris = STRING_LIST_INIT_DUP; |
1660 | int i; | |
b664e9ff | 1661 | struct strvec index_pack_args = STRVEC_INIT; |
603960b5 | 1662 | |
9dfa8dbe | 1663 | negotiator = &negotiator_alloc; |
4dfd0925 RC |
1664 | if (args->refetch) |
1665 | fetch_negotiator_init_noop(negotiator); | |
1666 | else | |
1667 | fetch_negotiator_init(r, negotiator); | |
603960b5 | 1668 | |
685fbd32 | 1669 | packet_reader_init(&reader, fd[0], NULL, 0, |
2d103c31 MS |
1670 | PACKET_READ_CHOMP_NEWLINE | |
1671 | PACKET_READ_DIE_ON_ERR_PACKET); | |
07c3c2aa JT |
1672 | if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 1) && |
1673 | server_supports_feature("fetch", "sideband-all", 0)) { | |
0bbc0bc5 JT |
1674 | reader.use_sideband = 1; |
1675 | reader.me = "fetch-pack"; | |
1676 | } | |
685fbd32 BW |
1677 | |
1678 | while (state != FETCH_DONE) { | |
1679 | switch (state) { | |
1680 | case FETCH_CHECK_LOCAL: | |
1681 | sort_ref_list(&ref, ref_compare_name); | |
1682 | QSORT(sought, nr_sought, cmp_ref_by_name); | |
1683 | ||
1684 | /* v2 supports these by default */ | |
1685 | allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1; | |
1686 | use_sideband = 2; | |
f7e20501 BW |
1687 | if (args->depth > 0 || args->deepen_since || args->deepen_not) |
1688 | args->deepen = 1; | |
685fbd32 | 1689 | |
685fbd32 | 1690 | /* Filter 'ref' by 'sought' and those that aren't local */ |
9dfa8dbe JT |
1691 | mark_complete_and_common_ref(negotiator, args, &ref); |
1692 | filter_refs(args, &ref, sought, nr_sought); | |
4dfd0925 | 1693 | if (!args->refetch && everything_local(args, &ref)) |
9dfa8dbe JT |
1694 | state = FETCH_DONE; |
1695 | else | |
685fbd32 | 1696 | state = FETCH_SEND_REQUEST; |
9dfa8dbe JT |
1697 | |
1698 | mark_tips(negotiator, args->negotiation_tips); | |
1699 | for_each_cached_alternate(negotiator, | |
1700 | insert_one_alternate_object); | |
685fbd32 BW |
1701 | break; |
1702 | case FETCH_SEND_REQUEST: | |
5fc31180 JS |
1703 | if (!negotiation_started) { |
1704 | negotiation_started = 1; | |
1705 | trace2_region_enter("fetch-pack", | |
1706 | "negotiation_v2", | |
1707 | the_repository); | |
1708 | } | |
a29263cf JS |
1709 | negotiation_round++; |
1710 | trace2_region_enter_printf("negotiation_v2", "round", | |
1711 | the_repository, "%d", | |
1712 | negotiation_round); | |
603960b5 | 1713 | if (send_fetch_request(negotiator, fd[1], args, ref, |
ec062838 | 1714 | &common, |
0bbc0bc5 | 1715 | &haves_to_send, &in_vain, |
4fa3f00a | 1716 | reader.use_sideband, |
a29263cf JS |
1717 | seen_ack)) { |
1718 | trace2_region_leave_printf("negotiation_v2", "round", | |
1719 | the_repository, "%d", | |
1720 | negotiation_round); | |
685fbd32 | 1721 | state = FETCH_GET_PACK; |
a29263cf | 1722 | } |
685fbd32 BW |
1723 | else |
1724 | state = FETCH_PROCESS_ACKS; | |
1725 | break; | |
1726 | case FETCH_PROCESS_ACKS: | |
1727 | /* Process ACKs/NAKs */ | |
81025703 JT |
1728 | process_section_header(&reader, "acknowledgments", 0); |
1729 | while (process_ack(negotiator, &reader, &common_oid, | |
1730 | &received_ready)) { | |
1731 | in_vain = 0; | |
1732 | seen_ack = 1; | |
1733 | oidset_insert(&common, &common_oid); | |
1734 | } | |
a29263cf JS |
1735 | trace2_region_leave_printf("negotiation_v2", "round", |
1736 | the_repository, "%d", | |
1737 | negotiation_round); | |
81025703 | 1738 | if (received_ready) { |
b0df0c16 DL |
1739 | /* |
1740 | * Don't check for response delimiter; get_pack() will | |
1741 | * read the rest of this response. | |
1742 | */ | |
685fbd32 | 1743 | state = FETCH_GET_PACK; |
81025703 | 1744 | } else { |
9c1e657a | 1745 | do_check_stateless_delimiter(args->stateless_rpc, &reader); |
685fbd32 | 1746 | state = FETCH_SEND_REQUEST; |
685fbd32 BW |
1747 | } |
1748 | break; | |
1749 | case FETCH_GET_PACK: | |
5fc31180 JS |
1750 | trace2_region_leave("fetch-pack", |
1751 | "negotiation_v2", | |
1752 | the_repository); | |
a29263cf JS |
1753 | trace2_data_intmax("negotiation_v2", the_repository, |
1754 | "total_rounds", negotiation_round); | |
f7e20501 BW |
1755 | /* Check for shallow-info section */ |
1756 | if (process_section_header(&reader, "shallow-info", 1)) | |
1339078f | 1757 | receive_shallow_info(args, &reader, shallows, si); |
f7e20501 | 1758 | |
73302051 | 1759 | if (process_section_header(&reader, "wanted-refs", 1)) |
e2842b39 | 1760 | receive_wanted_refs(&reader, sought, nr_sought); |
73302051 | 1761 | |
dd4b732d | 1762 | /* get the pack(s) */ |
88e9b1e3 IF |
1763 | if (git_env_bool("GIT_TRACE_REDACT", 1)) |
1764 | reader.options |= PACKET_READ_REDACT_URI_PATH; | |
dd4b732d JT |
1765 | if (process_section_header(&reader, "packfile-uris", 1)) |
1766 | receive_packfile_uris(&reader, &packfile_uris); | |
88e9b1e3 IF |
1767 | /* We don't expect more URIs. Reset to avoid expensive URI check. */ |
1768 | reader.options &= ~PACKET_READ_REDACT_URI_PATH; | |
1769 | ||
685fbd32 | 1770 | process_section_header(&reader, "packfile", 0); |
ae1a7eef JK |
1771 | |
1772 | /* | |
1773 | * this is the final request we'll make of the server; | |
1774 | * do a half-duplex shutdown to indicate that they can | |
1775 | * hang up as soon as the pack is sent. | |
1776 | */ | |
1777 | close(fd[1]); | |
1778 | fd[1] = -1; | |
1779 | ||
dd4b732d | 1780 | if (get_pack(args, fd, pack_lockfiles, |
b664e9ff | 1781 | packfile_uris.nr ? &index_pack_args : NULL, |
3745e269 | 1782 | sought, nr_sought, &fsck_options.gitmodules_found)) |
685fbd32 | 1783 | die(_("git fetch-pack: fetch failed.")); |
9c1e657a | 1784 | do_check_stateless_delimiter(args->stateless_rpc, &reader); |
685fbd32 BW |
1785 | |
1786 | state = FETCH_DONE; | |
1787 | break; | |
1788 | case FETCH_DONE: | |
1789 | continue; | |
1790 | } | |
1791 | } | |
1792 | ||
dd4b732d | 1793 | for (i = 0; i < packfile_uris.nr; i++) { |
b664e9ff | 1794 | int j; |
dd4b732d JT |
1795 | struct child_process cmd = CHILD_PROCESS_INIT; |
1796 | char packname[GIT_MAX_HEXSZ + 1]; | |
1797 | const char *uri = packfile_uris.items[i].string + | |
1798 | the_hash_algo->hexsz + 1; | |
1799 | ||
ef8d7ac4 JK |
1800 | strvec_push(&cmd.args, "http-fetch"); |
1801 | strvec_pushf(&cmd.args, "--packfile=%.*s", | |
f6d8942b JK |
1802 | (int) the_hash_algo->hexsz, |
1803 | packfile_uris.items[i].string); | |
b664e9ff JT |
1804 | for (j = 0; j < index_pack_args.nr; j++) |
1805 | strvec_pushf(&cmd.args, "--index-pack-arg=%s", | |
1806 | index_pack_args.v[j]); | |
ef8d7ac4 | 1807 | strvec_push(&cmd.args, uri); |
dd4b732d JT |
1808 | cmd.git_cmd = 1; |
1809 | cmd.no_stdin = 1; | |
1810 | cmd.out = -1; | |
1811 | if (start_command(&cmd)) | |
1812 | die("fetch-pack: unable to spawn http-fetch"); | |
1813 | ||
1814 | if (read_in_full(cmd.out, packname, 5) < 0 || | |
1815 | memcmp(packname, "keep\t", 5)) | |
1816 | die("fetch-pack: expected keep then TAB at start of http-fetch output"); | |
1817 | ||
1818 | if (read_in_full(cmd.out, packname, | |
1819 | the_hash_algo->hexsz + 1) < 0 || | |
1820 | packname[the_hash_algo->hexsz] != '\n') | |
1821 | die("fetch-pack: expected hash then LF at end of http-fetch output"); | |
1822 | ||
1823 | packname[the_hash_algo->hexsz] = '\0'; | |
1824 | ||
3745e269 | 1825 | parse_gitmodules_oids(cmd.out, &fsck_options.gitmodules_found); |
5476e1ef | 1826 | |
dd4b732d JT |
1827 | close(cmd.out); |
1828 | ||
1829 | if (finish_command(&cmd)) | |
1830 | die("fetch-pack: unable to finish http-fetch"); | |
1831 | ||
1832 | if (memcmp(packfile_uris.items[i].string, packname, | |
1833 | the_hash_algo->hexsz)) | |
1834 | die("fetch-pack: pack downloaded from %s does not match expected hash %.*s", | |
1835 | uri, (int) the_hash_algo->hexsz, | |
1836 | packfile_uris.items[i].string); | |
1837 | ||
1838 | string_list_append_nodup(pack_lockfiles, | |
1839 | xstrfmt("%s/pack/pack-%s.keep", | |
1840 | get_object_directory(), | |
1841 | packname)); | |
1842 | } | |
1843 | string_list_clear(&packfile_uris, 0); | |
b664e9ff | 1844 | strvec_clear(&index_pack_args); |
dd4b732d | 1845 | |
3745e269 ÆAB |
1846 | if (fsck_finish(&fsck_options)) |
1847 | die("fsck failed"); | |
dd4b732d | 1848 | |
603960b5 JT |
1849 | if (negotiator) |
1850 | negotiator->release(negotiator); | |
dd4b732d | 1851 | |
685fbd32 BW |
1852 | oidset_clear(&common); |
1853 | return ref; | |
1854 | } | |
1855 | ||
1362df0d ÆAB |
1856 | static int fetch_pack_config_cb(const char *var, const char *value, void *cb) |
1857 | { | |
1858 | if (strcmp(var, "fetch.fsck.skiplist") == 0) { | |
1859 | const char *path; | |
1860 | ||
1861 | if (git_config_pathname(&path, var, value)) | |
1862 | return 1; | |
1863 | strbuf_addf(&fsck_msg_types, "%cskiplist=%s", | |
1864 | fsck_msg_types.len ? ',' : '=', path); | |
1865 | free((char *)path); | |
1866 | return 0; | |
1867 | } | |
1868 | ||
1869 | if (skip_prefix(var, "fetch.fsck.", &var)) { | |
1870 | if (is_valid_msg_type(var, value)) | |
1871 | strbuf_addf(&fsck_msg_types, "%c%s=%s", | |
1872 | fsck_msg_types.len ? ',' : '=', var, value); | |
1873 | else | |
1874 | warning("Skipping unknown msg id '%s'", var); | |
1875 | return 0; | |
1876 | } | |
1877 | ||
1878 | return git_default_config(var, value, cb); | |
1879 | } | |
1880 | ||
f44af51d | 1881 | static void fetch_pack_config(void) |
745f7a8c | 1882 | { |
f44af51d TA |
1883 | git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit); |
1884 | git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit); | |
1885 | git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta); | |
1886 | git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects); | |
1887 | git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects); | |
1e905bbc | 1888 | git_config_get_bool("transfer.advertisesid", &advertise_sid); |
dd4b732d JT |
1889 | if (!uri_protocols.nr) { |
1890 | char *str; | |
1891 | ||
1892 | if (!git_config_get_string("fetch.uriprotocols", &str) && str) { | |
1893 | string_list_split(&uri_protocols, str, ',', -1); | |
1894 | free(str); | |
1895 | } | |
1896 | } | |
745f7a8c | 1897 | |
1362df0d | 1898 | git_config(fetch_pack_config_cb, NULL); |
745f7a8c NTND |
1899 | } |
1900 | ||
745f7a8c NTND |
1901 | static void fetch_pack_setup(void) |
1902 | { | |
1903 | static int did_setup; | |
1904 | if (did_setup) | |
1905 | return; | |
f44af51d | 1906 | fetch_pack_config(); |
745f7a8c NTND |
1907 | if (0 <= transfer_unpack_limit) |
1908 | unpack_limit = transfer_unpack_limit; | |
1909 | else if (0 <= fetch_unpack_limit) | |
1910 | unpack_limit = fetch_unpack_limit; | |
1911 | did_setup = 1; | |
1912 | } | |
1913 | ||
f2db854d JH |
1914 | static int remove_duplicates_in_refs(struct ref **ref, int nr) |
1915 | { | |
1916 | struct string_list names = STRING_LIST_INIT_NODUP; | |
1917 | int src, dst; | |
1918 | ||
1919 | for (src = dst = 0; src < nr; src++) { | |
1920 | struct string_list_item *item; | |
1921 | item = string_list_insert(&names, ref[src]->name); | |
1922 | if (item->util) | |
1923 | continue; /* already have it */ | |
1924 | item->util = ref[src]; | |
1925 | if (src != dst) | |
1926 | ref[dst] = ref[src]; | |
1927 | dst++; | |
1928 | } | |
1929 | for (src = dst; src < nr; src++) | |
1930 | ref[src] = NULL; | |
1931 | string_list_clear(&names, 0); | |
1932 | return dst; | |
1933 | } | |
1934 | ||
beea4152 | 1935 | static void update_shallow(struct fetch_pack_args *args, |
e2842b39 | 1936 | struct ref **sought, int nr_sought, |
beea4152 | 1937 | struct shallow_info *si) |
a796ccee | 1938 | { |
910650d2 | 1939 | struct oid_array ref = OID_ARRAY_INIT; |
4820a33b | 1940 | int *status; |
beea4152 NTND |
1941 | int i; |
1942 | ||
79891cb9 | 1943 | if (args->deepen && alternate_shallow_file) { |
a796ccee | 1944 | if (*alternate_shallow_file == '\0') { /* --unshallow */ |
102de880 | 1945 | unlink_or_warn(git_path_shallow(the_repository)); |
37b9dcab | 1946 | rollback_shallow_file(the_repository, &shallow_lock); |
a796ccee | 1947 | } else |
37b9dcab | 1948 | commit_shallow_file(the_repository, &shallow_lock); |
23311f35 | 1949 | alternate_shallow_file = NULL; |
a796ccee NTND |
1950 | return; |
1951 | } | |
beea4152 NTND |
1952 | |
1953 | if (!si->shallow || !si->shallow->nr) | |
1954 | return; | |
1955 | ||
beea4152 NTND |
1956 | if (args->cloning) { |
1957 | /* | |
1958 | * remote is shallow, but this is a clone, there are | |
1959 | * no objects in repo to worry about. Accept any | |
1960 | * shallow points that exist in the pack (iow in repo | |
1961 | * after get_pack() and reprepare_packed_git()) | |
1962 | */ | |
910650d2 | 1963 | struct oid_array extra = OID_ARRAY_INIT; |
ee3051bd | 1964 | struct object_id *oid = si->shallow->oid; |
beea4152 | 1965 | for (i = 0; i < si->shallow->nr; i++) |
ee3051bd | 1966 | if (has_object_file(&oid[i])) |
910650d2 | 1967 | oid_array_append(&extra, &oid[i]); |
beea4152 NTND |
1968 | if (extra.nr) { |
1969 | setup_alternate_shallow(&shallow_lock, | |
1970 | &alternate_shallow_file, | |
1971 | &extra); | |
37b9dcab | 1972 | commit_shallow_file(the_repository, &shallow_lock); |
23311f35 | 1973 | alternate_shallow_file = NULL; |
beea4152 | 1974 | } |
910650d2 | 1975 | oid_array_clear(&extra); |
beea4152 NTND |
1976 | return; |
1977 | } | |
4820a33b NTND |
1978 | |
1979 | if (!si->nr_ours && !si->nr_theirs) | |
1980 | return; | |
1981 | ||
1982 | remove_nonexistent_theirs_shallow(si); | |
4820a33b NTND |
1983 | if (!si->nr_ours && !si->nr_theirs) |
1984 | return; | |
e2842b39 JT |
1985 | for (i = 0; i < nr_sought; i++) |
1986 | oid_array_append(&ref, &sought[i]->old_oid); | |
4820a33b NTND |
1987 | si->ref = &ref; |
1988 | ||
48d25cae NTND |
1989 | if (args->update_shallow) { |
1990 | /* | |
1991 | * remote is also shallow, .git/shallow may be updated | |
1992 | * so all refs can be accepted. Make sure we only add | |
1993 | * shallow roots that are actually reachable from new | |
1994 | * refs. | |
1995 | */ | |
910650d2 | 1996 | struct oid_array extra = OID_ARRAY_INIT; |
ee3051bd | 1997 | struct object_id *oid = si->shallow->oid; |
48d25cae NTND |
1998 | assign_shallow_commits_to_refs(si, NULL, NULL); |
1999 | if (!si->nr_ours && !si->nr_theirs) { | |
910650d2 | 2000 | oid_array_clear(&ref); |
48d25cae NTND |
2001 | return; |
2002 | } | |
2003 | for (i = 0; i < si->nr_ours; i++) | |
910650d2 | 2004 | oid_array_append(&extra, &oid[si->ours[i]]); |
48d25cae | 2005 | for (i = 0; i < si->nr_theirs; i++) |
910650d2 | 2006 | oid_array_append(&extra, &oid[si->theirs[i]]); |
48d25cae NTND |
2007 | setup_alternate_shallow(&shallow_lock, |
2008 | &alternate_shallow_file, | |
2009 | &extra); | |
37b9dcab | 2010 | commit_shallow_file(the_repository, &shallow_lock); |
910650d2 | 2011 | oid_array_clear(&extra); |
2012 | oid_array_clear(&ref); | |
23311f35 | 2013 | alternate_shallow_file = NULL; |
48d25cae NTND |
2014 | return; |
2015 | } | |
2016 | ||
4820a33b NTND |
2017 | /* |
2018 | * remote is also shallow, check what ref is safe to update | |
2019 | * without updating .git/shallow | |
2020 | */ | |
ca56dadb | 2021 | CALLOC_ARRAY(status, nr_sought); |
4820a33b NTND |
2022 | assign_shallow_commits_to_refs(si, NULL, status); |
2023 | if (si->nr_ours || si->nr_theirs) { | |
e2842b39 | 2024 | for (i = 0; i < nr_sought; i++) |
4820a33b | 2025 | if (status[i]) |
e2842b39 | 2026 | sought[i]->status = REF_STATUS_REJECT_SHALLOW; |
4820a33b NTND |
2027 | } |
2028 | free(status); | |
910650d2 | 2029 | oid_array_clear(&ref); |
a796ccee NTND |
2030 | } |
2031 | ||
9fec7b21 | 2032 | static const struct object_id *iterate_ref_map(void *cb_data) |
cf1e7c07 JT |
2033 | { |
2034 | struct ref **rm = cb_data; | |
2035 | struct ref *ref = *rm; | |
2036 | ||
2037 | if (!ref) | |
9fec7b21 | 2038 | return NULL; |
cf1e7c07 | 2039 | *rm = ref->next; |
9fec7b21 | 2040 | return &ref->old_oid; |
cf1e7c07 JT |
2041 | } |
2042 | ||
745f7a8c | 2043 | struct ref *fetch_pack(struct fetch_pack_args *args, |
0f804b0b | 2044 | int fd[], |
745f7a8c | 2045 | const struct ref *ref, |
f2db854d | 2046 | struct ref **sought, int nr_sought, |
910650d2 | 2047 | struct oid_array *shallow, |
9da69a65 | 2048 | struct string_list *pack_lockfiles, |
685fbd32 | 2049 | enum protocol_version version) |
745f7a8c | 2050 | { |
745f7a8c | 2051 | struct ref *ref_cpy; |
beea4152 | 2052 | struct shallow_info si; |
1339078f | 2053 | struct oid_array shallows_scratch = OID_ARRAY_INIT; |
745f7a8c NTND |
2054 | |
2055 | fetch_pack_setup(); | |
f2db854d JH |
2056 | if (nr_sought) |
2057 | nr_sought = remove_duplicates_in_refs(sought, nr_sought); | |
745f7a8c | 2058 | |
01775651 | 2059 | if (version != protocol_v2 && !ref) { |
745f7a8c | 2060 | packet_flush(fd[1]); |
1dd73e20 | 2061 | die(_("no matching remote head")); |
745f7a8c | 2062 | } |
1e7d440b JT |
2063 | if (version == protocol_v2) { |
2064 | if (shallow->nr) | |
2065 | BUG("Protocol V2 does not provide shallows at this point in the fetch"); | |
2066 | memset(&si, 0, sizeof(si)); | |
685fbd32 | 2067 | ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought, |
1339078f | 2068 | &shallows_scratch, &si, |
9da69a65 | 2069 | pack_lockfiles); |
1e7d440b JT |
2070 | } else { |
2071 | prepare_shallow_info(&si, shallow); | |
685fbd32 | 2072 | ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought, |
9da69a65 | 2073 | &si, pack_lockfiles); |
1e7d440b | 2074 | } |
a49d2834 | 2075 | reprepare_packed_git(the_repository); |
cf1e7c07 JT |
2076 | |
2077 | if (!args->cloning && args->deepen) { | |
2078 | struct check_connected_options opt = CHECK_CONNECTED_INIT; | |
2079 | struct ref *iterator = ref_cpy; | |
2080 | opt.shallow_file = alternate_shallow_file; | |
2081 | if (args->deepen) | |
2082 | opt.is_deepening_fetch = 1; | |
2083 | if (check_connected(iterate_ref_map, &iterator, &opt)) { | |
2084 | error(_("remote did not send all necessary objects")); | |
2085 | free_refs(ref_cpy); | |
2086 | ref_cpy = NULL; | |
37b9dcab | 2087 | rollback_shallow_file(the_repository, &shallow_lock); |
cf1e7c07 JT |
2088 | goto cleanup; |
2089 | } | |
2090 | args->connectivity_checked = 1; | |
2091 | } | |
2092 | ||
e2842b39 | 2093 | update_shallow(args, sought, nr_sought, &si); |
cf1e7c07 | 2094 | cleanup: |
beea4152 | 2095 | clear_shallow_info(&si); |
1339078f | 2096 | oid_array_clear(&shallows_scratch); |
745f7a8c NTND |
2097 | return ref_cpy; |
2098 | } | |
e860d96b | 2099 | |
9c1e657a JT |
2100 | static int add_to_object_array(const struct object_id *oid, void *data) |
2101 | { | |
2102 | struct object_array *a = data; | |
2103 | ||
2104 | add_object_array(lookup_object(the_repository, oid), "", a); | |
2105 | return 0; | |
2106 | } | |
2107 | ||
2108 | static void clear_common_flag(struct oidset *s) | |
2109 | { | |
2110 | struct oidset_iter iter; | |
2111 | const struct object_id *oid; | |
2112 | oidset_iter_init(s, &iter); | |
2113 | ||
2114 | while ((oid = oidset_iter_next(&iter))) { | |
2115 | struct object *obj = lookup_object(the_repository, oid); | |
2116 | obj->flags &= ~COMMON; | |
2117 | } | |
2118 | } | |
2119 | ||
2120 | void negotiate_using_fetch(const struct oid_array *negotiation_tips, | |
2121 | const struct string_list *server_options, | |
2122 | int stateless_rpc, | |
2123 | int fd[], | |
2124 | struct oidset *acked_commits) | |
2125 | { | |
2126 | struct fetch_negotiator negotiator; | |
2127 | struct packet_reader reader; | |
2128 | struct object_array nt_object_array = OBJECT_ARRAY_INIT; | |
2129 | struct strbuf req_buf = STRBUF_INIT; | |
2130 | int haves_to_send = INITIAL_FLUSH; | |
2131 | int in_vain = 0; | |
2132 | int seen_ack = 0; | |
2133 | int last_iteration = 0; | |
a29263cf | 2134 | int negotiation_round = 0; |
9c1e657a JT |
2135 | timestamp_t min_generation = GENERATION_NUMBER_INFINITY; |
2136 | ||
2137 | fetch_negotiator_init(the_repository, &negotiator); | |
2138 | mark_tips(&negotiator, negotiation_tips); | |
2139 | ||
2140 | packet_reader_init(&reader, fd[0], NULL, 0, | |
2141 | PACKET_READ_CHOMP_NEWLINE | | |
2142 | PACKET_READ_DIE_ON_ERR_PACKET); | |
2143 | ||
2144 | oid_array_for_each((struct oid_array *) negotiation_tips, | |
2145 | add_to_object_array, | |
2146 | &nt_object_array); | |
2147 | ||
a29263cf | 2148 | trace2_region_enter("fetch-pack", "negotiate_using_fetch", the_repository); |
9c1e657a JT |
2149 | while (!last_iteration) { |
2150 | int haves_added; | |
2151 | struct object_id common_oid; | |
2152 | int received_ready = 0; | |
2153 | ||
a29263cf JS |
2154 | negotiation_round++; |
2155 | ||
2156 | trace2_region_enter_printf("negotiate_using_fetch", "round", | |
2157 | the_repository, "%d", | |
2158 | negotiation_round); | |
9c1e657a JT |
2159 | strbuf_reset(&req_buf); |
2160 | write_fetch_command_and_capabilities(&req_buf, server_options); | |
2161 | ||
2162 | packet_buf_write(&req_buf, "wait-for-done"); | |
2163 | ||
2164 | haves_added = add_haves(&negotiator, &req_buf, &haves_to_send); | |
2165 | in_vain += haves_added; | |
2166 | if (!haves_added || (seen_ack && in_vain >= MAX_IN_VAIN)) | |
2167 | last_iteration = 1; | |
2168 | ||
a29263cf JS |
2169 | trace2_data_intmax("negotiate_using_fetch", the_repository, |
2170 | "haves_added", haves_added); | |
2171 | trace2_data_intmax("negotiate_using_fetch", the_repository, | |
2172 | "in_vain", in_vain); | |
2173 | ||
9c1e657a JT |
2174 | /* Send request */ |
2175 | packet_buf_flush(&req_buf); | |
2176 | if (write_in_full(fd[1], req_buf.buf, req_buf.len) < 0) | |
2177 | die_errno(_("unable to write request to remote")); | |
2178 | ||
2179 | /* Process ACKs/NAKs */ | |
2180 | process_section_header(&reader, "acknowledgments", 0); | |
2181 | while (process_ack(&negotiator, &reader, &common_oid, | |
2182 | &received_ready)) { | |
2183 | struct commit *commit = lookup_commit(the_repository, | |
2184 | &common_oid); | |
2185 | if (commit) { | |
2186 | timestamp_t generation; | |
2187 | ||
2188 | parse_commit_or_die(commit); | |
2189 | commit->object.flags |= COMMON; | |
2190 | generation = commit_graph_generation(commit); | |
2191 | if (generation < min_generation) | |
2192 | min_generation = generation; | |
2193 | } | |
2194 | in_vain = 0; | |
2195 | seen_ack = 1; | |
2196 | oidset_insert(acked_commits, &common_oid); | |
2197 | } | |
2198 | if (received_ready) | |
2199 | die(_("unexpected 'ready' from remote")); | |
2200 | else | |
2201 | do_check_stateless_delimiter(stateless_rpc, &reader); | |
2202 | if (can_all_from_reach_with_flag(&nt_object_array, COMMON, | |
2203 | REACH_SCRATCH, 0, | |
2204 | min_generation)) | |
2205 | last_iteration = 1; | |
a29263cf JS |
2206 | trace2_region_leave_printf("negotiation", "round", |
2207 | the_repository, "%d", | |
2208 | negotiation_round); | |
9c1e657a | 2209 | } |
a29263cf JS |
2210 | trace2_region_enter("fetch-pack", "negotiate_using_fetch", the_repository); |
2211 | trace2_data_intmax("negotiate_using_fetch", the_repository, | |
2212 | "total_rounds", negotiation_round); | |
9c1e657a JT |
2213 | clear_common_flag(acked_commits); |
2214 | strbuf_release(&req_buf); | |
2215 | } | |
2216 | ||
e860d96b MM |
2217 | int report_unmatched_refs(struct ref **sought, int nr_sought) |
2218 | { | |
2219 | int i, ret = 0; | |
2220 | ||
2221 | for (i = 0; i < nr_sought; i++) { | |
d56583de | 2222 | if (!sought[i]) |
e860d96b | 2223 | continue; |
d56583de MM |
2224 | switch (sought[i]->match_status) { |
2225 | case REF_MATCHED: | |
2226 | continue; | |
2227 | case REF_NOT_MATCHED: | |
2228 | error(_("no such remote ref %s"), sought[i]->name); | |
2229 | break; | |
2230 | case REF_UNADVERTISED_NOT_ALLOWED: | |
2231 | error(_("Server does not allow request for unadvertised object %s"), | |
2232 | sought[i]->name); | |
2233 | break; | |
2234 | } | |
e860d96b MM |
2235 | ret = 1; |
2236 | } | |
2237 | return ret; | |
2238 | } |