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