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"
20 int option_parse_push_signed(const struct option
*opt
,
21 const char *arg
, int unset
)
24 *(int *)(opt
->value
) = SEND_PACK_PUSH_CERT_NEVER
;
27 switch (git_parse_maybe_bool(arg
)) {
29 *(int *)(opt
->value
) = SEND_PACK_PUSH_CERT_ALWAYS
;
32 *(int *)(opt
->value
) = SEND_PACK_PUSH_CERT_NEVER
;
35 if (!strcasecmp("if-asked", arg
)) {
36 *(int *)(opt
->value
) = SEND_PACK_PUSH_CERT_IF_ASKED
;
39 die("bad %s argument: %s", opt
->long_name
, arg
);
42 static void feed_object(const struct object_id
*oid
, FILE *fh
, int negative
)
45 !has_object_file_with_flags(oid
,
46 OBJECT_INFO_SKIP_FETCH_OBJECT
|
52 fputs(oid_to_hex(oid
), fh
);
57 * Make a pack stream and spit it out into file descriptor fd
59 static int pack_objects(int fd
, struct ref
*refs
, struct oid_array
*advertised
,
60 struct oid_array
*negotiated
,
61 struct send_pack_args
*args
)
64 * The child becomes pack-objects --revs; we feed
65 * the revision parameters to it via its stdin and
66 * let its stdout go back to the other end.
68 struct child_process po
= CHILD_PROCESS_INIT
;
73 strvec_push(&po
.args
, "pack-objects");
74 strvec_push(&po
.args
, "--all-progress-implied");
75 strvec_push(&po
.args
, "--revs");
76 strvec_push(&po
.args
, "--stdout");
77 if (args
->use_thin_pack
)
78 strvec_push(&po
.args
, "--thin");
79 if (args
->use_ofs_delta
)
80 strvec_push(&po
.args
, "--delta-base-offset");
81 if (args
->quiet
|| !args
->progress
)
82 strvec_push(&po
.args
, "-q");
84 strvec_push(&po
.args
, "--progress");
85 if (is_repository_shallow(the_repository
))
86 strvec_push(&po
.args
, "--shallow");
87 if (args
->disable_bitmaps
)
88 strvec_push(&po
.args
, "--no-use-bitmap-index");
90 po
.out
= args
->stateless_rpc
? -1 : fd
;
93 if (start_command(&po
))
94 die_errno("git pack-objects failed");
97 * We feed the pack-objects we just spawned with revision
98 * parameters by writing to the pipe.
100 po_in
= xfdopen(po
.in
, "w");
101 for (i
= 0; i
< advertised
->nr
; i
++)
102 feed_object(&advertised
->oid
[i
], po_in
, 1);
103 for (i
= 0; i
< negotiated
->nr
; i
++)
104 feed_object(&negotiated
->oid
[i
], po_in
, 1);
107 if (!is_null_oid(&refs
->old_oid
))
108 feed_object(&refs
->old_oid
, po_in
, 1);
109 if (!is_null_oid(&refs
->new_oid
))
110 feed_object(&refs
->new_oid
, po_in
, 0);
116 die_errno("error writing to pack-objects");
119 if (args
->stateless_rpc
) {
120 char *buf
= xmalloc(LARGE_PACKET_MAX
);
122 ssize_t n
= xread(po
.out
, buf
, LARGE_PACKET_MAX
);
125 send_sideband(fd
, -1, buf
, n
, LARGE_PACKET_MAX
);
132 rc
= finish_command(&po
);
135 * For a normal non-zero exit, we assume pack-objects wrote
136 * something useful to stderr. For death by signal, though,
137 * we should mention it to the user. The exception is SIGPIPE
138 * (141), because that's a normal occurrence if the remote end
139 * hangs up (and we'll report that by trying to read the unpack
142 if (rc
> 128 && rc
!= 141)
143 error("pack-objects died of signal %d", rc
- 128);
149 static int receive_unpack_status(struct packet_reader
*reader
)
151 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
152 return error(_("unexpected flush packet while reading remote unpack status"));
153 if (!skip_prefix(reader
->line
, "unpack ", &reader
->line
))
154 return error(_("unable to parse remote unpack status: %s"), reader
->line
);
155 if (strcmp(reader
->line
, "ok"))
156 return error(_("remote unpack failed: %s"), reader
->line
);
160 static int receive_status(struct packet_reader
*reader
, struct ref
*refs
)
164 struct ref_push_report
*report
= NULL
;
169 ret
= receive_unpack_status(reader
);
171 struct object_id old_oid
, new_oid
;
175 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
178 p
= strchr(head
, ' ');
180 error("invalid status line from remote: %s", reader
->line
);
186 if (!strcmp(head
, "option")) {
187 const char *key
, *val
;
189 if (!hint
|| !(report
|| new_report
)) {
191 error("'option' without a matching 'ok/ng' directive");
197 CALLOC_ARRAY(hint
->report
, 1);
198 report
= hint
->report
;
200 report
= hint
->report
;
202 report
= report
->next
;
203 CALLOC_ARRAY(report
->next
, 1);
204 report
= report
->next
;
209 p
= strchr(key
, ' ');
213 if (!strcmp(key
, "refname"))
214 report
->ref_name
= xstrdup_or_null(val
);
215 else if (!strcmp(key
, "old-oid") && val
&&
216 !parse_oid_hex(val
, &old_oid
, &val
))
217 report
->old_oid
= oiddup(&old_oid
);
218 else if (!strcmp(key
, "new-oid") && val
&&
219 !parse_oid_hex(val
, &new_oid
, &val
))
220 report
->new_oid
= oiddup(&new_oid
);
221 else if (!strcmp(key
, "forced-update"))
222 report
->forced_update
= 1;
228 if (strcmp(head
, "ok") && strcmp(head
, "ng")) {
229 error("invalid ref status from remote: %s", head
);
234 p
= strchr(refname
, ' ');
237 /* first try searching at our hint, falling back to all refs */
239 hint
= find_ref_by_name(hint
, refname
);
241 hint
= find_ref_by_name(refs
, refname
);
243 warning("remote reported status on unknown ref: %s",
247 if (hint
->status
!= REF_STATUS_EXPECTING_REPORT
&&
248 hint
->status
!= REF_STATUS_OK
&&
249 hint
->status
!= REF_STATUS_REMOTE_REJECT
) {
250 warning("remote reported status on unexpected ref: %s",
254 if (!strcmp(head
, "ng")) {
255 hint
->status
= REF_STATUS_REMOTE_REJECT
;
257 hint
->remote_status
= xstrdup(p
);
259 hint
->remote_status
= "failed";
261 hint
->status
= REF_STATUS_OK
;
262 hint
->remote_status
= xstrdup_or_null(p
);
269 static int sideband_demux(int UNUSED(in
), int out
, void *data
)
272 if (async_with_fork())
274 ret
= recv_sideband("send-pack", fd
[0], out
);
279 static int advertise_shallow_grafts_cb(const struct commit_graft
*graft
, void *cb
)
281 struct strbuf
*sb
= cb
;
282 if (graft
->nr_parent
== -1)
283 packet_buf_write(sb
, "shallow %s\n", oid_to_hex(&graft
->oid
));
287 static void advertise_shallow_grafts_buf(struct strbuf
*sb
)
289 if (!is_repository_shallow(the_repository
))
291 for_each_commit_graft(advertise_shallow_grafts_cb
, sb
);
294 #define CHECK_REF_NO_PUSH -1
295 #define CHECK_REF_STATUS_REJECTED -2
296 #define CHECK_REF_UPTODATE -3
297 static int check_to_send_update(const struct ref
*ref
, const struct send_pack_args
*args
)
299 if (!ref
->peer_ref
&& !args
->send_mirror
)
300 return CHECK_REF_NO_PUSH
;
302 /* Check for statuses set by set_ref_status_for_push() */
303 switch (ref
->status
) {
304 case REF_STATUS_REJECT_NONFASTFORWARD
:
305 case REF_STATUS_REJECT_ALREADY_EXISTS
:
306 case REF_STATUS_REJECT_FETCH_FIRST
:
307 case REF_STATUS_REJECT_NEEDS_FORCE
:
308 case REF_STATUS_REJECT_STALE
:
309 case REF_STATUS_REJECT_REMOTE_UPDATED
:
310 case REF_STATUS_REJECT_NODELETE
:
311 return CHECK_REF_STATUS_REJECTED
;
312 case REF_STATUS_UPTODATE
:
313 return CHECK_REF_UPTODATE
;
316 case REF_STATUS_EXPECTING_REPORT
:
317 /* already passed checks on the local side */
319 /* of course this is OK */
325 * the beginning of the next line, or the end of buffer.
327 * NEEDSWORK: perhaps move this to git-compat-util.h or somewhere and
328 * convert many similar uses found by "git grep -A4 memchr".
330 static const char *next_line(const char *line
, size_t len
)
332 const char *nl
= memchr(line
, '\n', len
);
334 return line
+ len
; /* incomplete line */
338 static int generate_push_cert(struct strbuf
*req_buf
,
339 const struct ref
*remote_refs
,
340 struct send_pack_args
*args
,
341 const char *cap_string
,
342 const char *push_cert_nonce
)
344 const struct ref
*ref
;
345 struct string_list_item
*item
;
346 char *signing_key_id
= xstrdup(get_signing_key_id());
348 struct strbuf cert
= STRBUF_INIT
;
351 strbuf_addstr(&cert
, "certificate version 0.1\n");
352 strbuf_addf(&cert
, "pusher %s ", signing_key_id
);
354 strbuf_addch(&cert
, '\n');
355 if (args
->url
&& *args
->url
) {
356 char *anon_url
= transport_anonymize_url(args
->url
);
357 strbuf_addf(&cert
, "pushee %s\n", anon_url
);
360 if (push_cert_nonce
[0])
361 strbuf_addf(&cert
, "nonce %s\n", push_cert_nonce
);
362 if (args
->push_options
)
363 for_each_string_list_item(item
, args
->push_options
)
364 strbuf_addf(&cert
, "push-option %s\n", item
->string
);
365 strbuf_addstr(&cert
, "\n");
367 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
368 if (check_to_send_update(ref
, args
) < 0)
371 strbuf_addf(&cert
, "%s %s %s\n",
372 oid_to_hex(&ref
->old_oid
),
373 oid_to_hex(&ref
->new_oid
),
379 if (sign_buffer(&cert
, &cert
, get_signing_key()))
380 die(_("failed to sign the push certificate"));
382 packet_buf_write(req_buf
, "push-cert%c%s", 0, cap_string
);
383 for (cp
= cert
.buf
; cp
< cert
.buf
+ cert
.len
; cp
= np
) {
384 np
= next_line(cp
, cert
.buf
+ cert
.len
- cp
);
385 packet_buf_write(req_buf
,
386 "%.*s", (int)(np
- cp
), cp
);
388 packet_buf_write(req_buf
, "push-cert-end\n");
391 free(signing_key_id
);
392 strbuf_release(&cert
);
396 #define NONCE_LEN_LIMIT 256
398 static void reject_invalid_nonce(const char *nonce
, int len
)
402 if (NONCE_LEN_LIMIT
<= len
)
403 die("the receiving end asked to sign an invalid nonce <%.*s>",
406 for (i
= 0; i
< len
; i
++) {
407 int ch
= nonce
[i
] & 0xFF;
409 ch
== '-' || ch
== '.' ||
410 ch
== '/' || ch
== '+' ||
411 ch
== '=' || ch
== '_')
413 die("the receiving end asked to sign an invalid nonce <%.*s>",
418 static void get_commons_through_negotiation(const char *url
,
419 const struct ref
*remote_refs
,
420 struct oid_array
*commons
)
422 struct child_process child
= CHILD_PROCESS_INIT
;
423 const struct ref
*ref
;
424 int len
= the_hash_algo
->hexsz
+ 1; /* hash + NL */
429 strvec_pushl(&child
.args
, "fetch", "--negotiate-only", NULL
);
430 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
431 if (!is_null_oid(&ref
->new_oid
))
432 strvec_pushf(&child
.args
, "--negotiation-tip=%s", oid_to_hex(&ref
->new_oid
));
434 strvec_push(&child
.args
, url
);
436 if (start_command(&child
))
437 die(_("send-pack: unable to fork off fetch subprocess"));
440 char hex_hash
[GIT_MAX_HEXSZ
+ 1];
441 int read_len
= read_in_full(child
.out
, hex_hash
, len
);
442 struct object_id oid
;
448 die("invalid length read %d", read_len
);
449 if (parse_oid_hex(hex_hash
, &oid
, &end
) || *end
!= '\n')
451 oid_array_append(commons
, &oid
);
454 if (finish_command(&child
)) {
456 * The information that push negotiation provides is useful but
459 warning(_("push negotiation failed; proceeding anyway with push"));
463 int send_pack(struct send_pack_args
*args
,
464 int fd
[], struct child_process
*conn
,
465 struct ref
*remote_refs
,
466 struct oid_array
*extra_have
)
468 struct oid_array commons
= OID_ARRAY_INIT
;
471 struct strbuf req_buf
= STRBUF_INIT
;
472 struct strbuf cap_buf
= STRBUF_INIT
;
474 int need_pack_data
= 0;
475 int allow_deleting_refs
= 0;
476 int status_report
= 0;
477 int use_sideband
= 0;
478 int quiet_supported
= 0;
479 int agent_supported
= 0;
480 int advertise_sid
= 0;
481 int push_negotiate
= 0;
483 int atomic_supported
= 0;
484 int use_push_options
= 0;
485 int push_options_supported
= 0;
486 int object_format_supported
= 0;
487 unsigned cmds_sent
= 0;
490 const char *push_cert_nonce
= NULL
;
491 struct packet_reader reader
;
495 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
496 "Perhaps you should specify a branch.\n");
500 git_config_get_bool("push.negotiate", &push_negotiate
);
502 get_commons_through_negotiation(args
->url
, remote_refs
, &commons
);
504 if (!git_config_get_bool("push.usebitmaps", &use_bitmaps
))
505 args
->disable_bitmaps
= !use_bitmaps
;
507 git_config_get_bool("transfer.advertisesid", &advertise_sid
);
509 /* Does the other end support the reporting? */
510 if (server_supports("report-status-v2"))
512 else if (server_supports("report-status"))
514 if (server_supports("delete-refs"))
515 allow_deleting_refs
= 1;
516 if (server_supports("ofs-delta"))
517 args
->use_ofs_delta
= 1;
518 if (server_supports("side-band-64k"))
520 if (server_supports("quiet"))
522 if (server_supports("agent"))
524 if (!server_supports("session-id"))
526 if (server_supports("no-thin"))
527 args
->use_thin_pack
= 0;
528 if (server_supports("atomic"))
529 atomic_supported
= 1;
530 if (server_supports("push-options"))
531 push_options_supported
= 1;
533 if (!server_supports_hash(the_hash_algo
->name
, &object_format_supported
))
534 die(_("the receiving end does not support this repository's hash algorithm"));
536 if (args
->push_cert
!= SEND_PACK_PUSH_CERT_NEVER
) {
538 push_cert_nonce
= server_feature_value("push-cert", &len
);
539 if (push_cert_nonce
) {
540 reject_invalid_nonce(push_cert_nonce
, len
);
541 push_cert_nonce
= xmemdupz(push_cert_nonce
, len
);
542 } else if (args
->push_cert
== SEND_PACK_PUSH_CERT_ALWAYS
) {
543 die(_("the receiving end does not support --signed push"));
544 } else if (args
->push_cert
== SEND_PACK_PUSH_CERT_IF_ASKED
) {
545 warning(_("not sending a push certificate since the"
546 " receiving end does not support --signed"
551 if (args
->atomic
&& !atomic_supported
)
552 die(_("the receiving end does not support --atomic push"));
554 use_atomic
= atomic_supported
&& args
->atomic
;
556 if (args
->push_options
&& !push_options_supported
)
557 die(_("the receiving end does not support push options"));
559 use_push_options
= push_options_supported
&& args
->push_options
;
561 if (status_report
== 1)
562 strbuf_addstr(&cap_buf
, " report-status");
563 else if (status_report
== 2)
564 strbuf_addstr(&cap_buf
, " report-status-v2");
566 strbuf_addstr(&cap_buf
, " side-band-64k");
567 if (quiet_supported
&& (args
->quiet
|| !args
->progress
))
568 strbuf_addstr(&cap_buf
, " quiet");
570 strbuf_addstr(&cap_buf
, " atomic");
571 if (use_push_options
)
572 strbuf_addstr(&cap_buf
, " push-options");
573 if (object_format_supported
)
574 strbuf_addf(&cap_buf
, " object-format=%s", the_hash_algo
->name
);
576 strbuf_addf(&cap_buf
, " agent=%s", git_user_agent_sanitized());
578 strbuf_addf(&cap_buf
, " session-id=%s", trace2_session_id());
581 * NEEDSWORK: why does delete-refs have to be so specific to
582 * send-pack machinery that set_ref_status_for_push() cannot
583 * set this bit for us???
585 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
586 if (ref
->deletion
&& !allow_deleting_refs
)
587 ref
->status
= REF_STATUS_REJECT_NODELETE
;
590 * Clear the status for each ref and see if we need to send
593 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
594 switch (check_to_send_update(ref
, args
)) {
595 case 0: /* no error */
597 case CHECK_REF_STATUS_REJECTED
:
599 * When we know the server would reject a ref update if
600 * we were to send it and we're trying to send the refs
601 * atomically, abort the whole operation.
604 strbuf_release(&req_buf
);
605 strbuf_release(&cap_buf
);
606 reject_atomic_push(remote_refs
, args
->send_mirror
);
607 error("atomic push failed for ref %s. status: %d\n",
608 ref
->name
, ref
->status
);
609 return args
->porcelain
? 0 : -1;
611 /* else fallthrough */
618 if (args
->dry_run
|| !status_report
)
619 ref
->status
= REF_STATUS_OK
;
621 ref
->status
= REF_STATUS_EXPECTING_REPORT
;
625 advertise_shallow_grafts_buf(&req_buf
);
628 * Finally, tell the other end!
630 if (!args
->dry_run
&& push_cert_nonce
)
631 cmds_sent
= generate_push_cert(&req_buf
, remote_refs
, args
,
632 cap_buf
.buf
, push_cert_nonce
);
633 else if (!args
->dry_run
)
634 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
635 char *old_hex
, *new_hex
;
637 if (check_to_send_update(ref
, args
) < 0)
640 old_hex
= oid_to_hex(&ref
->old_oid
);
641 new_hex
= oid_to_hex(&ref
->new_oid
);
643 packet_buf_write(&req_buf
,
645 old_hex
, new_hex
, ref
->name
, 0,
649 packet_buf_write(&req_buf
, "%s %s %s",
650 old_hex
, new_hex
, ref
->name
);
654 if (use_push_options
) {
655 struct string_list_item
*item
;
657 packet_buf_flush(&req_buf
);
658 for_each_string_list_item(item
, args
->push_options
)
659 packet_buf_write(&req_buf
, "%s", item
->string
);
662 if (args
->stateless_rpc
) {
663 if (!args
->dry_run
&& (cmds_sent
|| is_repository_shallow(the_repository
))) {
664 packet_buf_flush(&req_buf
);
665 send_sideband(out
, -1, req_buf
.buf
, req_buf
.len
, LARGE_PACKET_MAX
);
668 write_or_die(out
, req_buf
.buf
, req_buf
.len
);
671 strbuf_release(&req_buf
);
672 strbuf_release(&cap_buf
);
674 if (use_sideband
&& cmds_sent
) {
675 memset(&demux
, 0, sizeof(demux
));
676 demux
.proc
= sideband_demux
;
679 demux
.isolate_sigpipe
= 1;
680 if (start_async(&demux
))
681 die("send-pack: unable to fork off sideband demultiplexer");
685 packet_reader_init(&reader
, in
, NULL
, 0,
686 PACKET_READ_CHOMP_NEWLINE
|
687 PACKET_READ_DIE_ON_ERR_PACKET
);
689 if (need_pack_data
&& cmds_sent
) {
690 if (pack_objects(out
, remote_refs
, extra_have
, &commons
, args
) < 0) {
691 if (args
->stateless_rpc
)
693 if (git_connection_is_socket(conn
))
694 shutdown(fd
[0], SHUT_WR
);
697 * Do not even bother with the return value; we know we
698 * are failing, and just want the error() side effects,
699 * as well as marking refs with their remote status (if
703 receive_status(&reader
, remote_refs
);
707 finish_async(&demux
);
712 if (!args
->stateless_rpc
)
713 /* Closed by pack_objects() via start_command() */
716 if (args
->stateless_rpc
&& cmds_sent
)
719 if (status_report
&& cmds_sent
)
720 ret
= receive_status(&reader
, remote_refs
);
723 if (args
->stateless_rpc
)
726 if (use_sideband
&& cmds_sent
) {
728 if (finish_async(&demux
)) {
729 error("error in sideband demultiplexer");
740 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
741 switch (ref
->status
) {
742 case REF_STATUS_NONE
:
743 case REF_STATUS_UPTODATE
: