Merge branch 'tz/test-fix-pthreads-prereq'
[alt-git.git] / send-pack.c
blob0c7ccaef6805f1cffd5aa6246c885eb54fd258e5
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 "refs.h"
8 #include "object-store.h"
9 #include "pkt-line.h"
10 #include "sideband.h"
11 #include "run-command.h"
12 #include "remote.h"
13 #include "connect.h"
14 #include "send-pack.h"
15 #include "quote.h"
16 #include "transport.h"
17 #include "version.h"
18 #include "wrapper.h"
19 #include "oid-array.h"
20 #include "gpg-interface.h"
21 #include "shallow.h"
22 #include "parse-options.h"
23 #include "trace2.h"
24 #include "write-or-die.h"
26 int option_parse_push_signed(const struct option *opt,
27 const char *arg, int unset)
29 if (unset) {
30 *(int *)(opt->value) = SEND_PACK_PUSH_CERT_NEVER;
31 return 0;
33 switch (git_parse_maybe_bool(arg)) {
34 case 1:
35 *(int *)(opt->value) = SEND_PACK_PUSH_CERT_ALWAYS;
36 return 0;
37 case 0:
38 *(int *)(opt->value) = SEND_PACK_PUSH_CERT_NEVER;
39 return 0;
41 if (!strcasecmp("if-asked", arg)) {
42 *(int *)(opt->value) = SEND_PACK_PUSH_CERT_IF_ASKED;
43 return 0;
45 die("bad %s argument: %s", opt->long_name, arg);
48 static void feed_object(const struct object_id *oid, FILE *fh, int negative)
50 if (negative &&
51 !repo_has_object_file_with_flags(the_repository, oid,
52 OBJECT_INFO_SKIP_FETCH_OBJECT |
53 OBJECT_INFO_QUICK))
54 return;
56 if (negative)
57 putc('^', fh);
58 fputs(oid_to_hex(oid), fh);
59 putc('\n', fh);
63 * Make a pack stream and spit it out into file descriptor fd
65 static int pack_objects(int fd, struct ref *refs, struct oid_array *advertised,
66 struct oid_array *negotiated,
67 struct send_pack_args *args)
70 * The child becomes pack-objects --revs; we feed
71 * the revision parameters to it via its stdin and
72 * let its stdout go back to the other end.
74 struct child_process po = CHILD_PROCESS_INIT;
75 FILE *po_in;
76 int i;
77 int rc;
79 strvec_push(&po.args, "pack-objects");
80 strvec_push(&po.args, "--all-progress-implied");
81 strvec_push(&po.args, "--revs");
82 strvec_push(&po.args, "--stdout");
83 if (args->use_thin_pack)
84 strvec_push(&po.args, "--thin");
85 if (args->use_ofs_delta)
86 strvec_push(&po.args, "--delta-base-offset");
87 if (args->quiet || !args->progress)
88 strvec_push(&po.args, "-q");
89 if (args->progress)
90 strvec_push(&po.args, "--progress");
91 if (is_repository_shallow(the_repository))
92 strvec_push(&po.args, "--shallow");
93 if (args->disable_bitmaps)
94 strvec_push(&po.args, "--no-use-bitmap-index");
95 po.in = -1;
96 po.out = args->stateless_rpc ? -1 : fd;
97 po.git_cmd = 1;
98 po.clean_on_exit = 1;
99 if (start_command(&po))
100 die_errno("git pack-objects failed");
103 * We feed the pack-objects we just spawned with revision
104 * parameters by writing to the pipe.
106 po_in = xfdopen(po.in, "w");
107 for (i = 0; i < advertised->nr; i++)
108 feed_object(&advertised->oid[i], po_in, 1);
109 for (i = 0; i < negotiated->nr; i++)
110 feed_object(&negotiated->oid[i], po_in, 1);
112 while (refs) {
113 if (!is_null_oid(&refs->old_oid))
114 feed_object(&refs->old_oid, po_in, 1);
115 if (!is_null_oid(&refs->new_oid))
116 feed_object(&refs->new_oid, po_in, 0);
117 refs = refs->next;
120 fflush(po_in);
121 if (ferror(po_in))
122 die_errno("error writing to pack-objects");
123 fclose(po_in);
125 if (args->stateless_rpc) {
126 char *buf = xmalloc(LARGE_PACKET_MAX);
127 while (1) {
128 ssize_t n = xread(po.out, buf, LARGE_PACKET_MAX);
129 if (n <= 0)
130 break;
131 send_sideband(fd, -1, buf, n, LARGE_PACKET_MAX);
133 free(buf);
134 close(po.out);
135 po.out = -1;
138 rc = finish_command(&po);
139 if (rc) {
141 * For a normal non-zero exit, we assume pack-objects wrote
142 * something useful to stderr. For death by signal, though,
143 * we should mention it to the user. The exception is SIGPIPE
144 * (141), because that's a normal occurrence if the remote end
145 * hangs up (and we'll report that by trying to read the unpack
146 * status).
148 if (rc > 128 && rc != 141)
149 error("pack-objects died of signal %d", rc - 128);
150 return -1;
152 return 0;
155 static int receive_unpack_status(struct packet_reader *reader)
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;
166 static int receive_status(struct packet_reader *reader, struct ref *refs)
168 struct ref *hint;
169 int ret;
170 struct ref_push_report *report = NULL;
171 int new_report = 0;
172 int once = 0;
174 hint = NULL;
175 ret = receive_unpack_status(reader);
176 while (1) {
177 struct object_id old_oid, new_oid;
178 const char *head;
179 const char *refname;
180 char *p;
181 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
182 break;
183 head = reader->line;
184 p = strchr(head, ' ');
185 if (!p) {
186 error("invalid status line from remote: %s", reader->line);
187 ret = -1;
188 break;
190 *p++ = '\0';
192 if (!strcmp(head, "option")) {
193 const char *key, *val;
195 if (!hint || !(report || new_report)) {
196 if (!once++)
197 error("'option' without a matching 'ok/ng' directive");
198 ret = -1;
199 continue;
201 if (new_report) {
202 if (!hint->report) {
203 CALLOC_ARRAY(hint->report, 1);
204 report = hint->report;
205 } else {
206 report = hint->report;
207 while (report->next)
208 report = report->next;
209 CALLOC_ARRAY(report->next, 1);
210 report = report->next;
212 new_report = 0;
214 key = p;
215 p = strchr(key, ' ');
216 if (p)
217 *p++ = '\0';
218 val = p;
219 if (!strcmp(key, "refname"))
220 report->ref_name = xstrdup_or_null(val);
221 else if (!strcmp(key, "old-oid") && val &&
222 !parse_oid_hex(val, &old_oid, &val))
223 report->old_oid = oiddup(&old_oid);
224 else if (!strcmp(key, "new-oid") && val &&
225 !parse_oid_hex(val, &new_oid, &val))
226 report->new_oid = oiddup(&new_oid);
227 else if (!strcmp(key, "forced-update"))
228 report->forced_update = 1;
229 continue;
232 report = NULL;
233 new_report = 0;
234 if (strcmp(head, "ok") && strcmp(head, "ng")) {
235 error("invalid ref status from remote: %s", head);
236 ret = -1;
237 break;
239 refname = p;
240 p = strchr(refname, ' ');
241 if (p)
242 *p++ = '\0';
243 /* first try searching at our hint, falling back to all refs */
244 if (hint)
245 hint = find_ref_by_name(hint, refname);
246 if (!hint)
247 hint = find_ref_by_name(refs, refname);
248 if (!hint) {
249 warning("remote reported status on unknown ref: %s",
250 refname);
251 continue;
253 if (hint->status != REF_STATUS_EXPECTING_REPORT &&
254 hint->status != REF_STATUS_OK &&
255 hint->status != REF_STATUS_REMOTE_REJECT) {
256 warning("remote reported status on unexpected ref: %s",
257 refname);
258 continue;
260 if (!strcmp(head, "ng")) {
261 hint->status = REF_STATUS_REMOTE_REJECT;
262 if (p)
263 hint->remote_status = xstrdup(p);
264 else
265 hint->remote_status = "failed";
266 } else {
267 hint->status = REF_STATUS_OK;
268 hint->remote_status = xstrdup_or_null(p);
269 new_report = 1;
272 return ret;
275 static int sideband_demux(int in UNUSED, int out, void *data)
277 int *fd = data, ret;
278 if (async_with_fork())
279 close(fd[1]);
280 ret = recv_sideband("send-pack", fd[0], out);
281 close(out);
282 return ret;
285 static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb)
287 struct strbuf *sb = cb;
288 if (graft->nr_parent == -1)
289 packet_buf_write(sb, "shallow %s\n", oid_to_hex(&graft->oid));
290 return 0;
293 static void advertise_shallow_grafts_buf(struct strbuf *sb)
295 if (!is_repository_shallow(the_repository))
296 return;
297 for_each_commit_graft(advertise_shallow_grafts_cb, sb);
300 #define CHECK_REF_NO_PUSH -1
301 #define CHECK_REF_STATUS_REJECTED -2
302 #define CHECK_REF_UPTODATE -3
303 static int check_to_send_update(const struct ref *ref, const struct send_pack_args *args)
305 if (!ref->peer_ref && !args->send_mirror)
306 return CHECK_REF_NO_PUSH;
308 /* Check for statuses set by set_ref_status_for_push() */
309 switch (ref->status) {
310 case REF_STATUS_REJECT_NONFASTFORWARD:
311 case REF_STATUS_REJECT_ALREADY_EXISTS:
312 case REF_STATUS_REJECT_FETCH_FIRST:
313 case REF_STATUS_REJECT_NEEDS_FORCE:
314 case REF_STATUS_REJECT_STALE:
315 case REF_STATUS_REJECT_REMOTE_UPDATED:
316 case REF_STATUS_REJECT_NODELETE:
317 return CHECK_REF_STATUS_REJECTED;
318 case REF_STATUS_UPTODATE:
319 return CHECK_REF_UPTODATE;
321 default:
322 case REF_STATUS_EXPECTING_REPORT:
323 /* already passed checks on the local side */
324 case REF_STATUS_OK:
325 /* of course this is OK */
326 return 0;
331 * the beginning of the next line, or the end of buffer.
333 * NEEDSWORK: perhaps move this to git-compat-util.h or somewhere and
334 * convert many similar uses found by "git grep -A4 memchr".
336 static const char *next_line(const char *line, size_t len)
338 const char *nl = memchr(line, '\n', len);
339 if (!nl)
340 return line + len; /* incomplete line */
341 return nl + 1;
344 static int generate_push_cert(struct strbuf *req_buf,
345 const struct ref *remote_refs,
346 struct send_pack_args *args,
347 const char *cap_string,
348 const char *push_cert_nonce)
350 const struct ref *ref;
351 struct string_list_item *item;
352 char *signing_key_id = xstrdup(get_signing_key_id());
353 const char *cp, *np;
354 struct strbuf cert = STRBUF_INIT;
355 int update_seen = 0;
357 strbuf_addstr(&cert, "certificate version 0.1\n");
358 strbuf_addf(&cert, "pusher %s ", signing_key_id);
359 datestamp(&cert);
360 strbuf_addch(&cert, '\n');
361 if (args->url && *args->url) {
362 char *anon_url = transport_anonymize_url(args->url);
363 strbuf_addf(&cert, "pushee %s\n", anon_url);
364 free(anon_url);
366 if (push_cert_nonce[0])
367 strbuf_addf(&cert, "nonce %s\n", push_cert_nonce);
368 if (args->push_options)
369 for_each_string_list_item(item, args->push_options)
370 strbuf_addf(&cert, "push-option %s\n", item->string);
371 strbuf_addstr(&cert, "\n");
373 for (ref = remote_refs; ref; ref = ref->next) {
374 if (check_to_send_update(ref, args) < 0)
375 continue;
376 update_seen = 1;
377 strbuf_addf(&cert, "%s %s %s\n",
378 oid_to_hex(&ref->old_oid),
379 oid_to_hex(&ref->new_oid),
380 ref->name);
382 if (!update_seen)
383 goto free_return;
385 if (sign_buffer(&cert, &cert, get_signing_key()))
386 die(_("failed to sign the push certificate"));
388 packet_buf_write(req_buf, "push-cert%c%s", 0, cap_string);
389 for (cp = cert.buf; cp < cert.buf + cert.len; cp = np) {
390 np = next_line(cp, cert.buf + cert.len - cp);
391 packet_buf_write(req_buf,
392 "%.*s", (int)(np - cp), cp);
394 packet_buf_write(req_buf, "push-cert-end\n");
396 free_return:
397 free(signing_key_id);
398 strbuf_release(&cert);
399 return update_seen;
402 #define NONCE_LEN_LIMIT 256
404 static void reject_invalid_nonce(const char *nonce, int len)
406 int i = 0;
408 if (NONCE_LEN_LIMIT <= len)
409 die("the receiving end asked to sign an invalid nonce <%.*s>",
410 len, nonce);
412 for (i = 0; i < len; i++) {
413 int ch = nonce[i] & 0xFF;
414 if (isalnum(ch) ||
415 ch == '-' || ch == '.' ||
416 ch == '/' || ch == '+' ||
417 ch == '=' || ch == '_')
418 continue;
419 die("the receiving end asked to sign an invalid nonce <%.*s>",
420 len, nonce);
424 static void get_commons_through_negotiation(const char *url,
425 const struct ref *remote_refs,
426 struct oid_array *commons)
428 struct child_process child = CHILD_PROCESS_INIT;
429 const struct ref *ref;
430 int len = the_hash_algo->hexsz + 1; /* hash + NL */
432 child.git_cmd = 1;
433 child.no_stdin = 1;
434 child.out = -1;
435 strvec_pushl(&child.args, "fetch", "--negotiate-only", NULL);
436 for (ref = remote_refs; ref; ref = ref->next) {
437 if (!is_null_oid(&ref->new_oid))
438 strvec_pushf(&child.args, "--negotiation-tip=%s", oid_to_hex(&ref->new_oid));
440 strvec_push(&child.args, url);
442 if (start_command(&child))
443 die(_("send-pack: unable to fork off fetch subprocess"));
445 do {
446 char hex_hash[GIT_MAX_HEXSZ + 1];
447 int read_len = read_in_full(child.out, hex_hash, len);
448 struct object_id oid;
449 const char *end;
451 if (!read_len)
452 break;
453 if (read_len != len)
454 die("invalid length read %d", read_len);
455 if (parse_oid_hex(hex_hash, &oid, &end) || *end != '\n')
456 die("invalid hash");
457 oid_array_append(commons, &oid);
458 } while (1);
460 if (finish_command(&child)) {
462 * The information that push negotiation provides is useful but
463 * not mandatory.
465 warning(_("push negotiation failed; proceeding anyway with push"));
469 int send_pack(struct send_pack_args *args,
470 int fd[], struct child_process *conn,
471 struct ref *remote_refs,
472 struct oid_array *extra_have)
474 struct oid_array commons = OID_ARRAY_INIT;
475 int in = fd[0];
476 int out = fd[1];
477 struct strbuf req_buf = STRBUF_INIT;
478 struct strbuf cap_buf = STRBUF_INIT;
479 struct ref *ref;
480 int need_pack_data = 0;
481 int allow_deleting_refs = 0;
482 int status_report = 0;
483 int use_sideband = 0;
484 int quiet_supported = 0;
485 int agent_supported = 0;
486 int advertise_sid = 0;
487 int push_negotiate = 0;
488 int use_atomic = 0;
489 int atomic_supported = 0;
490 int use_push_options = 0;
491 int push_options_supported = 0;
492 int object_format_supported = 0;
493 unsigned cmds_sent = 0;
494 int ret;
495 struct async demux;
496 const char *push_cert_nonce = NULL;
497 struct packet_reader reader;
498 int use_bitmaps;
500 if (!remote_refs) {
501 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
502 "Perhaps you should specify a branch.\n");
503 return 0;
506 git_config_get_bool("push.negotiate", &push_negotiate);
507 if (push_negotiate)
508 get_commons_through_negotiation(args->url, remote_refs, &commons);
510 if (!git_config_get_bool("push.usebitmaps", &use_bitmaps))
511 args->disable_bitmaps = !use_bitmaps;
513 git_config_get_bool("transfer.advertisesid", &advertise_sid);
515 /* Does the other end support the reporting? */
516 if (server_supports("report-status-v2"))
517 status_report = 2;
518 else if (server_supports("report-status"))
519 status_report = 1;
520 if (server_supports("delete-refs"))
521 allow_deleting_refs = 1;
522 if (server_supports("ofs-delta"))
523 args->use_ofs_delta = 1;
524 if (server_supports("side-band-64k"))
525 use_sideband = 1;
526 if (server_supports("quiet"))
527 quiet_supported = 1;
528 if (server_supports("agent"))
529 agent_supported = 1;
530 if (!server_supports("session-id"))
531 advertise_sid = 0;
532 if (server_supports("no-thin"))
533 args->use_thin_pack = 0;
534 if (server_supports("atomic"))
535 atomic_supported = 1;
536 if (server_supports("push-options"))
537 push_options_supported = 1;
539 if (!server_supports_hash(the_hash_algo->name, &object_format_supported))
540 die(_("the receiving end does not support this repository's hash algorithm"));
542 if (args->push_cert != SEND_PACK_PUSH_CERT_NEVER) {
543 size_t len;
544 push_cert_nonce = server_feature_value("push-cert", &len);
545 if (push_cert_nonce) {
546 reject_invalid_nonce(push_cert_nonce, len);
547 push_cert_nonce = xmemdupz(push_cert_nonce, len);
548 } else if (args->push_cert == SEND_PACK_PUSH_CERT_ALWAYS) {
549 die(_("the receiving end does not support --signed push"));
550 } else if (args->push_cert == SEND_PACK_PUSH_CERT_IF_ASKED) {
551 warning(_("not sending a push certificate since the"
552 " receiving end does not support --signed"
553 " push"));
557 if (args->atomic && !atomic_supported)
558 die(_("the receiving end does not support --atomic push"));
560 use_atomic = atomic_supported && args->atomic;
562 if (args->push_options && !push_options_supported)
563 die(_("the receiving end does not support push options"));
565 use_push_options = push_options_supported && args->push_options;
567 if (status_report == 1)
568 strbuf_addstr(&cap_buf, " report-status");
569 else if (status_report == 2)
570 strbuf_addstr(&cap_buf, " report-status-v2");
571 if (use_sideband)
572 strbuf_addstr(&cap_buf, " side-band-64k");
573 if (quiet_supported && (args->quiet || !args->progress))
574 strbuf_addstr(&cap_buf, " quiet");
575 if (use_atomic)
576 strbuf_addstr(&cap_buf, " atomic");
577 if (use_push_options)
578 strbuf_addstr(&cap_buf, " push-options");
579 if (object_format_supported)
580 strbuf_addf(&cap_buf, " object-format=%s", the_hash_algo->name);
581 if (agent_supported)
582 strbuf_addf(&cap_buf, " agent=%s", git_user_agent_sanitized());
583 if (advertise_sid)
584 strbuf_addf(&cap_buf, " session-id=%s", trace2_session_id());
587 * NEEDSWORK: why does delete-refs have to be so specific to
588 * send-pack machinery that set_ref_status_for_push() cannot
589 * set this bit for us???
591 for (ref = remote_refs; ref; ref = ref->next)
592 if (ref->deletion && !allow_deleting_refs)
593 ref->status = REF_STATUS_REJECT_NODELETE;
596 * Clear the status for each ref and see if we need to send
597 * the pack data.
599 for (ref = remote_refs; ref; ref = ref->next) {
600 switch (check_to_send_update(ref, args)) {
601 case 0: /* no error */
602 break;
603 case CHECK_REF_STATUS_REJECTED:
605 * When we know the server would reject a ref update if
606 * we were to send it and we're trying to send the refs
607 * atomically, abort the whole operation.
609 if (use_atomic) {
610 strbuf_release(&req_buf);
611 strbuf_release(&cap_buf);
612 reject_atomic_push(remote_refs, args->send_mirror);
613 error("atomic push failed for ref %s. status: %d\n",
614 ref->name, ref->status);
615 return args->porcelain ? 0 : -1;
617 /* else fallthrough */
618 default:
619 continue;
621 if (!ref->deletion)
622 need_pack_data = 1;
624 if (args->dry_run || !status_report)
625 ref->status = REF_STATUS_OK;
626 else
627 ref->status = REF_STATUS_EXPECTING_REPORT;
630 if (!args->dry_run)
631 advertise_shallow_grafts_buf(&req_buf);
634 * Finally, tell the other end!
636 if (!args->dry_run && push_cert_nonce)
637 cmds_sent = generate_push_cert(&req_buf, remote_refs, args,
638 cap_buf.buf, push_cert_nonce);
639 else if (!args->dry_run)
640 for (ref = remote_refs; ref; ref = ref->next) {
641 char *old_hex, *new_hex;
643 if (check_to_send_update(ref, args) < 0)
644 continue;
646 old_hex = oid_to_hex(&ref->old_oid);
647 new_hex = oid_to_hex(&ref->new_oid);
648 if (!cmds_sent) {
649 packet_buf_write(&req_buf,
650 "%s %s %s%c%s",
651 old_hex, new_hex, ref->name, 0,
652 cap_buf.buf);
653 cmds_sent = 1;
654 } else {
655 packet_buf_write(&req_buf, "%s %s %s",
656 old_hex, new_hex, ref->name);
660 if (use_push_options) {
661 struct string_list_item *item;
663 packet_buf_flush(&req_buf);
664 for_each_string_list_item(item, args->push_options)
665 packet_buf_write(&req_buf, "%s", item->string);
668 if (args->stateless_rpc) {
669 if (!args->dry_run && (cmds_sent || is_repository_shallow(the_repository))) {
670 packet_buf_flush(&req_buf);
671 send_sideband(out, -1, req_buf.buf, req_buf.len, LARGE_PACKET_MAX);
673 } else {
674 write_or_die(out, req_buf.buf, req_buf.len);
675 packet_flush(out);
677 strbuf_release(&req_buf);
678 strbuf_release(&cap_buf);
680 if (use_sideband && cmds_sent) {
681 memset(&demux, 0, sizeof(demux));
682 demux.proc = sideband_demux;
683 demux.data = fd;
684 demux.out = -1;
685 demux.isolate_sigpipe = 1;
686 if (start_async(&demux))
687 die("send-pack: unable to fork off sideband demultiplexer");
688 in = demux.out;
691 packet_reader_init(&reader, in, NULL, 0,
692 PACKET_READ_CHOMP_NEWLINE |
693 PACKET_READ_DIE_ON_ERR_PACKET);
695 if (need_pack_data && cmds_sent) {
696 if (pack_objects(out, remote_refs, extra_have, &commons, args) < 0) {
697 if (args->stateless_rpc)
698 close(out);
699 if (git_connection_is_socket(conn))
700 shutdown(fd[0], SHUT_WR);
703 * Do not even bother with the return value; we know we
704 * are failing, and just want the error() side effects,
705 * as well as marking refs with their remote status (if
706 * we get one).
708 if (status_report)
709 receive_status(&reader, remote_refs);
711 if (use_sideband) {
712 close(demux.out);
713 finish_async(&demux);
715 fd[1] = -1;
716 return -1;
718 if (!args->stateless_rpc)
719 /* Closed by pack_objects() via start_command() */
720 fd[1] = -1;
722 if (args->stateless_rpc && cmds_sent)
723 packet_flush(out);
725 if (status_report && cmds_sent)
726 ret = receive_status(&reader, remote_refs);
727 else
728 ret = 0;
729 if (args->stateless_rpc)
730 packet_flush(out);
732 if (use_sideband && cmds_sent) {
733 close(demux.out);
734 if (finish_async(&demux)) {
735 error("error in sideband demultiplexer");
736 ret = -1;
740 if (ret < 0)
741 return ret;
743 if (args->porcelain)
744 return 0;
746 for (ref = remote_refs; ref; ref = ref->next) {
747 switch (ref->status) {
748 case REF_STATUS_NONE:
749 case REF_STATUS_UPTODATE:
750 case REF_STATUS_OK:
751 break;
752 default:
753 return -1;
756 return 0;