Merge branch 'en/merge-strategy-docs' into maint
[git.git] / send-pack.c
blob5a79e0e711031907bcec04acc18a64c2b9d40adc
1 #include "builtin.h"
2 #include "config.h"
3 #include "commit.h"
4 #include "refs.h"
5 #include "object-store.h"
6 #include "pkt-line.h"
7 #include "sideband.h"
8 #include "run-command.h"
9 #include "remote.h"
10 #include "connect.h"
11 #include "send-pack.h"
12 #include "quote.h"
13 #include "transport.h"
14 #include "version.h"
15 #include "oid-array.h"
16 #include "gpg-interface.h"
17 #include "cache.h"
18 #include "shallow.h"
20 int option_parse_push_signed(const struct option *opt,
21 const char *arg, int unset)
23 if (unset) {
24 *(int *)(opt->value) = SEND_PACK_PUSH_CERT_NEVER;
25 return 0;
27 switch (git_parse_maybe_bool(arg)) {
28 case 1:
29 *(int *)(opt->value) = SEND_PACK_PUSH_CERT_ALWAYS;
30 return 0;
31 case 0:
32 *(int *)(opt->value) = SEND_PACK_PUSH_CERT_NEVER;
33 return 0;
35 if (!strcasecmp("if-asked", arg)) {
36 *(int *)(opt->value) = SEND_PACK_PUSH_CERT_IF_ASKED;
37 return 0;
39 die("bad %s argument: %s", opt->long_name, arg);
42 static void feed_object(const struct object_id *oid, FILE *fh, int negative)
44 if (negative &&
45 !has_object_file_with_flags(oid,
46 OBJECT_INFO_SKIP_FETCH_OBJECT |
47 OBJECT_INFO_QUICK))
48 return;
50 if (negative)
51 putc('^', fh);
52 fputs(oid_to_hex(oid), fh);
53 putc('\n', fh);
57 * Make a pack stream and spit it out into file descriptor fd
59 static int pack_objects(int fd, struct ref *refs, struct oid_array *advertised,
60 struct oid_array *negotiated,
61 struct send_pack_args *args)
64 * The child becomes pack-objects --revs; we feed
65 * the revision parameters to it via its stdin and
66 * let its stdout go back to the other end.
68 struct child_process po = CHILD_PROCESS_INIT;
69 FILE *po_in;
70 int i;
71 int rc;
73 strvec_push(&po.args, "pack-objects");
74 strvec_push(&po.args, "--all-progress-implied");
75 strvec_push(&po.args, "--revs");
76 strvec_push(&po.args, "--stdout");
77 if (args->use_thin_pack)
78 strvec_push(&po.args, "--thin");
79 if (args->use_ofs_delta)
80 strvec_push(&po.args, "--delta-base-offset");
81 if (args->quiet || !args->progress)
82 strvec_push(&po.args, "-q");
83 if (args->progress)
84 strvec_push(&po.args, "--progress");
85 if (is_repository_shallow(the_repository))
86 strvec_push(&po.args, "--shallow");
87 po.in = -1;
88 po.out = args->stateless_rpc ? -1 : fd;
89 po.git_cmd = 1;
90 po.clean_on_exit = 1;
91 if (start_command(&po))
92 die_errno("git pack-objects failed");
95 * We feed the pack-objects we just spawned with revision
96 * parameters by writing to the pipe.
98 po_in = xfdopen(po.in, "w");
99 for (i = 0; i < advertised->nr; i++)
100 feed_object(&advertised->oid[i], po_in, 1);
101 for (i = 0; i < negotiated->nr; i++)
102 feed_object(&negotiated->oid[i], po_in, 1);
104 while (refs) {
105 if (!is_null_oid(&refs->old_oid))
106 feed_object(&refs->old_oid, po_in, 1);
107 if (!is_null_oid(&refs->new_oid))
108 feed_object(&refs->new_oid, po_in, 0);
109 refs = refs->next;
112 fflush(po_in);
113 if (ferror(po_in))
114 die_errno("error writing to pack-objects");
115 fclose(po_in);
117 if (args->stateless_rpc) {
118 char *buf = xmalloc(LARGE_PACKET_MAX);
119 while (1) {
120 ssize_t n = xread(po.out, buf, LARGE_PACKET_MAX);
121 if (n <= 0)
122 break;
123 send_sideband(fd, -1, buf, n, LARGE_PACKET_MAX);
125 free(buf);
126 close(po.out);
127 po.out = -1;
130 rc = finish_command(&po);
131 if (rc) {
133 * For a normal non-zero exit, we assume pack-objects wrote
134 * something useful to stderr. For death by signal, though,
135 * we should mention it to the user. The exception is SIGPIPE
136 * (141), because that's a normal occurrence if the remote end
137 * hangs up (and we'll report that by trying to read the unpack
138 * status).
140 if (rc > 128 && rc != 141)
141 error("pack-objects died of signal %d", rc - 128);
142 return -1;
144 return 0;
147 static int receive_unpack_status(struct packet_reader *reader)
149 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
150 return error(_("unexpected flush packet while reading remote unpack status"));
151 if (!skip_prefix(reader->line, "unpack ", &reader->line))
152 return error(_("unable to parse remote unpack status: %s"), reader->line);
153 if (strcmp(reader->line, "ok"))
154 return error(_("remote unpack failed: %s"), reader->line);
155 return 0;
158 static int receive_status(struct packet_reader *reader, struct ref *refs)
160 struct ref *hint;
161 int ret;
162 struct ref_push_report *report = NULL;
163 int new_report = 0;
164 int once = 0;
166 hint = NULL;
167 ret = receive_unpack_status(reader);
168 while (1) {
169 struct object_id old_oid, new_oid;
170 const char *head;
171 const char *refname;
172 char *p;
173 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
174 break;
175 head = reader->line;
176 p = strchr(head, ' ');
177 if (!p) {
178 error("invalid status line from remote: %s", reader->line);
179 ret = -1;
180 break;
182 *p++ = '\0';
184 if (!strcmp(head, "option")) {
185 const char *key, *val;
187 if (!hint || !(report || new_report)) {
188 if (!once++)
189 error("'option' without a matching 'ok/ng' directive");
190 ret = -1;
191 continue;
193 if (new_report) {
194 if (!hint->report) {
195 CALLOC_ARRAY(hint->report, 1);
196 report = hint->report;
197 } else {
198 report = hint->report;
199 while (report->next)
200 report = report->next;
201 CALLOC_ARRAY(report->next, 1);
202 report = report->next;
204 new_report = 0;
206 key = p;
207 p = strchr(key, ' ');
208 if (p)
209 *p++ = '\0';
210 val = p;
211 if (!strcmp(key, "refname"))
212 report->ref_name = xstrdup_or_null(val);
213 else if (!strcmp(key, "old-oid") && val &&
214 !parse_oid_hex(val, &old_oid, &val))
215 report->old_oid = oiddup(&old_oid);
216 else if (!strcmp(key, "new-oid") && val &&
217 !parse_oid_hex(val, &new_oid, &val))
218 report->new_oid = oiddup(&new_oid);
219 else if (!strcmp(key, "forced-update"))
220 report->forced_update = 1;
221 continue;
224 report = NULL;
225 new_report = 0;
226 if (strcmp(head, "ok") && strcmp(head, "ng")) {
227 error("invalid ref status from remote: %s", head);
228 ret = -1;
229 break;
231 refname = p;
232 p = strchr(refname, ' ');
233 if (p)
234 *p++ = '\0';
235 /* first try searching at our hint, falling back to all refs */
236 if (hint)
237 hint = find_ref_by_name(hint, refname);
238 if (!hint)
239 hint = find_ref_by_name(refs, refname);
240 if (!hint) {
241 warning("remote reported status on unknown ref: %s",
242 refname);
243 continue;
245 if (hint->status != REF_STATUS_EXPECTING_REPORT &&
246 hint->status != REF_STATUS_OK &&
247 hint->status != REF_STATUS_REMOTE_REJECT) {
248 warning("remote reported status on unexpected ref: %s",
249 refname);
250 continue;
252 if (!strcmp(head, "ng")) {
253 hint->status = REF_STATUS_REMOTE_REJECT;
254 if (p)
255 hint->remote_status = xstrdup(p);
256 else
257 hint->remote_status = "failed";
258 } else {
259 hint->status = REF_STATUS_OK;
260 hint->remote_status = xstrdup_or_null(p);
261 new_report = 1;
264 return ret;
267 static int sideband_demux(int in, int out, void *data)
269 int *fd = data, ret;
270 if (async_with_fork())
271 close(fd[1]);
272 ret = recv_sideband("send-pack", fd[0], out);
273 close(out);
274 return ret;
277 static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb)
279 struct strbuf *sb = cb;
280 if (graft->nr_parent == -1)
281 packet_buf_write(sb, "shallow %s\n", oid_to_hex(&graft->oid));
282 return 0;
285 static void advertise_shallow_grafts_buf(struct strbuf *sb)
287 if (!is_repository_shallow(the_repository))
288 return;
289 for_each_commit_graft(advertise_shallow_grafts_cb, sb);
292 #define CHECK_REF_NO_PUSH -1
293 #define CHECK_REF_STATUS_REJECTED -2
294 #define CHECK_REF_UPTODATE -3
295 static int check_to_send_update(const struct ref *ref, const struct send_pack_args *args)
297 if (!ref->peer_ref && !args->send_mirror)
298 return CHECK_REF_NO_PUSH;
300 /* Check for statuses set by set_ref_status_for_push() */
301 switch (ref->status) {
302 case REF_STATUS_REJECT_NONFASTFORWARD:
303 case REF_STATUS_REJECT_ALREADY_EXISTS:
304 case REF_STATUS_REJECT_FETCH_FIRST:
305 case REF_STATUS_REJECT_NEEDS_FORCE:
306 case REF_STATUS_REJECT_STALE:
307 case REF_STATUS_REJECT_REMOTE_UPDATED:
308 case REF_STATUS_REJECT_NODELETE:
309 return CHECK_REF_STATUS_REJECTED;
310 case REF_STATUS_UPTODATE:
311 return CHECK_REF_UPTODATE;
313 default:
314 case REF_STATUS_EXPECTING_REPORT:
315 /* already passed checks on the local side */
316 case REF_STATUS_OK:
317 /* of course this is OK */
318 return 0;
323 * the beginning of the next line, or the end of buffer.
325 * NEEDSWORK: perhaps move this to git-compat-util.h or somewhere and
326 * convert many similar uses found by "git grep -A4 memchr".
328 static const char *next_line(const char *line, size_t len)
330 const char *nl = memchr(line, '\n', len);
331 if (!nl)
332 return line + len; /* incomplete line */
333 return nl + 1;
336 static int generate_push_cert(struct strbuf *req_buf,
337 const struct ref *remote_refs,
338 struct send_pack_args *args,
339 const char *cap_string,
340 const char *push_cert_nonce)
342 const struct ref *ref;
343 struct string_list_item *item;
344 char *signing_key = xstrdup(get_signing_key());
345 const char *cp, *np;
346 struct strbuf cert = STRBUF_INIT;
347 int update_seen = 0;
349 strbuf_addstr(&cert, "certificate version 0.1\n");
350 strbuf_addf(&cert, "pusher %s ", signing_key);
351 datestamp(&cert);
352 strbuf_addch(&cert, '\n');
353 if (args->url && *args->url) {
354 char *anon_url = transport_anonymize_url(args->url);
355 strbuf_addf(&cert, "pushee %s\n", anon_url);
356 free(anon_url);
358 if (push_cert_nonce[0])
359 strbuf_addf(&cert, "nonce %s\n", push_cert_nonce);
360 if (args->push_options)
361 for_each_string_list_item(item, args->push_options)
362 strbuf_addf(&cert, "push-option %s\n", item->string);
363 strbuf_addstr(&cert, "\n");
365 for (ref = remote_refs; ref; ref = ref->next) {
366 if (check_to_send_update(ref, args) < 0)
367 continue;
368 update_seen = 1;
369 strbuf_addf(&cert, "%s %s %s\n",
370 oid_to_hex(&ref->old_oid),
371 oid_to_hex(&ref->new_oid),
372 ref->name);
374 if (!update_seen)
375 goto free_return;
377 if (sign_buffer(&cert, &cert, signing_key))
378 die(_("failed to sign the push certificate"));
380 packet_buf_write(req_buf, "push-cert%c%s", 0, cap_string);
381 for (cp = cert.buf; cp < cert.buf + cert.len; cp = np) {
382 np = next_line(cp, cert.buf + cert.len - cp);
383 packet_buf_write(req_buf,
384 "%.*s", (int)(np - cp), cp);
386 packet_buf_write(req_buf, "push-cert-end\n");
388 free_return:
389 free(signing_key);
390 strbuf_release(&cert);
391 return update_seen;
394 #define NONCE_LEN_LIMIT 256
396 static void reject_invalid_nonce(const char *nonce, int len)
398 int i = 0;
400 if (NONCE_LEN_LIMIT <= len)
401 die("the receiving end asked to sign an invalid nonce <%.*s>",
402 len, nonce);
404 for (i = 0; i < len; i++) {
405 int ch = nonce[i] & 0xFF;
406 if (isalnum(ch) ||
407 ch == '-' || ch == '.' ||
408 ch == '/' || ch == '+' ||
409 ch == '=' || ch == '_')
410 continue;
411 die("the receiving end asked to sign an invalid nonce <%.*s>",
412 len, nonce);
416 static void get_commons_through_negotiation(const char *url,
417 const struct ref *remote_refs,
418 struct oid_array *commons)
420 struct child_process child = CHILD_PROCESS_INIT;
421 const struct ref *ref;
422 int len = the_hash_algo->hexsz + 1; /* hash + NL */
424 child.git_cmd = 1;
425 child.no_stdin = 1;
426 child.out = -1;
427 strvec_pushl(&child.args, "fetch", "--negotiate-only", NULL);
428 for (ref = remote_refs; ref; ref = ref->next)
429 strvec_pushf(&child.args, "--negotiation-tip=%s", oid_to_hex(&ref->new_oid));
430 strvec_push(&child.args, url);
432 if (start_command(&child))
433 die(_("send-pack: unable to fork off fetch subprocess"));
435 do {
436 char hex_hash[GIT_MAX_HEXSZ + 1];
437 int read_len = read_in_full(child.out, hex_hash, len);
438 struct object_id oid;
439 const char *end;
441 if (!read_len)
442 break;
443 if (read_len != len)
444 die("invalid length read %d", read_len);
445 if (parse_oid_hex(hex_hash, &oid, &end) || *end != '\n')
446 die("invalid hash");
447 oid_array_append(commons, &oid);
448 } while (1);
450 if (finish_command(&child)) {
452 * The information that push negotiation provides is useful but
453 * not mandatory.
455 warning(_("push negotiation failed; proceeding anyway with push"));
459 int send_pack(struct send_pack_args *args,
460 int fd[], struct child_process *conn,
461 struct ref *remote_refs,
462 struct oid_array *extra_have)
464 struct oid_array commons = OID_ARRAY_INIT;
465 int in = fd[0];
466 int out = fd[1];
467 struct strbuf req_buf = STRBUF_INIT;
468 struct strbuf cap_buf = STRBUF_INIT;
469 struct ref *ref;
470 int need_pack_data = 0;
471 int allow_deleting_refs = 0;
472 int status_report = 0;
473 int use_sideband = 0;
474 int quiet_supported = 0;
475 int agent_supported = 0;
476 int advertise_sid = 0;
477 int push_negotiate = 0;
478 int use_atomic = 0;
479 int atomic_supported = 0;
480 int use_push_options = 0;
481 int push_options_supported = 0;
482 int object_format_supported = 0;
483 unsigned cmds_sent = 0;
484 int ret;
485 struct async demux;
486 const char *push_cert_nonce = NULL;
487 struct packet_reader reader;
489 if (!remote_refs) {
490 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
491 "Perhaps you should specify a branch.\n");
492 return 0;
495 git_config_get_bool("push.negotiate", &push_negotiate);
496 if (push_negotiate)
497 get_commons_through_negotiation(args->url, remote_refs, &commons);
499 git_config_get_bool("transfer.advertisesid", &advertise_sid);
501 /* Does the other end support the reporting? */
502 if (server_supports("report-status-v2"))
503 status_report = 2;
504 else if (server_supports("report-status"))
505 status_report = 1;
506 if (server_supports("delete-refs"))
507 allow_deleting_refs = 1;
508 if (server_supports("ofs-delta"))
509 args->use_ofs_delta = 1;
510 if (server_supports("side-band-64k"))
511 use_sideband = 1;
512 if (server_supports("quiet"))
513 quiet_supported = 1;
514 if (server_supports("agent"))
515 agent_supported = 1;
516 if (!server_supports("session-id"))
517 advertise_sid = 0;
518 if (server_supports("no-thin"))
519 args->use_thin_pack = 0;
520 if (server_supports("atomic"))
521 atomic_supported = 1;
522 if (server_supports("push-options"))
523 push_options_supported = 1;
525 if (!server_supports_hash(the_hash_algo->name, &object_format_supported))
526 die(_("the receiving end does not support this repository's hash algorithm"));
528 if (args->push_cert != SEND_PACK_PUSH_CERT_NEVER) {
529 int len;
530 push_cert_nonce = server_feature_value("push-cert", &len);
531 if (push_cert_nonce) {
532 reject_invalid_nonce(push_cert_nonce, len);
533 push_cert_nonce = xmemdupz(push_cert_nonce, len);
534 } else if (args->push_cert == SEND_PACK_PUSH_CERT_ALWAYS) {
535 die(_("the receiving end does not support --signed push"));
536 } else if (args->push_cert == SEND_PACK_PUSH_CERT_IF_ASKED) {
537 warning(_("not sending a push certificate since the"
538 " receiving end does not support --signed"
539 " push"));
543 if (args->atomic && !atomic_supported)
544 die(_("the receiving end does not support --atomic push"));
546 use_atomic = atomic_supported && args->atomic;
548 if (args->push_options && !push_options_supported)
549 die(_("the receiving end does not support push options"));
551 use_push_options = push_options_supported && args->push_options;
553 if (status_report == 1)
554 strbuf_addstr(&cap_buf, " report-status");
555 else if (status_report == 2)
556 strbuf_addstr(&cap_buf, " report-status-v2");
557 if (use_sideband)
558 strbuf_addstr(&cap_buf, " side-band-64k");
559 if (quiet_supported && (args->quiet || !args->progress))
560 strbuf_addstr(&cap_buf, " quiet");
561 if (use_atomic)
562 strbuf_addstr(&cap_buf, " atomic");
563 if (use_push_options)
564 strbuf_addstr(&cap_buf, " push-options");
565 if (object_format_supported)
566 strbuf_addf(&cap_buf, " object-format=%s", the_hash_algo->name);
567 if (agent_supported)
568 strbuf_addf(&cap_buf, " agent=%s", git_user_agent_sanitized());
569 if (advertise_sid)
570 strbuf_addf(&cap_buf, " session-id=%s", trace2_session_id());
573 * NEEDSWORK: why does delete-refs have to be so specific to
574 * send-pack machinery that set_ref_status_for_push() cannot
575 * set this bit for us???
577 for (ref = remote_refs; ref; ref = ref->next)
578 if (ref->deletion && !allow_deleting_refs)
579 ref->status = REF_STATUS_REJECT_NODELETE;
582 * Clear the status for each ref and see if we need to send
583 * the pack data.
585 for (ref = remote_refs; ref; ref = ref->next) {
586 switch (check_to_send_update(ref, args)) {
587 case 0: /* no error */
588 break;
589 case CHECK_REF_STATUS_REJECTED:
591 * When we know the server would reject a ref update if
592 * we were to send it and we're trying to send the refs
593 * atomically, abort the whole operation.
595 if (use_atomic) {
596 strbuf_release(&req_buf);
597 strbuf_release(&cap_buf);
598 reject_atomic_push(remote_refs, args->send_mirror);
599 error("atomic push failed for ref %s. status: %d\n",
600 ref->name, ref->status);
601 return args->porcelain ? 0 : -1;
603 /* else fallthrough */
604 default:
605 continue;
607 if (!ref->deletion)
608 need_pack_data = 1;
610 if (args->dry_run || !status_report)
611 ref->status = REF_STATUS_OK;
612 else
613 ref->status = REF_STATUS_EXPECTING_REPORT;
616 if (!args->dry_run)
617 advertise_shallow_grafts_buf(&req_buf);
620 * Finally, tell the other end!
622 if (!args->dry_run && push_cert_nonce)
623 cmds_sent = generate_push_cert(&req_buf, remote_refs, args,
624 cap_buf.buf, push_cert_nonce);
625 else if (!args->dry_run)
626 for (ref = remote_refs; ref; ref = ref->next) {
627 char *old_hex, *new_hex;
629 if (check_to_send_update(ref, args) < 0)
630 continue;
632 old_hex = oid_to_hex(&ref->old_oid);
633 new_hex = oid_to_hex(&ref->new_oid);
634 if (!cmds_sent) {
635 packet_buf_write(&req_buf,
636 "%s %s %s%c%s",
637 old_hex, new_hex, ref->name, 0,
638 cap_buf.buf);
639 cmds_sent = 1;
640 } else {
641 packet_buf_write(&req_buf, "%s %s %s",
642 old_hex, new_hex, ref->name);
646 if (use_push_options) {
647 struct string_list_item *item;
649 packet_buf_flush(&req_buf);
650 for_each_string_list_item(item, args->push_options)
651 packet_buf_write(&req_buf, "%s", item->string);
654 if (args->stateless_rpc) {
655 if (!args->dry_run && (cmds_sent || is_repository_shallow(the_repository))) {
656 packet_buf_flush(&req_buf);
657 send_sideband(out, -1, req_buf.buf, req_buf.len, LARGE_PACKET_MAX);
659 } else {
660 write_or_die(out, req_buf.buf, req_buf.len);
661 packet_flush(out);
663 strbuf_release(&req_buf);
664 strbuf_release(&cap_buf);
666 if (use_sideband && cmds_sent) {
667 memset(&demux, 0, sizeof(demux));
668 demux.proc = sideband_demux;
669 demux.data = fd;
670 demux.out = -1;
671 demux.isolate_sigpipe = 1;
672 if (start_async(&demux))
673 die("send-pack: unable to fork off sideband demultiplexer");
674 in = demux.out;
677 packet_reader_init(&reader, in, NULL, 0,
678 PACKET_READ_CHOMP_NEWLINE |
679 PACKET_READ_DIE_ON_ERR_PACKET);
681 if (need_pack_data && cmds_sent) {
682 if (pack_objects(out, remote_refs, extra_have, &commons, args) < 0) {
683 if (args->stateless_rpc)
684 close(out);
685 if (git_connection_is_socket(conn))
686 shutdown(fd[0], SHUT_WR);
689 * Do not even bother with the return value; we know we
690 * are failing, and just want the error() side effects,
691 * as well as marking refs with their remote status (if
692 * we get one).
694 if (status_report)
695 receive_status(&reader, remote_refs);
697 if (use_sideband) {
698 close(demux.out);
699 finish_async(&demux);
701 fd[1] = -1;
702 return -1;
704 if (!args->stateless_rpc)
705 /* Closed by pack_objects() via start_command() */
706 fd[1] = -1;
708 if (args->stateless_rpc && cmds_sent)
709 packet_flush(out);
711 if (status_report && cmds_sent)
712 ret = receive_status(&reader, remote_refs);
713 else
714 ret = 0;
715 if (args->stateless_rpc)
716 packet_flush(out);
718 if (use_sideband && cmds_sent) {
719 close(demux.out);
720 if (finish_async(&demux)) {
721 error("error in sideband demultiplexer");
722 ret = -1;
726 if (ret < 0)
727 return ret;
729 if (args->porcelain)
730 return 0;
732 for (ref = remote_refs; ref; ref = ref->next) {
733 switch (ref->status) {
734 case REF_STATUS_NONE:
735 case REF_STATUS_UPTODATE:
736 case REF_STATUS_OK:
737 break;
738 default:
739 return -1;
742 return 0;