4 #include "run-command.h"
10 #include "string-list.h"
11 #include "thread-utils.h"
13 #include "argv-array.h"
19 struct child_process
*helper
;
29 no_disconnect_req
: 1;
32 /* These go from remote name (as in "list") to private name */
33 struct refspec
*refspecs
;
35 /* Transport options for fetch-pack/send-pack (should one of
38 struct git_transport_options transport_options
;
41 static void sendline(struct helper_data
*helper
, struct strbuf
*buffer
)
44 fprintf(stderr
, "Debug: Remote helper: -> %s", buffer
->buf
);
45 if (write_in_full(helper
->helper
->in
, buffer
->buf
, buffer
->len
)
47 die_errno("Full write to remote helper failed");
50 static int recvline_fh(FILE *helper
, struct strbuf
*buffer
)
54 fprintf(stderr
, "Debug: Remote helper: Waiting...\n");
55 if (strbuf_getline(buffer
, helper
, '\n') == EOF
) {
57 fprintf(stderr
, "Debug: Remote helper quit.\n");
62 fprintf(stderr
, "Debug: Remote helper: <- %s\n", buffer
->buf
);
66 static int recvline(struct helper_data
*helper
, struct strbuf
*buffer
)
68 return recvline_fh(helper
->out
, buffer
);
71 static void xchgline(struct helper_data
*helper
, struct strbuf
*buffer
)
73 sendline(helper
, buffer
);
74 recvline(helper
, buffer
);
77 static void write_constant(int fd
, const char *str
)
80 fprintf(stderr
, "Debug: Remote helper: -> %s", str
);
81 if (write_in_full(fd
, str
, strlen(str
)) != strlen(str
))
82 die_errno("Full write to remote helper failed");
85 static const char *remove_ext_force(const char *url
)
88 const char *colon
= strchr(url
, ':');
89 if (colon
&& colon
[1] == ':')
95 static void do_take_over(struct transport
*transport
)
97 struct helper_data
*data
;
98 data
= (struct helper_data
*)transport
->data
;
99 transport_take_over(transport
, data
->helper
);
104 static struct child_process
*get_helper(struct transport
*transport
)
106 struct helper_data
*data
= transport
->data
;
107 struct argv_array argv
= ARGV_ARRAY_INIT
;
108 struct strbuf buf
= STRBUF_INIT
;
109 struct child_process
*helper
;
110 const char **refspecs
= NULL
;
112 int refspec_alloc
= 0;
115 char git_dir_buf
[sizeof(GIT_DIR_ENVIRONMENT
) + PATH_MAX
+ 1];
116 const char *helper_env
[] = {
125 helper
= xcalloc(1, sizeof(*helper
));
129 argv_array_pushf(&argv
, "git-remote-%s", data
->name
);
130 argv_array_push(&argv
, transport
->remote
->name
);
131 argv_array_push(&argv
, remove_ext_force(transport
->url
));
132 helper
->argv
= argv_array_detach(&argv
, NULL
);
134 helper
->silent_exec_failure
= 1;
136 snprintf(git_dir_buf
, sizeof(git_dir_buf
), "%s=%s", GIT_DIR_ENVIRONMENT
, get_git_dir());
137 helper
->env
= helper_env
;
139 code
= start_command(helper
);
140 if (code
< 0 && errno
== ENOENT
)
141 die("Unable to find remote helper for '%s'", data
->name
);
145 data
->helper
= helper
;
146 data
->no_disconnect_req
= 0;
149 * Open the output as FILE* so strbuf_getline() can be used.
150 * Do this with duped fd because fclose() will close the fd,
151 * and stuff like taking over will require the fd to remain.
153 duped
= dup(helper
->out
);
155 die_errno("Can't dup helper output fd");
156 data
->out
= xfdopen(duped
, "r");
158 write_constant(helper
->in
, "capabilities\n");
163 recvline(data
, &buf
);
168 if (*buf
.buf
== '*') {
169 capname
= buf
.buf
+ 1;
175 fprintf(stderr
, "Debug: Got cap %s\n", capname
);
176 if (!strcmp(capname
, "fetch"))
178 else if (!strcmp(capname
, "option"))
180 else if (!strcmp(capname
, "push"))
182 else if (!strcmp(capname
, "import"))
184 else if (!strcmp(capname
, "bidi-import"))
185 data
->bidi_import
= 1;
186 else if (!strcmp(capname
, "export"))
188 else if (!data
->refspecs
&& !prefixcmp(capname
, "refspec ")) {
192 refspecs
[refspec_nr
++] = xstrdup(capname
+ strlen("refspec "));
193 } else if (!strcmp(capname
, "connect")) {
195 } else if (!strcmp(capname
, "signed-tags")) {
196 data
->signed_tags
= 1;
197 } else if (!prefixcmp(capname
, "export-marks ")) {
198 struct strbuf arg
= STRBUF_INIT
;
199 strbuf_addstr(&arg
, "--export-marks=");
200 strbuf_addstr(&arg
, capname
+ strlen("export-marks "));
201 data
->export_marks
= strbuf_detach(&arg
, NULL
);
202 } else if (!prefixcmp(capname
, "import-marks")) {
203 struct strbuf arg
= STRBUF_INIT
;
204 strbuf_addstr(&arg
, "--import-marks=");
205 strbuf_addstr(&arg
, capname
+ strlen("import-marks "));
206 data
->import_marks
= strbuf_detach(&arg
, NULL
);
207 } else if (mandatory
) {
208 die("Unknown mandatory capability %s. This remote "
209 "helper probably needs newer version of Git.",
215 data
->refspec_nr
= refspec_nr
;
216 data
->refspecs
= parse_fetch_refspec(refspec_nr
, refspecs
);
217 for (i
= 0; i
< refspec_nr
; i
++)
218 free((char *)refspecs
[i
]);
221 strbuf_release(&buf
);
223 fprintf(stderr
, "Debug: Capabilities complete.\n");
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 argv_array_free_detached(data
->helper
->argv
);
257 static const char *unsupported_options
[] = {
258 TRANS_OPT_UPLOADPACK
,
259 TRANS_OPT_RECEIVEPACK
,
263 static const char *boolean_options
[] = {
269 static int set_helper_option(struct transport
*transport
,
270 const char *name
, const char *value
)
272 struct helper_data
*data
= transport
->data
;
273 struct strbuf buf
= STRBUF_INIT
;
274 int i
, ret
, is_bool
= 0;
276 get_helper(transport
);
281 for (i
= 0; i
< ARRAY_SIZE(unsupported_options
); i
++) {
282 if (!strcmp(name
, unsupported_options
[i
]))
286 for (i
= 0; i
< ARRAY_SIZE(boolean_options
); i
++) {
287 if (!strcmp(name
, boolean_options
[i
])) {
293 strbuf_addf(&buf
, "option %s ", name
);
295 strbuf_addstr(&buf
, value
? "true" : "false");
297 quote_c_style(value
, &buf
, NULL
, 0);
298 strbuf_addch(&buf
, '\n');
300 xchgline(data
, &buf
);
302 if (!strcmp(buf
.buf
, "ok"))
304 else if (!prefixcmp(buf
.buf
, "error")) {
306 } else if (!strcmp(buf
.buf
, "unsupported"))
309 warning("%s unexpectedly said: '%s'", data
->name
, buf
.buf
);
312 strbuf_release(&buf
);
316 static void standard_options(struct transport
*t
)
322 set_helper_option(t
, "progress", t
->progress
? "true" : "false");
324 n
= snprintf(buf
, sizeof(buf
), "%d", v
+ 1);
325 if (n
>= sizeof(buf
))
326 die("impossibly large verbosity value");
327 set_helper_option(t
, "verbosity", buf
);
330 static int release_helper(struct transport
*transport
)
333 struct helper_data
*data
= transport
->data
;
334 free_refspec(data
->refspec_nr
, data
->refspecs
);
335 data
->refspecs
= NULL
;
336 res
= disconnect_helper(transport
);
337 free(transport
->data
);
341 static int fetch_with_fetch(struct transport
*transport
,
342 int nr_heads
, struct ref
**to_fetch
)
344 struct helper_data
*data
= transport
->data
;
346 struct strbuf buf
= STRBUF_INIT
;
348 standard_options(transport
);
350 for (i
= 0; i
< nr_heads
; i
++) {
351 const struct ref
*posn
= to_fetch
[i
];
352 if (posn
->status
& REF_STATUS_UPTODATE
)
355 strbuf_addf(&buf
, "fetch %s %s\n",
356 sha1_to_hex(posn
->old_sha1
), posn
->name
);
359 strbuf_addch(&buf
, '\n');
360 sendline(data
, &buf
);
363 recvline(data
, &buf
);
365 if (!prefixcmp(buf
.buf
, "lock ")) {
366 const char *name
= buf
.buf
+ 5;
367 if (transport
->pack_lockfile
)
368 warning("%s also locked %s", data
->name
, name
);
370 transport
->pack_lockfile
= xstrdup(name
);
375 warning("%s unexpectedly said: '%s'", data
->name
, buf
.buf
);
377 strbuf_release(&buf
);
381 static int get_importer(struct transport
*transport
, struct child_process
*fastimport
)
383 struct child_process
*helper
= get_helper(transport
);
384 struct helper_data
*data
= transport
->data
;
385 struct argv_array argv
= ARGV_ARRAY_INIT
;
386 int cat_blob_fd
, code
;
387 memset(fastimport
, 0, sizeof(*fastimport
));
388 fastimport
->in
= helper
->out
;
389 argv_array_push(&argv
, "fast-import");
390 argv_array_push(&argv
, debug
? "--stats" : "--quiet");
392 if (data
->bidi_import
) {
393 cat_blob_fd
= xdup(helper
->in
);
394 argv_array_pushf(&argv
, "--cat-blob-fd=%d", cat_blob_fd
);
396 fastimport
->argv
= argv
.argv
;
397 fastimport
->git_cmd
= 1;
399 code
= start_command(fastimport
);
403 static int get_exporter(struct transport
*transport
,
404 struct child_process
*fastexport
,
405 struct string_list
*revlist_args
)
407 struct helper_data
*data
= transport
->data
;
408 struct child_process
*helper
= get_helper(transport
);
410 memset(fastexport
, 0, sizeof(*fastexport
));
412 /* we need to duplicate helper->in because we want to use it after
413 * fastexport is done with it. */
414 fastexport
->out
= dup(helper
->in
);
415 fastexport
->argv
= xcalloc(6 + revlist_args
->nr
, sizeof(*fastexport
->argv
));
416 fastexport
->argv
[argc
++] = "fast-export";
417 fastexport
->argv
[argc
++] = "--use-done-feature";
418 fastexport
->argv
[argc
++] = data
->signed_tags
?
419 "--signed-tags=verbatim" : "--signed-tags=warn-strip";
420 if (data
->export_marks
)
421 fastexport
->argv
[argc
++] = data
->export_marks
;
422 if (data
->import_marks
)
423 fastexport
->argv
[argc
++] = data
->import_marks
;
425 for (i
= 0; i
< revlist_args
->nr
; i
++)
426 fastexport
->argv
[argc
++] = revlist_args
->items
[i
].string
;
428 fastexport
->git_cmd
= 1;
429 return start_command(fastexport
);
432 static int fetch_with_import(struct transport
*transport
,
433 int nr_heads
, struct ref
**to_fetch
)
435 struct child_process fastimport
;
436 struct helper_data
*data
= transport
->data
;
439 struct strbuf buf
= STRBUF_INIT
;
441 get_helper(transport
);
443 if (get_importer(transport
, &fastimport
))
444 die("Couldn't run fast-import");
446 for (i
= 0; i
< nr_heads
; i
++) {
448 if (posn
->status
& REF_STATUS_UPTODATE
)
451 strbuf_addf(&buf
, "import %s\n", posn
->name
);
452 sendline(data
, &buf
);
456 write_constant(data
->helper
->in
, "\n");
458 * remote-helpers that advertise the bidi-import capability are required to
459 * buffer the complete batch of import commands until this newline before
460 * sending data to fast-import.
461 * These helpers read back data from fast-import on their stdin, which could
462 * be mixed with import commands, otherwise.
465 if (finish_command(&fastimport
))
466 die("Error while running fast-import");
467 argv_array_free_detached(fastimport
.argv
);
470 * The fast-import stream of a remote helper that advertises
471 * the "refspec" capability writes to the refs named after the
472 * right hand side of the first refspec matching each ref we
475 * (If no "refspec" capability was specified, for historical
476 * reasons we default to *:*.)
478 * Store the result in to_fetch[i].old_sha1. Callers such
479 * as "git fetch" can use the value to write feedback to the
480 * terminal, populate FETCH_HEAD, and determine what new value
481 * should be written to peer_ref if the update is a
482 * fast-forward or this is a forced update.
484 for (i
= 0; i
< nr_heads
; i
++) {
487 if (posn
->status
& REF_STATUS_UPTODATE
)
490 private = apply_refspecs(data
->refspecs
, data
->refspec_nr
, posn
->name
);
492 private = xstrdup(posn
->name
);
494 read_ref(private, posn
->old_sha1
);
498 strbuf_release(&buf
);
502 static int process_connect_service(struct transport
*transport
,
503 const char *name
, const char *exec
)
505 struct helper_data
*data
= transport
->data
;
506 struct strbuf cmdbuf
= STRBUF_INIT
;
507 struct child_process
*helper
;
508 int r
, duped
, ret
= 0;
511 helper
= get_helper(transport
);
514 * Yes, dup the pipe another time, as we need unbuffered version
515 * of input pipe as FILE*. fclose() closes the underlying fd and
516 * stream buffering only can be changed before first I/O operation
519 duped
= dup(helper
->out
);
521 die_errno("Can't dup helper output fd");
522 input
= xfdopen(duped
, "r");
523 setvbuf(input
, NULL
, _IONBF
, 0);
526 * Handle --upload-pack and friends. This is fire and forget...
527 * just warn if it fails.
529 if (strcmp(name
, exec
)) {
530 r
= set_helper_option(transport
, "servpath", exec
);
532 warning("Setting remote service path not supported by protocol.");
534 warning("Invalid remote service path.");
538 strbuf_addf(&cmdbuf
, "connect %s\n", name
);
542 sendline(data
, &cmdbuf
);
543 recvline_fh(input
, &cmdbuf
);
544 if (!strcmp(cmdbuf
.buf
, "")) {
545 data
->no_disconnect_req
= 1;
547 fprintf(stderr
, "Debug: Smart transport connection "
550 } else if (!strcmp(cmdbuf
.buf
, "fallback")) {
552 fprintf(stderr
, "Debug: Falling back to dumb "
555 die("Unknown response to connect: %s",
563 static int process_connect(struct transport
*transport
,
566 struct helper_data
*data
= transport
->data
;
570 name
= for_push
? "git-receive-pack" : "git-upload-pack";
572 exec
= data
->transport_options
.receivepack
;
574 exec
= data
->transport_options
.uploadpack
;
576 return process_connect_service(transport
, name
, exec
);
579 static int connect_helper(struct transport
*transport
, const char *name
,
580 const char *exec
, int fd
[2])
582 struct helper_data
*data
= transport
->data
;
584 /* Get_helper so connect is inited. */
585 get_helper(transport
);
587 die("Operation not supported by protocol.");
589 if (!process_connect_service(transport
, name
, exec
))
590 die("Can't connect to subservice %s.", name
);
592 fd
[0] = data
->helper
->out
;
593 fd
[1] = data
->helper
->in
;
597 static int fetch(struct transport
*transport
,
598 int nr_heads
, struct ref
**to_fetch
)
600 struct helper_data
*data
= transport
->data
;
603 if (process_connect(transport
, 0)) {
604 do_take_over(transport
);
605 return transport
->fetch(transport
, nr_heads
, to_fetch
);
609 for (i
= 0; i
< nr_heads
; i
++)
610 if (!(to_fetch
[i
]->status
& REF_STATUS_UPTODATE
))
617 return fetch_with_fetch(transport
, nr_heads
, to_fetch
);
620 return fetch_with_import(transport
, nr_heads
, to_fetch
);
625 static void push_update_ref_status(struct strbuf
*buf
,
627 struct ref
*remote_refs
)
632 if (!prefixcmp(buf
->buf
, "ok ")) {
633 status
= REF_STATUS_OK
;
634 refname
= buf
->buf
+ 3;
635 } else if (!prefixcmp(buf
->buf
, "error ")) {
636 status
= REF_STATUS_REMOTE_REJECT
;
637 refname
= buf
->buf
+ 6;
639 die("expected ok/error, helper said '%s'", buf
->buf
);
641 msg
= strchr(refname
, ' ');
643 struct strbuf msg_buf
= STRBUF_INIT
;
647 if (!unquote_c_style(&msg_buf
, msg
, &end
))
648 msg
= strbuf_detach(&msg_buf
, NULL
);
651 strbuf_release(&msg_buf
);
653 if (!strcmp(msg
, "no match")) {
654 status
= REF_STATUS_NONE
;
658 else if (!strcmp(msg
, "up to date")) {
659 status
= REF_STATUS_UPTODATE
;
663 else if (!strcmp(msg
, "non-fast forward")) {
664 status
= REF_STATUS_REJECT_NONFASTFORWARD
;
668 else if (!strcmp(msg
, "already exists")) {
669 status
= REF_STATUS_REJECT_ALREADY_EXISTS
;
673 else if (!strcmp(msg
, "fetch first")) {
674 status
= REF_STATUS_REJECT_FETCH_FIRST
;
678 else if (!strcmp(msg
, "needs force")) {
679 status
= REF_STATUS_REJECT_NEEDS_FORCE
;
686 *ref
= find_ref_by_name(*ref
, refname
);
688 *ref
= find_ref_by_name(remote_refs
, refname
);
690 warning("helper reported unexpected status of %s", refname
);
694 if ((*ref
)->status
!= REF_STATUS_NONE
) {
696 * Earlier, the ref was marked not to be pushed, so ignore the ref
697 * status reported by the remote helper if the latter is 'no match'.
699 if (status
== REF_STATUS_NONE
)
703 (*ref
)->status
= status
;
704 (*ref
)->remote_status
= msg
;
707 static void push_update_refs_status(struct helper_data
*data
,
708 struct ref
*remote_refs
)
710 struct strbuf buf
= STRBUF_INIT
;
711 struct ref
*ref
= remote_refs
;
713 recvline(data
, &buf
);
717 push_update_ref_status(&buf
, &ref
, remote_refs
);
719 strbuf_release(&buf
);
722 static int push_refs_with_push(struct transport
*transport
,
723 struct ref
*remote_refs
, int flags
)
725 int force_all
= flags
& TRANSPORT_PUSH_FORCE
;
726 int mirror
= flags
& TRANSPORT_PUSH_MIRROR
;
727 struct helper_data
*data
= transport
->data
;
728 struct strbuf buf
= STRBUF_INIT
;
731 get_helper(transport
);
735 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
736 if (!ref
->peer_ref
&& !mirror
)
739 /* Check for statuses set by set_ref_status_for_push() */
740 switch (ref
->status
) {
741 case REF_STATUS_REJECT_NONFASTFORWARD
:
742 case REF_STATUS_REJECT_ALREADY_EXISTS
:
743 case REF_STATUS_UPTODATE
:
752 strbuf_addstr(&buf
, "push ");
753 if (!ref
->deletion
) {
755 strbuf_addch(&buf
, '+');
757 strbuf_addstr(&buf
, ref
->peer_ref
->name
);
759 strbuf_addstr(&buf
, sha1_to_hex(ref
->new_sha1
));
761 strbuf_addch(&buf
, ':');
762 strbuf_addstr(&buf
, ref
->name
);
763 strbuf_addch(&buf
, '\n');
768 standard_options(transport
);
770 if (flags
& TRANSPORT_PUSH_DRY_RUN
) {
771 if (set_helper_option(transport
, "dry-run", "true") != 0)
772 die("helper %s does not support dry-run", data
->name
);
775 strbuf_addch(&buf
, '\n');
776 sendline(data
, &buf
);
777 strbuf_release(&buf
);
779 push_update_refs_status(data
, remote_refs
);
783 static int push_refs_with_export(struct transport
*transport
,
784 struct ref
*remote_refs
, int flags
)
787 struct child_process
*helper
, exporter
;
788 struct helper_data
*data
= transport
->data
;
789 struct string_list revlist_args
= STRING_LIST_INIT_NODUP
;
790 struct strbuf buf
= STRBUF_INIT
;
792 helper
= get_helper(transport
);
794 write_constant(helper
->in
, "export\n");
798 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
800 unsigned char sha1
[20];
804 private = apply_refspecs(data
->refspecs
, data
->refspec_nr
, ref
->name
);
805 if (private && !get_sha1(private, sha1
)) {
806 strbuf_addf(&buf
, "^%s", private);
807 string_list_append(&revlist_args
, strbuf_detach(&buf
, NULL
));
808 hashcpy(ref
->old_sha1
, sha1
);
813 die("remote-helpers do not support ref deletion");
817 string_list_append(&revlist_args
, ref
->peer_ref
->name
);
821 if (get_exporter(transport
, &exporter
, &revlist_args
))
822 die("Couldn't run fast-export");
824 if (finish_command(&exporter
))
825 die("Error while running fast-export");
826 push_update_refs_status(data
, remote_refs
);
830 static int push_refs(struct transport
*transport
,
831 struct ref
*remote_refs
, int flags
)
833 struct helper_data
*data
= transport
->data
;
835 if (process_connect(transport
, 1)) {
836 do_take_over(transport
);
837 return transport
->push_refs(transport
, remote_refs
, flags
);
841 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
842 "Perhaps you should specify a branch such as 'master'.\n");
847 return push_refs_with_push(transport
, remote_refs
, flags
);
850 return push_refs_with_export(transport
, remote_refs
, flags
);
856 static int has_attribute(const char *attrs
, const char *attr
) {
863 const char *space
= strchrnul(attrs
, ' ');
864 if (len
== space
- attrs
&& !strncmp(attrs
, attr
, len
))
872 static struct ref
*get_refs_list(struct transport
*transport
, int for_push
)
874 struct helper_data
*data
= transport
->data
;
875 struct child_process
*helper
;
876 struct ref
*ret
= NULL
;
877 struct ref
**tail
= &ret
;
879 struct strbuf buf
= STRBUF_INIT
;
881 helper
= get_helper(transport
);
883 if (process_connect(transport
, for_push
)) {
884 do_take_over(transport
);
885 return transport
->get_refs_list(transport
, for_push
);
888 if (data
->push
&& for_push
)
889 write_str_in_full(helper
->in
, "list for-push\n");
891 write_str_in_full(helper
->in
, "list\n");
895 recvline(data
, &buf
);
900 eov
= strchr(buf
.buf
, ' ');
902 die("Malformed response in ref list: %s", buf
.buf
);
903 eon
= strchr(eov
+ 1, ' ');
907 *tail
= alloc_ref(eov
+ 1);
908 if (buf
.buf
[0] == '@')
909 (*tail
)->symref
= xstrdup(buf
.buf
+ 1);
910 else if (buf
.buf
[0] != '?')
911 get_sha1_hex(buf
.buf
, (*tail
)->old_sha1
);
913 if (has_attribute(eon
+ 1, "unchanged")) {
914 (*tail
)->status
|= REF_STATUS_UPTODATE
;
915 read_ref((*tail
)->name
, (*tail
)->old_sha1
);
918 tail
= &((*tail
)->next
);
921 fprintf(stderr
, "Debug: Read ref listing.\n");
922 strbuf_release(&buf
);
924 for (posn
= ret
; posn
; posn
= posn
->next
)
925 resolve_remote_symref(posn
, ret
);
930 int transport_helper_init(struct transport
*transport
, const char *name
)
932 struct helper_data
*data
= xcalloc(sizeof(*data
), 1);
935 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
938 transport
->data
= data
;
939 transport
->set_option
= set_helper_option
;
940 transport
->get_refs_list
= get_refs_list
;
941 transport
->fetch
= fetch
;
942 transport
->push_refs
= push_refs
;
943 transport
->disconnect
= release_helper
;
944 transport
->connect
= connect_helper
;
945 transport
->smart_options
= &(data
->transport_options
);
950 * Linux pipes can buffer 65536 bytes at once (and most platforms can
951 * buffer less), so attempt reads and writes with up to that size.
953 #define BUFFERSIZE 65536
954 /* This should be enough to hold debugging message. */
955 #define PBUFFERSIZE 8192
957 /* Print bidirectional transfer loop debug message. */
958 __attribute__((format (printf
, 1, 2)))
959 static void transfer_debug(const char *fmt
, ...)
962 char msgbuf
[PBUFFERSIZE
];
963 static int debug_enabled
= -1;
965 if (debug_enabled
< 0)
966 debug_enabled
= getenv("GIT_TRANSLOOP_DEBUG") ? 1 : 0;
971 vsnprintf(msgbuf
, PBUFFERSIZE
, fmt
, args
);
973 fprintf(stderr
, "Transfer loop debugging: %s\n", msgbuf
);
976 /* Stream state: More data may be coming in this direction. */
977 #define SSTATE_TRANSFERING 0
979 * Stream state: No more data coming in this direction, flushing rest of
982 #define SSTATE_FLUSHING 1
983 /* Stream state: Transfer in this direction finished. */
984 #define SSTATE_FINISHED 2
986 #define STATE_NEEDS_READING(state) ((state) <= SSTATE_TRANSFERING)
987 #define STATE_NEEDS_WRITING(state) ((state) <= SSTATE_FLUSHING)
988 #define STATE_NEEDS_CLOSING(state) ((state) == SSTATE_FLUSHING)
990 /* Unidirectional transfer. */
991 struct unidirectional_transfer
{
996 /* Is source socket? */
998 /* Is destination socket? */
1000 /* Transfer state (TRANSFERRING/FLUSHING/FINISHED) */
1003 char buf
[BUFFERSIZE
];
1006 /* Name of source. */
1007 const char *src_name
;
1008 /* Name of destination. */
1009 const char *dest_name
;
1012 /* Closes the target (for writing) if transfer has finished. */
1013 static void udt_close_if_finished(struct unidirectional_transfer
*t
)
1015 if (STATE_NEEDS_CLOSING(t
->state
) && !t
->bufuse
) {
1016 t
->state
= SSTATE_FINISHED
;
1017 if (t
->dest_is_sock
)
1018 shutdown(t
->dest
, SHUT_WR
);
1021 transfer_debug("Closed %s.", t
->dest_name
);
1026 * Tries to read read data from source into buffer. If buffer is full,
1027 * no data is read. Returns 0 on success, -1 on error.
1029 static int udt_do_read(struct unidirectional_transfer
*t
)
1033 if (t
->bufuse
== BUFFERSIZE
)
1034 return 0; /* No space for more. */
1036 transfer_debug("%s is readable", t
->src_name
);
1037 bytes
= read(t
->src
, t
->buf
+ t
->bufuse
, BUFFERSIZE
- t
->bufuse
);
1038 if (bytes
< 0 && errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
&&
1040 error("read(%s) failed: %s", t
->src_name
, strerror(errno
));
1042 } else if (bytes
== 0) {
1043 transfer_debug("%s EOF (with %i bytes in buffer)",
1044 t
->src_name
, (int)t
->bufuse
);
1045 t
->state
= SSTATE_FLUSHING
;
1046 } else if (bytes
> 0) {
1048 transfer_debug("Read %i bytes from %s (buffer now at %i)",
1049 (int)bytes
, t
->src_name
, (int)t
->bufuse
);
1054 /* Tries to write data from buffer into destination. If buffer is empty,
1055 * no data is written. Returns 0 on success, -1 on error.
1057 static int udt_do_write(struct unidirectional_transfer
*t
)
1062 return 0; /* Nothing to write. */
1064 transfer_debug("%s is writable", t
->dest_name
);
1065 bytes
= write(t
->dest
, t
->buf
, t
->bufuse
);
1066 if (bytes
< 0 && errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
&&
1068 error("write(%s) failed: %s", t
->dest_name
, strerror(errno
));
1070 } else if (bytes
> 0) {
1073 memmove(t
->buf
, t
->buf
+ bytes
, t
->bufuse
);
1074 transfer_debug("Wrote %i bytes to %s (buffer now at %i)",
1075 (int)bytes
, t
->dest_name
, (int)t
->bufuse
);
1081 /* State of bidirectional transfer loop. */
1082 struct bidirectional_transfer_state
{
1083 /* Direction from program to git. */
1084 struct unidirectional_transfer ptg
;
1085 /* Direction from git to program. */
1086 struct unidirectional_transfer gtp
;
1089 static void *udt_copy_task_routine(void *udt
)
1091 struct unidirectional_transfer
*t
= (struct unidirectional_transfer
*)udt
;
1092 while (t
->state
!= SSTATE_FINISHED
) {
1093 if (STATE_NEEDS_READING(t
->state
))
1096 if (STATE_NEEDS_WRITING(t
->state
))
1097 if (udt_do_write(t
))
1099 if (STATE_NEEDS_CLOSING(t
->state
))
1100 udt_close_if_finished(t
);
1102 return udt
; /* Just some non-NULL value. */
1108 * Join thread, with apporiate errors on failure. Name is name for the
1109 * thread (for error messages). Returns 0 on success, 1 on failure.
1111 static int tloop_join(pthread_t thread
, const char *name
)
1115 err
= pthread_join(thread
, &tret
);
1117 error("%s thread failed", name
);
1121 error("%s thread failed to join: %s", name
, strerror(err
));
1128 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1131 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1133 pthread_t gtp_thread
;
1134 pthread_t ptg_thread
;
1137 err
= pthread_create(>p_thread
, NULL
, udt_copy_task_routine
,
1140 die("Can't start thread for copying data: %s", strerror(err
));
1141 err
= pthread_create(&ptg_thread
, NULL
, udt_copy_task_routine
,
1144 die("Can't start thread for copying data: %s", strerror(err
));
1146 ret
|= tloop_join(gtp_thread
, "Git to program copy");
1147 ret
|= tloop_join(ptg_thread
, "Program to git copy");
1152 /* Close the source and target (for writing) for transfer. */
1153 static void udt_kill_transfer(struct unidirectional_transfer
*t
)
1155 t
->state
= SSTATE_FINISHED
;
1157 * Socket read end left open isn't a disaster if nobody
1158 * attempts to read from it (mingw compat headers do not
1161 * We can't fully close the socket since otherwise gtp
1162 * task would first close the socket it sends data to
1163 * while closing the ptg file descriptors.
1165 if (!t
->src_is_sock
)
1167 if (t
->dest_is_sock
)
1168 shutdown(t
->dest
, SHUT_WR
);
1174 * Join process, with apporiate errors on failure. Name is name for the
1175 * process (for error messages). Returns 0 on success, 1 on failure.
1177 static int tloop_join(pid_t pid
, const char *name
)
1180 if (waitpid(pid
, &tret
, 0) < 0) {
1181 error("%s process failed to wait: %s", name
, strerror(errno
));
1184 if (!WIFEXITED(tret
) || WEXITSTATUS(tret
)) {
1185 error("%s process failed", name
);
1192 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1195 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1200 /* Fork thread #1: git to program. */
1203 die_errno("Can't start thread for copying data");
1204 else if (pid1
== 0) {
1205 udt_kill_transfer(&s
->ptg
);
1206 exit(udt_copy_task_routine(&s
->gtp
) ? 0 : 1);
1209 /* Fork thread #2: program to git. */
1212 die_errno("Can't start thread for copying data");
1213 else if (pid2
== 0) {
1214 udt_kill_transfer(&s
->gtp
);
1215 exit(udt_copy_task_routine(&s
->ptg
) ? 0 : 1);
1219 * Close both streams in parent as to not interfere with
1220 * end of file detection and wait for both tasks to finish.
1222 udt_kill_transfer(&s
->gtp
);
1223 udt_kill_transfer(&s
->ptg
);
1224 ret
|= tloop_join(pid1
, "Git to program copy");
1225 ret
|= tloop_join(pid2
, "Program to git copy");
1231 * Copies data from stdin to output and from input to stdout simultaneously.
1232 * Additionally filtering through given filter. If filter is NULL, uses
1235 int bidirectional_transfer_loop(int input
, int output
)
1237 struct bidirectional_transfer_state state
;
1239 /* Fill the state fields. */
1240 state
.ptg
.src
= input
;
1242 state
.ptg
.src_is_sock
= (input
== output
);
1243 state
.ptg
.dest_is_sock
= 0;
1244 state
.ptg
.state
= SSTATE_TRANSFERING
;
1245 state
.ptg
.bufuse
= 0;
1246 state
.ptg
.src_name
= "remote input";
1247 state
.ptg
.dest_name
= "stdout";
1250 state
.gtp
.dest
= output
;
1251 state
.gtp
.src_is_sock
= 0;
1252 state
.gtp
.dest_is_sock
= (input
== output
);
1253 state
.gtp
.state
= SSTATE_TRANSFERING
;
1254 state
.gtp
.bufuse
= 0;
1255 state
.gtp
.src_name
= "stdin";
1256 state
.gtp
.dest_name
= "remote output";
1258 return tloop_spawnwait_tasks(&state
);