Merge branch 'master' of github.com:nafmo/git-l10n-sv
[alt-git.git] / send-pack.c
blobf2e19838c9c342b3b9d6df5e9bf24d0add0d47d1
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 if (args->disable_bitmaps)
88 strvec_push(&po.args, "--no-use-bitmap-index");
89 po.in = -1;
90 po.out = args->stateless_rpc ? -1 : fd;
91 po.git_cmd = 1;
92 po.clean_on_exit = 1;
93 if (start_command(&po))
94 die_errno("git pack-objects failed");
97 * We feed the pack-objects we just spawned with revision
98 * parameters by writing to the pipe.
100 po_in = xfdopen(po.in, "w");
101 for (i = 0; i < advertised->nr; i++)
102 feed_object(&advertised->oid[i], po_in, 1);
103 for (i = 0; i < negotiated->nr; i++)
104 feed_object(&negotiated->oid[i], po_in, 1);
106 while (refs) {
107 if (!is_null_oid(&refs->old_oid))
108 feed_object(&refs->old_oid, po_in, 1);
109 if (!is_null_oid(&refs->new_oid))
110 feed_object(&refs->new_oid, po_in, 0);
111 refs = refs->next;
114 fflush(po_in);
115 if (ferror(po_in))
116 die_errno("error writing to pack-objects");
117 fclose(po_in);
119 if (args->stateless_rpc) {
120 char *buf = xmalloc(LARGE_PACKET_MAX);
121 while (1) {
122 ssize_t n = xread(po.out, buf, LARGE_PACKET_MAX);
123 if (n <= 0)
124 break;
125 send_sideband(fd, -1, buf, n, LARGE_PACKET_MAX);
127 free(buf);
128 close(po.out);
129 po.out = -1;
132 rc = finish_command(&po);
133 if (rc) {
135 * For a normal non-zero exit, we assume pack-objects wrote
136 * something useful to stderr. For death by signal, though,
137 * we should mention it to the user. The exception is SIGPIPE
138 * (141), because that's a normal occurrence if the remote end
139 * hangs up (and we'll report that by trying to read the unpack
140 * status).
142 if (rc > 128 && rc != 141)
143 error("pack-objects died of signal %d", rc - 128);
144 return -1;
146 return 0;
149 static int receive_unpack_status(struct packet_reader *reader)
151 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
152 return error(_("unexpected flush packet while reading remote unpack status"));
153 if (!skip_prefix(reader->line, "unpack ", &reader->line))
154 return error(_("unable to parse remote unpack status: %s"), reader->line);
155 if (strcmp(reader->line, "ok"))
156 return error(_("remote unpack failed: %s"), reader->line);
157 return 0;
160 static int receive_status(struct packet_reader *reader, struct ref *refs)
162 struct ref *hint;
163 int ret;
164 struct ref_push_report *report = NULL;
165 int new_report = 0;
166 int once = 0;
168 hint = NULL;
169 ret = receive_unpack_status(reader);
170 while (1) {
171 struct object_id old_oid, new_oid;
172 const char *head;
173 const char *refname;
174 char *p;
175 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
176 break;
177 head = reader->line;
178 p = strchr(head, ' ');
179 if (!p) {
180 error("invalid status line from remote: %s", reader->line);
181 ret = -1;
182 break;
184 *p++ = '\0';
186 if (!strcmp(head, "option")) {
187 const char *key, *val;
189 if (!hint || !(report || new_report)) {
190 if (!once++)
191 error("'option' without a matching 'ok/ng' directive");
192 ret = -1;
193 continue;
195 if (new_report) {
196 if (!hint->report) {
197 CALLOC_ARRAY(hint->report, 1);
198 report = hint->report;
199 } else {
200 report = hint->report;
201 while (report->next)
202 report = report->next;
203 CALLOC_ARRAY(report->next, 1);
204 report = report->next;
206 new_report = 0;
208 key = p;
209 p = strchr(key, ' ');
210 if (p)
211 *p++ = '\0';
212 val = p;
213 if (!strcmp(key, "refname"))
214 report->ref_name = xstrdup_or_null(val);
215 else if (!strcmp(key, "old-oid") && val &&
216 !parse_oid_hex(val, &old_oid, &val))
217 report->old_oid = oiddup(&old_oid);
218 else if (!strcmp(key, "new-oid") && val &&
219 !parse_oid_hex(val, &new_oid, &val))
220 report->new_oid = oiddup(&new_oid);
221 else if (!strcmp(key, "forced-update"))
222 report->forced_update = 1;
223 continue;
226 report = NULL;
227 new_report = 0;
228 if (strcmp(head, "ok") && strcmp(head, "ng")) {
229 error("invalid ref status from remote: %s", head);
230 ret = -1;
231 break;
233 refname = p;
234 p = strchr(refname, ' ');
235 if (p)
236 *p++ = '\0';
237 /* first try searching at our hint, falling back to all refs */
238 if (hint)
239 hint = find_ref_by_name(hint, refname);
240 if (!hint)
241 hint = find_ref_by_name(refs, refname);
242 if (!hint) {
243 warning("remote reported status on unknown ref: %s",
244 refname);
245 continue;
247 if (hint->status != REF_STATUS_EXPECTING_REPORT &&
248 hint->status != REF_STATUS_OK &&
249 hint->status != REF_STATUS_REMOTE_REJECT) {
250 warning("remote reported status on unexpected ref: %s",
251 refname);
252 continue;
254 if (!strcmp(head, "ng")) {
255 hint->status = REF_STATUS_REMOTE_REJECT;
256 if (p)
257 hint->remote_status = xstrdup(p);
258 else
259 hint->remote_status = "failed";
260 } else {
261 hint->status = REF_STATUS_OK;
262 hint->remote_status = xstrdup_or_null(p);
263 new_report = 1;
266 return ret;
269 static int sideband_demux(int in UNUSED, int out, void *data)
271 int *fd = data, ret;
272 if (async_with_fork())
273 close(fd[1]);
274 ret = recv_sideband("send-pack", fd[0], out);
275 close(out);
276 return ret;
279 static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb)
281 struct strbuf *sb = cb;
282 if (graft->nr_parent == -1)
283 packet_buf_write(sb, "shallow %s\n", oid_to_hex(&graft->oid));
284 return 0;
287 static void advertise_shallow_grafts_buf(struct strbuf *sb)
289 if (!is_repository_shallow(the_repository))
290 return;
291 for_each_commit_graft(advertise_shallow_grafts_cb, sb);
294 #define CHECK_REF_NO_PUSH -1
295 #define CHECK_REF_STATUS_REJECTED -2
296 #define CHECK_REF_UPTODATE -3
297 static int check_to_send_update(const struct ref *ref, const struct send_pack_args *args)
299 if (!ref->peer_ref && !args->send_mirror)
300 return CHECK_REF_NO_PUSH;
302 /* Check for statuses set by set_ref_status_for_push() */
303 switch (ref->status) {
304 case REF_STATUS_REJECT_NONFASTFORWARD:
305 case REF_STATUS_REJECT_ALREADY_EXISTS:
306 case REF_STATUS_REJECT_FETCH_FIRST:
307 case REF_STATUS_REJECT_NEEDS_FORCE:
308 case REF_STATUS_REJECT_STALE:
309 case REF_STATUS_REJECT_REMOTE_UPDATED:
310 case REF_STATUS_REJECT_NODELETE:
311 return CHECK_REF_STATUS_REJECTED;
312 case REF_STATUS_UPTODATE:
313 return CHECK_REF_UPTODATE;
315 default:
316 case REF_STATUS_EXPECTING_REPORT:
317 /* already passed checks on the local side */
318 case REF_STATUS_OK:
319 /* of course this is OK */
320 return 0;
325 * the beginning of the next line, or the end of buffer.
327 * NEEDSWORK: perhaps move this to git-compat-util.h or somewhere and
328 * convert many similar uses found by "git grep -A4 memchr".
330 static const char *next_line(const char *line, size_t len)
332 const char *nl = memchr(line, '\n', len);
333 if (!nl)
334 return line + len; /* incomplete line */
335 return nl + 1;
338 static int generate_push_cert(struct strbuf *req_buf,
339 const struct ref *remote_refs,
340 struct send_pack_args *args,
341 const char *cap_string,
342 const char *push_cert_nonce)
344 const struct ref *ref;
345 struct string_list_item *item;
346 char *signing_key_id = xstrdup(get_signing_key_id());
347 const char *cp, *np;
348 struct strbuf cert = STRBUF_INIT;
349 int update_seen = 0;
351 strbuf_addstr(&cert, "certificate version 0.1\n");
352 strbuf_addf(&cert, "pusher %s ", signing_key_id);
353 datestamp(&cert);
354 strbuf_addch(&cert, '\n');
355 if (args->url && *args->url) {
356 char *anon_url = transport_anonymize_url(args->url);
357 strbuf_addf(&cert, "pushee %s\n", anon_url);
358 free(anon_url);
360 if (push_cert_nonce[0])
361 strbuf_addf(&cert, "nonce %s\n", push_cert_nonce);
362 if (args->push_options)
363 for_each_string_list_item(item, args->push_options)
364 strbuf_addf(&cert, "push-option %s\n", item->string);
365 strbuf_addstr(&cert, "\n");
367 for (ref = remote_refs; ref; ref = ref->next) {
368 if (check_to_send_update(ref, args) < 0)
369 continue;
370 update_seen = 1;
371 strbuf_addf(&cert, "%s %s %s\n",
372 oid_to_hex(&ref->old_oid),
373 oid_to_hex(&ref->new_oid),
374 ref->name);
376 if (!update_seen)
377 goto free_return;
379 if (sign_buffer(&cert, &cert, get_signing_key()))
380 die(_("failed to sign the push certificate"));
382 packet_buf_write(req_buf, "push-cert%c%s", 0, cap_string);
383 for (cp = cert.buf; cp < cert.buf + cert.len; cp = np) {
384 np = next_line(cp, cert.buf + cert.len - cp);
385 packet_buf_write(req_buf,
386 "%.*s", (int)(np - cp), cp);
388 packet_buf_write(req_buf, "push-cert-end\n");
390 free_return:
391 free(signing_key_id);
392 strbuf_release(&cert);
393 return update_seen;
396 #define NONCE_LEN_LIMIT 256
398 static void reject_invalid_nonce(const char *nonce, int len)
400 int i = 0;
402 if (NONCE_LEN_LIMIT <= len)
403 die("the receiving end asked to sign an invalid nonce <%.*s>",
404 len, nonce);
406 for (i = 0; i < len; i++) {
407 int ch = nonce[i] & 0xFF;
408 if (isalnum(ch) ||
409 ch == '-' || ch == '.' ||
410 ch == '/' || ch == '+' ||
411 ch == '=' || ch == '_')
412 continue;
413 die("the receiving end asked to sign an invalid nonce <%.*s>",
414 len, nonce);
418 static void get_commons_through_negotiation(const char *url,
419 const struct ref *remote_refs,
420 struct oid_array *commons)
422 struct child_process child = CHILD_PROCESS_INIT;
423 const struct ref *ref;
424 int len = the_hash_algo->hexsz + 1; /* hash + NL */
426 child.git_cmd = 1;
427 child.no_stdin = 1;
428 child.out = -1;
429 strvec_pushl(&child.args, "fetch", "--negotiate-only", NULL);
430 for (ref = remote_refs; ref; ref = ref->next) {
431 if (!is_null_oid(&ref->new_oid))
432 strvec_pushf(&child.args, "--negotiation-tip=%s", oid_to_hex(&ref->new_oid));
434 strvec_push(&child.args, url);
436 if (start_command(&child))
437 die(_("send-pack: unable to fork off fetch subprocess"));
439 do {
440 char hex_hash[GIT_MAX_HEXSZ + 1];
441 int read_len = read_in_full(child.out, hex_hash, len);
442 struct object_id oid;
443 const char *end;
445 if (!read_len)
446 break;
447 if (read_len != len)
448 die("invalid length read %d", read_len);
449 if (parse_oid_hex(hex_hash, &oid, &end) || *end != '\n')
450 die("invalid hash");
451 oid_array_append(commons, &oid);
452 } while (1);
454 if (finish_command(&child)) {
456 * The information that push negotiation provides is useful but
457 * not mandatory.
459 warning(_("push negotiation failed; proceeding anyway with push"));
463 int send_pack(struct send_pack_args *args,
464 int fd[], struct child_process *conn,
465 struct ref *remote_refs,
466 struct oid_array *extra_have)
468 struct oid_array commons = OID_ARRAY_INIT;
469 int in = fd[0];
470 int out = fd[1];
471 struct strbuf req_buf = STRBUF_INIT;
472 struct strbuf cap_buf = STRBUF_INIT;
473 struct ref *ref;
474 int need_pack_data = 0;
475 int allow_deleting_refs = 0;
476 int status_report = 0;
477 int use_sideband = 0;
478 int quiet_supported = 0;
479 int agent_supported = 0;
480 int advertise_sid = 0;
481 int push_negotiate = 0;
482 int use_atomic = 0;
483 int atomic_supported = 0;
484 int use_push_options = 0;
485 int push_options_supported = 0;
486 int object_format_supported = 0;
487 unsigned cmds_sent = 0;
488 int ret;
489 struct async demux;
490 const char *push_cert_nonce = NULL;
491 struct packet_reader reader;
492 int use_bitmaps;
494 if (!remote_refs) {
495 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
496 "Perhaps you should specify a branch.\n");
497 return 0;
500 git_config_get_bool("push.negotiate", &push_negotiate);
501 if (push_negotiate)
502 get_commons_through_negotiation(args->url, remote_refs, &commons);
504 if (!git_config_get_bool("push.usebitmaps", &use_bitmaps))
505 args->disable_bitmaps = !use_bitmaps;
507 git_config_get_bool("transfer.advertisesid", &advertise_sid);
509 /* Does the other end support the reporting? */
510 if (server_supports("report-status-v2"))
511 status_report = 2;
512 else if (server_supports("report-status"))
513 status_report = 1;
514 if (server_supports("delete-refs"))
515 allow_deleting_refs = 1;
516 if (server_supports("ofs-delta"))
517 args->use_ofs_delta = 1;
518 if (server_supports("side-band-64k"))
519 use_sideband = 1;
520 if (server_supports("quiet"))
521 quiet_supported = 1;
522 if (server_supports("agent"))
523 agent_supported = 1;
524 if (!server_supports("session-id"))
525 advertise_sid = 0;
526 if (server_supports("no-thin"))
527 args->use_thin_pack = 0;
528 if (server_supports("atomic"))
529 atomic_supported = 1;
530 if (server_supports("push-options"))
531 push_options_supported = 1;
533 if (!server_supports_hash(the_hash_algo->name, &object_format_supported))
534 die(_("the receiving end does not support this repository's hash algorithm"));
536 if (args->push_cert != SEND_PACK_PUSH_CERT_NEVER) {
537 int len;
538 push_cert_nonce = server_feature_value("push-cert", &len);
539 if (push_cert_nonce) {
540 reject_invalid_nonce(push_cert_nonce, len);
541 push_cert_nonce = xmemdupz(push_cert_nonce, len);
542 } else if (args->push_cert == SEND_PACK_PUSH_CERT_ALWAYS) {
543 die(_("the receiving end does not support --signed push"));
544 } else if (args->push_cert == SEND_PACK_PUSH_CERT_IF_ASKED) {
545 warning(_("not sending a push certificate since the"
546 " receiving end does not support --signed"
547 " push"));
551 if (args->atomic && !atomic_supported)
552 die(_("the receiving end does not support --atomic push"));
554 use_atomic = atomic_supported && args->atomic;
556 if (args->push_options && !push_options_supported)
557 die(_("the receiving end does not support push options"));
559 use_push_options = push_options_supported && args->push_options;
561 if (status_report == 1)
562 strbuf_addstr(&cap_buf, " report-status");
563 else if (status_report == 2)
564 strbuf_addstr(&cap_buf, " report-status-v2");
565 if (use_sideband)
566 strbuf_addstr(&cap_buf, " side-band-64k");
567 if (quiet_supported && (args->quiet || !args->progress))
568 strbuf_addstr(&cap_buf, " quiet");
569 if (use_atomic)
570 strbuf_addstr(&cap_buf, " atomic");
571 if (use_push_options)
572 strbuf_addstr(&cap_buf, " push-options");
573 if (object_format_supported)
574 strbuf_addf(&cap_buf, " object-format=%s", the_hash_algo->name);
575 if (agent_supported)
576 strbuf_addf(&cap_buf, " agent=%s", git_user_agent_sanitized());
577 if (advertise_sid)
578 strbuf_addf(&cap_buf, " session-id=%s", trace2_session_id());
581 * NEEDSWORK: why does delete-refs have to be so specific to
582 * send-pack machinery that set_ref_status_for_push() cannot
583 * set this bit for us???
585 for (ref = remote_refs; ref; ref = ref->next)
586 if (ref->deletion && !allow_deleting_refs)
587 ref->status = REF_STATUS_REJECT_NODELETE;
590 * Clear the status for each ref and see if we need to send
591 * the pack data.
593 for (ref = remote_refs; ref; ref = ref->next) {
594 switch (check_to_send_update(ref, args)) {
595 case 0: /* no error */
596 break;
597 case CHECK_REF_STATUS_REJECTED:
599 * When we know the server would reject a ref update if
600 * we were to send it and we're trying to send the refs
601 * atomically, abort the whole operation.
603 if (use_atomic) {
604 strbuf_release(&req_buf);
605 strbuf_release(&cap_buf);
606 reject_atomic_push(remote_refs, args->send_mirror);
607 error("atomic push failed for ref %s. status: %d\n",
608 ref->name, ref->status);
609 return args->porcelain ? 0 : -1;
611 /* else fallthrough */
612 default:
613 continue;
615 if (!ref->deletion)
616 need_pack_data = 1;
618 if (args->dry_run || !status_report)
619 ref->status = REF_STATUS_OK;
620 else
621 ref->status = REF_STATUS_EXPECTING_REPORT;
624 if (!args->dry_run)
625 advertise_shallow_grafts_buf(&req_buf);
628 * Finally, tell the other end!
630 if (!args->dry_run && push_cert_nonce)
631 cmds_sent = generate_push_cert(&req_buf, remote_refs, args,
632 cap_buf.buf, push_cert_nonce);
633 else if (!args->dry_run)
634 for (ref = remote_refs; ref; ref = ref->next) {
635 char *old_hex, *new_hex;
637 if (check_to_send_update(ref, args) < 0)
638 continue;
640 old_hex = oid_to_hex(&ref->old_oid);
641 new_hex = oid_to_hex(&ref->new_oid);
642 if (!cmds_sent) {
643 packet_buf_write(&req_buf,
644 "%s %s %s%c%s",
645 old_hex, new_hex, ref->name, 0,
646 cap_buf.buf);
647 cmds_sent = 1;
648 } else {
649 packet_buf_write(&req_buf, "%s %s %s",
650 old_hex, new_hex, ref->name);
654 if (use_push_options) {
655 struct string_list_item *item;
657 packet_buf_flush(&req_buf);
658 for_each_string_list_item(item, args->push_options)
659 packet_buf_write(&req_buf, "%s", item->string);
662 if (args->stateless_rpc) {
663 if (!args->dry_run && (cmds_sent || is_repository_shallow(the_repository))) {
664 packet_buf_flush(&req_buf);
665 send_sideband(out, -1, req_buf.buf, req_buf.len, LARGE_PACKET_MAX);
667 } else {
668 write_or_die(out, req_buf.buf, req_buf.len);
669 packet_flush(out);
671 strbuf_release(&req_buf);
672 strbuf_release(&cap_buf);
674 if (use_sideband && cmds_sent) {
675 memset(&demux, 0, sizeof(demux));
676 demux.proc = sideband_demux;
677 demux.data = fd;
678 demux.out = -1;
679 demux.isolate_sigpipe = 1;
680 if (start_async(&demux))
681 die("send-pack: unable to fork off sideband demultiplexer");
682 in = demux.out;
685 packet_reader_init(&reader, in, NULL, 0,
686 PACKET_READ_CHOMP_NEWLINE |
687 PACKET_READ_DIE_ON_ERR_PACKET);
689 if (need_pack_data && cmds_sent) {
690 if (pack_objects(out, remote_refs, extra_have, &commons, args) < 0) {
691 if (args->stateless_rpc)
692 close(out);
693 if (git_connection_is_socket(conn))
694 shutdown(fd[0], SHUT_WR);
697 * Do not even bother with the return value; we know we
698 * are failing, and just want the error() side effects,
699 * as well as marking refs with their remote status (if
700 * we get one).
702 if (status_report)
703 receive_status(&reader, remote_refs);
705 if (use_sideband) {
706 close(demux.out);
707 finish_async(&demux);
709 fd[1] = -1;
710 return -1;
712 if (!args->stateless_rpc)
713 /* Closed by pack_objects() via start_command() */
714 fd[1] = -1;
716 if (args->stateless_rpc && cmds_sent)
717 packet_flush(out);
719 if (status_report && cmds_sent)
720 ret = receive_status(&reader, remote_refs);
721 else
722 ret = 0;
723 if (args->stateless_rpc)
724 packet_flush(out);
726 if (use_sideband && cmds_sent) {
727 close(demux.out);
728 if (finish_async(&demux)) {
729 error("error in sideband demultiplexer");
730 ret = -1;
734 if (ret < 0)
735 return ret;
737 if (args->porcelain)
738 return 0;
740 for (ref = remote_refs; ref; ref = ref->next) {
741 switch (ref->status) {
742 case REF_STATUS_NONE:
743 case REF_STATUS_UPTODATE:
744 case REF_STATUS_OK:
745 break;
746 default:
747 return -1;
750 return 0;