Sync with 2.44.1
[git/gitster.git] / send-pack.c
blob37f59d4f66bbc2b21bf08288a75de4a159b24625
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 "object-store-ll.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"
23 int option_parse_push_signed(const struct option *opt,
24 const char *arg, int unset)
26 if (unset) {
27 *(int *)(opt->value) = SEND_PACK_PUSH_CERT_NEVER;
28 return 0;
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;
38 if (!strcasecmp("if-asked", arg)) {
39 *(int *)(opt->value) = SEND_PACK_PUSH_CERT_IF_ASKED;
40 return 0;
42 die("bad %s argument: %s", opt->long_name, arg);
45 static void feed_object(const struct object_id *oid, FILE *fh, int negative)
47 if (negative &&
48 !repo_has_object_file_with_flags(the_repository, oid,
49 OBJECT_INFO_SKIP_FETCH_OBJECT |
50 OBJECT_INFO_QUICK))
51 return;
53 if (negative)
54 putc('^', fh);
55 fputs(oid_to_hex(oid), fh);
56 putc('\n', fh);
60 * Make a pack stream and spit it out into file descriptor fd
62 static int pack_objects(int fd, struct ref *refs, struct oid_array *advertised,
63 struct oid_array *negotiated,
64 struct send_pack_args *args)
67 * The child becomes pack-objects --revs; we feed
68 * the revision parameters to it via its stdin and
69 * let its stdout go back to the other end.
71 struct child_process po = CHILD_PROCESS_INIT;
72 FILE *po_in;
73 int i;
74 int rc;
76 strvec_push(&po.args, "pack-objects");
77 strvec_push(&po.args, "--all-progress-implied");
78 strvec_push(&po.args, "--revs");
79 strvec_push(&po.args, "--stdout");
80 if (args->use_thin_pack)
81 strvec_push(&po.args, "--thin");
82 if (args->use_ofs_delta)
83 strvec_push(&po.args, "--delta-base-offset");
84 if (args->quiet || !args->progress)
85 strvec_push(&po.args, "-q");
86 if (args->progress)
87 strvec_push(&po.args, "--progress");
88 if (is_repository_shallow(the_repository))
89 strvec_push(&po.args, "--shallow");
90 if (args->disable_bitmaps)
91 strvec_push(&po.args, "--no-use-bitmap-index");
92 po.in = -1;
93 po.out = args->stateless_rpc ? -1 : fd;
94 po.git_cmd = 1;
95 po.clean_on_exit = 1;
96 if (start_command(&po))
97 die_errno("git pack-objects failed");
100 * We feed the pack-objects we just spawned with revision
101 * parameters by writing to the pipe.
103 po_in = xfdopen(po.in, "w");
104 for (i = 0; i < advertised->nr; i++)
105 feed_object(&advertised->oid[i], po_in, 1);
106 for (i = 0; i < negotiated->nr; i++)
107 feed_object(&negotiated->oid[i], po_in, 1);
109 while (refs) {
110 if (!is_null_oid(&refs->old_oid))
111 feed_object(&refs->old_oid, po_in, 1);
112 if (!is_null_oid(&refs->new_oid))
113 feed_object(&refs->new_oid, po_in, 0);
114 refs = refs->next;
117 fflush(po_in);
118 if (ferror(po_in))
119 die_errno("error writing to pack-objects");
120 fclose(po_in);
122 if (args->stateless_rpc) {
123 char *buf = xmalloc(LARGE_PACKET_MAX);
124 while (1) {
125 ssize_t n = xread(po.out, buf, LARGE_PACKET_MAX);
126 if (n <= 0)
127 break;
128 send_sideband(fd, -1, buf, n, LARGE_PACKET_MAX);
130 free(buf);
131 close(po.out);
132 po.out = -1;
135 rc = finish_command(&po);
136 if (rc) {
138 * For a normal non-zero exit, we assume pack-objects wrote
139 * something useful to stderr. For death by signal, though,
140 * we should mention it to the user. The exception is SIGPIPE
141 * (141), because that's a normal occurrence if the remote end
142 * hangs up (and we'll report that by trying to read the unpack
143 * status).
145 if (rc > 128 && rc != 141)
146 error("pack-objects died of signal %d", rc - 128);
147 return -1;
149 return 0;
152 static int receive_unpack_status(struct packet_reader *reader)
154 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
155 return error(_("unexpected flush packet while reading remote unpack status"));
156 if (!skip_prefix(reader->line, "unpack ", &reader->line))
157 return error(_("unable to parse remote unpack status: %s"), reader->line);
158 if (strcmp(reader->line, "ok"))
159 return error(_("remote unpack failed: %s"), reader->line);
160 return 0;
163 static int receive_status(struct packet_reader *reader, struct ref *refs)
165 struct ref *hint;
166 int ret;
167 struct ref_push_report *report = NULL;
168 int new_report = 0;
169 int once = 0;
171 hint = NULL;
172 ret = receive_unpack_status(reader);
173 while (1) {
174 struct object_id old_oid, new_oid;
175 const char *head;
176 const char *refname;
177 char *p;
178 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
179 break;
180 head = reader->line;
181 p = strchr(head, ' ');
182 if (!p) {
183 error("invalid status line from remote: %s", reader->line);
184 ret = -1;
185 break;
187 *p++ = '\0';
189 if (!strcmp(head, "option")) {
190 const char *key, *val;
192 if (!hint || !(report || new_report)) {
193 if (!once++)
194 error("'option' without a matching 'ok/ng' directive");
195 ret = -1;
196 continue;
198 if (new_report) {
199 if (!hint->report) {
200 CALLOC_ARRAY(hint->report, 1);
201 report = hint->report;
202 } else {
203 report = hint->report;
204 while (report->next)
205 report = report->next;
206 CALLOC_ARRAY(report->next, 1);
207 report = report->next;
209 new_report = 0;
211 key = p;
212 p = strchr(key, ' ');
213 if (p)
214 *p++ = '\0';
215 val = p;
216 if (!strcmp(key, "refname"))
217 report->ref_name = xstrdup_or_null(val);
218 else if (!strcmp(key, "old-oid") && val &&
219 !parse_oid_hex(val, &old_oid, &val))
220 report->old_oid = oiddup(&old_oid);
221 else if (!strcmp(key, "new-oid") && val &&
222 !parse_oid_hex(val, &new_oid, &val))
223 report->new_oid = oiddup(&new_oid);
224 else if (!strcmp(key, "forced-update"))
225 report->forced_update = 1;
226 continue;
229 report = NULL;
230 new_report = 0;
231 if (strcmp(head, "ok") && strcmp(head, "ng")) {
232 error("invalid ref status from remote: %s", head);
233 ret = -1;
234 break;
236 refname = p;
237 p = strchr(refname, ' ');
238 if (p)
239 *p++ = '\0';
240 /* first try searching at our hint, falling back to all refs */
241 if (hint)
242 hint = find_ref_by_name(hint, refname);
243 if (!hint)
244 hint = find_ref_by_name(refs, refname);
245 if (!hint) {
246 warning("remote reported status on unknown ref: %s",
247 refname);
248 continue;
250 if (hint->status != REF_STATUS_EXPECTING_REPORT &&
251 hint->status != REF_STATUS_OK &&
252 hint->status != REF_STATUS_REMOTE_REJECT) {
253 warning("remote reported status on unexpected ref: %s",
254 refname);
255 continue;
257 if (!strcmp(head, "ng")) {
258 hint->status = REF_STATUS_REMOTE_REJECT;
259 if (p)
260 hint->remote_status = xstrdup(p);
261 else
262 hint->remote_status = "failed";
263 } else {
264 hint->status = REF_STATUS_OK;
265 hint->remote_status = xstrdup_or_null(p);
266 new_report = 1;
269 return ret;
272 static int sideband_demux(int in UNUSED, int out, void *data)
274 int *fd = data, ret;
275 if (async_with_fork())
276 close(fd[1]);
277 ret = recv_sideband("send-pack", fd[0], out);
278 close(out);
279 return ret;
282 static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb)
284 struct strbuf *sb = cb;
285 if (graft->nr_parent == -1)
286 packet_buf_write(sb, "shallow %s\n", oid_to_hex(&graft->oid));
287 return 0;
290 static void advertise_shallow_grafts_buf(struct strbuf *sb)
292 if (!is_repository_shallow(the_repository))
293 return;
294 for_each_commit_graft(advertise_shallow_grafts_cb, sb);
297 #define CHECK_REF_NO_PUSH -1
298 #define CHECK_REF_STATUS_REJECTED -2
299 #define CHECK_REF_UPTODATE -3
300 static int check_to_send_update(const struct ref *ref, const struct send_pack_args *args)
302 if (!ref->peer_ref && !args->send_mirror)
303 return CHECK_REF_NO_PUSH;
305 /* Check for statuses set by set_ref_status_for_push() */
306 switch (ref->status) {
307 case REF_STATUS_REJECT_NONFASTFORWARD:
308 case REF_STATUS_REJECT_ALREADY_EXISTS:
309 case REF_STATUS_REJECT_FETCH_FIRST:
310 case REF_STATUS_REJECT_NEEDS_FORCE:
311 case REF_STATUS_REJECT_STALE:
312 case REF_STATUS_REJECT_REMOTE_UPDATED:
313 case REF_STATUS_REJECT_NODELETE:
314 return CHECK_REF_STATUS_REJECTED;
315 case REF_STATUS_UPTODATE:
316 return CHECK_REF_UPTODATE;
318 default:
319 case REF_STATUS_EXPECTING_REPORT:
320 /* already passed checks on the local side */
321 case REF_STATUS_OK:
322 /* of course this is OK */
323 return 0;
328 * the beginning of the next line, or the end of buffer.
330 * NEEDSWORK: perhaps move this to git-compat-util.h or somewhere and
331 * convert many similar uses found by "git grep -A4 memchr".
333 static const char *next_line(const char *line, size_t len)
335 const char *nl = memchr(line, '\n', len);
336 if (!nl)
337 return line + len; /* incomplete line */
338 return nl + 1;
341 static int generate_push_cert(struct strbuf *req_buf,
342 const struct ref *remote_refs,
343 struct send_pack_args *args,
344 const char *cap_string,
345 const char *push_cert_nonce)
347 const struct ref *ref;
348 struct string_list_item *item;
349 char *signing_key_id = xstrdup(get_signing_key_id());
350 const char *cp, *np;
351 struct strbuf cert = STRBUF_INIT;
352 int update_seen = 0;
354 strbuf_addstr(&cert, "certificate version 0.1\n");
355 strbuf_addf(&cert, "pusher %s ", signing_key_id);
356 datestamp(&cert);
357 strbuf_addch(&cert, '\n');
358 if (args->url && *args->url) {
359 char *anon_url = transport_anonymize_url(args->url);
360 strbuf_addf(&cert, "pushee %s\n", anon_url);
361 free(anon_url);
363 if (push_cert_nonce[0])
364 strbuf_addf(&cert, "nonce %s\n", push_cert_nonce);
365 if (args->push_options)
366 for_each_string_list_item(item, args->push_options)
367 strbuf_addf(&cert, "push-option %s\n", item->string);
368 strbuf_addstr(&cert, "\n");
370 for (ref = remote_refs; ref; ref = ref->next) {
371 if (check_to_send_update(ref, args) < 0)
372 continue;
373 update_seen = 1;
374 strbuf_addf(&cert, "%s %s %s\n",
375 oid_to_hex(&ref->old_oid),
376 oid_to_hex(&ref->new_oid),
377 ref->name);
379 if (!update_seen)
380 goto free_return;
382 if (sign_buffer(&cert, &cert, get_signing_key()))
383 die(_("failed to sign the push certificate"));
385 packet_buf_write(req_buf, "push-cert%c%s", 0, cap_string);
386 for (cp = cert.buf; cp < cert.buf + cert.len; cp = np) {
387 np = next_line(cp, cert.buf + cert.len - cp);
388 packet_buf_write(req_buf,
389 "%.*s", (int)(np - cp), cp);
391 packet_buf_write(req_buf, "push-cert-end\n");
393 free_return:
394 free(signing_key_id);
395 strbuf_release(&cert);
396 return update_seen;
399 #define NONCE_LEN_LIMIT 256
401 static void reject_invalid_nonce(const char *nonce, int len)
403 int i = 0;
405 if (NONCE_LEN_LIMIT <= len)
406 die("the receiving end asked to sign an invalid nonce <%.*s>",
407 len, nonce);
409 for (i = 0; i < len; i++) {
410 int ch = nonce[i] & 0xFF;
411 if (isalnum(ch) ||
412 ch == '-' || ch == '.' ||
413 ch == '/' || ch == '+' ||
414 ch == '=' || ch == '_')
415 continue;
416 die("the receiving end asked to sign an invalid nonce <%.*s>",
417 len, nonce);
421 static void get_commons_through_negotiation(const char *url,
422 const struct ref *remote_refs,
423 struct oid_array *commons)
425 struct child_process child = CHILD_PROCESS_INIT;
426 const struct ref *ref;
427 int len = the_hash_algo->hexsz + 1; /* hash + NL */
429 child.git_cmd = 1;
430 child.no_stdin = 1;
431 child.out = -1;
432 strvec_pushl(&child.args, "fetch", "--negotiate-only", NULL);
433 for (ref = remote_refs; ref; ref = ref->next) {
434 if (!is_null_oid(&ref->new_oid))
435 strvec_pushf(&child.args, "--negotiation-tip=%s", oid_to_hex(&ref->new_oid));
437 strvec_push(&child.args, url);
439 if (start_command(&child))
440 die(_("send-pack: unable to fork off fetch subprocess"));
442 do {
443 char hex_hash[GIT_MAX_HEXSZ + 1];
444 int read_len = read_in_full(child.out, hex_hash, len);
445 struct object_id oid;
446 const char *end;
448 if (!read_len)
449 break;
450 if (read_len != len)
451 die("invalid length read %d", read_len);
452 if (parse_oid_hex(hex_hash, &oid, &end) || *end != '\n')
453 die("invalid hash");
454 oid_array_append(commons, &oid);
455 } while (1);
457 if (finish_command(&child)) {
459 * The information that push negotiation provides is useful but
460 * not mandatory.
462 warning(_("push negotiation failed; proceeding anyway with push"));
466 int send_pack(struct send_pack_args *args,
467 int fd[], struct child_process *conn,
468 struct ref *remote_refs,
469 struct oid_array *extra_have)
471 struct oid_array commons = OID_ARRAY_INIT;
472 int in = fd[0];
473 int out = fd[1];
474 struct strbuf req_buf = STRBUF_INIT;
475 struct strbuf cap_buf = STRBUF_INIT;
476 struct ref *ref;
477 int need_pack_data = 0;
478 int allow_deleting_refs = 0;
479 int status_report = 0;
480 int use_sideband = 0;
481 int quiet_supported = 0;
482 int agent_supported = 0;
483 int advertise_sid = 0;
484 int push_negotiate = 0;
485 int use_atomic = 0;
486 int atomic_supported = 0;
487 int use_push_options = 0;
488 int push_options_supported = 0;
489 int object_format_supported = 0;
490 unsigned cmds_sent = 0;
491 int ret;
492 struct async demux;
493 const char *push_cert_nonce = NULL;
494 struct packet_reader reader;
495 int use_bitmaps;
497 if (!remote_refs) {
498 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
499 "Perhaps you should specify a branch.\n");
500 return 0;
503 git_config_get_bool("push.negotiate", &push_negotiate);
504 if (push_negotiate)
505 get_commons_through_negotiation(args->url, remote_refs, &commons);
507 if (!git_config_get_bool("push.usebitmaps", &use_bitmaps))
508 args->disable_bitmaps = !use_bitmaps;
510 git_config_get_bool("transfer.advertisesid", &advertise_sid);
512 /* Does the other end support the reporting? */
513 if (server_supports("report-status-v2"))
514 status_report = 2;
515 else if (server_supports("report-status"))
516 status_report = 1;
517 if (server_supports("delete-refs"))
518 allow_deleting_refs = 1;
519 if (server_supports("ofs-delta"))
520 args->use_ofs_delta = 1;
521 if (server_supports("side-band-64k"))
522 use_sideband = 1;
523 if (server_supports("quiet"))
524 quiet_supported = 1;
525 if (server_supports("agent"))
526 agent_supported = 1;
527 if (!server_supports("session-id"))
528 advertise_sid = 0;
529 if (server_supports("no-thin"))
530 args->use_thin_pack = 0;
531 if (server_supports("atomic"))
532 atomic_supported = 1;
533 if (server_supports("push-options"))
534 push_options_supported = 1;
536 if (!server_supports_hash(the_hash_algo->name, &object_format_supported))
537 die(_("the receiving end does not support this repository's hash algorithm"));
539 if (args->push_cert != SEND_PACK_PUSH_CERT_NEVER) {
540 size_t len;
541 push_cert_nonce = server_feature_value("push-cert", &len);
542 if (push_cert_nonce) {
543 reject_invalid_nonce(push_cert_nonce, len);
544 push_cert_nonce = xmemdupz(push_cert_nonce, len);
545 } else if (args->push_cert == SEND_PACK_PUSH_CERT_ALWAYS) {
546 die(_("the receiving end does not support --signed push"));
547 } else if (args->push_cert == SEND_PACK_PUSH_CERT_IF_ASKED) {
548 warning(_("not sending a push certificate since the"
549 " receiving end does not support --signed"
550 " push"));
554 if (args->atomic && !atomic_supported)
555 die(_("the receiving end does not support --atomic push"));
557 use_atomic = atomic_supported && args->atomic;
559 if (args->push_options && !push_options_supported)
560 die(_("the receiving end does not support push options"));
562 use_push_options = push_options_supported && args->push_options;
564 if (status_report == 1)
565 strbuf_addstr(&cap_buf, " report-status");
566 else if (status_report == 2)
567 strbuf_addstr(&cap_buf, " report-status-v2");
568 if (use_sideband)
569 strbuf_addstr(&cap_buf, " side-band-64k");
570 if (quiet_supported && (args->quiet || !args->progress))
571 strbuf_addstr(&cap_buf, " quiet");
572 if (use_atomic)
573 strbuf_addstr(&cap_buf, " atomic");
574 if (use_push_options)
575 strbuf_addstr(&cap_buf, " push-options");
576 if (object_format_supported)
577 strbuf_addf(&cap_buf, " object-format=%s", the_hash_algo->name);
578 if (agent_supported)
579 strbuf_addf(&cap_buf, " agent=%s", git_user_agent_sanitized());
580 if (advertise_sid)
581 strbuf_addf(&cap_buf, " session-id=%s", trace2_session_id());
584 * NEEDSWORK: why does delete-refs have to be so specific to
585 * send-pack machinery that set_ref_status_for_push() cannot
586 * set this bit for us???
588 for (ref = remote_refs; ref; ref = ref->next)
589 if (ref->deletion && !allow_deleting_refs)
590 ref->status = REF_STATUS_REJECT_NODELETE;
593 * Clear the status for each ref and see if we need to send
594 * the pack data.
596 for (ref = remote_refs; ref; ref = ref->next) {
597 switch (check_to_send_update(ref, args)) {
598 case 0: /* no error */
599 break;
600 case CHECK_REF_STATUS_REJECTED:
602 * When we know the server would reject a ref update if
603 * we were to send it and we're trying to send the refs
604 * atomically, abort the whole operation.
606 if (use_atomic) {
607 strbuf_release(&req_buf);
608 strbuf_release(&cap_buf);
609 reject_atomic_push(remote_refs, args->send_mirror);
610 error("atomic push failed for ref %s. status: %d\n",
611 ref->name, ref->status);
612 return args->porcelain ? 0 : -1;
614 /* else fallthrough */
615 default:
616 continue;
618 if (!ref->deletion)
619 need_pack_data = 1;
621 if (args->dry_run || !status_report)
622 ref->status = REF_STATUS_OK;
623 else
624 ref->status = REF_STATUS_EXPECTING_REPORT;
627 if (!args->dry_run)
628 advertise_shallow_grafts_buf(&req_buf);
631 * Finally, tell the other end!
633 if (!args->dry_run && push_cert_nonce)
634 cmds_sent = generate_push_cert(&req_buf, remote_refs, args,
635 cap_buf.buf, push_cert_nonce);
636 else if (!args->dry_run)
637 for (ref = remote_refs; ref; ref = ref->next) {
638 char *old_hex, *new_hex;
640 if (check_to_send_update(ref, args) < 0)
641 continue;
643 old_hex = oid_to_hex(&ref->old_oid);
644 new_hex = oid_to_hex(&ref->new_oid);
645 if (!cmds_sent) {
646 packet_buf_write(&req_buf,
647 "%s %s %s%c%s",
648 old_hex, new_hex, ref->name, 0,
649 cap_buf.buf);
650 cmds_sent = 1;
651 } else {
652 packet_buf_write(&req_buf, "%s %s %s",
653 old_hex, new_hex, ref->name);
657 if (use_push_options) {
658 struct string_list_item *item;
660 packet_buf_flush(&req_buf);
661 for_each_string_list_item(item, args->push_options)
662 packet_buf_write(&req_buf, "%s", item->string);
665 if (args->stateless_rpc) {
666 if (!args->dry_run && (cmds_sent || is_repository_shallow(the_repository))) {
667 packet_buf_flush(&req_buf);
668 send_sideband(out, -1, req_buf.buf, req_buf.len, LARGE_PACKET_MAX);
670 } else {
671 write_or_die(out, req_buf.buf, req_buf.len);
672 packet_flush(out);
674 strbuf_release(&req_buf);
675 strbuf_release(&cap_buf);
677 if (use_sideband && cmds_sent) {
678 memset(&demux, 0, sizeof(demux));
679 demux.proc = sideband_demux;
680 demux.data = fd;
681 demux.out = -1;
682 demux.isolate_sigpipe = 1;
683 if (start_async(&demux))
684 die("send-pack: unable to fork off sideband demultiplexer");
685 in = demux.out;
688 packet_reader_init(&reader, in, NULL, 0,
689 PACKET_READ_CHOMP_NEWLINE |
690 PACKET_READ_DIE_ON_ERR_PACKET);
692 if (need_pack_data && cmds_sent) {
693 if (pack_objects(out, remote_refs, extra_have, &commons, args) < 0) {
694 if (args->stateless_rpc)
695 close(out);
696 if (git_connection_is_socket(conn))
697 shutdown(fd[0], SHUT_WR);
700 * Do not even bother with the return value; we know we
701 * are failing, and just want the error() side effects,
702 * as well as marking refs with their remote status (if
703 * we get one).
705 if (status_report)
706 receive_status(&reader, remote_refs);
708 if (use_sideband) {
709 close(demux.out);
710 finish_async(&demux);
712 fd[1] = -1;
713 return -1;
715 if (!args->stateless_rpc)
716 /* Closed by pack_objects() via start_command() */
717 fd[1] = -1;
719 if (args->stateless_rpc && cmds_sent)
720 packet_flush(out);
722 if (status_report && cmds_sent)
723 ret = receive_status(&reader, remote_refs);
724 else
725 ret = 0;
726 if (args->stateless_rpc)
727 packet_flush(out);
729 if (use_sideband && cmds_sent) {
730 close(demux.out);
731 if (finish_async(&demux)) {
732 error("error in sideband demultiplexer");
733 ret = -1;
737 if (ret < 0)
738 return ret;
740 if (args->porcelain)
741 return 0;
743 for (ref = remote_refs; ref; ref = ref->next) {
744 switch (ref->status) {
745 case REF_STATUS_NONE:
746 case REF_STATUS_UPTODATE:
747 case REF_STATUS_OK:
748 break;
749 default:
750 return -1;
753 return 0;