4 #include "run-command.h"
10 #include "string-list.h"
11 #include "thread-utils.h"
13 #include "argv-array.h"
16 /* TODO: put somewhere sensible, e.g. git_transport_options? */
17 static int auto_gc
= 1;
21 struct child_process
*helper
;
31 no_disconnect_req
: 1;
34 /* These go from remote name (as in "list") to private name */
35 struct refspec
*refspecs
;
37 /* Transport options for fetch-pack/send-pack (should one of
40 struct git_transport_options transport_options
;
43 static void sendline(struct helper_data
*helper
, struct strbuf
*buffer
)
46 fprintf(stderr
, "Debug: Remote helper: -> %s", buffer
->buf
);
47 if (write_in_full(helper
->helper
->in
, buffer
->buf
, buffer
->len
)
49 die_errno("Full write to remote helper failed");
52 static int recvline_fh(FILE *helper
, struct strbuf
*buffer
)
56 fprintf(stderr
, "Debug: Remote helper: Waiting...\n");
57 if (strbuf_getline(buffer
, helper
, '\n') == EOF
) {
59 fprintf(stderr
, "Debug: Remote helper quit.\n");
64 fprintf(stderr
, "Debug: Remote helper: <- %s\n", buffer
->buf
);
68 static int recvline(struct helper_data
*helper
, struct strbuf
*buffer
)
70 return recvline_fh(helper
->out
, buffer
);
73 static void xchgline(struct helper_data
*helper
, struct strbuf
*buffer
)
75 sendline(helper
, buffer
);
76 recvline(helper
, buffer
);
79 static void write_constant(int fd
, const char *str
)
82 fprintf(stderr
, "Debug: Remote helper: -> %s", str
);
83 if (write_in_full(fd
, str
, strlen(str
)) != strlen(str
))
84 die_errno("Full write to remote helper failed");
87 static const char *remove_ext_force(const char *url
)
90 const char *colon
= strchr(url
, ':');
91 if (colon
&& colon
[1] == ':')
97 static void do_take_over(struct transport
*transport
)
99 struct helper_data
*data
;
100 data
= (struct helper_data
*)transport
->data
;
101 transport_take_over(transport
, data
->helper
);
106 static struct child_process
*get_helper(struct transport
*transport
)
108 struct helper_data
*data
= transport
->data
;
109 struct argv_array argv
= ARGV_ARRAY_INIT
;
110 struct strbuf buf
= STRBUF_INIT
;
111 struct child_process
*helper
;
112 const char **refspecs
= NULL
;
114 int refspec_alloc
= 0;
117 char git_dir_buf
[sizeof(GIT_DIR_ENVIRONMENT
) + PATH_MAX
+ 1];
118 const char *helper_env
[] = {
127 helper
= xcalloc(1, sizeof(*helper
));
131 argv_array_pushf(&argv
, "git-remote-%s", data
->name
);
132 argv_array_push(&argv
, transport
->remote
->name
);
133 argv_array_push(&argv
, remove_ext_force(transport
->url
));
134 helper
->argv
= argv_array_detach(&argv
, NULL
);
136 helper
->silent_exec_failure
= 1;
138 snprintf(git_dir_buf
, sizeof(git_dir_buf
), "%s=%s", GIT_DIR_ENVIRONMENT
, get_git_dir());
139 helper
->env
= helper_env
;
141 code
= start_command(helper
);
142 if (code
< 0 && errno
== ENOENT
)
143 die("Unable to find remote helper for '%s'", data
->name
);
147 data
->helper
= helper
;
148 data
->no_disconnect_req
= 0;
151 * Open the output as FILE* so strbuf_getline() can be used.
152 * Do this with duped fd because fclose() will close the fd,
153 * and stuff like taking over will require the fd to remain.
155 duped
= dup(helper
->out
);
157 die_errno("Can't dup helper output fd");
158 data
->out
= xfdopen(duped
, "r");
160 write_constant(helper
->in
, "capabilities\n");
165 recvline(data
, &buf
);
170 if (*buf
.buf
== '*') {
171 capname
= buf
.buf
+ 1;
177 fprintf(stderr
, "Debug: Got cap %s\n", capname
);
178 if (!strcmp(capname
, "fetch"))
180 else if (!strcmp(capname
, "option"))
182 else if (!strcmp(capname
, "push"))
184 else if (!strcmp(capname
, "import"))
186 else if (!strcmp(capname
, "bidi-import"))
187 data
->bidi_import
= 1;
188 else if (!strcmp(capname
, "export"))
190 else if (!data
->refspecs
&& !prefixcmp(capname
, "refspec ")) {
194 refspecs
[refspec_nr
++] = xstrdup(capname
+ strlen("refspec "));
195 } else if (!strcmp(capname
, "connect")) {
197 } else if (!strcmp(capname
, "signed-tags")) {
198 data
->signed_tags
= 1;
199 } else if (!prefixcmp(capname
, "export-marks ")) {
200 struct strbuf arg
= STRBUF_INIT
;
201 strbuf_addstr(&arg
, "--export-marks=");
202 strbuf_addstr(&arg
, capname
+ strlen("export-marks "));
203 data
->export_marks
= strbuf_detach(&arg
, NULL
);
204 } else if (!prefixcmp(capname
, "import-marks")) {
205 struct strbuf arg
= STRBUF_INIT
;
206 strbuf_addstr(&arg
, "--import-marks=");
207 strbuf_addstr(&arg
, capname
+ strlen("import-marks "));
208 data
->import_marks
= strbuf_detach(&arg
, NULL
);
209 } else if (mandatory
) {
210 die("Unknown mandatory capability %s. This remote "
211 "helper probably needs newer version of Git.",
217 data
->refspec_nr
= refspec_nr
;
218 data
->refspecs
= parse_fetch_refspec(refspec_nr
, refspecs
);
219 for (i
= 0; i
< refspec_nr
; i
++)
220 free((char *)refspecs
[i
]);
223 strbuf_release(&buf
);
225 fprintf(stderr
, "Debug: Capabilities complete.\n");
229 static int disconnect_helper(struct transport
*transport
)
231 struct helper_data
*data
= transport
->data
;
236 fprintf(stderr
, "Debug: Disconnecting.\n");
237 if (!data
->no_disconnect_req
) {
239 * Ignore write errors; there's nothing we can do,
240 * since we're about to close the pipe anyway. And the
241 * most likely error is EPIPE due to the helper dying
242 * to report an error itself.
244 sigchain_push(SIGPIPE
, SIG_IGN
);
245 xwrite(data
->helper
->in
, "\n", 1);
246 sigchain_pop(SIGPIPE
);
248 close(data
->helper
->in
);
249 close(data
->helper
->out
);
251 res
= finish_command(data
->helper
);
252 argv_array_free_detached(data
->helper
->argv
);
259 static const char *unsupported_options
[] = {
260 TRANS_OPT_UPLOADPACK
,
261 TRANS_OPT_RECEIVEPACK
,
265 static const char *boolean_options
[] = {
271 static int set_helper_option(struct transport
*transport
,
272 const char *name
, const char *value
)
274 struct helper_data
*data
= transport
->data
;
275 struct strbuf buf
= STRBUF_INIT
;
276 int i
, ret
, is_bool
= 0;
278 get_helper(transport
);
283 for (i
= 0; i
< ARRAY_SIZE(unsupported_options
); i
++) {
284 if (!strcmp(name
, unsupported_options
[i
]))
288 for (i
= 0; i
< ARRAY_SIZE(boolean_options
); i
++) {
289 if (!strcmp(name
, boolean_options
[i
])) {
295 strbuf_addf(&buf
, "option %s ", name
);
297 strbuf_addstr(&buf
, value
? "true" : "false");
299 quote_c_style(value
, &buf
, NULL
, 0);
300 strbuf_addch(&buf
, '\n');
302 xchgline(data
, &buf
);
304 if (!strcmp(buf
.buf
, "ok"))
306 else if (!prefixcmp(buf
.buf
, "error")) {
308 } else if (!strcmp(buf
.buf
, "unsupported"))
311 warning("%s unexpectedly said: '%s'", data
->name
, buf
.buf
);
314 strbuf_release(&buf
);
318 static void standard_options(struct transport
*t
)
324 set_helper_option(t
, "progress", t
->progress
? "true" : "false");
326 n
= snprintf(buf
, sizeof(buf
), "%d", v
+ 1);
327 if (n
>= sizeof(buf
))
328 die("impossibly large verbosity value");
329 set_helper_option(t
, "verbosity", buf
);
332 static int release_helper(struct transport
*transport
)
335 struct helper_data
*data
= transport
->data
;
336 free_refspec(data
->refspec_nr
, data
->refspecs
);
337 data
->refspecs
= NULL
;
338 res
= disconnect_helper(transport
);
339 free(transport
->data
);
343 static int fetch_with_fetch(struct transport
*transport
,
344 int nr_heads
, struct ref
**to_fetch
)
346 struct helper_data
*data
= transport
->data
;
348 struct strbuf buf
= STRBUF_INIT
;
350 standard_options(transport
);
352 for (i
= 0; i
< nr_heads
; i
++) {
353 const struct ref
*posn
= to_fetch
[i
];
354 if (posn
->status
& REF_STATUS_UPTODATE
)
357 strbuf_addf(&buf
, "fetch %s %s\n",
358 sha1_to_hex(posn
->old_sha1
), posn
->name
);
361 strbuf_addch(&buf
, '\n');
362 sendline(data
, &buf
);
365 recvline(data
, &buf
);
367 if (!prefixcmp(buf
.buf
, "lock ")) {
368 const char *name
= buf
.buf
+ 5;
369 if (transport
->pack_lockfile
)
370 warning("%s also locked %s", data
->name
, name
);
372 transport
->pack_lockfile
= xstrdup(name
);
377 warning("%s unexpectedly said: '%s'", data
->name
, buf
.buf
);
379 strbuf_release(&buf
);
383 static int get_importer(struct transport
*transport
, struct child_process
*fastimport
)
385 struct child_process
*helper
= get_helper(transport
);
386 struct helper_data
*data
= transport
->data
;
387 struct argv_array argv
= ARGV_ARRAY_INIT
;
388 int cat_blob_fd
, code
;
389 memset(fastimport
, 0, sizeof(*fastimport
));
390 fastimport
->in
= helper
->out
;
391 argv_array_push(&argv
, "fast-import");
392 argv_array_push(&argv
, debug
? "--stats" : "--quiet");
394 if (data
->bidi_import
) {
395 cat_blob_fd
= xdup(helper
->in
);
396 argv_array_pushf(&argv
, "--cat-blob-fd=%d", cat_blob_fd
);
398 fastimport
->argv
= argv
.argv
;
399 fastimport
->git_cmd
= 1;
401 code
= start_command(fastimport
);
405 static int get_exporter(struct transport
*transport
,
406 struct child_process
*fastexport
,
407 struct string_list
*revlist_args
)
409 struct helper_data
*data
= transport
->data
;
410 struct child_process
*helper
= get_helper(transport
);
412 memset(fastexport
, 0, sizeof(*fastexport
));
414 /* we need to duplicate helper->in because we want to use it after
415 * fastexport is done with it. */
416 fastexport
->out
= dup(helper
->in
);
417 fastexport
->argv
= xcalloc(7 + revlist_args
->nr
, sizeof(*fastexport
->argv
));
418 fastexport
->argv
[argc
++] = "fast-export";
419 fastexport
->argv
[argc
++] = "--use-done-feature";
420 fastexport
->argv
[argc
++] = data
->signed_tags
?
421 "--signed-tags=verbatim" : "--signed-tags=warn-strip";
422 if (data
->export_marks
)
423 fastexport
->argv
[argc
++] = data
->export_marks
;
424 if (data
->import_marks
)
425 fastexport
->argv
[argc
++] = data
->import_marks
;
427 for (i
= 0; i
< revlist_args
->nr
; i
++)
428 fastexport
->argv
[argc
++] = revlist_args
->items
[i
].string
;
430 fastexport
->argv
[argc
++] = "--";
432 fastexport
->git_cmd
= 1;
433 return start_command(fastexport
);
436 static void check_helper_status(struct helper_data
*data
)
440 pid
= waitpid(data
->helper
->pid
, &status
, WNOHANG
);
442 die("Could not retrieve status of remote helper '%s'",
444 if (pid
> 0 && WIFEXITED(status
))
445 die("Remote helper '%s' died with %d",
446 data
->name
, WEXITSTATUS(status
));
449 static int fetch_with_import(struct transport
*transport
,
450 int nr_heads
, struct ref
**to_fetch
)
452 struct child_process fastimport
;
453 struct helper_data
*data
= transport
->data
;
456 struct strbuf buf
= STRBUF_INIT
;
458 get_helper(transport
);
460 if (get_importer(transport
, &fastimport
))
461 die("Couldn't run fast-import");
463 for (i
= 0; i
< nr_heads
; i
++) {
465 if (posn
->status
& REF_STATUS_UPTODATE
)
468 strbuf_addf(&buf
, "import %s\n", posn
->name
);
469 sendline(data
, &buf
);
473 write_constant(data
->helper
->in
, "\n");
475 * remote-helpers that advertise the bidi-import capability are required to
476 * buffer the complete batch of import commands until this newline before
477 * sending data to fast-import.
478 * These helpers read back data from fast-import on their stdin, which could
479 * be mixed with import commands, otherwise.
482 if (finish_command(&fastimport
))
483 die("Error while running fast-import");
484 argv_array_free_detached(fastimport
.argv
);
485 check_helper_status(data
);
488 * The fast-import stream of a remote helper that advertises
489 * the "refspec" capability writes to the refs named after the
490 * right hand side of the first refspec matching each ref we
493 * (If no "refspec" capability was specified, for historical
494 * reasons we default to *:*.)
496 * Store the result in to_fetch[i].old_sha1. Callers such
497 * as "git fetch" can use the value to write feedback to the
498 * terminal, populate FETCH_HEAD, and determine what new value
499 * should be written to peer_ref if the update is a
500 * fast-forward or this is a forced update.
502 for (i
= 0; i
< nr_heads
; i
++) {
505 if (posn
->status
& REF_STATUS_UPTODATE
)
508 private = apply_refspecs(data
->refspecs
, data
->refspec_nr
, posn
->name
);
510 private = xstrdup(posn
->name
);
512 read_ref(private, posn
->old_sha1
);
516 strbuf_release(&buf
);
518 const char *argv_gc_auto
[] = {
519 "gc", "--auto", "--quiet", NULL
,
521 run_command_v_opt(argv_gc_auto
, RUN_GIT_CMD
);
526 static int process_connect_service(struct transport
*transport
,
527 const char *name
, const char *exec
)
529 struct helper_data
*data
= transport
->data
;
530 struct strbuf cmdbuf
= STRBUF_INIT
;
531 struct child_process
*helper
;
532 int r
, duped
, ret
= 0;
535 helper
= get_helper(transport
);
538 * Yes, dup the pipe another time, as we need unbuffered version
539 * of input pipe as FILE*. fclose() closes the underlying fd and
540 * stream buffering only can be changed before first I/O operation
543 duped
= dup(helper
->out
);
545 die_errno("Can't dup helper output fd");
546 input
= xfdopen(duped
, "r");
547 setvbuf(input
, NULL
, _IONBF
, 0);
550 * Handle --upload-pack and friends. This is fire and forget...
551 * just warn if it fails.
553 if (strcmp(name
, exec
)) {
554 r
= set_helper_option(transport
, "servpath", exec
);
556 warning("Setting remote service path not supported by protocol.");
558 warning("Invalid remote service path.");
562 strbuf_addf(&cmdbuf
, "connect %s\n", name
);
566 sendline(data
, &cmdbuf
);
567 recvline_fh(input
, &cmdbuf
);
568 if (!strcmp(cmdbuf
.buf
, "")) {
569 data
->no_disconnect_req
= 1;
571 fprintf(stderr
, "Debug: Smart transport connection "
574 } else if (!strcmp(cmdbuf
.buf
, "fallback")) {
576 fprintf(stderr
, "Debug: Falling back to dumb "
579 die("Unknown response to connect: %s",
587 static int process_connect(struct transport
*transport
,
590 struct helper_data
*data
= transport
->data
;
594 name
= for_push
? "git-receive-pack" : "git-upload-pack";
596 exec
= data
->transport_options
.receivepack
;
598 exec
= data
->transport_options
.uploadpack
;
600 return process_connect_service(transport
, name
, exec
);
603 static int connect_helper(struct transport
*transport
, const char *name
,
604 const char *exec
, int fd
[2])
606 struct helper_data
*data
= transport
->data
;
608 /* Get_helper so connect is inited. */
609 get_helper(transport
);
611 die("Operation not supported by protocol.");
613 if (!process_connect_service(transport
, name
, exec
))
614 die("Can't connect to subservice %s.", name
);
616 fd
[0] = data
->helper
->out
;
617 fd
[1] = data
->helper
->in
;
621 static int fetch(struct transport
*transport
,
622 int nr_heads
, struct ref
**to_fetch
)
624 struct helper_data
*data
= transport
->data
;
627 if (process_connect(transport
, 0)) {
628 do_take_over(transport
);
629 return transport
->fetch(transport
, nr_heads
, to_fetch
);
633 for (i
= 0; i
< nr_heads
; i
++)
634 if (!(to_fetch
[i
]->status
& REF_STATUS_UPTODATE
))
641 return fetch_with_fetch(transport
, nr_heads
, to_fetch
);
644 return fetch_with_import(transport
, nr_heads
, to_fetch
);
649 static void push_update_ref_status(struct strbuf
*buf
,
651 struct ref
*remote_refs
)
656 if (!prefixcmp(buf
->buf
, "ok ")) {
657 status
= REF_STATUS_OK
;
658 refname
= buf
->buf
+ 3;
659 } else if (!prefixcmp(buf
->buf
, "error ")) {
660 status
= REF_STATUS_REMOTE_REJECT
;
661 refname
= buf
->buf
+ 6;
663 die("expected ok/error, helper said '%s'", buf
->buf
);
665 msg
= strchr(refname
, ' ');
667 struct strbuf msg_buf
= STRBUF_INIT
;
671 if (!unquote_c_style(&msg_buf
, msg
, &end
))
672 msg
= strbuf_detach(&msg_buf
, NULL
);
675 strbuf_release(&msg_buf
);
677 if (!strcmp(msg
, "no match")) {
678 status
= REF_STATUS_NONE
;
682 else if (!strcmp(msg
, "up to date")) {
683 status
= REF_STATUS_UPTODATE
;
687 else if (!strcmp(msg
, "non-fast forward")) {
688 status
= REF_STATUS_REJECT_NONFASTFORWARD
;
692 else if (!strcmp(msg
, "already exists")) {
693 status
= REF_STATUS_REJECT_ALREADY_EXISTS
;
697 else if (!strcmp(msg
, "fetch first")) {
698 status
= REF_STATUS_REJECT_FETCH_FIRST
;
702 else if (!strcmp(msg
, "needs force")) {
703 status
= REF_STATUS_REJECT_NEEDS_FORCE
;
710 *ref
= find_ref_by_name(*ref
, refname
);
712 *ref
= find_ref_by_name(remote_refs
, refname
);
714 warning("helper reported unexpected status of %s", refname
);
718 if ((*ref
)->status
!= REF_STATUS_NONE
) {
720 * Earlier, the ref was marked not to be pushed, so ignore the ref
721 * status reported by the remote helper if the latter is 'no match'.
723 if (status
== REF_STATUS_NONE
)
727 (*ref
)->status
= status
;
728 (*ref
)->remote_status
= msg
;
731 static void push_update_refs_status(struct helper_data
*data
,
732 struct ref
*remote_refs
)
734 struct strbuf buf
= STRBUF_INIT
;
735 struct ref
*ref
= remote_refs
;
737 recvline(data
, &buf
);
741 push_update_ref_status(&buf
, &ref
, remote_refs
);
743 strbuf_release(&buf
);
746 static int push_refs_with_push(struct transport
*transport
,
747 struct ref
*remote_refs
, int flags
)
749 int force_all
= flags
& TRANSPORT_PUSH_FORCE
;
750 int mirror
= flags
& TRANSPORT_PUSH_MIRROR
;
751 struct helper_data
*data
= transport
->data
;
752 struct strbuf buf
= STRBUF_INIT
;
755 get_helper(transport
);
759 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
760 if (!ref
->peer_ref
&& !mirror
)
763 /* Check for statuses set by set_ref_status_for_push() */
764 switch (ref
->status
) {
765 case REF_STATUS_REJECT_NONFASTFORWARD
:
766 case REF_STATUS_REJECT_ALREADY_EXISTS
:
767 case REF_STATUS_UPTODATE
:
776 strbuf_addstr(&buf
, "push ");
777 if (!ref
->deletion
) {
779 strbuf_addch(&buf
, '+');
781 strbuf_addstr(&buf
, ref
->peer_ref
->name
);
783 strbuf_addstr(&buf
, sha1_to_hex(ref
->new_sha1
));
785 strbuf_addch(&buf
, ':');
786 strbuf_addstr(&buf
, ref
->name
);
787 strbuf_addch(&buf
, '\n');
792 standard_options(transport
);
794 if (flags
& TRANSPORT_PUSH_DRY_RUN
) {
795 if (set_helper_option(transport
, "dry-run", "true") != 0)
796 die("helper %s does not support dry-run", data
->name
);
799 strbuf_addch(&buf
, '\n');
800 sendline(data
, &buf
);
801 strbuf_release(&buf
);
803 push_update_refs_status(data
, remote_refs
);
807 static int push_refs_with_export(struct transport
*transport
,
808 struct ref
*remote_refs
, int flags
)
811 struct child_process
*helper
, exporter
;
812 struct helper_data
*data
= transport
->data
;
813 struct string_list revlist_args
= STRING_LIST_INIT_NODUP
;
814 struct strbuf buf
= STRBUF_INIT
;
816 helper
= get_helper(transport
);
818 write_constant(helper
->in
, "export\n");
822 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
824 unsigned char sha1
[20];
828 private = apply_refspecs(data
->refspecs
, data
->refspec_nr
, ref
->name
);
829 if (private && !get_sha1(private, sha1
)) {
830 strbuf_addf(&buf
, "^%s", private);
831 string_list_append(&revlist_args
, strbuf_detach(&buf
, NULL
));
832 hashcpy(ref
->old_sha1
, sha1
);
837 die("remote-helpers do not support ref deletion");
841 string_list_append(&revlist_args
, ref
->peer_ref
->name
);
845 if (get_exporter(transport
, &exporter
, &revlist_args
))
846 die("Couldn't run fast-export");
848 if (finish_command(&exporter
))
849 die("Error while running fast-export");
850 check_helper_status(data
);
851 push_update_refs_status(data
, remote_refs
);
855 static int push_refs(struct transport
*transport
,
856 struct ref
*remote_refs
, int flags
)
858 struct helper_data
*data
= transport
->data
;
860 if (process_connect(transport
, 1)) {
861 do_take_over(transport
);
862 return transport
->push_refs(transport
, remote_refs
, flags
);
866 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
867 "Perhaps you should specify a branch such as 'master'.\n");
872 return push_refs_with_push(transport
, remote_refs
, flags
);
875 return push_refs_with_export(transport
, remote_refs
, flags
);
881 static int has_attribute(const char *attrs
, const char *attr
) {
888 const char *space
= strchrnul(attrs
, ' ');
889 if (len
== space
- attrs
&& !strncmp(attrs
, attr
, len
))
897 static struct ref
*get_refs_list(struct transport
*transport
, int for_push
)
899 struct helper_data
*data
= transport
->data
;
900 struct child_process
*helper
;
901 struct ref
*ret
= NULL
;
902 struct ref
**tail
= &ret
;
904 struct strbuf buf
= STRBUF_INIT
;
906 helper
= get_helper(transport
);
908 if (process_connect(transport
, for_push
)) {
909 do_take_over(transport
);
910 return transport
->get_refs_list(transport
, for_push
);
913 if (data
->push
&& for_push
)
914 write_str_in_full(helper
->in
, "list for-push\n");
916 write_str_in_full(helper
->in
, "list\n");
920 recvline(data
, &buf
);
925 eov
= strchr(buf
.buf
, ' ');
927 die("Malformed response in ref list: %s", buf
.buf
);
928 eon
= strchr(eov
+ 1, ' ');
932 *tail
= alloc_ref(eov
+ 1);
933 if (buf
.buf
[0] == '@')
934 (*tail
)->symref
= xstrdup(buf
.buf
+ 1);
935 else if (buf
.buf
[0] != '?')
936 get_sha1_hex(buf
.buf
, (*tail
)->old_sha1
);
938 if (has_attribute(eon
+ 1, "unchanged")) {
939 (*tail
)->status
|= REF_STATUS_UPTODATE
;
940 read_ref((*tail
)->name
, (*tail
)->old_sha1
);
943 tail
= &((*tail
)->next
);
946 fprintf(stderr
, "Debug: Read ref listing.\n");
947 strbuf_release(&buf
);
949 for (posn
= ret
; posn
; posn
= posn
->next
)
950 resolve_remote_symref(posn
, ret
);
955 int transport_helper_init(struct transport
*transport
, const char *name
)
957 struct helper_data
*data
= xcalloc(sizeof(*data
), 1);
960 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
963 transport
->data
= data
;
964 transport
->set_option
= set_helper_option
;
965 transport
->get_refs_list
= get_refs_list
;
966 transport
->fetch
= fetch
;
967 transport
->push_refs
= push_refs
;
968 transport
->disconnect
= release_helper
;
969 transport
->connect
= connect_helper
;
970 transport
->smart_options
= &(data
->transport_options
);
975 * Linux pipes can buffer 65536 bytes at once (and most platforms can
976 * buffer less), so attempt reads and writes with up to that size.
978 #define BUFFERSIZE 65536
979 /* This should be enough to hold debugging message. */
980 #define PBUFFERSIZE 8192
982 /* Print bidirectional transfer loop debug message. */
983 static void transfer_debug(const char *fmt
, ...)
986 char msgbuf
[PBUFFERSIZE
];
987 static int debug_enabled
= -1;
989 if (debug_enabled
< 0)
990 debug_enabled
= getenv("GIT_TRANSLOOP_DEBUG") ? 1 : 0;
995 vsnprintf(msgbuf
, PBUFFERSIZE
, fmt
, args
);
997 fprintf(stderr
, "Transfer loop debugging: %s\n", msgbuf
);
1000 /* Stream state: More data may be coming in this direction. */
1001 #define SSTATE_TRANSFERING 0
1003 * Stream state: No more data coming in this direction, flushing rest of
1006 #define SSTATE_FLUSHING 1
1007 /* Stream state: Transfer in this direction finished. */
1008 #define SSTATE_FINISHED 2
1010 #define STATE_NEEDS_READING(state) ((state) <= SSTATE_TRANSFERING)
1011 #define STATE_NEEDS_WRITING(state) ((state) <= SSTATE_FLUSHING)
1012 #define STATE_NEEDS_CLOSING(state) ((state) == SSTATE_FLUSHING)
1014 /* Unidirectional transfer. */
1015 struct unidirectional_transfer
{
1020 /* Is source socket? */
1022 /* Is destination socket? */
1024 /* Transfer state (TRANSFERRING/FLUSHING/FINISHED) */
1027 char buf
[BUFFERSIZE
];
1030 /* Name of source. */
1031 const char *src_name
;
1032 /* Name of destination. */
1033 const char *dest_name
;
1036 /* Closes the target (for writing) if transfer has finished. */
1037 static void udt_close_if_finished(struct unidirectional_transfer
*t
)
1039 if (STATE_NEEDS_CLOSING(t
->state
) && !t
->bufuse
) {
1040 t
->state
= SSTATE_FINISHED
;
1041 if (t
->dest_is_sock
)
1042 shutdown(t
->dest
, SHUT_WR
);
1045 transfer_debug("Closed %s.", t
->dest_name
);
1050 * Tries to read read data from source into buffer. If buffer is full,
1051 * no data is read. Returns 0 on success, -1 on error.
1053 static int udt_do_read(struct unidirectional_transfer
*t
)
1057 if (t
->bufuse
== BUFFERSIZE
)
1058 return 0; /* No space for more. */
1060 transfer_debug("%s is readable", t
->src_name
);
1061 bytes
= read(t
->src
, t
->buf
+ t
->bufuse
, BUFFERSIZE
- t
->bufuse
);
1062 if (bytes
< 0 && errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
&&
1064 error("read(%s) failed: %s", t
->src_name
, strerror(errno
));
1066 } else if (bytes
== 0) {
1067 transfer_debug("%s EOF (with %i bytes in buffer)",
1068 t
->src_name
, t
->bufuse
);
1069 t
->state
= SSTATE_FLUSHING
;
1070 } else if (bytes
> 0) {
1072 transfer_debug("Read %i bytes from %s (buffer now at %i)",
1073 (int)bytes
, t
->src_name
, (int)t
->bufuse
);
1078 /* Tries to write data from buffer into destination. If buffer is empty,
1079 * no data is written. Returns 0 on success, -1 on error.
1081 static int udt_do_write(struct unidirectional_transfer
*t
)
1086 return 0; /* Nothing to write. */
1088 transfer_debug("%s is writable", t
->dest_name
);
1089 bytes
= write(t
->dest
, t
->buf
, t
->bufuse
);
1090 if (bytes
< 0 && errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
&&
1092 error("write(%s) failed: %s", t
->dest_name
, strerror(errno
));
1094 } else if (bytes
> 0) {
1097 memmove(t
->buf
, t
->buf
+ bytes
, t
->bufuse
);
1098 transfer_debug("Wrote %i bytes to %s (buffer now at %i)",
1099 (int)bytes
, t
->dest_name
, (int)t
->bufuse
);
1105 /* State of bidirectional transfer loop. */
1106 struct bidirectional_transfer_state
{
1107 /* Direction from program to git. */
1108 struct unidirectional_transfer ptg
;
1109 /* Direction from git to program. */
1110 struct unidirectional_transfer gtp
;
1113 static void *udt_copy_task_routine(void *udt
)
1115 struct unidirectional_transfer
*t
= (struct unidirectional_transfer
*)udt
;
1116 while (t
->state
!= SSTATE_FINISHED
) {
1117 if (STATE_NEEDS_READING(t
->state
))
1120 if (STATE_NEEDS_WRITING(t
->state
))
1121 if (udt_do_write(t
))
1123 if (STATE_NEEDS_CLOSING(t
->state
))
1124 udt_close_if_finished(t
);
1126 return udt
; /* Just some non-NULL value. */
1132 * Join thread, with apporiate errors on failure. Name is name for the
1133 * thread (for error messages). Returns 0 on success, 1 on failure.
1135 static int tloop_join(pthread_t thread
, const char *name
)
1139 err
= pthread_join(thread
, &tret
);
1141 error("%s thread failed", name
);
1145 error("%s thread failed to join: %s", name
, strerror(err
));
1152 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1155 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1157 pthread_t gtp_thread
;
1158 pthread_t ptg_thread
;
1161 err
= pthread_create(>p_thread
, NULL
, udt_copy_task_routine
,
1164 die("Can't start thread for copying data: %s", strerror(err
));
1165 err
= pthread_create(&ptg_thread
, NULL
, udt_copy_task_routine
,
1168 die("Can't start thread for copying data: %s", strerror(err
));
1170 ret
|= tloop_join(gtp_thread
, "Git to program copy");
1171 ret
|= tloop_join(ptg_thread
, "Program to git copy");
1176 /* Close the source and target (for writing) for transfer. */
1177 static void udt_kill_transfer(struct unidirectional_transfer
*t
)
1179 t
->state
= SSTATE_FINISHED
;
1181 * Socket read end left open isn't a disaster if nobody
1182 * attempts to read from it (mingw compat headers do not
1185 * We can't fully close the socket since otherwise gtp
1186 * task would first close the socket it sends data to
1187 * while closing the ptg file descriptors.
1189 if (!t
->src_is_sock
)
1191 if (t
->dest_is_sock
)
1192 shutdown(t
->dest
, SHUT_WR
);
1198 * Join process, with apporiate errors on failure. Name is name for the
1199 * process (for error messages). Returns 0 on success, 1 on failure.
1201 static int tloop_join(pid_t pid
, const char *name
)
1204 if (waitpid(pid
, &tret
, 0) < 0) {
1205 error("%s process failed to wait: %s", name
, strerror(errno
));
1208 if (!WIFEXITED(tret
) || WEXITSTATUS(tret
)) {
1209 error("%s process failed", name
);
1216 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1219 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1224 /* Fork thread #1: git to program. */
1227 die_errno("Can't start thread for copying data");
1228 else if (pid1
== 0) {
1229 udt_kill_transfer(&s
->ptg
);
1230 exit(udt_copy_task_routine(&s
->gtp
) ? 0 : 1);
1233 /* Fork thread #2: program to git. */
1236 die_errno("Can't start thread for copying data");
1237 else if (pid2
== 0) {
1238 udt_kill_transfer(&s
->gtp
);
1239 exit(udt_copy_task_routine(&s
->ptg
) ? 0 : 1);
1243 * Close both streams in parent as to not interfere with
1244 * end of file detection and wait for both tasks to finish.
1246 udt_kill_transfer(&s
->gtp
);
1247 udt_kill_transfer(&s
->ptg
);
1248 ret
|= tloop_join(pid1
, "Git to program copy");
1249 ret
|= tloop_join(pid2
, "Program to git copy");
1255 * Copies data from stdin to output and from input to stdout simultaneously.
1256 * Additionally filtering through given filter. If filter is NULL, uses
1259 int bidirectional_transfer_loop(int input
, int output
)
1261 struct bidirectional_transfer_state state
;
1263 /* Fill the state fields. */
1264 state
.ptg
.src
= input
;
1266 state
.ptg
.src_is_sock
= (input
== output
);
1267 state
.ptg
.dest_is_sock
= 0;
1268 state
.ptg
.state
= SSTATE_TRANSFERING
;
1269 state
.ptg
.bufuse
= 0;
1270 state
.ptg
.src_name
= "remote input";
1271 state
.ptg
.dest_name
= "stdout";
1274 state
.gtp
.dest
= output
;
1275 state
.gtp
.src_is_sock
= 0;
1276 state
.gtp
.dest_is_sock
= (input
== output
);
1277 state
.gtp
.state
= SSTATE_TRANSFERING
;
1278 state
.gtp
.bufuse
= 0;
1279 state
.gtp
.src_name
= "stdin";
1280 state
.gtp
.dest_name
= "remote output";
1282 return tloop_spawnwait_tasks(&state
);