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