| 1 | #include "git-compat-util.h" |
| 2 | #include "config.h" |
| 3 | #include "commit.h" |
| 4 | #include "date.h" |
| 5 | #include "gettext.h" |
| 6 | #include "hex.h" |
| 7 | #include "odb.h" |
| 8 | #include "pkt-line.h" |
| 9 | #include "sideband.h" |
| 10 | #include "run-command.h" |
| 11 | #include "remote.h" |
| 12 | #include "connect.h" |
| 13 | #include "send-pack.h" |
| 14 | #include "transport.h" |
| 15 | #include "version.h" |
| 16 | #include "oid-array.h" |
| 17 | #include "gpg-interface.h" |
| 18 | #include "shallow.h" |
| 19 | #include "parse-options.h" |
| 20 | #include "trace2.h" |
| 21 | #include "write-or-die.h" |
| 22 | |
| 23 | int option_parse_push_signed(const struct option *opt, |
| 24 | const char *arg, int unset) |
| 25 | { |
| 26 | if (unset) { |
| 27 | *(int *)(opt->value) = SEND_PACK_PUSH_CERT_NEVER; |
| 28 | return 0; |
| 29 | } |
| 30 | switch (git_parse_maybe_bool(arg)) { |
| 31 | case 1: |
| 32 | *(int *)(opt->value) = SEND_PACK_PUSH_CERT_ALWAYS; |
| 33 | return 0; |
| 34 | case 0: |
| 35 | *(int *)(opt->value) = SEND_PACK_PUSH_CERT_NEVER; |
| 36 | return 0; |
| 37 | } |
| 38 | if (!strcasecmp("if-asked", arg)) { |
| 39 | *(int *)(opt->value) = SEND_PACK_PUSH_CERT_IF_ASKED; |
| 40 | return 0; |
| 41 | } |
| 42 | die("bad %s argument: %s", opt->long_name, arg); |
| 43 | } |
| 44 | |
| 45 | static void feed_object(struct repository *r, |
| 46 | const struct object_id *oid, FILE *fh, int negative) |
| 47 | { |
| 48 | if (negative && !odb_has_object(r->objects, oid, 0)) |
| 49 | return; |
| 50 | |
| 51 | if (negative) |
| 52 | putc('^', fh); |
| 53 | fputs(oid_to_hex(oid), fh); |
| 54 | putc('\n', fh); |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * Make a pack stream and spit it out into file descriptor fd |
| 59 | */ |
| 60 | static int pack_objects(struct repository *r, |
| 61 | int fd, struct ref *refs, struct oid_array *advertised, |
| 62 | struct oid_array *negotiated, |
| 63 | struct send_pack_args *args) |
| 64 | { |
| 65 | /* |
| 66 | * The child becomes pack-objects --revs; we feed |
| 67 | * the revision parameters to it via its stdin and |
| 68 | * let its stdout go back to the other end. |
| 69 | */ |
| 70 | struct child_process po = CHILD_PROCESS_INIT; |
| 71 | FILE *po_in; |
| 72 | int rc; |
| 73 | |
| 74 | trace2_region_enter("send_pack", "pack_objects", r); |
| 75 | strvec_push(&po.args, "pack-objects"); |
| 76 | strvec_push(&po.args, "--all-progress-implied"); |
| 77 | strvec_push(&po.args, "--revs"); |
| 78 | strvec_push(&po.args, "--stdout"); |
| 79 | if (args->use_thin_pack) |
| 80 | strvec_push(&po.args, "--thin"); |
| 81 | if (args->use_ofs_delta) |
| 82 | strvec_push(&po.args, "--delta-base-offset"); |
| 83 | if (args->no_ref_delta) |
| 84 | strvec_push(&po.args, "--no-ref-delta"); |
| 85 | if (args->quiet || !args->progress) |
| 86 | strvec_push(&po.args, "-q"); |
| 87 | if (args->progress) |
| 88 | strvec_push(&po.args, "--progress"); |
| 89 | if (is_repository_shallow(r)) |
| 90 | strvec_push(&po.args, "--shallow"); |
| 91 | if (args->disable_bitmaps) |
| 92 | strvec_push(&po.args, "--no-use-bitmap-index"); |
| 93 | po.in = -1; |
| 94 | po.out = args->stateless_rpc ? -1 : fd; |
| 95 | po.git_cmd = 1; |
| 96 | po.clean_on_exit = 1; |
| 97 | if (start_command(&po)) |
| 98 | die_errno("git pack-objects failed"); |
| 99 | |
| 100 | /* |
| 101 | * We feed the pack-objects we just spawned with revision |
| 102 | * parameters by writing to the pipe. |
| 103 | */ |
| 104 | po_in = xfdopen(po.in, "w"); |
| 105 | for (size_t i = 0; i < advertised->nr; i++) |
| 106 | feed_object(r, &advertised->oid[i], po_in, 1); |
| 107 | for (size_t i = 0; i < negotiated->nr; i++) |
| 108 | feed_object(r, &negotiated->oid[i], po_in, 1); |
| 109 | |
| 110 | while (refs) { |
| 111 | if (!is_null_oid(&refs->old_oid)) |
| 112 | feed_object(r, &refs->old_oid, po_in, 1); |
| 113 | if (!is_null_oid(&refs->new_oid)) |
| 114 | feed_object(r, &refs->new_oid, po_in, 0); |
| 115 | refs = refs->next; |
| 116 | } |
| 117 | |
| 118 | fflush(po_in); |
| 119 | if (ferror(po_in)) |
| 120 | die_errno("error writing to pack-objects"); |
| 121 | fclose(po_in); |
| 122 | |
| 123 | if (args->stateless_rpc) { |
| 124 | char *buf = xmalloc(LARGE_PACKET_MAX); |
| 125 | while (1) { |
| 126 | ssize_t n = xread(po.out, buf, LARGE_PACKET_MAX); |
| 127 | if (n <= 0) |
| 128 | break; |
| 129 | send_sideband(fd, -1, buf, n, LARGE_PACKET_MAX); |
| 130 | } |
| 131 | free(buf); |
| 132 | close(po.out); |
| 133 | po.out = -1; |
| 134 | } |
| 135 | |
| 136 | rc = finish_command(&po); |
| 137 | if (rc) { |
| 138 | /* |
| 139 | * For a normal non-zero exit, we assume pack-objects wrote |
| 140 | * something useful to stderr. For death by signal, though, |
| 141 | * we should mention it to the user. The exception is SIGPIPE |
| 142 | * (141), because that's a normal occurrence if the remote end |
| 143 | * hangs up (and we'll report that by trying to read the unpack |
| 144 | * status). |
| 145 | */ |
| 146 | if (rc > 128 && rc != 141) |
| 147 | error("pack-objects died of signal %d", rc - 128); |
| 148 | trace2_region_leave("send_pack", "pack_objects", r); |
| 149 | return -1; |
| 150 | } |
| 151 | trace2_region_leave("send_pack", "pack_objects", r); |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | static int receive_unpack_status(struct packet_reader *reader) |
| 156 | { |
| 157 | if (packet_reader_read(reader) != PACKET_READ_NORMAL) |
| 158 | return error(_("unexpected flush packet while reading remote unpack status")); |
| 159 | if (!skip_prefix(reader->line, "unpack ", &reader->line)) |
| 160 | return error(_("unable to parse remote unpack status: %s"), reader->line); |
| 161 | if (strcmp(reader->line, "ok")) |
| 162 | return error(_("remote unpack failed: %s"), reader->line); |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static int receive_status(struct repository *r, |
| 167 | struct packet_reader *reader, struct ref *refs) |
| 168 | { |
| 169 | struct ref *hint; |
| 170 | int ret; |
| 171 | struct ref_push_report *report = NULL; |
| 172 | int new_report = 0; |
| 173 | int once = 0; |
| 174 | |
| 175 | trace2_region_enter("send_pack", "receive_status", r); |
| 176 | hint = NULL; |
| 177 | ret = receive_unpack_status(reader); |
| 178 | while (1) { |
| 179 | struct object_id old_oid, new_oid; |
| 180 | char *head; |
| 181 | char *refname; |
| 182 | char *p; |
| 183 | if (packet_reader_read(reader) != PACKET_READ_NORMAL) |
| 184 | break; |
| 185 | head = reader->line; |
| 186 | p = strchr(head, ' '); |
| 187 | if (!p) { |
| 188 | error("invalid status line from remote: %s", reader->line); |
| 189 | ret = -1; |
| 190 | break; |
| 191 | } |
| 192 | *p++ = '\0'; |
| 193 | |
| 194 | if (!strcmp(head, "option")) { |
| 195 | char *key; |
| 196 | const char *val; |
| 197 | |
| 198 | if (!hint || !(report || new_report)) { |
| 199 | if (!once++) |
| 200 | error("'option' without a matching 'ok/ng' directive"); |
| 201 | ret = -1; |
| 202 | continue; |
| 203 | } |
| 204 | if (new_report) { |
| 205 | if (!hint->report) { |
| 206 | CALLOC_ARRAY(hint->report, 1); |
| 207 | report = hint->report; |
| 208 | } else { |
| 209 | report = hint->report; |
| 210 | while (report->next) |
| 211 | report = report->next; |
| 212 | CALLOC_ARRAY(report->next, 1); |
| 213 | report = report->next; |
| 214 | } |
| 215 | new_report = 0; |
| 216 | } |
| 217 | key = p; |
| 218 | p = strchr(key, ' '); |
| 219 | if (p) |
| 220 | *p++ = '\0'; |
| 221 | val = p; |
| 222 | if (!strcmp(key, "refname")) |
| 223 | report->ref_name = xstrdup_or_null(val); |
| 224 | else if (!strcmp(key, "old-oid") && val && |
| 225 | !parse_oid_hex_algop(val, &old_oid, &val, r->hash_algo)) |
| 226 | report->old_oid = oiddup(&old_oid); |
| 227 | else if (!strcmp(key, "new-oid") && val && |
| 228 | !parse_oid_hex_algop(val, &new_oid, &val, r->hash_algo)) |
| 229 | report->new_oid = oiddup(&new_oid); |
| 230 | else if (!strcmp(key, "forced-update")) |
| 231 | report->forced_update = 1; |
| 232 | continue; |
| 233 | } |
| 234 | |
| 235 | report = NULL; |
| 236 | new_report = 0; |
| 237 | if (strcmp(head, "ok") && strcmp(head, "ng")) { |
| 238 | error("invalid ref status from remote: %s", head); |
| 239 | ret = -1; |
| 240 | break; |
| 241 | } |
| 242 | refname = p; |
| 243 | p = strchr(refname, ' '); |
| 244 | if (p) |
| 245 | *p++ = '\0'; |
| 246 | /* first try searching at our hint, falling back to all refs */ |
| 247 | if (hint) |
| 248 | hint = find_ref_by_name(hint, refname); |
| 249 | if (!hint) |
| 250 | hint = find_ref_by_name(refs, refname); |
| 251 | if (!hint) { |
| 252 | warning("remote reported status on unknown ref: %s", |
| 253 | refname); |
| 254 | continue; |
| 255 | } |
| 256 | if (hint->status != REF_STATUS_EXPECTING_REPORT && |
| 257 | hint->status != REF_STATUS_OK && |
| 258 | hint->status != REF_STATUS_REMOTE_REJECT) { |
| 259 | warning("remote reported status on unexpected ref: %s", |
| 260 | refname); |
| 261 | continue; |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * Clients sending duplicate refs can cause the same value |
| 266 | * to be overridden, causing a memory leak. |
| 267 | */ |
| 268 | free(hint->remote_status); |
| 269 | |
| 270 | if (!strcmp(head, "ng")) { |
| 271 | hint->status = REF_STATUS_REMOTE_REJECT; |
| 272 | if (p) |
| 273 | hint->remote_status = xstrdup(p); |
| 274 | else |
| 275 | hint->remote_status = xstrdup("failed"); |
| 276 | } else { |
| 277 | hint->status = REF_STATUS_OK; |
| 278 | hint->remote_status = xstrdup_or_null(p); |
| 279 | new_report = 1; |
| 280 | } |
| 281 | } |
| 282 | trace2_region_leave("send_pack", "receive_status", r); |
| 283 | return ret; |
| 284 | } |
| 285 | |
| 286 | static int sideband_demux(int in UNUSED, int out, void *data) |
| 287 | { |
| 288 | int *fd = data, ret; |
| 289 | if (async_with_fork()) |
| 290 | close(fd[1]); |
| 291 | ret = recv_sideband("send-pack", fd[0], out); |
| 292 | close(out); |
| 293 | return ret; |
| 294 | } |
| 295 | |
| 296 | static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb) |
| 297 | { |
| 298 | struct strbuf *sb = cb; |
| 299 | if (graft->nr_parent == -1) |
| 300 | packet_buf_write(sb, "shallow %s\n", oid_to_hex(&graft->oid)); |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | static void advertise_shallow_grafts_buf(struct repository *r, struct strbuf *sb) |
| 305 | { |
| 306 | if (!is_repository_shallow(r)) |
| 307 | return; |
| 308 | for_each_commit_graft(advertise_shallow_grafts_cb, sb); |
| 309 | } |
| 310 | |
| 311 | #define CHECK_REF_NO_PUSH -1 |
| 312 | #define CHECK_REF_STATUS_REJECTED -2 |
| 313 | #define CHECK_REF_UPTODATE -3 |
| 314 | static int check_to_send_update(const struct ref *ref, const struct send_pack_args *args) |
| 315 | { |
| 316 | if (!ref->peer_ref && !args->send_mirror) |
| 317 | return CHECK_REF_NO_PUSH; |
| 318 | |
| 319 | /* Check for statuses set by set_ref_status_for_push() */ |
| 320 | switch (ref->status) { |
| 321 | case REF_STATUS_REJECT_NONFASTFORWARD: |
| 322 | case REF_STATUS_REJECT_ALREADY_EXISTS: |
| 323 | case REF_STATUS_REJECT_FETCH_FIRST: |
| 324 | case REF_STATUS_REJECT_NEEDS_FORCE: |
| 325 | case REF_STATUS_REJECT_STALE: |
| 326 | case REF_STATUS_REJECT_REMOTE_UPDATED: |
| 327 | case REF_STATUS_REJECT_NODELETE: |
| 328 | return CHECK_REF_STATUS_REJECTED; |
| 329 | case REF_STATUS_UPTODATE: |
| 330 | return CHECK_REF_UPTODATE; |
| 331 | |
| 332 | default: |
| 333 | case REF_STATUS_EXPECTING_REPORT: |
| 334 | /* already passed checks on the local side */ |
| 335 | case REF_STATUS_OK: |
| 336 | /* of course this is OK */ |
| 337 | return 0; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | /* |
| 342 | * the beginning of the next line, or the end of buffer. |
| 343 | * |
| 344 | * NEEDSWORK: perhaps move this to git-compat-util.h or somewhere and |
| 345 | * convert many similar uses found by "git grep -A4 memchr". |
| 346 | */ |
| 347 | static const char *next_line(const char *line, size_t len) |
| 348 | { |
| 349 | const char *nl = memchr(line, '\n', len); |
| 350 | if (!nl) |
| 351 | return line + len; /* incomplete line */ |
| 352 | return nl + 1; |
| 353 | } |
| 354 | |
| 355 | static int generate_push_cert(struct strbuf *req_buf, |
| 356 | const struct ref *remote_refs, |
| 357 | struct send_pack_args *args, |
| 358 | const char *cap_string, |
| 359 | const char *push_cert_nonce) |
| 360 | { |
| 361 | const struct ref *ref; |
| 362 | struct string_list_item *item; |
| 363 | char *signing_key_id = get_signing_key_id(); |
| 364 | char *signing_key = get_signing_key(); |
| 365 | const char *cp, *np; |
| 366 | struct strbuf cert = STRBUF_INIT; |
| 367 | int update_seen = 0; |
| 368 | |
| 369 | strbuf_addstr(&cert, "certificate version 0.1\n"); |
| 370 | strbuf_addf(&cert, "pusher %s ", signing_key_id); |
| 371 | datestamp(&cert); |
| 372 | strbuf_addch(&cert, '\n'); |
| 373 | if (args->url && *args->url) { |
| 374 | char *anon_url = transport_anonymize_url(args->url); |
| 375 | strbuf_addf(&cert, "pushee %s\n", anon_url); |
| 376 | free(anon_url); |
| 377 | } |
| 378 | if (push_cert_nonce[0]) |
| 379 | strbuf_addf(&cert, "nonce %s\n", push_cert_nonce); |
| 380 | if (args->push_options) |
| 381 | for_each_string_list_item(item, args->push_options) |
| 382 | strbuf_addf(&cert, "push-option %s\n", item->string); |
| 383 | strbuf_addstr(&cert, "\n"); |
| 384 | |
| 385 | for (ref = remote_refs; ref; ref = ref->next) { |
| 386 | if (check_to_send_update(ref, args) < 0) |
| 387 | continue; |
| 388 | update_seen = 1; |
| 389 | strbuf_addf(&cert, "%s %s %s\n", |
| 390 | oid_to_hex(&ref->old_oid), |
| 391 | oid_to_hex(&ref->new_oid), |
| 392 | ref->name); |
| 393 | } |
| 394 | if (!update_seen) |
| 395 | goto free_return; |
| 396 | |
| 397 | if (sign_buffer(&cert, &cert, signing_key, 0)) |
| 398 | die(_("failed to sign the push certificate")); |
| 399 | |
| 400 | packet_buf_write(req_buf, "push-cert%c%s", 0, cap_string); |
| 401 | for (cp = cert.buf; cp < cert.buf + cert.len; cp = np) { |
| 402 | np = next_line(cp, cert.buf + cert.len - cp); |
| 403 | packet_buf_write(req_buf, |
| 404 | "%.*s", (int)(np - cp), cp); |
| 405 | } |
| 406 | packet_buf_write(req_buf, "push-cert-end\n"); |
| 407 | |
| 408 | free_return: |
| 409 | free(signing_key_id); |
| 410 | free(signing_key); |
| 411 | strbuf_release(&cert); |
| 412 | return update_seen; |
| 413 | } |
| 414 | |
| 415 | #define NONCE_LEN_LIMIT 256 |
| 416 | |
| 417 | static void reject_invalid_nonce(const char *nonce, int len) |
| 418 | { |
| 419 | int i = 0; |
| 420 | |
| 421 | if (NONCE_LEN_LIMIT <= len) |
| 422 | die("the receiving end asked to sign an invalid nonce <%.*s>", |
| 423 | len, nonce); |
| 424 | |
| 425 | for (i = 0; i < len; i++) { |
| 426 | int ch = nonce[i] & 0xFF; |
| 427 | if (isalnum(ch) || |
| 428 | ch == '-' || ch == '.' || |
| 429 | ch == '/' || ch == '+' || |
| 430 | ch == '=' || ch == '_') |
| 431 | continue; |
| 432 | die("the receiving end asked to sign an invalid nonce <%.*s>", |
| 433 | len, nonce); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | static void get_commons_through_negotiation(struct repository *r, |
| 438 | const char *url, |
| 439 | const struct string_list *negotiation_include, |
| 440 | const struct string_list *negotiation_restrict, |
| 441 | const struct ref *remote_refs, |
| 442 | struct oid_array *commons) |
| 443 | { |
| 444 | struct child_process child = CHILD_PROCESS_INIT; |
| 445 | const struct ref *ref; |
| 446 | int len = r->hash_algo->hexsz + 1; /* hash + NL */ |
| 447 | int nr_negotiation = 0; |
| 448 | |
| 449 | child.git_cmd = 1; |
| 450 | child.no_stdin = 1; |
| 451 | child.out = -1; |
| 452 | strvec_pushl(&child.args, "fetch", "--negotiate-only", NULL); |
| 453 | |
| 454 | if (negotiation_restrict && negotiation_restrict->nr) { |
| 455 | struct string_list_item *item; |
| 456 | for_each_string_list_item(item, negotiation_restrict) |
| 457 | strvec_pushf(&child.args, "--negotiation-restrict=%s", |
| 458 | item->string); |
| 459 | nr_negotiation = negotiation_restrict->nr; |
| 460 | } else { |
| 461 | for (ref = remote_refs; ref; ref = ref->next) { |
| 462 | if (!is_null_oid(&ref->new_oid)) { |
| 463 | strvec_pushf(&child.args, "--negotiation-restrict=%s", |
| 464 | oid_to_hex(&ref->new_oid)); |
| 465 | nr_negotiation++; |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | if (negotiation_include && negotiation_include->nr) { |
| 471 | struct string_list_item *item; |
| 472 | for_each_string_list_item(item, negotiation_include) |
| 473 | strvec_pushf(&child.args, "--negotiation-include=%s", |
| 474 | item->string); |
| 475 | nr_negotiation += negotiation_include->nr; |
| 476 | } |
| 477 | |
| 478 | strvec_push(&child.args, url); |
| 479 | |
| 480 | if (!nr_negotiation) { |
| 481 | child_process_clear(&child); |
| 482 | return; |
| 483 | } |
| 484 | |
| 485 | if (start_command(&child)) |
| 486 | die(_("send-pack: unable to fork off fetch subprocess")); |
| 487 | |
| 488 | do { |
| 489 | char hex_hash[GIT_MAX_HEXSZ + 1]; |
| 490 | int read_len = read_in_full(child.out, hex_hash, len); |
| 491 | struct object_id oid; |
| 492 | const char *end; |
| 493 | |
| 494 | if (!read_len) |
| 495 | break; |
| 496 | if (read_len != len) |
| 497 | die("invalid length read %d", read_len); |
| 498 | if (parse_oid_hex_algop(hex_hash, &oid, &end, r->hash_algo) || *end != '\n') |
| 499 | die("invalid hash"); |
| 500 | oid_array_append(commons, &oid); |
| 501 | } while (1); |
| 502 | |
| 503 | if (finish_command(&child)) { |
| 504 | /* |
| 505 | * The information that push negotiation provides is useful but |
| 506 | * not mandatory. |
| 507 | */ |
| 508 | warning(_("push negotiation failed; proceeding anyway with push")); |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | int send_pack(struct repository *r, |
| 513 | struct send_pack_args *args, |
| 514 | int fd[], struct child_process *conn, |
| 515 | struct ref *remote_refs, |
| 516 | struct oid_array *extra_have) |
| 517 | { |
| 518 | struct oid_array commons = OID_ARRAY_INIT; |
| 519 | int in = fd[0]; |
| 520 | int out = fd[1]; |
| 521 | struct strbuf req_buf = STRBUF_INIT; |
| 522 | struct strbuf cap_buf = STRBUF_INIT; |
| 523 | struct ref *ref; |
| 524 | int need_pack_data = 0; |
| 525 | int allow_deleting_refs = 0; |
| 526 | int status_report = 0; |
| 527 | int use_sideband = 0; |
| 528 | int quiet_supported = 0; |
| 529 | int agent_supported = 0; |
| 530 | int advertise_sid = 0; |
| 531 | int push_negotiate = 0; |
| 532 | int use_atomic = 0; |
| 533 | int atomic_supported = 0; |
| 534 | int use_push_options = 0; |
| 535 | int push_options_supported = 0; |
| 536 | int object_format_supported = 0; |
| 537 | unsigned cmds_sent = 0; |
| 538 | int ret; |
| 539 | struct async demux; |
| 540 | char *push_cert_nonce = NULL; |
| 541 | struct packet_reader reader; |
| 542 | int use_bitmaps; |
| 543 | |
| 544 | if (!remote_refs) { |
| 545 | fprintf(stderr, "No refs in common and none specified; doing nothing.\n" |
| 546 | "Perhaps you should specify a branch.\n"); |
| 547 | ret = 0; |
| 548 | goto out; |
| 549 | } |
| 550 | |
| 551 | repo_config_get_bool(r, "push.negotiate", &push_negotiate); |
| 552 | if (push_negotiate) { |
| 553 | trace2_region_enter("send_pack", "push_negotiate", r); |
| 554 | get_commons_through_negotiation(r, args->url, |
| 555 | args->negotiation_include, |
| 556 | args->negotiation_restrict, |
| 557 | remote_refs, &commons); |
| 558 | trace2_region_leave("send_pack", "push_negotiate", r); |
| 559 | } |
| 560 | |
| 561 | if (!repo_config_get_bool(r, "push.usebitmaps", &use_bitmaps)) |
| 562 | args->disable_bitmaps = !use_bitmaps; |
| 563 | |
| 564 | repo_config_get_bool(r, "transfer.advertisesid", &advertise_sid); |
| 565 | |
| 566 | /* Does the other end support the reporting? */ |
| 567 | if (server_supports("report-status-v2")) |
| 568 | status_report = 2; |
| 569 | else if (server_supports("report-status")) |
| 570 | status_report = 1; |
| 571 | if (server_supports("delete-refs")) |
| 572 | allow_deleting_refs = 1; |
| 573 | if (server_supports("ofs-delta")) |
| 574 | args->use_ofs_delta = 1; |
| 575 | if (server_supports("no-ref-delta")) |
| 576 | args->no_ref_delta = 1; |
| 577 | if (server_supports("side-band-64k")) |
| 578 | use_sideband = 1; |
| 579 | if (server_supports("quiet")) |
| 580 | quiet_supported = 1; |
| 581 | if (server_supports("agent")) |
| 582 | agent_supported = 1; |
| 583 | if (!server_supports("session-id")) |
| 584 | advertise_sid = 0; |
| 585 | if (server_supports("no-thin")) |
| 586 | args->use_thin_pack = 0; |
| 587 | if (server_supports("atomic")) |
| 588 | atomic_supported = 1; |
| 589 | if (server_supports("push-options")) |
| 590 | push_options_supported = 1; |
| 591 | |
| 592 | if (!server_supports_hash(r->hash_algo->name, &object_format_supported)) |
| 593 | die(_("the receiving end does not support this repository's hash algorithm")); |
| 594 | |
| 595 | if (args->push_cert != SEND_PACK_PUSH_CERT_NEVER) { |
| 596 | size_t len; |
| 597 | const char *nonce = server_feature_value("push-cert", &len); |
| 598 | |
| 599 | if (nonce) { |
| 600 | reject_invalid_nonce(nonce, len); |
| 601 | push_cert_nonce = xmemdupz(nonce, len); |
| 602 | } else if (args->push_cert == SEND_PACK_PUSH_CERT_ALWAYS) { |
| 603 | die(_("the receiving end does not support --signed push")); |
| 604 | } else if (args->push_cert == SEND_PACK_PUSH_CERT_IF_ASKED) { |
| 605 | warning(_("not sending a push certificate since the" |
| 606 | " receiving end does not support --signed" |
| 607 | " push")); |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | if (args->atomic && !atomic_supported) |
| 612 | die(_("the receiving end does not support --atomic push")); |
| 613 | |
| 614 | use_atomic = atomic_supported && args->atomic; |
| 615 | |
| 616 | if (args->push_options && !push_options_supported) |
| 617 | die(_("the receiving end does not support push options")); |
| 618 | |
| 619 | use_push_options = push_options_supported && args->push_options; |
| 620 | |
| 621 | if (status_report == 1) |
| 622 | strbuf_addstr(&cap_buf, " report-status"); |
| 623 | else if (status_report == 2) |
| 624 | strbuf_addstr(&cap_buf, " report-status-v2"); |
| 625 | if (use_sideband) |
| 626 | strbuf_addstr(&cap_buf, " side-band-64k"); |
| 627 | if (quiet_supported && (args->quiet || !args->progress)) |
| 628 | strbuf_addstr(&cap_buf, " quiet"); |
| 629 | if (use_atomic) |
| 630 | strbuf_addstr(&cap_buf, " atomic"); |
| 631 | if (use_push_options) |
| 632 | strbuf_addstr(&cap_buf, " push-options"); |
| 633 | if (object_format_supported) |
| 634 | strbuf_addf(&cap_buf, " object-format=%s", r->hash_algo->name); |
| 635 | if (agent_supported) |
| 636 | strbuf_addf(&cap_buf, " agent=%s", git_user_agent_sanitized()); |
| 637 | if (advertise_sid) |
| 638 | strbuf_addf(&cap_buf, " session-id=%s", trace2_session_id()); |
| 639 | |
| 640 | /* |
| 641 | * NEEDSWORK: why does delete-refs have to be so specific to |
| 642 | * send-pack machinery that set_ref_status_for_push() cannot |
| 643 | * set this bit for us??? |
| 644 | */ |
| 645 | for (ref = remote_refs; ref; ref = ref->next) |
| 646 | if (ref->deletion && !allow_deleting_refs) |
| 647 | ref->status = REF_STATUS_REJECT_NODELETE; |
| 648 | |
| 649 | /* |
| 650 | * Clear the status for each ref and see if we need to send |
| 651 | * the pack data. |
| 652 | */ |
| 653 | for (ref = remote_refs; ref; ref = ref->next) { |
| 654 | switch (check_to_send_update(ref, args)) { |
| 655 | case 0: /* no error */ |
| 656 | break; |
| 657 | case CHECK_REF_STATUS_REJECTED: |
| 658 | /* |
| 659 | * When we know the server would reject a ref update if |
| 660 | * we were to send it and we're trying to send the refs |
| 661 | * atomically, abort the whole operation. |
| 662 | */ |
| 663 | if (use_atomic) { |
| 664 | reject_atomic_push(remote_refs, args->send_mirror); |
| 665 | error("atomic push failed for ref %s. status: %d", |
| 666 | ref->name, ref->status); |
| 667 | ret = ERROR_SEND_PACK_BAD_REF_STATUS; |
| 668 | packet_flush(out); |
| 669 | goto out; |
| 670 | } |
| 671 | /* else fallthrough */ |
| 672 | default: |
| 673 | continue; |
| 674 | } |
| 675 | if (!ref->deletion) |
| 676 | need_pack_data = 1; |
| 677 | |
| 678 | if (args->dry_run || !status_report) |
| 679 | ref->status = REF_STATUS_OK; |
| 680 | else |
| 681 | ref->status = REF_STATUS_EXPECTING_REPORT; |
| 682 | } |
| 683 | |
| 684 | if (!args->dry_run) |
| 685 | advertise_shallow_grafts_buf(r, &req_buf); |
| 686 | |
| 687 | /* |
| 688 | * Finally, tell the other end! |
| 689 | */ |
| 690 | if (!args->dry_run && push_cert_nonce) { |
| 691 | cmds_sent = generate_push_cert(&req_buf, remote_refs, args, |
| 692 | cap_buf.buf, push_cert_nonce); |
| 693 | trace2_printf("Generated push certificate"); |
| 694 | } else if (!args->dry_run) { |
| 695 | for (ref = remote_refs; ref; ref = ref->next) { |
| 696 | char *old_hex, *new_hex; |
| 697 | |
| 698 | if (check_to_send_update(ref, args) < 0) |
| 699 | continue; |
| 700 | |
| 701 | old_hex = oid_to_hex(&ref->old_oid); |
| 702 | new_hex = oid_to_hex(&ref->new_oid); |
| 703 | if (!cmds_sent) { |
| 704 | packet_buf_write(&req_buf, |
| 705 | "%s %s %s%c%s", |
| 706 | old_hex, new_hex, ref->name, 0, |
| 707 | cap_buf.buf); |
| 708 | cmds_sent = 1; |
| 709 | } else { |
| 710 | packet_buf_write(&req_buf, "%s %s %s", |
| 711 | old_hex, new_hex, ref->name); |
| 712 | } |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | if (use_push_options) { |
| 717 | struct string_list_item *item; |
| 718 | |
| 719 | packet_buf_flush(&req_buf); |
| 720 | for_each_string_list_item(item, args->push_options) |
| 721 | packet_buf_write(&req_buf, "%s", item->string); |
| 722 | } |
| 723 | |
| 724 | if (args->stateless_rpc) { |
| 725 | if (!args->dry_run && (cmds_sent || is_repository_shallow(r))) { |
| 726 | packet_buf_flush(&req_buf); |
| 727 | send_sideband(out, -1, req_buf.buf, req_buf.len, LARGE_PACKET_MAX); |
| 728 | } |
| 729 | } else { |
| 730 | write_or_die(out, req_buf.buf, req_buf.len); |
| 731 | packet_flush(out); |
| 732 | } |
| 733 | |
| 734 | if (use_sideband && cmds_sent) { |
| 735 | memset(&demux, 0, sizeof(demux)); |
| 736 | demux.proc = sideband_demux; |
| 737 | demux.data = fd; |
| 738 | demux.out = -1; |
| 739 | demux.isolate_sigpipe = 1; |
| 740 | if (start_async(&demux)) |
| 741 | die("send-pack: unable to fork off sideband demultiplexer"); |
| 742 | in = demux.out; |
| 743 | } |
| 744 | |
| 745 | packet_reader_init(&reader, in, NULL, 0, |
| 746 | PACKET_READ_CHOMP_NEWLINE | |
| 747 | PACKET_READ_DIE_ON_ERR_PACKET); |
| 748 | |
| 749 | if (need_pack_data && cmds_sent) { |
| 750 | if (pack_objects(r, out, remote_refs, extra_have, &commons, args) < 0) { |
| 751 | if (args->stateless_rpc) |
| 752 | close(out); |
| 753 | if (git_connection_is_socket(conn)) |
| 754 | shutdown(fd[0], SHUT_WR); |
| 755 | |
| 756 | /* |
| 757 | * Do not even bother with the return value; we know we |
| 758 | * are failing, and just want the error() side effects, |
| 759 | * as well as marking refs with their remote status (if |
| 760 | * we get one). |
| 761 | */ |
| 762 | if (status_report) |
| 763 | receive_status(r, &reader, remote_refs); |
| 764 | |
| 765 | if (use_sideband) { |
| 766 | close(demux.out); |
| 767 | finish_async(&demux); |
| 768 | } |
| 769 | fd[1] = -1; |
| 770 | |
| 771 | ret = -1; |
| 772 | goto out; |
| 773 | } |
| 774 | if (!args->stateless_rpc) |
| 775 | /* Closed by pack_objects() via start_command() */ |
| 776 | fd[1] = -1; |
| 777 | } |
| 778 | if (args->stateless_rpc && cmds_sent) |
| 779 | packet_flush(out); |
| 780 | |
| 781 | if (status_report && cmds_sent) |
| 782 | ret = receive_status(r, &reader, remote_refs); |
| 783 | else |
| 784 | ret = 0; |
| 785 | if (args->stateless_rpc) |
| 786 | packet_flush(out); |
| 787 | |
| 788 | if (use_sideband && cmds_sent) { |
| 789 | close(demux.out); |
| 790 | if (finish_async(&demux)) { |
| 791 | error("error in sideband demultiplexer"); |
| 792 | ret = -1; |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | if (ret < 0) |
| 797 | goto out; |
| 798 | |
| 799 | for (ref = remote_refs; ref; ref = ref->next) { |
| 800 | switch (ref->status) { |
| 801 | case REF_STATUS_NONE: |
| 802 | case REF_STATUS_UPTODATE: |
| 803 | case REF_STATUS_OK: |
| 804 | break; |
| 805 | default: |
| 806 | ret = ERROR_SEND_PACK_BAD_REF_STATUS; |
| 807 | goto out; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | ret = 0; |
| 812 | |
| 813 | out: |
| 814 | oid_array_clear(&commons); |
| 815 | strbuf_release(&req_buf); |
| 816 | strbuf_release(&cap_buf); |
| 817 | free(push_cert_nonce); |
| 818 | return ret; |
| 819 | } |