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 check_connectivity
: 1,
30 no_disconnect_req
: 1;
33 /* These go from remote name (as in "list") to private name */
34 struct refspec
*refspecs
;
36 /* Transport options for fetch-pack/send-pack (should one of
39 struct git_transport_options transport_options
;
42 static void sendline(struct helper_data
*helper
, struct strbuf
*buffer
)
45 fprintf(stderr
, "Debug: Remote helper: -> %s", buffer
->buf
);
46 if (write_in_full(helper
->helper
->in
, buffer
->buf
, buffer
->len
)
48 die_errno("Full write to remote helper failed");
51 static int recvline_fh(FILE *helper
, struct strbuf
*buffer
)
55 fprintf(stderr
, "Debug: Remote helper: Waiting...\n");
56 if (strbuf_getline(buffer
, helper
, '\n') == EOF
) {
58 fprintf(stderr
, "Debug: Remote helper quit.\n");
63 fprintf(stderr
, "Debug: Remote helper: <- %s\n", buffer
->buf
);
67 static int recvline(struct helper_data
*helper
, struct strbuf
*buffer
)
69 return recvline_fh(helper
->out
, buffer
);
72 static void xchgline(struct helper_data
*helper
, struct strbuf
*buffer
)
74 sendline(helper
, buffer
);
75 recvline(helper
, buffer
);
78 static void write_constant(int fd
, const char *str
)
81 fprintf(stderr
, "Debug: Remote helper: -> %s", str
);
82 if (write_in_full(fd
, str
, strlen(str
)) != strlen(str
))
83 die_errno("Full write to remote helper failed");
86 static const char *remove_ext_force(const char *url
)
89 const char *colon
= strchr(url
, ':');
90 if (colon
&& colon
[1] == ':')
96 static void do_take_over(struct transport
*transport
)
98 struct helper_data
*data
;
99 data
= (struct helper_data
*)transport
->data
;
100 transport_take_over(transport
, data
->helper
);
105 static struct child_process
*get_helper(struct transport
*transport
)
107 struct helper_data
*data
= transport
->data
;
108 struct argv_array argv
= ARGV_ARRAY_INIT
;
109 struct strbuf buf
= STRBUF_INIT
;
110 struct child_process
*helper
;
111 const char **refspecs
= NULL
;
113 int refspec_alloc
= 0;
116 char git_dir_buf
[sizeof(GIT_DIR_ENVIRONMENT
) + PATH_MAX
+ 1];
117 const char *helper_env
[] = {
126 helper
= xcalloc(1, sizeof(*helper
));
130 argv_array_pushf(&argv
, "git-remote-%s", data
->name
);
131 argv_array_push(&argv
, transport
->remote
->name
);
132 argv_array_push(&argv
, remove_ext_force(transport
->url
));
133 helper
->argv
= argv_array_detach(&argv
, NULL
);
135 helper
->silent_exec_failure
= 1;
137 snprintf(git_dir_buf
, sizeof(git_dir_buf
), "%s=%s", GIT_DIR_ENVIRONMENT
, get_git_dir());
138 helper
->env
= helper_env
;
140 code
= start_command(helper
);
141 if (code
< 0 && errno
== ENOENT
)
142 die("Unable to find remote helper for '%s'", data
->name
);
146 data
->helper
= helper
;
147 data
->no_disconnect_req
= 0;
150 * Open the output as FILE* so strbuf_getline() can be used.
151 * Do this with duped fd because fclose() will close the fd,
152 * and stuff like taking over will require the fd to remain.
154 duped
= dup(helper
->out
);
156 die_errno("Can't dup helper output fd");
157 data
->out
= xfdopen(duped
, "r");
159 write_constant(helper
->in
, "capabilities\n");
164 recvline(data
, &buf
);
169 if (*buf
.buf
== '*') {
170 capname
= buf
.buf
+ 1;
176 fprintf(stderr
, "Debug: Got cap %s\n", capname
);
177 if (!strcmp(capname
, "fetch"))
179 else if (!strcmp(capname
, "option"))
181 else if (!strcmp(capname
, "push"))
183 else if (!strcmp(capname
, "import"))
185 else if (!strcmp(capname
, "bidi-import"))
186 data
->bidi_import
= 1;
187 else if (!strcmp(capname
, "export"))
189 else if (!strcmp(capname
, "check-connectivity"))
190 data
->check_connectivity
= 1;
191 else if (!data
->refspecs
&& !prefixcmp(capname
, "refspec ")) {
195 refspecs
[refspec_nr
++] = xstrdup(capname
+ strlen("refspec "));
196 } else if (!strcmp(capname
, "connect")) {
198 } else if (!strcmp(capname
, "signed-tags")) {
199 data
->signed_tags
= 1;
200 } else if (!prefixcmp(capname
, "export-marks ")) {
201 struct strbuf arg
= STRBUF_INIT
;
202 strbuf_addstr(&arg
, "--export-marks=");
203 strbuf_addstr(&arg
, capname
+ strlen("export-marks "));
204 data
->export_marks
= strbuf_detach(&arg
, NULL
);
205 } else if (!prefixcmp(capname
, "import-marks")) {
206 struct strbuf arg
= STRBUF_INIT
;
207 strbuf_addstr(&arg
, "--import-marks=");
208 strbuf_addstr(&arg
, capname
+ strlen("import-marks "));
209 data
->import_marks
= strbuf_detach(&arg
, NULL
);
210 } else if (mandatory
) {
211 die("Unknown mandatory capability %s. This remote "
212 "helper probably needs newer version of Git.",
218 data
->refspec_nr
= refspec_nr
;
219 data
->refspecs
= parse_fetch_refspec(refspec_nr
, refspecs
);
220 for (i
= 0; i
< refspec_nr
; i
++)
221 free((char *)refspecs
[i
]);
224 strbuf_release(&buf
);
226 fprintf(stderr
, "Debug: Capabilities complete.\n");
230 static int disconnect_helper(struct transport
*transport
)
232 struct helper_data
*data
= transport
->data
;
237 fprintf(stderr
, "Debug: Disconnecting.\n");
238 if (!data
->no_disconnect_req
) {
240 * Ignore write errors; there's nothing we can do,
241 * since we're about to close the pipe anyway. And the
242 * most likely error is EPIPE due to the helper dying
243 * to report an error itself.
245 sigchain_push(SIGPIPE
, SIG_IGN
);
246 xwrite(data
->helper
->in
, "\n", 1);
247 sigchain_pop(SIGPIPE
);
249 close(data
->helper
->in
);
250 close(data
->helper
->out
);
252 res
= finish_command(data
->helper
);
253 argv_array_free_detached(data
->helper
->argv
);
260 static const char *unsupported_options
[] = {
261 TRANS_OPT_UPLOADPACK
,
262 TRANS_OPT_RECEIVEPACK
,
266 static const char *boolean_options
[] = {
272 static int set_helper_option(struct transport
*transport
,
273 const char *name
, const char *value
)
275 struct helper_data
*data
= transport
->data
;
276 struct strbuf buf
= STRBUF_INIT
;
277 int i
, ret
, is_bool
= 0;
279 get_helper(transport
);
284 for (i
= 0; i
< ARRAY_SIZE(unsupported_options
); i
++) {
285 if (!strcmp(name
, unsupported_options
[i
]))
289 for (i
= 0; i
< ARRAY_SIZE(boolean_options
); i
++) {
290 if (!strcmp(name
, boolean_options
[i
])) {
296 strbuf_addf(&buf
, "option %s ", name
);
298 strbuf_addstr(&buf
, value
? "true" : "false");
300 quote_c_style(value
, &buf
, NULL
, 0);
301 strbuf_addch(&buf
, '\n');
303 xchgline(data
, &buf
);
305 if (!strcmp(buf
.buf
, "ok"))
307 else if (!prefixcmp(buf
.buf
, "error")) {
309 } else if (!strcmp(buf
.buf
, "unsupported"))
312 warning("%s unexpectedly said: '%s'", data
->name
, buf
.buf
);
315 strbuf_release(&buf
);
319 static void standard_options(struct transport
*t
)
325 set_helper_option(t
, "progress", t
->progress
? "true" : "false");
327 n
= snprintf(buf
, sizeof(buf
), "%d", v
+ 1);
328 if (n
>= sizeof(buf
))
329 die("impossibly large verbosity value");
330 set_helper_option(t
, "verbosity", buf
);
333 static int release_helper(struct transport
*transport
)
336 struct helper_data
*data
= transport
->data
;
337 free_refspec(data
->refspec_nr
, data
->refspecs
);
338 data
->refspecs
= NULL
;
339 res
= disconnect_helper(transport
);
340 free(transport
->data
);
344 static int fetch_with_fetch(struct transport
*transport
,
345 int nr_heads
, struct ref
**to_fetch
)
347 struct helper_data
*data
= transport
->data
;
349 struct strbuf buf
= STRBUF_INIT
;
351 standard_options(transport
);
352 if (data
->check_connectivity
&&
353 data
->transport_options
.check_self_contained_and_connected
)
354 set_helper_option(transport
, "check-connectivity", "true");
356 for (i
= 0; i
< nr_heads
; i
++) {
357 const struct ref
*posn
= to_fetch
[i
];
358 if (posn
->status
& REF_STATUS_UPTODATE
)
361 strbuf_addf(&buf
, "fetch %s %s\n",
362 sha1_to_hex(posn
->old_sha1
), posn
->name
);
365 strbuf_addch(&buf
, '\n');
366 sendline(data
, &buf
);
369 recvline(data
, &buf
);
371 if (!prefixcmp(buf
.buf
, "lock ")) {
372 const char *name
= buf
.buf
+ 5;
373 if (transport
->pack_lockfile
)
374 warning("%s also locked %s", data
->name
, name
);
376 transport
->pack_lockfile
= xstrdup(name
);
378 else if (data
->check_connectivity
&&
379 data
->transport_options
.check_self_contained_and_connected
&&
380 !strcmp(buf
.buf
, "connectivity-ok"))
381 data
->transport_options
.self_contained_and_connected
= 1;
385 warning("%s unexpectedly said: '%s'", data
->name
, buf
.buf
);
387 strbuf_release(&buf
);
391 static int get_importer(struct transport
*transport
, struct child_process
*fastimport
)
393 struct child_process
*helper
= get_helper(transport
);
394 struct helper_data
*data
= transport
->data
;
395 struct argv_array argv
= ARGV_ARRAY_INIT
;
396 int cat_blob_fd
, code
;
397 memset(fastimport
, 0, sizeof(*fastimport
));
398 fastimport
->in
= helper
->out
;
399 argv_array_push(&argv
, "fast-import");
400 argv_array_push(&argv
, debug
? "--stats" : "--quiet");
402 if (data
->bidi_import
) {
403 cat_blob_fd
= xdup(helper
->in
);
404 argv_array_pushf(&argv
, "--cat-blob-fd=%d", cat_blob_fd
);
406 fastimport
->argv
= argv
.argv
;
407 fastimport
->git_cmd
= 1;
409 code
= start_command(fastimport
);
413 static int get_exporter(struct transport
*transport
,
414 struct child_process
*fastexport
,
415 struct string_list
*revlist_args
)
417 struct helper_data
*data
= transport
->data
;
418 struct child_process
*helper
= get_helper(transport
);
420 memset(fastexport
, 0, sizeof(*fastexport
));
422 /* we need to duplicate helper->in because we want to use it after
423 * fastexport is done with it. */
424 fastexport
->out
= dup(helper
->in
);
425 fastexport
->argv
= xcalloc(6 + revlist_args
->nr
, sizeof(*fastexport
->argv
));
426 fastexport
->argv
[argc
++] = "fast-export";
427 fastexport
->argv
[argc
++] = "--use-done-feature";
428 fastexport
->argv
[argc
++] = data
->signed_tags
?
429 "--signed-tags=verbatim" : "--signed-tags=warn-strip";
430 if (data
->export_marks
)
431 fastexport
->argv
[argc
++] = data
->export_marks
;
432 if (data
->import_marks
)
433 fastexport
->argv
[argc
++] = data
->import_marks
;
435 for (i
= 0; i
< revlist_args
->nr
; i
++)
436 fastexport
->argv
[argc
++] = revlist_args
->items
[i
].string
;
438 fastexport
->git_cmd
= 1;
439 return start_command(fastexport
);
442 static int fetch_with_import(struct transport
*transport
,
443 int nr_heads
, struct ref
**to_fetch
)
445 struct child_process fastimport
;
446 struct helper_data
*data
= transport
->data
;
449 struct strbuf buf
= STRBUF_INIT
;
451 get_helper(transport
);
453 if (get_importer(transport
, &fastimport
))
454 die("Couldn't run fast-import");
456 for (i
= 0; i
< nr_heads
; i
++) {
458 if (posn
->status
& REF_STATUS_UPTODATE
)
461 strbuf_addf(&buf
, "import %s\n", posn
->name
);
462 sendline(data
, &buf
);
466 write_constant(data
->helper
->in
, "\n");
468 * remote-helpers that advertise the bidi-import capability are required to
469 * buffer the complete batch of import commands until this newline before
470 * sending data to fast-import.
471 * These helpers read back data from fast-import on their stdin, which could
472 * be mixed with import commands, otherwise.
475 if (finish_command(&fastimport
))
476 die("Error while running fast-import");
477 argv_array_free_detached(fastimport
.argv
);
480 * The fast-import stream of a remote helper that advertises
481 * the "refspec" capability writes to the refs named after the
482 * right hand side of the first refspec matching each ref we
485 * (If no "refspec" capability was specified, for historical
486 * reasons we default to *:*.)
488 * Store the result in to_fetch[i].old_sha1. Callers such
489 * as "git fetch" can use the value to write feedback to the
490 * terminal, populate FETCH_HEAD, and determine what new value
491 * should be written to peer_ref if the update is a
492 * fast-forward or this is a forced update.
494 for (i
= 0; i
< nr_heads
; i
++) {
497 if (posn
->status
& REF_STATUS_UPTODATE
)
500 private = apply_refspecs(data
->refspecs
, data
->refspec_nr
, posn
->name
);
502 private = xstrdup(posn
->name
);
504 read_ref(private, posn
->old_sha1
);
508 strbuf_release(&buf
);
512 static int process_connect_service(struct transport
*transport
,
513 const char *name
, const char *exec
)
515 struct helper_data
*data
= transport
->data
;
516 struct strbuf cmdbuf
= STRBUF_INIT
;
517 struct child_process
*helper
;
518 int r
, duped
, ret
= 0;
521 helper
= get_helper(transport
);
524 * Yes, dup the pipe another time, as we need unbuffered version
525 * of input pipe as FILE*. fclose() closes the underlying fd and
526 * stream buffering only can be changed before first I/O operation
529 duped
= dup(helper
->out
);
531 die_errno("Can't dup helper output fd");
532 input
= xfdopen(duped
, "r");
533 setvbuf(input
, NULL
, _IONBF
, 0);
536 * Handle --upload-pack and friends. This is fire and forget...
537 * just warn if it fails.
539 if (strcmp(name
, exec
)) {
540 r
= set_helper_option(transport
, "servpath", exec
);
542 warning("Setting remote service path not supported by protocol.");
544 warning("Invalid remote service path.");
548 strbuf_addf(&cmdbuf
, "connect %s\n", name
);
552 sendline(data
, &cmdbuf
);
553 recvline_fh(input
, &cmdbuf
);
554 if (!strcmp(cmdbuf
.buf
, "")) {
555 data
->no_disconnect_req
= 1;
557 fprintf(stderr
, "Debug: Smart transport connection "
560 } else if (!strcmp(cmdbuf
.buf
, "fallback")) {
562 fprintf(stderr
, "Debug: Falling back to dumb "
565 die("Unknown response to connect: %s",
573 static int process_connect(struct transport
*transport
,
576 struct helper_data
*data
= transport
->data
;
580 name
= for_push
? "git-receive-pack" : "git-upload-pack";
582 exec
= data
->transport_options
.receivepack
;
584 exec
= data
->transport_options
.uploadpack
;
586 return process_connect_service(transport
, name
, exec
);
589 static int connect_helper(struct transport
*transport
, const char *name
,
590 const char *exec
, int fd
[2])
592 struct helper_data
*data
= transport
->data
;
594 /* Get_helper so connect is inited. */
595 get_helper(transport
);
597 die("Operation not supported by protocol.");
599 if (!process_connect_service(transport
, name
, exec
))
600 die("Can't connect to subservice %s.", name
);
602 fd
[0] = data
->helper
->out
;
603 fd
[1] = data
->helper
->in
;
607 static int fetch(struct transport
*transport
,
608 int nr_heads
, struct ref
**to_fetch
)
610 struct helper_data
*data
= transport
->data
;
613 if (process_connect(transport
, 0)) {
614 do_take_over(transport
);
615 return transport
->fetch(transport
, nr_heads
, to_fetch
);
619 for (i
= 0; i
< nr_heads
; i
++)
620 if (!(to_fetch
[i
]->status
& REF_STATUS_UPTODATE
))
627 return fetch_with_fetch(transport
, nr_heads
, to_fetch
);
630 return fetch_with_import(transport
, nr_heads
, to_fetch
);
635 static void push_update_ref_status(struct strbuf
*buf
,
637 struct ref
*remote_refs
)
642 if (!prefixcmp(buf
->buf
, "ok ")) {
643 status
= REF_STATUS_OK
;
644 refname
= buf
->buf
+ 3;
645 } else if (!prefixcmp(buf
->buf
, "error ")) {
646 status
= REF_STATUS_REMOTE_REJECT
;
647 refname
= buf
->buf
+ 6;
649 die("expected ok/error, helper said '%s'", buf
->buf
);
651 msg
= strchr(refname
, ' ');
653 struct strbuf msg_buf
= STRBUF_INIT
;
657 if (!unquote_c_style(&msg_buf
, msg
, &end
))
658 msg
= strbuf_detach(&msg_buf
, NULL
);
661 strbuf_release(&msg_buf
);
663 if (!strcmp(msg
, "no match")) {
664 status
= REF_STATUS_NONE
;
668 else if (!strcmp(msg
, "up to date")) {
669 status
= REF_STATUS_UPTODATE
;
673 else if (!strcmp(msg
, "non-fast forward")) {
674 status
= REF_STATUS_REJECT_NONFASTFORWARD
;
678 else if (!strcmp(msg
, "already exists")) {
679 status
= REF_STATUS_REJECT_ALREADY_EXISTS
;
683 else if (!strcmp(msg
, "fetch first")) {
684 status
= REF_STATUS_REJECT_FETCH_FIRST
;
688 else if (!strcmp(msg
, "needs force")) {
689 status
= REF_STATUS_REJECT_NEEDS_FORCE
;
696 *ref
= find_ref_by_name(*ref
, refname
);
698 *ref
= find_ref_by_name(remote_refs
, refname
);
700 warning("helper reported unexpected status of %s", refname
);
704 if ((*ref
)->status
!= REF_STATUS_NONE
) {
706 * Earlier, the ref was marked not to be pushed, so ignore the ref
707 * status reported by the remote helper if the latter is 'no match'.
709 if (status
== REF_STATUS_NONE
)
713 (*ref
)->status
= status
;
714 (*ref
)->remote_status
= msg
;
717 static void push_update_refs_status(struct helper_data
*data
,
718 struct ref
*remote_refs
)
720 struct strbuf buf
= STRBUF_INIT
;
721 struct ref
*ref
= remote_refs
;
723 recvline(data
, &buf
);
727 push_update_ref_status(&buf
, &ref
, remote_refs
);
729 strbuf_release(&buf
);
732 static int push_refs_with_push(struct transport
*transport
,
733 struct ref
*remote_refs
, int flags
)
735 int force_all
= flags
& TRANSPORT_PUSH_FORCE
;
736 int mirror
= flags
& TRANSPORT_PUSH_MIRROR
;
737 struct helper_data
*data
= transport
->data
;
738 struct strbuf buf
= STRBUF_INIT
;
741 get_helper(transport
);
745 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
746 if (!ref
->peer_ref
&& !mirror
)
749 /* Check for statuses set by set_ref_status_for_push() */
750 switch (ref
->status
) {
751 case REF_STATUS_REJECT_NONFASTFORWARD
:
752 case REF_STATUS_REJECT_ALREADY_EXISTS
:
753 case REF_STATUS_UPTODATE
:
762 strbuf_addstr(&buf
, "push ");
763 if (!ref
->deletion
) {
765 strbuf_addch(&buf
, '+');
767 strbuf_addstr(&buf
, ref
->peer_ref
->name
);
769 strbuf_addstr(&buf
, sha1_to_hex(ref
->new_sha1
));
771 strbuf_addch(&buf
, ':');
772 strbuf_addstr(&buf
, ref
->name
);
773 strbuf_addch(&buf
, '\n');
778 standard_options(transport
);
780 if (flags
& TRANSPORT_PUSH_DRY_RUN
) {
781 if (set_helper_option(transport
, "dry-run", "true") != 0)
782 die("helper %s does not support dry-run", data
->name
);
785 strbuf_addch(&buf
, '\n');
786 sendline(data
, &buf
);
787 strbuf_release(&buf
);
789 push_update_refs_status(data
, remote_refs
);
793 static int push_refs_with_export(struct transport
*transport
,
794 struct ref
*remote_refs
, int flags
)
797 struct child_process
*helper
, exporter
;
798 struct helper_data
*data
= transport
->data
;
799 struct string_list revlist_args
= STRING_LIST_INIT_NODUP
;
800 struct strbuf buf
= STRBUF_INIT
;
802 helper
= get_helper(transport
);
804 write_constant(helper
->in
, "export\n");
808 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
810 unsigned char sha1
[20];
814 private = apply_refspecs(data
->refspecs
, data
->refspec_nr
, ref
->name
);
815 if (private && !get_sha1(private, sha1
)) {
816 strbuf_addf(&buf
, "^%s", private);
817 string_list_append(&revlist_args
, strbuf_detach(&buf
, NULL
));
818 hashcpy(ref
->old_sha1
, sha1
);
823 die("remote-helpers do not support ref deletion");
827 string_list_append(&revlist_args
, ref
->peer_ref
->name
);
831 if (get_exporter(transport
, &exporter
, &revlist_args
))
832 die("Couldn't run fast-export");
834 if (finish_command(&exporter
))
835 die("Error while running fast-export");
836 push_update_refs_status(data
, remote_refs
);
840 static int push_refs(struct transport
*transport
,
841 struct ref
*remote_refs
, int flags
)
843 struct helper_data
*data
= transport
->data
;
845 if (process_connect(transport
, 1)) {
846 do_take_over(transport
);
847 return transport
->push_refs(transport
, remote_refs
, flags
);
851 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
852 "Perhaps you should specify a branch such as 'master'.\n");
857 return push_refs_with_push(transport
, remote_refs
, flags
);
860 return push_refs_with_export(transport
, remote_refs
, flags
);
866 static int has_attribute(const char *attrs
, const char *attr
) {
873 const char *space
= strchrnul(attrs
, ' ');
874 if (len
== space
- attrs
&& !strncmp(attrs
, attr
, len
))
882 static struct ref
*get_refs_list(struct transport
*transport
, int for_push
)
884 struct helper_data
*data
= transport
->data
;
885 struct child_process
*helper
;
886 struct ref
*ret
= NULL
;
887 struct ref
**tail
= &ret
;
889 struct strbuf buf
= STRBUF_INIT
;
891 helper
= get_helper(transport
);
893 if (process_connect(transport
, for_push
)) {
894 do_take_over(transport
);
895 return transport
->get_refs_list(transport
, for_push
);
898 if (data
->push
&& for_push
)
899 write_str_in_full(helper
->in
, "list for-push\n");
901 write_str_in_full(helper
->in
, "list\n");
905 recvline(data
, &buf
);
910 eov
= strchr(buf
.buf
, ' ');
912 die("Malformed response in ref list: %s", buf
.buf
);
913 eon
= strchr(eov
+ 1, ' ');
917 *tail
= alloc_ref(eov
+ 1);
918 if (buf
.buf
[0] == '@')
919 (*tail
)->symref
= xstrdup(buf
.buf
+ 1);
920 else if (buf
.buf
[0] != '?')
921 get_sha1_hex(buf
.buf
, (*tail
)->old_sha1
);
923 if (has_attribute(eon
+ 1, "unchanged")) {
924 (*tail
)->status
|= REF_STATUS_UPTODATE
;
925 read_ref((*tail
)->name
, (*tail
)->old_sha1
);
928 tail
= &((*tail
)->next
);
931 fprintf(stderr
, "Debug: Read ref listing.\n");
932 strbuf_release(&buf
);
934 for (posn
= ret
; posn
; posn
= posn
->next
)
935 resolve_remote_symref(posn
, ret
);
940 int transport_helper_init(struct transport
*transport
, const char *name
)
942 struct helper_data
*data
= xcalloc(sizeof(*data
), 1);
945 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
948 transport
->data
= data
;
949 transport
->set_option
= set_helper_option
;
950 transport
->get_refs_list
= get_refs_list
;
951 transport
->fetch
= fetch
;
952 transport
->push_refs
= push_refs
;
953 transport
->disconnect
= release_helper
;
954 transport
->connect
= connect_helper
;
955 transport
->smart_options
= &(data
->transport_options
);
960 * Linux pipes can buffer 65536 bytes at once (and most platforms can
961 * buffer less), so attempt reads and writes with up to that size.
963 #define BUFFERSIZE 65536
964 /* This should be enough to hold debugging message. */
965 #define PBUFFERSIZE 8192
967 /* Print bidirectional transfer loop debug message. */
968 static void transfer_debug(const char *fmt
, ...)
971 char msgbuf
[PBUFFERSIZE
];
972 static int debug_enabled
= -1;
974 if (debug_enabled
< 0)
975 debug_enabled
= getenv("GIT_TRANSLOOP_DEBUG") ? 1 : 0;
980 vsnprintf(msgbuf
, PBUFFERSIZE
, fmt
, args
);
982 fprintf(stderr
, "Transfer loop debugging: %s\n", msgbuf
);
985 /* Stream state: More data may be coming in this direction. */
986 #define SSTATE_TRANSFERING 0
988 * Stream state: No more data coming in this direction, flushing rest of
991 #define SSTATE_FLUSHING 1
992 /* Stream state: Transfer in this direction finished. */
993 #define SSTATE_FINISHED 2
995 #define STATE_NEEDS_READING(state) ((state) <= SSTATE_TRANSFERING)
996 #define STATE_NEEDS_WRITING(state) ((state) <= SSTATE_FLUSHING)
997 #define STATE_NEEDS_CLOSING(state) ((state) == SSTATE_FLUSHING)
999 /* Unidirectional transfer. */
1000 struct unidirectional_transfer
{
1005 /* Is source socket? */
1007 /* Is destination socket? */
1009 /* Transfer state (TRANSFERRING/FLUSHING/FINISHED) */
1012 char buf
[BUFFERSIZE
];
1015 /* Name of source. */
1016 const char *src_name
;
1017 /* Name of destination. */
1018 const char *dest_name
;
1021 /* Closes the target (for writing) if transfer has finished. */
1022 static void udt_close_if_finished(struct unidirectional_transfer
*t
)
1024 if (STATE_NEEDS_CLOSING(t
->state
) && !t
->bufuse
) {
1025 t
->state
= SSTATE_FINISHED
;
1026 if (t
->dest_is_sock
)
1027 shutdown(t
->dest
, SHUT_WR
);
1030 transfer_debug("Closed %s.", t
->dest_name
);
1035 * Tries to read read data from source into buffer. If buffer is full,
1036 * no data is read. Returns 0 on success, -1 on error.
1038 static int udt_do_read(struct unidirectional_transfer
*t
)
1042 if (t
->bufuse
== BUFFERSIZE
)
1043 return 0; /* No space for more. */
1045 transfer_debug("%s is readable", t
->src_name
);
1046 bytes
= read(t
->src
, t
->buf
+ t
->bufuse
, BUFFERSIZE
- t
->bufuse
);
1047 if (bytes
< 0 && errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
&&
1049 error("read(%s) failed: %s", t
->src_name
, strerror(errno
));
1051 } else if (bytes
== 0) {
1052 transfer_debug("%s EOF (with %i bytes in buffer)",
1053 t
->src_name
, t
->bufuse
);
1054 t
->state
= SSTATE_FLUSHING
;
1055 } else if (bytes
> 0) {
1057 transfer_debug("Read %i bytes from %s (buffer now at %i)",
1058 (int)bytes
, t
->src_name
, (int)t
->bufuse
);
1063 /* Tries to write data from buffer into destination. If buffer is empty,
1064 * no data is written. Returns 0 on success, -1 on error.
1066 static int udt_do_write(struct unidirectional_transfer
*t
)
1071 return 0; /* Nothing to write. */
1073 transfer_debug("%s is writable", t
->dest_name
);
1074 bytes
= write(t
->dest
, t
->buf
, t
->bufuse
);
1075 if (bytes
< 0 && errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
&&
1077 error("write(%s) failed: %s", t
->dest_name
, strerror(errno
));
1079 } else if (bytes
> 0) {
1082 memmove(t
->buf
, t
->buf
+ bytes
, t
->bufuse
);
1083 transfer_debug("Wrote %i bytes to %s (buffer now at %i)",
1084 (int)bytes
, t
->dest_name
, (int)t
->bufuse
);
1090 /* State of bidirectional transfer loop. */
1091 struct bidirectional_transfer_state
{
1092 /* Direction from program to git. */
1093 struct unidirectional_transfer ptg
;
1094 /* Direction from git to program. */
1095 struct unidirectional_transfer gtp
;
1098 static void *udt_copy_task_routine(void *udt
)
1100 struct unidirectional_transfer
*t
= (struct unidirectional_transfer
*)udt
;
1101 while (t
->state
!= SSTATE_FINISHED
) {
1102 if (STATE_NEEDS_READING(t
->state
))
1105 if (STATE_NEEDS_WRITING(t
->state
))
1106 if (udt_do_write(t
))
1108 if (STATE_NEEDS_CLOSING(t
->state
))
1109 udt_close_if_finished(t
);
1111 return udt
; /* Just some non-NULL value. */
1117 * Join thread, with apporiate errors on failure. Name is name for the
1118 * thread (for error messages). Returns 0 on success, 1 on failure.
1120 static int tloop_join(pthread_t thread
, const char *name
)
1124 err
= pthread_join(thread
, &tret
);
1126 error("%s thread failed", name
);
1130 error("%s thread failed to join: %s", name
, strerror(err
));
1137 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1140 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1142 pthread_t gtp_thread
;
1143 pthread_t ptg_thread
;
1146 err
= pthread_create(>p_thread
, NULL
, udt_copy_task_routine
,
1149 die("Can't start thread for copying data: %s", strerror(err
));
1150 err
= pthread_create(&ptg_thread
, NULL
, udt_copy_task_routine
,
1153 die("Can't start thread for copying data: %s", strerror(err
));
1155 ret
|= tloop_join(gtp_thread
, "Git to program copy");
1156 ret
|= tloop_join(ptg_thread
, "Program to git copy");
1161 /* Close the source and target (for writing) for transfer. */
1162 static void udt_kill_transfer(struct unidirectional_transfer
*t
)
1164 t
->state
= SSTATE_FINISHED
;
1166 * Socket read end left open isn't a disaster if nobody
1167 * attempts to read from it (mingw compat headers do not
1170 * We can't fully close the socket since otherwise gtp
1171 * task would first close the socket it sends data to
1172 * while closing the ptg file descriptors.
1174 if (!t
->src_is_sock
)
1176 if (t
->dest_is_sock
)
1177 shutdown(t
->dest
, SHUT_WR
);
1183 * Join process, with apporiate errors on failure. Name is name for the
1184 * process (for error messages). Returns 0 on success, 1 on failure.
1186 static int tloop_join(pid_t pid
, const char *name
)
1189 if (waitpid(pid
, &tret
, 0) < 0) {
1190 error("%s process failed to wait: %s", name
, strerror(errno
));
1193 if (!WIFEXITED(tret
) || WEXITSTATUS(tret
)) {
1194 error("%s process failed", name
);
1201 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1204 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1209 /* Fork thread #1: git to program. */
1212 die_errno("Can't start thread for copying data");
1213 else if (pid1
== 0) {
1214 udt_kill_transfer(&s
->ptg
);
1215 exit(udt_copy_task_routine(&s
->gtp
) ? 0 : 1);
1218 /* Fork thread #2: program to git. */
1221 die_errno("Can't start thread for copying data");
1222 else if (pid2
== 0) {
1223 udt_kill_transfer(&s
->gtp
);
1224 exit(udt_copy_task_routine(&s
->ptg
) ? 0 : 1);
1228 * Close both streams in parent as to not interfere with
1229 * end of file detection and wait for both tasks to finish.
1231 udt_kill_transfer(&s
->gtp
);
1232 udt_kill_transfer(&s
->ptg
);
1233 ret
|= tloop_join(pid1
, "Git to program copy");
1234 ret
|= tloop_join(pid2
, "Program to git copy");
1240 * Copies data from stdin to output and from input to stdout simultaneously.
1241 * Additionally filtering through given filter. If filter is NULL, uses
1244 int bidirectional_transfer_loop(int input
, int output
)
1246 struct bidirectional_transfer_state state
;
1248 /* Fill the state fields. */
1249 state
.ptg
.src
= input
;
1251 state
.ptg
.src_is_sock
= (input
== output
);
1252 state
.ptg
.dest_is_sock
= 0;
1253 state
.ptg
.state
= SSTATE_TRANSFERING
;
1254 state
.ptg
.bufuse
= 0;
1255 state
.ptg
.src_name
= "remote input";
1256 state
.ptg
.dest_name
= "stdout";
1259 state
.gtp
.dest
= output
;
1260 state
.gtp
.src_is_sock
= 0;
1261 state
.gtp
.dest_is_sock
= (input
== output
);
1262 state
.gtp
.state
= SSTATE_TRANSFERING
;
1263 state
.gtp
.bufuse
= 0;
1264 state
.gtp
.src_name
= "stdin";
1265 state
.gtp
.dest_name
= "remote output";
1267 return tloop_spawnwait_tasks(&state
);