4 #include "run-command.h"
10 #include "string-list.h"
11 #include "thread-utils.h"
13 #include "argv-array.h"
20 struct child_process
*helper
;
30 check_connectivity
: 1,
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
, const char *name
)
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
, helper
->name
);
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 (!strcmp(capname
, "check-connectivity"))
191 data
->check_connectivity
= 1;
192 else if (!data
->refspecs
&& !prefixcmp(capname
, "refspec ")) {
196 refspecs
[refspec_nr
++] = xstrdup(capname
+ strlen("refspec "));
197 } else if (!strcmp(capname
, "connect")) {
199 } else if (!strcmp(capname
, "signed-tags")) {
200 data
->signed_tags
= 1;
201 } else if (!prefixcmp(capname
, "export-marks ")) {
202 struct strbuf arg
= STRBUF_INIT
;
203 strbuf_addstr(&arg
, "--export-marks=");
204 strbuf_addstr(&arg
, capname
+ strlen("export-marks "));
205 data
->export_marks
= strbuf_detach(&arg
, NULL
);
206 } else if (!prefixcmp(capname
, "import-marks")) {
207 struct strbuf arg
= STRBUF_INIT
;
208 strbuf_addstr(&arg
, "--import-marks=");
209 strbuf_addstr(&arg
, capname
+ strlen("import-marks "));
210 data
->import_marks
= strbuf_detach(&arg
, NULL
);
211 } else if (mandatory
) {
212 die("Unknown mandatory capability %s. This remote "
213 "helper probably needs newer version of Git.",
219 data
->refspec_nr
= refspec_nr
;
220 data
->refspecs
= parse_fetch_refspec(refspec_nr
, refspecs
);
221 for (i
= 0; i
< refspec_nr
; i
++)
222 free((char *)refspecs
[i
]);
224 } else if (data
->import
|| data
->bidi_import
|| data
->export
) {
225 warning("This remote helper should implement refspec capability.");
227 strbuf_release(&buf
);
229 fprintf(stderr
, "Debug: Capabilities complete.\n");
233 static int disconnect_helper(struct transport
*transport
)
235 struct helper_data
*data
= transport
->data
;
240 fprintf(stderr
, "Debug: Disconnecting.\n");
241 if (!data
->no_disconnect_req
) {
243 * Ignore write errors; there's nothing we can do,
244 * since we're about to close the pipe anyway. And the
245 * most likely error is EPIPE due to the helper dying
246 * to report an error itself.
248 sigchain_push(SIGPIPE
, SIG_IGN
);
249 xwrite(data
->helper
->in
, "\n", 1);
250 sigchain_pop(SIGPIPE
);
252 close(data
->helper
->in
);
253 close(data
->helper
->out
);
255 res
= finish_command(data
->helper
);
256 argv_array_free_detached(data
->helper
->argv
);
263 static const char *unsupported_options
[] = {
264 TRANS_OPT_UPLOADPACK
,
265 TRANS_OPT_RECEIVEPACK
,
269 static const char *boolean_options
[] = {
275 static int set_helper_option(struct transport
*transport
,
276 const char *name
, const char *value
)
278 struct helper_data
*data
= transport
->data
;
279 struct strbuf buf
= STRBUF_INIT
;
280 int i
, ret
, is_bool
= 0;
282 get_helper(transport
);
287 for (i
= 0; i
< ARRAY_SIZE(unsupported_options
); i
++) {
288 if (!strcmp(name
, unsupported_options
[i
]))
292 for (i
= 0; i
< ARRAY_SIZE(boolean_options
); i
++) {
293 if (!strcmp(name
, boolean_options
[i
])) {
299 strbuf_addf(&buf
, "option %s ", name
);
301 strbuf_addstr(&buf
, value
? "true" : "false");
303 quote_c_style(value
, &buf
, NULL
, 0);
304 strbuf_addch(&buf
, '\n');
306 xchgline(data
, &buf
);
308 if (!strcmp(buf
.buf
, "ok"))
310 else if (!prefixcmp(buf
.buf
, "error")) {
312 } else if (!strcmp(buf
.buf
, "unsupported"))
315 warning("%s unexpectedly said: '%s'", data
->name
, buf
.buf
);
318 strbuf_release(&buf
);
322 static void standard_options(struct transport
*t
)
328 set_helper_option(t
, "progress", t
->progress
? "true" : "false");
330 n
= snprintf(buf
, sizeof(buf
), "%d", v
+ 1);
331 if (n
>= sizeof(buf
))
332 die("impossibly large verbosity value");
333 set_helper_option(t
, "verbosity", buf
);
336 static int release_helper(struct transport
*transport
)
339 struct helper_data
*data
= transport
->data
;
340 free_refspec(data
->refspec_nr
, data
->refspecs
);
341 data
->refspecs
= NULL
;
342 res
= disconnect_helper(transport
);
343 free(transport
->data
);
347 static int fetch_with_fetch(struct transport
*transport
,
348 int nr_heads
, struct ref
**to_fetch
)
350 struct helper_data
*data
= transport
->data
;
352 struct strbuf buf
= STRBUF_INIT
;
354 standard_options(transport
);
355 if (data
->check_connectivity
&&
356 data
->transport_options
.check_self_contained_and_connected
)
357 set_helper_option(transport
, "check-connectivity", "true");
359 for (i
= 0; i
< nr_heads
; i
++) {
360 const struct ref
*posn
= to_fetch
[i
];
361 if (posn
->status
& REF_STATUS_UPTODATE
)
364 strbuf_addf(&buf
, "fetch %s %s\n",
365 sha1_to_hex(posn
->old_sha1
), posn
->name
);
368 strbuf_addch(&buf
, '\n');
369 sendline(data
, &buf
);
372 recvline(data
, &buf
);
374 if (!prefixcmp(buf
.buf
, "lock ")) {
375 const char *name
= buf
.buf
+ 5;
376 if (transport
->pack_lockfile
)
377 warning("%s also locked %s", data
->name
, name
);
379 transport
->pack_lockfile
= xstrdup(name
);
381 else if (data
->check_connectivity
&&
382 data
->transport_options
.check_self_contained_and_connected
&&
383 !strcmp(buf
.buf
, "connectivity-ok"))
384 data
->transport_options
.self_contained_and_connected
= 1;
388 warning("%s unexpectedly said: '%s'", data
->name
, buf
.buf
);
390 strbuf_release(&buf
);
394 static int get_importer(struct transport
*transport
, struct child_process
*fastimport
)
396 struct child_process
*helper
= get_helper(transport
);
397 struct helper_data
*data
= transport
->data
;
398 struct argv_array argv
= ARGV_ARRAY_INIT
;
399 int cat_blob_fd
, code
;
400 memset(fastimport
, 0, sizeof(*fastimport
));
401 fastimport
->in
= helper
->out
;
402 argv_array_push(&argv
, "fast-import");
403 argv_array_push(&argv
, debug
? "--stats" : "--quiet");
405 if (data
->bidi_import
) {
406 cat_blob_fd
= xdup(helper
->in
);
407 argv_array_pushf(&argv
, "--cat-blob-fd=%d", cat_blob_fd
);
409 fastimport
->argv
= argv
.argv
;
410 fastimport
->git_cmd
= 1;
412 code
= start_command(fastimport
);
416 static int get_exporter(struct transport
*transport
,
417 struct child_process
*fastexport
,
418 struct string_list
*revlist_args
)
420 struct helper_data
*data
= transport
->data
;
421 struct child_process
*helper
= get_helper(transport
);
423 memset(fastexport
, 0, sizeof(*fastexport
));
425 /* we need to duplicate helper->in because we want to use it after
426 * fastexport is done with it. */
427 fastexport
->out
= dup(helper
->in
);
428 fastexport
->argv
= xcalloc(6 + revlist_args
->nr
, sizeof(*fastexport
->argv
));
429 fastexport
->argv
[argc
++] = "fast-export";
430 fastexport
->argv
[argc
++] = "--use-done-feature";
431 fastexport
->argv
[argc
++] = data
->signed_tags
?
432 "--signed-tags=verbatim" : "--signed-tags=warn-strip";
433 if (data
->export_marks
)
434 fastexport
->argv
[argc
++] = data
->export_marks
;
435 if (data
->import_marks
)
436 fastexport
->argv
[argc
++] = data
->import_marks
;
438 for (i
= 0; i
< revlist_args
->nr
; i
++)
439 fastexport
->argv
[argc
++] = revlist_args
->items
[i
].string
;
441 fastexport
->git_cmd
= 1;
442 return start_command(fastexport
);
445 static int fetch_with_import(struct transport
*transport
,
446 int nr_heads
, struct ref
**to_fetch
)
448 struct child_process fastimport
;
449 struct helper_data
*data
= transport
->data
;
452 struct strbuf buf
= STRBUF_INIT
;
454 get_helper(transport
);
456 if (get_importer(transport
, &fastimport
))
457 die("Couldn't run fast-import");
459 for (i
= 0; i
< nr_heads
; i
++) {
461 if (posn
->status
& REF_STATUS_UPTODATE
)
464 strbuf_addf(&buf
, "import %s\n", posn
->name
);
465 sendline(data
, &buf
);
469 write_constant(data
->helper
->in
, "\n");
471 * remote-helpers that advertise the bidi-import capability are required to
472 * buffer the complete batch of import commands until this newline before
473 * sending data to fast-import.
474 * These helpers read back data from fast-import on their stdin, which could
475 * be mixed with import commands, otherwise.
478 if (finish_command(&fastimport
))
479 die("Error while running fast-import");
480 argv_array_free_detached(fastimport
.argv
);
483 * The fast-import stream of a remote helper that advertises
484 * the "refspec" capability writes to the refs named after the
485 * right hand side of the first refspec matching each ref we
488 * (If no "refspec" capability was specified, for historical
489 * reasons we default to the equivalent of *:*.)
491 * Store the result in to_fetch[i].old_sha1. Callers such
492 * as "git fetch" can use the value to write feedback to the
493 * terminal, populate FETCH_HEAD, and determine what new value
494 * should be written to peer_ref if the update is a
495 * fast-forward or this is a forced update.
497 for (i
= 0; i
< nr_heads
; i
++) {
500 if (posn
->status
& REF_STATUS_UPTODATE
)
503 private = apply_refspecs(data
->refspecs
, data
->refspec_nr
, posn
->name
);
505 private = xstrdup(posn
->name
);
507 read_ref(private, posn
->old_sha1
);
511 strbuf_release(&buf
);
515 static int process_connect_service(struct transport
*transport
,
516 const char *name
, const char *exec
)
518 struct helper_data
*data
= transport
->data
;
519 struct strbuf cmdbuf
= STRBUF_INIT
;
520 struct child_process
*helper
;
521 int r
, duped
, ret
= 0;
524 helper
= get_helper(transport
);
527 * Yes, dup the pipe another time, as we need unbuffered version
528 * of input pipe as FILE*. fclose() closes the underlying fd and
529 * stream buffering only can be changed before first I/O operation
532 duped
= dup(helper
->out
);
534 die_errno("Can't dup helper output fd");
535 input
= xfdopen(duped
, "r");
536 setvbuf(input
, NULL
, _IONBF
, 0);
539 * Handle --upload-pack and friends. This is fire and forget...
540 * just warn if it fails.
542 if (strcmp(name
, exec
)) {
543 r
= set_helper_option(transport
, "servpath", exec
);
545 warning("Setting remote service path not supported by protocol.");
547 warning("Invalid remote service path.");
551 strbuf_addf(&cmdbuf
, "connect %s\n", name
);
555 sendline(data
, &cmdbuf
);
556 recvline_fh(input
, &cmdbuf
, name
);
557 if (!strcmp(cmdbuf
.buf
, "")) {
558 data
->no_disconnect_req
= 1;
560 fprintf(stderr
, "Debug: Smart transport connection "
563 } else if (!strcmp(cmdbuf
.buf
, "fallback")) {
565 fprintf(stderr
, "Debug: Falling back to dumb "
568 die("Unknown response to connect: %s",
576 static int process_connect(struct transport
*transport
,
579 struct helper_data
*data
= transport
->data
;
583 name
= for_push
? "git-receive-pack" : "git-upload-pack";
585 exec
= data
->transport_options
.receivepack
;
587 exec
= data
->transport_options
.uploadpack
;
589 return process_connect_service(transport
, name
, exec
);
592 static int connect_helper(struct transport
*transport
, const char *name
,
593 const char *exec
, int fd
[2])
595 struct helper_data
*data
= transport
->data
;
597 /* Get_helper so connect is inited. */
598 get_helper(transport
);
600 die("Operation not supported by protocol.");
602 if (!process_connect_service(transport
, name
, exec
))
603 die("Can't connect to subservice %s.", name
);
605 fd
[0] = data
->helper
->out
;
606 fd
[1] = data
->helper
->in
;
610 static int fetch(struct transport
*transport
,
611 int nr_heads
, struct ref
**to_fetch
)
613 struct helper_data
*data
= transport
->data
;
616 if (process_connect(transport
, 0)) {
617 do_take_over(transport
);
618 return transport
->fetch(transport
, nr_heads
, to_fetch
);
622 for (i
= 0; i
< nr_heads
; i
++)
623 if (!(to_fetch
[i
]->status
& REF_STATUS_UPTODATE
))
630 return fetch_with_fetch(transport
, nr_heads
, to_fetch
);
633 return fetch_with_import(transport
, nr_heads
, to_fetch
);
638 static int push_update_ref_status(struct strbuf
*buf
,
640 struct ref
*remote_refs
)
645 if (!prefixcmp(buf
->buf
, "ok ")) {
646 status
= REF_STATUS_OK
;
647 refname
= buf
->buf
+ 3;
648 } else if (!prefixcmp(buf
->buf
, "error ")) {
649 status
= REF_STATUS_REMOTE_REJECT
;
650 refname
= buf
->buf
+ 6;
652 die("expected ok/error, helper said '%s'", buf
->buf
);
654 msg
= strchr(refname
, ' ');
656 struct strbuf msg_buf
= STRBUF_INIT
;
660 if (!unquote_c_style(&msg_buf
, msg
, &end
))
661 msg
= strbuf_detach(&msg_buf
, NULL
);
664 strbuf_release(&msg_buf
);
666 if (!strcmp(msg
, "no match")) {
667 status
= REF_STATUS_NONE
;
671 else if (!strcmp(msg
, "up to date")) {
672 status
= REF_STATUS_UPTODATE
;
676 else if (!strcmp(msg
, "non-fast forward")) {
677 status
= REF_STATUS_REJECT_NONFASTFORWARD
;
681 else if (!strcmp(msg
, "already exists")) {
682 status
= REF_STATUS_REJECT_ALREADY_EXISTS
;
686 else if (!strcmp(msg
, "fetch first")) {
687 status
= REF_STATUS_REJECT_FETCH_FIRST
;
691 else if (!strcmp(msg
, "needs force")) {
692 status
= REF_STATUS_REJECT_NEEDS_FORCE
;
696 else if (!strcmp(msg
, "stale info")) {
697 status
= REF_STATUS_REJECT_STALE
;
704 *ref
= find_ref_by_name(*ref
, refname
);
706 *ref
= find_ref_by_name(remote_refs
, refname
);
708 warning("helper reported unexpected status of %s", refname
);
712 if ((*ref
)->status
!= REF_STATUS_NONE
) {
714 * Earlier, the ref was marked not to be pushed, so ignore the ref
715 * status reported by the remote helper if the latter is 'no match'.
717 if (status
== REF_STATUS_NONE
)
721 (*ref
)->status
= status
;
722 (*ref
)->remote_status
= msg
;
723 return !(status
== REF_STATUS_OK
);
726 static void push_update_refs_status(struct helper_data
*data
,
727 struct ref
*remote_refs
)
729 struct strbuf buf
= STRBUF_INIT
;
730 struct ref
*ref
= remote_refs
;
734 recvline(data
, &buf
);
738 if (push_update_ref_status(&buf
, &ref
, remote_refs
))
744 /* propagate back the update to the remote namespace */
745 private = apply_refspecs(data
->refspecs
, data
->refspec_nr
, ref
->name
);
748 update_ref("update by helper", private, ref
->new_sha1
, NULL
, 0, 0);
751 strbuf_release(&buf
);
754 static int push_refs_with_push(struct transport
*transport
,
755 struct ref
*remote_refs
, int flags
)
757 int force_all
= flags
& TRANSPORT_PUSH_FORCE
;
758 int mirror
= flags
& TRANSPORT_PUSH_MIRROR
;
759 struct helper_data
*data
= transport
->data
;
760 struct strbuf buf
= STRBUF_INIT
;
762 struct string_list cas_options
= STRING_LIST_INIT_DUP
;
763 struct string_list_item
*cas_option
;
765 get_helper(transport
);
769 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
770 if (!ref
->peer_ref
&& !mirror
)
773 /* Check for statuses set by set_ref_status_for_push() */
774 switch (ref
->status
) {
775 case REF_STATUS_REJECT_NONFASTFORWARD
:
776 case REF_STATUS_REJECT_STALE
:
777 case REF_STATUS_REJECT_ALREADY_EXISTS
:
778 case REF_STATUS_UPTODATE
:
787 strbuf_addstr(&buf
, "push ");
788 if (!ref
->deletion
) {
790 strbuf_addch(&buf
, '+');
792 strbuf_addstr(&buf
, ref
->peer_ref
->name
);
794 strbuf_addstr(&buf
, sha1_to_hex(ref
->new_sha1
));
796 strbuf_addch(&buf
, ':');
797 strbuf_addstr(&buf
, ref
->name
);
798 strbuf_addch(&buf
, '\n');
801 * The "--force-with-lease" options without explicit
802 * values to expect have already been expanded into
803 * the ref->old_sha1_expect[] field; we can ignore
804 * transport->smart_options->cas altogether and instead
805 * can enumerate them from the refs.
807 if (ref
->expect_old_sha1
) {
808 struct strbuf cas
= STRBUF_INIT
;
809 strbuf_addf(&cas
, "%s:%s",
810 ref
->name
, sha1_to_hex(ref
->old_sha1_expect
));
811 string_list_append(&cas_options
, strbuf_detach(&cas
, NULL
));
815 string_list_clear(&cas_options
, 0);
819 standard_options(transport
);
820 for_each_string_list_item(cas_option
, &cas_options
)
821 set_helper_option(transport
, "cas", cas_option
->string
);
823 if (flags
& TRANSPORT_PUSH_DRY_RUN
) {
824 if (set_helper_option(transport
, "dry-run", "true") != 0)
825 die("helper %s does not support dry-run", data
->name
);
828 strbuf_addch(&buf
, '\n');
829 sendline(data
, &buf
);
830 strbuf_release(&buf
);
832 push_update_refs_status(data
, remote_refs
);
836 static int push_refs_with_export(struct transport
*transport
,
837 struct ref
*remote_refs
, int flags
)
840 struct child_process
*helper
, exporter
;
841 struct helper_data
*data
= transport
->data
;
842 struct string_list revlist_args
= STRING_LIST_INIT_NODUP
;
843 struct strbuf buf
= STRBUF_INIT
;
846 die("remote-helper doesn't support push; refspec needed");
848 if (flags
& TRANSPORT_PUSH_DRY_RUN
) {
849 if (set_helper_option(transport
, "dry-run", "true") != 0)
850 die("helper %s does not support dry-run", data
->name
);
853 helper
= get_helper(transport
);
855 write_constant(helper
->in
, "export\n");
859 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
861 unsigned char sha1
[20];
864 die("remote-helpers do not support ref deletion");
866 private = apply_refspecs(data
->refspecs
, data
->refspec_nr
, ref
->name
);
867 if (private && !get_sha1(private, sha1
)) {
868 strbuf_addf(&buf
, "^%s", private);
869 string_list_append(&revlist_args
, strbuf_detach(&buf
, NULL
));
870 hashcpy(ref
->old_sha1
, sha1
);
875 die("remote-helpers do not support ref deletion");
878 if (strcmp(ref
->peer_ref
->name
, ref
->name
))
879 die("remote-helpers do not support old:new syntax");
880 string_list_append(&revlist_args
, ref
->peer_ref
->name
);
884 if (get_exporter(transport
, &exporter
, &revlist_args
))
885 die("Couldn't run fast-export");
887 if (finish_command(&exporter
))
888 die("Error while running fast-export");
889 push_update_refs_status(data
, remote_refs
);
893 static int push_refs(struct transport
*transport
,
894 struct ref
*remote_refs
, int flags
)
896 struct helper_data
*data
= transport
->data
;
898 if (process_connect(transport
, 1)) {
899 do_take_over(transport
);
900 return transport
->push_refs(transport
, remote_refs
, flags
);
904 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
905 "Perhaps you should specify a branch such as 'master'.\n");
910 return push_refs_with_push(transport
, remote_refs
, flags
);
913 return push_refs_with_export(transport
, remote_refs
, flags
);
919 static int has_attribute(const char *attrs
, const char *attr
) {
926 const char *space
= strchrnul(attrs
, ' ');
927 if (len
== space
- attrs
&& !strncmp(attrs
, attr
, len
))
935 static struct ref
*get_refs_list(struct transport
*transport
, int for_push
)
937 struct helper_data
*data
= transport
->data
;
938 struct child_process
*helper
;
939 struct ref
*ret
= NULL
;
940 struct ref
**tail
= &ret
;
942 struct strbuf buf
= STRBUF_INIT
;
944 helper
= get_helper(transport
);
946 if (process_connect(transport
, for_push
)) {
947 do_take_over(transport
);
948 return transport
->get_refs_list(transport
, for_push
);
951 if (data
->push
&& for_push
)
952 write_str_in_full(helper
->in
, "list for-push\n");
954 write_str_in_full(helper
->in
, "list\n");
958 recvline(data
, &buf
);
963 eov
= strchr(buf
.buf
, ' ');
965 die("Malformed response in ref list: %s", buf
.buf
);
966 eon
= strchr(eov
+ 1, ' ');
970 *tail
= alloc_ref(eov
+ 1);
971 if (buf
.buf
[0] == '@')
972 (*tail
)->symref
= xstrdup(buf
.buf
+ 1);
973 else if (buf
.buf
[0] != '?')
974 get_sha1_hex(buf
.buf
, (*tail
)->old_sha1
);
976 if (has_attribute(eon
+ 1, "unchanged")) {
977 (*tail
)->status
|= REF_STATUS_UPTODATE
;
978 read_ref((*tail
)->name
, (*tail
)->old_sha1
);
981 tail
= &((*tail
)->next
);
984 fprintf(stderr
, "Debug: Read ref listing.\n");
985 strbuf_release(&buf
);
987 for (posn
= ret
; posn
; posn
= posn
->next
)
988 resolve_remote_symref(posn
, ret
);
993 int transport_helper_init(struct transport
*transport
, const char *name
)
995 struct helper_data
*data
= xcalloc(sizeof(*data
), 1);
998 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
1001 transport
->data
= data
;
1002 transport
->set_option
= set_helper_option
;
1003 transport
->get_refs_list
= get_refs_list
;
1004 transport
->fetch
= fetch
;
1005 transport
->push_refs
= push_refs
;
1006 transport
->disconnect
= release_helper
;
1007 transport
->connect
= connect_helper
;
1008 transport
->smart_options
= &(data
->transport_options
);
1013 * Linux pipes can buffer 65536 bytes at once (and most platforms can
1014 * buffer less), so attempt reads and writes with up to that size.
1016 #define BUFFERSIZE 65536
1017 /* This should be enough to hold debugging message. */
1018 #define PBUFFERSIZE 8192
1020 /* Print bidirectional transfer loop debug message. */
1021 __attribute__((format (printf
, 1, 2)))
1022 static void transfer_debug(const char *fmt
, ...)
1025 char msgbuf
[PBUFFERSIZE
];
1026 static int debug_enabled
= -1;
1028 if (debug_enabled
< 0)
1029 debug_enabled
= getenv("GIT_TRANSLOOP_DEBUG") ? 1 : 0;
1033 va_start(args
, fmt
);
1034 vsnprintf(msgbuf
, PBUFFERSIZE
, fmt
, args
);
1036 fprintf(stderr
, "Transfer loop debugging: %s\n", msgbuf
);
1039 /* Stream state: More data may be coming in this direction. */
1040 #define SSTATE_TRANSFERING 0
1042 * Stream state: No more data coming in this direction, flushing rest of
1045 #define SSTATE_FLUSHING 1
1046 /* Stream state: Transfer in this direction finished. */
1047 #define SSTATE_FINISHED 2
1049 #define STATE_NEEDS_READING(state) ((state) <= SSTATE_TRANSFERING)
1050 #define STATE_NEEDS_WRITING(state) ((state) <= SSTATE_FLUSHING)
1051 #define STATE_NEEDS_CLOSING(state) ((state) == SSTATE_FLUSHING)
1053 /* Unidirectional transfer. */
1054 struct unidirectional_transfer
{
1059 /* Is source socket? */
1061 /* Is destination socket? */
1063 /* Transfer state (TRANSFERRING/FLUSHING/FINISHED) */
1066 char buf
[BUFFERSIZE
];
1069 /* Name of source. */
1070 const char *src_name
;
1071 /* Name of destination. */
1072 const char *dest_name
;
1075 /* Closes the target (for writing) if transfer has finished. */
1076 static void udt_close_if_finished(struct unidirectional_transfer
*t
)
1078 if (STATE_NEEDS_CLOSING(t
->state
) && !t
->bufuse
) {
1079 t
->state
= SSTATE_FINISHED
;
1080 if (t
->dest_is_sock
)
1081 shutdown(t
->dest
, SHUT_WR
);
1084 transfer_debug("Closed %s.", t
->dest_name
);
1089 * Tries to read read data from source into buffer. If buffer is full,
1090 * no data is read. Returns 0 on success, -1 on error.
1092 static int udt_do_read(struct unidirectional_transfer
*t
)
1096 if (t
->bufuse
== BUFFERSIZE
)
1097 return 0; /* No space for more. */
1099 transfer_debug("%s is readable", t
->src_name
);
1100 bytes
= read(t
->src
, t
->buf
+ t
->bufuse
, BUFFERSIZE
- t
->bufuse
);
1101 if (bytes
< 0 && errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
&&
1103 error("read(%s) failed: %s", t
->src_name
, strerror(errno
));
1105 } else if (bytes
== 0) {
1106 transfer_debug("%s EOF (with %i bytes in buffer)",
1107 t
->src_name
, (int)t
->bufuse
);
1108 t
->state
= SSTATE_FLUSHING
;
1109 } else if (bytes
> 0) {
1111 transfer_debug("Read %i bytes from %s (buffer now at %i)",
1112 (int)bytes
, t
->src_name
, (int)t
->bufuse
);
1117 /* Tries to write data from buffer into destination. If buffer is empty,
1118 * no data is written. Returns 0 on success, -1 on error.
1120 static int udt_do_write(struct unidirectional_transfer
*t
)
1125 return 0; /* Nothing to write. */
1127 transfer_debug("%s is writable", t
->dest_name
);
1128 bytes
= write(t
->dest
, t
->buf
, t
->bufuse
);
1129 if (bytes
< 0 && errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
&&
1131 error("write(%s) failed: %s", t
->dest_name
, strerror(errno
));
1133 } else if (bytes
> 0) {
1136 memmove(t
->buf
, t
->buf
+ bytes
, t
->bufuse
);
1137 transfer_debug("Wrote %i bytes to %s (buffer now at %i)",
1138 (int)bytes
, t
->dest_name
, (int)t
->bufuse
);
1144 /* State of bidirectional transfer loop. */
1145 struct bidirectional_transfer_state
{
1146 /* Direction from program to git. */
1147 struct unidirectional_transfer ptg
;
1148 /* Direction from git to program. */
1149 struct unidirectional_transfer gtp
;
1152 static void *udt_copy_task_routine(void *udt
)
1154 struct unidirectional_transfer
*t
= (struct unidirectional_transfer
*)udt
;
1155 while (t
->state
!= SSTATE_FINISHED
) {
1156 if (STATE_NEEDS_READING(t
->state
))
1159 if (STATE_NEEDS_WRITING(t
->state
))
1160 if (udt_do_write(t
))
1162 if (STATE_NEEDS_CLOSING(t
->state
))
1163 udt_close_if_finished(t
);
1165 return udt
; /* Just some non-NULL value. */
1171 * Join thread, with appropriate errors on failure. Name is name for the
1172 * thread (for error messages). Returns 0 on success, 1 on failure.
1174 static int tloop_join(pthread_t thread
, const char *name
)
1178 err
= pthread_join(thread
, &tret
);
1180 error("%s thread failed", name
);
1184 error("%s thread failed to join: %s", name
, strerror(err
));
1191 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1194 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1196 pthread_t gtp_thread
;
1197 pthread_t ptg_thread
;
1200 err
= pthread_create(>p_thread
, NULL
, udt_copy_task_routine
,
1203 die("Can't start thread for copying data: %s", strerror(err
));
1204 err
= pthread_create(&ptg_thread
, NULL
, udt_copy_task_routine
,
1207 die("Can't start thread for copying data: %s", strerror(err
));
1209 ret
|= tloop_join(gtp_thread
, "Git to program copy");
1210 ret
|= tloop_join(ptg_thread
, "Program to git copy");
1215 /* Close the source and target (for writing) for transfer. */
1216 static void udt_kill_transfer(struct unidirectional_transfer
*t
)
1218 t
->state
= SSTATE_FINISHED
;
1220 * Socket read end left open isn't a disaster if nobody
1221 * attempts to read from it (mingw compat headers do not
1224 * We can't fully close the socket since otherwise gtp
1225 * task would first close the socket it sends data to
1226 * while closing the ptg file descriptors.
1228 if (!t
->src_is_sock
)
1230 if (t
->dest_is_sock
)
1231 shutdown(t
->dest
, SHUT_WR
);
1237 * Join process, with appropriate errors on failure. Name is name for the
1238 * process (for error messages). Returns 0 on success, 1 on failure.
1240 static int tloop_join(pid_t pid
, const char *name
)
1243 if (waitpid(pid
, &tret
, 0) < 0) {
1244 error("%s process failed to wait: %s", name
, strerror(errno
));
1247 if (!WIFEXITED(tret
) || WEXITSTATUS(tret
)) {
1248 error("%s process failed", name
);
1255 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1258 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1263 /* Fork thread #1: git to program. */
1266 die_errno("Can't start thread for copying data");
1267 else if (pid1
== 0) {
1268 udt_kill_transfer(&s
->ptg
);
1269 exit(udt_copy_task_routine(&s
->gtp
) ? 0 : 1);
1272 /* Fork thread #2: program to git. */
1275 die_errno("Can't start thread for copying data");
1276 else if (pid2
== 0) {
1277 udt_kill_transfer(&s
->gtp
);
1278 exit(udt_copy_task_routine(&s
->ptg
) ? 0 : 1);
1282 * Close both streams in parent as to not interfere with
1283 * end of file detection and wait for both tasks to finish.
1285 udt_kill_transfer(&s
->gtp
);
1286 udt_kill_transfer(&s
->ptg
);
1287 ret
|= tloop_join(pid1
, "Git to program copy");
1288 ret
|= tloop_join(pid2
, "Program to git copy");
1294 * Copies data from stdin to output and from input to stdout simultaneously.
1295 * Additionally filtering through given filter. If filter is NULL, uses
1298 int bidirectional_transfer_loop(int input
, int output
)
1300 struct bidirectional_transfer_state state
;
1302 /* Fill the state fields. */
1303 state
.ptg
.src
= input
;
1305 state
.ptg
.src_is_sock
= (input
== output
);
1306 state
.ptg
.dest_is_sock
= 0;
1307 state
.ptg
.state
= SSTATE_TRANSFERING
;
1308 state
.ptg
.bufuse
= 0;
1309 state
.ptg
.src_name
= "remote input";
1310 state
.ptg
.dest_name
= "stdout";
1313 state
.gtp
.dest
= output
;
1314 state
.gtp
.src_is_sock
= 0;
1315 state
.gtp
.dest_is_sock
= (input
== output
);
1316 state
.gtp
.state
= SSTATE_TRANSFERING
;
1317 state
.gtp
.bufuse
= 0;
1318 state
.gtp
.src_name
= "stdin";
1319 state
.gtp
.dest_name
= "remote output";
1321 return tloop_spawnwait_tasks(&state
);