1 #include "git-compat-util.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(struct repository
*r
,
46 const struct object_id
*oid
, FILE *fh
, int negative
)
48 if (negative
&& !odb_has_object(r
->objects
, oid
, 0))
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(struct repository
*r
,
61 int fd
, struct ref
*refs
, struct oid_array
*advertised
,
62 struct oid_array
*negotiated
,
63 struct send_pack_args
*args
)
66 * The child becomes pack-objects --revs; we feed
67 * the revision parameters to it via its stdin and
68 * let its stdout go back to the other end.
70 struct child_process po
= CHILD_PROCESS_INIT
;
74 trace2_region_enter("send_pack", "pack_objects", r
);
75 strvec_push(&po
.args
, "pack-objects");
76 strvec_push(&po
.args
, "--all-progress-implied");
77 strvec_push(&po
.args
, "--revs");
78 strvec_push(&po
.args
, "--stdout");
79 if (args
->use_thin_pack
)
80 strvec_push(&po
.args
, "--thin");
81 if (args
->use_ofs_delta
)
82 strvec_push(&po
.args
, "--delta-base-offset");
83 if (args
->quiet
|| !args
->progress
)
84 strvec_push(&po
.args
, "-q");
86 strvec_push(&po
.args
, "--progress");
87 if (is_repository_shallow(r
))
88 strvec_push(&po
.args
, "--shallow");
89 if (args
->disable_bitmaps
)
90 strvec_push(&po
.args
, "--no-use-bitmap-index");
92 po
.out
= args
->stateless_rpc
? -1 : fd
;
95 if (start_command(&po
))
96 die_errno("git pack-objects failed");
99 * We feed the pack-objects we just spawned with revision
100 * parameters by writing to the pipe.
102 po_in
= xfdopen(po
.in
, "w");
103 for (size_t i
= 0; i
< advertised
->nr
; i
++)
104 feed_object(r
, &advertised
->oid
[i
], po_in
, 1);
105 for (size_t i
= 0; i
< negotiated
->nr
; i
++)
106 feed_object(r
, &negotiated
->oid
[i
], po_in
, 1);
109 if (!is_null_oid(&refs
->old_oid
))
110 feed_object(r
, &refs
->old_oid
, po_in
, 1);
111 if (!is_null_oid(&refs
->new_oid
))
112 feed_object(r
, &refs
->new_oid
, po_in
, 0);
118 die_errno("error writing to pack-objects");
121 if (args
->stateless_rpc
) {
122 char *buf
= xmalloc(LARGE_PACKET_MAX
);
124 ssize_t n
= xread(po
.out
, buf
, LARGE_PACKET_MAX
);
127 send_sideband(fd
, -1, buf
, n
, LARGE_PACKET_MAX
);
134 rc
= finish_command(&po
);
137 * For a normal non-zero exit, we assume pack-objects wrote
138 * something useful to stderr. For death by signal, though,
139 * we should mention it to the user. The exception is SIGPIPE
140 * (141), because that's a normal occurrence if the remote end
141 * hangs up (and we'll report that by trying to read the unpack
144 if (rc
> 128 && rc
!= 141)
145 error("pack-objects died of signal %d", rc
- 128);
146 trace2_region_leave("send_pack", "pack_objects", r
);
149 trace2_region_leave("send_pack", "pack_objects", r
);
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 repository
*r
,
165 struct packet_reader
*reader
, struct ref
*refs
)
169 struct ref_push_report
*report
= NULL
;
173 trace2_region_enter("send_pack", "receive_status", r
);
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_algop(val
, &old_oid
, &val
, r
->hash_algo
))
223 report
->old_oid
= oiddup(&old_oid
);
224 else if (!strcmp(key
, "new-oid") && val
&&
225 !parse_oid_hex_algop(val
, &new_oid
, &val
, r
->hash_algo
))
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",
262 * Clients sending duplicate refs can cause the same value
263 * to be overridden, causing a memory leak.
265 free(hint
->remote_status
);
267 if (!strcmp(head
, "ng")) {
268 hint
->status
= REF_STATUS_REMOTE_REJECT
;
270 hint
->remote_status
= xstrdup(p
);
272 hint
->remote_status
= xstrdup("failed");
274 hint
->status
= REF_STATUS_OK
;
275 hint
->remote_status
= xstrdup_or_null(p
);
279 trace2_region_leave("send_pack", "receive_status", r
);
283 static int sideband_demux(int in UNUSED
, int out
, void *data
)
286 if (async_with_fork())
288 ret
= recv_sideband("send-pack", fd
[0], out
);
293 static int advertise_shallow_grafts_cb(const struct commit_graft
*graft
, void *cb
)
295 struct strbuf
*sb
= cb
;
296 if (graft
->nr_parent
== -1)
297 packet_buf_write(sb
, "shallow %s\n", oid_to_hex(&graft
->oid
));
301 static void advertise_shallow_grafts_buf(struct repository
*r
, struct strbuf
*sb
)
303 if (!is_repository_shallow(r
))
305 for_each_commit_graft(advertise_shallow_grafts_cb
, sb
);
308 #define CHECK_REF_NO_PUSH -1
309 #define CHECK_REF_STATUS_REJECTED -2
310 #define CHECK_REF_UPTODATE -3
311 static int check_to_send_update(const struct ref
*ref
, const struct send_pack_args
*args
)
313 if (!ref
->peer_ref
&& !args
->send_mirror
)
314 return CHECK_REF_NO_PUSH
;
316 /* Check for statuses set by set_ref_status_for_push() */
317 switch (ref
->status
) {
318 case REF_STATUS_REJECT_NONFASTFORWARD
:
319 case REF_STATUS_REJECT_ALREADY_EXISTS
:
320 case REF_STATUS_REJECT_FETCH_FIRST
:
321 case REF_STATUS_REJECT_NEEDS_FORCE
:
322 case REF_STATUS_REJECT_STALE
:
323 case REF_STATUS_REJECT_REMOTE_UPDATED
:
324 case REF_STATUS_REJECT_NODELETE
:
325 return CHECK_REF_STATUS_REJECTED
;
326 case REF_STATUS_UPTODATE
:
327 return CHECK_REF_UPTODATE
;
330 case REF_STATUS_EXPECTING_REPORT
:
331 /* already passed checks on the local side */
333 /* of course this is OK */
339 * the beginning of the next line, or the end of buffer.
341 * NEEDSWORK: perhaps move this to git-compat-util.h or somewhere and
342 * convert many similar uses found by "git grep -A4 memchr".
344 static const char *next_line(const char *line
, size_t len
)
346 const char *nl
= memchr(line
, '\n', len
);
348 return line
+ len
; /* incomplete line */
352 static int generate_push_cert(struct strbuf
*req_buf
,
353 const struct ref
*remote_refs
,
354 struct send_pack_args
*args
,
355 const char *cap_string
,
356 const char *push_cert_nonce
)
358 const struct ref
*ref
;
359 struct string_list_item
*item
;
360 char *signing_key_id
= get_signing_key_id();
361 char *signing_key
= get_signing_key();
363 struct strbuf cert
= STRBUF_INIT
;
366 strbuf_addstr(&cert
, "certificate version 0.1\n");
367 strbuf_addf(&cert
, "pusher %s ", signing_key_id
);
369 strbuf_addch(&cert
, '\n');
370 if (args
->url
&& *args
->url
) {
371 char *anon_url
= transport_anonymize_url(args
->url
);
372 strbuf_addf(&cert
, "pushee %s\n", anon_url
);
375 if (push_cert_nonce
[0])
376 strbuf_addf(&cert
, "nonce %s\n", push_cert_nonce
);
377 if (args
->push_options
)
378 for_each_string_list_item(item
, args
->push_options
)
379 strbuf_addf(&cert
, "push-option %s\n", item
->string
);
380 strbuf_addstr(&cert
, "\n");
382 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
383 if (check_to_send_update(ref
, args
) < 0)
386 strbuf_addf(&cert
, "%s %s %s\n",
387 oid_to_hex(&ref
->old_oid
),
388 oid_to_hex(&ref
->new_oid
),
394 if (sign_buffer(&cert
, &cert
, signing_key
))
395 die(_("failed to sign the push certificate"));
397 packet_buf_write(req_buf
, "push-cert%c%s", 0, cap_string
);
398 for (cp
= cert
.buf
; cp
< cert
.buf
+ cert
.len
; cp
= np
) {
399 np
= next_line(cp
, cert
.buf
+ cert
.len
- cp
);
400 packet_buf_write(req_buf
,
401 "%.*s", (int)(np
- cp
), cp
);
403 packet_buf_write(req_buf
, "push-cert-end\n");
406 free(signing_key_id
);
408 strbuf_release(&cert
);
412 #define NONCE_LEN_LIMIT 256
414 static void reject_invalid_nonce(const char *nonce
, int len
)
418 if (NONCE_LEN_LIMIT
<= len
)
419 die("the receiving end asked to sign an invalid nonce <%.*s>",
422 for (i
= 0; i
< len
; i
++) {
423 int ch
= nonce
[i
] & 0xFF;
425 ch
== '-' || ch
== '.' ||
426 ch
== '/' || ch
== '+' ||
427 ch
== '=' || ch
== '_')
429 die("the receiving end asked to sign an invalid nonce <%.*s>",
434 static void get_commons_through_negotiation(struct repository
*r
,
436 const struct ref
*remote_refs
,
437 struct oid_array
*commons
)
439 struct child_process child
= CHILD_PROCESS_INIT
;
440 const struct ref
*ref
;
441 int len
= r
->hash_algo
->hexsz
+ 1; /* hash + NL */
442 int nr_negotiation_tip
= 0;
447 strvec_pushl(&child
.args
, "fetch", "--negotiate-only", NULL
);
448 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
449 if (!is_null_oid(&ref
->new_oid
)) {
450 strvec_pushf(&child
.args
, "--negotiation-tip=%s",
451 oid_to_hex(&ref
->new_oid
));
452 nr_negotiation_tip
++;
455 strvec_push(&child
.args
, url
);
457 if (!nr_negotiation_tip
) {
458 child_process_clear(&child
);
462 if (start_command(&child
))
463 die(_("send-pack: unable to fork off fetch subprocess"));
466 char hex_hash
[GIT_MAX_HEXSZ
+ 1];
467 int read_len
= read_in_full(child
.out
, hex_hash
, len
);
468 struct object_id oid
;
474 die("invalid length read %d", read_len
);
475 if (parse_oid_hex_algop(hex_hash
, &oid
, &end
, r
->hash_algo
) || *end
!= '\n')
477 oid_array_append(commons
, &oid
);
480 if (finish_command(&child
)) {
482 * The information that push negotiation provides is useful but
485 warning(_("push negotiation failed; proceeding anyway with push"));
489 int send_pack(struct repository
*r
,
490 struct send_pack_args
*args
,
491 int fd
[], struct child_process
*conn
,
492 struct ref
*remote_refs
,
493 struct oid_array
*extra_have
)
495 struct oid_array commons
= OID_ARRAY_INIT
;
498 struct strbuf req_buf
= STRBUF_INIT
;
499 struct strbuf cap_buf
= STRBUF_INIT
;
501 int need_pack_data
= 0;
502 int allow_deleting_refs
= 0;
503 int status_report
= 0;
504 int use_sideband
= 0;
505 int quiet_supported
= 0;
506 int agent_supported
= 0;
507 int advertise_sid
= 0;
508 int push_negotiate
= 0;
510 int atomic_supported
= 0;
511 int use_push_options
= 0;
512 int push_options_supported
= 0;
513 int object_format_supported
= 0;
514 unsigned cmds_sent
= 0;
517 char *push_cert_nonce
= NULL
;
518 struct packet_reader reader
;
522 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
523 "Perhaps you should specify a branch.\n");
528 repo_config_get_bool(r
, "push.negotiate", &push_negotiate
);
529 if (push_negotiate
) {
530 trace2_region_enter("send_pack", "push_negotiate", r
);
531 get_commons_through_negotiation(r
, args
->url
, remote_refs
, &commons
);
532 trace2_region_leave("send_pack", "push_negotiate", r
);
535 if (!repo_config_get_bool(r
, "push.usebitmaps", &use_bitmaps
))
536 args
->disable_bitmaps
= !use_bitmaps
;
538 repo_config_get_bool(r
, "transfer.advertisesid", &advertise_sid
);
540 /* Does the other end support the reporting? */
541 if (server_supports("report-status-v2"))
543 else if (server_supports("report-status"))
545 if (server_supports("delete-refs"))
546 allow_deleting_refs
= 1;
547 if (server_supports("ofs-delta"))
548 args
->use_ofs_delta
= 1;
549 if (server_supports("side-band-64k"))
551 if (server_supports("quiet"))
553 if (server_supports("agent"))
555 if (!server_supports("session-id"))
557 if (server_supports("no-thin"))
558 args
->use_thin_pack
= 0;
559 if (server_supports("atomic"))
560 atomic_supported
= 1;
561 if (server_supports("push-options"))
562 push_options_supported
= 1;
564 if (!server_supports_hash(r
->hash_algo
->name
, &object_format_supported
))
565 die(_("the receiving end does not support this repository's hash algorithm"));
567 if (args
->push_cert
!= SEND_PACK_PUSH_CERT_NEVER
) {
569 const char *nonce
= server_feature_value("push-cert", &len
);
572 reject_invalid_nonce(nonce
, len
);
573 push_cert_nonce
= xmemdupz(nonce
, len
);
574 } else if (args
->push_cert
== SEND_PACK_PUSH_CERT_ALWAYS
) {
575 die(_("the receiving end does not support --signed push"));
576 } else if (args
->push_cert
== SEND_PACK_PUSH_CERT_IF_ASKED
) {
577 warning(_("not sending a push certificate since the"
578 " receiving end does not support --signed"
583 if (args
->atomic
&& !atomic_supported
)
584 die(_("the receiving end does not support --atomic push"));
586 use_atomic
= atomic_supported
&& args
->atomic
;
588 if (args
->push_options
&& !push_options_supported
)
589 die(_("the receiving end does not support push options"));
591 use_push_options
= push_options_supported
&& args
->push_options
;
593 if (status_report
== 1)
594 strbuf_addstr(&cap_buf
, " report-status");
595 else if (status_report
== 2)
596 strbuf_addstr(&cap_buf
, " report-status-v2");
598 strbuf_addstr(&cap_buf
, " side-band-64k");
599 if (quiet_supported
&& (args
->quiet
|| !args
->progress
))
600 strbuf_addstr(&cap_buf
, " quiet");
602 strbuf_addstr(&cap_buf
, " atomic");
603 if (use_push_options
)
604 strbuf_addstr(&cap_buf
, " push-options");
605 if (object_format_supported
)
606 strbuf_addf(&cap_buf
, " object-format=%s", r
->hash_algo
->name
);
608 strbuf_addf(&cap_buf
, " agent=%s", git_user_agent_sanitized());
610 strbuf_addf(&cap_buf
, " session-id=%s", trace2_session_id());
613 * NEEDSWORK: why does delete-refs have to be so specific to
614 * send-pack machinery that set_ref_status_for_push() cannot
615 * set this bit for us???
617 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
618 if (ref
->deletion
&& !allow_deleting_refs
)
619 ref
->status
= REF_STATUS_REJECT_NODELETE
;
622 * Clear the status for each ref and see if we need to send
625 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
626 switch (check_to_send_update(ref
, args
)) {
627 case 0: /* no error */
629 case CHECK_REF_STATUS_REJECTED
:
631 * When we know the server would reject a ref update if
632 * we were to send it and we're trying to send the refs
633 * atomically, abort the whole operation.
636 reject_atomic_push(remote_refs
, args
->send_mirror
);
637 error("atomic push failed for ref %s. status: %d",
638 ref
->name
, ref
->status
);
639 ret
= ERROR_SEND_PACK_BAD_REF_STATUS
;
643 /* else fallthrough */
650 if (args
->dry_run
|| !status_report
)
651 ref
->status
= REF_STATUS_OK
;
653 ref
->status
= REF_STATUS_EXPECTING_REPORT
;
657 advertise_shallow_grafts_buf(r
, &req_buf
);
660 * Finally, tell the other end!
662 if (!args
->dry_run
&& push_cert_nonce
) {
663 cmds_sent
= generate_push_cert(&req_buf
, remote_refs
, args
,
664 cap_buf
.buf
, push_cert_nonce
);
665 trace2_printf("Generated push certificate");
666 } else if (!args
->dry_run
) {
667 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
668 char *old_hex
, *new_hex
;
670 if (check_to_send_update(ref
, args
) < 0)
673 old_hex
= oid_to_hex(&ref
->old_oid
);
674 new_hex
= oid_to_hex(&ref
->new_oid
);
676 packet_buf_write(&req_buf
,
678 old_hex
, new_hex
, ref
->name
, 0,
682 packet_buf_write(&req_buf
, "%s %s %s",
683 old_hex
, new_hex
, ref
->name
);
688 if (use_push_options
) {
689 struct string_list_item
*item
;
691 packet_buf_flush(&req_buf
);
692 for_each_string_list_item(item
, args
->push_options
)
693 packet_buf_write(&req_buf
, "%s", item
->string
);
696 if (args
->stateless_rpc
) {
697 if (!args
->dry_run
&& (cmds_sent
|| is_repository_shallow(r
))) {
698 packet_buf_flush(&req_buf
);
699 send_sideband(out
, -1, req_buf
.buf
, req_buf
.len
, LARGE_PACKET_MAX
);
702 write_or_die(out
, req_buf
.buf
, req_buf
.len
);
706 if (use_sideband
&& cmds_sent
) {
707 memset(&demux
, 0, sizeof(demux
));
708 demux
.proc
= sideband_demux
;
711 demux
.isolate_sigpipe
= 1;
712 if (start_async(&demux
))
713 die("send-pack: unable to fork off sideband demultiplexer");
717 packet_reader_init(&reader
, in
, NULL
, 0,
718 PACKET_READ_CHOMP_NEWLINE
|
719 PACKET_READ_DIE_ON_ERR_PACKET
);
721 if (need_pack_data
&& cmds_sent
) {
722 if (pack_objects(r
, out
, remote_refs
, extra_have
, &commons
, args
) < 0) {
723 if (args
->stateless_rpc
)
725 if (git_connection_is_socket(conn
))
726 shutdown(fd
[0], SHUT_WR
);
729 * Do not even bother with the return value; we know we
730 * are failing, and just want the error() side effects,
731 * as well as marking refs with their remote status (if
735 receive_status(r
, &reader
, remote_refs
);
739 finish_async(&demux
);
746 if (!args
->stateless_rpc
)
747 /* Closed by pack_objects() via start_command() */
750 if (args
->stateless_rpc
&& cmds_sent
)
753 if (status_report
&& cmds_sent
)
754 ret
= receive_status(r
, &reader
, remote_refs
);
757 if (args
->stateless_rpc
)
760 if (use_sideband
&& cmds_sent
) {
762 if (finish_async(&demux
)) {
763 error("error in sideband demultiplexer");
771 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
772 switch (ref
->status
) {
773 case REF_STATUS_NONE
:
774 case REF_STATUS_UPTODATE
:
778 ret
= ERROR_SEND_PACK_BAD_REF_STATUS
;
786 oid_array_clear(&commons
);
787 strbuf_release(&req_buf
);
788 strbuf_release(&cap_buf
);
789 free(push_cert_nonce
);