4 #include "run-command.h"
9 #include "string-list.h"
10 #include "thread-utils.h"
12 #include "argv-array.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;
38 * As an optimization, the transport code may invoke fetch before
39 * get_refs_list. If this happens, and if the transport helper doesn't
40 * support connect or stateless_connect, we need to invoke
41 * get_refs_list ourselves if we haven't already done so. Keep track of
42 * whether we have invoked get_refs_list.
44 unsigned get_refs_list_called
: 1;
48 /* These go from remote name (as in "list") to private name */
50 /* Transport options for fetch-pack/send-pack (should one of
53 struct git_transport_options transport_options
;
56 static void sendline(struct helper_data
*helper
, struct strbuf
*buffer
)
59 fprintf(stderr
, "Debug: Remote helper: -> %s", buffer
->buf
);
60 if (write_in_full(helper
->helper
->in
, buffer
->buf
, buffer
->len
) < 0)
61 die_errno(_("full write to remote helper failed"));
64 static int recvline_fh(FILE *helper
, struct strbuf
*buffer
)
68 fprintf(stderr
, "Debug: Remote helper: Waiting...\n");
69 if (strbuf_getline(buffer
, helper
) == EOF
) {
71 fprintf(stderr
, "Debug: Remote helper quit.\n");
76 fprintf(stderr
, "Debug: Remote helper: <- %s\n", buffer
->buf
);
80 static int recvline(struct helper_data
*helper
, struct strbuf
*buffer
)
82 return recvline_fh(helper
->out
, buffer
);
85 static void write_constant(int fd
, const char *str
)
88 fprintf(stderr
, "Debug: Remote helper: -> %s", str
);
89 if (write_in_full(fd
, str
, strlen(str
)) < 0)
90 die_errno(_("full write to remote helper failed"));
93 static const char *remove_ext_force(const char *url
)
96 const char *colon
= strchr(url
, ':');
97 if (colon
&& colon
[1] == ':')
103 static void do_take_over(struct transport
*transport
)
105 struct helper_data
*data
;
106 data
= (struct helper_data
*)transport
->data
;
107 transport_take_over(transport
, data
->helper
);
112 static void standard_options(struct transport
*t
);
114 static struct child_process
*get_helper(struct transport
*transport
)
116 struct helper_data
*data
= transport
->data
;
117 struct strbuf buf
= STRBUF_INIT
;
118 struct child_process
*helper
;
125 helper
= xmalloc(sizeof(*helper
));
126 child_process_init(helper
);
130 argv_array_pushf(&helper
->args
, "git-remote-%s", data
->name
);
131 argv_array_push(&helper
->args
, transport
->remote
->name
);
132 argv_array_push(&helper
->args
, remove_ext_force(transport
->url
));
134 helper
->silent_exec_failure
= 1;
137 argv_array_pushf(&helper
->env_array
, "%s=%s",
138 GIT_DIR_ENVIRONMENT
, get_git_dir());
140 helper
->trace2_child_class
= helper
->args
.argv
[0]; /* "remote-<name>" */
142 code
= start_command(helper
);
143 if (code
< 0 && errno
== ENOENT
)
144 die(_("unable to find remote helper for '%s'"), data
->name
);
148 data
->helper
= helper
;
149 data
->no_disconnect_req
= 0;
150 refspec_init(&data
->rs
, REFSPEC_FETCH
);
153 * Open the output as FILE* so strbuf_getline_*() family of
154 * functions can be used.
155 * Do this with duped fd because fclose() will close the fd,
156 * and stuff like taking over will require the fd to remain.
158 duped
= dup(helper
->out
);
160 die_errno(_("can't dup helper output fd"));
161 data
->out
= xfdopen(duped
, "r");
163 write_constant(helper
->in
, "capabilities\n");
166 const char *capname
, *arg
;
168 if (recvline(data
, &buf
))
174 if (*buf
.buf
== '*') {
175 capname
= buf
.buf
+ 1;
181 fprintf(stderr
, "Debug: Got cap %s\n", capname
);
182 if (!strcmp(capname
, "fetch"))
184 else if (!strcmp(capname
, "option"))
186 else if (!strcmp(capname
, "push"))
188 else if (!strcmp(capname
, "import"))
190 else if (!strcmp(capname
, "bidi-import"))
191 data
->bidi_import
= 1;
192 else if (!strcmp(capname
, "export"))
194 else if (!strcmp(capname
, "check-connectivity"))
195 data
->check_connectivity
= 1;
196 else if (skip_prefix(capname
, "refspec ", &arg
)) {
197 refspec_append(&data
->rs
, arg
);
198 } else if (!strcmp(capname
, "connect")) {
200 } else if (!strcmp(capname
, "stateless-connect")) {
201 data
->stateless_connect
= 1;
202 } else if (!strcmp(capname
, "signed-tags")) {
203 data
->signed_tags
= 1;
204 } else if (skip_prefix(capname
, "export-marks ", &arg
)) {
205 data
->export_marks
= xstrdup(arg
);
206 } else if (skip_prefix(capname
, "import-marks ", &arg
)) {
207 data
->import_marks
= xstrdup(arg
);
208 } else if (starts_with(capname
, "no-private-update")) {
209 data
->no_private_update
= 1;
210 } else if (mandatory
) {
211 die(_("unknown mandatory capability %s; this remote "
212 "helper probably needs newer version of Git"),
216 if (!data
->rs
.nr
&& (data
->import
|| data
->bidi_import
|| data
->export
)) {
217 warning(_("this remote helper should implement refspec capability"));
219 strbuf_release(&buf
);
221 fprintf(stderr
, "Debug: Capabilities complete.\n");
222 standard_options(transport
);
226 static int disconnect_helper(struct transport
*transport
)
228 struct helper_data
*data
= transport
->data
;
233 fprintf(stderr
, "Debug: Disconnecting.\n");
234 if (!data
->no_disconnect_req
) {
236 * Ignore write errors; there's nothing we can do,
237 * since we're about to close the pipe anyway. And the
238 * most likely error is EPIPE due to the helper dying
239 * to report an error itself.
241 sigchain_push(SIGPIPE
, SIG_IGN
);
242 xwrite(data
->helper
->in
, "\n", 1);
243 sigchain_pop(SIGPIPE
);
245 close(data
->helper
->in
);
246 close(data
->helper
->out
);
248 res
= finish_command(data
->helper
);
249 FREE_AND_NULL(data
->helper
);
254 static const char *unsupported_options
[] = {
255 TRANS_OPT_UPLOADPACK
,
256 TRANS_OPT_RECEIVEPACK
,
261 static const char *boolean_options
[] = {
264 TRANS_OPT_FOLLOWTAGS
,
265 TRANS_OPT_DEEPEN_RELATIVE
268 static int strbuf_set_helper_option(struct helper_data
*data
,
274 if (recvline(data
, buf
))
277 if (!strcmp(buf
->buf
, "ok"))
279 else if (starts_with(buf
->buf
, "error"))
281 else if (!strcmp(buf
->buf
, "unsupported"))
284 warning(_("%s unexpectedly said: '%s'"), data
->name
, buf
->buf
);
290 static int string_list_set_helper_option(struct helper_data
*data
,
292 struct string_list
*list
)
294 struct strbuf buf
= STRBUF_INIT
;
297 for (i
= 0; i
< list
->nr
; i
++) {
298 strbuf_addf(&buf
, "option %s ", name
);
299 quote_c_style(list
->items
[i
].string
, &buf
, NULL
, 0);
300 strbuf_addch(&buf
, '\n');
302 if ((ret
= strbuf_set_helper_option(data
, &buf
)))
306 strbuf_release(&buf
);
310 static int set_helper_option(struct transport
*transport
,
311 const char *name
, const char *value
)
313 struct helper_data
*data
= transport
->data
;
314 struct strbuf buf
= STRBUF_INIT
;
315 int i
, ret
, is_bool
= 0;
317 get_helper(transport
);
322 if (!strcmp(name
, "deepen-not"))
323 return string_list_set_helper_option(data
, name
,
324 (struct string_list
*)value
);
326 for (i
= 0; i
< ARRAY_SIZE(unsupported_options
); i
++) {
327 if (!strcmp(name
, unsupported_options
[i
]))
331 for (i
= 0; i
< ARRAY_SIZE(boolean_options
); i
++) {
332 if (!strcmp(name
, boolean_options
[i
])) {
338 strbuf_addf(&buf
, "option %s ", name
);
340 strbuf_addstr(&buf
, value
? "true" : "false");
342 quote_c_style(value
, &buf
, NULL
, 0);
343 strbuf_addch(&buf
, '\n');
345 ret
= strbuf_set_helper_option(data
, &buf
);
346 strbuf_release(&buf
);
350 static void standard_options(struct transport
*t
)
355 set_helper_option(t
, "progress", t
->progress
? "true" : "false");
357 xsnprintf(buf
, sizeof(buf
), "%d", v
+ 1);
358 set_helper_option(t
, "verbosity", buf
);
361 case TRANSPORT_FAMILY_ALL
:
363 * this is already the default,
364 * do not break old remote helpers by setting "all" here
367 case TRANSPORT_FAMILY_IPV4
:
368 set_helper_option(t
, "family", "ipv4");
370 case TRANSPORT_FAMILY_IPV6
:
371 set_helper_option(t
, "family", "ipv6");
376 static int release_helper(struct transport
*transport
)
379 struct helper_data
*data
= transport
->data
;
380 refspec_clear(&data
->rs
);
381 res
= disconnect_helper(transport
);
382 free(transport
->data
);
386 static int fetch_with_fetch(struct transport
*transport
,
387 int nr_heads
, struct ref
**to_fetch
)
389 struct helper_data
*data
= transport
->data
;
391 struct strbuf buf
= STRBUF_INIT
;
393 for (i
= 0; i
< nr_heads
; i
++) {
394 const struct ref
*posn
= to_fetch
[i
];
395 if (posn
->status
& REF_STATUS_UPTODATE
)
398 strbuf_addf(&buf
, "fetch %s %s\n",
399 oid_to_hex(&posn
->old_oid
),
400 posn
->symref
? posn
->symref
: posn
->name
);
403 strbuf_addch(&buf
, '\n');
404 sendline(data
, &buf
);
407 if (recvline(data
, &buf
))
410 if (starts_with(buf
.buf
, "lock ")) {
411 const char *name
= buf
.buf
+ 5;
412 if (transport
->pack_lockfile
)
413 warning(_("%s also locked %s"), data
->name
, name
);
415 transport
->pack_lockfile
= xstrdup(name
);
417 else if (data
->check_connectivity
&&
418 data
->transport_options
.check_self_contained_and_connected
&&
419 !strcmp(buf
.buf
, "connectivity-ok"))
420 data
->transport_options
.self_contained_and_connected
= 1;
424 warning(_("%s unexpectedly said: '%s'"), data
->name
, buf
.buf
);
426 strbuf_release(&buf
);
430 static int get_importer(struct transport
*transport
, struct child_process
*fastimport
)
432 struct child_process
*helper
= get_helper(transport
);
433 struct helper_data
*data
= transport
->data
;
434 int cat_blob_fd
, code
;
435 child_process_init(fastimport
);
436 fastimport
->in
= xdup(helper
->out
);
437 argv_array_push(&fastimport
->args
, "fast-import");
438 argv_array_push(&fastimport
->args
, debug
? "--stats" : "--quiet");
440 if (data
->bidi_import
) {
441 cat_blob_fd
= xdup(helper
->in
);
442 argv_array_pushf(&fastimport
->args
, "--cat-blob-fd=%d", cat_blob_fd
);
444 fastimport
->git_cmd
= 1;
446 code
= start_command(fastimport
);
450 static int get_exporter(struct transport
*transport
,
451 struct child_process
*fastexport
,
452 struct string_list
*revlist_args
)
454 struct helper_data
*data
= transport
->data
;
455 struct child_process
*helper
= get_helper(transport
);
458 child_process_init(fastexport
);
460 /* we need to duplicate helper->in because we want to use it after
461 * fastexport is done with it. */
462 fastexport
->out
= dup(helper
->in
);
463 argv_array_push(&fastexport
->args
, "fast-export");
464 argv_array_push(&fastexport
->args
, "--use-done-feature");
465 argv_array_push(&fastexport
->args
, data
->signed_tags
?
466 "--signed-tags=verbatim" : "--signed-tags=warn-strip");
467 if (data
->export_marks
)
468 argv_array_pushf(&fastexport
->args
, "--export-marks=%s.tmp", data
->export_marks
);
469 if (data
->import_marks
)
470 argv_array_pushf(&fastexport
->args
, "--import-marks=%s", data
->import_marks
);
472 for (i
= 0; i
< revlist_args
->nr
; i
++)
473 argv_array_push(&fastexport
->args
, revlist_args
->items
[i
].string
);
475 fastexport
->git_cmd
= 1;
476 return start_command(fastexport
);
479 static int fetch_with_import(struct transport
*transport
,
480 int nr_heads
, struct ref
**to_fetch
)
482 struct child_process fastimport
;
483 struct helper_data
*data
= transport
->data
;
486 struct strbuf buf
= STRBUF_INIT
;
488 get_helper(transport
);
490 if (get_importer(transport
, &fastimport
))
491 die(_("couldn't run fast-import"));
493 for (i
= 0; i
< nr_heads
; i
++) {
495 if (posn
->status
& REF_STATUS_UPTODATE
)
498 strbuf_addf(&buf
, "import %s\n",
499 posn
->symref
? posn
->symref
: posn
->name
);
500 sendline(data
, &buf
);
504 write_constant(data
->helper
->in
, "\n");
506 * remote-helpers that advertise the bidi-import capability are required to
507 * buffer the complete batch of import commands until this newline before
508 * sending data to fast-import.
509 * These helpers read back data from fast-import on their stdin, which could
510 * be mixed with import commands, otherwise.
513 if (finish_command(&fastimport
))
514 die(_("error while running fast-import"));
517 * The fast-import stream of a remote helper that advertises
518 * the "refspec" capability writes to the refs named after the
519 * right hand side of the first refspec matching each ref we
522 * (If no "refspec" capability was specified, for historical
523 * reasons we default to the equivalent of *:*.)
525 * Store the result in to_fetch[i].old_sha1. Callers such
526 * as "git fetch" can use the value to write feedback to the
527 * terminal, populate FETCH_HEAD, and determine what new value
528 * should be written to peer_ref if the update is a
529 * fast-forward or this is a forced update.
531 for (i
= 0; i
< nr_heads
; i
++) {
532 char *private, *name
;
534 if (posn
->status
& REF_STATUS_UPTODATE
)
536 name
= posn
->symref
? posn
->symref
: posn
->name
;
538 private = apply_refspecs(&data
->rs
, name
);
540 private = xstrdup(name
);
542 if (read_ref(private, &posn
->old_oid
) < 0)
543 die(_("could not read ref %s"), private);
547 strbuf_release(&buf
);
551 static int run_connect(struct transport
*transport
, struct strbuf
*cmdbuf
)
553 struct helper_data
*data
= transport
->data
;
557 struct child_process
*helper
;
559 helper
= get_helper(transport
);
562 * Yes, dup the pipe another time, as we need unbuffered version
563 * of input pipe as FILE*. fclose() closes the underlying fd and
564 * stream buffering only can be changed before first I/O operation
567 duped
= dup(helper
->out
);
569 die_errno(_("can't dup helper output fd"));
570 input
= xfdopen(duped
, "r");
571 setvbuf(input
, NULL
, _IONBF
, 0);
573 sendline(data
, cmdbuf
);
574 if (recvline_fh(input
, cmdbuf
))
577 if (!strcmp(cmdbuf
->buf
, "")) {
578 data
->no_disconnect_req
= 1;
580 fprintf(stderr
, "Debug: Smart transport connection "
583 } else if (!strcmp(cmdbuf
->buf
, "fallback")) {
585 fprintf(stderr
, "Debug: Falling back to dumb "
588 die(_("unknown response to connect: %s"),
596 static int process_connect_service(struct transport
*transport
,
597 const char *name
, const char *exec
)
599 struct helper_data
*data
= transport
->data
;
600 struct strbuf cmdbuf
= STRBUF_INIT
;
604 * Handle --upload-pack and friends. This is fire and forget...
605 * just warn if it fails.
607 if (strcmp(name
, exec
)) {
608 int r
= set_helper_option(transport
, "servpath", exec
);
610 warning(_("setting remote service path not supported by protocol"));
612 warning(_("invalid remote service path"));
616 strbuf_addf(&cmdbuf
, "connect %s\n", name
);
617 ret
= run_connect(transport
, &cmdbuf
);
618 } else if (data
->stateless_connect
&&
619 (get_protocol_version_config() == protocol_v2
) &&
620 !strcmp("git-upload-pack", name
)) {
621 strbuf_addf(&cmdbuf
, "stateless-connect %s\n", name
);
622 ret
= run_connect(transport
, &cmdbuf
);
624 transport
->stateless_rpc
= 1;
627 strbuf_release(&cmdbuf
);
631 static int process_connect(struct transport
*transport
,
634 struct helper_data
*data
= transport
->data
;
638 name
= for_push
? "git-receive-pack" : "git-upload-pack";
640 exec
= data
->transport_options
.receivepack
;
642 exec
= data
->transport_options
.uploadpack
;
644 return process_connect_service(transport
, name
, exec
);
647 static int connect_helper(struct transport
*transport
, const char *name
,
648 const char *exec
, int fd
[2])
650 struct helper_data
*data
= transport
->data
;
652 /* Get_helper so connect is inited. */
653 get_helper(transport
);
655 die(_("operation not supported by protocol"));
657 if (!process_connect_service(transport
, name
, exec
))
658 die(_("can't connect to subservice %s"), name
);
660 fd
[0] = data
->helper
->out
;
661 fd
[1] = data
->helper
->in
;
665 static struct ref
*get_refs_list_using_list(struct transport
*transport
,
668 static int fetch(struct transport
*transport
,
669 int nr_heads
, struct ref
**to_fetch
)
671 struct helper_data
*data
= transport
->data
;
674 get_helper(transport
);
676 if (process_connect(transport
, 0)) {
677 do_take_over(transport
);
678 return transport
->vtable
->fetch(transport
, nr_heads
, to_fetch
);
681 if (!data
->get_refs_list_called
)
682 get_refs_list_using_list(transport
, 0);
685 for (i
= 0; i
< nr_heads
; i
++)
686 if (!(to_fetch
[i
]->status
& REF_STATUS_UPTODATE
))
692 if (data
->check_connectivity
&&
693 data
->transport_options
.check_self_contained_and_connected
)
694 set_helper_option(transport
, "check-connectivity", "true");
696 if (transport
->cloning
)
697 set_helper_option(transport
, "cloning", "true");
699 if (data
->transport_options
.update_shallow
)
700 set_helper_option(transport
, "update-shallow", "true");
702 if (data
->transport_options
.filter_options
.choice
) {
703 const char *spec
= expand_list_objects_filter_spec(
704 &data
->transport_options
.filter_options
);
705 set_helper_option(transport
, "filter", spec
);
708 if (data
->transport_options
.negotiation_tips
)
709 warning("Ignoring --negotiation-tip because the protocol does not support it.");
712 return fetch_with_fetch(transport
, nr_heads
, to_fetch
);
715 return fetch_with_import(transport
, nr_heads
, to_fetch
);
720 static int push_update_ref_status(struct strbuf
*buf
,
722 struct ref
*remote_refs
)
725 int status
, forced
= 0;
727 if (starts_with(buf
->buf
, "ok ")) {
728 status
= REF_STATUS_OK
;
729 refname
= buf
->buf
+ 3;
730 } else if (starts_with(buf
->buf
, "error ")) {
731 status
= REF_STATUS_REMOTE_REJECT
;
732 refname
= buf
->buf
+ 6;
734 die(_("expected ok/error, helper said '%s'"), buf
->buf
);
736 msg
= strchr(refname
, ' ');
738 struct strbuf msg_buf
= STRBUF_INIT
;
742 if (!unquote_c_style(&msg_buf
, msg
, &end
))
743 msg
= strbuf_detach(&msg_buf
, NULL
);
746 strbuf_release(&msg_buf
);
748 if (!strcmp(msg
, "no match")) {
749 status
= REF_STATUS_NONE
;
752 else if (!strcmp(msg
, "up to date")) {
753 status
= REF_STATUS_UPTODATE
;
756 else if (!strcmp(msg
, "non-fast forward")) {
757 status
= REF_STATUS_REJECT_NONFASTFORWARD
;
760 else if (!strcmp(msg
, "already exists")) {
761 status
= REF_STATUS_REJECT_ALREADY_EXISTS
;
764 else if (!strcmp(msg
, "fetch first")) {
765 status
= REF_STATUS_REJECT_FETCH_FIRST
;
768 else if (!strcmp(msg
, "needs force")) {
769 status
= REF_STATUS_REJECT_NEEDS_FORCE
;
772 else if (!strcmp(msg
, "stale info")) {
773 status
= REF_STATUS_REJECT_STALE
;
776 else if (!strcmp(msg
, "forced update")) {
783 *ref
= find_ref_by_name(*ref
, refname
);
785 *ref
= find_ref_by_name(remote_refs
, refname
);
787 warning(_("helper reported unexpected status of %s"), refname
);
791 if ((*ref
)->status
!= REF_STATUS_NONE
) {
793 * Earlier, the ref was marked not to be pushed, so ignore the ref
794 * status reported by the remote helper if the latter is 'no match'.
796 if (status
== REF_STATUS_NONE
)
800 (*ref
)->status
= status
;
801 (*ref
)->forced_update
|= forced
;
802 (*ref
)->remote_status
= msg
;
803 return !(status
== REF_STATUS_OK
);
806 static int push_update_refs_status(struct helper_data
*data
,
807 struct ref
*remote_refs
,
810 struct strbuf buf
= STRBUF_INIT
;
811 struct ref
*ref
= remote_refs
;
817 if (recvline(data
, &buf
)) {
825 if (push_update_ref_status(&buf
, &ref
, remote_refs
))
828 if (flags
& TRANSPORT_PUSH_DRY_RUN
|| !data
->rs
.nr
|| data
->no_private_update
)
831 /* propagate back the update to the remote namespace */
832 private = apply_refspecs(&data
->rs
, ref
->name
);
835 update_ref("update by helper", private, &ref
->new_oid
, NULL
,
839 strbuf_release(&buf
);
843 static void set_common_push_options(struct transport
*transport
,
844 const char *name
, int flags
)
846 if (flags
& TRANSPORT_PUSH_DRY_RUN
) {
847 if (set_helper_option(transport
, "dry-run", "true") != 0)
848 die(_("helper %s does not support dry-run"), name
);
849 } else if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
) {
850 if (set_helper_option(transport
, TRANS_OPT_PUSH_CERT
, "true") != 0)
851 die(_("helper %s does not support --signed"), name
);
852 } else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
) {
853 if (set_helper_option(transport
, TRANS_OPT_PUSH_CERT
, "if-asked") != 0)
854 die(_("helper %s does not support --signed=if-asked"), name
);
857 if (flags
& TRANSPORT_PUSH_OPTIONS
) {
858 struct string_list_item
*item
;
859 for_each_string_list_item(item
, transport
->push_options
)
860 if (set_helper_option(transport
, "push-option", item
->string
) != 0)
861 die(_("helper %s does not support 'push-option'"), name
);
865 static int push_refs_with_push(struct transport
*transport
,
866 struct ref
*remote_refs
, int flags
)
868 int force_all
= flags
& TRANSPORT_PUSH_FORCE
;
869 int mirror
= flags
& TRANSPORT_PUSH_MIRROR
;
870 int atomic
= flags
& TRANSPORT_PUSH_ATOMIC
;
871 struct helper_data
*data
= transport
->data
;
872 struct strbuf buf
= STRBUF_INIT
;
874 struct string_list cas_options
= STRING_LIST_INIT_DUP
;
875 struct string_list_item
*cas_option
;
877 get_helper(transport
);
881 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
882 if (!ref
->peer_ref
&& !mirror
)
885 /* Check for statuses set by set_ref_status_for_push() */
886 switch (ref
->status
) {
887 case REF_STATUS_REJECT_NONFASTFORWARD
:
888 case REF_STATUS_REJECT_STALE
:
889 case REF_STATUS_REJECT_ALREADY_EXISTS
:
891 string_list_clear(&cas_options
, 0);
895 case REF_STATUS_UPTODATE
:
904 strbuf_addstr(&buf
, "push ");
905 if (!ref
->deletion
) {
907 strbuf_addch(&buf
, '+');
909 strbuf_addstr(&buf
, ref
->peer_ref
->name
);
911 strbuf_addstr(&buf
, oid_to_hex(&ref
->new_oid
));
913 strbuf_addch(&buf
, ':');
914 strbuf_addstr(&buf
, ref
->name
);
915 strbuf_addch(&buf
, '\n');
918 * The "--force-with-lease" options without explicit
919 * values to expect have already been expanded into
920 * the ref->old_oid_expect[] field; we can ignore
921 * transport->smart_options->cas altogether and instead
922 * can enumerate them from the refs.
924 if (ref
->expect_old_sha1
) {
925 struct strbuf cas
= STRBUF_INIT
;
926 strbuf_addf(&cas
, "%s:%s",
927 ref
->name
, oid_to_hex(&ref
->old_oid_expect
));
928 string_list_append_nodup(&cas_options
,
929 strbuf_detach(&cas
, NULL
));
933 string_list_clear(&cas_options
, 0);
937 for_each_string_list_item(cas_option
, &cas_options
)
938 set_helper_option(transport
, "cas", cas_option
->string
);
939 set_common_push_options(transport
, data
->name
, flags
);
941 strbuf_addch(&buf
, '\n');
942 sendline(data
, &buf
);
943 strbuf_release(&buf
);
944 string_list_clear(&cas_options
, 0);
946 return push_update_refs_status(data
, remote_refs
, flags
);
949 static int push_refs_with_export(struct transport
*transport
,
950 struct ref
*remote_refs
, int flags
)
953 struct child_process
*helper
, exporter
;
954 struct helper_data
*data
= transport
->data
;
955 struct string_list revlist_args
= STRING_LIST_INIT_DUP
;
956 struct strbuf buf
= STRBUF_INIT
;
959 die(_("remote-helper doesn't support push; refspec needed"));
961 set_common_push_options(transport
, data
->name
, flags
);
962 if (flags
& TRANSPORT_PUSH_FORCE
) {
963 if (set_helper_option(transport
, "force", "true") != 0)
964 warning(_("helper %s does not support 'force'"), data
->name
);
967 helper
= get_helper(transport
);
969 write_constant(helper
->in
, "export\n");
971 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
973 struct object_id oid
;
975 private = apply_refspecs(&data
->rs
, ref
->name
);
976 if (private && !get_oid(private, &oid
)) {
977 strbuf_addf(&buf
, "^%s", private);
978 string_list_append_nodup(&revlist_args
,
979 strbuf_detach(&buf
, NULL
));
980 oidcpy(&ref
->old_oid
, &oid
);
985 if (strcmp(ref
->name
, ref
->peer_ref
->name
)) {
986 if (!ref
->deletion
) {
990 /* Follow symbolic refs (mainly for HEAD). */
991 name
= resolve_ref_unsafe(ref
->peer_ref
->name
,
994 if (!name
|| !(flag
& REF_ISSYMREF
))
995 name
= ref
->peer_ref
->name
;
997 strbuf_addf(&buf
, "%s:%s", name
, ref
->name
);
999 strbuf_addf(&buf
, ":%s", ref
->name
);
1001 string_list_append(&revlist_args
, "--refspec");
1002 string_list_append(&revlist_args
, buf
.buf
);
1003 strbuf_release(&buf
);
1006 string_list_append(&revlist_args
, ref
->peer_ref
->name
);
1010 if (get_exporter(transport
, &exporter
, &revlist_args
))
1011 die(_("couldn't run fast-export"));
1013 string_list_clear(&revlist_args
, 1);
1015 if (finish_command(&exporter
))
1016 die(_("error while running fast-export"));
1017 if (push_update_refs_status(data
, remote_refs
, flags
))
1020 if (data
->export_marks
) {
1021 strbuf_addf(&buf
, "%s.tmp", data
->export_marks
);
1022 rename(buf
.buf
, data
->export_marks
);
1023 strbuf_release(&buf
);
1029 static int push_refs(struct transport
*transport
,
1030 struct ref
*remote_refs
, int flags
)
1032 struct helper_data
*data
= transport
->data
;
1034 if (process_connect(transport
, 1)) {
1035 do_take_over(transport
);
1036 return transport
->vtable
->push_refs(transport
, remote_refs
, flags
);
1041 _("No refs in common and none specified; doing nothing.\n"
1042 "Perhaps you should specify a branch such as 'master'.\n"));
1047 return push_refs_with_push(transport
, remote_refs
, flags
);
1050 return push_refs_with_export(transport
, remote_refs
, flags
);
1056 static int has_attribute(const char *attrs
, const char *attr
)
1064 const char *space
= strchrnul(attrs
, ' ');
1065 if (len
== space
- attrs
&& !strncmp(attrs
, attr
, len
))
1073 static struct ref
*get_refs_list(struct transport
*transport
, int for_push
,
1074 const struct argv_array
*ref_prefixes
)
1076 get_helper(transport
);
1078 if (process_connect(transport
, for_push
)) {
1079 do_take_over(transport
);
1080 return transport
->vtable
->get_refs_list(transport
, for_push
, ref_prefixes
);
1083 return get_refs_list_using_list(transport
, for_push
);
1086 static struct ref
*get_refs_list_using_list(struct transport
*transport
,
1089 struct helper_data
*data
= transport
->data
;
1090 struct child_process
*helper
;
1091 struct ref
*ret
= NULL
;
1092 struct ref
**tail
= &ret
;
1094 struct strbuf buf
= STRBUF_INIT
;
1096 data
->get_refs_list_called
= 1;
1097 helper
= get_helper(transport
);
1099 if (data
->push
&& for_push
)
1100 write_str_in_full(helper
->in
, "list for-push\n");
1102 write_str_in_full(helper
->in
, "list\n");
1106 if (recvline(data
, &buf
))
1112 eov
= strchr(buf
.buf
, ' ');
1114 die(_("malformed response in ref list: %s"), buf
.buf
);
1115 eon
= strchr(eov
+ 1, ' ');
1119 *tail
= alloc_ref(eov
+ 1);
1120 if (buf
.buf
[0] == '@')
1121 (*tail
)->symref
= xstrdup(buf
.buf
+ 1);
1122 else if (buf
.buf
[0] != '?')
1123 get_oid_hex(buf
.buf
, &(*tail
)->old_oid
);
1125 if (has_attribute(eon
+ 1, "unchanged")) {
1126 (*tail
)->status
|= REF_STATUS_UPTODATE
;
1127 if (read_ref((*tail
)->name
, &(*tail
)->old_oid
) < 0)
1128 die(_("could not read ref %s"),
1132 tail
= &((*tail
)->next
);
1135 fprintf(stderr
, "Debug: Read ref listing.\n");
1136 strbuf_release(&buf
);
1138 for (posn
= ret
; posn
; posn
= posn
->next
)
1139 resolve_remote_symref(posn
, ret
);
1144 static struct transport_vtable vtable
= {
1153 int transport_helper_init(struct transport
*transport
, const char *name
)
1155 struct helper_data
*data
= xcalloc(1, sizeof(*data
));
1158 transport_check_allowed(name
);
1160 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
1163 transport
->data
= data
;
1164 transport
->vtable
= &vtable
;
1165 transport
->smart_options
= &(data
->transport_options
);
1170 * Linux pipes can buffer 65536 bytes at once (and most platforms can
1171 * buffer less), so attempt reads and writes with up to that size.
1173 #define BUFFERSIZE 65536
1174 /* This should be enough to hold debugging message. */
1175 #define PBUFFERSIZE 8192
1177 /* Print bidirectional transfer loop debug message. */
1178 __attribute__((format (printf
, 1, 2)))
1179 static void transfer_debug(const char *fmt
, ...)
1182 * NEEDSWORK: This function is sometimes used from multiple threads, and
1183 * we end up using debug_enabled racily. That "should not matter" since
1184 * we always write the same value, but it's still wrong. This function
1185 * is listed in .tsan-suppressions for the time being.
1189 char msgbuf
[PBUFFERSIZE
];
1190 static int debug_enabled
= -1;
1192 if (debug_enabled
< 0)
1193 debug_enabled
= getenv("GIT_TRANSLOOP_DEBUG") ? 1 : 0;
1197 va_start(args
, fmt
);
1198 vsnprintf(msgbuf
, PBUFFERSIZE
, fmt
, args
);
1200 fprintf(stderr
, "Transfer loop debugging: %s\n", msgbuf
);
1203 /* Stream state: More data may be coming in this direction. */
1204 #define SSTATE_TRANSFERRING 0
1206 * Stream state: No more data coming in this direction, flushing rest of
1209 #define SSTATE_FLUSHING 1
1210 /* Stream state: Transfer in this direction finished. */
1211 #define SSTATE_FINISHED 2
1213 #define STATE_NEEDS_READING(state) ((state) <= SSTATE_TRANSFERRING)
1214 #define STATE_NEEDS_WRITING(state) ((state) <= SSTATE_FLUSHING)
1215 #define STATE_NEEDS_CLOSING(state) ((state) == SSTATE_FLUSHING)
1217 /* Unidirectional transfer. */
1218 struct unidirectional_transfer
{
1223 /* Is source socket? */
1225 /* Is destination socket? */
1227 /* Transfer state (TRANSFERRING/FLUSHING/FINISHED) */
1230 char buf
[BUFFERSIZE
];
1233 /* Name of source. */
1234 const char *src_name
;
1235 /* Name of destination. */
1236 const char *dest_name
;
1239 /* Closes the target (for writing) if transfer has finished. */
1240 static void udt_close_if_finished(struct unidirectional_transfer
*t
)
1242 if (STATE_NEEDS_CLOSING(t
->state
) && !t
->bufuse
) {
1243 t
->state
= SSTATE_FINISHED
;
1244 if (t
->dest_is_sock
)
1245 shutdown(t
->dest
, SHUT_WR
);
1248 transfer_debug("Closed %s.", t
->dest_name
);
1253 * Tries to read data from source into buffer. If buffer is full,
1254 * no data is read. Returns 0 on success, -1 on error.
1256 static int udt_do_read(struct unidirectional_transfer
*t
)
1260 if (t
->bufuse
== BUFFERSIZE
)
1261 return 0; /* No space for more. */
1263 transfer_debug("%s is readable", t
->src_name
);
1264 bytes
= xread(t
->src
, t
->buf
+ t
->bufuse
, BUFFERSIZE
- t
->bufuse
);
1266 error_errno(_("read(%s) failed"), t
->src_name
);
1268 } else if (bytes
== 0) {
1269 transfer_debug("%s EOF (with %i bytes in buffer)",
1270 t
->src_name
, (int)t
->bufuse
);
1271 t
->state
= SSTATE_FLUSHING
;
1272 } else if (bytes
> 0) {
1274 transfer_debug("Read %i bytes from %s (buffer now at %i)",
1275 (int)bytes
, t
->src_name
, (int)t
->bufuse
);
1280 /* Tries to write data from buffer into destination. If buffer is empty,
1281 * no data is written. Returns 0 on success, -1 on error.
1283 static int udt_do_write(struct unidirectional_transfer
*t
)
1288 return 0; /* Nothing to write. */
1290 transfer_debug("%s is writable", t
->dest_name
);
1291 bytes
= xwrite(t
->dest
, t
->buf
, t
->bufuse
);
1293 error_errno(_("write(%s) failed"), t
->dest_name
);
1295 } else if (bytes
> 0) {
1298 memmove(t
->buf
, t
->buf
+ bytes
, t
->bufuse
);
1299 transfer_debug("Wrote %i bytes to %s (buffer now at %i)",
1300 (int)bytes
, t
->dest_name
, (int)t
->bufuse
);
1306 /* State of bidirectional transfer loop. */
1307 struct bidirectional_transfer_state
{
1308 /* Direction from program to git. */
1309 struct unidirectional_transfer ptg
;
1310 /* Direction from git to program. */
1311 struct unidirectional_transfer gtp
;
1314 static void *udt_copy_task_routine(void *udt
)
1316 struct unidirectional_transfer
*t
= (struct unidirectional_transfer
*)udt
;
1317 while (t
->state
!= SSTATE_FINISHED
) {
1318 if (STATE_NEEDS_READING(t
->state
))
1321 if (STATE_NEEDS_WRITING(t
->state
))
1322 if (udt_do_write(t
))
1324 if (STATE_NEEDS_CLOSING(t
->state
))
1325 udt_close_if_finished(t
);
1327 return udt
; /* Just some non-NULL value. */
1333 * Join thread, with appropriate errors on failure. Name is name for the
1334 * thread (for error messages). Returns 0 on success, 1 on failure.
1336 static int tloop_join(pthread_t thread
, const char *name
)
1340 err
= pthread_join(thread
, &tret
);
1342 error(_("%s thread failed"), name
);
1346 error(_("%s thread failed to join: %s"), name
, strerror(err
));
1353 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1356 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1358 pthread_t gtp_thread
;
1359 pthread_t ptg_thread
;
1362 err
= pthread_create(>p_thread
, NULL
, udt_copy_task_routine
,
1365 die(_("can't start thread for copying data: %s"), strerror(err
));
1366 err
= pthread_create(&ptg_thread
, NULL
, udt_copy_task_routine
,
1369 die(_("can't start thread for copying data: %s"), strerror(err
));
1371 ret
|= tloop_join(gtp_thread
, "Git to program copy");
1372 ret
|= tloop_join(ptg_thread
, "Program to git copy");
1377 /* Close the source and target (for writing) for transfer. */
1378 static void udt_kill_transfer(struct unidirectional_transfer
*t
)
1380 t
->state
= SSTATE_FINISHED
;
1382 * Socket read end left open isn't a disaster if nobody
1383 * attempts to read from it (mingw compat headers do not
1386 * We can't fully close the socket since otherwise gtp
1387 * task would first close the socket it sends data to
1388 * while closing the ptg file descriptors.
1390 if (!t
->src_is_sock
)
1392 if (t
->dest_is_sock
)
1393 shutdown(t
->dest
, SHUT_WR
);
1399 * Join process, with appropriate errors on failure. Name is name for the
1400 * process (for error messages). Returns 0 on success, 1 on failure.
1402 static int tloop_join(pid_t pid
, const char *name
)
1405 if (waitpid(pid
, &tret
, 0) < 0) {
1406 error_errno(_("%s process failed to wait"), name
);
1409 if (!WIFEXITED(tret
) || WEXITSTATUS(tret
)) {
1410 error(_("%s process failed"), name
);
1417 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1420 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1425 /* Fork thread #1: git to program. */
1428 die_errno(_("can't start thread for copying data"));
1429 else if (pid1
== 0) {
1430 udt_kill_transfer(&s
->ptg
);
1431 exit(udt_copy_task_routine(&s
->gtp
) ? 0 : 1);
1434 /* Fork thread #2: program to git. */
1437 die_errno(_("can't start thread for copying data"));
1438 else if (pid2
== 0) {
1439 udt_kill_transfer(&s
->gtp
);
1440 exit(udt_copy_task_routine(&s
->ptg
) ? 0 : 1);
1444 * Close both streams in parent as to not interfere with
1445 * end of file detection and wait for both tasks to finish.
1447 udt_kill_transfer(&s
->gtp
);
1448 udt_kill_transfer(&s
->ptg
);
1449 ret
|= tloop_join(pid1
, "Git to program copy");
1450 ret
|= tloop_join(pid2
, "Program to git copy");
1456 * Copies data from stdin to output and from input to stdout simultaneously.
1457 * Additionally filtering through given filter. If filter is NULL, uses
1460 int bidirectional_transfer_loop(int input
, int output
)
1462 struct bidirectional_transfer_state state
;
1464 /* Fill the state fields. */
1465 state
.ptg
.src
= input
;
1467 state
.ptg
.src_is_sock
= (input
== output
);
1468 state
.ptg
.dest_is_sock
= 0;
1469 state
.ptg
.state
= SSTATE_TRANSFERRING
;
1470 state
.ptg
.bufuse
= 0;
1471 state
.ptg
.src_name
= "remote input";
1472 state
.ptg
.dest_name
= "stdout";
1475 state
.gtp
.dest
= output
;
1476 state
.gtp
.src_is_sock
= 0;
1477 state
.gtp
.dest_is_sock
= (input
== output
);
1478 state
.gtp
.state
= SSTATE_TRANSFERRING
;
1479 state
.gtp
.bufuse
= 0;
1480 state
.gtp
.src_name
= "stdin";
1481 state
.gtp
.dest_name
= "remote output";
1483 return tloop_spawnwait_tasks(&state
);