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 trace2_region_enter("send_pack", "pack_objects", the_repository
);
79 strvec_push(&po
.args
, "pack-objects");
80 strvec_push(&po
.args
, "--all-progress-implied");
81 strvec_push(&po
.args
, "--revs");
82 strvec_push(&po
.args
, "--stdout");
83 if (args
->use_thin_pack
)
84 strvec_push(&po
.args
, "--thin");
85 if (args
->use_ofs_delta
)
86 strvec_push(&po
.args
, "--delta-base-offset");
87 if (args
->quiet
|| !args
->progress
)
88 strvec_push(&po
.args
, "-q");
90 strvec_push(&po
.args
, "--progress");
91 if (is_repository_shallow(the_repository
))
92 strvec_push(&po
.args
, "--shallow");
93 if (args
->disable_bitmaps
)
94 strvec_push(&po
.args
, "--no-use-bitmap-index");
96 po
.out
= args
->stateless_rpc
? -1 : fd
;
99 if (start_command(&po
))
100 die_errno("git pack-objects failed");
103 * We feed the pack-objects we just spawned with revision
104 * parameters by writing to the pipe.
106 po_in
= xfdopen(po
.in
, "w");
107 for (i
= 0; i
< advertised
->nr
; i
++)
108 feed_object(&advertised
->oid
[i
], po_in
, 1);
109 for (i
= 0; i
< negotiated
->nr
; i
++)
110 feed_object(&negotiated
->oid
[i
], po_in
, 1);
113 if (!is_null_oid(&refs
->old_oid
))
114 feed_object(&refs
->old_oid
, po_in
, 1);
115 if (!is_null_oid(&refs
->new_oid
))
116 feed_object(&refs
->new_oid
, po_in
, 0);
122 die_errno("error writing to pack-objects");
125 if (args
->stateless_rpc
) {
126 char *buf
= xmalloc(LARGE_PACKET_MAX
);
128 ssize_t n
= xread(po
.out
, buf
, LARGE_PACKET_MAX
);
131 send_sideband(fd
, -1, buf
, n
, LARGE_PACKET_MAX
);
138 rc
= finish_command(&po
);
141 * For a normal non-zero exit, we assume pack-objects wrote
142 * something useful to stderr. For death by signal, though,
143 * we should mention it to the user. The exception is SIGPIPE
144 * (141), because that's a normal occurrence if the remote end
145 * hangs up (and we'll report that by trying to read the unpack
148 if (rc
> 128 && rc
!= 141)
149 error("pack-objects died of signal %d", rc
- 128);
150 trace2_region_leave("send_pack", "pack_objects", the_repository
);
153 trace2_region_leave("send_pack", "pack_objects", the_repository
);
157 static int receive_unpack_status(struct packet_reader
*reader
)
159 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
160 return error(_("unexpected flush packet while reading remote unpack status"));
161 if (!skip_prefix(reader
->line
, "unpack ", &reader
->line
))
162 return error(_("unable to parse remote unpack status: %s"), reader
->line
);
163 if (strcmp(reader
->line
, "ok"))
164 return error(_("remote unpack failed: %s"), reader
->line
);
168 static int receive_status(struct packet_reader
*reader
, struct ref
*refs
)
172 struct ref_push_report
*report
= NULL
;
176 trace2_region_enter("send_pack", "receive_status", the_repository
);
178 ret
= receive_unpack_status(reader
);
180 struct object_id old_oid
, new_oid
;
184 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
187 p
= strchr(head
, ' ');
189 error("invalid status line from remote: %s", reader
->line
);
195 if (!strcmp(head
, "option")) {
196 const char *key
, *val
;
198 if (!hint
|| !(report
|| new_report
)) {
200 error("'option' without a matching 'ok/ng' directive");
206 CALLOC_ARRAY(hint
->report
, 1);
207 report
= hint
->report
;
209 report
= hint
->report
;
211 report
= report
->next
;
212 CALLOC_ARRAY(report
->next
, 1);
213 report
= report
->next
;
218 p
= strchr(key
, ' ');
222 if (!strcmp(key
, "refname"))
223 report
->ref_name
= xstrdup_or_null(val
);
224 else if (!strcmp(key
, "old-oid") && val
&&
225 !parse_oid_hex(val
, &old_oid
, &val
))
226 report
->old_oid
= oiddup(&old_oid
);
227 else if (!strcmp(key
, "new-oid") && val
&&
228 !parse_oid_hex(val
, &new_oid
, &val
))
229 report
->new_oid
= oiddup(&new_oid
);
230 else if (!strcmp(key
, "forced-update"))
231 report
->forced_update
= 1;
237 if (strcmp(head
, "ok") && strcmp(head
, "ng")) {
238 error("invalid ref status from remote: %s", head
);
243 p
= strchr(refname
, ' ');
246 /* first try searching at our hint, falling back to all refs */
248 hint
= find_ref_by_name(hint
, refname
);
250 hint
= find_ref_by_name(refs
, refname
);
252 warning("remote reported status on unknown ref: %s",
256 if (hint
->status
!= REF_STATUS_EXPECTING_REPORT
&&
257 hint
->status
!= REF_STATUS_OK
&&
258 hint
->status
!= REF_STATUS_REMOTE_REJECT
) {
259 warning("remote reported status on unexpected ref: %s",
263 if (!strcmp(head
, "ng")) {
264 hint
->status
= REF_STATUS_REMOTE_REJECT
;
266 hint
->remote_status
= xstrdup(p
);
268 hint
->remote_status
= xstrdup("failed");
270 hint
->status
= REF_STATUS_OK
;
271 hint
->remote_status
= xstrdup_or_null(p
);
275 trace2_region_leave("send_pack", "receive_status", the_repository
);
279 static int sideband_demux(int in UNUSED
, int out
, void *data
)
282 if (async_with_fork())
284 ret
= recv_sideband("send-pack", fd
[0], out
);
289 static int advertise_shallow_grafts_cb(const struct commit_graft
*graft
, void *cb
)
291 struct strbuf
*sb
= cb
;
292 if (graft
->nr_parent
== -1)
293 packet_buf_write(sb
, "shallow %s\n", oid_to_hex(&graft
->oid
));
297 static void advertise_shallow_grafts_buf(struct strbuf
*sb
)
299 if (!is_repository_shallow(the_repository
))
301 for_each_commit_graft(advertise_shallow_grafts_cb
, sb
);
304 #define CHECK_REF_NO_PUSH -1
305 #define CHECK_REF_STATUS_REJECTED -2
306 #define CHECK_REF_UPTODATE -3
307 static int check_to_send_update(const struct ref
*ref
, const struct send_pack_args
*args
)
309 if (!ref
->peer_ref
&& !args
->send_mirror
)
310 return CHECK_REF_NO_PUSH
;
312 /* Check for statuses set by set_ref_status_for_push() */
313 switch (ref
->status
) {
314 case REF_STATUS_REJECT_NONFASTFORWARD
:
315 case REF_STATUS_REJECT_ALREADY_EXISTS
:
316 case REF_STATUS_REJECT_FETCH_FIRST
:
317 case REF_STATUS_REJECT_NEEDS_FORCE
:
318 case REF_STATUS_REJECT_STALE
:
319 case REF_STATUS_REJECT_REMOTE_UPDATED
:
320 case REF_STATUS_REJECT_NODELETE
:
321 return CHECK_REF_STATUS_REJECTED
;
322 case REF_STATUS_UPTODATE
:
323 return CHECK_REF_UPTODATE
;
326 case REF_STATUS_EXPECTING_REPORT
:
327 /* already passed checks on the local side */
329 /* of course this is OK */
335 * the beginning of the next line, or the end of buffer.
337 * NEEDSWORK: perhaps move this to git-compat-util.h or somewhere and
338 * convert many similar uses found by "git grep -A4 memchr".
340 static const char *next_line(const char *line
, size_t len
)
342 const char *nl
= memchr(line
, '\n', len
);
344 return line
+ len
; /* incomplete line */
348 static int generate_push_cert(struct strbuf
*req_buf
,
349 const struct ref
*remote_refs
,
350 struct send_pack_args
*args
,
351 const char *cap_string
,
352 const char *push_cert_nonce
)
354 const struct ref
*ref
;
355 struct string_list_item
*item
;
356 char *signing_key_id
= get_signing_key_id();
357 char *signing_key
= get_signing_key();
359 struct strbuf cert
= STRBUF_INIT
;
362 strbuf_addstr(&cert
, "certificate version 0.1\n");
363 strbuf_addf(&cert
, "pusher %s ", signing_key_id
);
365 strbuf_addch(&cert
, '\n');
366 if (args
->url
&& *args
->url
) {
367 char *anon_url
= transport_anonymize_url(args
->url
);
368 strbuf_addf(&cert
, "pushee %s\n", anon_url
);
371 if (push_cert_nonce
[0])
372 strbuf_addf(&cert
, "nonce %s\n", push_cert_nonce
);
373 if (args
->push_options
)
374 for_each_string_list_item(item
, args
->push_options
)
375 strbuf_addf(&cert
, "push-option %s\n", item
->string
);
376 strbuf_addstr(&cert
, "\n");
378 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
379 if (check_to_send_update(ref
, args
) < 0)
382 strbuf_addf(&cert
, "%s %s %s\n",
383 oid_to_hex(&ref
->old_oid
),
384 oid_to_hex(&ref
->new_oid
),
390 if (sign_buffer(&cert
, &cert
, signing_key
))
391 die(_("failed to sign the push certificate"));
393 packet_buf_write(req_buf
, "push-cert%c%s", 0, cap_string
);
394 for (cp
= cert
.buf
; cp
< cert
.buf
+ cert
.len
; cp
= np
) {
395 np
= next_line(cp
, cert
.buf
+ cert
.len
- cp
);
396 packet_buf_write(req_buf
,
397 "%.*s", (int)(np
- cp
), cp
);
399 packet_buf_write(req_buf
, "push-cert-end\n");
402 free(signing_key_id
);
404 strbuf_release(&cert
);
408 #define NONCE_LEN_LIMIT 256
410 static void reject_invalid_nonce(const char *nonce
, int len
)
414 if (NONCE_LEN_LIMIT
<= len
)
415 die("the receiving end asked to sign an invalid nonce <%.*s>",
418 for (i
= 0; i
< len
; i
++) {
419 int ch
= nonce
[i
] & 0xFF;
421 ch
== '-' || ch
== '.' ||
422 ch
== '/' || ch
== '+' ||
423 ch
== '=' || ch
== '_')
425 die("the receiving end asked to sign an invalid nonce <%.*s>",
430 static void get_commons_through_negotiation(const char *url
,
431 const struct ref
*remote_refs
,
432 struct oid_array
*commons
)
434 struct child_process child
= CHILD_PROCESS_INIT
;
435 const struct ref
*ref
;
436 int len
= the_hash_algo
->hexsz
+ 1; /* hash + NL */
437 int nr_negotiation_tip
= 0;
442 strvec_pushl(&child
.args
, "fetch", "--negotiate-only", NULL
);
443 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
444 if (!is_null_oid(&ref
->new_oid
)) {
445 strvec_pushf(&child
.args
, "--negotiation-tip=%s",
446 oid_to_hex(&ref
->new_oid
));
447 nr_negotiation_tip
++;
450 strvec_push(&child
.args
, url
);
452 if (!nr_negotiation_tip
) {
453 child_process_clear(&child
);
457 if (start_command(&child
))
458 die(_("send-pack: unable to fork off fetch subprocess"));
461 char hex_hash
[GIT_MAX_HEXSZ
+ 1];
462 int read_len
= read_in_full(child
.out
, hex_hash
, len
);
463 struct object_id oid
;
469 die("invalid length read %d", read_len
);
470 if (parse_oid_hex(hex_hash
, &oid
, &end
) || *end
!= '\n')
472 oid_array_append(commons
, &oid
);
475 if (finish_command(&child
)) {
477 * The information that push negotiation provides is useful but
480 warning(_("push negotiation failed; proceeding anyway with push"));
484 int send_pack(struct send_pack_args
*args
,
485 int fd
[], struct child_process
*conn
,
486 struct ref
*remote_refs
,
487 struct oid_array
*extra_have
)
489 struct oid_array commons
= OID_ARRAY_INIT
;
492 struct strbuf req_buf
= STRBUF_INIT
;
493 struct strbuf cap_buf
= STRBUF_INIT
;
495 int need_pack_data
= 0;
496 int allow_deleting_refs
= 0;
497 int status_report
= 0;
498 int use_sideband
= 0;
499 int quiet_supported
= 0;
500 int agent_supported
= 0;
501 int advertise_sid
= 0;
502 int push_negotiate
= 0;
504 int atomic_supported
= 0;
505 int use_push_options
= 0;
506 int push_options_supported
= 0;
507 int object_format_supported
= 0;
508 unsigned cmds_sent
= 0;
511 char *push_cert_nonce
= NULL
;
512 struct packet_reader reader
;
516 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
517 "Perhaps you should specify a branch.\n");
522 git_config_get_bool("push.negotiate", &push_negotiate
);
523 if (push_negotiate
) {
524 trace2_region_enter("send_pack", "push_negotiate", the_repository
);
525 get_commons_through_negotiation(args
->url
, remote_refs
, &commons
);
526 trace2_region_leave("send_pack", "push_negotiate", the_repository
);
529 if (!git_config_get_bool("push.usebitmaps", &use_bitmaps
))
530 args
->disable_bitmaps
= !use_bitmaps
;
532 git_config_get_bool("transfer.advertisesid", &advertise_sid
);
534 /* Does the other end support the reporting? */
535 if (server_supports("report-status-v2"))
537 else if (server_supports("report-status"))
539 if (server_supports("delete-refs"))
540 allow_deleting_refs
= 1;
541 if (server_supports("ofs-delta"))
542 args
->use_ofs_delta
= 1;
543 if (server_supports("side-band-64k"))
545 if (server_supports("quiet"))
547 if (server_supports("agent"))
549 if (!server_supports("session-id"))
551 if (server_supports("no-thin"))
552 args
->use_thin_pack
= 0;
553 if (server_supports("atomic"))
554 atomic_supported
= 1;
555 if (server_supports("push-options"))
556 push_options_supported
= 1;
558 if (!server_supports_hash(the_hash_algo
->name
, &object_format_supported
))
559 die(_("the receiving end does not support this repository's hash algorithm"));
561 if (args
->push_cert
!= SEND_PACK_PUSH_CERT_NEVER
) {
563 const char *nonce
= server_feature_value("push-cert", &len
);
566 reject_invalid_nonce(nonce
, len
);
567 push_cert_nonce
= xmemdupz(nonce
, len
);
568 } else if (args
->push_cert
== SEND_PACK_PUSH_CERT_ALWAYS
) {
569 die(_("the receiving end does not support --signed push"));
570 } else if (args
->push_cert
== SEND_PACK_PUSH_CERT_IF_ASKED
) {
571 warning(_("not sending a push certificate since the"
572 " receiving end does not support --signed"
577 if (args
->atomic
&& !atomic_supported
)
578 die(_("the receiving end does not support --atomic push"));
580 use_atomic
= atomic_supported
&& args
->atomic
;
582 if (args
->push_options
&& !push_options_supported
)
583 die(_("the receiving end does not support push options"));
585 use_push_options
= push_options_supported
&& args
->push_options
;
587 if (status_report
== 1)
588 strbuf_addstr(&cap_buf
, " report-status");
589 else if (status_report
== 2)
590 strbuf_addstr(&cap_buf
, " report-status-v2");
592 strbuf_addstr(&cap_buf
, " side-band-64k");
593 if (quiet_supported
&& (args
->quiet
|| !args
->progress
))
594 strbuf_addstr(&cap_buf
, " quiet");
596 strbuf_addstr(&cap_buf
, " atomic");
597 if (use_push_options
)
598 strbuf_addstr(&cap_buf
, " push-options");
599 if (object_format_supported
)
600 strbuf_addf(&cap_buf
, " object-format=%s", the_hash_algo
->name
);
602 strbuf_addf(&cap_buf
, " agent=%s", git_user_agent_sanitized());
604 strbuf_addf(&cap_buf
, " session-id=%s", trace2_session_id());
607 * NEEDSWORK: why does delete-refs have to be so specific to
608 * send-pack machinery that set_ref_status_for_push() cannot
609 * set this bit for us???
611 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
612 if (ref
->deletion
&& !allow_deleting_refs
)
613 ref
->status
= REF_STATUS_REJECT_NODELETE
;
616 * Clear the status for each ref and see if we need to send
619 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
620 switch (check_to_send_update(ref
, args
)) {
621 case 0: /* no error */
623 case CHECK_REF_STATUS_REJECTED
:
625 * When we know the server would reject a ref update if
626 * we were to send it and we're trying to send the refs
627 * atomically, abort the whole operation.
630 reject_atomic_push(remote_refs
, args
->send_mirror
);
631 error("atomic push failed for ref %s. status: %d",
632 ref
->name
, ref
->status
);
633 ret
= args
->porcelain
? 0 : -1;
636 /* else fallthrough */
643 if (args
->dry_run
|| !status_report
)
644 ref
->status
= REF_STATUS_OK
;
646 ref
->status
= REF_STATUS_EXPECTING_REPORT
;
650 advertise_shallow_grafts_buf(&req_buf
);
653 * Finally, tell the other end!
655 if (!args
->dry_run
&& push_cert_nonce
) {
656 cmds_sent
= generate_push_cert(&req_buf
, remote_refs
, args
,
657 cap_buf
.buf
, push_cert_nonce
);
658 trace2_printf("Generated push certificate");
659 } else if (!args
->dry_run
) {
660 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
661 char *old_hex
, *new_hex
;
663 if (check_to_send_update(ref
, args
) < 0)
666 old_hex
= oid_to_hex(&ref
->old_oid
);
667 new_hex
= oid_to_hex(&ref
->new_oid
);
669 packet_buf_write(&req_buf
,
671 old_hex
, new_hex
, ref
->name
, 0,
675 packet_buf_write(&req_buf
, "%s %s %s",
676 old_hex
, new_hex
, ref
->name
);
681 if (use_push_options
) {
682 struct string_list_item
*item
;
684 packet_buf_flush(&req_buf
);
685 for_each_string_list_item(item
, args
->push_options
)
686 packet_buf_write(&req_buf
, "%s", item
->string
);
689 if (args
->stateless_rpc
) {
690 if (!args
->dry_run
&& (cmds_sent
|| is_repository_shallow(the_repository
))) {
691 packet_buf_flush(&req_buf
);
692 send_sideband(out
, -1, req_buf
.buf
, req_buf
.len
, LARGE_PACKET_MAX
);
695 write_or_die(out
, req_buf
.buf
, req_buf
.len
);
699 if (use_sideband
&& cmds_sent
) {
700 memset(&demux
, 0, sizeof(demux
));
701 demux
.proc
= sideband_demux
;
704 demux
.isolate_sigpipe
= 1;
705 if (start_async(&demux
))
706 die("send-pack: unable to fork off sideband demultiplexer");
710 packet_reader_init(&reader
, in
, NULL
, 0,
711 PACKET_READ_CHOMP_NEWLINE
|
712 PACKET_READ_DIE_ON_ERR_PACKET
);
714 if (need_pack_data
&& cmds_sent
) {
715 if (pack_objects(out
, remote_refs
, extra_have
, &commons
, args
) < 0) {
716 if (args
->stateless_rpc
)
718 if (git_connection_is_socket(conn
))
719 shutdown(fd
[0], SHUT_WR
);
722 * Do not even bother with the return value; we know we
723 * are failing, and just want the error() side effects,
724 * as well as marking refs with their remote status (if
728 receive_status(&reader
, remote_refs
);
732 finish_async(&demux
);
739 if (!args
->stateless_rpc
)
740 /* Closed by pack_objects() via start_command() */
743 if (args
->stateless_rpc
&& cmds_sent
)
746 if (status_report
&& cmds_sent
)
747 ret
= receive_status(&reader
, remote_refs
);
750 if (args
->stateless_rpc
)
753 if (use_sideband
&& cmds_sent
) {
755 if (finish_async(&demux
)) {
756 error("error in sideband demultiplexer");
764 if (args
->porcelain
) {
769 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
770 switch (ref
->status
) {
771 case REF_STATUS_NONE
:
772 case REF_STATUS_UPTODATE
:
784 oid_array_clear(&commons
);
785 strbuf_release(&req_buf
);
786 strbuf_release(&cap_buf
);
787 free(push_cert_nonce
);