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 /* These go from remote name (as in "list") to private name */
40 /* Transport options for fetch-pack/send-pack (should one of
43 struct git_transport_options transport_options
;
46 static void sendline(struct helper_data
*helper
, struct strbuf
*buffer
)
49 fprintf(stderr
, "Debug: Remote helper: -> %s", buffer
->buf
);
50 if (write_in_full(helper
->helper
->in
, buffer
->buf
, buffer
->len
) < 0)
51 die_errno(_("full write to remote helper failed"));
54 static int recvline_fh(FILE *helper
, struct strbuf
*buffer
)
58 fprintf(stderr
, "Debug: Remote helper: Waiting...\n");
59 if (strbuf_getline(buffer
, helper
) == EOF
) {
61 fprintf(stderr
, "Debug: Remote helper quit.\n");
66 fprintf(stderr
, "Debug: Remote helper: <- %s\n", buffer
->buf
);
70 static int recvline(struct helper_data
*helper
, struct strbuf
*buffer
)
72 return recvline_fh(helper
->out
, buffer
);
75 static void write_constant(int fd
, const char *str
)
78 fprintf(stderr
, "Debug: Remote helper: -> %s", str
);
79 if (write_in_full(fd
, str
, strlen(str
)) < 0)
80 die_errno(_("full write to remote helper failed"));
83 static const char *remove_ext_force(const char *url
)
86 const char *colon
= strchr(url
, ':');
87 if (colon
&& colon
[1] == ':')
93 static void do_take_over(struct transport
*transport
)
95 struct helper_data
*data
;
96 data
= (struct helper_data
*)transport
->data
;
97 transport_take_over(transport
, data
->helper
);
102 static void standard_options(struct transport
*t
);
104 static struct child_process
*get_helper(struct transport
*transport
)
106 struct helper_data
*data
= transport
->data
;
107 struct strbuf buf
= STRBUF_INIT
;
108 struct child_process
*helper
;
115 helper
= xmalloc(sizeof(*helper
));
116 child_process_init(helper
);
120 argv_array_pushf(&helper
->args
, "git-remote-%s", data
->name
);
121 argv_array_push(&helper
->args
, transport
->remote
->name
);
122 argv_array_push(&helper
->args
, remove_ext_force(transport
->url
));
124 helper
->silent_exec_failure
= 1;
127 argv_array_pushf(&helper
->env_array
, "%s=%s",
128 GIT_DIR_ENVIRONMENT
, get_git_dir());
130 code
= start_command(helper
);
131 if (code
< 0 && errno
== ENOENT
)
132 die(_("unable to find remote helper for '%s'"), data
->name
);
136 data
->helper
= helper
;
137 data
->no_disconnect_req
= 0;
138 refspec_init(&data
->rs
, REFSPEC_FETCH
);
141 * Open the output as FILE* so strbuf_getline_*() family of
142 * functions can be used.
143 * Do this with duped fd because fclose() will close the fd,
144 * and stuff like taking over will require the fd to remain.
146 duped
= dup(helper
->out
);
148 die_errno(_("can't dup helper output fd"));
149 data
->out
= xfdopen(duped
, "r");
151 write_constant(helper
->in
, "capabilities\n");
154 const char *capname
, *arg
;
156 if (recvline(data
, &buf
))
162 if (*buf
.buf
== '*') {
163 capname
= buf
.buf
+ 1;
169 fprintf(stderr
, "Debug: Got cap %s\n", capname
);
170 if (!strcmp(capname
, "fetch"))
172 else if (!strcmp(capname
, "option"))
174 else if (!strcmp(capname
, "push"))
176 else if (!strcmp(capname
, "import"))
178 else if (!strcmp(capname
, "bidi-import"))
179 data
->bidi_import
= 1;
180 else if (!strcmp(capname
, "export"))
182 else if (!strcmp(capname
, "check-connectivity"))
183 data
->check_connectivity
= 1;
184 else if (skip_prefix(capname
, "refspec ", &arg
)) {
185 refspec_append(&data
->rs
, arg
);
186 } else if (!strcmp(capname
, "connect")) {
188 } else if (!strcmp(capname
, "stateless-connect")) {
189 data
->stateless_connect
= 1;
190 } else if (!strcmp(capname
, "signed-tags")) {
191 data
->signed_tags
= 1;
192 } else if (skip_prefix(capname
, "export-marks ", &arg
)) {
193 data
->export_marks
= xstrdup(arg
);
194 } else if (skip_prefix(capname
, "import-marks ", &arg
)) {
195 data
->import_marks
= xstrdup(arg
);
196 } else if (starts_with(capname
, "no-private-update")) {
197 data
->no_private_update
= 1;
198 } else if (mandatory
) {
199 die(_("unknown mandatory capability %s; this remote "
200 "helper probably needs newer version of Git"),
204 if (!data
->rs
.nr
&& (data
->import
|| data
->bidi_import
|| data
->export
)) {
205 warning(_("this remote helper should implement refspec capability"));
207 strbuf_release(&buf
);
209 fprintf(stderr
, "Debug: Capabilities complete.\n");
210 standard_options(transport
);
214 static int disconnect_helper(struct transport
*transport
)
216 struct helper_data
*data
= transport
->data
;
221 fprintf(stderr
, "Debug: Disconnecting.\n");
222 if (!data
->no_disconnect_req
) {
224 * Ignore write errors; there's nothing we can do,
225 * since we're about to close the pipe anyway. And the
226 * most likely error is EPIPE due to the helper dying
227 * to report an error itself.
229 sigchain_push(SIGPIPE
, SIG_IGN
);
230 xwrite(data
->helper
->in
, "\n", 1);
231 sigchain_pop(SIGPIPE
);
233 close(data
->helper
->in
);
234 close(data
->helper
->out
);
236 res
= finish_command(data
->helper
);
237 FREE_AND_NULL(data
->helper
);
242 static const char *unsupported_options
[] = {
243 TRANS_OPT_UPLOADPACK
,
244 TRANS_OPT_RECEIVEPACK
,
249 static const char *boolean_options
[] = {
252 TRANS_OPT_FOLLOWTAGS
,
253 TRANS_OPT_DEEPEN_RELATIVE
256 static int strbuf_set_helper_option(struct helper_data
*data
,
262 if (recvline(data
, buf
))
265 if (!strcmp(buf
->buf
, "ok"))
267 else if (starts_with(buf
->buf
, "error"))
269 else if (!strcmp(buf
->buf
, "unsupported"))
272 warning(_("%s unexpectedly said: '%s'"), data
->name
, buf
->buf
);
278 static int string_list_set_helper_option(struct helper_data
*data
,
280 struct string_list
*list
)
282 struct strbuf buf
= STRBUF_INIT
;
285 for (i
= 0; i
< list
->nr
; i
++) {
286 strbuf_addf(&buf
, "option %s ", name
);
287 quote_c_style(list
->items
[i
].string
, &buf
, NULL
, 0);
288 strbuf_addch(&buf
, '\n');
290 if ((ret
= strbuf_set_helper_option(data
, &buf
)))
294 strbuf_release(&buf
);
298 static int set_helper_option(struct transport
*transport
,
299 const char *name
, const char *value
)
301 struct helper_data
*data
= transport
->data
;
302 struct strbuf buf
= STRBUF_INIT
;
303 int i
, ret
, is_bool
= 0;
305 get_helper(transport
);
310 if (!strcmp(name
, "deepen-not"))
311 return string_list_set_helper_option(data
, name
,
312 (struct string_list
*)value
);
314 for (i
= 0; i
< ARRAY_SIZE(unsupported_options
); i
++) {
315 if (!strcmp(name
, unsupported_options
[i
]))
319 for (i
= 0; i
< ARRAY_SIZE(boolean_options
); i
++) {
320 if (!strcmp(name
, boolean_options
[i
])) {
326 strbuf_addf(&buf
, "option %s ", name
);
328 strbuf_addstr(&buf
, value
? "true" : "false");
330 quote_c_style(value
, &buf
, NULL
, 0);
331 strbuf_addch(&buf
, '\n');
333 ret
= strbuf_set_helper_option(data
, &buf
);
334 strbuf_release(&buf
);
338 static void standard_options(struct transport
*t
)
343 set_helper_option(t
, "progress", t
->progress
? "true" : "false");
345 xsnprintf(buf
, sizeof(buf
), "%d", v
+ 1);
346 set_helper_option(t
, "verbosity", buf
);
349 case TRANSPORT_FAMILY_ALL
:
351 * this is already the default,
352 * do not break old remote helpers by setting "all" here
355 case TRANSPORT_FAMILY_IPV4
:
356 set_helper_option(t
, "family", "ipv4");
358 case TRANSPORT_FAMILY_IPV6
:
359 set_helper_option(t
, "family", "ipv6");
364 static int release_helper(struct transport
*transport
)
367 struct helper_data
*data
= transport
->data
;
368 refspec_clear(&data
->rs
);
369 res
= disconnect_helper(transport
);
370 free(transport
->data
);
374 static int fetch_with_fetch(struct transport
*transport
,
375 int nr_heads
, struct ref
**to_fetch
)
377 struct helper_data
*data
= transport
->data
;
379 struct strbuf buf
= STRBUF_INIT
;
381 for (i
= 0; i
< nr_heads
; i
++) {
382 const struct ref
*posn
= to_fetch
[i
];
383 if (posn
->status
& REF_STATUS_UPTODATE
)
386 strbuf_addf(&buf
, "fetch %s %s\n",
387 oid_to_hex(&posn
->old_oid
),
388 posn
->symref
? posn
->symref
: posn
->name
);
391 strbuf_addch(&buf
, '\n');
392 sendline(data
, &buf
);
395 if (recvline(data
, &buf
))
398 if (starts_with(buf
.buf
, "lock ")) {
399 const char *name
= buf
.buf
+ 5;
400 if (transport
->pack_lockfile
)
401 warning(_("%s also locked %s"), data
->name
, name
);
403 transport
->pack_lockfile
= xstrdup(name
);
405 else if (data
->check_connectivity
&&
406 data
->transport_options
.check_self_contained_and_connected
&&
407 !strcmp(buf
.buf
, "connectivity-ok"))
408 data
->transport_options
.self_contained_and_connected
= 1;
412 warning(_("%s unexpectedly said: '%s'"), data
->name
, buf
.buf
);
414 strbuf_release(&buf
);
418 static int get_importer(struct transport
*transport
, struct child_process
*fastimport
)
420 struct child_process
*helper
= get_helper(transport
);
421 struct helper_data
*data
= transport
->data
;
422 int cat_blob_fd
, code
;
423 child_process_init(fastimport
);
424 fastimport
->in
= helper
->out
;
425 argv_array_push(&fastimport
->args
, "fast-import");
426 argv_array_push(&fastimport
->args
, "--allow-unsafe-features");
427 argv_array_push(&fastimport
->args
, debug
? "--stats" : "--quiet");
429 if (data
->bidi_import
) {
430 cat_blob_fd
= xdup(helper
->in
);
431 argv_array_pushf(&fastimport
->args
, "--cat-blob-fd=%d", cat_blob_fd
);
433 fastimport
->git_cmd
= 1;
435 code
= start_command(fastimport
);
439 static int get_exporter(struct transport
*transport
,
440 struct child_process
*fastexport
,
441 struct string_list
*revlist_args
)
443 struct helper_data
*data
= transport
->data
;
444 struct child_process
*helper
= get_helper(transport
);
447 child_process_init(fastexport
);
449 /* we need to duplicate helper->in because we want to use it after
450 * fastexport is done with it. */
451 fastexport
->out
= dup(helper
->in
);
452 argv_array_push(&fastexport
->args
, "fast-export");
453 argv_array_push(&fastexport
->args
, "--use-done-feature");
454 argv_array_push(&fastexport
->args
, data
->signed_tags
?
455 "--signed-tags=verbatim" : "--signed-tags=warn-strip");
456 if (data
->export_marks
)
457 argv_array_pushf(&fastexport
->args
, "--export-marks=%s.tmp", data
->export_marks
);
458 if (data
->import_marks
)
459 argv_array_pushf(&fastexport
->args
, "--import-marks=%s", data
->import_marks
);
461 for (i
= 0; i
< revlist_args
->nr
; i
++)
462 argv_array_push(&fastexport
->args
, revlist_args
->items
[i
].string
);
464 fastexport
->git_cmd
= 1;
465 return start_command(fastexport
);
468 static int fetch_with_import(struct transport
*transport
,
469 int nr_heads
, struct ref
**to_fetch
)
471 struct child_process fastimport
;
472 struct helper_data
*data
= transport
->data
;
475 struct strbuf buf
= STRBUF_INIT
;
477 get_helper(transport
);
479 if (get_importer(transport
, &fastimport
))
480 die(_("couldn't run fast-import"));
482 for (i
= 0; i
< nr_heads
; i
++) {
484 if (posn
->status
& REF_STATUS_UPTODATE
)
487 strbuf_addf(&buf
, "import %s\n",
488 posn
->symref
? posn
->symref
: posn
->name
);
489 sendline(data
, &buf
);
493 write_constant(data
->helper
->in
, "\n");
495 * remote-helpers that advertise the bidi-import capability are required to
496 * buffer the complete batch of import commands until this newline before
497 * sending data to fast-import.
498 * These helpers read back data from fast-import on their stdin, which could
499 * be mixed with import commands, otherwise.
502 if (finish_command(&fastimport
))
503 die(_("error while running fast-import"));
506 * The fast-import stream of a remote helper that advertises
507 * the "refspec" capability writes to the refs named after the
508 * right hand side of the first refspec matching each ref we
511 * (If no "refspec" capability was specified, for historical
512 * reasons we default to the equivalent of *:*.)
514 * Store the result in to_fetch[i].old_sha1. Callers such
515 * as "git fetch" can use the value to write feedback to the
516 * terminal, populate FETCH_HEAD, and determine what new value
517 * should be written to peer_ref if the update is a
518 * fast-forward or this is a forced update.
520 for (i
= 0; i
< nr_heads
; i
++) {
521 char *private, *name
;
523 if (posn
->status
& REF_STATUS_UPTODATE
)
525 name
= posn
->symref
? posn
->symref
: posn
->name
;
527 private = apply_refspecs(&data
->rs
, name
);
529 private = xstrdup(name
);
531 if (read_ref(private, &posn
->old_oid
) < 0)
532 die(_("could not read ref %s"), private);
536 strbuf_release(&buf
);
540 static int run_connect(struct transport
*transport
, struct strbuf
*cmdbuf
)
542 struct helper_data
*data
= transport
->data
;
546 struct child_process
*helper
;
548 helper
= get_helper(transport
);
551 * Yes, dup the pipe another time, as we need unbuffered version
552 * of input pipe as FILE*. fclose() closes the underlying fd and
553 * stream buffering only can be changed before first I/O operation
556 duped
= dup(helper
->out
);
558 die_errno(_("can't dup helper output fd"));
559 input
= xfdopen(duped
, "r");
560 setvbuf(input
, NULL
, _IONBF
, 0);
562 sendline(data
, cmdbuf
);
563 if (recvline_fh(input
, cmdbuf
))
566 if (!strcmp(cmdbuf
->buf
, "")) {
567 data
->no_disconnect_req
= 1;
569 fprintf(stderr
, "Debug: Smart transport connection "
572 } else if (!strcmp(cmdbuf
->buf
, "fallback")) {
574 fprintf(stderr
, "Debug: Falling back to dumb "
577 die(_("unknown response to connect: %s"),
585 static int process_connect_service(struct transport
*transport
,
586 const char *name
, const char *exec
)
588 struct helper_data
*data
= transport
->data
;
589 struct strbuf cmdbuf
= STRBUF_INIT
;
593 * Handle --upload-pack and friends. This is fire and forget...
594 * just warn if it fails.
596 if (strcmp(name
, exec
)) {
597 int r
= set_helper_option(transport
, "servpath", exec
);
599 warning(_("setting remote service path not supported by protocol"));
601 warning(_("invalid remote service path"));
605 strbuf_addf(&cmdbuf
, "connect %s\n", name
);
606 ret
= run_connect(transport
, &cmdbuf
);
607 } else if (data
->stateless_connect
&&
608 (get_protocol_version_config() == protocol_v2
) &&
609 !strcmp("git-upload-pack", name
)) {
610 strbuf_addf(&cmdbuf
, "stateless-connect %s\n", name
);
611 ret
= run_connect(transport
, &cmdbuf
);
613 transport
->stateless_rpc
= 1;
616 strbuf_release(&cmdbuf
);
620 static int process_connect(struct transport
*transport
,
623 struct helper_data
*data
= transport
->data
;
627 name
= for_push
? "git-receive-pack" : "git-upload-pack";
629 exec
= data
->transport_options
.receivepack
;
631 exec
= data
->transport_options
.uploadpack
;
633 return process_connect_service(transport
, name
, exec
);
636 static int connect_helper(struct transport
*transport
, const char *name
,
637 const char *exec
, int fd
[2])
639 struct helper_data
*data
= transport
->data
;
641 /* Get_helper so connect is inited. */
642 get_helper(transport
);
644 die(_("operation not supported by protocol"));
646 if (!process_connect_service(transport
, name
, exec
))
647 die(_("can't connect to subservice %s"), name
);
649 fd
[0] = data
->helper
->out
;
650 fd
[1] = data
->helper
->in
;
654 static int fetch(struct transport
*transport
,
655 int nr_heads
, struct ref
**to_fetch
)
657 struct helper_data
*data
= transport
->data
;
660 if (process_connect(transport
, 0)) {
661 do_take_over(transport
);
662 return transport
->vtable
->fetch(transport
, nr_heads
, to_fetch
);
666 for (i
= 0; i
< nr_heads
; i
++)
667 if (!(to_fetch
[i
]->status
& REF_STATUS_UPTODATE
))
673 if (data
->check_connectivity
&&
674 data
->transport_options
.check_self_contained_and_connected
)
675 set_helper_option(transport
, "check-connectivity", "true");
677 if (transport
->cloning
)
678 set_helper_option(transport
, "cloning", "true");
680 if (data
->transport_options
.update_shallow
)
681 set_helper_option(transport
, "update-shallow", "true");
683 if (data
->transport_options
.filter_options
.choice
) {
684 struct strbuf expanded_filter_spec
= STRBUF_INIT
;
685 expand_list_objects_filter_spec(
686 &data
->transport_options
.filter_options
,
687 &expanded_filter_spec
);
688 set_helper_option(transport
, "filter",
689 expanded_filter_spec
.buf
);
690 strbuf_release(&expanded_filter_spec
);
693 if (data
->transport_options
.negotiation_tips
)
694 warning("Ignoring --negotiation-tip because the protocol does not support it.");
697 return fetch_with_fetch(transport
, nr_heads
, to_fetch
);
700 return fetch_with_import(transport
, nr_heads
, to_fetch
);
705 static int push_update_ref_status(struct strbuf
*buf
,
707 struct ref
*remote_refs
)
710 int status
, forced
= 0;
712 if (starts_with(buf
->buf
, "ok ")) {
713 status
= REF_STATUS_OK
;
714 refname
= buf
->buf
+ 3;
715 } else if (starts_with(buf
->buf
, "error ")) {
716 status
= REF_STATUS_REMOTE_REJECT
;
717 refname
= buf
->buf
+ 6;
719 die(_("expected ok/error, helper said '%s'"), buf
->buf
);
721 msg
= strchr(refname
, ' ');
723 struct strbuf msg_buf
= STRBUF_INIT
;
727 if (!unquote_c_style(&msg_buf
, msg
, &end
))
728 msg
= strbuf_detach(&msg_buf
, NULL
);
731 strbuf_release(&msg_buf
);
733 if (!strcmp(msg
, "no match")) {
734 status
= REF_STATUS_NONE
;
737 else if (!strcmp(msg
, "up to date")) {
738 status
= REF_STATUS_UPTODATE
;
741 else if (!strcmp(msg
, "non-fast forward")) {
742 status
= REF_STATUS_REJECT_NONFASTFORWARD
;
745 else if (!strcmp(msg
, "already exists")) {
746 status
= REF_STATUS_REJECT_ALREADY_EXISTS
;
749 else if (!strcmp(msg
, "fetch first")) {
750 status
= REF_STATUS_REJECT_FETCH_FIRST
;
753 else if (!strcmp(msg
, "needs force")) {
754 status
= REF_STATUS_REJECT_NEEDS_FORCE
;
757 else if (!strcmp(msg
, "stale info")) {
758 status
= REF_STATUS_REJECT_STALE
;
761 else if (!strcmp(msg
, "forced update")) {
768 *ref
= find_ref_by_name(*ref
, refname
);
770 *ref
= find_ref_by_name(remote_refs
, refname
);
772 warning(_("helper reported unexpected status of %s"), refname
);
776 if ((*ref
)->status
!= REF_STATUS_NONE
) {
778 * Earlier, the ref was marked not to be pushed, so ignore the ref
779 * status reported by the remote helper if the latter is 'no match'.
781 if (status
== REF_STATUS_NONE
)
785 (*ref
)->status
= status
;
786 (*ref
)->forced_update
|= forced
;
787 (*ref
)->remote_status
= msg
;
788 return !(status
== REF_STATUS_OK
);
791 static int push_update_refs_status(struct helper_data
*data
,
792 struct ref
*remote_refs
,
795 struct strbuf buf
= STRBUF_INIT
;
796 struct ref
*ref
= remote_refs
;
802 if (recvline(data
, &buf
)) {
810 if (push_update_ref_status(&buf
, &ref
, remote_refs
))
813 if (flags
& TRANSPORT_PUSH_DRY_RUN
|| !data
->rs
.nr
|| data
->no_private_update
)
816 /* propagate back the update to the remote namespace */
817 private = apply_refspecs(&data
->rs
, ref
->name
);
820 update_ref("update by helper", private, &ref
->new_oid
, NULL
,
824 strbuf_release(&buf
);
828 static void set_common_push_options(struct transport
*transport
,
829 const char *name
, int flags
)
831 if (flags
& TRANSPORT_PUSH_DRY_RUN
) {
832 if (set_helper_option(transport
, "dry-run", "true") != 0)
833 die(_("helper %s does not support dry-run"), name
);
834 } else if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
) {
835 if (set_helper_option(transport
, TRANS_OPT_PUSH_CERT
, "true") != 0)
836 die(_("helper %s does not support --signed"), name
);
837 } else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
) {
838 if (set_helper_option(transport
, TRANS_OPT_PUSH_CERT
, "if-asked") != 0)
839 die(_("helper %s does not support --signed=if-asked"), name
);
842 if (flags
& TRANSPORT_PUSH_OPTIONS
) {
843 struct string_list_item
*item
;
844 for_each_string_list_item(item
, transport
->push_options
)
845 if (set_helper_option(transport
, "push-option", item
->string
) != 0)
846 die(_("helper %s does not support 'push-option'"), name
);
850 static int push_refs_with_push(struct transport
*transport
,
851 struct ref
*remote_refs
, int flags
)
853 int force_all
= flags
& TRANSPORT_PUSH_FORCE
;
854 int mirror
= flags
& TRANSPORT_PUSH_MIRROR
;
855 struct helper_data
*data
= transport
->data
;
856 struct strbuf buf
= STRBUF_INIT
;
858 struct string_list cas_options
= STRING_LIST_INIT_DUP
;
859 struct string_list_item
*cas_option
;
861 get_helper(transport
);
865 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
866 if (!ref
->peer_ref
&& !mirror
)
869 /* Check for statuses set by set_ref_status_for_push() */
870 switch (ref
->status
) {
871 case REF_STATUS_REJECT_NONFASTFORWARD
:
872 case REF_STATUS_REJECT_STALE
:
873 case REF_STATUS_REJECT_ALREADY_EXISTS
:
874 case REF_STATUS_UPTODATE
:
883 strbuf_addstr(&buf
, "push ");
884 if (!ref
->deletion
) {
886 strbuf_addch(&buf
, '+');
888 strbuf_addstr(&buf
, ref
->peer_ref
->name
);
890 strbuf_addstr(&buf
, oid_to_hex(&ref
->new_oid
));
892 strbuf_addch(&buf
, ':');
893 strbuf_addstr(&buf
, ref
->name
);
894 strbuf_addch(&buf
, '\n');
897 * The "--force-with-lease" options without explicit
898 * values to expect have already been expanded into
899 * the ref->old_oid_expect[] field; we can ignore
900 * transport->smart_options->cas altogether and instead
901 * can enumerate them from the refs.
903 if (ref
->expect_old_sha1
) {
904 struct strbuf cas
= STRBUF_INIT
;
905 strbuf_addf(&cas
, "%s:%s",
906 ref
->name
, oid_to_hex(&ref
->old_oid_expect
));
907 string_list_append_nodup(&cas_options
,
908 strbuf_detach(&cas
, NULL
));
912 string_list_clear(&cas_options
, 0);
916 for_each_string_list_item(cas_option
, &cas_options
)
917 set_helper_option(transport
, "cas", cas_option
->string
);
918 set_common_push_options(transport
, data
->name
, flags
);
920 strbuf_addch(&buf
, '\n');
921 sendline(data
, &buf
);
922 strbuf_release(&buf
);
923 string_list_clear(&cas_options
, 0);
925 return push_update_refs_status(data
, remote_refs
, flags
);
928 static int push_refs_with_export(struct transport
*transport
,
929 struct ref
*remote_refs
, int flags
)
932 struct child_process
*helper
, exporter
;
933 struct helper_data
*data
= transport
->data
;
934 struct string_list revlist_args
= STRING_LIST_INIT_DUP
;
935 struct strbuf buf
= STRBUF_INIT
;
938 die(_("remote-helper doesn't support push; refspec needed"));
940 set_common_push_options(transport
, data
->name
, flags
);
941 if (flags
& TRANSPORT_PUSH_FORCE
) {
942 if (set_helper_option(transport
, "force", "true") != 0)
943 warning(_("helper %s does not support 'force'"), data
->name
);
946 helper
= get_helper(transport
);
948 write_constant(helper
->in
, "export\n");
950 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
952 struct object_id oid
;
954 private = apply_refspecs(&data
->rs
, ref
->name
);
955 if (private && !get_oid(private, &oid
)) {
956 strbuf_addf(&buf
, "^%s", private);
957 string_list_append_nodup(&revlist_args
,
958 strbuf_detach(&buf
, NULL
));
959 oidcpy(&ref
->old_oid
, &oid
);
964 if (strcmp(ref
->name
, ref
->peer_ref
->name
)) {
965 if (!ref
->deletion
) {
969 /* Follow symbolic refs (mainly for HEAD). */
970 name
= resolve_ref_unsafe(ref
->peer_ref
->name
,
973 if (!name
|| !(flag
& REF_ISSYMREF
))
974 name
= ref
->peer_ref
->name
;
976 strbuf_addf(&buf
, "%s:%s", name
, ref
->name
);
978 strbuf_addf(&buf
, ":%s", ref
->name
);
980 string_list_append(&revlist_args
, "--refspec");
981 string_list_append(&revlist_args
, buf
.buf
);
982 strbuf_release(&buf
);
985 string_list_append(&revlist_args
, ref
->peer_ref
->name
);
989 if (get_exporter(transport
, &exporter
, &revlist_args
))
990 die(_("couldn't run fast-export"));
992 string_list_clear(&revlist_args
, 1);
994 if (finish_command(&exporter
))
995 die(_("error while running fast-export"));
996 if (push_update_refs_status(data
, remote_refs
, flags
))
999 if (data
->export_marks
) {
1000 strbuf_addf(&buf
, "%s.tmp", data
->export_marks
);
1001 rename(buf
.buf
, data
->export_marks
);
1002 strbuf_release(&buf
);
1008 static int push_refs(struct transport
*transport
,
1009 struct ref
*remote_refs
, int flags
)
1011 struct helper_data
*data
= transport
->data
;
1013 if (process_connect(transport
, 1)) {
1014 do_take_over(transport
);
1015 return transport
->vtable
->push_refs(transport
, remote_refs
, flags
);
1020 _("No refs in common and none specified; doing nothing.\n"
1021 "Perhaps you should specify a branch such as 'master'.\n"));
1026 return push_refs_with_push(transport
, remote_refs
, flags
);
1029 return push_refs_with_export(transport
, remote_refs
, flags
);
1035 static int has_attribute(const char *attrs
, const char *attr
)
1043 const char *space
= strchrnul(attrs
, ' ');
1044 if (len
== space
- attrs
&& !strncmp(attrs
, attr
, len
))
1052 static struct ref
*get_refs_list(struct transport
*transport
, int for_push
,
1053 const struct argv_array
*ref_prefixes
)
1055 struct helper_data
*data
= transport
->data
;
1056 struct child_process
*helper
;
1057 struct ref
*ret
= NULL
;
1058 struct ref
**tail
= &ret
;
1060 struct strbuf buf
= STRBUF_INIT
;
1062 helper
= get_helper(transport
);
1064 if (process_connect(transport
, for_push
)) {
1065 do_take_over(transport
);
1066 return transport
->vtable
->get_refs_list(transport
, for_push
, ref_prefixes
);
1069 if (data
->push
&& for_push
)
1070 write_str_in_full(helper
->in
, "list for-push\n");
1072 write_str_in_full(helper
->in
, "list\n");
1076 if (recvline(data
, &buf
))
1082 eov
= strchr(buf
.buf
, ' ');
1084 die(_("malformed response in ref list: %s"), buf
.buf
);
1085 eon
= strchr(eov
+ 1, ' ');
1089 *tail
= alloc_ref(eov
+ 1);
1090 if (buf
.buf
[0] == '@')
1091 (*tail
)->symref
= xstrdup(buf
.buf
+ 1);
1092 else if (buf
.buf
[0] != '?')
1093 get_oid_hex(buf
.buf
, &(*tail
)->old_oid
);
1095 if (has_attribute(eon
+ 1, "unchanged")) {
1096 (*tail
)->status
|= REF_STATUS_UPTODATE
;
1097 if (read_ref((*tail
)->name
, &(*tail
)->old_oid
) < 0)
1098 die(_("could not read ref %s"),
1102 tail
= &((*tail
)->next
);
1105 fprintf(stderr
, "Debug: Read ref listing.\n");
1106 strbuf_release(&buf
);
1108 for (posn
= ret
; posn
; posn
= posn
->next
)
1109 resolve_remote_symref(posn
, ret
);
1114 static struct transport_vtable vtable
= {
1124 int transport_helper_init(struct transport
*transport
, const char *name
)
1126 struct helper_data
*data
= xcalloc(1, sizeof(*data
));
1129 transport_check_allowed(name
);
1131 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
1134 transport
->data
= data
;
1135 transport
->vtable
= &vtable
;
1136 transport
->smart_options
= &(data
->transport_options
);
1141 * Linux pipes can buffer 65536 bytes at once (and most platforms can
1142 * buffer less), so attempt reads and writes with up to that size.
1144 #define BUFFERSIZE 65536
1145 /* This should be enough to hold debugging message. */
1146 #define PBUFFERSIZE 8192
1148 /* Print bidirectional transfer loop debug message. */
1149 __attribute__((format (printf
, 1, 2)))
1150 static void transfer_debug(const char *fmt
, ...)
1153 * NEEDSWORK: This function is sometimes used from multiple threads, and
1154 * we end up using debug_enabled racily. That "should not matter" since
1155 * we always write the same value, but it's still wrong. This function
1156 * is listed in .tsan-suppressions for the time being.
1160 char msgbuf
[PBUFFERSIZE
];
1161 static int debug_enabled
= -1;
1163 if (debug_enabled
< 0)
1164 debug_enabled
= getenv("GIT_TRANSLOOP_DEBUG") ? 1 : 0;
1168 va_start(args
, fmt
);
1169 vsnprintf(msgbuf
, PBUFFERSIZE
, fmt
, args
);
1171 fprintf(stderr
, "Transfer loop debugging: %s\n", msgbuf
);
1174 /* Stream state: More data may be coming in this direction. */
1175 #define SSTATE_TRANSFERRING 0
1177 * Stream state: No more data coming in this direction, flushing rest of
1180 #define SSTATE_FLUSHING 1
1181 /* Stream state: Transfer in this direction finished. */
1182 #define SSTATE_FINISHED 2
1184 #define STATE_NEEDS_READING(state) ((state) <= SSTATE_TRANSFERRING)
1185 #define STATE_NEEDS_WRITING(state) ((state) <= SSTATE_FLUSHING)
1186 #define STATE_NEEDS_CLOSING(state) ((state) == SSTATE_FLUSHING)
1188 /* Unidirectional transfer. */
1189 struct unidirectional_transfer
{
1194 /* Is source socket? */
1196 /* Is destination socket? */
1198 /* Transfer state (TRANSFERRING/FLUSHING/FINISHED) */
1201 char buf
[BUFFERSIZE
];
1204 /* Name of source. */
1205 const char *src_name
;
1206 /* Name of destination. */
1207 const char *dest_name
;
1210 /* Closes the target (for writing) if transfer has finished. */
1211 static void udt_close_if_finished(struct unidirectional_transfer
*t
)
1213 if (STATE_NEEDS_CLOSING(t
->state
) && !t
->bufuse
) {
1214 t
->state
= SSTATE_FINISHED
;
1215 if (t
->dest_is_sock
)
1216 shutdown(t
->dest
, SHUT_WR
);
1219 transfer_debug("Closed %s.", t
->dest_name
);
1224 * Tries to read data from source into buffer. If buffer is full,
1225 * no data is read. Returns 0 on success, -1 on error.
1227 static int udt_do_read(struct unidirectional_transfer
*t
)
1231 if (t
->bufuse
== BUFFERSIZE
)
1232 return 0; /* No space for more. */
1234 transfer_debug("%s is readable", t
->src_name
);
1235 bytes
= xread(t
->src
, t
->buf
+ t
->bufuse
, BUFFERSIZE
- t
->bufuse
);
1237 error_errno(_("read(%s) failed"), t
->src_name
);
1239 } else if (bytes
== 0) {
1240 transfer_debug("%s EOF (with %i bytes in buffer)",
1241 t
->src_name
, (int)t
->bufuse
);
1242 t
->state
= SSTATE_FLUSHING
;
1243 } else if (bytes
> 0) {
1245 transfer_debug("Read %i bytes from %s (buffer now at %i)",
1246 (int)bytes
, t
->src_name
, (int)t
->bufuse
);
1251 /* Tries to write data from buffer into destination. If buffer is empty,
1252 * no data is written. Returns 0 on success, -1 on error.
1254 static int udt_do_write(struct unidirectional_transfer
*t
)
1259 return 0; /* Nothing to write. */
1261 transfer_debug("%s is writable", t
->dest_name
);
1262 bytes
= xwrite(t
->dest
, t
->buf
, t
->bufuse
);
1264 error_errno(_("write(%s) failed"), t
->dest_name
);
1266 } else if (bytes
> 0) {
1269 memmove(t
->buf
, t
->buf
+ bytes
, t
->bufuse
);
1270 transfer_debug("Wrote %i bytes to %s (buffer now at %i)",
1271 (int)bytes
, t
->dest_name
, (int)t
->bufuse
);
1277 /* State of bidirectional transfer loop. */
1278 struct bidirectional_transfer_state
{
1279 /* Direction from program to git. */
1280 struct unidirectional_transfer ptg
;
1281 /* Direction from git to program. */
1282 struct unidirectional_transfer gtp
;
1285 static void *udt_copy_task_routine(void *udt
)
1287 struct unidirectional_transfer
*t
= (struct unidirectional_transfer
*)udt
;
1288 while (t
->state
!= SSTATE_FINISHED
) {
1289 if (STATE_NEEDS_READING(t
->state
))
1292 if (STATE_NEEDS_WRITING(t
->state
))
1293 if (udt_do_write(t
))
1295 if (STATE_NEEDS_CLOSING(t
->state
))
1296 udt_close_if_finished(t
);
1298 return udt
; /* Just some non-NULL value. */
1304 * Join thread, with appropriate errors on failure. Name is name for the
1305 * thread (for error messages). Returns 0 on success, 1 on failure.
1307 static int tloop_join(pthread_t thread
, const char *name
)
1311 err
= pthread_join(thread
, &tret
);
1313 error(_("%s thread failed"), name
);
1317 error(_("%s thread failed to join: %s"), name
, strerror(err
));
1324 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1327 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1329 pthread_t gtp_thread
;
1330 pthread_t ptg_thread
;
1333 err
= pthread_create(>p_thread
, NULL
, udt_copy_task_routine
,
1336 die(_("can't start thread for copying data: %s"), strerror(err
));
1337 err
= pthread_create(&ptg_thread
, NULL
, udt_copy_task_routine
,
1340 die(_("can't start thread for copying data: %s"), strerror(err
));
1342 ret
|= tloop_join(gtp_thread
, "Git to program copy");
1343 ret
|= tloop_join(ptg_thread
, "Program to git copy");
1348 /* Close the source and target (for writing) for transfer. */
1349 static void udt_kill_transfer(struct unidirectional_transfer
*t
)
1351 t
->state
= SSTATE_FINISHED
;
1353 * Socket read end left open isn't a disaster if nobody
1354 * attempts to read from it (mingw compat headers do not
1357 * We can't fully close the socket since otherwise gtp
1358 * task would first close the socket it sends data to
1359 * while closing the ptg file descriptors.
1361 if (!t
->src_is_sock
)
1363 if (t
->dest_is_sock
)
1364 shutdown(t
->dest
, SHUT_WR
);
1370 * Join process, with appropriate errors on failure. Name is name for the
1371 * process (for error messages). Returns 0 on success, 1 on failure.
1373 static int tloop_join(pid_t pid
, const char *name
)
1376 if (waitpid(pid
, &tret
, 0) < 0) {
1377 error_errno(_("%s process failed to wait"), name
);
1380 if (!WIFEXITED(tret
) || WEXITSTATUS(tret
)) {
1381 error(_("%s process failed"), name
);
1388 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1391 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1396 /* Fork thread #1: git to program. */
1399 die_errno(_("can't start thread for copying data"));
1400 else if (pid1
== 0) {
1401 udt_kill_transfer(&s
->ptg
);
1402 exit(udt_copy_task_routine(&s
->gtp
) ? 0 : 1);
1405 /* Fork thread #2: program to git. */
1408 die_errno(_("can't start thread for copying data"));
1409 else if (pid2
== 0) {
1410 udt_kill_transfer(&s
->gtp
);
1411 exit(udt_copy_task_routine(&s
->ptg
) ? 0 : 1);
1415 * Close both streams in parent as to not interfere with
1416 * end of file detection and wait for both tasks to finish.
1418 udt_kill_transfer(&s
->gtp
);
1419 udt_kill_transfer(&s
->ptg
);
1420 ret
|= tloop_join(pid1
, "Git to program copy");
1421 ret
|= tloop_join(pid2
, "Program to git copy");
1427 * Copies data from stdin to output and from input to stdout simultaneously.
1428 * Additionally filtering through given filter. If filter is NULL, uses
1431 int bidirectional_transfer_loop(int input
, int output
)
1433 struct bidirectional_transfer_state state
;
1435 /* Fill the state fields. */
1436 state
.ptg
.src
= input
;
1438 state
.ptg
.src_is_sock
= (input
== output
);
1439 state
.ptg
.dest_is_sock
= 0;
1440 state
.ptg
.state
= SSTATE_TRANSFERRING
;
1441 state
.ptg
.bufuse
= 0;
1442 state
.ptg
.src_name
= "remote input";
1443 state
.ptg
.dest_name
= "stdout";
1446 state
.gtp
.dest
= output
;
1447 state
.gtp
.src_is_sock
= 0;
1448 state
.gtp
.dest_is_sock
= (input
== output
);
1449 state
.gtp
.state
= SSTATE_TRANSFERRING
;
1450 state
.gtp
.bufuse
= 0;
1451 state
.gtp
.src_name
= "stdin";
1452 state
.gtp
.dest_name
= "remote output";
1454 return tloop_spawnwait_tasks(&state
);