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