4 #include "run-command.h"
9 #include "string-list.h"
10 #include "thread-utils.h"
15 #include "transport-internal.h"
22 struct child_process
*helper
;
31 stateless_connect
: 1,
33 check_connectivity
: 1,
34 no_disconnect_req
: 1,
35 no_private_update
: 1,
39 * As an optimization, the transport code may invoke fetch before
40 * get_refs_list. If this happens, and if the transport helper doesn't
41 * support connect or stateless_connect, we need to invoke
42 * get_refs_list ourselves if we haven't already done so. Keep track of
43 * whether we have invoked get_refs_list.
45 unsigned get_refs_list_called
: 1;
49 /* These go from remote name (as in "list") to private name */
51 /* Transport options for fetch-pack/send-pack (should one of
54 struct git_transport_options transport_options
;
57 static void sendline(struct helper_data
*helper
, struct strbuf
*buffer
)
60 fprintf(stderr
, "Debug: Remote helper: -> %s", buffer
->buf
);
61 if (write_in_full(helper
->helper
->in
, buffer
->buf
, buffer
->len
) < 0)
62 die_errno(_("full write to remote helper failed"));
65 static int recvline_fh(FILE *helper
, struct strbuf
*buffer
)
69 fprintf(stderr
, "Debug: Remote helper: Waiting...\n");
70 if (strbuf_getline(buffer
, helper
) == EOF
) {
72 fprintf(stderr
, "Debug: Remote helper quit.\n");
77 fprintf(stderr
, "Debug: Remote helper: <- %s\n", buffer
->buf
);
81 static int recvline(struct helper_data
*helper
, struct strbuf
*buffer
)
83 return recvline_fh(helper
->out
, buffer
);
86 static void write_constant(int fd
, const char *str
)
89 fprintf(stderr
, "Debug: Remote helper: -> %s", str
);
90 if (write_in_full(fd
, str
, strlen(str
)) < 0)
91 die_errno(_("full write to remote helper failed"));
94 static const char *remove_ext_force(const char *url
)
97 const char *colon
= strchr(url
, ':');
98 if (colon
&& colon
[1] == ':')
104 static void do_take_over(struct transport
*transport
)
106 struct helper_data
*data
;
107 data
= (struct helper_data
*)transport
->data
;
108 transport_take_over(transport
, data
->helper
);
113 static void standard_options(struct transport
*t
);
115 static struct child_process
*get_helper(struct transport
*transport
)
117 struct helper_data
*data
= transport
->data
;
118 struct strbuf buf
= STRBUF_INIT
;
119 struct child_process
*helper
;
126 helper
= xmalloc(sizeof(*helper
));
127 child_process_init(helper
);
131 strvec_pushf(&helper
->args
, "remote-%s", data
->name
);
132 strvec_push(&helper
->args
, transport
->remote
->name
);
133 strvec_push(&helper
->args
, remove_ext_force(transport
->url
));
135 helper
->silent_exec_failure
= 1;
138 strvec_pushf(&helper
->env
, "%s=%s",
139 GIT_DIR_ENVIRONMENT
, get_git_dir());
141 helper
->trace2_child_class
= helper
->args
.v
[0]; /* "remote-<name>" */
143 code
= start_command(helper
);
144 if (code
< 0 && errno
== ENOENT
)
145 die(_("unable to find remote helper for '%s'"), data
->name
);
149 data
->helper
= helper
;
150 data
->no_disconnect_req
= 0;
151 refspec_init(&data
->rs
, REFSPEC_FETCH
);
154 * Open the output as FILE* so strbuf_getline_*() family of
155 * functions can be used.
156 * Do this with duped fd because fclose() will close the fd,
157 * and stuff like taking over will require the fd to remain.
159 duped
= dup(helper
->out
);
161 die_errno(_("can't dup helper output fd"));
162 data
->out
= xfdopen(duped
, "r");
164 write_constant(helper
->in
, "capabilities\n");
167 const char *capname
, *arg
;
169 if (recvline(data
, &buf
))
175 if (*buf
.buf
== '*') {
176 capname
= buf
.buf
+ 1;
182 fprintf(stderr
, "Debug: Got cap %s\n", capname
);
183 if (!strcmp(capname
, "fetch"))
185 else if (!strcmp(capname
, "option"))
187 else if (!strcmp(capname
, "push"))
189 else if (!strcmp(capname
, "import"))
191 else if (!strcmp(capname
, "bidi-import"))
192 data
->bidi_import
= 1;
193 else if (!strcmp(capname
, "export"))
195 else if (!strcmp(capname
, "check-connectivity"))
196 data
->check_connectivity
= 1;
197 else if (skip_prefix(capname
, "refspec ", &arg
)) {
198 refspec_append(&data
->rs
, arg
);
199 } else if (!strcmp(capname
, "connect")) {
201 } else if (!strcmp(capname
, "stateless-connect")) {
202 data
->stateless_connect
= 1;
203 } else if (!strcmp(capname
, "signed-tags")) {
204 data
->signed_tags
= 1;
205 } else if (skip_prefix(capname
, "export-marks ", &arg
)) {
206 data
->export_marks
= xstrdup(arg
);
207 } else if (skip_prefix(capname
, "import-marks ", &arg
)) {
208 data
->import_marks
= xstrdup(arg
);
209 } else if (starts_with(capname
, "no-private-update")) {
210 data
->no_private_update
= 1;
211 } else if (starts_with(capname
, "object-format")) {
212 data
->object_format
= 1;
213 } else if (mandatory
) {
214 die(_("unknown mandatory capability %s; this remote "
215 "helper probably needs newer version of Git"),
219 if (!data
->rs
.nr
&& (data
->import
|| data
->bidi_import
|| data
->export
)) {
220 warning(_("this remote helper should implement refspec capability"));
222 strbuf_release(&buf
);
224 fprintf(stderr
, "Debug: Capabilities complete.\n");
225 standard_options(transport
);
229 static int disconnect_helper(struct transport
*transport
)
231 struct helper_data
*data
= transport
->data
;
236 fprintf(stderr
, "Debug: Disconnecting.\n");
237 if (!data
->no_disconnect_req
) {
239 * Ignore write errors; there's nothing we can do,
240 * since we're about to close the pipe anyway. And the
241 * most likely error is EPIPE due to the helper dying
242 * to report an error itself.
244 sigchain_push(SIGPIPE
, SIG_IGN
);
245 xwrite(data
->helper
->in
, "\n", 1);
246 sigchain_pop(SIGPIPE
);
248 close(data
->helper
->in
);
249 close(data
->helper
->out
);
251 res
= finish_command(data
->helper
);
252 FREE_AND_NULL(data
->helper
);
257 static const char *unsupported_options
[] = {
258 TRANS_OPT_UPLOADPACK
,
259 TRANS_OPT_RECEIVEPACK
,
264 static const char *boolean_options
[] = {
267 TRANS_OPT_FOLLOWTAGS
,
268 TRANS_OPT_DEEPEN_RELATIVE
271 static int strbuf_set_helper_option(struct helper_data
*data
,
277 if (recvline(data
, buf
))
280 if (!strcmp(buf
->buf
, "ok"))
282 else if (starts_with(buf
->buf
, "error"))
284 else if (!strcmp(buf
->buf
, "unsupported"))
287 warning(_("%s unexpectedly said: '%s'"), data
->name
, buf
->buf
);
293 static int string_list_set_helper_option(struct helper_data
*data
,
295 struct string_list
*list
)
297 struct strbuf buf
= STRBUF_INIT
;
300 for (i
= 0; i
< list
->nr
; i
++) {
301 strbuf_addf(&buf
, "option %s ", name
);
302 quote_c_style(list
->items
[i
].string
, &buf
, NULL
, 0);
303 strbuf_addch(&buf
, '\n');
305 if ((ret
= strbuf_set_helper_option(data
, &buf
)))
309 strbuf_release(&buf
);
313 static int set_helper_option(struct transport
*transport
,
314 const char *name
, const char *value
)
316 struct helper_data
*data
= transport
->data
;
317 struct strbuf buf
= STRBUF_INIT
;
318 int i
, ret
, is_bool
= 0;
320 get_helper(transport
);
325 if (!strcmp(name
, "deepen-not"))
326 return string_list_set_helper_option(data
, name
,
327 (struct string_list
*)value
);
329 for (i
= 0; i
< ARRAY_SIZE(unsupported_options
); i
++) {
330 if (!strcmp(name
, unsupported_options
[i
]))
334 for (i
= 0; i
< ARRAY_SIZE(boolean_options
); i
++) {
335 if (!strcmp(name
, boolean_options
[i
])) {
341 strbuf_addf(&buf
, "option %s ", name
);
343 strbuf_addstr(&buf
, value
? "true" : "false");
345 quote_c_style(value
, &buf
, NULL
, 0);
346 strbuf_addch(&buf
, '\n');
348 ret
= strbuf_set_helper_option(data
, &buf
);
349 strbuf_release(&buf
);
353 static void standard_options(struct transport
*t
)
358 set_helper_option(t
, "progress", t
->progress
? "true" : "false");
360 xsnprintf(buf
, sizeof(buf
), "%d", v
+ 1);
361 set_helper_option(t
, "verbosity", buf
);
364 case TRANSPORT_FAMILY_ALL
:
366 * this is already the default,
367 * do not break old remote helpers by setting "all" here
370 case TRANSPORT_FAMILY_IPV4
:
371 set_helper_option(t
, "family", "ipv4");
373 case TRANSPORT_FAMILY_IPV6
:
374 set_helper_option(t
, "family", "ipv6");
379 static int release_helper(struct transport
*transport
)
382 struct helper_data
*data
= transport
->data
;
383 refspec_clear(&data
->rs
);
384 res
= disconnect_helper(transport
);
385 free(transport
->data
);
389 static int fetch_with_fetch(struct transport
*transport
,
390 int nr_heads
, struct ref
**to_fetch
)
392 struct helper_data
*data
= transport
->data
;
394 struct strbuf buf
= STRBUF_INIT
;
396 for (i
= 0; i
< nr_heads
; i
++) {
397 const struct ref
*posn
= to_fetch
[i
];
398 if (posn
->status
& REF_STATUS_UPTODATE
)
401 strbuf_addf(&buf
, "fetch %s %s\n",
402 oid_to_hex(&posn
->old_oid
),
403 posn
->symref
? posn
->symref
: posn
->name
);
406 strbuf_addch(&buf
, '\n');
407 sendline(data
, &buf
);
412 if (recvline(data
, &buf
))
415 if (skip_prefix(buf
.buf
, "lock ", &name
)) {
416 if (transport
->pack_lockfiles
.nr
)
417 warning(_("%s also locked %s"), data
->name
, name
);
419 string_list_append(&transport
->pack_lockfiles
,
422 else if (data
->check_connectivity
&&
423 data
->transport_options
.check_self_contained_and_connected
&&
424 !strcmp(buf
.buf
, "connectivity-ok"))
425 data
->transport_options
.self_contained_and_connected
= 1;
429 warning(_("%s unexpectedly said: '%s'"), data
->name
, buf
.buf
);
431 strbuf_release(&buf
);
435 static int get_importer(struct transport
*transport
, struct child_process
*fastimport
)
437 struct child_process
*helper
= get_helper(transport
);
438 struct helper_data
*data
= transport
->data
;
439 int cat_blob_fd
, code
;
440 child_process_init(fastimport
);
441 fastimport
->in
= xdup(helper
->out
);
442 strvec_push(&fastimport
->args
, "fast-import");
443 strvec_push(&fastimport
->args
, "--allow-unsafe-features");
444 strvec_push(&fastimport
->args
, debug
? "--stats" : "--quiet");
446 if (data
->bidi_import
) {
447 cat_blob_fd
= xdup(helper
->in
);
448 strvec_pushf(&fastimport
->args
, "--cat-blob-fd=%d", cat_blob_fd
);
450 fastimport
->git_cmd
= 1;
452 code
= start_command(fastimport
);
456 static int get_exporter(struct transport
*transport
,
457 struct child_process
*fastexport
,
458 struct string_list
*revlist_args
)
460 struct helper_data
*data
= transport
->data
;
461 struct child_process
*helper
= get_helper(transport
);
464 child_process_init(fastexport
);
466 /* we need to duplicate helper->in because we want to use it after
467 * fastexport is done with it. */
468 fastexport
->out
= dup(helper
->in
);
469 strvec_push(&fastexport
->args
, "fast-export");
470 strvec_push(&fastexport
->args
, "--use-done-feature");
471 strvec_push(&fastexport
->args
, data
->signed_tags
?
472 "--signed-tags=verbatim" : "--signed-tags=warn-strip");
473 if (data
->export_marks
)
474 strvec_pushf(&fastexport
->args
, "--export-marks=%s.tmp", data
->export_marks
);
475 if (data
->import_marks
)
476 strvec_pushf(&fastexport
->args
, "--import-marks=%s", data
->import_marks
);
478 for (i
= 0; i
< revlist_args
->nr
; i
++)
479 strvec_push(&fastexport
->args
, revlist_args
->items
[i
].string
);
481 fastexport
->git_cmd
= 1;
482 return start_command(fastexport
);
485 static int fetch_with_import(struct transport
*transport
,
486 int nr_heads
, struct ref
**to_fetch
)
488 struct child_process fastimport
;
489 struct helper_data
*data
= transport
->data
;
492 struct strbuf buf
= STRBUF_INIT
;
494 get_helper(transport
);
496 if (get_importer(transport
, &fastimport
))
497 die(_("couldn't run fast-import"));
499 for (i
= 0; i
< nr_heads
; i
++) {
501 if (posn
->status
& REF_STATUS_UPTODATE
)
504 strbuf_addf(&buf
, "import %s\n",
505 posn
->symref
? posn
->symref
: posn
->name
);
506 sendline(data
, &buf
);
510 write_constant(data
->helper
->in
, "\n");
512 * remote-helpers that advertise the bidi-import capability are required to
513 * buffer the complete batch of import commands until this newline before
514 * sending data to fast-import.
515 * These helpers read back data from fast-import on their stdin, which could
516 * be mixed with import commands, otherwise.
519 if (finish_command(&fastimport
))
520 die(_("error while running fast-import"));
523 * The fast-import stream of a remote helper that advertises
524 * the "refspec" capability writes to the refs named after the
525 * right hand side of the first refspec matching each ref we
528 * (If no "refspec" capability was specified, for historical
529 * reasons we default to the equivalent of *:*.)
531 * Store the result in to_fetch[i].old_sha1. Callers such
532 * as "git fetch" can use the value to write feedback to the
533 * terminal, populate FETCH_HEAD, and determine what new value
534 * should be written to peer_ref if the update is a
535 * fast-forward or this is a forced update.
537 for (i
= 0; i
< nr_heads
; i
++) {
538 char *private, *name
;
540 if (posn
->status
& REF_STATUS_UPTODATE
)
542 name
= posn
->symref
? posn
->symref
: posn
->name
;
544 private = apply_refspecs(&data
->rs
, name
);
546 private = xstrdup(name
);
548 if (read_ref(private, &posn
->old_oid
) < 0)
549 die(_("could not read ref %s"), private);
553 strbuf_release(&buf
);
557 static int run_connect(struct transport
*transport
, struct strbuf
*cmdbuf
)
559 struct helper_data
*data
= transport
->data
;
563 struct child_process
*helper
;
565 helper
= get_helper(transport
);
568 * Yes, dup the pipe another time, as we need unbuffered version
569 * of input pipe as FILE*. fclose() closes the underlying fd and
570 * stream buffering only can be changed before first I/O operation
573 duped
= dup(helper
->out
);
575 die_errno(_("can't dup helper output fd"));
576 input
= xfdopen(duped
, "r");
577 setvbuf(input
, NULL
, _IONBF
, 0);
579 sendline(data
, cmdbuf
);
580 if (recvline_fh(input
, cmdbuf
))
583 if (!strcmp(cmdbuf
->buf
, "")) {
584 data
->no_disconnect_req
= 1;
586 fprintf(stderr
, "Debug: Smart transport connection "
589 } else if (!strcmp(cmdbuf
->buf
, "fallback")) {
591 fprintf(stderr
, "Debug: Falling back to dumb "
594 die(_("unknown response to connect: %s"),
602 static int process_connect_service(struct transport
*transport
,
603 const char *name
, const char *exec
)
605 struct helper_data
*data
= transport
->data
;
606 struct strbuf cmdbuf
= STRBUF_INIT
;
610 * Handle --upload-pack and friends. This is fire and forget...
611 * just warn if it fails.
613 if (strcmp(name
, exec
)) {
614 int r
= set_helper_option(transport
, "servpath", exec
);
616 warning(_("setting remote service path not supported by protocol"));
618 warning(_("invalid remote service path"));
622 strbuf_addf(&cmdbuf
, "connect %s\n", name
);
623 ret
= run_connect(transport
, &cmdbuf
);
624 } else if (data
->stateless_connect
&&
625 (get_protocol_version_config() == protocol_v2
) &&
626 !strcmp("git-upload-pack", name
)) {
627 strbuf_addf(&cmdbuf
, "stateless-connect %s\n", name
);
628 ret
= run_connect(transport
, &cmdbuf
);
630 transport
->stateless_rpc
= 1;
633 strbuf_release(&cmdbuf
);
637 static int process_connect(struct transport
*transport
,
640 struct helper_data
*data
= transport
->data
;
644 name
= for_push
? "git-receive-pack" : "git-upload-pack";
646 exec
= data
->transport_options
.receivepack
;
648 exec
= data
->transport_options
.uploadpack
;
650 return process_connect_service(transport
, name
, exec
);
653 static int connect_helper(struct transport
*transport
, const char *name
,
654 const char *exec
, int fd
[2])
656 struct helper_data
*data
= transport
->data
;
658 /* Get_helper so connect is inited. */
659 get_helper(transport
);
661 die(_("operation not supported by protocol"));
663 if (!process_connect_service(transport
, name
, exec
))
664 die(_("can't connect to subservice %s"), name
);
666 fd
[0] = data
->helper
->out
;
667 fd
[1] = data
->helper
->in
;
671 static struct ref
*get_refs_list_using_list(struct transport
*transport
,
674 static int fetch_refs(struct transport
*transport
,
675 int nr_heads
, struct ref
**to_fetch
)
677 struct helper_data
*data
= transport
->data
;
680 get_helper(transport
);
682 if (process_connect(transport
, 0)) {
683 do_take_over(transport
);
684 return transport
->vtable
->fetch_refs(transport
, nr_heads
, to_fetch
);
688 * If we reach here, then the server, the client, and/or the transport
689 * helper does not support protocol v2. --negotiate-only requires
692 if (data
->transport_options
.acked_commits
) {
693 warning(_("--negotiate-only requires protocol v2"));
697 if (!data
->get_refs_list_called
)
698 get_refs_list_using_list(transport
, 0);
701 for (i
= 0; i
< nr_heads
; i
++)
702 if (!(to_fetch
[i
]->status
& REF_STATUS_UPTODATE
))
708 if (data
->check_connectivity
&&
709 data
->transport_options
.check_self_contained_and_connected
)
710 set_helper_option(transport
, "check-connectivity", "true");
712 if (transport
->cloning
)
713 set_helper_option(transport
, "cloning", "true");
715 if (data
->transport_options
.update_shallow
)
716 set_helper_option(transport
, "update-shallow", "true");
718 if (data
->transport_options
.refetch
)
719 set_helper_option(transport
, "refetch", "true");
721 if (data
->transport_options
.filter_options
.choice
) {
722 const char *spec
= expand_list_objects_filter_spec(
723 &data
->transport_options
.filter_options
);
724 set_helper_option(transport
, "filter", spec
);
727 if (data
->transport_options
.negotiation_tips
)
728 warning("Ignoring --negotiation-tip because the protocol does not support it.");
731 return fetch_with_fetch(transport
, nr_heads
, to_fetch
);
734 return fetch_with_import(transport
, nr_heads
, to_fetch
);
739 struct push_update_ref_state
{
741 struct ref_push_report
*report
;
745 static int push_update_ref_status(struct strbuf
*buf
,
746 struct push_update_ref_state
*state
,
747 struct ref
*remote_refs
)
750 int status
, forced
= 0;
752 if (starts_with(buf
->buf
, "option ")) {
753 struct object_id old_oid
, new_oid
;
754 const char *key
, *val
;
757 if (!state
->hint
|| !(state
->report
|| state
->new_report
))
758 die(_("'option' without a matching 'ok/error' directive"));
759 if (state
->new_report
) {
760 if (!state
->hint
->report
) {
761 CALLOC_ARRAY(state
->hint
->report
, 1);
762 state
->report
= state
->hint
->report
;
764 state
->report
= state
->hint
->report
;
765 while (state
->report
->next
)
766 state
->report
= state
->report
->next
;
767 CALLOC_ARRAY(state
->report
->next
, 1);
768 state
->report
= state
->report
->next
;
770 state
->new_report
= 0;
773 p
= strchr(key
, ' ');
777 if (!strcmp(key
, "refname"))
778 state
->report
->ref_name
= xstrdup_or_null(val
);
779 else if (!strcmp(key
, "old-oid") && val
&&
780 !parse_oid_hex(val
, &old_oid
, &val
))
781 state
->report
->old_oid
= oiddup(&old_oid
);
782 else if (!strcmp(key
, "new-oid") && val
&&
783 !parse_oid_hex(val
, &new_oid
, &val
))
784 state
->report
->new_oid
= oiddup(&new_oid
);
785 else if (!strcmp(key
, "forced-update"))
786 state
->report
->forced_update
= 1;
787 /* Not update remote namespace again. */
791 state
->report
= NULL
;
792 state
->new_report
= 0;
794 if (starts_with(buf
->buf
, "ok ")) {
795 status
= REF_STATUS_OK
;
796 refname
= buf
->buf
+ 3;
797 } else if (starts_with(buf
->buf
, "error ")) {
798 status
= REF_STATUS_REMOTE_REJECT
;
799 refname
= buf
->buf
+ 6;
801 die(_("expected ok/error, helper said '%s'"), buf
->buf
);
803 msg
= strchr(refname
, ' ');
805 struct strbuf msg_buf
= STRBUF_INIT
;
809 if (!unquote_c_style(&msg_buf
, msg
, &end
))
810 msg
= strbuf_detach(&msg_buf
, NULL
);
813 strbuf_release(&msg_buf
);
815 if (!strcmp(msg
, "no match")) {
816 status
= REF_STATUS_NONE
;
819 else if (!strcmp(msg
, "up to date")) {
820 status
= REF_STATUS_UPTODATE
;
823 else if (!strcmp(msg
, "non-fast forward")) {
824 status
= REF_STATUS_REJECT_NONFASTFORWARD
;
827 else if (!strcmp(msg
, "already exists")) {
828 status
= REF_STATUS_REJECT_ALREADY_EXISTS
;
831 else if (!strcmp(msg
, "fetch first")) {
832 status
= REF_STATUS_REJECT_FETCH_FIRST
;
835 else if (!strcmp(msg
, "needs force")) {
836 status
= REF_STATUS_REJECT_NEEDS_FORCE
;
839 else if (!strcmp(msg
, "stale info")) {
840 status
= REF_STATUS_REJECT_STALE
;
843 else if (!strcmp(msg
, "remote ref updated since checkout")) {
844 status
= REF_STATUS_REJECT_REMOTE_UPDATED
;
847 else if (!strcmp(msg
, "forced update")) {
851 else if (!strcmp(msg
, "expecting report")) {
852 status
= REF_STATUS_EXPECTING_REPORT
;
858 state
->hint
= find_ref_by_name(state
->hint
, refname
);
860 state
->hint
= find_ref_by_name(remote_refs
, refname
);
862 warning(_("helper reported unexpected status of %s"), refname
);
866 if (state
->hint
->status
!= REF_STATUS_NONE
) {
868 * Earlier, the ref was marked not to be pushed, so ignore the ref
869 * status reported by the remote helper if the latter is 'no match'.
871 if (status
== REF_STATUS_NONE
)
875 if (status
== REF_STATUS_OK
)
876 state
->new_report
= 1;
877 state
->hint
->status
= status
;
878 state
->hint
->forced_update
|= forced
;
879 state
->hint
->remote_status
= msg
;
880 return !(status
== REF_STATUS_OK
);
883 static int push_update_refs_status(struct helper_data
*data
,
884 struct ref
*remote_refs
,
888 struct ref_push_report
*report
;
889 struct strbuf buf
= STRBUF_INIT
;
890 struct push_update_ref_state state
= { remote_refs
, NULL
, 0 };
893 if (recvline(data
, &buf
)) {
894 strbuf_release(&buf
);
899 push_update_ref_status(&buf
, &state
, remote_refs
);
901 strbuf_release(&buf
);
903 if (flags
& TRANSPORT_PUSH_DRY_RUN
|| !data
->rs
.nr
|| data
->no_private_update
)
906 /* propagate back the update to the remote namespace */
907 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
910 if (ref
->status
!= REF_STATUS_OK
)
914 private = apply_refspecs(&data
->rs
, ref
->name
);
917 update_ref("update by helper", private, &(ref
->new_oid
),
921 for (report
= ref
->report
; report
; report
= report
->next
) {
922 private = apply_refspecs(&data
->rs
,
928 update_ref("update by helper", private,
940 static void set_common_push_options(struct transport
*transport
,
941 const char *name
, int flags
)
943 if (flags
& TRANSPORT_PUSH_DRY_RUN
) {
944 if (set_helper_option(transport
, "dry-run", "true") != 0)
945 die(_("helper %s does not support dry-run"), name
);
946 } else if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
) {
947 if (set_helper_option(transport
, TRANS_OPT_PUSH_CERT
, "true") != 0)
948 die(_("helper %s does not support --signed"), name
);
949 } else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
) {
950 if (set_helper_option(transport
, TRANS_OPT_PUSH_CERT
, "if-asked") != 0)
951 die(_("helper %s does not support --signed=if-asked"), name
);
954 if (flags
& TRANSPORT_PUSH_ATOMIC
)
955 if (set_helper_option(transport
, TRANS_OPT_ATOMIC
, "true") != 0)
956 die(_("helper %s does not support --atomic"), name
);
958 if (flags
& TRANSPORT_PUSH_FORCE_IF_INCLUDES
)
959 if (set_helper_option(transport
, TRANS_OPT_FORCE_IF_INCLUDES
, "true") != 0)
960 die(_("helper %s does not support --%s"),
961 name
, TRANS_OPT_FORCE_IF_INCLUDES
);
963 if (flags
& TRANSPORT_PUSH_OPTIONS
) {
964 struct string_list_item
*item
;
965 for_each_string_list_item(item
, transport
->push_options
)
966 if (set_helper_option(transport
, "push-option", item
->string
) != 0)
967 die(_("helper %s does not support 'push-option'"), name
);
971 static int push_refs_with_push(struct transport
*transport
,
972 struct ref
*remote_refs
, int flags
)
974 int force_all
= flags
& TRANSPORT_PUSH_FORCE
;
975 int mirror
= flags
& TRANSPORT_PUSH_MIRROR
;
976 int atomic
= flags
& TRANSPORT_PUSH_ATOMIC
;
977 struct helper_data
*data
= transport
->data
;
978 struct strbuf buf
= STRBUF_INIT
;
980 struct string_list cas_options
= STRING_LIST_INIT_DUP
;
981 struct string_list_item
*cas_option
;
983 get_helper(transport
);
987 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
988 if (!ref
->peer_ref
&& !mirror
)
991 /* Check for statuses set by set_ref_status_for_push() */
992 switch (ref
->status
) {
993 case REF_STATUS_REJECT_NONFASTFORWARD
:
994 case REF_STATUS_REJECT_STALE
:
995 case REF_STATUS_REJECT_ALREADY_EXISTS
:
996 case REF_STATUS_REJECT_REMOTE_UPDATED
:
998 reject_atomic_push(remote_refs
, mirror
);
999 string_list_clear(&cas_options
, 0);
1003 case REF_STATUS_UPTODATE
:
1012 strbuf_addstr(&buf
, "push ");
1013 if (!ref
->deletion
) {
1015 strbuf_addch(&buf
, '+');
1017 strbuf_addstr(&buf
, ref
->peer_ref
->name
);
1019 strbuf_addstr(&buf
, oid_to_hex(&ref
->new_oid
));
1021 strbuf_addch(&buf
, ':');
1022 strbuf_addstr(&buf
, ref
->name
);
1023 strbuf_addch(&buf
, '\n');
1026 * The "--force-with-lease" options without explicit
1027 * values to expect have already been expanded into
1028 * the ref->old_oid_expect[] field; we can ignore
1029 * transport->smart_options->cas altogether and instead
1030 * can enumerate them from the refs.
1032 if (ref
->expect_old_sha1
) {
1033 struct strbuf cas
= STRBUF_INIT
;
1034 strbuf_addf(&cas
, "%s:%s",
1035 ref
->name
, oid_to_hex(&ref
->old_oid_expect
));
1036 string_list_append_nodup(&cas_options
,
1037 strbuf_detach(&cas
, NULL
));
1041 string_list_clear(&cas_options
, 0);
1045 for_each_string_list_item(cas_option
, &cas_options
)
1046 set_helper_option(transport
, "cas", cas_option
->string
);
1047 set_common_push_options(transport
, data
->name
, flags
);
1049 strbuf_addch(&buf
, '\n');
1050 sendline(data
, &buf
);
1051 strbuf_release(&buf
);
1052 string_list_clear(&cas_options
, 0);
1054 return push_update_refs_status(data
, remote_refs
, flags
);
1057 static int push_refs_with_export(struct transport
*transport
,
1058 struct ref
*remote_refs
, int flags
)
1061 struct child_process
*helper
, exporter
;
1062 struct helper_data
*data
= transport
->data
;
1063 struct string_list revlist_args
= STRING_LIST_INIT_DUP
;
1064 struct strbuf buf
= STRBUF_INIT
;
1067 die(_("remote-helper doesn't support push; refspec needed"));
1069 set_common_push_options(transport
, data
->name
, flags
);
1070 if (flags
& TRANSPORT_PUSH_FORCE
) {
1071 if (set_helper_option(transport
, "force", "true") != 0)
1072 warning(_("helper %s does not support 'force'"), data
->name
);
1075 helper
= get_helper(transport
);
1077 write_constant(helper
->in
, "export\n");
1079 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1081 struct object_id oid
;
1083 private = apply_refspecs(&data
->rs
, ref
->name
);
1084 if (private && !get_oid(private, &oid
)) {
1085 strbuf_addf(&buf
, "^%s", private);
1086 string_list_append_nodup(&revlist_args
,
1087 strbuf_detach(&buf
, NULL
));
1088 oidcpy(&ref
->old_oid
, &oid
);
1092 if (ref
->peer_ref
) {
1093 if (strcmp(ref
->name
, ref
->peer_ref
->name
)) {
1094 if (!ref
->deletion
) {
1098 /* Follow symbolic refs (mainly for HEAD). */
1099 name
= resolve_ref_unsafe(ref
->peer_ref
->name
,
1100 RESOLVE_REF_READING
,
1102 if (!name
|| !(flag
& REF_ISSYMREF
))
1103 name
= ref
->peer_ref
->name
;
1105 strbuf_addf(&buf
, "%s:%s", name
, ref
->name
);
1107 strbuf_addf(&buf
, ":%s", ref
->name
);
1109 string_list_append(&revlist_args
, "--refspec");
1110 string_list_append(&revlist_args
, buf
.buf
);
1111 strbuf_release(&buf
);
1114 string_list_append(&revlist_args
, ref
->peer_ref
->name
);
1118 if (get_exporter(transport
, &exporter
, &revlist_args
))
1119 die(_("couldn't run fast-export"));
1121 string_list_clear(&revlist_args
, 1);
1123 if (finish_command(&exporter
))
1124 die(_("error while running fast-export"));
1125 if (push_update_refs_status(data
, remote_refs
, flags
))
1128 if (data
->export_marks
) {
1129 strbuf_addf(&buf
, "%s.tmp", data
->export_marks
);
1130 rename(buf
.buf
, data
->export_marks
);
1131 strbuf_release(&buf
);
1137 static int push_refs(struct transport
*transport
,
1138 struct ref
*remote_refs
, int flags
)
1140 struct helper_data
*data
= transport
->data
;
1142 if (process_connect(transport
, 1)) {
1143 do_take_over(transport
);
1144 return transport
->vtable
->push_refs(transport
, remote_refs
, flags
);
1149 _("No refs in common and none specified; doing nothing.\n"
1150 "Perhaps you should specify a branch.\n"));
1155 return push_refs_with_push(transport
, remote_refs
, flags
);
1158 return push_refs_with_export(transport
, remote_refs
, flags
);
1164 static int has_attribute(const char *attrs
, const char *attr
)
1172 const char *space
= strchrnul(attrs
, ' ');
1173 if (len
== space
- attrs
&& !strncmp(attrs
, attr
, len
))
1181 static struct ref
*get_refs_list(struct transport
*transport
, int for_push
,
1182 struct transport_ls_refs_options
*transport_options
)
1184 get_helper(transport
);
1186 if (process_connect(transport
, for_push
)) {
1187 do_take_over(transport
);
1188 return transport
->vtable
->get_refs_list(transport
, for_push
,
1192 return get_refs_list_using_list(transport
, for_push
);
1195 static struct ref
*get_refs_list_using_list(struct transport
*transport
,
1198 struct helper_data
*data
= transport
->data
;
1199 struct child_process
*helper
;
1200 struct ref
*ret
= NULL
;
1201 struct ref
**tail
= &ret
;
1203 struct strbuf buf
= STRBUF_INIT
;
1205 data
->get_refs_list_called
= 1;
1206 helper
= get_helper(transport
);
1208 if (data
->object_format
) {
1209 write_str_in_full(helper
->in
, "option object-format\n");
1210 if (recvline(data
, &buf
) || strcmp(buf
.buf
, "ok"))
1214 if (data
->push
&& for_push
)
1215 write_str_in_full(helper
->in
, "list for-push\n");
1217 write_str_in_full(helper
->in
, "list\n");
1221 if (recvline(data
, &buf
))
1226 else if (buf
.buf
[0] == ':') {
1228 if (skip_prefix(buf
.buf
, ":object-format ", &value
)) {
1229 int algo
= hash_algo_by_name(value
);
1230 if (algo
== GIT_HASH_UNKNOWN
)
1231 die(_("unsupported object format '%s'"),
1233 transport
->hash_algo
= &hash_algos
[algo
];
1238 eov
= strchr(buf
.buf
, ' ');
1240 die(_("malformed response in ref list: %s"), buf
.buf
);
1241 eon
= strchr(eov
+ 1, ' ');
1245 *tail
= alloc_ref(eov
+ 1);
1246 if (buf
.buf
[0] == '@')
1247 (*tail
)->symref
= xstrdup(buf
.buf
+ 1);
1248 else if (buf
.buf
[0] != '?')
1249 get_oid_hex_algop(buf
.buf
, &(*tail
)->old_oid
, transport
->hash_algo
);
1251 if (has_attribute(eon
+ 1, "unchanged")) {
1252 (*tail
)->status
|= REF_STATUS_UPTODATE
;
1253 if (read_ref((*tail
)->name
, &(*tail
)->old_oid
) < 0)
1254 die(_("could not read ref %s"),
1258 tail
= &((*tail
)->next
);
1261 fprintf(stderr
, "Debug: Read ref listing.\n");
1262 strbuf_release(&buf
);
1264 for (posn
= ret
; posn
; posn
= posn
->next
)
1265 resolve_remote_symref(posn
, ret
);
1270 static int get_bundle_uri(struct transport
*transport
)
1272 get_helper(transport
);
1274 if (process_connect(transport
, 0)) {
1275 do_take_over(transport
);
1276 return transport
->vtable
->get_bundle_uri(transport
);
1282 static struct transport_vtable vtable
= {
1283 .set_option
= set_helper_option
,
1284 .get_refs_list
= get_refs_list
,
1285 .get_bundle_uri
= get_bundle_uri
,
1286 .fetch_refs
= fetch_refs
,
1287 .push_refs
= push_refs
,
1288 .connect
= connect_helper
,
1289 .disconnect
= release_helper
1292 int transport_helper_init(struct transport
*transport
, const char *name
)
1294 struct helper_data
*data
= xcalloc(1, sizeof(*data
));
1297 transport_check_allowed(name
);
1299 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
1302 list_objects_filter_init(&data
->transport_options
.filter_options
);
1304 transport
->data
= data
;
1305 transport
->vtable
= &vtable
;
1306 transport
->smart_options
= &(data
->transport_options
);
1311 * Linux pipes can buffer 65536 bytes at once (and most platforms can
1312 * buffer less), so attempt reads and writes with up to that size.
1314 #define BUFFERSIZE 65536
1315 /* This should be enough to hold debugging message. */
1316 #define PBUFFERSIZE 8192
1318 /* Print bidirectional transfer loop debug message. */
1319 __attribute__((format (printf
, 1, 2)))
1320 static void transfer_debug(const char *fmt
, ...)
1323 * NEEDSWORK: This function is sometimes used from multiple threads, and
1324 * we end up using debug_enabled racily. That "should not matter" since
1325 * we always write the same value, but it's still wrong. This function
1326 * is listed in .tsan-suppressions for the time being.
1330 char msgbuf
[PBUFFERSIZE
];
1331 static int debug_enabled
= -1;
1333 if (debug_enabled
< 0)
1334 debug_enabled
= getenv("GIT_TRANSLOOP_DEBUG") ? 1 : 0;
1338 va_start(args
, fmt
);
1339 vsnprintf(msgbuf
, PBUFFERSIZE
, fmt
, args
);
1341 fprintf(stderr
, "Transfer loop debugging: %s\n", msgbuf
);
1344 /* Stream state: More data may be coming in this direction. */
1345 #define SSTATE_TRANSFERRING 0
1347 * Stream state: No more data coming in this direction, flushing rest of
1350 #define SSTATE_FLUSHING 1
1351 /* Stream state: Transfer in this direction finished. */
1352 #define SSTATE_FINISHED 2
1354 #define STATE_NEEDS_READING(state) ((state) <= SSTATE_TRANSFERRING)
1355 #define STATE_NEEDS_WRITING(state) ((state) <= SSTATE_FLUSHING)
1356 #define STATE_NEEDS_CLOSING(state) ((state) == SSTATE_FLUSHING)
1358 /* Unidirectional transfer. */
1359 struct unidirectional_transfer
{
1364 /* Is source socket? */
1366 /* Is destination socket? */
1368 /* Transfer state (TRANSFERRING/FLUSHING/FINISHED) */
1371 char buf
[BUFFERSIZE
];
1374 /* Name of source. */
1375 const char *src_name
;
1376 /* Name of destination. */
1377 const char *dest_name
;
1380 /* Closes the target (for writing) if transfer has finished. */
1381 static void udt_close_if_finished(struct unidirectional_transfer
*t
)
1383 if (STATE_NEEDS_CLOSING(t
->state
) && !t
->bufuse
) {
1384 t
->state
= SSTATE_FINISHED
;
1385 if (t
->dest_is_sock
)
1386 shutdown(t
->dest
, SHUT_WR
);
1389 transfer_debug("Closed %s.", t
->dest_name
);
1394 * Tries to read data from source into buffer. If buffer is full,
1395 * no data is read. Returns 0 on success, -1 on error.
1397 static int udt_do_read(struct unidirectional_transfer
*t
)
1401 if (t
->bufuse
== BUFFERSIZE
)
1402 return 0; /* No space for more. */
1404 transfer_debug("%s is readable", t
->src_name
);
1405 bytes
= xread(t
->src
, t
->buf
+ t
->bufuse
, BUFFERSIZE
- t
->bufuse
);
1407 error_errno(_("read(%s) failed"), t
->src_name
);
1409 } else if (bytes
== 0) {
1410 transfer_debug("%s EOF (with %i bytes in buffer)",
1411 t
->src_name
, (int)t
->bufuse
);
1412 t
->state
= SSTATE_FLUSHING
;
1413 } else if (bytes
> 0) {
1415 transfer_debug("Read %i bytes from %s (buffer now at %i)",
1416 (int)bytes
, t
->src_name
, (int)t
->bufuse
);
1421 /* Tries to write data from buffer into destination. If buffer is empty,
1422 * no data is written. Returns 0 on success, -1 on error.
1424 static int udt_do_write(struct unidirectional_transfer
*t
)
1429 return 0; /* Nothing to write. */
1431 transfer_debug("%s is writable", t
->dest_name
);
1432 bytes
= xwrite(t
->dest
, t
->buf
, t
->bufuse
);
1434 error_errno(_("write(%s) failed"), t
->dest_name
);
1436 } else if (bytes
> 0) {
1439 memmove(t
->buf
, t
->buf
+ bytes
, t
->bufuse
);
1440 transfer_debug("Wrote %i bytes to %s (buffer now at %i)",
1441 (int)bytes
, t
->dest_name
, (int)t
->bufuse
);
1447 /* State of bidirectional transfer loop. */
1448 struct bidirectional_transfer_state
{
1449 /* Direction from program to git. */
1450 struct unidirectional_transfer ptg
;
1451 /* Direction from git to program. */
1452 struct unidirectional_transfer gtp
;
1455 static void *udt_copy_task_routine(void *udt
)
1457 struct unidirectional_transfer
*t
= (struct unidirectional_transfer
*)udt
;
1458 while (t
->state
!= SSTATE_FINISHED
) {
1459 if (STATE_NEEDS_READING(t
->state
))
1462 if (STATE_NEEDS_WRITING(t
->state
))
1463 if (udt_do_write(t
))
1465 if (STATE_NEEDS_CLOSING(t
->state
))
1466 udt_close_if_finished(t
);
1468 return udt
; /* Just some non-NULL value. */
1474 * Join thread, with appropriate errors on failure. Name is name for the
1475 * thread (for error messages). Returns 0 on success, 1 on failure.
1477 static int tloop_join(pthread_t thread
, const char *name
)
1481 err
= pthread_join(thread
, &tret
);
1483 error(_("%s thread failed"), name
);
1487 error(_("%s thread failed to join: %s"), name
, strerror(err
));
1494 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1497 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1499 pthread_t gtp_thread
;
1500 pthread_t ptg_thread
;
1503 err
= pthread_create(>p_thread
, NULL
, udt_copy_task_routine
,
1506 die(_("can't start thread for copying data: %s"), strerror(err
));
1507 err
= pthread_create(&ptg_thread
, NULL
, udt_copy_task_routine
,
1510 die(_("can't start thread for copying data: %s"), strerror(err
));
1512 ret
|= tloop_join(gtp_thread
, "Git to program copy");
1513 ret
|= tloop_join(ptg_thread
, "Program to git copy");
1518 /* Close the source and target (for writing) for transfer. */
1519 static void udt_kill_transfer(struct unidirectional_transfer
*t
)
1521 t
->state
= SSTATE_FINISHED
;
1523 * Socket read end left open isn't a disaster if nobody
1524 * attempts to read from it (mingw compat headers do not
1527 * We can't fully close the socket since otherwise gtp
1528 * task would first close the socket it sends data to
1529 * while closing the ptg file descriptors.
1531 if (!t
->src_is_sock
)
1533 if (t
->dest_is_sock
)
1534 shutdown(t
->dest
, SHUT_WR
);
1540 * Join process, with appropriate errors on failure. Name is name for the
1541 * process (for error messages). Returns 0 on success, 1 on failure.
1543 static int tloop_join(pid_t pid
, const char *name
)
1546 if (waitpid(pid
, &tret
, 0) < 0) {
1547 error_errno(_("%s process failed to wait"), name
);
1550 if (!WIFEXITED(tret
) || WEXITSTATUS(tret
)) {
1551 error(_("%s process failed"), name
);
1558 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1561 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1566 /* Fork thread #1: git to program. */
1569 die_errno(_("can't start thread for copying data"));
1570 else if (pid1
== 0) {
1571 udt_kill_transfer(&s
->ptg
);
1572 exit(udt_copy_task_routine(&s
->gtp
) ? 0 : 1);
1575 /* Fork thread #2: program to git. */
1578 die_errno(_("can't start thread for copying data"));
1579 else if (pid2
== 0) {
1580 udt_kill_transfer(&s
->gtp
);
1581 exit(udt_copy_task_routine(&s
->ptg
) ? 0 : 1);
1585 * Close both streams in parent as to not interfere with
1586 * end of file detection and wait for both tasks to finish.
1588 udt_kill_transfer(&s
->gtp
);
1589 udt_kill_transfer(&s
->ptg
);
1590 ret
|= tloop_join(pid1
, "Git to program copy");
1591 ret
|= tloop_join(pid2
, "Program to git copy");
1597 * Copies data from stdin to output and from input to stdout simultaneously.
1598 * Additionally filtering through given filter. If filter is NULL, uses
1601 int bidirectional_transfer_loop(int input
, int output
)
1603 struct bidirectional_transfer_state state
;
1605 /* Fill the state fields. */
1606 state
.ptg
.src
= input
;
1608 state
.ptg
.src_is_sock
= (input
== output
);
1609 state
.ptg
.dest_is_sock
= 0;
1610 state
.ptg
.state
= SSTATE_TRANSFERRING
;
1611 state
.ptg
.bufuse
= 0;
1612 state
.ptg
.src_name
= "remote input";
1613 state
.ptg
.dest_name
= "stdout";
1616 state
.gtp
.dest
= output
;
1617 state
.gtp
.src_is_sock
= 0;
1618 state
.gtp
.dest_is_sock
= (input
== output
);
1619 state
.gtp
.state
= SSTATE_TRANSFERRING
;
1620 state
.gtp
.bufuse
= 0;
1621 state
.gtp
.src_name
= "stdin";
1622 state
.gtp
.dest_name
= "remote output";
1624 return tloop_spawnwait_tasks(&state
);
1627 void reject_atomic_push(struct ref
*remote_refs
, int mirror_mode
)
1631 /* Mark other refs as failed */
1632 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1633 if (!ref
->peer_ref
&& !mirror_mode
)
1636 switch (ref
->status
) {
1637 case REF_STATUS_NONE
:
1639 case REF_STATUS_EXPECTING_REPORT
:
1640 ref
->status
= REF_STATUS_ATOMIC_PUSH_FAILED
;
1643 break; /* do nothing */