1 #include "git-compat-util.h"
7 #include "object-store-ll.h"
10 #include "run-command.h"
13 #include "send-pack.h"
14 #include "transport.h"
16 #include "oid-array.h"
17 #include "gpg-interface.h"
19 #include "parse-options.h"
21 #include "write-or-die.h"
23 int option_parse_push_signed(const struct option
*opt
,
24 const char *arg
, int unset
)
27 *(int *)(opt
->value
) = SEND_PACK_PUSH_CERT_NEVER
;
30 switch (git_parse_maybe_bool(arg
)) {
32 *(int *)(opt
->value
) = SEND_PACK_PUSH_CERT_ALWAYS
;
35 *(int *)(opt
->value
) = SEND_PACK_PUSH_CERT_NEVER
;
38 if (!strcasecmp("if-asked", arg
)) {
39 *(int *)(opt
->value
) = SEND_PACK_PUSH_CERT_IF_ASKED
;
42 die("bad %s argument: %s", opt
->long_name
, arg
);
45 static void feed_object(const struct object_id
*oid
, FILE *fh
, int negative
)
48 !repo_has_object_file_with_flags(the_repository
, oid
,
49 OBJECT_INFO_SKIP_FETCH_OBJECT
|
55 fputs(oid_to_hex(oid
), fh
);
60 * Make a pack stream and spit it out into file descriptor fd
62 static int pack_objects(int fd
, struct ref
*refs
, struct oid_array
*advertised
,
63 struct oid_array
*negotiated
,
64 struct send_pack_args
*args
)
67 * The child becomes pack-objects --revs; we feed
68 * the revision parameters to it via its stdin and
69 * let its stdout go back to the other end.
71 struct child_process po
= CHILD_PROCESS_INIT
;
76 strvec_push(&po
.args
, "pack-objects");
77 strvec_push(&po
.args
, "--all-progress-implied");
78 strvec_push(&po
.args
, "--revs");
79 strvec_push(&po
.args
, "--stdout");
80 if (args
->use_thin_pack
)
81 strvec_push(&po
.args
, "--thin");
82 if (args
->use_ofs_delta
)
83 strvec_push(&po
.args
, "--delta-base-offset");
84 if (args
->quiet
|| !args
->progress
)
85 strvec_push(&po
.args
, "-q");
87 strvec_push(&po
.args
, "--progress");
88 if (is_repository_shallow(the_repository
))
89 strvec_push(&po
.args
, "--shallow");
90 if (args
->disable_bitmaps
)
91 strvec_push(&po
.args
, "--no-use-bitmap-index");
93 po
.out
= args
->stateless_rpc
? -1 : fd
;
96 if (start_command(&po
))
97 die_errno("git pack-objects failed");
100 * We feed the pack-objects we just spawned with revision
101 * parameters by writing to the pipe.
103 po_in
= xfdopen(po
.in
, "w");
104 for (i
= 0; i
< advertised
->nr
; i
++)
105 feed_object(&advertised
->oid
[i
], po_in
, 1);
106 for (i
= 0; i
< negotiated
->nr
; i
++)
107 feed_object(&negotiated
->oid
[i
], po_in
, 1);
110 if (!is_null_oid(&refs
->old_oid
))
111 feed_object(&refs
->old_oid
, po_in
, 1);
112 if (!is_null_oid(&refs
->new_oid
))
113 feed_object(&refs
->new_oid
, po_in
, 0);
119 die_errno("error writing to pack-objects");
122 if (args
->stateless_rpc
) {
123 char *buf
= xmalloc(LARGE_PACKET_MAX
);
125 ssize_t n
= xread(po
.out
, buf
, LARGE_PACKET_MAX
);
128 send_sideband(fd
, -1, buf
, n
, LARGE_PACKET_MAX
);
135 rc
= finish_command(&po
);
138 * For a normal non-zero exit, we assume pack-objects wrote
139 * something useful to stderr. For death by signal, though,
140 * we should mention it to the user. The exception is SIGPIPE
141 * (141), because that's a normal occurrence if the remote end
142 * hangs up (and we'll report that by trying to read the unpack
145 if (rc
> 128 && rc
!= 141)
146 error("pack-objects died of signal %d", rc
- 128);
152 static int receive_unpack_status(struct packet_reader
*reader
)
154 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
155 return error(_("unexpected flush packet while reading remote unpack status"));
156 if (!skip_prefix(reader
->line
, "unpack ", &reader
->line
))
157 return error(_("unable to parse remote unpack status: %s"), reader
->line
);
158 if (strcmp(reader
->line
, "ok"))
159 return error(_("remote unpack failed: %s"), reader
->line
);
163 static int receive_status(struct packet_reader
*reader
, struct ref
*refs
)
167 struct ref_push_report
*report
= NULL
;
172 ret
= receive_unpack_status(reader
);
174 struct object_id old_oid
, new_oid
;
178 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
181 p
= strchr(head
, ' ');
183 error("invalid status line from remote: %s", reader
->line
);
189 if (!strcmp(head
, "option")) {
190 const char *key
, *val
;
192 if (!hint
|| !(report
|| new_report
)) {
194 error("'option' without a matching 'ok/ng' directive");
200 CALLOC_ARRAY(hint
->report
, 1);
201 report
= hint
->report
;
203 report
= hint
->report
;
205 report
= report
->next
;
206 CALLOC_ARRAY(report
->next
, 1);
207 report
= report
->next
;
212 p
= strchr(key
, ' ');
216 if (!strcmp(key
, "refname"))
217 report
->ref_name
= xstrdup_or_null(val
);
218 else if (!strcmp(key
, "old-oid") && val
&&
219 !parse_oid_hex(val
, &old_oid
, &val
))
220 report
->old_oid
= oiddup(&old_oid
);
221 else if (!strcmp(key
, "new-oid") && val
&&
222 !parse_oid_hex(val
, &new_oid
, &val
))
223 report
->new_oid
= oiddup(&new_oid
);
224 else if (!strcmp(key
, "forced-update"))
225 report
->forced_update
= 1;
231 if (strcmp(head
, "ok") && strcmp(head
, "ng")) {
232 error("invalid ref status from remote: %s", head
);
237 p
= strchr(refname
, ' ');
240 /* first try searching at our hint, falling back to all refs */
242 hint
= find_ref_by_name(hint
, refname
);
244 hint
= find_ref_by_name(refs
, refname
);
246 warning("remote reported status on unknown ref: %s",
250 if (hint
->status
!= REF_STATUS_EXPECTING_REPORT
&&
251 hint
->status
!= REF_STATUS_OK
&&
252 hint
->status
!= REF_STATUS_REMOTE_REJECT
) {
253 warning("remote reported status on unexpected ref: %s",
257 if (!strcmp(head
, "ng")) {
258 hint
->status
= REF_STATUS_REMOTE_REJECT
;
260 hint
->remote_status
= xstrdup(p
);
262 hint
->remote_status
= "failed";
264 hint
->status
= REF_STATUS_OK
;
265 hint
->remote_status
= xstrdup_or_null(p
);
272 static int sideband_demux(int in UNUSED
, int out
, void *data
)
275 if (async_with_fork())
277 ret
= recv_sideband("send-pack", fd
[0], out
);
282 static int advertise_shallow_grafts_cb(const struct commit_graft
*graft
, void *cb
)
284 struct strbuf
*sb
= cb
;
285 if (graft
->nr_parent
== -1)
286 packet_buf_write(sb
, "shallow %s\n", oid_to_hex(&graft
->oid
));
290 static void advertise_shallow_grafts_buf(struct strbuf
*sb
)
292 if (!is_repository_shallow(the_repository
))
294 for_each_commit_graft(advertise_shallow_grafts_cb
, sb
);
297 #define CHECK_REF_NO_PUSH -1
298 #define CHECK_REF_STATUS_REJECTED -2
299 #define CHECK_REF_UPTODATE -3
300 static int check_to_send_update(const struct ref
*ref
, const struct send_pack_args
*args
)
302 if (!ref
->peer_ref
&& !args
->send_mirror
)
303 return CHECK_REF_NO_PUSH
;
305 /* Check for statuses set by set_ref_status_for_push() */
306 switch (ref
->status
) {
307 case REF_STATUS_REJECT_NONFASTFORWARD
:
308 case REF_STATUS_REJECT_ALREADY_EXISTS
:
309 case REF_STATUS_REJECT_FETCH_FIRST
:
310 case REF_STATUS_REJECT_NEEDS_FORCE
:
311 case REF_STATUS_REJECT_STALE
:
312 case REF_STATUS_REJECT_REMOTE_UPDATED
:
313 case REF_STATUS_REJECT_NODELETE
:
314 return CHECK_REF_STATUS_REJECTED
;
315 case REF_STATUS_UPTODATE
:
316 return CHECK_REF_UPTODATE
;
319 case REF_STATUS_EXPECTING_REPORT
:
320 /* already passed checks on the local side */
322 /* of course this is OK */
328 * the beginning of the next line, or the end of buffer.
330 * NEEDSWORK: perhaps move this to git-compat-util.h or somewhere and
331 * convert many similar uses found by "git grep -A4 memchr".
333 static const char *next_line(const char *line
, size_t len
)
335 const char *nl
= memchr(line
, '\n', len
);
337 return line
+ len
; /* incomplete line */
341 static int generate_push_cert(struct strbuf
*req_buf
,
342 const struct ref
*remote_refs
,
343 struct send_pack_args
*args
,
344 const char *cap_string
,
345 const char *push_cert_nonce
)
347 const struct ref
*ref
;
348 struct string_list_item
*item
;
349 char *signing_key_id
= xstrdup(get_signing_key_id());
351 struct strbuf cert
= STRBUF_INIT
;
354 strbuf_addstr(&cert
, "certificate version 0.1\n");
355 strbuf_addf(&cert
, "pusher %s ", signing_key_id
);
357 strbuf_addch(&cert
, '\n');
358 if (args
->url
&& *args
->url
) {
359 char *anon_url
= transport_anonymize_url(args
->url
);
360 strbuf_addf(&cert
, "pushee %s\n", anon_url
);
363 if (push_cert_nonce
[0])
364 strbuf_addf(&cert
, "nonce %s\n", push_cert_nonce
);
365 if (args
->push_options
)
366 for_each_string_list_item(item
, args
->push_options
)
367 strbuf_addf(&cert
, "push-option %s\n", item
->string
);
368 strbuf_addstr(&cert
, "\n");
370 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
371 if (check_to_send_update(ref
, args
) < 0)
374 strbuf_addf(&cert
, "%s %s %s\n",
375 oid_to_hex(&ref
->old_oid
),
376 oid_to_hex(&ref
->new_oid
),
382 if (sign_buffer(&cert
, &cert
, get_signing_key()))
383 die(_("failed to sign the push certificate"));
385 packet_buf_write(req_buf
, "push-cert%c%s", 0, cap_string
);
386 for (cp
= cert
.buf
; cp
< cert
.buf
+ cert
.len
; cp
= np
) {
387 np
= next_line(cp
, cert
.buf
+ cert
.len
- cp
);
388 packet_buf_write(req_buf
,
389 "%.*s", (int)(np
- cp
), cp
);
391 packet_buf_write(req_buf
, "push-cert-end\n");
394 free(signing_key_id
);
395 strbuf_release(&cert
);
399 #define NONCE_LEN_LIMIT 256
401 static void reject_invalid_nonce(const char *nonce
, int len
)
405 if (NONCE_LEN_LIMIT
<= len
)
406 die("the receiving end asked to sign an invalid nonce <%.*s>",
409 for (i
= 0; i
< len
; i
++) {
410 int ch
= nonce
[i
] & 0xFF;
412 ch
== '-' || ch
== '.' ||
413 ch
== '/' || ch
== '+' ||
414 ch
== '=' || ch
== '_')
416 die("the receiving end asked to sign an invalid nonce <%.*s>",
421 static void get_commons_through_negotiation(const char *url
,
422 const struct ref
*remote_refs
,
423 struct oid_array
*commons
)
425 struct child_process child
= CHILD_PROCESS_INIT
;
426 const struct ref
*ref
;
427 int len
= the_hash_algo
->hexsz
+ 1; /* hash + NL */
432 strvec_pushl(&child
.args
, "fetch", "--negotiate-only", NULL
);
433 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
434 if (!is_null_oid(&ref
->new_oid
))
435 strvec_pushf(&child
.args
, "--negotiation-tip=%s", oid_to_hex(&ref
->new_oid
));
437 strvec_push(&child
.args
, url
);
439 if (start_command(&child
))
440 die(_("send-pack: unable to fork off fetch subprocess"));
443 char hex_hash
[GIT_MAX_HEXSZ
+ 1];
444 int read_len
= read_in_full(child
.out
, hex_hash
, len
);
445 struct object_id oid
;
451 die("invalid length read %d", read_len
);
452 if (parse_oid_hex(hex_hash
, &oid
, &end
) || *end
!= '\n')
454 oid_array_append(commons
, &oid
);
457 if (finish_command(&child
)) {
459 * The information that push negotiation provides is useful but
462 warning(_("push negotiation failed; proceeding anyway with push"));
466 int send_pack(struct send_pack_args
*args
,
467 int fd
[], struct child_process
*conn
,
468 struct ref
*remote_refs
,
469 struct oid_array
*extra_have
)
471 struct oid_array commons
= OID_ARRAY_INIT
;
474 struct strbuf req_buf
= STRBUF_INIT
;
475 struct strbuf cap_buf
= STRBUF_INIT
;
477 int need_pack_data
= 0;
478 int allow_deleting_refs
= 0;
479 int status_report
= 0;
480 int use_sideband
= 0;
481 int quiet_supported
= 0;
482 int agent_supported
= 0;
483 int advertise_sid
= 0;
484 int push_negotiate
= 0;
486 int atomic_supported
= 0;
487 int use_push_options
= 0;
488 int push_options_supported
= 0;
489 int object_format_supported
= 0;
490 unsigned cmds_sent
= 0;
493 const char *push_cert_nonce
= NULL
;
494 struct packet_reader reader
;
498 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
499 "Perhaps you should specify a branch.\n");
503 git_config_get_bool("push.negotiate", &push_negotiate
);
505 get_commons_through_negotiation(args
->url
, remote_refs
, &commons
);
507 if (!git_config_get_bool("push.usebitmaps", &use_bitmaps
))
508 args
->disable_bitmaps
= !use_bitmaps
;
510 git_config_get_bool("transfer.advertisesid", &advertise_sid
);
512 /* Does the other end support the reporting? */
513 if (server_supports("report-status-v2"))
515 else if (server_supports("report-status"))
517 if (server_supports("delete-refs"))
518 allow_deleting_refs
= 1;
519 if (server_supports("ofs-delta"))
520 args
->use_ofs_delta
= 1;
521 if (server_supports("side-band-64k"))
523 if (server_supports("quiet"))
525 if (server_supports("agent"))
527 if (!server_supports("session-id"))
529 if (server_supports("no-thin"))
530 args
->use_thin_pack
= 0;
531 if (server_supports("atomic"))
532 atomic_supported
= 1;
533 if (server_supports("push-options"))
534 push_options_supported
= 1;
536 if (!server_supports_hash(the_hash_algo
->name
, &object_format_supported
))
537 die(_("the receiving end does not support this repository's hash algorithm"));
539 if (args
->push_cert
!= SEND_PACK_PUSH_CERT_NEVER
) {
541 push_cert_nonce
= server_feature_value("push-cert", &len
);
542 if (push_cert_nonce
) {
543 reject_invalid_nonce(push_cert_nonce
, len
);
544 push_cert_nonce
= xmemdupz(push_cert_nonce
, len
);
545 } else if (args
->push_cert
== SEND_PACK_PUSH_CERT_ALWAYS
) {
546 die(_("the receiving end does not support --signed push"));
547 } else if (args
->push_cert
== SEND_PACK_PUSH_CERT_IF_ASKED
) {
548 warning(_("not sending a push certificate since the"
549 " receiving end does not support --signed"
554 if (args
->atomic
&& !atomic_supported
)
555 die(_("the receiving end does not support --atomic push"));
557 use_atomic
= atomic_supported
&& args
->atomic
;
559 if (args
->push_options
&& !push_options_supported
)
560 die(_("the receiving end does not support push options"));
562 use_push_options
= push_options_supported
&& args
->push_options
;
564 if (status_report
== 1)
565 strbuf_addstr(&cap_buf
, " report-status");
566 else if (status_report
== 2)
567 strbuf_addstr(&cap_buf
, " report-status-v2");
569 strbuf_addstr(&cap_buf
, " side-band-64k");
570 if (quiet_supported
&& (args
->quiet
|| !args
->progress
))
571 strbuf_addstr(&cap_buf
, " quiet");
573 strbuf_addstr(&cap_buf
, " atomic");
574 if (use_push_options
)
575 strbuf_addstr(&cap_buf
, " push-options");
576 if (object_format_supported
)
577 strbuf_addf(&cap_buf
, " object-format=%s", the_hash_algo
->name
);
579 strbuf_addf(&cap_buf
, " agent=%s", git_user_agent_sanitized());
581 strbuf_addf(&cap_buf
, " session-id=%s", trace2_session_id());
584 * NEEDSWORK: why does delete-refs have to be so specific to
585 * send-pack machinery that set_ref_status_for_push() cannot
586 * set this bit for us???
588 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
589 if (ref
->deletion
&& !allow_deleting_refs
)
590 ref
->status
= REF_STATUS_REJECT_NODELETE
;
593 * Clear the status for each ref and see if we need to send
596 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
597 switch (check_to_send_update(ref
, args
)) {
598 case 0: /* no error */
600 case CHECK_REF_STATUS_REJECTED
:
602 * When we know the server would reject a ref update if
603 * we were to send it and we're trying to send the refs
604 * atomically, abort the whole operation.
607 strbuf_release(&req_buf
);
608 strbuf_release(&cap_buf
);
609 reject_atomic_push(remote_refs
, args
->send_mirror
);
610 error("atomic push failed for ref %s. status: %d\n",
611 ref
->name
, ref
->status
);
612 return args
->porcelain
? 0 : -1;
614 /* else fallthrough */
621 if (args
->dry_run
|| !status_report
)
622 ref
->status
= REF_STATUS_OK
;
624 ref
->status
= REF_STATUS_EXPECTING_REPORT
;
628 advertise_shallow_grafts_buf(&req_buf
);
631 * Finally, tell the other end!
633 if (!args
->dry_run
&& push_cert_nonce
)
634 cmds_sent
= generate_push_cert(&req_buf
, remote_refs
, args
,
635 cap_buf
.buf
, push_cert_nonce
);
636 else if (!args
->dry_run
)
637 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
638 char *old_hex
, *new_hex
;
640 if (check_to_send_update(ref
, args
) < 0)
643 old_hex
= oid_to_hex(&ref
->old_oid
);
644 new_hex
= oid_to_hex(&ref
->new_oid
);
646 packet_buf_write(&req_buf
,
648 old_hex
, new_hex
, ref
->name
, 0,
652 packet_buf_write(&req_buf
, "%s %s %s",
653 old_hex
, new_hex
, ref
->name
);
657 if (use_push_options
) {
658 struct string_list_item
*item
;
660 packet_buf_flush(&req_buf
);
661 for_each_string_list_item(item
, args
->push_options
)
662 packet_buf_write(&req_buf
, "%s", item
->string
);
665 if (args
->stateless_rpc
) {
666 if (!args
->dry_run
&& (cmds_sent
|| is_repository_shallow(the_repository
))) {
667 packet_buf_flush(&req_buf
);
668 send_sideband(out
, -1, req_buf
.buf
, req_buf
.len
, LARGE_PACKET_MAX
);
671 write_or_die(out
, req_buf
.buf
, req_buf
.len
);
674 strbuf_release(&req_buf
);
675 strbuf_release(&cap_buf
);
677 if (use_sideband
&& cmds_sent
) {
678 memset(&demux
, 0, sizeof(demux
));
679 demux
.proc
= sideband_demux
;
682 demux
.isolate_sigpipe
= 1;
683 if (start_async(&demux
))
684 die("send-pack: unable to fork off sideband demultiplexer");
688 packet_reader_init(&reader
, in
, NULL
, 0,
689 PACKET_READ_CHOMP_NEWLINE
|
690 PACKET_READ_DIE_ON_ERR_PACKET
);
692 if (need_pack_data
&& cmds_sent
) {
693 if (pack_objects(out
, remote_refs
, extra_have
, &commons
, args
) < 0) {
694 if (args
->stateless_rpc
)
696 if (git_connection_is_socket(conn
))
697 shutdown(fd
[0], SHUT_WR
);
700 * Do not even bother with the return value; we know we
701 * are failing, and just want the error() side effects,
702 * as well as marking refs with their remote status (if
706 receive_status(&reader
, remote_refs
);
710 finish_async(&demux
);
715 if (!args
->stateless_rpc
)
716 /* Closed by pack_objects() via start_command() */
719 if (args
->stateless_rpc
&& cmds_sent
)
722 if (status_report
&& cmds_sent
)
723 ret
= receive_status(&reader
, remote_refs
);
726 if (args
->stateless_rpc
)
729 if (use_sideband
&& cmds_sent
) {
731 if (finish_async(&demux
)) {
732 error("error in sideband demultiplexer");
743 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
744 switch (ref
->status
) {
745 case REF_STATUS_NONE
:
746 case REF_STATUS_UPTODATE
: