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