1 #include "git-compat-util.h"
7 #include "object-store.h"
10 #include "run-command.h"
13 #include "send-pack.h"
15 #include "transport.h"
18 #include "oid-array.h"
19 #include "gpg-interface.h"
22 #include "write-or-die.h"
24 int option_parse_push_signed(const struct option
*opt
,
25 const char *arg
, int unset
)
28 *(int *)(opt
->value
) = SEND_PACK_PUSH_CERT_NEVER
;
31 switch (git_parse_maybe_bool(arg
)) {
33 *(int *)(opt
->value
) = SEND_PACK_PUSH_CERT_ALWAYS
;
36 *(int *)(opt
->value
) = SEND_PACK_PUSH_CERT_NEVER
;
39 if (!strcasecmp("if-asked", arg
)) {
40 *(int *)(opt
->value
) = SEND_PACK_PUSH_CERT_IF_ASKED
;
43 die("bad %s argument: %s", opt
->long_name
, arg
);
46 static void feed_object(const struct object_id
*oid
, FILE *fh
, int negative
)
49 !repo_has_object_file_with_flags(the_repository
, oid
,
50 OBJECT_INFO_SKIP_FETCH_OBJECT
|
56 fputs(oid_to_hex(oid
), fh
);
61 * Make a pack stream and spit it out into file descriptor fd
63 static int pack_objects(int fd
, struct ref
*refs
, struct oid_array
*advertised
,
64 struct oid_array
*negotiated
,
65 struct send_pack_args
*args
)
68 * The child becomes pack-objects --revs; we feed
69 * the revision parameters to it via its stdin and
70 * let its stdout go back to the other end.
72 struct child_process po
= CHILD_PROCESS_INIT
;
77 strvec_push(&po
.args
, "pack-objects");
78 strvec_push(&po
.args
, "--all-progress-implied");
79 strvec_push(&po
.args
, "--revs");
80 strvec_push(&po
.args
, "--stdout");
81 if (args
->use_thin_pack
)
82 strvec_push(&po
.args
, "--thin");
83 if (args
->use_ofs_delta
)
84 strvec_push(&po
.args
, "--delta-base-offset");
85 if (args
->quiet
|| !args
->progress
)
86 strvec_push(&po
.args
, "-q");
88 strvec_push(&po
.args
, "--progress");
89 if (is_repository_shallow(the_repository
))
90 strvec_push(&po
.args
, "--shallow");
91 if (args
->disable_bitmaps
)
92 strvec_push(&po
.args
, "--no-use-bitmap-index");
94 po
.out
= args
->stateless_rpc
? -1 : fd
;
97 if (start_command(&po
))
98 die_errno("git pack-objects failed");
101 * We feed the pack-objects we just spawned with revision
102 * parameters by writing to the pipe.
104 po_in
= xfdopen(po
.in
, "w");
105 for (i
= 0; i
< advertised
->nr
; i
++)
106 feed_object(&advertised
->oid
[i
], po_in
, 1);
107 for (i
= 0; i
< negotiated
->nr
; i
++)
108 feed_object(&negotiated
->oid
[i
], po_in
, 1);
111 if (!is_null_oid(&refs
->old_oid
))
112 feed_object(&refs
->old_oid
, po_in
, 1);
113 if (!is_null_oid(&refs
->new_oid
))
114 feed_object(&refs
->new_oid
, po_in
, 0);
120 die_errno("error writing to pack-objects");
123 if (args
->stateless_rpc
) {
124 char *buf
= xmalloc(LARGE_PACKET_MAX
);
126 ssize_t n
= xread(po
.out
, buf
, LARGE_PACKET_MAX
);
129 send_sideband(fd
, -1, buf
, n
, LARGE_PACKET_MAX
);
136 rc
= finish_command(&po
);
139 * For a normal non-zero exit, we assume pack-objects wrote
140 * something useful to stderr. For death by signal, though,
141 * we should mention it to the user. The exception is SIGPIPE
142 * (141), because that's a normal occurrence if the remote end
143 * hangs up (and we'll report that by trying to read the unpack
146 if (rc
> 128 && rc
!= 141)
147 error("pack-objects died of signal %d", rc
- 128);
153 static int receive_unpack_status(struct packet_reader
*reader
)
155 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
156 return error(_("unexpected flush packet while reading remote unpack status"));
157 if (!skip_prefix(reader
->line
, "unpack ", &reader
->line
))
158 return error(_("unable to parse remote unpack status: %s"), reader
->line
);
159 if (strcmp(reader
->line
, "ok"))
160 return error(_("remote unpack failed: %s"), reader
->line
);
164 static int receive_status(struct packet_reader
*reader
, struct ref
*refs
)
168 struct ref_push_report
*report
= NULL
;
173 ret
= receive_unpack_status(reader
);
175 struct object_id old_oid
, new_oid
;
179 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
182 p
= strchr(head
, ' ');
184 error("invalid status line from remote: %s", reader
->line
);
190 if (!strcmp(head
, "option")) {
191 const char *key
, *val
;
193 if (!hint
|| !(report
|| new_report
)) {
195 error("'option' without a matching 'ok/ng' directive");
201 CALLOC_ARRAY(hint
->report
, 1);
202 report
= hint
->report
;
204 report
= hint
->report
;
206 report
= report
->next
;
207 CALLOC_ARRAY(report
->next
, 1);
208 report
= report
->next
;
213 p
= strchr(key
, ' ');
217 if (!strcmp(key
, "refname"))
218 report
->ref_name
= xstrdup_or_null(val
);
219 else if (!strcmp(key
, "old-oid") && val
&&
220 !parse_oid_hex(val
, &old_oid
, &val
))
221 report
->old_oid
= oiddup(&old_oid
);
222 else if (!strcmp(key
, "new-oid") && val
&&
223 !parse_oid_hex(val
, &new_oid
, &val
))
224 report
->new_oid
= oiddup(&new_oid
);
225 else if (!strcmp(key
, "forced-update"))
226 report
->forced_update
= 1;
232 if (strcmp(head
, "ok") && strcmp(head
, "ng")) {
233 error("invalid ref status from remote: %s", head
);
238 p
= strchr(refname
, ' ');
241 /* first try searching at our hint, falling back to all refs */
243 hint
= find_ref_by_name(hint
, refname
);
245 hint
= find_ref_by_name(refs
, refname
);
247 warning("remote reported status on unknown ref: %s",
251 if (hint
->status
!= REF_STATUS_EXPECTING_REPORT
&&
252 hint
->status
!= REF_STATUS_OK
&&
253 hint
->status
!= REF_STATUS_REMOTE_REJECT
) {
254 warning("remote reported status on unexpected ref: %s",
258 if (!strcmp(head
, "ng")) {
259 hint
->status
= REF_STATUS_REMOTE_REJECT
;
261 hint
->remote_status
= xstrdup(p
);
263 hint
->remote_status
= "failed";
265 hint
->status
= REF_STATUS_OK
;
266 hint
->remote_status
= xstrdup_or_null(p
);
273 static int sideband_demux(int in UNUSED
, int out
, void *data
)
276 if (async_with_fork())
278 ret
= recv_sideband("send-pack", fd
[0], out
);
283 static int advertise_shallow_grafts_cb(const struct commit_graft
*graft
, void *cb
)
285 struct strbuf
*sb
= cb
;
286 if (graft
->nr_parent
== -1)
287 packet_buf_write(sb
, "shallow %s\n", oid_to_hex(&graft
->oid
));
291 static void advertise_shallow_grafts_buf(struct strbuf
*sb
)
293 if (!is_repository_shallow(the_repository
))
295 for_each_commit_graft(advertise_shallow_grafts_cb
, sb
);
298 #define CHECK_REF_NO_PUSH -1
299 #define CHECK_REF_STATUS_REJECTED -2
300 #define CHECK_REF_UPTODATE -3
301 static int check_to_send_update(const struct ref
*ref
, const struct send_pack_args
*args
)
303 if (!ref
->peer_ref
&& !args
->send_mirror
)
304 return CHECK_REF_NO_PUSH
;
306 /* Check for statuses set by set_ref_status_for_push() */
307 switch (ref
->status
) {
308 case REF_STATUS_REJECT_NONFASTFORWARD
:
309 case REF_STATUS_REJECT_ALREADY_EXISTS
:
310 case REF_STATUS_REJECT_FETCH_FIRST
:
311 case REF_STATUS_REJECT_NEEDS_FORCE
:
312 case REF_STATUS_REJECT_STALE
:
313 case REF_STATUS_REJECT_REMOTE_UPDATED
:
314 case REF_STATUS_REJECT_NODELETE
:
315 return CHECK_REF_STATUS_REJECTED
;
316 case REF_STATUS_UPTODATE
:
317 return CHECK_REF_UPTODATE
;
320 case REF_STATUS_EXPECTING_REPORT
:
321 /* already passed checks on the local side */
323 /* of course this is OK */
329 * the beginning of the next line, or the end of buffer.
331 * NEEDSWORK: perhaps move this to git-compat-util.h or somewhere and
332 * convert many similar uses found by "git grep -A4 memchr".
334 static const char *next_line(const char *line
, size_t len
)
336 const char *nl
= memchr(line
, '\n', len
);
338 return line
+ len
; /* incomplete line */
342 static int generate_push_cert(struct strbuf
*req_buf
,
343 const struct ref
*remote_refs
,
344 struct send_pack_args
*args
,
345 const char *cap_string
,
346 const char *push_cert_nonce
)
348 const struct ref
*ref
;
349 struct string_list_item
*item
;
350 char *signing_key_id
= xstrdup(get_signing_key_id());
352 struct strbuf cert
= STRBUF_INIT
;
355 strbuf_addstr(&cert
, "certificate version 0.1\n");
356 strbuf_addf(&cert
, "pusher %s ", signing_key_id
);
358 strbuf_addch(&cert
, '\n');
359 if (args
->url
&& *args
->url
) {
360 char *anon_url
= transport_anonymize_url(args
->url
);
361 strbuf_addf(&cert
, "pushee %s\n", anon_url
);
364 if (push_cert_nonce
[0])
365 strbuf_addf(&cert
, "nonce %s\n", push_cert_nonce
);
366 if (args
->push_options
)
367 for_each_string_list_item(item
, args
->push_options
)
368 strbuf_addf(&cert
, "push-option %s\n", item
->string
);
369 strbuf_addstr(&cert
, "\n");
371 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
372 if (check_to_send_update(ref
, args
) < 0)
375 strbuf_addf(&cert
, "%s %s %s\n",
376 oid_to_hex(&ref
->old_oid
),
377 oid_to_hex(&ref
->new_oid
),
383 if (sign_buffer(&cert
, &cert
, get_signing_key()))
384 die(_("failed to sign the push certificate"));
386 packet_buf_write(req_buf
, "push-cert%c%s", 0, cap_string
);
387 for (cp
= cert
.buf
; cp
< cert
.buf
+ cert
.len
; cp
= np
) {
388 np
= next_line(cp
, cert
.buf
+ cert
.len
- cp
);
389 packet_buf_write(req_buf
,
390 "%.*s", (int)(np
- cp
), cp
);
392 packet_buf_write(req_buf
, "push-cert-end\n");
395 free(signing_key_id
);
396 strbuf_release(&cert
);
400 #define NONCE_LEN_LIMIT 256
402 static void reject_invalid_nonce(const char *nonce
, int len
)
406 if (NONCE_LEN_LIMIT
<= len
)
407 die("the receiving end asked to sign an invalid nonce <%.*s>",
410 for (i
= 0; i
< len
; i
++) {
411 int ch
= nonce
[i
] & 0xFF;
413 ch
== '-' || ch
== '.' ||
414 ch
== '/' || ch
== '+' ||
415 ch
== '=' || ch
== '_')
417 die("the receiving end asked to sign an invalid nonce <%.*s>",
422 static void get_commons_through_negotiation(const char *url
,
423 const struct ref
*remote_refs
,
424 struct oid_array
*commons
)
426 struct child_process child
= CHILD_PROCESS_INIT
;
427 const struct ref
*ref
;
428 int len
= the_hash_algo
->hexsz
+ 1; /* hash + NL */
433 strvec_pushl(&child
.args
, "fetch", "--negotiate-only", NULL
);
434 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
435 if (!is_null_oid(&ref
->new_oid
))
436 strvec_pushf(&child
.args
, "--negotiation-tip=%s", oid_to_hex(&ref
->new_oid
));
438 strvec_push(&child
.args
, url
);
440 if (start_command(&child
))
441 die(_("send-pack: unable to fork off fetch subprocess"));
444 char hex_hash
[GIT_MAX_HEXSZ
+ 1];
445 int read_len
= read_in_full(child
.out
, hex_hash
, len
);
446 struct object_id oid
;
452 die("invalid length read %d", read_len
);
453 if (parse_oid_hex(hex_hash
, &oid
, &end
) || *end
!= '\n')
455 oid_array_append(commons
, &oid
);
458 if (finish_command(&child
)) {
460 * The information that push negotiation provides is useful but
463 warning(_("push negotiation failed; proceeding anyway with push"));
467 int send_pack(struct send_pack_args
*args
,
468 int fd
[], struct child_process
*conn
,
469 struct ref
*remote_refs
,
470 struct oid_array
*extra_have
)
472 struct oid_array commons
= OID_ARRAY_INIT
;
475 struct strbuf req_buf
= STRBUF_INIT
;
476 struct strbuf cap_buf
= STRBUF_INIT
;
478 int need_pack_data
= 0;
479 int allow_deleting_refs
= 0;
480 int status_report
= 0;
481 int use_sideband
= 0;
482 int quiet_supported
= 0;
483 int agent_supported
= 0;
484 int advertise_sid
= 0;
485 int push_negotiate
= 0;
487 int atomic_supported
= 0;
488 int use_push_options
= 0;
489 int push_options_supported
= 0;
490 int object_format_supported
= 0;
491 unsigned cmds_sent
= 0;
494 const char *push_cert_nonce
= NULL
;
495 struct packet_reader reader
;
499 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
500 "Perhaps you should specify a branch.\n");
504 git_config_get_bool("push.negotiate", &push_negotiate
);
506 get_commons_through_negotiation(args
->url
, remote_refs
, &commons
);
508 if (!git_config_get_bool("push.usebitmaps", &use_bitmaps
))
509 args
->disable_bitmaps
= !use_bitmaps
;
511 git_config_get_bool("transfer.advertisesid", &advertise_sid
);
513 /* Does the other end support the reporting? */
514 if (server_supports("report-status-v2"))
516 else if (server_supports("report-status"))
518 if (server_supports("delete-refs"))
519 allow_deleting_refs
= 1;
520 if (server_supports("ofs-delta"))
521 args
->use_ofs_delta
= 1;
522 if (server_supports("side-band-64k"))
524 if (server_supports("quiet"))
526 if (server_supports("agent"))
528 if (!server_supports("session-id"))
530 if (server_supports("no-thin"))
531 args
->use_thin_pack
= 0;
532 if (server_supports("atomic"))
533 atomic_supported
= 1;
534 if (server_supports("push-options"))
535 push_options_supported
= 1;
537 if (!server_supports_hash(the_hash_algo
->name
, &object_format_supported
))
538 die(_("the receiving end does not support this repository's hash algorithm"));
540 if (args
->push_cert
!= SEND_PACK_PUSH_CERT_NEVER
) {
542 push_cert_nonce
= server_feature_value("push-cert", &len
);
543 if (push_cert_nonce
) {
544 reject_invalid_nonce(push_cert_nonce
, len
);
545 push_cert_nonce
= xmemdupz(push_cert_nonce
, len
);
546 } else if (args
->push_cert
== SEND_PACK_PUSH_CERT_ALWAYS
) {
547 die(_("the receiving end does not support --signed push"));
548 } else if (args
->push_cert
== SEND_PACK_PUSH_CERT_IF_ASKED
) {
549 warning(_("not sending a push certificate since the"
550 " receiving end does not support --signed"
555 if (args
->atomic
&& !atomic_supported
)
556 die(_("the receiving end does not support --atomic push"));
558 use_atomic
= atomic_supported
&& args
->atomic
;
560 if (args
->push_options
&& !push_options_supported
)
561 die(_("the receiving end does not support push options"));
563 use_push_options
= push_options_supported
&& args
->push_options
;
565 if (status_report
== 1)
566 strbuf_addstr(&cap_buf
, " report-status");
567 else if (status_report
== 2)
568 strbuf_addstr(&cap_buf
, " report-status-v2");
570 strbuf_addstr(&cap_buf
, " side-band-64k");
571 if (quiet_supported
&& (args
->quiet
|| !args
->progress
))
572 strbuf_addstr(&cap_buf
, " quiet");
574 strbuf_addstr(&cap_buf
, " atomic");
575 if (use_push_options
)
576 strbuf_addstr(&cap_buf
, " push-options");
577 if (object_format_supported
)
578 strbuf_addf(&cap_buf
, " object-format=%s", the_hash_algo
->name
);
580 strbuf_addf(&cap_buf
, " agent=%s", git_user_agent_sanitized());
582 strbuf_addf(&cap_buf
, " session-id=%s", trace2_session_id());
585 * NEEDSWORK: why does delete-refs have to be so specific to
586 * send-pack machinery that set_ref_status_for_push() cannot
587 * set this bit for us???
589 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
590 if (ref
->deletion
&& !allow_deleting_refs
)
591 ref
->status
= REF_STATUS_REJECT_NODELETE
;
594 * Clear the status for each ref and see if we need to send
597 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
598 switch (check_to_send_update(ref
, args
)) {
599 case 0: /* no error */
601 case CHECK_REF_STATUS_REJECTED
:
603 * When we know the server would reject a ref update if
604 * we were to send it and we're trying to send the refs
605 * atomically, abort the whole operation.
608 strbuf_release(&req_buf
);
609 strbuf_release(&cap_buf
);
610 reject_atomic_push(remote_refs
, args
->send_mirror
);
611 error("atomic push failed for ref %s. status: %d\n",
612 ref
->name
, ref
->status
);
613 return args
->porcelain
? 0 : -1;
615 /* else fallthrough */
622 if (args
->dry_run
|| !status_report
)
623 ref
->status
= REF_STATUS_OK
;
625 ref
->status
= REF_STATUS_EXPECTING_REPORT
;
629 advertise_shallow_grafts_buf(&req_buf
);
632 * Finally, tell the other end!
634 if (!args
->dry_run
&& push_cert_nonce
)
635 cmds_sent
= generate_push_cert(&req_buf
, remote_refs
, args
,
636 cap_buf
.buf
, push_cert_nonce
);
637 else if (!args
->dry_run
)
638 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
639 char *old_hex
, *new_hex
;
641 if (check_to_send_update(ref
, args
) < 0)
644 old_hex
= oid_to_hex(&ref
->old_oid
);
645 new_hex
= oid_to_hex(&ref
->new_oid
);
647 packet_buf_write(&req_buf
,
649 old_hex
, new_hex
, ref
->name
, 0,
653 packet_buf_write(&req_buf
, "%s %s %s",
654 old_hex
, new_hex
, ref
->name
);
658 if (use_push_options
) {
659 struct string_list_item
*item
;
661 packet_buf_flush(&req_buf
);
662 for_each_string_list_item(item
, args
->push_options
)
663 packet_buf_write(&req_buf
, "%s", item
->string
);
666 if (args
->stateless_rpc
) {
667 if (!args
->dry_run
&& (cmds_sent
|| is_repository_shallow(the_repository
))) {
668 packet_buf_flush(&req_buf
);
669 send_sideband(out
, -1, req_buf
.buf
, req_buf
.len
, LARGE_PACKET_MAX
);
672 write_or_die(out
, req_buf
.buf
, req_buf
.len
);
675 strbuf_release(&req_buf
);
676 strbuf_release(&cap_buf
);
678 if (use_sideband
&& cmds_sent
) {
679 memset(&demux
, 0, sizeof(demux
));
680 demux
.proc
= sideband_demux
;
683 demux
.isolate_sigpipe
= 1;
684 if (start_async(&demux
))
685 die("send-pack: unable to fork off sideband demultiplexer");
689 packet_reader_init(&reader
, in
, NULL
, 0,
690 PACKET_READ_CHOMP_NEWLINE
|
691 PACKET_READ_DIE_ON_ERR_PACKET
);
693 if (need_pack_data
&& cmds_sent
) {
694 if (pack_objects(out
, remote_refs
, extra_have
, &commons
, args
) < 0) {
695 if (args
->stateless_rpc
)
697 if (git_connection_is_socket(conn
))
698 shutdown(fd
[0], SHUT_WR
);
701 * Do not even bother with the return value; we know we
702 * are failing, and just want the error() side effects,
703 * as well as marking refs with their remote status (if
707 receive_status(&reader
, remote_refs
);
711 finish_async(&demux
);
716 if (!args
->stateless_rpc
)
717 /* Closed by pack_objects() via start_command() */
720 if (args
->stateless_rpc
&& cmds_sent
)
723 if (status_report
&& cmds_sent
)
724 ret
= receive_status(&reader
, remote_refs
);
727 if (args
->stateless_rpc
)
730 if (use_sideband
&& cmds_sent
) {
732 if (finish_async(&demux
)) {
733 error("error in sideband demultiplexer");
744 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
745 switch (ref
->status
) {
746 case REF_STATUS_NONE
:
747 case REF_STATUS_UPTODATE
: