6 #include "run-command.h"
11 #include "transport.h"
13 #include "sha1-array.h"
14 #include "gpg-interface.h"
17 int option_parse_push_signed(const struct option *opt,
18 const char *arg, int unset)
21 *(int *)(opt->value) = SEND_PACK_PUSH_CERT_NEVER;
24 switch (git_parse_maybe_bool(arg)) {
26 *(int *)(opt->value) = SEND_PACK_PUSH_CERT_ALWAYS;
29 *(int *)(opt->value) = SEND_PACK_PUSH_CERT_NEVER;
32 if (!strcasecmp("if-asked", arg)) {
33 *(int *)(opt->value) = SEND_PACK_PUSH_CERT_IF_ASKED;
36 die("bad %s argument: %s", opt->long_name, arg);
39 static void feed_object(const unsigned char *sha1, FILE *fh, int negative)
41 if (negative && !has_sha1_file(sha1))
46 fputs(sha1_to_hex(sha1), fh);
51 * Make a pack stream and spit it out into file descriptor fd
53 static int pack_objects(int fd, struct ref *refs, struct oid_array *extra, struct send_pack_args *args)
56 * The child becomes pack-objects --revs; we feed
57 * the revision parameters to it via its stdin and
58 * let its stdout go back to the other end.
60 const char *argv[] = {
62 "--all-progress-implied",
72 struct child_process po = CHILD_PROCESS_INIT;
78 if (args->use_thin_pack)
80 if (args->use_ofs_delta)
81 argv[i++] = "--delta-base-offset";
82 if (args->quiet || !args->progress)
85 argv[i++] = "--progress";
86 if (is_repository_shallow())
87 argv[i++] = "--shallow";
90 po.out = args->stateless_rpc ? -1 : fd;
92 if (start_command(&po))
93 die_errno("git pack-objects failed");
96 * We feed the pack-objects we just spawned with revision
97 * parameters by writing to the pipe.
99 po_in = xfdopen(po.in, "w");
100 for (i = 0; i < extra->nr; i++)
101 feed_object(extra->oid[i].hash, po_in, 1);
104 if (!is_null_oid(&refs->old_oid))
105 feed_object(refs->old_oid.hash, po_in, 1);
106 if (!is_null_oid(&refs->new_oid))
107 feed_object(refs->new_oid.hash, po_in, 0);
113 die_errno("error writing to pack-objects");
116 if (args->stateless_rpc) {
117 char *buf = xmalloc(LARGE_PACKET_MAX);
119 ssize_t n = xread(po.out, buf, LARGE_PACKET_MAX);
122 send_sideband(fd, -1, buf, n, LARGE_PACKET_MAX);
129 rc = finish_command(&po);
132 * For a normal non-zero exit, we assume pack-objects wrote
133 * something useful to stderr. For death by signal, though,
134 * we should mention it to the user. The exception is SIGPIPE
135 * (141), because that's a normal occurence if the remote end
136 * hangs up (and we'll report that by trying to read the unpack
139 if (rc > 128 && rc != 141)
140 error("pack-objects died of signal %d", rc - 128);
146 static int receive_unpack_status(int in)
148 const char *line = packet_read_line(in, NULL);
149 if (!skip_prefix(line, "unpack ", &line))
150 return error(_("unable to parse remote unpack status: %s"), line);
151 if (strcmp(line, "ok"))
152 return error(_("remote unpack failed: %s"), line);
156 static int receive_status(int in, struct ref *refs)
162 ret = receive_unpack_status(in);
166 char *line = packet_read_line(in, NULL);
169 if (!starts_with(line, "ok ") && !starts_with(line, "ng ")) {
170 error("invalid ref status from remote: %s", line);
176 msg = strchr(refname, ' ');
180 /* first try searching at our hint, falling back to all refs */
182 hint = find_ref_by_name(hint, refname);
184 hint = find_ref_by_name(refs, refname);
186 warning("remote reported status on unknown ref: %s",
190 if (hint->status != REF_STATUS_EXPECTING_REPORT) {
191 warning("remote reported status on unexpected ref: %s",
196 if (line[0] == 'o' && line[1] == 'k')
197 hint->status = REF_STATUS_OK;
199 hint->status = REF_STATUS_REMOTE_REJECT;
202 hint->remote_status = xstrdup_or_null(msg);
203 /* start our next search from the next ref */
209 static int sideband_demux(int in, int out, void *data)
215 ret = recv_sideband("send-pack", fd[0], out);
220 static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb)
222 struct strbuf *sb = cb;
223 if (graft->nr_parent == -1)
224 packet_buf_write(sb, "shallow %s\n", oid_to_hex(&graft->oid));
228 static void advertise_shallow_grafts_buf(struct strbuf *sb)
230 if (!is_repository_shallow())
232 for_each_commit_graft(advertise_shallow_grafts_cb, sb);
235 #define CHECK_REF_NO_PUSH -1
236 #define CHECK_REF_STATUS_REJECTED -2
237 #define CHECK_REF_UPTODATE -3
238 static int check_to_send_update(const struct ref *ref, const struct send_pack_args *args)
240 if (!ref->peer_ref && !args->send_mirror)
241 return CHECK_REF_NO_PUSH;
243 /* Check for statuses set by set_ref_status_for_push() */
244 switch (ref->status) {
245 case REF_STATUS_REJECT_NONFASTFORWARD:
246 case REF_STATUS_REJECT_ALREADY_EXISTS:
247 case REF_STATUS_REJECT_FETCH_FIRST:
248 case REF_STATUS_REJECT_NEEDS_FORCE:
249 case REF_STATUS_REJECT_STALE:
250 case REF_STATUS_REJECT_NODELETE:
251 return CHECK_REF_STATUS_REJECTED;
252 case REF_STATUS_UPTODATE:
253 return CHECK_REF_UPTODATE;
260 * the beginning of the next line, or the end of buffer.
262 * NEEDSWORK: perhaps move this to git-compat-util.h or somewhere and
263 * convert many similar uses found by "git grep -A4 memchr".
265 static const char *next_line(const char *line, size_t len)
267 const char *nl = memchr(line, '\n', len);
269 return line + len; /* incomplete line */
273 static int generate_push_cert(struct strbuf *req_buf,
274 const struct ref *remote_refs,
275 struct send_pack_args *args,
276 const char *cap_string,
277 const char *push_cert_nonce)
279 const struct ref *ref;
280 struct string_list_item *item;
281 char *signing_key = xstrdup(get_signing_key());
283 struct strbuf cert = STRBUF_INIT;
286 strbuf_addstr(&cert, "certificate version 0.1\n");
287 strbuf_addf(&cert, "pusher %s ", signing_key);
289 strbuf_addch(&cert, '\n');
290 if (args->url && *args->url) {
291 char *anon_url = transport_anonymize_url(args->url);
292 strbuf_addf(&cert, "pushee %s\n", anon_url);
295 if (push_cert_nonce[0])
296 strbuf_addf(&cert, "nonce %s\n", push_cert_nonce);
297 if (args->push_options)
298 for_each_string_list_item(item, args->push_options)
299 strbuf_addf(&cert, "push-option %s\n", item->string);
300 strbuf_addstr(&cert, "\n");
302 for (ref = remote_refs; ref; ref = ref->next) {
303 if (check_to_send_update(ref, args) < 0)
306 strbuf_addf(&cert, "%s %s %s\n",
307 oid_to_hex(&ref->old_oid),
308 oid_to_hex(&ref->new_oid),
314 if (sign_buffer(&cert, &cert, signing_key))
315 die(_("failed to sign the push certificate"));
317 packet_buf_write(req_buf, "push-cert%c%s", 0, cap_string);
318 for (cp = cert.buf; cp < cert.buf + cert.len; cp = np) {
319 np = next_line(cp, cert.buf + cert.len - cp);
320 packet_buf_write(req_buf,
321 "%.*s", (int)(np - cp), cp);
323 packet_buf_write(req_buf, "push-cert-end\n");
327 strbuf_release(&cert);
332 static int atomic_push_failure(struct send_pack_args *args,
333 struct ref *remote_refs,
334 struct ref *failing_ref)
337 /* Mark other refs as failed */
338 for (ref = remote_refs; ref; ref = ref->next) {
339 if (!ref->peer_ref && !args->send_mirror)
342 switch (ref->status) {
343 case REF_STATUS_EXPECTING_REPORT:
344 ref->status = REF_STATUS_ATOMIC_PUSH_FAILED;
347 break; /* do nothing */
350 return error("atomic push failed for ref %s. status: %d\n",
351 failing_ref->name, failing_ref->status);
354 #define NONCE_LEN_LIMIT 256
356 static void reject_invalid_nonce(const char *nonce, int len)
360 if (NONCE_LEN_LIMIT <= len)
361 die("the receiving end asked to sign an invalid nonce <%.*s>",
364 for (i = 0; i < len; i++) {
365 int ch = nonce[i] & 0xFF;
367 ch == '-' || ch == '.' ||
368 ch == '/' || ch == '+' ||
369 ch == '=' || ch == '_')
371 die("the receiving end asked to sign an invalid nonce <%.*s>",
376 int send_pack(struct send_pack_args *args,
377 int fd[], struct child_process *conn,
378 struct ref *remote_refs,
379 struct oid_array *extra_have)
383 struct strbuf req_buf = STRBUF_INIT;
384 struct strbuf cap_buf = STRBUF_INIT;
386 int need_pack_data = 0;
387 int allow_deleting_refs = 0;
388 int status_report = 0;
389 int use_sideband = 0;
390 int quiet_supported = 0;
391 int agent_supported = 0;
393 int atomic_supported = 0;
394 int use_push_options = 0;
395 int push_options_supported = 0;
396 unsigned cmds_sent = 0;
399 const char *push_cert_nonce = NULL;
401 /* Does the other end support the reporting? */
402 if (server_supports("report-status"))
404 if (server_supports("delete-refs"))
405 allow_deleting_refs = 1;
406 if (server_supports("ofs-delta"))
407 args->use_ofs_delta = 1;
408 if (server_supports("side-band-64k"))
410 if (server_supports("quiet"))
412 if (server_supports("agent"))
414 if (server_supports("no-thin"))
415 args->use_thin_pack = 0;
416 if (server_supports("atomic"))
417 atomic_supported = 1;
418 if (server_supports("push-options"))
419 push_options_supported = 1;
421 if (args->push_cert != SEND_PACK_PUSH_CERT_NEVER) {
423 push_cert_nonce = server_feature_value("push-cert", &len);
424 if (push_cert_nonce) {
425 reject_invalid_nonce(push_cert_nonce, len);
426 push_cert_nonce = xmemdupz(push_cert_nonce, len);
427 } else if (args->push_cert == SEND_PACK_PUSH_CERT_ALWAYS) {
428 die(_("the receiving end does not support --signed push"));
429 } else if (args->push_cert == SEND_PACK_PUSH_CERT_IF_ASKED) {
430 warning(_("not sending a push certificate since the"
431 " receiving end does not support --signed"
437 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
438 "Perhaps you should specify a branch such as 'master'.\n");
441 if (args->atomic && !atomic_supported)
442 die(_("the receiving end does not support --atomic push"));
444 use_atomic = atomic_supported && args->atomic;
446 if (args->push_options && !push_options_supported)
447 die(_("the receiving end does not support push options"));
449 use_push_options = push_options_supported && args->push_options;
452 strbuf_addstr(&cap_buf, " report-status");
454 strbuf_addstr(&cap_buf, " side-band-64k");
455 if (quiet_supported && (args->quiet || !args->progress))
456 strbuf_addstr(&cap_buf, " quiet");
458 strbuf_addstr(&cap_buf, " atomic");
459 if (use_push_options)
460 strbuf_addstr(&cap_buf, " push-options");
462 strbuf_addf(&cap_buf, " agent=%s", git_user_agent_sanitized());
465 * NEEDSWORK: why does delete-refs have to be so specific to
466 * send-pack machinery that set_ref_status_for_push() cannot
467 * set this bit for us???
469 for (ref = remote_refs; ref; ref = ref->next)
470 if (ref->deletion && !allow_deleting_refs)
471 ref->status = REF_STATUS_REJECT_NODELETE;
474 advertise_shallow_grafts_buf(&req_buf);
476 if (!args->dry_run && push_cert_nonce)
477 cmds_sent = generate_push_cert(&req_buf, remote_refs, args,
478 cap_buf.buf, push_cert_nonce);
481 * Clear the status for each ref and see if we need to send
484 for (ref = remote_refs; ref; ref = ref->next) {
485 switch (check_to_send_update(ref, args)) {
486 case 0: /* no error */
488 case CHECK_REF_STATUS_REJECTED:
490 * When we know the server would reject a ref update if
491 * we were to send it and we're trying to send the refs
492 * atomically, abort the whole operation.
495 return atomic_push_failure(args, remote_refs, ref);
496 /* Fallthrough for non atomic case. */
503 if (args->dry_run || !status_report)
504 ref->status = REF_STATUS_OK;
506 ref->status = REF_STATUS_EXPECTING_REPORT;
510 * Finally, tell the other end!
512 for (ref = remote_refs; ref; ref = ref->next) {
513 char *old_hex, *new_hex;
515 if (args->dry_run || push_cert_nonce)
518 if (check_to_send_update(ref, args) < 0)
521 old_hex = oid_to_hex(&ref->old_oid);
522 new_hex = oid_to_hex(&ref->new_oid);
524 packet_buf_write(&req_buf,
526 old_hex, new_hex, ref->name, 0,
530 packet_buf_write(&req_buf, "%s %s %s",
531 old_hex, new_hex, ref->name);
535 if (use_push_options) {
536 struct string_list_item *item;
538 packet_buf_flush(&req_buf);
539 for_each_string_list_item(item, args->push_options)
540 packet_buf_write(&req_buf, "%s", item->string);
543 if (args->stateless_rpc) {
544 if (!args->dry_run && (cmds_sent || is_repository_shallow())) {
545 packet_buf_flush(&req_buf);
546 send_sideband(out, -1, req_buf.buf, req_buf.len, LARGE_PACKET_MAX);
549 write_or_die(out, req_buf.buf, req_buf.len);
552 strbuf_release(&req_buf);
553 strbuf_release(&cap_buf);
555 if (use_sideband && cmds_sent) {
556 memset(&demux, 0, sizeof(demux));
557 demux.proc = sideband_demux;
560 demux.isolate_sigpipe = 1;
561 if (start_async(&demux))
562 die("send-pack: unable to fork off sideband demultiplexer");
566 if (need_pack_data && cmds_sent) {
567 if (pack_objects(out, remote_refs, extra_have, args) < 0) {
568 for (ref = remote_refs; ref; ref = ref->next)
569 ref->status = REF_STATUS_NONE;
570 if (args->stateless_rpc)
572 if (git_connection_is_socket(conn))
573 shutdown(fd[0], SHUT_WR);
576 * Do not even bother with the return value; we know we
577 * are failing, and just want the error() side effects.
580 receive_unpack_status(in);
584 finish_async(&demux);
589 if (!args->stateless_rpc)
590 /* Closed by pack_objects() via start_command() */
593 if (args->stateless_rpc && cmds_sent)
596 if (status_report && cmds_sent)
597 ret = receive_status(in, remote_refs);
600 if (args->stateless_rpc)
603 if (use_sideband && cmds_sent) {
605 if (finish_async(&demux)) {
606 error("error in sideband demultiplexer");
617 for (ref = remote_refs; ref; ref = ref->next) {
618 switch (ref->status) {
619 case REF_STATUS_NONE:
620 case REF_STATUS_UPTODATE: