1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
9 #include "object-store-ll.h"
12 #include "run-command.h"
15 #include "send-pack.h"
16 #include "transport.h"
18 #include "oid-array.h"
19 #include "gpg-interface.h"
21 #include "parse-options.h"
23 #include "write-or-die.h"
25 int option_parse_push_signed(const struct option
*opt
,
26 const char *arg
, int unset
)
29 *(int *)(opt
->value
) = SEND_PACK_PUSH_CERT_NEVER
;
32 switch (git_parse_maybe_bool(arg
)) {
34 *(int *)(opt
->value
) = SEND_PACK_PUSH_CERT_ALWAYS
;
37 *(int *)(opt
->value
) = SEND_PACK_PUSH_CERT_NEVER
;
40 if (!strcasecmp("if-asked", arg
)) {
41 *(int *)(opt
->value
) = SEND_PACK_PUSH_CERT_IF_ASKED
;
44 die("bad %s argument: %s", opt
->long_name
, arg
);
47 static void feed_object(const struct object_id
*oid
, FILE *fh
, int negative
)
50 !repo_has_object_file_with_flags(the_repository
, oid
,
51 OBJECT_INFO_SKIP_FETCH_OBJECT
|
57 fputs(oid_to_hex(oid
), fh
);
62 * Make a pack stream and spit it out into file descriptor fd
64 static int pack_objects(int fd
, struct ref
*refs
, struct oid_array
*advertised
,
65 struct oid_array
*negotiated
,
66 struct send_pack_args
*args
)
69 * The child becomes pack-objects --revs; we feed
70 * the revision parameters to it via its stdin and
71 * let its stdout go back to the other end.
73 struct child_process po
= CHILD_PROCESS_INIT
;
78 strvec_push(&po
.args
, "pack-objects");
79 strvec_push(&po
.args
, "--all-progress-implied");
80 strvec_push(&po
.args
, "--revs");
81 strvec_push(&po
.args
, "--stdout");
82 if (args
->use_thin_pack
)
83 strvec_push(&po
.args
, "--thin");
84 if (args
->use_ofs_delta
)
85 strvec_push(&po
.args
, "--delta-base-offset");
86 if (args
->quiet
|| !args
->progress
)
87 strvec_push(&po
.args
, "-q");
89 strvec_push(&po
.args
, "--progress");
90 if (is_repository_shallow(the_repository
))
91 strvec_push(&po
.args
, "--shallow");
92 if (args
->disable_bitmaps
)
93 strvec_push(&po
.args
, "--no-use-bitmap-index");
95 po
.out
= args
->stateless_rpc
? -1 : fd
;
98 if (start_command(&po
))
99 die_errno("git pack-objects failed");
102 * We feed the pack-objects we just spawned with revision
103 * parameters by writing to the pipe.
105 po_in
= xfdopen(po
.in
, "w");
106 for (i
= 0; i
< advertised
->nr
; i
++)
107 feed_object(&advertised
->oid
[i
], po_in
, 1);
108 for (i
= 0; i
< negotiated
->nr
; i
++)
109 feed_object(&negotiated
->oid
[i
], po_in
, 1);
112 if (!is_null_oid(&refs
->old_oid
))
113 feed_object(&refs
->old_oid
, po_in
, 1);
114 if (!is_null_oid(&refs
->new_oid
))
115 feed_object(&refs
->new_oid
, po_in
, 0);
121 die_errno("error writing to pack-objects");
124 if (args
->stateless_rpc
) {
125 char *buf
= xmalloc(LARGE_PACKET_MAX
);
127 ssize_t n
= xread(po
.out
, buf
, LARGE_PACKET_MAX
);
130 send_sideband(fd
, -1, buf
, n
, LARGE_PACKET_MAX
);
137 rc
= finish_command(&po
);
140 * For a normal non-zero exit, we assume pack-objects wrote
141 * something useful to stderr. For death by signal, though,
142 * we should mention it to the user. The exception is SIGPIPE
143 * (141), because that's a normal occurrence if the remote end
144 * hangs up (and we'll report that by trying to read the unpack
147 if (rc
> 128 && rc
!= 141)
148 error("pack-objects died of signal %d", rc
- 128);
154 static int receive_unpack_status(struct packet_reader
*reader
)
156 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
157 return error(_("unexpected flush packet while reading remote unpack status"));
158 if (!skip_prefix(reader
->line
, "unpack ", &reader
->line
))
159 return error(_("unable to parse remote unpack status: %s"), reader
->line
);
160 if (strcmp(reader
->line
, "ok"))
161 return error(_("remote unpack failed: %s"), reader
->line
);
165 static int receive_status(struct packet_reader
*reader
, struct ref
*refs
)
169 struct ref_push_report
*report
= NULL
;
174 ret
= receive_unpack_status(reader
);
176 struct object_id old_oid
, new_oid
;
180 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
183 p
= strchr(head
, ' ');
185 error("invalid status line from remote: %s", reader
->line
);
191 if (!strcmp(head
, "option")) {
192 const char *key
, *val
;
194 if (!hint
|| !(report
|| new_report
)) {
196 error("'option' without a matching 'ok/ng' directive");
202 CALLOC_ARRAY(hint
->report
, 1);
203 report
= hint
->report
;
205 report
= hint
->report
;
207 report
= report
->next
;
208 CALLOC_ARRAY(report
->next
, 1);
209 report
= report
->next
;
214 p
= strchr(key
, ' ');
218 if (!strcmp(key
, "refname"))
219 report
->ref_name
= xstrdup_or_null(val
);
220 else if (!strcmp(key
, "old-oid") && val
&&
221 !parse_oid_hex(val
, &old_oid
, &val
))
222 report
->old_oid
= oiddup(&old_oid
);
223 else if (!strcmp(key
, "new-oid") && val
&&
224 !parse_oid_hex(val
, &new_oid
, &val
))
225 report
->new_oid
= oiddup(&new_oid
);
226 else if (!strcmp(key
, "forced-update"))
227 report
->forced_update
= 1;
233 if (strcmp(head
, "ok") && strcmp(head
, "ng")) {
234 error("invalid ref status from remote: %s", head
);
239 p
= strchr(refname
, ' ');
242 /* first try searching at our hint, falling back to all refs */
244 hint
= find_ref_by_name(hint
, refname
);
246 hint
= find_ref_by_name(refs
, refname
);
248 warning("remote reported status on unknown ref: %s",
252 if (hint
->status
!= REF_STATUS_EXPECTING_REPORT
&&
253 hint
->status
!= REF_STATUS_OK
&&
254 hint
->status
!= REF_STATUS_REMOTE_REJECT
) {
255 warning("remote reported status on unexpected ref: %s",
259 if (!strcmp(head
, "ng")) {
260 hint
->status
= REF_STATUS_REMOTE_REJECT
;
262 hint
->remote_status
= xstrdup(p
);
264 hint
->remote_status
= xstrdup("failed");
266 hint
->status
= REF_STATUS_OK
;
267 hint
->remote_status
= xstrdup_or_null(p
);
274 static int sideband_demux(int in UNUSED
, int out
, void *data
)
277 if (async_with_fork())
279 ret
= recv_sideband("send-pack", fd
[0], out
);
284 static int advertise_shallow_grafts_cb(const struct commit_graft
*graft
, void *cb
)
286 struct strbuf
*sb
= cb
;
287 if (graft
->nr_parent
== -1)
288 packet_buf_write(sb
, "shallow %s\n", oid_to_hex(&graft
->oid
));
292 static void advertise_shallow_grafts_buf(struct strbuf
*sb
)
294 if (!is_repository_shallow(the_repository
))
296 for_each_commit_graft(advertise_shallow_grafts_cb
, sb
);
299 #define CHECK_REF_NO_PUSH -1
300 #define CHECK_REF_STATUS_REJECTED -2
301 #define CHECK_REF_UPTODATE -3
302 static int check_to_send_update(const struct ref
*ref
, const struct send_pack_args
*args
)
304 if (!ref
->peer_ref
&& !args
->send_mirror
)
305 return CHECK_REF_NO_PUSH
;
307 /* Check for statuses set by set_ref_status_for_push() */
308 switch (ref
->status
) {
309 case REF_STATUS_REJECT_NONFASTFORWARD
:
310 case REF_STATUS_REJECT_ALREADY_EXISTS
:
311 case REF_STATUS_REJECT_FETCH_FIRST
:
312 case REF_STATUS_REJECT_NEEDS_FORCE
:
313 case REF_STATUS_REJECT_STALE
:
314 case REF_STATUS_REJECT_REMOTE_UPDATED
:
315 case REF_STATUS_REJECT_NODELETE
:
316 return CHECK_REF_STATUS_REJECTED
;
317 case REF_STATUS_UPTODATE
:
318 return CHECK_REF_UPTODATE
;
321 case REF_STATUS_EXPECTING_REPORT
:
322 /* already passed checks on the local side */
324 /* of course this is OK */
330 * the beginning of the next line, or the end of buffer.
332 * NEEDSWORK: perhaps move this to git-compat-util.h or somewhere and
333 * convert many similar uses found by "git grep -A4 memchr".
335 static const char *next_line(const char *line
, size_t len
)
337 const char *nl
= memchr(line
, '\n', len
);
339 return line
+ len
; /* incomplete line */
343 static int generate_push_cert(struct strbuf
*req_buf
,
344 const struct ref
*remote_refs
,
345 struct send_pack_args
*args
,
346 const char *cap_string
,
347 const char *push_cert_nonce
)
349 const struct ref
*ref
;
350 struct string_list_item
*item
;
351 char *signing_key_id
= xstrdup(get_signing_key_id());
353 struct strbuf cert
= STRBUF_INIT
;
356 strbuf_addstr(&cert
, "certificate version 0.1\n");
357 strbuf_addf(&cert
, "pusher %s ", signing_key_id
);
359 strbuf_addch(&cert
, '\n');
360 if (args
->url
&& *args
->url
) {
361 char *anon_url
= transport_anonymize_url(args
->url
);
362 strbuf_addf(&cert
, "pushee %s\n", anon_url
);
365 if (push_cert_nonce
[0])
366 strbuf_addf(&cert
, "nonce %s\n", push_cert_nonce
);
367 if (args
->push_options
)
368 for_each_string_list_item(item
, args
->push_options
)
369 strbuf_addf(&cert
, "push-option %s\n", item
->string
);
370 strbuf_addstr(&cert
, "\n");
372 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
373 if (check_to_send_update(ref
, args
) < 0)
376 strbuf_addf(&cert
, "%s %s %s\n",
377 oid_to_hex(&ref
->old_oid
),
378 oid_to_hex(&ref
->new_oid
),
384 if (sign_buffer(&cert
, &cert
, get_signing_key()))
385 die(_("failed to sign the push certificate"));
387 packet_buf_write(req_buf
, "push-cert%c%s", 0, cap_string
);
388 for (cp
= cert
.buf
; cp
< cert
.buf
+ cert
.len
; cp
= np
) {
389 np
= next_line(cp
, cert
.buf
+ cert
.len
- cp
);
390 packet_buf_write(req_buf
,
391 "%.*s", (int)(np
- cp
), cp
);
393 packet_buf_write(req_buf
, "push-cert-end\n");
396 free(signing_key_id
);
397 strbuf_release(&cert
);
401 #define NONCE_LEN_LIMIT 256
403 static void reject_invalid_nonce(const char *nonce
, int len
)
407 if (NONCE_LEN_LIMIT
<= len
)
408 die("the receiving end asked to sign an invalid nonce <%.*s>",
411 for (i
= 0; i
< len
; i
++) {
412 int ch
= nonce
[i
] & 0xFF;
414 ch
== '-' || ch
== '.' ||
415 ch
== '/' || ch
== '+' ||
416 ch
== '=' || ch
== '_')
418 die("the receiving end asked to sign an invalid nonce <%.*s>",
423 static void get_commons_through_negotiation(const char *url
,
424 const struct ref
*remote_refs
,
425 struct oid_array
*commons
)
427 struct child_process child
= CHILD_PROCESS_INIT
;
428 const struct ref
*ref
;
429 int len
= the_hash_algo
->hexsz
+ 1; /* hash + NL */
430 int nr_negotiation_tip
= 0;
435 strvec_pushl(&child
.args
, "fetch", "--negotiate-only", NULL
);
436 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
437 if (!is_null_oid(&ref
->new_oid
)) {
438 strvec_pushf(&child
.args
, "--negotiation-tip=%s",
439 oid_to_hex(&ref
->new_oid
));
440 nr_negotiation_tip
++;
443 strvec_push(&child
.args
, url
);
445 if (!nr_negotiation_tip
) {
446 child_process_clear(&child
);
450 if (start_command(&child
))
451 die(_("send-pack: unable to fork off fetch subprocess"));
454 char hex_hash
[GIT_MAX_HEXSZ
+ 1];
455 int read_len
= read_in_full(child
.out
, hex_hash
, len
);
456 struct object_id oid
;
462 die("invalid length read %d", read_len
);
463 if (parse_oid_hex(hex_hash
, &oid
, &end
) || *end
!= '\n')
465 oid_array_append(commons
, &oid
);
468 if (finish_command(&child
)) {
470 * The information that push negotiation provides is useful but
473 warning(_("push negotiation failed; proceeding anyway with push"));
477 int send_pack(struct send_pack_args
*args
,
478 int fd
[], struct child_process
*conn
,
479 struct ref
*remote_refs
,
480 struct oid_array
*extra_have
)
482 struct oid_array commons
= OID_ARRAY_INIT
;
485 struct strbuf req_buf
= STRBUF_INIT
;
486 struct strbuf cap_buf
= STRBUF_INIT
;
488 int need_pack_data
= 0;
489 int allow_deleting_refs
= 0;
490 int status_report
= 0;
491 int use_sideband
= 0;
492 int quiet_supported
= 0;
493 int agent_supported
= 0;
494 int advertise_sid
= 0;
495 int push_negotiate
= 0;
497 int atomic_supported
= 0;
498 int use_push_options
= 0;
499 int push_options_supported
= 0;
500 int object_format_supported
= 0;
501 unsigned cmds_sent
= 0;
504 const char *push_cert_nonce
= NULL
;
505 struct packet_reader reader
;
509 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
510 "Perhaps you should specify a branch.\n");
514 git_config_get_bool("push.negotiate", &push_negotiate
);
516 get_commons_through_negotiation(args
->url
, remote_refs
, &commons
);
518 if (!git_config_get_bool("push.usebitmaps", &use_bitmaps
))
519 args
->disable_bitmaps
= !use_bitmaps
;
521 git_config_get_bool("transfer.advertisesid", &advertise_sid
);
523 /* Does the other end support the reporting? */
524 if (server_supports("report-status-v2"))
526 else if (server_supports("report-status"))
528 if (server_supports("delete-refs"))
529 allow_deleting_refs
= 1;
530 if (server_supports("ofs-delta"))
531 args
->use_ofs_delta
= 1;
532 if (server_supports("side-band-64k"))
534 if (server_supports("quiet"))
536 if (server_supports("agent"))
538 if (!server_supports("session-id"))
540 if (server_supports("no-thin"))
541 args
->use_thin_pack
= 0;
542 if (server_supports("atomic"))
543 atomic_supported
= 1;
544 if (server_supports("push-options"))
545 push_options_supported
= 1;
547 if (!server_supports_hash(the_hash_algo
->name
, &object_format_supported
))
548 die(_("the receiving end does not support this repository's hash algorithm"));
550 if (args
->push_cert
!= SEND_PACK_PUSH_CERT_NEVER
) {
552 push_cert_nonce
= server_feature_value("push-cert", &len
);
553 if (push_cert_nonce
) {
554 reject_invalid_nonce(push_cert_nonce
, len
);
555 push_cert_nonce
= xmemdupz(push_cert_nonce
, len
);
556 } else if (args
->push_cert
== SEND_PACK_PUSH_CERT_ALWAYS
) {
557 die(_("the receiving end does not support --signed push"));
558 } else if (args
->push_cert
== SEND_PACK_PUSH_CERT_IF_ASKED
) {
559 warning(_("not sending a push certificate since the"
560 " receiving end does not support --signed"
565 if (args
->atomic
&& !atomic_supported
)
566 die(_("the receiving end does not support --atomic push"));
568 use_atomic
= atomic_supported
&& args
->atomic
;
570 if (args
->push_options
&& !push_options_supported
)
571 die(_("the receiving end does not support push options"));
573 use_push_options
= push_options_supported
&& args
->push_options
;
575 if (status_report
== 1)
576 strbuf_addstr(&cap_buf
, " report-status");
577 else if (status_report
== 2)
578 strbuf_addstr(&cap_buf
, " report-status-v2");
580 strbuf_addstr(&cap_buf
, " side-band-64k");
581 if (quiet_supported
&& (args
->quiet
|| !args
->progress
))
582 strbuf_addstr(&cap_buf
, " quiet");
584 strbuf_addstr(&cap_buf
, " atomic");
585 if (use_push_options
)
586 strbuf_addstr(&cap_buf
, " push-options");
587 if (object_format_supported
)
588 strbuf_addf(&cap_buf
, " object-format=%s", the_hash_algo
->name
);
590 strbuf_addf(&cap_buf
, " agent=%s", git_user_agent_sanitized());
592 strbuf_addf(&cap_buf
, " session-id=%s", trace2_session_id());
595 * NEEDSWORK: why does delete-refs have to be so specific to
596 * send-pack machinery that set_ref_status_for_push() cannot
597 * set this bit for us???
599 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
600 if (ref
->deletion
&& !allow_deleting_refs
)
601 ref
->status
= REF_STATUS_REJECT_NODELETE
;
604 * Clear the status for each ref and see if we need to send
607 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
608 switch (check_to_send_update(ref
, args
)) {
609 case 0: /* no error */
611 case CHECK_REF_STATUS_REJECTED
:
613 * When we know the server would reject a ref update if
614 * we were to send it and we're trying to send the refs
615 * atomically, abort the whole operation.
618 strbuf_release(&req_buf
);
619 strbuf_release(&cap_buf
);
620 reject_atomic_push(remote_refs
, args
->send_mirror
);
621 error("atomic push failed for ref %s. status: %d\n",
622 ref
->name
, ref
->status
);
623 return args
->porcelain
? 0 : -1;
625 /* else fallthrough */
632 if (args
->dry_run
|| !status_report
)
633 ref
->status
= REF_STATUS_OK
;
635 ref
->status
= REF_STATUS_EXPECTING_REPORT
;
639 advertise_shallow_grafts_buf(&req_buf
);
642 * Finally, tell the other end!
644 if (!args
->dry_run
&& push_cert_nonce
)
645 cmds_sent
= generate_push_cert(&req_buf
, remote_refs
, args
,
646 cap_buf
.buf
, push_cert_nonce
);
647 else if (!args
->dry_run
)
648 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
649 char *old_hex
, *new_hex
;
651 if (check_to_send_update(ref
, args
) < 0)
654 old_hex
= oid_to_hex(&ref
->old_oid
);
655 new_hex
= oid_to_hex(&ref
->new_oid
);
657 packet_buf_write(&req_buf
,
659 old_hex
, new_hex
, ref
->name
, 0,
663 packet_buf_write(&req_buf
, "%s %s %s",
664 old_hex
, new_hex
, ref
->name
);
668 if (use_push_options
) {
669 struct string_list_item
*item
;
671 packet_buf_flush(&req_buf
);
672 for_each_string_list_item(item
, args
->push_options
)
673 packet_buf_write(&req_buf
, "%s", item
->string
);
676 if (args
->stateless_rpc
) {
677 if (!args
->dry_run
&& (cmds_sent
|| is_repository_shallow(the_repository
))) {
678 packet_buf_flush(&req_buf
);
679 send_sideband(out
, -1, req_buf
.buf
, req_buf
.len
, LARGE_PACKET_MAX
);
682 write_or_die(out
, req_buf
.buf
, req_buf
.len
);
685 strbuf_release(&req_buf
);
686 strbuf_release(&cap_buf
);
688 if (use_sideband
&& cmds_sent
) {
689 memset(&demux
, 0, sizeof(demux
));
690 demux
.proc
= sideband_demux
;
693 demux
.isolate_sigpipe
= 1;
694 if (start_async(&demux
))
695 die("send-pack: unable to fork off sideband demultiplexer");
699 packet_reader_init(&reader
, in
, NULL
, 0,
700 PACKET_READ_CHOMP_NEWLINE
|
701 PACKET_READ_DIE_ON_ERR_PACKET
);
703 if (need_pack_data
&& cmds_sent
) {
704 if (pack_objects(out
, remote_refs
, extra_have
, &commons
, args
) < 0) {
705 if (args
->stateless_rpc
)
707 if (git_connection_is_socket(conn
))
708 shutdown(fd
[0], SHUT_WR
);
711 * Do not even bother with the return value; we know we
712 * are failing, and just want the error() side effects,
713 * as well as marking refs with their remote status (if
717 receive_status(&reader
, remote_refs
);
721 finish_async(&demux
);
726 if (!args
->stateless_rpc
)
727 /* Closed by pack_objects() via start_command() */
730 if (args
->stateless_rpc
&& cmds_sent
)
733 if (status_report
&& cmds_sent
)
734 ret
= receive_status(&reader
, remote_refs
);
737 if (args
->stateless_rpc
)
740 if (use_sideband
&& cmds_sent
) {
742 if (finish_async(&demux
)) {
743 error("error in sideband demultiplexer");
754 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
755 switch (ref
->status
) {
756 case REF_STATUS_NONE
:
757 case REF_STATUS_UPTODATE
: