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