t/helper: mark unused argv/argc arguments
[git.git] / send-pack.c
blob423a5cfe22d140026474695340488ae1423a6317
1 #include "git-compat-util.h"
2 #include "config.h"
3 #include "commit.h"
4 #include "hex.h"
5 #include "refs.h"
6 #include "object-store.h"
7 #include "pkt-line.h"
8 #include "sideband.h"
9 #include "run-command.h"
10 #include "remote.h"
11 #include "connect.h"
12 #include "send-pack.h"
13 #include "quote.h"
14 #include "transport.h"
15 #include "version.h"
16 #include "oid-array.h"
17 #include "gpg-interface.h"
18 #include "cache.h"
19 #include "shallow.h"
21 int option_parse_push_signed(const struct option *opt,
22 const char *arg, int unset)
24 if (unset) {
25 *(int *)(opt->value) = SEND_PACK_PUSH_CERT_NEVER;
26 return 0;
28 switch (git_parse_maybe_bool(arg)) {
29 case 1:
30 *(int *)(opt->value) = SEND_PACK_PUSH_CERT_ALWAYS;
31 return 0;
32 case 0:
33 *(int *)(opt->value) = SEND_PACK_PUSH_CERT_NEVER;
34 return 0;
36 if (!strcasecmp("if-asked", arg)) {
37 *(int *)(opt->value) = SEND_PACK_PUSH_CERT_IF_ASKED;
38 return 0;
40 die("bad %s argument: %s", opt->long_name, arg);
43 static void feed_object(const struct object_id *oid, FILE *fh, int negative)
45 if (negative &&
46 !has_object_file_with_flags(oid,
47 OBJECT_INFO_SKIP_FETCH_OBJECT |
48 OBJECT_INFO_QUICK))
49 return;
51 if (negative)
52 putc('^', fh);
53 fputs(oid_to_hex(oid), fh);
54 putc('\n', fh);
58 * Make a pack stream and spit it out into file descriptor fd
60 static int pack_objects(int fd, struct ref *refs, struct oid_array *advertised,
61 struct oid_array *negotiated,
62 struct send_pack_args *args)
65 * The child becomes pack-objects --revs; we feed
66 * the revision parameters to it via its stdin and
67 * let its stdout go back to the other end.
69 struct child_process po = CHILD_PROCESS_INIT;
70 FILE *po_in;
71 int i;
72 int rc;
74 strvec_push(&po.args, "pack-objects");
75 strvec_push(&po.args, "--all-progress-implied");
76 strvec_push(&po.args, "--revs");
77 strvec_push(&po.args, "--stdout");
78 if (args->use_thin_pack)
79 strvec_push(&po.args, "--thin");
80 if (args->use_ofs_delta)
81 strvec_push(&po.args, "--delta-base-offset");
82 if (args->quiet || !args->progress)
83 strvec_push(&po.args, "-q");
84 if (args->progress)
85 strvec_push(&po.args, "--progress");
86 if (is_repository_shallow(the_repository))
87 strvec_push(&po.args, "--shallow");
88 if (args->disable_bitmaps)
89 strvec_push(&po.args, "--no-use-bitmap-index");
90 po.in = -1;
91 po.out = args->stateless_rpc ? -1 : fd;
92 po.git_cmd = 1;
93 po.clean_on_exit = 1;
94 if (start_command(&po))
95 die_errno("git pack-objects failed");
98 * We feed the pack-objects we just spawned with revision
99 * parameters by writing to the pipe.
101 po_in = xfdopen(po.in, "w");
102 for (i = 0; i < advertised->nr; i++)
103 feed_object(&advertised->oid[i], po_in, 1);
104 for (i = 0; i < negotiated->nr; i++)
105 feed_object(&negotiated->oid[i], po_in, 1);
107 while (refs) {
108 if (!is_null_oid(&refs->old_oid))
109 feed_object(&refs->old_oid, po_in, 1);
110 if (!is_null_oid(&refs->new_oid))
111 feed_object(&refs->new_oid, po_in, 0);
112 refs = refs->next;
115 fflush(po_in);
116 if (ferror(po_in))
117 die_errno("error writing to pack-objects");
118 fclose(po_in);
120 if (args->stateless_rpc) {
121 char *buf = xmalloc(LARGE_PACKET_MAX);
122 while (1) {
123 ssize_t n = xread(po.out, buf, LARGE_PACKET_MAX);
124 if (n <= 0)
125 break;
126 send_sideband(fd, -1, buf, n, LARGE_PACKET_MAX);
128 free(buf);
129 close(po.out);
130 po.out = -1;
133 rc = finish_command(&po);
134 if (rc) {
136 * For a normal non-zero exit, we assume pack-objects wrote
137 * something useful to stderr. For death by signal, though,
138 * we should mention it to the user. The exception is SIGPIPE
139 * (141), because that's a normal occurrence if the remote end
140 * hangs up (and we'll report that by trying to read the unpack
141 * status).
143 if (rc > 128 && rc != 141)
144 error("pack-objects died of signal %d", rc - 128);
145 return -1;
147 return 0;
150 static int receive_unpack_status(struct packet_reader *reader)
152 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
153 return error(_("unexpected flush packet while reading remote unpack status"));
154 if (!skip_prefix(reader->line, "unpack ", &reader->line))
155 return error(_("unable to parse remote unpack status: %s"), reader->line);
156 if (strcmp(reader->line, "ok"))
157 return error(_("remote unpack failed: %s"), reader->line);
158 return 0;
161 static int receive_status(struct packet_reader *reader, struct ref *refs)
163 struct ref *hint;
164 int ret;
165 struct ref_push_report *report = NULL;
166 int new_report = 0;
167 int once = 0;
169 hint = NULL;
170 ret = receive_unpack_status(reader);
171 while (1) {
172 struct object_id old_oid, new_oid;
173 const char *head;
174 const char *refname;
175 char *p;
176 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
177 break;
178 head = reader->line;
179 p = strchr(head, ' ');
180 if (!p) {
181 error("invalid status line from remote: %s", reader->line);
182 ret = -1;
183 break;
185 *p++ = '\0';
187 if (!strcmp(head, "option")) {
188 const char *key, *val;
190 if (!hint || !(report || new_report)) {
191 if (!once++)
192 error("'option' without a matching 'ok/ng' directive");
193 ret = -1;
194 continue;
196 if (new_report) {
197 if (!hint->report) {
198 CALLOC_ARRAY(hint->report, 1);
199 report = hint->report;
200 } else {
201 report = hint->report;
202 while (report->next)
203 report = report->next;
204 CALLOC_ARRAY(report->next, 1);
205 report = report->next;
207 new_report = 0;
209 key = p;
210 p = strchr(key, ' ');
211 if (p)
212 *p++ = '\0';
213 val = p;
214 if (!strcmp(key, "refname"))
215 report->ref_name = xstrdup_or_null(val);
216 else if (!strcmp(key, "old-oid") && val &&
217 !parse_oid_hex(val, &old_oid, &val))
218 report->old_oid = oiddup(&old_oid);
219 else if (!strcmp(key, "new-oid") && val &&
220 !parse_oid_hex(val, &new_oid, &val))
221 report->new_oid = oiddup(&new_oid);
222 else if (!strcmp(key, "forced-update"))
223 report->forced_update = 1;
224 continue;
227 report = NULL;
228 new_report = 0;
229 if (strcmp(head, "ok") && strcmp(head, "ng")) {
230 error("invalid ref status from remote: %s", head);
231 ret = -1;
232 break;
234 refname = p;
235 p = strchr(refname, ' ');
236 if (p)
237 *p++ = '\0';
238 /* first try searching at our hint, falling back to all refs */
239 if (hint)
240 hint = find_ref_by_name(hint, refname);
241 if (!hint)
242 hint = find_ref_by_name(refs, refname);
243 if (!hint) {
244 warning("remote reported status on unknown ref: %s",
245 refname);
246 continue;
248 if (hint->status != REF_STATUS_EXPECTING_REPORT &&
249 hint->status != REF_STATUS_OK &&
250 hint->status != REF_STATUS_REMOTE_REJECT) {
251 warning("remote reported status on unexpected ref: %s",
252 refname);
253 continue;
255 if (!strcmp(head, "ng")) {
256 hint->status = REF_STATUS_REMOTE_REJECT;
257 if (p)
258 hint->remote_status = xstrdup(p);
259 else
260 hint->remote_status = "failed";
261 } else {
262 hint->status = REF_STATUS_OK;
263 hint->remote_status = xstrdup_or_null(p);
264 new_report = 1;
267 return ret;
270 static int sideband_demux(int in UNUSED, int out, void *data)
272 int *fd = data, ret;
273 if (async_with_fork())
274 close(fd[1]);
275 ret = recv_sideband("send-pack", fd[0], out);
276 close(out);
277 return ret;
280 static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb)
282 struct strbuf *sb = cb;
283 if (graft->nr_parent == -1)
284 packet_buf_write(sb, "shallow %s\n", oid_to_hex(&graft->oid));
285 return 0;
288 static void advertise_shallow_grafts_buf(struct strbuf *sb)
290 if (!is_repository_shallow(the_repository))
291 return;
292 for_each_commit_graft(advertise_shallow_grafts_cb, sb);
295 #define CHECK_REF_NO_PUSH -1
296 #define CHECK_REF_STATUS_REJECTED -2
297 #define CHECK_REF_UPTODATE -3
298 static int check_to_send_update(const struct ref *ref, const struct send_pack_args *args)
300 if (!ref->peer_ref && !args->send_mirror)
301 return CHECK_REF_NO_PUSH;
303 /* Check for statuses set by set_ref_status_for_push() */
304 switch (ref->status) {
305 case REF_STATUS_REJECT_NONFASTFORWARD:
306 case REF_STATUS_REJECT_ALREADY_EXISTS:
307 case REF_STATUS_REJECT_FETCH_FIRST:
308 case REF_STATUS_REJECT_NEEDS_FORCE:
309 case REF_STATUS_REJECT_STALE:
310 case REF_STATUS_REJECT_REMOTE_UPDATED:
311 case REF_STATUS_REJECT_NODELETE:
312 return CHECK_REF_STATUS_REJECTED;
313 case REF_STATUS_UPTODATE:
314 return CHECK_REF_UPTODATE;
316 default:
317 case REF_STATUS_EXPECTING_REPORT:
318 /* already passed checks on the local side */
319 case REF_STATUS_OK:
320 /* of course this is OK */
321 return 0;
326 * the beginning of the next line, or the end of buffer.
328 * NEEDSWORK: perhaps move this to git-compat-util.h or somewhere and
329 * convert many similar uses found by "git grep -A4 memchr".
331 static const char *next_line(const char *line, size_t len)
333 const char *nl = memchr(line, '\n', len);
334 if (!nl)
335 return line + len; /* incomplete line */
336 return nl + 1;
339 static int generate_push_cert(struct strbuf *req_buf,
340 const struct ref *remote_refs,
341 struct send_pack_args *args,
342 const char *cap_string,
343 const char *push_cert_nonce)
345 const struct ref *ref;
346 struct string_list_item *item;
347 char *signing_key_id = xstrdup(get_signing_key_id());
348 const char *cp, *np;
349 struct strbuf cert = STRBUF_INIT;
350 int update_seen = 0;
352 strbuf_addstr(&cert, "certificate version 0.1\n");
353 strbuf_addf(&cert, "pusher %s ", signing_key_id);
354 datestamp(&cert);
355 strbuf_addch(&cert, '\n');
356 if (args->url && *args->url) {
357 char *anon_url = transport_anonymize_url(args->url);
358 strbuf_addf(&cert, "pushee %s\n", anon_url);
359 free(anon_url);
361 if (push_cert_nonce[0])
362 strbuf_addf(&cert, "nonce %s\n", push_cert_nonce);
363 if (args->push_options)
364 for_each_string_list_item(item, args->push_options)
365 strbuf_addf(&cert, "push-option %s\n", item->string);
366 strbuf_addstr(&cert, "\n");
368 for (ref = remote_refs; ref; ref = ref->next) {
369 if (check_to_send_update(ref, args) < 0)
370 continue;
371 update_seen = 1;
372 strbuf_addf(&cert, "%s %s %s\n",
373 oid_to_hex(&ref->old_oid),
374 oid_to_hex(&ref->new_oid),
375 ref->name);
377 if (!update_seen)
378 goto free_return;
380 if (sign_buffer(&cert, &cert, get_signing_key()))
381 die(_("failed to sign the push certificate"));
383 packet_buf_write(req_buf, "push-cert%c%s", 0, cap_string);
384 for (cp = cert.buf; cp < cert.buf + cert.len; cp = np) {
385 np = next_line(cp, cert.buf + cert.len - cp);
386 packet_buf_write(req_buf,
387 "%.*s", (int)(np - cp), cp);
389 packet_buf_write(req_buf, "push-cert-end\n");
391 free_return:
392 free(signing_key_id);
393 strbuf_release(&cert);
394 return update_seen;
397 #define NONCE_LEN_LIMIT 256
399 static void reject_invalid_nonce(const char *nonce, int len)
401 int i = 0;
403 if (NONCE_LEN_LIMIT <= len)
404 die("the receiving end asked to sign an invalid nonce <%.*s>",
405 len, nonce);
407 for (i = 0; i < len; i++) {
408 int ch = nonce[i] & 0xFF;
409 if (isalnum(ch) ||
410 ch == '-' || ch == '.' ||
411 ch == '/' || ch == '+' ||
412 ch == '=' || ch == '_')
413 continue;
414 die("the receiving end asked to sign an invalid nonce <%.*s>",
415 len, nonce);
419 static void get_commons_through_negotiation(const char *url,
420 const struct ref *remote_refs,
421 struct oid_array *commons)
423 struct child_process child = CHILD_PROCESS_INIT;
424 const struct ref *ref;
425 int len = the_hash_algo->hexsz + 1; /* hash + NL */
427 child.git_cmd = 1;
428 child.no_stdin = 1;
429 child.out = -1;
430 strvec_pushl(&child.args, "fetch", "--negotiate-only", NULL);
431 for (ref = remote_refs; ref; ref = ref->next) {
432 if (!is_null_oid(&ref->new_oid))
433 strvec_pushf(&child.args, "--negotiation-tip=%s", oid_to_hex(&ref->new_oid));
435 strvec_push(&child.args, url);
437 if (start_command(&child))
438 die(_("send-pack: unable to fork off fetch subprocess"));
440 do {
441 char hex_hash[GIT_MAX_HEXSZ + 1];
442 int read_len = read_in_full(child.out, hex_hash, len);
443 struct object_id oid;
444 const char *end;
446 if (!read_len)
447 break;
448 if (read_len != len)
449 die("invalid length read %d", read_len);
450 if (parse_oid_hex(hex_hash, &oid, &end) || *end != '\n')
451 die("invalid hash");
452 oid_array_append(commons, &oid);
453 } while (1);
455 if (finish_command(&child)) {
457 * The information that push negotiation provides is useful but
458 * not mandatory.
460 warning(_("push negotiation failed; proceeding anyway with push"));
464 int send_pack(struct send_pack_args *args,
465 int fd[], struct child_process *conn,
466 struct ref *remote_refs,
467 struct oid_array *extra_have)
469 struct oid_array commons = OID_ARRAY_INIT;
470 int in = fd[0];
471 int out = fd[1];
472 struct strbuf req_buf = STRBUF_INIT;
473 struct strbuf cap_buf = STRBUF_INIT;
474 struct ref *ref;
475 int need_pack_data = 0;
476 int allow_deleting_refs = 0;
477 int status_report = 0;
478 int use_sideband = 0;
479 int quiet_supported = 0;
480 int agent_supported = 0;
481 int advertise_sid = 0;
482 int push_negotiate = 0;
483 int use_atomic = 0;
484 int atomic_supported = 0;
485 int use_push_options = 0;
486 int push_options_supported = 0;
487 int object_format_supported = 0;
488 unsigned cmds_sent = 0;
489 int ret;
490 struct async demux;
491 const char *push_cert_nonce = NULL;
492 struct packet_reader reader;
493 int use_bitmaps;
495 if (!remote_refs) {
496 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
497 "Perhaps you should specify a branch.\n");
498 return 0;
501 git_config_get_bool("push.negotiate", &push_negotiate);
502 if (push_negotiate)
503 get_commons_through_negotiation(args->url, remote_refs, &commons);
505 if (!git_config_get_bool("push.usebitmaps", &use_bitmaps))
506 args->disable_bitmaps = !use_bitmaps;
508 git_config_get_bool("transfer.advertisesid", &advertise_sid);
510 /* Does the other end support the reporting? */
511 if (server_supports("report-status-v2"))
512 status_report = 2;
513 else if (server_supports("report-status"))
514 status_report = 1;
515 if (server_supports("delete-refs"))
516 allow_deleting_refs = 1;
517 if (server_supports("ofs-delta"))
518 args->use_ofs_delta = 1;
519 if (server_supports("side-band-64k"))
520 use_sideband = 1;
521 if (server_supports("quiet"))
522 quiet_supported = 1;
523 if (server_supports("agent"))
524 agent_supported = 1;
525 if (!server_supports("session-id"))
526 advertise_sid = 0;
527 if (server_supports("no-thin"))
528 args->use_thin_pack = 0;
529 if (server_supports("atomic"))
530 atomic_supported = 1;
531 if (server_supports("push-options"))
532 push_options_supported = 1;
534 if (!server_supports_hash(the_hash_algo->name, &object_format_supported))
535 die(_("the receiving end does not support this repository's hash algorithm"));
537 if (args->push_cert != SEND_PACK_PUSH_CERT_NEVER) {
538 int len;
539 push_cert_nonce = server_feature_value("push-cert", &len);
540 if (push_cert_nonce) {
541 reject_invalid_nonce(push_cert_nonce, len);
542 push_cert_nonce = xmemdupz(push_cert_nonce, len);
543 } else if (args->push_cert == SEND_PACK_PUSH_CERT_ALWAYS) {
544 die(_("the receiving end does not support --signed push"));
545 } else if (args->push_cert == SEND_PACK_PUSH_CERT_IF_ASKED) {
546 warning(_("not sending a push certificate since the"
547 " receiving end does not support --signed"
548 " push"));
552 if (args->atomic && !atomic_supported)
553 die(_("the receiving end does not support --atomic push"));
555 use_atomic = atomic_supported && args->atomic;
557 if (args->push_options && !push_options_supported)
558 die(_("the receiving end does not support push options"));
560 use_push_options = push_options_supported && args->push_options;
562 if (status_report == 1)
563 strbuf_addstr(&cap_buf, " report-status");
564 else if (status_report == 2)
565 strbuf_addstr(&cap_buf, " report-status-v2");
566 if (use_sideband)
567 strbuf_addstr(&cap_buf, " side-band-64k");
568 if (quiet_supported && (args->quiet || !args->progress))
569 strbuf_addstr(&cap_buf, " quiet");
570 if (use_atomic)
571 strbuf_addstr(&cap_buf, " atomic");
572 if (use_push_options)
573 strbuf_addstr(&cap_buf, " push-options");
574 if (object_format_supported)
575 strbuf_addf(&cap_buf, " object-format=%s", the_hash_algo->name);
576 if (agent_supported)
577 strbuf_addf(&cap_buf, " agent=%s", git_user_agent_sanitized());
578 if (advertise_sid)
579 strbuf_addf(&cap_buf, " session-id=%s", trace2_session_id());
582 * NEEDSWORK: why does delete-refs have to be so specific to
583 * send-pack machinery that set_ref_status_for_push() cannot
584 * set this bit for us???
586 for (ref = remote_refs; ref; ref = ref->next)
587 if (ref->deletion && !allow_deleting_refs)
588 ref->status = REF_STATUS_REJECT_NODELETE;
591 * Clear the status for each ref and see if we need to send
592 * the pack data.
594 for (ref = remote_refs; ref; ref = ref->next) {
595 switch (check_to_send_update(ref, args)) {
596 case 0: /* no error */
597 break;
598 case CHECK_REF_STATUS_REJECTED:
600 * When we know the server would reject a ref update if
601 * we were to send it and we're trying to send the refs
602 * atomically, abort the whole operation.
604 if (use_atomic) {
605 strbuf_release(&req_buf);
606 strbuf_release(&cap_buf);
607 reject_atomic_push(remote_refs, args->send_mirror);
608 error("atomic push failed for ref %s. status: %d\n",
609 ref->name, ref->status);
610 return args->porcelain ? 0 : -1;
612 /* else fallthrough */
613 default:
614 continue;
616 if (!ref->deletion)
617 need_pack_data = 1;
619 if (args->dry_run || !status_report)
620 ref->status = REF_STATUS_OK;
621 else
622 ref->status = REF_STATUS_EXPECTING_REPORT;
625 if (!args->dry_run)
626 advertise_shallow_grafts_buf(&req_buf);
629 * Finally, tell the other end!
631 if (!args->dry_run && push_cert_nonce)
632 cmds_sent = generate_push_cert(&req_buf, remote_refs, args,
633 cap_buf.buf, push_cert_nonce);
634 else if (!args->dry_run)
635 for (ref = remote_refs; ref; ref = ref->next) {
636 char *old_hex, *new_hex;
638 if (check_to_send_update(ref, args) < 0)
639 continue;
641 old_hex = oid_to_hex(&ref->old_oid);
642 new_hex = oid_to_hex(&ref->new_oid);
643 if (!cmds_sent) {
644 packet_buf_write(&req_buf,
645 "%s %s %s%c%s",
646 old_hex, new_hex, ref->name, 0,
647 cap_buf.buf);
648 cmds_sent = 1;
649 } else {
650 packet_buf_write(&req_buf, "%s %s %s",
651 old_hex, new_hex, ref->name);
655 if (use_push_options) {
656 struct string_list_item *item;
658 packet_buf_flush(&req_buf);
659 for_each_string_list_item(item, args->push_options)
660 packet_buf_write(&req_buf, "%s", item->string);
663 if (args->stateless_rpc) {
664 if (!args->dry_run && (cmds_sent || is_repository_shallow(the_repository))) {
665 packet_buf_flush(&req_buf);
666 send_sideband(out, -1, req_buf.buf, req_buf.len, LARGE_PACKET_MAX);
668 } else {
669 write_or_die(out, req_buf.buf, req_buf.len);
670 packet_flush(out);
672 strbuf_release(&req_buf);
673 strbuf_release(&cap_buf);
675 if (use_sideband && cmds_sent) {
676 memset(&demux, 0, sizeof(demux));
677 demux.proc = sideband_demux;
678 demux.data = fd;
679 demux.out = -1;
680 demux.isolate_sigpipe = 1;
681 if (start_async(&demux))
682 die("send-pack: unable to fork off sideband demultiplexer");
683 in = demux.out;
686 packet_reader_init(&reader, in, NULL, 0,
687 PACKET_READ_CHOMP_NEWLINE |
688 PACKET_READ_DIE_ON_ERR_PACKET);
690 if (need_pack_data && cmds_sent) {
691 if (pack_objects(out, remote_refs, extra_have, &commons, args) < 0) {
692 if (args->stateless_rpc)
693 close(out);
694 if (git_connection_is_socket(conn))
695 shutdown(fd[0], SHUT_WR);
698 * Do not even bother with the return value; we know we
699 * are failing, and just want the error() side effects,
700 * as well as marking refs with their remote status (if
701 * we get one).
703 if (status_report)
704 receive_status(&reader, remote_refs);
706 if (use_sideband) {
707 close(demux.out);
708 finish_async(&demux);
710 fd[1] = -1;
711 return -1;
713 if (!args->stateless_rpc)
714 /* Closed by pack_objects() via start_command() */
715 fd[1] = -1;
717 if (args->stateless_rpc && cmds_sent)
718 packet_flush(out);
720 if (status_report && cmds_sent)
721 ret = receive_status(&reader, remote_refs);
722 else
723 ret = 0;
724 if (args->stateless_rpc)
725 packet_flush(out);
727 if (use_sideband && cmds_sent) {
728 close(demux.out);
729 if (finish_async(&demux)) {
730 error("error in sideband demultiplexer");
731 ret = -1;
735 if (ret < 0)
736 return ret;
738 if (args->porcelain)
739 return 0;
741 for (ref = remote_refs; ref; ref = ref->next) {
742 switch (ref->status) {
743 case REF_STATUS_NONE:
744 case REF_STATUS_UPTODATE:
745 case REF_STATUS_OK:
746 break;
747 default:
748 return -1;
751 return 0;