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"
17 #include "oid-array.h"
18 #include "gpg-interface.h"
22 int option_parse_push_signed(const struct option
*opt
,
23 const char *arg
, int unset
)
26 *(int *)(opt
->value
) = SEND_PACK_PUSH_CERT_NEVER
;
29 switch (git_parse_maybe_bool(arg
)) {
31 *(int *)(opt
->value
) = SEND_PACK_PUSH_CERT_ALWAYS
;
34 *(int *)(opt
->value
) = SEND_PACK_PUSH_CERT_NEVER
;
37 if (!strcasecmp("if-asked", arg
)) {
38 *(int *)(opt
->value
) = SEND_PACK_PUSH_CERT_IF_ASKED
;
41 die("bad %s argument: %s", opt
->long_name
, arg
);
44 static void feed_object(const struct object_id
*oid
, FILE *fh
, int negative
)
47 !has_object_file_with_flags(oid
,
48 OBJECT_INFO_SKIP_FETCH_OBJECT
|
54 fputs(oid_to_hex(oid
), fh
);
59 * Make a pack stream and spit it out into file descriptor fd
61 static int pack_objects(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
;
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(the_repository
))
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 (i
= 0; i
< advertised
->nr
; i
++)
104 feed_object(&advertised
->oid
[i
], po_in
, 1);
105 for (i
= 0; i
< negotiated
->nr
; i
++)
106 feed_object(&negotiated
->oid
[i
], po_in
, 1);
109 if (!is_null_oid(&refs
->old_oid
))
110 feed_object(&refs
->old_oid
, po_in
, 1);
111 if (!is_null_oid(&refs
->new_oid
))
112 feed_object(&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);
151 static int receive_unpack_status(struct packet_reader
*reader
)
153 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
154 return error(_("unexpected flush packet while reading remote unpack status"));
155 if (!skip_prefix(reader
->line
, "unpack ", &reader
->line
))
156 return error(_("unable to parse remote unpack status: %s"), reader
->line
);
157 if (strcmp(reader
->line
, "ok"))
158 return error(_("remote unpack failed: %s"), reader
->line
);
162 static int receive_status(struct packet_reader
*reader
, struct ref
*refs
)
166 struct ref_push_report
*report
= NULL
;
171 ret
= receive_unpack_status(reader
);
173 struct object_id old_oid
, new_oid
;
177 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
180 p
= strchr(head
, ' ');
182 error("invalid status line from remote: %s", reader
->line
);
188 if (!strcmp(head
, "option")) {
189 const char *key
, *val
;
191 if (!hint
|| !(report
|| new_report
)) {
193 error("'option' without a matching 'ok/ng' directive");
199 CALLOC_ARRAY(hint
->report
, 1);
200 report
= hint
->report
;
202 report
= hint
->report
;
204 report
= report
->next
;
205 CALLOC_ARRAY(report
->next
, 1);
206 report
= report
->next
;
211 p
= strchr(key
, ' ');
215 if (!strcmp(key
, "refname"))
216 report
->ref_name
= xstrdup_or_null(val
);
217 else if (!strcmp(key
, "old-oid") && val
&&
218 !parse_oid_hex(val
, &old_oid
, &val
))
219 report
->old_oid
= oiddup(&old_oid
);
220 else if (!strcmp(key
, "new-oid") && val
&&
221 !parse_oid_hex(val
, &new_oid
, &val
))
222 report
->new_oid
= oiddup(&new_oid
);
223 else if (!strcmp(key
, "forced-update"))
224 report
->forced_update
= 1;
230 if (strcmp(head
, "ok") && strcmp(head
, "ng")) {
231 error("invalid ref status from remote: %s", head
);
236 p
= strchr(refname
, ' ');
239 /* first try searching at our hint, falling back to all refs */
241 hint
= find_ref_by_name(hint
, refname
);
243 hint
= find_ref_by_name(refs
, refname
);
245 warning("remote reported status on unknown ref: %s",
249 if (hint
->status
!= REF_STATUS_EXPECTING_REPORT
&&
250 hint
->status
!= REF_STATUS_OK
&&
251 hint
->status
!= REF_STATUS_REMOTE_REJECT
) {
252 warning("remote reported status on unexpected ref: %s",
256 if (!strcmp(head
, "ng")) {
257 hint
->status
= REF_STATUS_REMOTE_REJECT
;
259 hint
->remote_status
= xstrdup(p
);
261 hint
->remote_status
= "failed";
263 hint
->status
= REF_STATUS_OK
;
264 hint
->remote_status
= xstrdup_or_null(p
);
271 static int sideband_demux(int in UNUSED
, int out
, void *data
)
274 if (async_with_fork())
276 ret
= recv_sideband("send-pack", fd
[0], out
);
281 static int advertise_shallow_grafts_cb(const struct commit_graft
*graft
, void *cb
)
283 struct strbuf
*sb
= cb
;
284 if (graft
->nr_parent
== -1)
285 packet_buf_write(sb
, "shallow %s\n", oid_to_hex(&graft
->oid
));
289 static void advertise_shallow_grafts_buf(struct strbuf
*sb
)
291 if (!is_repository_shallow(the_repository
))
293 for_each_commit_graft(advertise_shallow_grafts_cb
, sb
);
296 #define CHECK_REF_NO_PUSH -1
297 #define CHECK_REF_STATUS_REJECTED -2
298 #define CHECK_REF_UPTODATE -3
299 static int check_to_send_update(const struct ref
*ref
, const struct send_pack_args
*args
)
301 if (!ref
->peer_ref
&& !args
->send_mirror
)
302 return CHECK_REF_NO_PUSH
;
304 /* Check for statuses set by set_ref_status_for_push() */
305 switch (ref
->status
) {
306 case REF_STATUS_REJECT_NONFASTFORWARD
:
307 case REF_STATUS_REJECT_ALREADY_EXISTS
:
308 case REF_STATUS_REJECT_FETCH_FIRST
:
309 case REF_STATUS_REJECT_NEEDS_FORCE
:
310 case REF_STATUS_REJECT_STALE
:
311 case REF_STATUS_REJECT_REMOTE_UPDATED
:
312 case REF_STATUS_REJECT_NODELETE
:
313 return CHECK_REF_STATUS_REJECTED
;
314 case REF_STATUS_UPTODATE
:
315 return CHECK_REF_UPTODATE
;
318 case REF_STATUS_EXPECTING_REPORT
:
319 /* already passed checks on the local side */
321 /* of course this is OK */
327 * the beginning of the next line, or the end of buffer.
329 * NEEDSWORK: perhaps move this to git-compat-util.h or somewhere and
330 * convert many similar uses found by "git grep -A4 memchr".
332 static const char *next_line(const char *line
, size_t len
)
334 const char *nl
= memchr(line
, '\n', len
);
336 return line
+ len
; /* incomplete line */
340 static int generate_push_cert(struct strbuf
*req_buf
,
341 const struct ref
*remote_refs
,
342 struct send_pack_args
*args
,
343 const char *cap_string
,
344 const char *push_cert_nonce
)
346 const struct ref
*ref
;
347 struct string_list_item
*item
;
348 char *signing_key_id
= xstrdup(get_signing_key_id());
350 struct strbuf cert
= STRBUF_INIT
;
353 strbuf_addstr(&cert
, "certificate version 0.1\n");
354 strbuf_addf(&cert
, "pusher %s ", signing_key_id
);
356 strbuf_addch(&cert
, '\n');
357 if (args
->url
&& *args
->url
) {
358 char *anon_url
= transport_anonymize_url(args
->url
);
359 strbuf_addf(&cert
, "pushee %s\n", anon_url
);
362 if (push_cert_nonce
[0])
363 strbuf_addf(&cert
, "nonce %s\n", push_cert_nonce
);
364 if (args
->push_options
)
365 for_each_string_list_item(item
, args
->push_options
)
366 strbuf_addf(&cert
, "push-option %s\n", item
->string
);
367 strbuf_addstr(&cert
, "\n");
369 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
370 if (check_to_send_update(ref
, args
) < 0)
373 strbuf_addf(&cert
, "%s %s %s\n",
374 oid_to_hex(&ref
->old_oid
),
375 oid_to_hex(&ref
->new_oid
),
381 if (sign_buffer(&cert
, &cert
, get_signing_key()))
382 die(_("failed to sign the push certificate"));
384 packet_buf_write(req_buf
, "push-cert%c%s", 0, cap_string
);
385 for (cp
= cert
.buf
; cp
< cert
.buf
+ cert
.len
; cp
= np
) {
386 np
= next_line(cp
, cert
.buf
+ cert
.len
- cp
);
387 packet_buf_write(req_buf
,
388 "%.*s", (int)(np
- cp
), cp
);
390 packet_buf_write(req_buf
, "push-cert-end\n");
393 free(signing_key_id
);
394 strbuf_release(&cert
);
398 #define NONCE_LEN_LIMIT 256
400 static void reject_invalid_nonce(const char *nonce
, int len
)
404 if (NONCE_LEN_LIMIT
<= len
)
405 die("the receiving end asked to sign an invalid nonce <%.*s>",
408 for (i
= 0; i
< len
; i
++) {
409 int ch
= nonce
[i
] & 0xFF;
411 ch
== '-' || ch
== '.' ||
412 ch
== '/' || ch
== '+' ||
413 ch
== '=' || ch
== '_')
415 die("the receiving end asked to sign an invalid nonce <%.*s>",
420 static void get_commons_through_negotiation(const char *url
,
421 const struct ref
*remote_refs
,
422 struct oid_array
*commons
)
424 struct child_process child
= CHILD_PROCESS_INIT
;
425 const struct ref
*ref
;
426 int len
= the_hash_algo
->hexsz
+ 1; /* hash + NL */
431 strvec_pushl(&child
.args
, "fetch", "--negotiate-only", NULL
);
432 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
433 if (!is_null_oid(&ref
->new_oid
))
434 strvec_pushf(&child
.args
, "--negotiation-tip=%s", oid_to_hex(&ref
->new_oid
));
436 strvec_push(&child
.args
, url
);
438 if (start_command(&child
))
439 die(_("send-pack: unable to fork off fetch subprocess"));
442 char hex_hash
[GIT_MAX_HEXSZ
+ 1];
443 int read_len
= read_in_full(child
.out
, hex_hash
, len
);
444 struct object_id oid
;
450 die("invalid length read %d", read_len
);
451 if (parse_oid_hex(hex_hash
, &oid
, &end
) || *end
!= '\n')
453 oid_array_append(commons
, &oid
);
456 if (finish_command(&child
)) {
458 * The information that push negotiation provides is useful but
461 warning(_("push negotiation failed; proceeding anyway with push"));
465 int send_pack(struct send_pack_args
*args
,
466 int fd
[], struct child_process
*conn
,
467 struct ref
*remote_refs
,
468 struct oid_array
*extra_have
)
470 struct oid_array commons
= OID_ARRAY_INIT
;
473 struct strbuf req_buf
= STRBUF_INIT
;
474 struct strbuf cap_buf
= STRBUF_INIT
;
476 int need_pack_data
= 0;
477 int allow_deleting_refs
= 0;
478 int status_report
= 0;
479 int use_sideband
= 0;
480 int quiet_supported
= 0;
481 int agent_supported
= 0;
482 int advertise_sid
= 0;
483 int push_negotiate
= 0;
485 int atomic_supported
= 0;
486 int use_push_options
= 0;
487 int push_options_supported
= 0;
488 int object_format_supported
= 0;
489 unsigned cmds_sent
= 0;
492 const char *push_cert_nonce
= NULL
;
493 struct packet_reader reader
;
497 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
498 "Perhaps you should specify a branch.\n");
502 git_config_get_bool("push.negotiate", &push_negotiate
);
504 get_commons_through_negotiation(args
->url
, remote_refs
, &commons
);
506 if (!git_config_get_bool("push.usebitmaps", &use_bitmaps
))
507 args
->disable_bitmaps
= !use_bitmaps
;
509 git_config_get_bool("transfer.advertisesid", &advertise_sid
);
511 /* Does the other end support the reporting? */
512 if (server_supports("report-status-v2"))
514 else if (server_supports("report-status"))
516 if (server_supports("delete-refs"))
517 allow_deleting_refs
= 1;
518 if (server_supports("ofs-delta"))
519 args
->use_ofs_delta
= 1;
520 if (server_supports("side-band-64k"))
522 if (server_supports("quiet"))
524 if (server_supports("agent"))
526 if (!server_supports("session-id"))
528 if (server_supports("no-thin"))
529 args
->use_thin_pack
= 0;
530 if (server_supports("atomic"))
531 atomic_supported
= 1;
532 if (server_supports("push-options"))
533 push_options_supported
= 1;
535 if (!server_supports_hash(the_hash_algo
->name
, &object_format_supported
))
536 die(_("the receiving end does not support this repository's hash algorithm"));
538 if (args
->push_cert
!= SEND_PACK_PUSH_CERT_NEVER
) {
540 push_cert_nonce
= server_feature_value("push-cert", &len
);
541 if (push_cert_nonce
) {
542 reject_invalid_nonce(push_cert_nonce
, len
);
543 push_cert_nonce
= xmemdupz(push_cert_nonce
, len
);
544 } else if (args
->push_cert
== SEND_PACK_PUSH_CERT_ALWAYS
) {
545 die(_("the receiving end does not support --signed push"));
546 } else if (args
->push_cert
== SEND_PACK_PUSH_CERT_IF_ASKED
) {
547 warning(_("not sending a push certificate since the"
548 " receiving end does not support --signed"
553 if (args
->atomic
&& !atomic_supported
)
554 die(_("the receiving end does not support --atomic push"));
556 use_atomic
= atomic_supported
&& args
->atomic
;
558 if (args
->push_options
&& !push_options_supported
)
559 die(_("the receiving end does not support push options"));
561 use_push_options
= push_options_supported
&& args
->push_options
;
563 if (status_report
== 1)
564 strbuf_addstr(&cap_buf
, " report-status");
565 else if (status_report
== 2)
566 strbuf_addstr(&cap_buf
, " report-status-v2");
568 strbuf_addstr(&cap_buf
, " side-band-64k");
569 if (quiet_supported
&& (args
->quiet
|| !args
->progress
))
570 strbuf_addstr(&cap_buf
, " quiet");
572 strbuf_addstr(&cap_buf
, " atomic");
573 if (use_push_options
)
574 strbuf_addstr(&cap_buf
, " push-options");
575 if (object_format_supported
)
576 strbuf_addf(&cap_buf
, " object-format=%s", the_hash_algo
->name
);
578 strbuf_addf(&cap_buf
, " agent=%s", git_user_agent_sanitized());
580 strbuf_addf(&cap_buf
, " session-id=%s", trace2_session_id());
583 * NEEDSWORK: why does delete-refs have to be so specific to
584 * send-pack machinery that set_ref_status_for_push() cannot
585 * set this bit for us???
587 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
588 if (ref
->deletion
&& !allow_deleting_refs
)
589 ref
->status
= REF_STATUS_REJECT_NODELETE
;
592 * Clear the status for each ref and see if we need to send
595 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
596 switch (check_to_send_update(ref
, args
)) {
597 case 0: /* no error */
599 case CHECK_REF_STATUS_REJECTED
:
601 * When we know the server would reject a ref update if
602 * we were to send it and we're trying to send the refs
603 * atomically, abort the whole operation.
606 strbuf_release(&req_buf
);
607 strbuf_release(&cap_buf
);
608 reject_atomic_push(remote_refs
, args
->send_mirror
);
609 error("atomic push failed for ref %s. status: %d\n",
610 ref
->name
, ref
->status
);
611 return args
->porcelain
? 0 : -1;
613 /* else fallthrough */
620 if (args
->dry_run
|| !status_report
)
621 ref
->status
= REF_STATUS_OK
;
623 ref
->status
= REF_STATUS_EXPECTING_REPORT
;
627 advertise_shallow_grafts_buf(&req_buf
);
630 * Finally, tell the other end!
632 if (!args
->dry_run
&& push_cert_nonce
)
633 cmds_sent
= generate_push_cert(&req_buf
, remote_refs
, args
,
634 cap_buf
.buf
, push_cert_nonce
);
635 else if (!args
->dry_run
)
636 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
637 char *old_hex
, *new_hex
;
639 if (check_to_send_update(ref
, args
) < 0)
642 old_hex
= oid_to_hex(&ref
->old_oid
);
643 new_hex
= oid_to_hex(&ref
->new_oid
);
645 packet_buf_write(&req_buf
,
647 old_hex
, new_hex
, ref
->name
, 0,
651 packet_buf_write(&req_buf
, "%s %s %s",
652 old_hex
, new_hex
, ref
->name
);
656 if (use_push_options
) {
657 struct string_list_item
*item
;
659 packet_buf_flush(&req_buf
);
660 for_each_string_list_item(item
, args
->push_options
)
661 packet_buf_write(&req_buf
, "%s", item
->string
);
664 if (args
->stateless_rpc
) {
665 if (!args
->dry_run
&& (cmds_sent
|| is_repository_shallow(the_repository
))) {
666 packet_buf_flush(&req_buf
);
667 send_sideband(out
, -1, req_buf
.buf
, req_buf
.len
, LARGE_PACKET_MAX
);
670 write_or_die(out
, req_buf
.buf
, req_buf
.len
);
673 strbuf_release(&req_buf
);
674 strbuf_release(&cap_buf
);
676 if (use_sideband
&& cmds_sent
) {
677 memset(&demux
, 0, sizeof(demux
));
678 demux
.proc
= sideband_demux
;
681 demux
.isolate_sigpipe
= 1;
682 if (start_async(&demux
))
683 die("send-pack: unable to fork off sideband demultiplexer");
687 packet_reader_init(&reader
, in
, NULL
, 0,
688 PACKET_READ_CHOMP_NEWLINE
|
689 PACKET_READ_DIE_ON_ERR_PACKET
);
691 if (need_pack_data
&& cmds_sent
) {
692 if (pack_objects(out
, remote_refs
, extra_have
, &commons
, args
) < 0) {
693 if (args
->stateless_rpc
)
695 if (git_connection_is_socket(conn
))
696 shutdown(fd
[0], SHUT_WR
);
699 * Do not even bother with the return value; we know we
700 * are failing, and just want the error() side effects,
701 * as well as marking refs with their remote status (if
705 receive_status(&reader
, remote_refs
);
709 finish_async(&demux
);
714 if (!args
->stateless_rpc
)
715 /* Closed by pack_objects() via start_command() */
718 if (args
->stateless_rpc
&& cmds_sent
)
721 if (status_report
&& cmds_sent
)
722 ret
= receive_status(&reader
, remote_refs
);
725 if (args
->stateless_rpc
)
728 if (use_sideband
&& cmds_sent
) {
730 if (finish_async(&demux
)) {
731 error("error in sideband demultiplexer");
742 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
743 switch (ref
->status
) {
744 case REF_STATUS_NONE
:
745 case REF_STATUS_UPTODATE
: