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