5 #include "object-store.h"
8 #include "run-command.h"
11 #include "send-pack.h"
13 #include "transport.h"
15 #include "oid-array.h"
16 #include "gpg-interface.h"
19 #include "parse-options.h"
21 int option_parse_push_signed(const struct option
*opt
,
22 const char *arg
, int unset
)
25 *(int *)(opt
->value
) = SEND_PACK_PUSH_CERT_NEVER
;
28 switch (git_parse_maybe_bool(arg
)) {
30 *(int *)(opt
->value
) = SEND_PACK_PUSH_CERT_ALWAYS
;
33 *(int *)(opt
->value
) = SEND_PACK_PUSH_CERT_NEVER
;
36 if (!strcasecmp("if-asked", arg
)) {
37 *(int *)(opt
->value
) = SEND_PACK_PUSH_CERT_IF_ASKED
;
40 die("bad %s argument: %s", opt
->long_name
, arg
);
43 static void feed_object(const struct object_id
*oid
, FILE *fh
, int negative
)
46 !has_object_file_with_flags(oid
,
47 OBJECT_INFO_SKIP_FETCH_OBJECT
|
53 fputs(oid_to_hex(oid
), fh
);
58 * Make a pack stream and spit it out into file descriptor fd
60 static int pack_objects(int fd
, struct ref
*refs
, struct oid_array
*advertised
,
61 struct oid_array
*negotiated
,
62 struct send_pack_args
*args
)
65 * The child becomes pack-objects --revs; we feed
66 * the revision parameters to it via its stdin and
67 * let its stdout go back to the other end.
69 struct child_process po
= CHILD_PROCESS_INIT
;
74 strvec_push(&po
.args
, "pack-objects");
75 strvec_push(&po
.args
, "--all-progress-implied");
76 strvec_push(&po
.args
, "--revs");
77 strvec_push(&po
.args
, "--stdout");
78 if (args
->use_thin_pack
)
79 strvec_push(&po
.args
, "--thin");
80 if (args
->use_ofs_delta
)
81 strvec_push(&po
.args
, "--delta-base-offset");
82 if (args
->quiet
|| !args
->progress
)
83 strvec_push(&po
.args
, "-q");
85 strvec_push(&po
.args
, "--progress");
86 if (is_repository_shallow(the_repository
))
87 strvec_push(&po
.args
, "--shallow");
88 if (args
->disable_bitmaps
)
89 strvec_push(&po
.args
, "--no-use-bitmap-index");
91 po
.out
= args
->stateless_rpc
? -1 : fd
;
94 if (start_command(&po
))
95 die_errno("git pack-objects failed");
98 * We feed the pack-objects we just spawned with revision
99 * parameters by writing to the pipe.
101 po_in
= xfdopen(po
.in
, "w");
102 for (i
= 0; i
< advertised
->nr
; i
++)
103 feed_object(&advertised
->oid
[i
], po_in
, 1);
104 for (i
= 0; i
< negotiated
->nr
; i
++)
105 feed_object(&negotiated
->oid
[i
], po_in
, 1);
108 if (!is_null_oid(&refs
->old_oid
))
109 feed_object(&refs
->old_oid
, po_in
, 1);
110 if (!is_null_oid(&refs
->new_oid
))
111 feed_object(&refs
->new_oid
, po_in
, 0);
117 die_errno("error writing to pack-objects");
120 if (args
->stateless_rpc
) {
121 char *buf
= xmalloc(LARGE_PACKET_MAX
);
123 ssize_t n
= xread(po
.out
, buf
, LARGE_PACKET_MAX
);
126 send_sideband(fd
, -1, buf
, n
, LARGE_PACKET_MAX
);
133 rc
= finish_command(&po
);
136 * For a normal non-zero exit, we assume pack-objects wrote
137 * something useful to stderr. For death by signal, though,
138 * we should mention it to the user. The exception is SIGPIPE
139 * (141), because that's a normal occurrence if the remote end
140 * hangs up (and we'll report that by trying to read the unpack
143 if (rc
> 128 && rc
!= 141)
144 error("pack-objects died of signal %d", rc
- 128);
150 static int receive_unpack_status(struct packet_reader
*reader
)
152 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
153 return error(_("unexpected flush packet while reading remote unpack status"));
154 if (!skip_prefix(reader
->line
, "unpack ", &reader
->line
))
155 return error(_("unable to parse remote unpack status: %s"), reader
->line
);
156 if (strcmp(reader
->line
, "ok"))
157 return error(_("remote unpack failed: %s"), reader
->line
);
161 static int receive_status(struct packet_reader
*reader
, struct ref
*refs
)
165 struct ref_push_report
*report
= NULL
;
170 ret
= receive_unpack_status(reader
);
172 struct object_id old_oid
, new_oid
;
176 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
179 p
= strchr(head
, ' ');
181 error("invalid status line from remote: %s", reader
->line
);
187 if (!strcmp(head
, "option")) {
188 const char *key
, *val
;
190 if (!hint
|| !(report
|| new_report
)) {
192 error("'option' without a matching 'ok/ng' directive");
198 CALLOC_ARRAY(hint
->report
, 1);
199 report
= hint
->report
;
201 report
= hint
->report
;
203 report
= report
->next
;
204 CALLOC_ARRAY(report
->next
, 1);
205 report
= report
->next
;
210 p
= strchr(key
, ' ');
214 if (!strcmp(key
, "refname"))
215 report
->ref_name
= xstrdup_or_null(val
);
216 else if (!strcmp(key
, "old-oid") && val
&&
217 !parse_oid_hex(val
, &old_oid
, &val
))
218 report
->old_oid
= oiddup(&old_oid
);
219 else if (!strcmp(key
, "new-oid") && val
&&
220 !parse_oid_hex(val
, &new_oid
, &val
))
221 report
->new_oid
= oiddup(&new_oid
);
222 else if (!strcmp(key
, "forced-update"))
223 report
->forced_update
= 1;
229 if (strcmp(head
, "ok") && strcmp(head
, "ng")) {
230 error("invalid ref status from remote: %s", head
);
235 p
= strchr(refname
, ' ');
238 /* first try searching at our hint, falling back to all refs */
240 hint
= find_ref_by_name(hint
, refname
);
242 hint
= find_ref_by_name(refs
, refname
);
244 warning("remote reported status on unknown ref: %s",
248 if (hint
->status
!= REF_STATUS_EXPECTING_REPORT
&&
249 hint
->status
!= REF_STATUS_OK
&&
250 hint
->status
!= REF_STATUS_REMOTE_REJECT
) {
251 warning("remote reported status on unexpected ref: %s",
255 if (!strcmp(head
, "ng")) {
256 hint
->status
= REF_STATUS_REMOTE_REJECT
;
258 hint
->remote_status
= xstrdup(p
);
260 hint
->remote_status
= "failed";
262 hint
->status
= REF_STATUS_OK
;
263 hint
->remote_status
= xstrdup_or_null(p
);
270 static int sideband_demux(int in UNUSED
, int out
, void *data
)
273 if (async_with_fork())
275 ret
= recv_sideband("send-pack", fd
[0], out
);
280 static int advertise_shallow_grafts_cb(const struct commit_graft
*graft
, void *cb
)
282 struct strbuf
*sb
= cb
;
283 if (graft
->nr_parent
== -1)
284 packet_buf_write(sb
, "shallow %s\n", oid_to_hex(&graft
->oid
));
288 static void advertise_shallow_grafts_buf(struct strbuf
*sb
)
290 if (!is_repository_shallow(the_repository
))
292 for_each_commit_graft(advertise_shallow_grafts_cb
, sb
);
295 #define CHECK_REF_NO_PUSH -1
296 #define CHECK_REF_STATUS_REJECTED -2
297 #define CHECK_REF_UPTODATE -3
298 static int check_to_send_update(const struct ref
*ref
, const struct send_pack_args
*args
)
300 if (!ref
->peer_ref
&& !args
->send_mirror
)
301 return CHECK_REF_NO_PUSH
;
303 /* Check for statuses set by set_ref_status_for_push() */
304 switch (ref
->status
) {
305 case REF_STATUS_REJECT_NONFASTFORWARD
:
306 case REF_STATUS_REJECT_ALREADY_EXISTS
:
307 case REF_STATUS_REJECT_FETCH_FIRST
:
308 case REF_STATUS_REJECT_NEEDS_FORCE
:
309 case REF_STATUS_REJECT_STALE
:
310 case REF_STATUS_REJECT_REMOTE_UPDATED
:
311 case REF_STATUS_REJECT_NODELETE
:
312 return CHECK_REF_STATUS_REJECTED
;
313 case REF_STATUS_UPTODATE
:
314 return CHECK_REF_UPTODATE
;
317 case REF_STATUS_EXPECTING_REPORT
:
318 /* already passed checks on the local side */
320 /* of course this is OK */
326 * the beginning of the next line, or the end of buffer.
328 * NEEDSWORK: perhaps move this to git-compat-util.h or somewhere and
329 * convert many similar uses found by "git grep -A4 memchr".
331 static const char *next_line(const char *line
, size_t len
)
333 const char *nl
= memchr(line
, '\n', len
);
335 return line
+ len
; /* incomplete line */
339 static int generate_push_cert(struct strbuf
*req_buf
,
340 const struct ref
*remote_refs
,
341 struct send_pack_args
*args
,
342 const char *cap_string
,
343 const char *push_cert_nonce
)
345 const struct ref
*ref
;
346 struct string_list_item
*item
;
347 char *signing_key_id
= xstrdup(get_signing_key_id());
349 struct strbuf cert
= STRBUF_INIT
;
352 strbuf_addstr(&cert
, "certificate version 0.1\n");
353 strbuf_addf(&cert
, "pusher %s ", signing_key_id
);
355 strbuf_addch(&cert
, '\n');
356 if (args
->url
&& *args
->url
) {
357 char *anon_url
= transport_anonymize_url(args
->url
);
358 strbuf_addf(&cert
, "pushee %s\n", anon_url
);
361 if (push_cert_nonce
[0])
362 strbuf_addf(&cert
, "nonce %s\n", push_cert_nonce
);
363 if (args
->push_options
)
364 for_each_string_list_item(item
, args
->push_options
)
365 strbuf_addf(&cert
, "push-option %s\n", item
->string
);
366 strbuf_addstr(&cert
, "\n");
368 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
369 if (check_to_send_update(ref
, args
) < 0)
372 strbuf_addf(&cert
, "%s %s %s\n",
373 oid_to_hex(&ref
->old_oid
),
374 oid_to_hex(&ref
->new_oid
),
380 if (sign_buffer(&cert
, &cert
, get_signing_key()))
381 die(_("failed to sign the push certificate"));
383 packet_buf_write(req_buf
, "push-cert%c%s", 0, cap_string
);
384 for (cp
= cert
.buf
; cp
< cert
.buf
+ cert
.len
; cp
= np
) {
385 np
= next_line(cp
, cert
.buf
+ cert
.len
- cp
);
386 packet_buf_write(req_buf
,
387 "%.*s", (int)(np
- cp
), cp
);
389 packet_buf_write(req_buf
, "push-cert-end\n");
392 free(signing_key_id
);
393 strbuf_release(&cert
);
397 #define NONCE_LEN_LIMIT 256
399 static void reject_invalid_nonce(const char *nonce
, int len
)
403 if (NONCE_LEN_LIMIT
<= len
)
404 die("the receiving end asked to sign an invalid nonce <%.*s>",
407 for (i
= 0; i
< len
; i
++) {
408 int ch
= nonce
[i
] & 0xFF;
410 ch
== '-' || ch
== '.' ||
411 ch
== '/' || ch
== '+' ||
412 ch
== '=' || ch
== '_')
414 die("the receiving end asked to sign an invalid nonce <%.*s>",
419 static void get_commons_through_negotiation(const char *url
,
420 const struct ref
*remote_refs
,
421 struct oid_array
*commons
)
423 struct child_process child
= CHILD_PROCESS_INIT
;
424 const struct ref
*ref
;
425 int len
= the_hash_algo
->hexsz
+ 1; /* hash + NL */
430 strvec_pushl(&child
.args
, "fetch", "--negotiate-only", NULL
);
431 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
432 if (!is_null_oid(&ref
->new_oid
))
433 strvec_pushf(&child
.args
, "--negotiation-tip=%s", oid_to_hex(&ref
->new_oid
));
435 strvec_push(&child
.args
, url
);
437 if (start_command(&child
))
438 die(_("send-pack: unable to fork off fetch subprocess"));
441 char hex_hash
[GIT_MAX_HEXSZ
+ 1];
442 int read_len
= read_in_full(child
.out
, hex_hash
, len
);
443 struct object_id oid
;
449 die("invalid length read %d", read_len
);
450 if (parse_oid_hex(hex_hash
, &oid
, &end
) || *end
!= '\n')
452 oid_array_append(commons
, &oid
);
455 if (finish_command(&child
)) {
457 * The information that push negotiation provides is useful but
460 warning(_("push negotiation failed; proceeding anyway with push"));
464 int send_pack(struct send_pack_args
*args
,
465 int fd
[], struct child_process
*conn
,
466 struct ref
*remote_refs
,
467 struct oid_array
*extra_have
)
469 struct oid_array commons
= OID_ARRAY_INIT
;
472 struct strbuf req_buf
= STRBUF_INIT
;
473 struct strbuf cap_buf
= STRBUF_INIT
;
475 int need_pack_data
= 0;
476 int allow_deleting_refs
= 0;
477 int status_report
= 0;
478 int use_sideband
= 0;
479 int quiet_supported
= 0;
480 int agent_supported
= 0;
481 int advertise_sid
= 0;
482 int push_negotiate
= 0;
484 int atomic_supported
= 0;
485 int use_push_options
= 0;
486 int push_options_supported
= 0;
487 int object_format_supported
= 0;
488 unsigned cmds_sent
= 0;
491 const char *push_cert_nonce
= NULL
;
492 struct packet_reader reader
;
496 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
497 "Perhaps you should specify a branch.\n");
501 git_config_get_bool("push.negotiate", &push_negotiate
);
503 get_commons_through_negotiation(args
->url
, remote_refs
, &commons
);
505 if (!git_config_get_bool("push.usebitmaps", &use_bitmaps
))
506 args
->disable_bitmaps
= !use_bitmaps
;
508 git_config_get_bool("transfer.advertisesid", &advertise_sid
);
510 /* Does the other end support the reporting? */
511 if (server_supports("report-status-v2"))
513 else if (server_supports("report-status"))
515 if (server_supports("delete-refs"))
516 allow_deleting_refs
= 1;
517 if (server_supports("ofs-delta"))
518 args
->use_ofs_delta
= 1;
519 if (server_supports("side-band-64k"))
521 if (server_supports("quiet"))
523 if (server_supports("agent"))
525 if (!server_supports("session-id"))
527 if (server_supports("no-thin"))
528 args
->use_thin_pack
= 0;
529 if (server_supports("atomic"))
530 atomic_supported
= 1;
531 if (server_supports("push-options"))
532 push_options_supported
= 1;
534 if (!server_supports_hash(the_hash_algo
->name
, &object_format_supported
))
535 die(_("the receiving end does not support this repository's hash algorithm"));
537 if (args
->push_cert
!= SEND_PACK_PUSH_CERT_NEVER
) {
539 push_cert_nonce
= server_feature_value("push-cert", &len
);
540 if (push_cert_nonce
) {
541 reject_invalid_nonce(push_cert_nonce
, len
);
542 push_cert_nonce
= xmemdupz(push_cert_nonce
, len
);
543 } else if (args
->push_cert
== SEND_PACK_PUSH_CERT_ALWAYS
) {
544 die(_("the receiving end does not support --signed push"));
545 } else if (args
->push_cert
== SEND_PACK_PUSH_CERT_IF_ASKED
) {
546 warning(_("not sending a push certificate since the"
547 " receiving end does not support --signed"
552 if (args
->atomic
&& !atomic_supported
)
553 die(_("the receiving end does not support --atomic push"));
555 use_atomic
= atomic_supported
&& args
->atomic
;
557 if (args
->push_options
&& !push_options_supported
)
558 die(_("the receiving end does not support push options"));
560 use_push_options
= push_options_supported
&& args
->push_options
;
562 if (status_report
== 1)
563 strbuf_addstr(&cap_buf
, " report-status");
564 else if (status_report
== 2)
565 strbuf_addstr(&cap_buf
, " report-status-v2");
567 strbuf_addstr(&cap_buf
, " side-band-64k");
568 if (quiet_supported
&& (args
->quiet
|| !args
->progress
))
569 strbuf_addstr(&cap_buf
, " quiet");
571 strbuf_addstr(&cap_buf
, " atomic");
572 if (use_push_options
)
573 strbuf_addstr(&cap_buf
, " push-options");
574 if (object_format_supported
)
575 strbuf_addf(&cap_buf
, " object-format=%s", the_hash_algo
->name
);
577 strbuf_addf(&cap_buf
, " agent=%s", git_user_agent_sanitized());
579 strbuf_addf(&cap_buf
, " session-id=%s", trace2_session_id());
582 * NEEDSWORK: why does delete-refs have to be so specific to
583 * send-pack machinery that set_ref_status_for_push() cannot
584 * set this bit for us???
586 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
587 if (ref
->deletion
&& !allow_deleting_refs
)
588 ref
->status
= REF_STATUS_REJECT_NODELETE
;
591 * Clear the status for each ref and see if we need to send
594 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
595 switch (check_to_send_update(ref
, args
)) {
596 case 0: /* no error */
598 case CHECK_REF_STATUS_REJECTED
:
600 * When we know the server would reject a ref update if
601 * we were to send it and we're trying to send the refs
602 * atomically, abort the whole operation.
605 strbuf_release(&req_buf
);
606 strbuf_release(&cap_buf
);
607 reject_atomic_push(remote_refs
, args
->send_mirror
);
608 error("atomic push failed for ref %s. status: %d\n",
609 ref
->name
, ref
->status
);
610 return args
->porcelain
? 0 : -1;
612 /* else fallthrough */
619 if (args
->dry_run
|| !status_report
)
620 ref
->status
= REF_STATUS_OK
;
622 ref
->status
= REF_STATUS_EXPECTING_REPORT
;
626 advertise_shallow_grafts_buf(&req_buf
);
629 * Finally, tell the other end!
631 if (!args
->dry_run
&& push_cert_nonce
)
632 cmds_sent
= generate_push_cert(&req_buf
, remote_refs
, args
,
633 cap_buf
.buf
, push_cert_nonce
);
634 else if (!args
->dry_run
)
635 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
636 char *old_hex
, *new_hex
;
638 if (check_to_send_update(ref
, args
) < 0)
641 old_hex
= oid_to_hex(&ref
->old_oid
);
642 new_hex
= oid_to_hex(&ref
->new_oid
);
644 packet_buf_write(&req_buf
,
646 old_hex
, new_hex
, ref
->name
, 0,
650 packet_buf_write(&req_buf
, "%s %s %s",
651 old_hex
, new_hex
, ref
->name
);
655 if (use_push_options
) {
656 struct string_list_item
*item
;
658 packet_buf_flush(&req_buf
);
659 for_each_string_list_item(item
, args
->push_options
)
660 packet_buf_write(&req_buf
, "%s", item
->string
);
663 if (args
->stateless_rpc
) {
664 if (!args
->dry_run
&& (cmds_sent
|| is_repository_shallow(the_repository
))) {
665 packet_buf_flush(&req_buf
);
666 send_sideband(out
, -1, req_buf
.buf
, req_buf
.len
, LARGE_PACKET_MAX
);
669 write_or_die(out
, req_buf
.buf
, req_buf
.len
);
672 strbuf_release(&req_buf
);
673 strbuf_release(&cap_buf
);
675 if (use_sideband
&& cmds_sent
) {
676 memset(&demux
, 0, sizeof(demux
));
677 demux
.proc
= sideband_demux
;
680 demux
.isolate_sigpipe
= 1;
681 if (start_async(&demux
))
682 die("send-pack: unable to fork off sideband demultiplexer");
686 packet_reader_init(&reader
, in
, NULL
, 0,
687 PACKET_READ_CHOMP_NEWLINE
|
688 PACKET_READ_DIE_ON_ERR_PACKET
);
690 if (need_pack_data
&& cmds_sent
) {
691 if (pack_objects(out
, remote_refs
, extra_have
, &commons
, args
) < 0) {
692 if (args
->stateless_rpc
)
694 if (git_connection_is_socket(conn
))
695 shutdown(fd
[0], SHUT_WR
);
698 * Do not even bother with the return value; we know we
699 * are failing, and just want the error() side effects,
700 * as well as marking refs with their remote status (if
704 receive_status(&reader
, remote_refs
);
708 finish_async(&demux
);
713 if (!args
->stateless_rpc
)
714 /* Closed by pack_objects() via start_command() */
717 if (args
->stateless_rpc
&& cmds_sent
)
720 if (status_report
&& cmds_sent
)
721 ret
= receive_status(&reader
, remote_refs
);
724 if (args
->stateless_rpc
)
727 if (use_sideband
&& cmds_sent
) {
729 if (finish_async(&demux
)) {
730 error("error in sideband demultiplexer");
741 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
742 switch (ref
->status
) {
743 case REF_STATUS_NONE
:
744 case REF_STATUS_UPTODATE
: