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 */
39 struct refspec_item
*refspecs
;
41 /* Transport options for fetch-pack/send-pack (should one of
44 struct git_transport_options transport_options
;
47 static void sendline(struct helper_data
*helper
, struct strbuf
*buffer
)
50 fprintf(stderr
, "Debug: Remote helper: -> %s", buffer
->buf
);
51 if (write_in_full(helper
->helper
->in
, buffer
->buf
, buffer
->len
) < 0)
52 die_errno("Full write to remote helper failed");
55 static int recvline_fh(FILE *helper
, struct strbuf
*buffer
)
59 fprintf(stderr
, "Debug: Remote helper: Waiting...\n");
60 if (strbuf_getline(buffer
, helper
) == EOF
) {
62 fprintf(stderr
, "Debug: Remote helper quit.\n");
67 fprintf(stderr
, "Debug: Remote helper: <- %s\n", buffer
->buf
);
71 static int recvline(struct helper_data
*helper
, struct strbuf
*buffer
)
73 return recvline_fh(helper
->out
, buffer
);
76 static void write_constant(int fd
, const char *str
)
79 fprintf(stderr
, "Debug: Remote helper: -> %s", str
);
80 if (write_in_full(fd
, str
, strlen(str
)) < 0)
81 die_errno("Full write to remote helper failed");
84 static const char *remove_ext_force(const char *url
)
87 const char *colon
= strchr(url
, ':');
88 if (colon
&& colon
[1] == ':')
94 static void do_take_over(struct transport
*transport
)
96 struct helper_data
*data
;
97 data
= (struct helper_data
*)transport
->data
;
98 transport_take_over(transport
, data
->helper
);
103 static void standard_options(struct transport
*t
);
105 static struct child_process
*get_helper(struct transport
*transport
)
107 struct helper_data
*data
= transport
->data
;
108 struct strbuf buf
= STRBUF_INIT
;
109 struct child_process
*helper
;
110 const char **refspecs
= NULL
;
112 int refspec_alloc
= 0;
119 helper
= xmalloc(sizeof(*helper
));
120 child_process_init(helper
);
124 argv_array_pushf(&helper
->args
, "git-remote-%s", data
->name
);
125 argv_array_push(&helper
->args
, transport
->remote
->name
);
126 argv_array_push(&helper
->args
, remove_ext_force(transport
->url
));
128 helper
->silent_exec_failure
= 1;
131 argv_array_pushf(&helper
->env_array
, "%s=%s",
132 GIT_DIR_ENVIRONMENT
, get_git_dir());
134 code
= start_command(helper
);
135 if (code
< 0 && errno
== ENOENT
)
136 die("Unable to find remote helper for '%s'", data
->name
);
140 data
->helper
= helper
;
141 data
->no_disconnect_req
= 0;
144 * Open the output as FILE* so strbuf_getline_*() family of
145 * functions can be used.
146 * Do this with duped fd because fclose() will close the fd,
147 * and stuff like taking over will require the fd to remain.
149 duped
= dup(helper
->out
);
151 die_errno("Can't dup helper output fd");
152 data
->out
= xfdopen(duped
, "r");
154 write_constant(helper
->in
, "capabilities\n");
157 const char *capname
, *arg
;
159 if (recvline(data
, &buf
))
165 if (*buf
.buf
== '*') {
166 capname
= buf
.buf
+ 1;
172 fprintf(stderr
, "Debug: Got cap %s\n", capname
);
173 if (!strcmp(capname
, "fetch"))
175 else if (!strcmp(capname
, "option"))
177 else if (!strcmp(capname
, "push"))
179 else if (!strcmp(capname
, "import"))
181 else if (!strcmp(capname
, "bidi-import"))
182 data
->bidi_import
= 1;
183 else if (!strcmp(capname
, "export"))
185 else if (!strcmp(capname
, "check-connectivity"))
186 data
->check_connectivity
= 1;
187 else if (!data
->refspecs
&& skip_prefix(capname
, "refspec ", &arg
)) {
191 refspecs
[refspec_nr
++] = xstrdup(arg
);
192 } else if (!strcmp(capname
, "connect")) {
194 } else if (!strcmp(capname
, "stateless-connect")) {
195 data
->stateless_connect
= 1;
196 } else if (!strcmp(capname
, "signed-tags")) {
197 data
->signed_tags
= 1;
198 } else if (skip_prefix(capname
, "export-marks ", &arg
)) {
199 data
->export_marks
= xstrdup(arg
);
200 } else if (skip_prefix(capname
, "import-marks ", &arg
)) {
201 data
->import_marks
= xstrdup(arg
);
202 } else if (starts_with(capname
, "no-private-update")) {
203 data
->no_private_update
= 1;
204 } else if (mandatory
) {
205 die("Unknown mandatory capability %s. This remote "
206 "helper probably needs newer version of Git.",
212 data
->refspec_nr
= refspec_nr
;
213 data
->refspecs
= parse_fetch_refspec(refspec_nr
, refspecs
);
214 for (i
= 0; i
< refspec_nr
; i
++)
215 free((char *)refspecs
[i
]);
217 } else if (data
->import
|| data
->bidi_import
|| data
->export
) {
218 warning("This remote helper should implement refspec capability.");
220 strbuf_release(&buf
);
222 fprintf(stderr
, "Debug: Capabilities complete.\n");
223 standard_options(transport
);
227 static int disconnect_helper(struct transport
*transport
)
229 struct helper_data
*data
= transport
->data
;
234 fprintf(stderr
, "Debug: Disconnecting.\n");
235 if (!data
->no_disconnect_req
) {
237 * Ignore write errors; there's nothing we can do,
238 * since we're about to close the pipe anyway. And the
239 * most likely error is EPIPE due to the helper dying
240 * to report an error itself.
242 sigchain_push(SIGPIPE
, SIG_IGN
);
243 xwrite(data
->helper
->in
, "\n", 1);
244 sigchain_pop(SIGPIPE
);
246 close(data
->helper
->in
);
247 close(data
->helper
->out
);
249 res
= finish_command(data
->helper
);
250 FREE_AND_NULL(data
->helper
);
255 static const char *unsupported_options
[] = {
256 TRANS_OPT_UPLOADPACK
,
257 TRANS_OPT_RECEIVEPACK
,
262 static const char *boolean_options
[] = {
265 TRANS_OPT_FOLLOWTAGS
,
266 TRANS_OPT_DEEPEN_RELATIVE
269 static int strbuf_set_helper_option(struct helper_data
*data
,
275 if (recvline(data
, buf
))
278 if (!strcmp(buf
->buf
, "ok"))
280 else if (starts_with(buf
->buf
, "error"))
282 else if (!strcmp(buf
->buf
, "unsupported"))
285 warning("%s unexpectedly said: '%s'", data
->name
, buf
->buf
);
291 static int string_list_set_helper_option(struct helper_data
*data
,
293 struct string_list
*list
)
295 struct strbuf buf
= STRBUF_INIT
;
298 for (i
= 0; i
< list
->nr
; i
++) {
299 strbuf_addf(&buf
, "option %s ", name
);
300 quote_c_style(list
->items
[i
].string
, &buf
, NULL
, 0);
301 strbuf_addch(&buf
, '\n');
303 if ((ret
= strbuf_set_helper_option(data
, &buf
)))
307 strbuf_release(&buf
);
311 static int set_helper_option(struct transport
*transport
,
312 const char *name
, const char *value
)
314 struct helper_data
*data
= transport
->data
;
315 struct strbuf buf
= STRBUF_INIT
;
316 int i
, ret
, is_bool
= 0;
318 get_helper(transport
);
323 if (!strcmp(name
, "deepen-not"))
324 return string_list_set_helper_option(data
, name
,
325 (struct string_list
*)value
);
327 for (i
= 0; i
< ARRAY_SIZE(unsupported_options
); i
++) {
328 if (!strcmp(name
, unsupported_options
[i
]))
332 for (i
= 0; i
< ARRAY_SIZE(boolean_options
); i
++) {
333 if (!strcmp(name
, boolean_options
[i
])) {
339 strbuf_addf(&buf
, "option %s ", name
);
341 strbuf_addstr(&buf
, value
? "true" : "false");
343 quote_c_style(value
, &buf
, NULL
, 0);
344 strbuf_addch(&buf
, '\n');
346 ret
= strbuf_set_helper_option(data
, &buf
);
347 strbuf_release(&buf
);
351 static void standard_options(struct transport
*t
)
356 set_helper_option(t
, "progress", t
->progress
? "true" : "false");
358 xsnprintf(buf
, sizeof(buf
), "%d", v
+ 1);
359 set_helper_option(t
, "verbosity", buf
);
362 case TRANSPORT_FAMILY_ALL
:
364 * this is already the default,
365 * do not break old remote helpers by setting "all" here
368 case TRANSPORT_FAMILY_IPV4
:
369 set_helper_option(t
, "family", "ipv4");
371 case TRANSPORT_FAMILY_IPV6
:
372 set_helper_option(t
, "family", "ipv6");
377 static int release_helper(struct transport
*transport
)
380 struct helper_data
*data
= transport
->data
;
381 free_refspec(data
->refspec_nr
, data
->refspecs
);
382 data
->refspecs
= NULL
;
383 res
= disconnect_helper(transport
);
384 free(transport
->data
);
388 static int fetch_with_fetch(struct transport
*transport
,
389 int nr_heads
, struct ref
**to_fetch
)
391 struct helper_data
*data
= transport
->data
;
393 struct strbuf buf
= STRBUF_INIT
;
395 for (i
= 0; i
< nr_heads
; i
++) {
396 const struct ref
*posn
= to_fetch
[i
];
397 if (posn
->status
& REF_STATUS_UPTODATE
)
400 strbuf_addf(&buf
, "fetch %s %s\n",
401 oid_to_hex(&posn
->old_oid
),
402 posn
->symref
? posn
->symref
: posn
->name
);
405 strbuf_addch(&buf
, '\n');
406 sendline(data
, &buf
);
409 if (recvline(data
, &buf
))
412 if (starts_with(buf
.buf
, "lock ")) {
413 const char *name
= buf
.buf
+ 5;
414 if (transport
->pack_lockfile
)
415 warning("%s also locked %s", data
->name
, name
);
417 transport
->pack_lockfile
= xstrdup(name
);
419 else if (data
->check_connectivity
&&
420 data
->transport_options
.check_self_contained_and_connected
&&
421 !strcmp(buf
.buf
, "connectivity-ok"))
422 data
->transport_options
.self_contained_and_connected
= 1;
426 warning("%s unexpectedly said: '%s'", data
->name
, buf
.buf
);
428 strbuf_release(&buf
);
432 static int get_importer(struct transport
*transport
, struct child_process
*fastimport
)
434 struct child_process
*helper
= get_helper(transport
);
435 struct helper_data
*data
= transport
->data
;
436 int cat_blob_fd
, code
;
437 child_process_init(fastimport
);
438 fastimport
->in
= helper
->out
;
439 argv_array_push(&fastimport
->args
, "fast-import");
440 argv_array_push(&fastimport
->args
, debug
? "--stats" : "--quiet");
442 if (data
->bidi_import
) {
443 cat_blob_fd
= xdup(helper
->in
);
444 argv_array_pushf(&fastimport
->args
, "--cat-blob-fd=%d", cat_blob_fd
);
446 fastimport
->git_cmd
= 1;
448 code
= start_command(fastimport
);
452 static int get_exporter(struct transport
*transport
,
453 struct child_process
*fastexport
,
454 struct string_list
*revlist_args
)
456 struct helper_data
*data
= transport
->data
;
457 struct child_process
*helper
= get_helper(transport
);
460 child_process_init(fastexport
);
462 /* we need to duplicate helper->in because we want to use it after
463 * fastexport is done with it. */
464 fastexport
->out
= dup(helper
->in
);
465 argv_array_push(&fastexport
->args
, "fast-export");
466 argv_array_push(&fastexport
->args
, "--use-done-feature");
467 argv_array_push(&fastexport
->args
, data
->signed_tags
?
468 "--signed-tags=verbatim" : "--signed-tags=warn-strip");
469 if (data
->export_marks
)
470 argv_array_pushf(&fastexport
->args
, "--export-marks=%s.tmp", data
->export_marks
);
471 if (data
->import_marks
)
472 argv_array_pushf(&fastexport
->args
, "--import-marks=%s", data
->import_marks
);
474 for (i
= 0; i
< revlist_args
->nr
; i
++)
475 argv_array_push(&fastexport
->args
, revlist_args
->items
[i
].string
);
477 fastexport
->git_cmd
= 1;
478 return start_command(fastexport
);
481 static int fetch_with_import(struct transport
*transport
,
482 int nr_heads
, struct ref
**to_fetch
)
484 struct child_process fastimport
;
485 struct helper_data
*data
= transport
->data
;
488 struct strbuf buf
= STRBUF_INIT
;
490 get_helper(transport
);
492 if (get_importer(transport
, &fastimport
))
493 die("Couldn't run fast-import");
495 for (i
= 0; i
< nr_heads
; i
++) {
497 if (posn
->status
& REF_STATUS_UPTODATE
)
500 strbuf_addf(&buf
, "import %s\n",
501 posn
->symref
? posn
->symref
: posn
->name
);
502 sendline(data
, &buf
);
506 write_constant(data
->helper
->in
, "\n");
508 * remote-helpers that advertise the bidi-import capability are required to
509 * buffer the complete batch of import commands until this newline before
510 * sending data to fast-import.
511 * These helpers read back data from fast-import on their stdin, which could
512 * be mixed with import commands, otherwise.
515 if (finish_command(&fastimport
))
516 die("Error while running fast-import");
519 * The fast-import stream of a remote helper that advertises
520 * the "refspec" capability writes to the refs named after the
521 * right hand side of the first refspec matching each ref we
524 * (If no "refspec" capability was specified, for historical
525 * reasons we default to the equivalent of *:*.)
527 * Store the result in to_fetch[i].old_sha1. Callers such
528 * as "git fetch" can use the value to write feedback to the
529 * terminal, populate FETCH_HEAD, and determine what new value
530 * should be written to peer_ref if the update is a
531 * fast-forward or this is a forced update.
533 for (i
= 0; i
< nr_heads
; i
++) {
534 char *private, *name
;
536 if (posn
->status
& REF_STATUS_UPTODATE
)
538 name
= posn
->symref
? posn
->symref
: posn
->name
;
540 private = apply_refspecs(data
->refspecs
, data
->refspec_nr
, name
);
542 private = xstrdup(name
);
544 if (read_ref(private, &posn
->old_oid
) < 0)
545 die("Could not read ref %s", private);
549 strbuf_release(&buf
);
553 static int run_connect(struct transport
*transport
, struct strbuf
*cmdbuf
)
555 struct helper_data
*data
= transport
->data
;
559 struct child_process
*helper
;
561 helper
= get_helper(transport
);
564 * Yes, dup the pipe another time, as we need unbuffered version
565 * of input pipe as FILE*. fclose() closes the underlying fd and
566 * stream buffering only can be changed before first I/O operation
569 duped
= dup(helper
->out
);
571 die_errno("Can't dup helper output fd");
572 input
= xfdopen(duped
, "r");
573 setvbuf(input
, NULL
, _IONBF
, 0);
575 sendline(data
, cmdbuf
);
576 if (recvline_fh(input
, cmdbuf
))
579 if (!strcmp(cmdbuf
->buf
, "")) {
580 data
->no_disconnect_req
= 1;
582 fprintf(stderr
, "Debug: Smart transport connection "
585 } else if (!strcmp(cmdbuf
->buf
, "fallback")) {
587 fprintf(stderr
, "Debug: Falling back to dumb "
590 die("Unknown response to connect: %s",
598 static int process_connect_service(struct transport
*transport
,
599 const char *name
, const char *exec
)
601 struct helper_data
*data
= transport
->data
;
602 struct strbuf cmdbuf
= STRBUF_INIT
;
606 * Handle --upload-pack and friends. This is fire and forget...
607 * just warn if it fails.
609 if (strcmp(name
, exec
)) {
610 int r
= set_helper_option(transport
, "servpath", exec
);
612 warning("Setting remote service path not supported by protocol.");
614 warning("Invalid remote service path.");
618 strbuf_addf(&cmdbuf
, "connect %s\n", name
);
619 ret
= run_connect(transport
, &cmdbuf
);
620 } else if (data
->stateless_connect
&&
621 (get_protocol_version_config() == protocol_v2
) &&
622 !strcmp("git-upload-pack", name
)) {
623 strbuf_addf(&cmdbuf
, "stateless-connect %s\n", name
);
624 ret
= run_connect(transport
, &cmdbuf
);
626 transport
->stateless_rpc
= 1;
629 strbuf_release(&cmdbuf
);
633 static int process_connect(struct transport
*transport
,
636 struct helper_data
*data
= transport
->data
;
640 name
= for_push
? "git-receive-pack" : "git-upload-pack";
642 exec
= data
->transport_options
.receivepack
;
644 exec
= data
->transport_options
.uploadpack
;
646 return process_connect_service(transport
, name
, exec
);
649 static int connect_helper(struct transport
*transport
, const char *name
,
650 const char *exec
, int fd
[2])
652 struct helper_data
*data
= transport
->data
;
654 /* Get_helper so connect is inited. */
655 get_helper(transport
);
657 die("Operation not supported by protocol.");
659 if (!process_connect_service(transport
, name
, exec
))
660 die("Can't connect to subservice %s.", name
);
662 fd
[0] = data
->helper
->out
;
663 fd
[1] = data
->helper
->in
;
667 static int fetch(struct transport
*transport
,
668 int nr_heads
, struct ref
**to_fetch
)
670 struct helper_data
*data
= transport
->data
;
673 if (process_connect(transport
, 0)) {
674 do_take_over(transport
);
675 return transport
->vtable
->fetch(transport
, nr_heads
, to_fetch
);
679 for (i
= 0; i
< nr_heads
; i
++)
680 if (!(to_fetch
[i
]->status
& REF_STATUS_UPTODATE
))
686 if (data
->check_connectivity
&&
687 data
->transport_options
.check_self_contained_and_connected
)
688 set_helper_option(transport
, "check-connectivity", "true");
690 if (transport
->cloning
)
691 set_helper_option(transport
, "cloning", "true");
693 if (data
->transport_options
.update_shallow
)
694 set_helper_option(transport
, "update-shallow", "true");
696 if (data
->transport_options
.filter_options
.choice
)
699 data
->transport_options
.filter_options
.filter_spec
);
702 return fetch_with_fetch(transport
, nr_heads
, to_fetch
);
705 return fetch_with_import(transport
, nr_heads
, to_fetch
);
710 static int push_update_ref_status(struct strbuf
*buf
,
712 struct ref
*remote_refs
)
715 int status
, forced
= 0;
717 if (starts_with(buf
->buf
, "ok ")) {
718 status
= REF_STATUS_OK
;
719 refname
= buf
->buf
+ 3;
720 } else if (starts_with(buf
->buf
, "error ")) {
721 status
= REF_STATUS_REMOTE_REJECT
;
722 refname
= buf
->buf
+ 6;
724 die("expected ok/error, helper said '%s'", buf
->buf
);
726 msg
= strchr(refname
, ' ');
728 struct strbuf msg_buf
= STRBUF_INIT
;
732 if (!unquote_c_style(&msg_buf
, msg
, &end
))
733 msg
= strbuf_detach(&msg_buf
, NULL
);
736 strbuf_release(&msg_buf
);
738 if (!strcmp(msg
, "no match")) {
739 status
= REF_STATUS_NONE
;
742 else if (!strcmp(msg
, "up to date")) {
743 status
= REF_STATUS_UPTODATE
;
746 else if (!strcmp(msg
, "non-fast forward")) {
747 status
= REF_STATUS_REJECT_NONFASTFORWARD
;
750 else if (!strcmp(msg
, "already exists")) {
751 status
= REF_STATUS_REJECT_ALREADY_EXISTS
;
754 else if (!strcmp(msg
, "fetch first")) {
755 status
= REF_STATUS_REJECT_FETCH_FIRST
;
758 else if (!strcmp(msg
, "needs force")) {
759 status
= REF_STATUS_REJECT_NEEDS_FORCE
;
762 else if (!strcmp(msg
, "stale info")) {
763 status
= REF_STATUS_REJECT_STALE
;
766 else if (!strcmp(msg
, "forced update")) {
773 *ref
= find_ref_by_name(*ref
, refname
);
775 *ref
= find_ref_by_name(remote_refs
, refname
);
777 warning("helper reported unexpected status of %s", refname
);
781 if ((*ref
)->status
!= REF_STATUS_NONE
) {
783 * Earlier, the ref was marked not to be pushed, so ignore the ref
784 * status reported by the remote helper if the latter is 'no match'.
786 if (status
== REF_STATUS_NONE
)
790 (*ref
)->status
= status
;
791 (*ref
)->forced_update
|= forced
;
792 (*ref
)->remote_status
= msg
;
793 return !(status
== REF_STATUS_OK
);
796 static int push_update_refs_status(struct helper_data
*data
,
797 struct ref
*remote_refs
,
800 struct strbuf buf
= STRBUF_INIT
;
801 struct ref
*ref
= remote_refs
;
807 if (recvline(data
, &buf
)) {
815 if (push_update_ref_status(&buf
, &ref
, remote_refs
))
818 if (flags
& TRANSPORT_PUSH_DRY_RUN
|| !data
->refspecs
|| data
->no_private_update
)
821 /* propagate back the update to the remote namespace */
822 private = apply_refspecs(data
->refspecs
, data
->refspec_nr
, ref
->name
);
825 update_ref("update by helper", private, &ref
->new_oid
, NULL
,
829 strbuf_release(&buf
);
833 static void set_common_push_options(struct transport
*transport
,
834 const char *name
, int flags
)
836 if (flags
& TRANSPORT_PUSH_DRY_RUN
) {
837 if (set_helper_option(transport
, "dry-run", "true") != 0)
838 die("helper %s does not support dry-run", name
);
839 } else if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
) {
840 if (set_helper_option(transport
, TRANS_OPT_PUSH_CERT
, "true") != 0)
841 die("helper %s does not support --signed", name
);
842 } else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
) {
843 if (set_helper_option(transport
, TRANS_OPT_PUSH_CERT
, "if-asked") != 0)
844 die("helper %s does not support --signed=if-asked", name
);
847 if (flags
& TRANSPORT_PUSH_OPTIONS
) {
848 struct string_list_item
*item
;
849 for_each_string_list_item(item
, transport
->push_options
)
850 if (set_helper_option(transport
, "push-option", item
->string
) != 0)
851 die("helper %s does not support 'push-option'", name
);
855 static int push_refs_with_push(struct transport
*transport
,
856 struct ref
*remote_refs
, int flags
)
858 int force_all
= flags
& TRANSPORT_PUSH_FORCE
;
859 int mirror
= flags
& TRANSPORT_PUSH_MIRROR
;
860 struct helper_data
*data
= transport
->data
;
861 struct strbuf buf
= STRBUF_INIT
;
863 struct string_list cas_options
= STRING_LIST_INIT_DUP
;
864 struct string_list_item
*cas_option
;
866 get_helper(transport
);
870 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
871 if (!ref
->peer_ref
&& !mirror
)
874 /* Check for statuses set by set_ref_status_for_push() */
875 switch (ref
->status
) {
876 case REF_STATUS_REJECT_NONFASTFORWARD
:
877 case REF_STATUS_REJECT_STALE
:
878 case REF_STATUS_REJECT_ALREADY_EXISTS
:
879 case REF_STATUS_UPTODATE
:
888 strbuf_addstr(&buf
, "push ");
889 if (!ref
->deletion
) {
891 strbuf_addch(&buf
, '+');
893 strbuf_addstr(&buf
, ref
->peer_ref
->name
);
895 strbuf_addstr(&buf
, oid_to_hex(&ref
->new_oid
));
897 strbuf_addch(&buf
, ':');
898 strbuf_addstr(&buf
, ref
->name
);
899 strbuf_addch(&buf
, '\n');
902 * The "--force-with-lease" options without explicit
903 * values to expect have already been expanded into
904 * the ref->old_oid_expect[] field; we can ignore
905 * transport->smart_options->cas altogether and instead
906 * can enumerate them from the refs.
908 if (ref
->expect_old_sha1
) {
909 struct strbuf cas
= STRBUF_INIT
;
910 strbuf_addf(&cas
, "%s:%s",
911 ref
->name
, oid_to_hex(&ref
->old_oid_expect
));
912 string_list_append_nodup(&cas_options
,
913 strbuf_detach(&cas
, NULL
));
917 string_list_clear(&cas_options
, 0);
921 for_each_string_list_item(cas_option
, &cas_options
)
922 set_helper_option(transport
, "cas", cas_option
->string
);
923 set_common_push_options(transport
, data
->name
, flags
);
925 strbuf_addch(&buf
, '\n');
926 sendline(data
, &buf
);
927 strbuf_release(&buf
);
928 string_list_clear(&cas_options
, 0);
930 return push_update_refs_status(data
, remote_refs
, flags
);
933 static int push_refs_with_export(struct transport
*transport
,
934 struct ref
*remote_refs
, int flags
)
937 struct child_process
*helper
, exporter
;
938 struct helper_data
*data
= transport
->data
;
939 struct string_list revlist_args
= STRING_LIST_INIT_DUP
;
940 struct strbuf buf
= STRBUF_INIT
;
943 die("remote-helper doesn't support push; refspec needed");
945 set_common_push_options(transport
, data
->name
, flags
);
946 if (flags
& TRANSPORT_PUSH_FORCE
) {
947 if (set_helper_option(transport
, "force", "true") != 0)
948 warning("helper %s does not support 'force'", data
->name
);
951 helper
= get_helper(transport
);
953 write_constant(helper
->in
, "export\n");
955 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
957 struct object_id oid
;
959 private = apply_refspecs(data
->refspecs
, data
->refspec_nr
, ref
->name
);
960 if (private && !get_oid(private, &oid
)) {
961 strbuf_addf(&buf
, "^%s", private);
962 string_list_append_nodup(&revlist_args
,
963 strbuf_detach(&buf
, NULL
));
964 oidcpy(&ref
->old_oid
, &oid
);
969 if (strcmp(ref
->name
, ref
->peer_ref
->name
)) {
970 if (!ref
->deletion
) {
974 /* Follow symbolic refs (mainly for HEAD). */
975 name
= resolve_ref_unsafe(ref
->peer_ref
->name
,
978 if (!name
|| !(flag
& REF_ISSYMREF
))
979 name
= ref
->peer_ref
->name
;
981 strbuf_addf(&buf
, "%s:%s", name
, ref
->name
);
983 strbuf_addf(&buf
, ":%s", ref
->name
);
985 string_list_append(&revlist_args
, "--refspec");
986 string_list_append(&revlist_args
, buf
.buf
);
987 strbuf_release(&buf
);
990 string_list_append(&revlist_args
, ref
->peer_ref
->name
);
994 if (get_exporter(transport
, &exporter
, &revlist_args
))
995 die("Couldn't run fast-export");
997 string_list_clear(&revlist_args
, 1);
999 if (finish_command(&exporter
))
1000 die("Error while running fast-export");
1001 if (push_update_refs_status(data
, remote_refs
, flags
))
1004 if (data
->export_marks
) {
1005 strbuf_addf(&buf
, "%s.tmp", data
->export_marks
);
1006 rename(buf
.buf
, data
->export_marks
);
1007 strbuf_release(&buf
);
1013 static int push_refs(struct transport
*transport
,
1014 struct ref
*remote_refs
, int flags
)
1016 struct helper_data
*data
= transport
->data
;
1018 if (process_connect(transport
, 1)) {
1019 do_take_over(transport
);
1020 return transport
->vtable
->push_refs(transport
, remote_refs
, flags
);
1024 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
1025 "Perhaps you should specify a branch such as 'master'.\n");
1030 return push_refs_with_push(transport
, remote_refs
, flags
);
1033 return push_refs_with_export(transport
, remote_refs
, flags
);
1039 static int has_attribute(const char *attrs
, const char *attr
) {
1046 const char *space
= strchrnul(attrs
, ' ');
1047 if (len
== space
- attrs
&& !strncmp(attrs
, attr
, len
))
1055 static struct ref
*get_refs_list(struct transport
*transport
, int for_push
,
1056 const struct argv_array
*ref_prefixes
)
1058 struct helper_data
*data
= transport
->data
;
1059 struct child_process
*helper
;
1060 struct ref
*ret
= NULL
;
1061 struct ref
**tail
= &ret
;
1063 struct strbuf buf
= STRBUF_INIT
;
1065 helper
= get_helper(transport
);
1067 if (process_connect(transport
, for_push
)) {
1068 do_take_over(transport
);
1069 return transport
->vtable
->get_refs_list(transport
, for_push
, ref_prefixes
);
1072 if (data
->push
&& for_push
)
1073 write_str_in_full(helper
->in
, "list for-push\n");
1075 write_str_in_full(helper
->in
, "list\n");
1079 if (recvline(data
, &buf
))
1085 eov
= strchr(buf
.buf
, ' ');
1087 die("Malformed response in ref list: %s", buf
.buf
);
1088 eon
= strchr(eov
+ 1, ' ');
1092 *tail
= alloc_ref(eov
+ 1);
1093 if (buf
.buf
[0] == '@')
1094 (*tail
)->symref
= xstrdup(buf
.buf
+ 1);
1095 else if (buf
.buf
[0] != '?')
1096 get_oid_hex(buf
.buf
, &(*tail
)->old_oid
);
1098 if (has_attribute(eon
+ 1, "unchanged")) {
1099 (*tail
)->status
|= REF_STATUS_UPTODATE
;
1100 if (read_ref((*tail
)->name
, &(*tail
)->old_oid
) < 0)
1101 die(_("Could not read ref %s"),
1105 tail
= &((*tail
)->next
);
1108 fprintf(stderr
, "Debug: Read ref listing.\n");
1109 strbuf_release(&buf
);
1111 for (posn
= ret
; posn
; posn
= posn
->next
)
1112 resolve_remote_symref(posn
, ret
);
1117 static struct transport_vtable vtable
= {
1126 int transport_helper_init(struct transport
*transport
, const char *name
)
1128 struct helper_data
*data
= xcalloc(1, sizeof(*data
));
1131 transport_check_allowed(name
);
1133 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
1136 transport
->data
= data
;
1137 transport
->vtable
= &vtable
;
1138 transport
->smart_options
= &(data
->transport_options
);
1143 * Linux pipes can buffer 65536 bytes at once (and most platforms can
1144 * buffer less), so attempt reads and writes with up to that size.
1146 #define BUFFERSIZE 65536
1147 /* This should be enough to hold debugging message. */
1148 #define PBUFFERSIZE 8192
1150 /* Print bidirectional transfer loop debug message. */
1151 __attribute__((format (printf
, 1, 2)))
1152 static void transfer_debug(const char *fmt
, ...)
1155 * NEEDSWORK: This function is sometimes used from multiple threads, and
1156 * we end up using debug_enabled racily. That "should not matter" since
1157 * we always write the same value, but it's still wrong. This function
1158 * is listed in .tsan-suppressions for the time being.
1162 char msgbuf
[PBUFFERSIZE
];
1163 static int debug_enabled
= -1;
1165 if (debug_enabled
< 0)
1166 debug_enabled
= getenv("GIT_TRANSLOOP_DEBUG") ? 1 : 0;
1170 va_start(args
, fmt
);
1171 vsnprintf(msgbuf
, PBUFFERSIZE
, fmt
, args
);
1173 fprintf(stderr
, "Transfer loop debugging: %s\n", msgbuf
);
1176 /* Stream state: More data may be coming in this direction. */
1177 #define SSTATE_TRANSFERRING 0
1179 * Stream state: No more data coming in this direction, flushing rest of
1182 #define SSTATE_FLUSHING 1
1183 /* Stream state: Transfer in this direction finished. */
1184 #define SSTATE_FINISHED 2
1186 #define STATE_NEEDS_READING(state) ((state) <= SSTATE_TRANSFERRING)
1187 #define STATE_NEEDS_WRITING(state) ((state) <= SSTATE_FLUSHING)
1188 #define STATE_NEEDS_CLOSING(state) ((state) == SSTATE_FLUSHING)
1190 /* Unidirectional transfer. */
1191 struct unidirectional_transfer
{
1196 /* Is source socket? */
1198 /* Is destination socket? */
1200 /* Transfer state (TRANSFERRING/FLUSHING/FINISHED) */
1203 char buf
[BUFFERSIZE
];
1206 /* Name of source. */
1207 const char *src_name
;
1208 /* Name of destination. */
1209 const char *dest_name
;
1212 /* Closes the target (for writing) if transfer has finished. */
1213 static void udt_close_if_finished(struct unidirectional_transfer
*t
)
1215 if (STATE_NEEDS_CLOSING(t
->state
) && !t
->bufuse
) {
1216 t
->state
= SSTATE_FINISHED
;
1217 if (t
->dest_is_sock
)
1218 shutdown(t
->dest
, SHUT_WR
);
1221 transfer_debug("Closed %s.", t
->dest_name
);
1226 * Tries to read data from source into buffer. If buffer is full,
1227 * no data is read. Returns 0 on success, -1 on error.
1229 static int udt_do_read(struct unidirectional_transfer
*t
)
1233 if (t
->bufuse
== BUFFERSIZE
)
1234 return 0; /* No space for more. */
1236 transfer_debug("%s is readable", t
->src_name
);
1237 bytes
= read(t
->src
, t
->buf
+ t
->bufuse
, BUFFERSIZE
- t
->bufuse
);
1238 if (bytes
< 0 && errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
&&
1240 error_errno("read(%s) failed", t
->src_name
);
1242 } else if (bytes
== 0) {
1243 transfer_debug("%s EOF (with %i bytes in buffer)",
1244 t
->src_name
, (int)t
->bufuse
);
1245 t
->state
= SSTATE_FLUSHING
;
1246 } else if (bytes
> 0) {
1248 transfer_debug("Read %i bytes from %s (buffer now at %i)",
1249 (int)bytes
, t
->src_name
, (int)t
->bufuse
);
1254 /* Tries to write data from buffer into destination. If buffer is empty,
1255 * no data is written. Returns 0 on success, -1 on error.
1257 static int udt_do_write(struct unidirectional_transfer
*t
)
1262 return 0; /* Nothing to write. */
1264 transfer_debug("%s is writable", t
->dest_name
);
1265 bytes
= xwrite(t
->dest
, t
->buf
, t
->bufuse
);
1266 if (bytes
< 0 && errno
!= EWOULDBLOCK
) {
1267 error_errno("write(%s) failed", t
->dest_name
);
1269 } else if (bytes
> 0) {
1272 memmove(t
->buf
, t
->buf
+ bytes
, t
->bufuse
);
1273 transfer_debug("Wrote %i bytes to %s (buffer now at %i)",
1274 (int)bytes
, t
->dest_name
, (int)t
->bufuse
);
1280 /* State of bidirectional transfer loop. */
1281 struct bidirectional_transfer_state
{
1282 /* Direction from program to git. */
1283 struct unidirectional_transfer ptg
;
1284 /* Direction from git to program. */
1285 struct unidirectional_transfer gtp
;
1288 static void *udt_copy_task_routine(void *udt
)
1290 struct unidirectional_transfer
*t
= (struct unidirectional_transfer
*)udt
;
1291 while (t
->state
!= SSTATE_FINISHED
) {
1292 if (STATE_NEEDS_READING(t
->state
))
1295 if (STATE_NEEDS_WRITING(t
->state
))
1296 if (udt_do_write(t
))
1298 if (STATE_NEEDS_CLOSING(t
->state
))
1299 udt_close_if_finished(t
);
1301 return udt
; /* Just some non-NULL value. */
1307 * Join thread, with appropriate errors on failure. Name is name for the
1308 * thread (for error messages). Returns 0 on success, 1 on failure.
1310 static int tloop_join(pthread_t thread
, const char *name
)
1314 err
= pthread_join(thread
, &tret
);
1316 error("%s thread failed", name
);
1320 error("%s thread failed to join: %s", name
, strerror(err
));
1327 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1330 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1332 pthread_t gtp_thread
;
1333 pthread_t ptg_thread
;
1336 err
= pthread_create(>p_thread
, NULL
, udt_copy_task_routine
,
1339 die("Can't start thread for copying data: %s", strerror(err
));
1340 err
= pthread_create(&ptg_thread
, NULL
, udt_copy_task_routine
,
1343 die("Can't start thread for copying data: %s", strerror(err
));
1345 ret
|= tloop_join(gtp_thread
, "Git to program copy");
1346 ret
|= tloop_join(ptg_thread
, "Program to git copy");
1351 /* Close the source and target (for writing) for transfer. */
1352 static void udt_kill_transfer(struct unidirectional_transfer
*t
)
1354 t
->state
= SSTATE_FINISHED
;
1356 * Socket read end left open isn't a disaster if nobody
1357 * attempts to read from it (mingw compat headers do not
1360 * We can't fully close the socket since otherwise gtp
1361 * task would first close the socket it sends data to
1362 * while closing the ptg file descriptors.
1364 if (!t
->src_is_sock
)
1366 if (t
->dest_is_sock
)
1367 shutdown(t
->dest
, SHUT_WR
);
1373 * Join process, with appropriate errors on failure. Name is name for the
1374 * process (for error messages). Returns 0 on success, 1 on failure.
1376 static int tloop_join(pid_t pid
, const char *name
)
1379 if (waitpid(pid
, &tret
, 0) < 0) {
1380 error_errno("%s process failed to wait", name
);
1383 if (!WIFEXITED(tret
) || WEXITSTATUS(tret
)) {
1384 error("%s process failed", name
);
1391 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1394 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1399 /* Fork thread #1: git to program. */
1402 die_errno("Can't start thread for copying data");
1403 else if (pid1
== 0) {
1404 udt_kill_transfer(&s
->ptg
);
1405 exit(udt_copy_task_routine(&s
->gtp
) ? 0 : 1);
1408 /* Fork thread #2: program to git. */
1411 die_errno("Can't start thread for copying data");
1412 else if (pid2
== 0) {
1413 udt_kill_transfer(&s
->gtp
);
1414 exit(udt_copy_task_routine(&s
->ptg
) ? 0 : 1);
1418 * Close both streams in parent as to not interfere with
1419 * end of file detection and wait for both tasks to finish.
1421 udt_kill_transfer(&s
->gtp
);
1422 udt_kill_transfer(&s
->ptg
);
1423 ret
|= tloop_join(pid1
, "Git to program copy");
1424 ret
|= tloop_join(pid2
, "Program to git copy");
1430 * Copies data from stdin to output and from input to stdout simultaneously.
1431 * Additionally filtering through given filter. If filter is NULL, uses
1434 int bidirectional_transfer_loop(int input
, int output
)
1436 struct bidirectional_transfer_state state
;
1438 /* Fill the state fields. */
1439 state
.ptg
.src
= input
;
1441 state
.ptg
.src_is_sock
= (input
== output
);
1442 state
.ptg
.dest_is_sock
= 0;
1443 state
.ptg
.state
= SSTATE_TRANSFERRING
;
1444 state
.ptg
.bufuse
= 0;
1445 state
.ptg
.src_name
= "remote input";
1446 state
.ptg
.dest_name
= "stdout";
1449 state
.gtp
.dest
= output
;
1450 state
.gtp
.src_is_sock
= 0;
1451 state
.gtp
.dest_is_sock
= (input
== output
);
1452 state
.gtp
.state
= SSTATE_TRANSFERRING
;
1453 state
.gtp
.bufuse
= 0;
1454 state
.gtp
.src_name
= "stdin";
1455 state
.gtp
.dest_name
= "remote output";
1457 return tloop_spawnwait_tasks(&state
);