notes.c: append separator instead of insert by pos
[alt-git.git] / send-pack.c
blobf81bbb28d79259f25db58548ca3d51fb8b2f6a2d
1 #include "git-compat-util.h"
2 #include "config.h"
3 #include "commit.h"
4 #include "gettext.h"
5 #include "hex.h"
6 #include "refs.h"
7 #include "object-store.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 "quote.h"
15 #include "transport.h"
16 #include "version.h"
17 #include "oid-array.h"
18 #include "gpg-interface.h"
19 #include "shallow.h"
20 #include "parse-options.h"
21 #include "trace2.h"
22 #include "write-or-die.h"
24 int option_parse_push_signed(const struct option *opt,
25 const char *arg, int unset)
27 if (unset) {
28 *(int *)(opt->value) = SEND_PACK_PUSH_CERT_NEVER;
29 return 0;
31 switch (git_parse_maybe_bool(arg)) {
32 case 1:
33 *(int *)(opt->value) = SEND_PACK_PUSH_CERT_ALWAYS;
34 return 0;
35 case 0:
36 *(int *)(opt->value) = SEND_PACK_PUSH_CERT_NEVER;
37 return 0;
39 if (!strcasecmp("if-asked", arg)) {
40 *(int *)(opt->value) = SEND_PACK_PUSH_CERT_IF_ASKED;
41 return 0;
43 die("bad %s argument: %s", opt->long_name, arg);
46 static void feed_object(const struct object_id *oid, FILE *fh, int negative)
48 if (negative &&
49 !repo_has_object_file_with_flags(the_repository, oid,
50 OBJECT_INFO_SKIP_FETCH_OBJECT |
51 OBJECT_INFO_QUICK))
52 return;
54 if (negative)
55 putc('^', fh);
56 fputs(oid_to_hex(oid), fh);
57 putc('\n', fh);
61 * Make a pack stream and spit it out into file descriptor fd
63 static int pack_objects(int fd, struct ref *refs, struct oid_array *advertised,
64 struct oid_array *negotiated,
65 struct send_pack_args *args)
68 * The child becomes pack-objects --revs; we feed
69 * the revision parameters to it via its stdin and
70 * let its stdout go back to the other end.
72 struct child_process po = CHILD_PROCESS_INIT;
73 FILE *po_in;
74 int i;
75 int rc;
77 strvec_push(&po.args, "pack-objects");
78 strvec_push(&po.args, "--all-progress-implied");
79 strvec_push(&po.args, "--revs");
80 strvec_push(&po.args, "--stdout");
81 if (args->use_thin_pack)
82 strvec_push(&po.args, "--thin");
83 if (args->use_ofs_delta)
84 strvec_push(&po.args, "--delta-base-offset");
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(the_repository))
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");
101 * We feed the pack-objects we just spawned with revision
102 * parameters by writing to the pipe.
104 po_in = xfdopen(po.in, "w");
105 for (i = 0; i < advertised->nr; i++)
106 feed_object(&advertised->oid[i], po_in, 1);
107 for (i = 0; i < negotiated->nr; i++)
108 feed_object(&negotiated->oid[i], po_in, 1);
110 while (refs) {
111 if (!is_null_oid(&refs->old_oid))
112 feed_object(&refs->old_oid, po_in, 1);
113 if (!is_null_oid(&refs->new_oid))
114 feed_object(&refs->new_oid, po_in, 0);
115 refs = refs->next;
118 fflush(po_in);
119 if (ferror(po_in))
120 die_errno("error writing to pack-objects");
121 fclose(po_in);
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);
131 free(buf);
132 close(po.out);
133 po.out = -1;
136 rc = finish_command(&po);
137 if (rc) {
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).
146 if (rc > 128 && rc != 141)
147 error("pack-objects died of signal %d", rc - 128);
148 return -1;
150 return 0;
153 static int receive_unpack_status(struct packet_reader *reader)
155 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
156 return error(_("unexpected flush packet while reading remote unpack status"));
157 if (!skip_prefix(reader->line, "unpack ", &reader->line))
158 return error(_("unable to parse remote unpack status: %s"), reader->line);
159 if (strcmp(reader->line, "ok"))
160 return error(_("remote unpack failed: %s"), reader->line);
161 return 0;
164 static int receive_status(struct packet_reader *reader, struct ref *refs)
166 struct ref *hint;
167 int ret;
168 struct ref_push_report *report = NULL;
169 int new_report = 0;
170 int once = 0;
172 hint = NULL;
173 ret = receive_unpack_status(reader);
174 while (1) {
175 struct object_id old_oid, new_oid;
176 const char *head;
177 const char *refname;
178 char *p;
179 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
180 break;
181 head = reader->line;
182 p = strchr(head, ' ');
183 if (!p) {
184 error("invalid status line from remote: %s", reader->line);
185 ret = -1;
186 break;
188 *p++ = '\0';
190 if (!strcmp(head, "option")) {
191 const char *key, *val;
193 if (!hint || !(report || new_report)) {
194 if (!once++)
195 error("'option' without a matching 'ok/ng' directive");
196 ret = -1;
197 continue;
199 if (new_report) {
200 if (!hint->report) {
201 CALLOC_ARRAY(hint->report, 1);
202 report = hint->report;
203 } else {
204 report = hint->report;
205 while (report->next)
206 report = report->next;
207 CALLOC_ARRAY(report->next, 1);
208 report = report->next;
210 new_report = 0;
212 key = p;
213 p = strchr(key, ' ');
214 if (p)
215 *p++ = '\0';
216 val = p;
217 if (!strcmp(key, "refname"))
218 report->ref_name = xstrdup_or_null(val);
219 else if (!strcmp(key, "old-oid") && val &&
220 !parse_oid_hex(val, &old_oid, &val))
221 report->old_oid = oiddup(&old_oid);
222 else if (!strcmp(key, "new-oid") && val &&
223 !parse_oid_hex(val, &new_oid, &val))
224 report->new_oid = oiddup(&new_oid);
225 else if (!strcmp(key, "forced-update"))
226 report->forced_update = 1;
227 continue;
230 report = NULL;
231 new_report = 0;
232 if (strcmp(head, "ok") && strcmp(head, "ng")) {
233 error("invalid ref status from remote: %s", head);
234 ret = -1;
235 break;
237 refname = p;
238 p = strchr(refname, ' ');
239 if (p)
240 *p++ = '\0';
241 /* first try searching at our hint, falling back to all refs */
242 if (hint)
243 hint = find_ref_by_name(hint, refname);
244 if (!hint)
245 hint = find_ref_by_name(refs, refname);
246 if (!hint) {
247 warning("remote reported status on unknown ref: %s",
248 refname);
249 continue;
251 if (hint->status != REF_STATUS_EXPECTING_REPORT &&
252 hint->status != REF_STATUS_OK &&
253 hint->status != REF_STATUS_REMOTE_REJECT) {
254 warning("remote reported status on unexpected ref: %s",
255 refname);
256 continue;
258 if (!strcmp(head, "ng")) {
259 hint->status = REF_STATUS_REMOTE_REJECT;
260 if (p)
261 hint->remote_status = xstrdup(p);
262 else
263 hint->remote_status = "failed";
264 } else {
265 hint->status = REF_STATUS_OK;
266 hint->remote_status = xstrdup_or_null(p);
267 new_report = 1;
270 return ret;
273 static int sideband_demux(int in UNUSED, int out, void *data)
275 int *fd = data, ret;
276 if (async_with_fork())
277 close(fd[1]);
278 ret = recv_sideband("send-pack", fd[0], out);
279 close(out);
280 return ret;
283 static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb)
285 struct strbuf *sb = cb;
286 if (graft->nr_parent == -1)
287 packet_buf_write(sb, "shallow %s\n", oid_to_hex(&graft->oid));
288 return 0;
291 static void advertise_shallow_grafts_buf(struct strbuf *sb)
293 if (!is_repository_shallow(the_repository))
294 return;
295 for_each_commit_graft(advertise_shallow_grafts_cb, sb);
298 #define CHECK_REF_NO_PUSH -1
299 #define CHECK_REF_STATUS_REJECTED -2
300 #define CHECK_REF_UPTODATE -3
301 static int check_to_send_update(const struct ref *ref, const struct send_pack_args *args)
303 if (!ref->peer_ref && !args->send_mirror)
304 return CHECK_REF_NO_PUSH;
306 /* Check for statuses set by set_ref_status_for_push() */
307 switch (ref->status) {
308 case REF_STATUS_REJECT_NONFASTFORWARD:
309 case REF_STATUS_REJECT_ALREADY_EXISTS:
310 case REF_STATUS_REJECT_FETCH_FIRST:
311 case REF_STATUS_REJECT_NEEDS_FORCE:
312 case REF_STATUS_REJECT_STALE:
313 case REF_STATUS_REJECT_REMOTE_UPDATED:
314 case REF_STATUS_REJECT_NODELETE:
315 return CHECK_REF_STATUS_REJECTED;
316 case REF_STATUS_UPTODATE:
317 return CHECK_REF_UPTODATE;
319 default:
320 case REF_STATUS_EXPECTING_REPORT:
321 /* already passed checks on the local side */
322 case REF_STATUS_OK:
323 /* of course this is OK */
324 return 0;
329 * the beginning of the next line, or the end of buffer.
331 * NEEDSWORK: perhaps move this to git-compat-util.h or somewhere and
332 * convert many similar uses found by "git grep -A4 memchr".
334 static const char *next_line(const char *line, size_t len)
336 const char *nl = memchr(line, '\n', len);
337 if (!nl)
338 return line + len; /* incomplete line */
339 return nl + 1;
342 static int generate_push_cert(struct strbuf *req_buf,
343 const struct ref *remote_refs,
344 struct send_pack_args *args,
345 const char *cap_string,
346 const char *push_cert_nonce)
348 const struct ref *ref;
349 struct string_list_item *item;
350 char *signing_key_id = xstrdup(get_signing_key_id());
351 const char *cp, *np;
352 struct strbuf cert = STRBUF_INIT;
353 int update_seen = 0;
355 strbuf_addstr(&cert, "certificate version 0.1\n");
356 strbuf_addf(&cert, "pusher %s ", signing_key_id);
357 datestamp(&cert);
358 strbuf_addch(&cert, '\n');
359 if (args->url && *args->url) {
360 char *anon_url = transport_anonymize_url(args->url);
361 strbuf_addf(&cert, "pushee %s\n", anon_url);
362 free(anon_url);
364 if (push_cert_nonce[0])
365 strbuf_addf(&cert, "nonce %s\n", push_cert_nonce);
366 if (args->push_options)
367 for_each_string_list_item(item, args->push_options)
368 strbuf_addf(&cert, "push-option %s\n", item->string);
369 strbuf_addstr(&cert, "\n");
371 for (ref = remote_refs; ref; ref = ref->next) {
372 if (check_to_send_update(ref, args) < 0)
373 continue;
374 update_seen = 1;
375 strbuf_addf(&cert, "%s %s %s\n",
376 oid_to_hex(&ref->old_oid),
377 oid_to_hex(&ref->new_oid),
378 ref->name);
380 if (!update_seen)
381 goto free_return;
383 if (sign_buffer(&cert, &cert, get_signing_key()))
384 die(_("failed to sign the push certificate"));
386 packet_buf_write(req_buf, "push-cert%c%s", 0, cap_string);
387 for (cp = cert.buf; cp < cert.buf + cert.len; cp = np) {
388 np = next_line(cp, cert.buf + cert.len - cp);
389 packet_buf_write(req_buf,
390 "%.*s", (int)(np - cp), cp);
392 packet_buf_write(req_buf, "push-cert-end\n");
394 free_return:
395 free(signing_key_id);
396 strbuf_release(&cert);
397 return update_seen;
400 #define NONCE_LEN_LIMIT 256
402 static void reject_invalid_nonce(const char *nonce, int len)
404 int i = 0;
406 if (NONCE_LEN_LIMIT <= len)
407 die("the receiving end asked to sign an invalid nonce <%.*s>",
408 len, nonce);
410 for (i = 0; i < len; i++) {
411 int ch = nonce[i] & 0xFF;
412 if (isalnum(ch) ||
413 ch == '-' || ch == '.' ||
414 ch == '/' || ch == '+' ||
415 ch == '=' || ch == '_')
416 continue;
417 die("the receiving end asked to sign an invalid nonce <%.*s>",
418 len, nonce);
422 static void get_commons_through_negotiation(const char *url,
423 const struct ref *remote_refs,
424 struct oid_array *commons)
426 struct child_process child = CHILD_PROCESS_INIT;
427 const struct ref *ref;
428 int len = the_hash_algo->hexsz + 1; /* hash + NL */
430 child.git_cmd = 1;
431 child.no_stdin = 1;
432 child.out = -1;
433 strvec_pushl(&child.args, "fetch", "--negotiate-only", NULL);
434 for (ref = remote_refs; ref; ref = ref->next) {
435 if (!is_null_oid(&ref->new_oid))
436 strvec_pushf(&child.args, "--negotiation-tip=%s", oid_to_hex(&ref->new_oid));
438 strvec_push(&child.args, url);
440 if (start_command(&child))
441 die(_("send-pack: unable to fork off fetch subprocess"));
443 do {
444 char hex_hash[GIT_MAX_HEXSZ + 1];
445 int read_len = read_in_full(child.out, hex_hash, len);
446 struct object_id oid;
447 const char *end;
449 if (!read_len)
450 break;
451 if (read_len != len)
452 die("invalid length read %d", read_len);
453 if (parse_oid_hex(hex_hash, &oid, &end) || *end != '\n')
454 die("invalid hash");
455 oid_array_append(commons, &oid);
456 } while (1);
458 if (finish_command(&child)) {
460 * The information that push negotiation provides is useful but
461 * not mandatory.
463 warning(_("push negotiation failed; proceeding anyway with push"));
467 int send_pack(struct send_pack_args *args,
468 int fd[], struct child_process *conn,
469 struct ref *remote_refs,
470 struct oid_array *extra_have)
472 struct oid_array commons = OID_ARRAY_INIT;
473 int in = fd[0];
474 int out = fd[1];
475 struct strbuf req_buf = STRBUF_INIT;
476 struct strbuf cap_buf = STRBUF_INIT;
477 struct ref *ref;
478 int need_pack_data = 0;
479 int allow_deleting_refs = 0;
480 int status_report = 0;
481 int use_sideband = 0;
482 int quiet_supported = 0;
483 int agent_supported = 0;
484 int advertise_sid = 0;
485 int push_negotiate = 0;
486 int use_atomic = 0;
487 int atomic_supported = 0;
488 int use_push_options = 0;
489 int push_options_supported = 0;
490 int object_format_supported = 0;
491 unsigned cmds_sent = 0;
492 int ret;
493 struct async demux;
494 const char *push_cert_nonce = NULL;
495 struct packet_reader reader;
496 int use_bitmaps;
498 if (!remote_refs) {
499 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
500 "Perhaps you should specify a branch.\n");
501 return 0;
504 git_config_get_bool("push.negotiate", &push_negotiate);
505 if (push_negotiate)
506 get_commons_through_negotiation(args->url, remote_refs, &commons);
508 if (!git_config_get_bool("push.usebitmaps", &use_bitmaps))
509 args->disable_bitmaps = !use_bitmaps;
511 git_config_get_bool("transfer.advertisesid", &advertise_sid);
513 /* Does the other end support the reporting? */
514 if (server_supports("report-status-v2"))
515 status_report = 2;
516 else if (server_supports("report-status"))
517 status_report = 1;
518 if (server_supports("delete-refs"))
519 allow_deleting_refs = 1;
520 if (server_supports("ofs-delta"))
521 args->use_ofs_delta = 1;
522 if (server_supports("side-band-64k"))
523 use_sideband = 1;
524 if (server_supports("quiet"))
525 quiet_supported = 1;
526 if (server_supports("agent"))
527 agent_supported = 1;
528 if (!server_supports("session-id"))
529 advertise_sid = 0;
530 if (server_supports("no-thin"))
531 args->use_thin_pack = 0;
532 if (server_supports("atomic"))
533 atomic_supported = 1;
534 if (server_supports("push-options"))
535 push_options_supported = 1;
537 if (!server_supports_hash(the_hash_algo->name, &object_format_supported))
538 die(_("the receiving end does not support this repository's hash algorithm"));
540 if (args->push_cert != SEND_PACK_PUSH_CERT_NEVER) {
541 int len;
542 push_cert_nonce = server_feature_value("push-cert", &len);
543 if (push_cert_nonce) {
544 reject_invalid_nonce(push_cert_nonce, len);
545 push_cert_nonce = xmemdupz(push_cert_nonce, len);
546 } else if (args->push_cert == SEND_PACK_PUSH_CERT_ALWAYS) {
547 die(_("the receiving end does not support --signed push"));
548 } else if (args->push_cert == SEND_PACK_PUSH_CERT_IF_ASKED) {
549 warning(_("not sending a push certificate since the"
550 " receiving end does not support --signed"
551 " push"));
555 if (args->atomic && !atomic_supported)
556 die(_("the receiving end does not support --atomic push"));
558 use_atomic = atomic_supported && args->atomic;
560 if (args->push_options && !push_options_supported)
561 die(_("the receiving end does not support push options"));
563 use_push_options = push_options_supported && args->push_options;
565 if (status_report == 1)
566 strbuf_addstr(&cap_buf, " report-status");
567 else if (status_report == 2)
568 strbuf_addstr(&cap_buf, " report-status-v2");
569 if (use_sideband)
570 strbuf_addstr(&cap_buf, " side-band-64k");
571 if (quiet_supported && (args->quiet || !args->progress))
572 strbuf_addstr(&cap_buf, " quiet");
573 if (use_atomic)
574 strbuf_addstr(&cap_buf, " atomic");
575 if (use_push_options)
576 strbuf_addstr(&cap_buf, " push-options");
577 if (object_format_supported)
578 strbuf_addf(&cap_buf, " object-format=%s", the_hash_algo->name);
579 if (agent_supported)
580 strbuf_addf(&cap_buf, " agent=%s", git_user_agent_sanitized());
581 if (advertise_sid)
582 strbuf_addf(&cap_buf, " session-id=%s", trace2_session_id());
585 * NEEDSWORK: why does delete-refs have to be so specific to
586 * send-pack machinery that set_ref_status_for_push() cannot
587 * set this bit for us???
589 for (ref = remote_refs; ref; ref = ref->next)
590 if (ref->deletion && !allow_deleting_refs)
591 ref->status = REF_STATUS_REJECT_NODELETE;
594 * Clear the status for each ref and see if we need to send
595 * the pack data.
597 for (ref = remote_refs; ref; ref = ref->next) {
598 switch (check_to_send_update(ref, args)) {
599 case 0: /* no error */
600 break;
601 case CHECK_REF_STATUS_REJECTED:
603 * When we know the server would reject a ref update if
604 * we were to send it and we're trying to send the refs
605 * atomically, abort the whole operation.
607 if (use_atomic) {
608 strbuf_release(&req_buf);
609 strbuf_release(&cap_buf);
610 reject_atomic_push(remote_refs, args->send_mirror);
611 error("atomic push failed for ref %s. status: %d\n",
612 ref->name, ref->status);
613 return args->porcelain ? 0 : -1;
615 /* else fallthrough */
616 default:
617 continue;
619 if (!ref->deletion)
620 need_pack_data = 1;
622 if (args->dry_run || !status_report)
623 ref->status = REF_STATUS_OK;
624 else
625 ref->status = REF_STATUS_EXPECTING_REPORT;
628 if (!args->dry_run)
629 advertise_shallow_grafts_buf(&req_buf);
632 * Finally, tell the other end!
634 if (!args->dry_run && push_cert_nonce)
635 cmds_sent = generate_push_cert(&req_buf, remote_refs, args,
636 cap_buf.buf, push_cert_nonce);
637 else if (!args->dry_run)
638 for (ref = remote_refs; ref; ref = ref->next) {
639 char *old_hex, *new_hex;
641 if (check_to_send_update(ref, args) < 0)
642 continue;
644 old_hex = oid_to_hex(&ref->old_oid);
645 new_hex = oid_to_hex(&ref->new_oid);
646 if (!cmds_sent) {
647 packet_buf_write(&req_buf,
648 "%s %s %s%c%s",
649 old_hex, new_hex, ref->name, 0,
650 cap_buf.buf);
651 cmds_sent = 1;
652 } else {
653 packet_buf_write(&req_buf, "%s %s %s",
654 old_hex, new_hex, ref->name);
658 if (use_push_options) {
659 struct string_list_item *item;
661 packet_buf_flush(&req_buf);
662 for_each_string_list_item(item, args->push_options)
663 packet_buf_write(&req_buf, "%s", item->string);
666 if (args->stateless_rpc) {
667 if (!args->dry_run && (cmds_sent || is_repository_shallow(the_repository))) {
668 packet_buf_flush(&req_buf);
669 send_sideband(out, -1, req_buf.buf, req_buf.len, LARGE_PACKET_MAX);
671 } else {
672 write_or_die(out, req_buf.buf, req_buf.len);
673 packet_flush(out);
675 strbuf_release(&req_buf);
676 strbuf_release(&cap_buf);
678 if (use_sideband && cmds_sent) {
679 memset(&demux, 0, sizeof(demux));
680 demux.proc = sideband_demux;
681 demux.data = fd;
682 demux.out = -1;
683 demux.isolate_sigpipe = 1;
684 if (start_async(&demux))
685 die("send-pack: unable to fork off sideband demultiplexer");
686 in = demux.out;
689 packet_reader_init(&reader, in, NULL, 0,
690 PACKET_READ_CHOMP_NEWLINE |
691 PACKET_READ_DIE_ON_ERR_PACKET);
693 if (need_pack_data && cmds_sent) {
694 if (pack_objects(out, remote_refs, extra_have, &commons, args) < 0) {
695 if (args->stateless_rpc)
696 close(out);
697 if (git_connection_is_socket(conn))
698 shutdown(fd[0], SHUT_WR);
701 * Do not even bother with the return value; we know we
702 * are failing, and just want the error() side effects,
703 * as well as marking refs with their remote status (if
704 * we get one).
706 if (status_report)
707 receive_status(&reader, remote_refs);
709 if (use_sideband) {
710 close(demux.out);
711 finish_async(&demux);
713 fd[1] = -1;
714 return -1;
716 if (!args->stateless_rpc)
717 /* Closed by pack_objects() via start_command() */
718 fd[1] = -1;
720 if (args->stateless_rpc && cmds_sent)
721 packet_flush(out);
723 if (status_report && cmds_sent)
724 ret = receive_status(&reader, remote_refs);
725 else
726 ret = 0;
727 if (args->stateless_rpc)
728 packet_flush(out);
730 if (use_sideband && cmds_sent) {
731 close(demux.out);
732 if (finish_async(&demux)) {
733 error("error in sideband demultiplexer");
734 ret = -1;
738 if (ret < 0)
739 return ret;
741 if (args->porcelain)
742 return 0;
744 for (ref = remote_refs; ref; ref = ref->next) {
745 switch (ref->status) {
746 case REF_STATUS_NONE:
747 case REF_STATUS_UPTODATE:
748 case REF_STATUS_OK:
749 break;
750 default:
751 return -1;
754 return 0;