1 #include "git-compat-util.h"
8 #include "object-store-ll.h"
11 #include "run-command.h"
14 #include "send-pack.h"
16 #include "transport.h"
19 #include "oid-array.h"
20 #include "gpg-interface.h"
22 #include "parse-options.h"
24 #include "write-or-die.h"
26 int option_parse_push_signed(const struct option
*opt
,
27 const char *arg
, int unset
)
30 *(int *)(opt
->value
) = SEND_PACK_PUSH_CERT_NEVER
;
33 switch (git_parse_maybe_bool(arg
)) {
35 *(int *)(opt
->value
) = SEND_PACK_PUSH_CERT_ALWAYS
;
38 *(int *)(opt
->value
) = SEND_PACK_PUSH_CERT_NEVER
;
41 if (!strcasecmp("if-asked", arg
)) {
42 *(int *)(opt
->value
) = SEND_PACK_PUSH_CERT_IF_ASKED
;
45 die("bad %s argument: %s", opt
->long_name
, arg
);
48 static void feed_object(const struct object_id
*oid
, FILE *fh
, int negative
)
51 !repo_has_object_file_with_flags(the_repository
, oid
,
52 OBJECT_INFO_SKIP_FETCH_OBJECT
|
58 fputs(oid_to_hex(oid
), fh
);
63 * Make a pack stream and spit it out into file descriptor fd
65 static int pack_objects(int fd
, struct ref
*refs
, struct oid_array
*advertised
,
66 struct oid_array
*negotiated
,
67 struct send_pack_args
*args
)
70 * The child becomes pack-objects --revs; we feed
71 * the revision parameters to it via its stdin and
72 * let its stdout go back to the other end.
74 struct child_process po
= CHILD_PROCESS_INIT
;
79 strvec_push(&po
.args
, "pack-objects");
80 strvec_push(&po
.args
, "--all-progress-implied");
81 strvec_push(&po
.args
, "--revs");
82 strvec_push(&po
.args
, "--stdout");
83 if (args
->use_thin_pack
)
84 strvec_push(&po
.args
, "--thin");
85 if (args
->use_ofs_delta
)
86 strvec_push(&po
.args
, "--delta-base-offset");
87 if (args
->quiet
|| !args
->progress
)
88 strvec_push(&po
.args
, "-q");
90 strvec_push(&po
.args
, "--progress");
91 if (is_repository_shallow(the_repository
))
92 strvec_push(&po
.args
, "--shallow");
93 if (args
->disable_bitmaps
)
94 strvec_push(&po
.args
, "--no-use-bitmap-index");
96 po
.out
= args
->stateless_rpc
? -1 : fd
;
99 if (start_command(&po
))
100 die_errno("git pack-objects failed");
103 * We feed the pack-objects we just spawned with revision
104 * parameters by writing to the pipe.
106 po_in
= xfdopen(po
.in
, "w");
107 for (i
= 0; i
< advertised
->nr
; i
++)
108 feed_object(&advertised
->oid
[i
], po_in
, 1);
109 for (i
= 0; i
< negotiated
->nr
; i
++)
110 feed_object(&negotiated
->oid
[i
], po_in
, 1);
113 if (!is_null_oid(&refs
->old_oid
))
114 feed_object(&refs
->old_oid
, po_in
, 1);
115 if (!is_null_oid(&refs
->new_oid
))
116 feed_object(&refs
->new_oid
, po_in
, 0);
122 die_errno("error writing to pack-objects");
125 if (args
->stateless_rpc
) {
126 char *buf
= xmalloc(LARGE_PACKET_MAX
);
128 ssize_t n
= xread(po
.out
, buf
, LARGE_PACKET_MAX
);
131 send_sideband(fd
, -1, buf
, n
, LARGE_PACKET_MAX
);
138 rc
= finish_command(&po
);
141 * For a normal non-zero exit, we assume pack-objects wrote
142 * something useful to stderr. For death by signal, though,
143 * we should mention it to the user. The exception is SIGPIPE
144 * (141), because that's a normal occurrence if the remote end
145 * hangs up (and we'll report that by trying to read the unpack
148 if (rc
> 128 && rc
!= 141)
149 error("pack-objects died of signal %d", rc
- 128);
155 static int receive_unpack_status(struct packet_reader
*reader
)
157 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
158 return error(_("unexpected flush packet while reading remote unpack status"));
159 if (!skip_prefix(reader
->line
, "unpack ", &reader
->line
))
160 return error(_("unable to parse remote unpack status: %s"), reader
->line
);
161 if (strcmp(reader
->line
, "ok"))
162 return error(_("remote unpack failed: %s"), reader
->line
);
166 static int receive_status(struct packet_reader
*reader
, struct ref
*refs
)
170 struct ref_push_report
*report
= NULL
;
175 ret
= receive_unpack_status(reader
);
177 struct object_id old_oid
, new_oid
;
181 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
184 p
= strchr(head
, ' ');
186 error("invalid status line from remote: %s", reader
->line
);
192 if (!strcmp(head
, "option")) {
193 const char *key
, *val
;
195 if (!hint
|| !(report
|| new_report
)) {
197 error("'option' without a matching 'ok/ng' directive");
203 CALLOC_ARRAY(hint
->report
, 1);
204 report
= hint
->report
;
206 report
= hint
->report
;
208 report
= report
->next
;
209 CALLOC_ARRAY(report
->next
, 1);
210 report
= report
->next
;
215 p
= strchr(key
, ' ');
219 if (!strcmp(key
, "refname"))
220 report
->ref_name
= xstrdup_or_null(val
);
221 else if (!strcmp(key
, "old-oid") && val
&&
222 !parse_oid_hex(val
, &old_oid
, &val
))
223 report
->old_oid
= oiddup(&old_oid
);
224 else if (!strcmp(key
, "new-oid") && val
&&
225 !parse_oid_hex(val
, &new_oid
, &val
))
226 report
->new_oid
= oiddup(&new_oid
);
227 else if (!strcmp(key
, "forced-update"))
228 report
->forced_update
= 1;
234 if (strcmp(head
, "ok") && strcmp(head
, "ng")) {
235 error("invalid ref status from remote: %s", head
);
240 p
= strchr(refname
, ' ');
243 /* first try searching at our hint, falling back to all refs */
245 hint
= find_ref_by_name(hint
, refname
);
247 hint
= find_ref_by_name(refs
, refname
);
249 warning("remote reported status on unknown ref: %s",
253 if (hint
->status
!= REF_STATUS_EXPECTING_REPORT
&&
254 hint
->status
!= REF_STATUS_OK
&&
255 hint
->status
!= REF_STATUS_REMOTE_REJECT
) {
256 warning("remote reported status on unexpected ref: %s",
260 if (!strcmp(head
, "ng")) {
261 hint
->status
= REF_STATUS_REMOTE_REJECT
;
263 hint
->remote_status
= xstrdup(p
);
265 hint
->remote_status
= "failed";
267 hint
->status
= REF_STATUS_OK
;
268 hint
->remote_status
= xstrdup_or_null(p
);
275 static int sideband_demux(int in UNUSED
, int out
, void *data
)
278 if (async_with_fork())
280 ret
= recv_sideband("send-pack", fd
[0], out
);
285 static int advertise_shallow_grafts_cb(const struct commit_graft
*graft
, void *cb
)
287 struct strbuf
*sb
= cb
;
288 if (graft
->nr_parent
== -1)
289 packet_buf_write(sb
, "shallow %s\n", oid_to_hex(&graft
->oid
));
293 static void advertise_shallow_grafts_buf(struct strbuf
*sb
)
295 if (!is_repository_shallow(the_repository
))
297 for_each_commit_graft(advertise_shallow_grafts_cb
, sb
);
300 #define CHECK_REF_NO_PUSH -1
301 #define CHECK_REF_STATUS_REJECTED -2
302 #define CHECK_REF_UPTODATE -3
303 static int check_to_send_update(const struct ref
*ref
, const struct send_pack_args
*args
)
305 if (!ref
->peer_ref
&& !args
->send_mirror
)
306 return CHECK_REF_NO_PUSH
;
308 /* Check for statuses set by set_ref_status_for_push() */
309 switch (ref
->status
) {
310 case REF_STATUS_REJECT_NONFASTFORWARD
:
311 case REF_STATUS_REJECT_ALREADY_EXISTS
:
312 case REF_STATUS_REJECT_FETCH_FIRST
:
313 case REF_STATUS_REJECT_NEEDS_FORCE
:
314 case REF_STATUS_REJECT_STALE
:
315 case REF_STATUS_REJECT_REMOTE_UPDATED
:
316 case REF_STATUS_REJECT_NODELETE
:
317 return CHECK_REF_STATUS_REJECTED
;
318 case REF_STATUS_UPTODATE
:
319 return CHECK_REF_UPTODATE
;
322 case REF_STATUS_EXPECTING_REPORT
:
323 /* already passed checks on the local side */
325 /* of course this is OK */
331 * the beginning of the next line, or the end of buffer.
333 * NEEDSWORK: perhaps move this to git-compat-util.h or somewhere and
334 * convert many similar uses found by "git grep -A4 memchr".
336 static const char *next_line(const char *line
, size_t len
)
338 const char *nl
= memchr(line
, '\n', len
);
340 return line
+ len
; /* incomplete line */
344 static int generate_push_cert(struct strbuf
*req_buf
,
345 const struct ref
*remote_refs
,
346 struct send_pack_args
*args
,
347 const char *cap_string
,
348 const char *push_cert_nonce
)
350 const struct ref
*ref
;
351 struct string_list_item
*item
;
352 char *signing_key_id
= xstrdup(get_signing_key_id());
354 struct strbuf cert
= STRBUF_INIT
;
357 strbuf_addstr(&cert
, "certificate version 0.1\n");
358 strbuf_addf(&cert
, "pusher %s ", signing_key_id
);
360 strbuf_addch(&cert
, '\n');
361 if (args
->url
&& *args
->url
) {
362 char *anon_url
= transport_anonymize_url(args
->url
);
363 strbuf_addf(&cert
, "pushee %s\n", anon_url
);
366 if (push_cert_nonce
[0])
367 strbuf_addf(&cert
, "nonce %s\n", push_cert_nonce
);
368 if (args
->push_options
)
369 for_each_string_list_item(item
, args
->push_options
)
370 strbuf_addf(&cert
, "push-option %s\n", item
->string
);
371 strbuf_addstr(&cert
, "\n");
373 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
374 if (check_to_send_update(ref
, args
) < 0)
377 strbuf_addf(&cert
, "%s %s %s\n",
378 oid_to_hex(&ref
->old_oid
),
379 oid_to_hex(&ref
->new_oid
),
385 if (sign_buffer(&cert
, &cert
, get_signing_key()))
386 die(_("failed to sign the push certificate"));
388 packet_buf_write(req_buf
, "push-cert%c%s", 0, cap_string
);
389 for (cp
= cert
.buf
; cp
< cert
.buf
+ cert
.len
; cp
= np
) {
390 np
= next_line(cp
, cert
.buf
+ cert
.len
- cp
);
391 packet_buf_write(req_buf
,
392 "%.*s", (int)(np
- cp
), cp
);
394 packet_buf_write(req_buf
, "push-cert-end\n");
397 free(signing_key_id
);
398 strbuf_release(&cert
);
402 #define NONCE_LEN_LIMIT 256
404 static void reject_invalid_nonce(const char *nonce
, int len
)
408 if (NONCE_LEN_LIMIT
<= len
)
409 die("the receiving end asked to sign an invalid nonce <%.*s>",
412 for (i
= 0; i
< len
; i
++) {
413 int ch
= nonce
[i
] & 0xFF;
415 ch
== '-' || ch
== '.' ||
416 ch
== '/' || ch
== '+' ||
417 ch
== '=' || ch
== '_')
419 die("the receiving end asked to sign an invalid nonce <%.*s>",
424 static void get_commons_through_negotiation(const char *url
,
425 const struct ref
*remote_refs
,
426 struct oid_array
*commons
)
428 struct child_process child
= CHILD_PROCESS_INIT
;
429 const struct ref
*ref
;
430 int len
= the_hash_algo
->hexsz
+ 1; /* hash + NL */
435 strvec_pushl(&child
.args
, "fetch", "--negotiate-only", NULL
);
436 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
437 if (!is_null_oid(&ref
->new_oid
))
438 strvec_pushf(&child
.args
, "--negotiation-tip=%s", oid_to_hex(&ref
->new_oid
));
440 strvec_push(&child
.args
, url
);
442 if (start_command(&child
))
443 die(_("send-pack: unable to fork off fetch subprocess"));
446 char hex_hash
[GIT_MAX_HEXSZ
+ 1];
447 int read_len
= read_in_full(child
.out
, hex_hash
, len
);
448 struct object_id oid
;
454 die("invalid length read %d", read_len
);
455 if (parse_oid_hex(hex_hash
, &oid
, &end
) || *end
!= '\n')
457 oid_array_append(commons
, &oid
);
460 if (finish_command(&child
)) {
462 * The information that push negotiation provides is useful but
465 warning(_("push negotiation failed; proceeding anyway with push"));
469 int send_pack(struct send_pack_args
*args
,
470 int fd
[], struct child_process
*conn
,
471 struct ref
*remote_refs
,
472 struct oid_array
*extra_have
)
474 struct oid_array commons
= OID_ARRAY_INIT
;
477 struct strbuf req_buf
= STRBUF_INIT
;
478 struct strbuf cap_buf
= STRBUF_INIT
;
480 int need_pack_data
= 0;
481 int allow_deleting_refs
= 0;
482 int status_report
= 0;
483 int use_sideband
= 0;
484 int quiet_supported
= 0;
485 int agent_supported
= 0;
486 int advertise_sid
= 0;
487 int push_negotiate
= 0;
489 int atomic_supported
= 0;
490 int use_push_options
= 0;
491 int push_options_supported
= 0;
492 int object_format_supported
= 0;
493 unsigned cmds_sent
= 0;
496 const char *push_cert_nonce
= NULL
;
497 struct packet_reader reader
;
501 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
502 "Perhaps you should specify a branch.\n");
506 git_config_get_bool("push.negotiate", &push_negotiate
);
508 get_commons_through_negotiation(args
->url
, remote_refs
, &commons
);
510 if (!git_config_get_bool("push.usebitmaps", &use_bitmaps
))
511 args
->disable_bitmaps
= !use_bitmaps
;
513 git_config_get_bool("transfer.advertisesid", &advertise_sid
);
515 /* Does the other end support the reporting? */
516 if (server_supports("report-status-v2"))
518 else if (server_supports("report-status"))
520 if (server_supports("delete-refs"))
521 allow_deleting_refs
= 1;
522 if (server_supports("ofs-delta"))
523 args
->use_ofs_delta
= 1;
524 if (server_supports("side-band-64k"))
526 if (server_supports("quiet"))
528 if (server_supports("agent"))
530 if (!server_supports("session-id"))
532 if (server_supports("no-thin"))
533 args
->use_thin_pack
= 0;
534 if (server_supports("atomic"))
535 atomic_supported
= 1;
536 if (server_supports("push-options"))
537 push_options_supported
= 1;
539 if (!server_supports_hash(the_hash_algo
->name
, &object_format_supported
))
540 die(_("the receiving end does not support this repository's hash algorithm"));
542 if (args
->push_cert
!= SEND_PACK_PUSH_CERT_NEVER
) {
544 push_cert_nonce
= server_feature_value("push-cert", &len
);
545 if (push_cert_nonce
) {
546 reject_invalid_nonce(push_cert_nonce
, len
);
547 push_cert_nonce
= xmemdupz(push_cert_nonce
, len
);
548 } else if (args
->push_cert
== SEND_PACK_PUSH_CERT_ALWAYS
) {
549 die(_("the receiving end does not support --signed push"));
550 } else if (args
->push_cert
== SEND_PACK_PUSH_CERT_IF_ASKED
) {
551 warning(_("not sending a push certificate since the"
552 " receiving end does not support --signed"
557 if (args
->atomic
&& !atomic_supported
)
558 die(_("the receiving end does not support --atomic push"));
560 use_atomic
= atomic_supported
&& args
->atomic
;
562 if (args
->push_options
&& !push_options_supported
)
563 die(_("the receiving end does not support push options"));
565 use_push_options
= push_options_supported
&& args
->push_options
;
567 if (status_report
== 1)
568 strbuf_addstr(&cap_buf
, " report-status");
569 else if (status_report
== 2)
570 strbuf_addstr(&cap_buf
, " report-status-v2");
572 strbuf_addstr(&cap_buf
, " side-band-64k");
573 if (quiet_supported
&& (args
->quiet
|| !args
->progress
))
574 strbuf_addstr(&cap_buf
, " quiet");
576 strbuf_addstr(&cap_buf
, " atomic");
577 if (use_push_options
)
578 strbuf_addstr(&cap_buf
, " push-options");
579 if (object_format_supported
)
580 strbuf_addf(&cap_buf
, " object-format=%s", the_hash_algo
->name
);
582 strbuf_addf(&cap_buf
, " agent=%s", git_user_agent_sanitized());
584 strbuf_addf(&cap_buf
, " session-id=%s", trace2_session_id());
587 * NEEDSWORK: why does delete-refs have to be so specific to
588 * send-pack machinery that set_ref_status_for_push() cannot
589 * set this bit for us???
591 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
592 if (ref
->deletion
&& !allow_deleting_refs
)
593 ref
->status
= REF_STATUS_REJECT_NODELETE
;
596 * Clear the status for each ref and see if we need to send
599 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
600 switch (check_to_send_update(ref
, args
)) {
601 case 0: /* no error */
603 case CHECK_REF_STATUS_REJECTED
:
605 * When we know the server would reject a ref update if
606 * we were to send it and we're trying to send the refs
607 * atomically, abort the whole operation.
610 strbuf_release(&req_buf
);
611 strbuf_release(&cap_buf
);
612 reject_atomic_push(remote_refs
, args
->send_mirror
);
613 error("atomic push failed for ref %s. status: %d\n",
614 ref
->name
, ref
->status
);
615 return args
->porcelain
? 0 : -1;
617 /* else fallthrough */
624 if (args
->dry_run
|| !status_report
)
625 ref
->status
= REF_STATUS_OK
;
627 ref
->status
= REF_STATUS_EXPECTING_REPORT
;
631 advertise_shallow_grafts_buf(&req_buf
);
634 * Finally, tell the other end!
636 if (!args
->dry_run
&& push_cert_nonce
)
637 cmds_sent
= generate_push_cert(&req_buf
, remote_refs
, args
,
638 cap_buf
.buf
, push_cert_nonce
);
639 else if (!args
->dry_run
)
640 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
641 char *old_hex
, *new_hex
;
643 if (check_to_send_update(ref
, args
) < 0)
646 old_hex
= oid_to_hex(&ref
->old_oid
);
647 new_hex
= oid_to_hex(&ref
->new_oid
);
649 packet_buf_write(&req_buf
,
651 old_hex
, new_hex
, ref
->name
, 0,
655 packet_buf_write(&req_buf
, "%s %s %s",
656 old_hex
, new_hex
, ref
->name
);
660 if (use_push_options
) {
661 struct string_list_item
*item
;
663 packet_buf_flush(&req_buf
);
664 for_each_string_list_item(item
, args
->push_options
)
665 packet_buf_write(&req_buf
, "%s", item
->string
);
668 if (args
->stateless_rpc
) {
669 if (!args
->dry_run
&& (cmds_sent
|| is_repository_shallow(the_repository
))) {
670 packet_buf_flush(&req_buf
);
671 send_sideband(out
, -1, req_buf
.buf
, req_buf
.len
, LARGE_PACKET_MAX
);
674 write_or_die(out
, req_buf
.buf
, req_buf
.len
);
677 strbuf_release(&req_buf
);
678 strbuf_release(&cap_buf
);
680 if (use_sideband
&& cmds_sent
) {
681 memset(&demux
, 0, sizeof(demux
));
682 demux
.proc
= sideband_demux
;
685 demux
.isolate_sigpipe
= 1;
686 if (start_async(&demux
))
687 die("send-pack: unable to fork off sideband demultiplexer");
691 packet_reader_init(&reader
, in
, NULL
, 0,
692 PACKET_READ_CHOMP_NEWLINE
|
693 PACKET_READ_DIE_ON_ERR_PACKET
);
695 if (need_pack_data
&& cmds_sent
) {
696 if (pack_objects(out
, remote_refs
, extra_have
, &commons
, args
) < 0) {
697 if (args
->stateless_rpc
)
699 if (git_connection_is_socket(conn
))
700 shutdown(fd
[0], SHUT_WR
);
703 * Do not even bother with the return value; we know we
704 * are failing, and just want the error() side effects,
705 * as well as marking refs with their remote status (if
709 receive_status(&reader
, remote_refs
);
713 finish_async(&demux
);
718 if (!args
->stateless_rpc
)
719 /* Closed by pack_objects() via start_command() */
722 if (args
->stateless_rpc
&& cmds_sent
)
725 if (status_report
&& cmds_sent
)
726 ret
= receive_status(&reader
, remote_refs
);
729 if (args
->stateless_rpc
)
732 if (use_sideband
&& cmds_sent
) {
734 if (finish_async(&demux
)) {
735 error("error in sideband demultiplexer");
746 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
747 switch (ref
->status
) {
748 case REF_STATUS_NONE
:
749 case REF_STATUS_UPTODATE
: