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,
32 no_private_update
: 1;
35 /* These go from remote name (as in "list") to private name */
36 struct refspec
*refspecs
;
38 /* Transport options for fetch-pack/send-pack (should one of
41 struct git_transport_options transport_options
;
44 static void sendline(struct helper_data
*helper
, struct strbuf
*buffer
)
47 fprintf(stderr
, "Debug: Remote helper: -> %s", buffer
->buf
);
48 if (write_in_full(helper
->helper
->in
, buffer
->buf
, buffer
->len
)
50 die_errno("Full write to remote helper failed");
53 static int recvline_fh(FILE *helper
, struct strbuf
*buffer
, const char *name
)
57 fprintf(stderr
, "Debug: Remote helper: Waiting...\n");
58 if (strbuf_getline(buffer
, helper
, '\n') == EOF
) {
60 fprintf(stderr
, "Debug: Remote helper quit.\n");
65 fprintf(stderr
, "Debug: Remote helper: <- %s\n", buffer
->buf
);
69 static int recvline(struct helper_data
*helper
, struct strbuf
*buffer
)
71 return recvline_fh(helper
->out
, buffer
, helper
->name
);
74 static void xchgline(struct helper_data
*helper
, struct strbuf
*buffer
)
76 sendline(helper
, buffer
);
77 recvline(helper
, buffer
);
80 static void write_constant(int fd
, const char *str
)
83 fprintf(stderr
, "Debug: Remote helper: -> %s", str
);
84 if (write_in_full(fd
, str
, strlen(str
)) != strlen(str
))
85 die_errno("Full write to remote helper failed");
88 static const char *remove_ext_force(const char *url
)
91 const char *colon
= strchr(url
, ':');
92 if (colon
&& colon
[1] == ':')
98 static void do_take_over(struct transport
*transport
)
100 struct helper_data
*data
;
101 data
= (struct helper_data
*)transport
->data
;
102 transport_take_over(transport
, data
->helper
);
107 static struct child_process
*get_helper(struct transport
*transport
)
109 struct helper_data
*data
= transport
->data
;
110 struct argv_array argv
= ARGV_ARRAY_INIT
;
111 struct strbuf buf
= STRBUF_INIT
;
112 struct child_process
*helper
;
113 const char **refspecs
= NULL
;
115 int refspec_alloc
= 0;
118 char git_dir_buf
[sizeof(GIT_DIR_ENVIRONMENT
) + PATH_MAX
+ 1];
119 const char *helper_env
[] = {
128 helper
= xcalloc(1, sizeof(*helper
));
132 argv_array_pushf(&argv
, "git-remote-%s", data
->name
);
133 argv_array_push(&argv
, transport
->remote
->name
);
134 argv_array_push(&argv
, remove_ext_force(transport
->url
));
135 helper
->argv
= argv_array_detach(&argv
, NULL
);
137 helper
->silent_exec_failure
= 1;
139 snprintf(git_dir_buf
, sizeof(git_dir_buf
), "%s=%s", GIT_DIR_ENVIRONMENT
, get_git_dir());
140 helper
->env
= helper_env
;
142 code
= start_command(helper
);
143 if (code
< 0 && errno
== ENOENT
)
144 die("Unable to find remote helper for '%s'", data
->name
);
148 data
->helper
= helper
;
149 data
->no_disconnect_req
= 0;
152 * Open the output as FILE* so strbuf_getline() can be used.
153 * Do this with duped fd because fclose() will close the fd,
154 * and stuff like taking over will require the fd to remain.
156 duped
= dup(helper
->out
);
158 die_errno("Can't dup helper output fd");
159 data
->out
= xfdopen(duped
, "r");
161 write_constant(helper
->in
, "capabilities\n");
166 recvline(data
, &buf
);
171 if (*buf
.buf
== '*') {
172 capname
= buf
.buf
+ 1;
178 fprintf(stderr
, "Debug: Got cap %s\n", capname
);
179 if (!strcmp(capname
, "fetch"))
181 else if (!strcmp(capname
, "option"))
183 else if (!strcmp(capname
, "push"))
185 else if (!strcmp(capname
, "import"))
187 else if (!strcmp(capname
, "bidi-import"))
188 data
->bidi_import
= 1;
189 else if (!strcmp(capname
, "export"))
191 else if (!strcmp(capname
, "check-connectivity"))
192 data
->check_connectivity
= 1;
193 else if (!data
->refspecs
&& starts_with(capname
, "refspec ")) {
197 refspecs
[refspec_nr
++] = xstrdup(capname
+ strlen("refspec "));
198 } else if (!strcmp(capname
, "connect")) {
200 } else if (!strcmp(capname
, "signed-tags")) {
201 data
->signed_tags
= 1;
202 } else if (starts_with(capname
, "export-marks ")) {
203 struct strbuf arg
= STRBUF_INIT
;
204 strbuf_addstr(&arg
, "--export-marks=");
205 strbuf_addstr(&arg
, capname
+ strlen("export-marks "));
206 data
->export_marks
= strbuf_detach(&arg
, NULL
);
207 } else if (starts_with(capname
, "import-marks")) {
208 struct strbuf arg
= STRBUF_INIT
;
209 strbuf_addstr(&arg
, "--import-marks=");
210 strbuf_addstr(&arg
, capname
+ strlen("import-marks "));
211 data
->import_marks
= strbuf_detach(&arg
, NULL
);
212 } else if (starts_with(capname
, "no-private-update")) {
213 data
->no_private_update
= 1;
214 } else if (mandatory
) {
215 die("Unknown mandatory capability %s. This remote "
216 "helper probably needs newer version of Git.",
222 data
->refspec_nr
= refspec_nr
;
223 data
->refspecs
= parse_fetch_refspec(refspec_nr
, refspecs
);
224 for (i
= 0; i
< refspec_nr
; i
++)
225 free((char *)refspecs
[i
]);
227 } else if (data
->import
|| data
->bidi_import
|| data
->export
) {
228 warning("This remote helper should implement refspec capability.");
230 strbuf_release(&buf
);
232 fprintf(stderr
, "Debug: Capabilities complete.\n");
236 static int disconnect_helper(struct transport
*transport
)
238 struct helper_data
*data
= transport
->data
;
243 fprintf(stderr
, "Debug: Disconnecting.\n");
244 if (!data
->no_disconnect_req
) {
246 * Ignore write errors; there's nothing we can do,
247 * since we're about to close the pipe anyway. And the
248 * most likely error is EPIPE due to the helper dying
249 * to report an error itself.
251 sigchain_push(SIGPIPE
, SIG_IGN
);
252 xwrite(data
->helper
->in
, "\n", 1);
253 sigchain_pop(SIGPIPE
);
255 close(data
->helper
->in
);
256 close(data
->helper
->out
);
258 res
= finish_command(data
->helper
);
259 argv_array_free_detached(data
->helper
->argv
);
266 static const char *unsupported_options
[] = {
267 TRANS_OPT_UPLOADPACK
,
268 TRANS_OPT_RECEIVEPACK
,
273 static const char *boolean_options
[] = {
279 static int set_helper_option(struct transport
*transport
,
280 const char *name
, const char *value
)
282 struct helper_data
*data
= transport
->data
;
283 struct strbuf buf
= STRBUF_INIT
;
284 int i
, ret
, is_bool
= 0;
286 get_helper(transport
);
291 for (i
= 0; i
< ARRAY_SIZE(unsupported_options
); i
++) {
292 if (!strcmp(name
, unsupported_options
[i
]))
296 for (i
= 0; i
< ARRAY_SIZE(boolean_options
); i
++) {
297 if (!strcmp(name
, boolean_options
[i
])) {
303 strbuf_addf(&buf
, "option %s ", name
);
305 strbuf_addstr(&buf
, value
? "true" : "false");
307 quote_c_style(value
, &buf
, NULL
, 0);
308 strbuf_addch(&buf
, '\n');
310 xchgline(data
, &buf
);
312 if (!strcmp(buf
.buf
, "ok"))
314 else if (starts_with(buf
.buf
, "error")) {
316 } else if (!strcmp(buf
.buf
, "unsupported"))
319 warning("%s unexpectedly said: '%s'", data
->name
, buf
.buf
);
322 strbuf_release(&buf
);
326 static void standard_options(struct transport
*t
)
332 set_helper_option(t
, "progress", t
->progress
? "true" : "false");
334 n
= snprintf(buf
, sizeof(buf
), "%d", v
+ 1);
335 if (n
>= sizeof(buf
))
336 die("impossibly large verbosity value");
337 set_helper_option(t
, "verbosity", buf
);
340 static int release_helper(struct transport
*transport
)
343 struct helper_data
*data
= transport
->data
;
344 free_refspec(data
->refspec_nr
, data
->refspecs
);
345 data
->refspecs
= NULL
;
346 res
= disconnect_helper(transport
);
347 free(transport
->data
);
351 static int fetch_with_fetch(struct transport
*transport
,
352 int nr_heads
, struct ref
**to_fetch
)
354 struct helper_data
*data
= transport
->data
;
356 struct strbuf buf
= STRBUF_INIT
;
358 standard_options(transport
);
359 if (data
->check_connectivity
&&
360 data
->transport_options
.check_self_contained_and_connected
)
361 set_helper_option(transport
, "check-connectivity", "true");
363 if (transport
->cloning
)
364 set_helper_option(transport
, "cloning", "true");
366 if (data
->transport_options
.update_shallow
)
367 set_helper_option(transport
, "update-shallow", "true");
369 for (i
= 0; i
< nr_heads
; i
++) {
370 const struct ref
*posn
= to_fetch
[i
];
371 if (posn
->status
& REF_STATUS_UPTODATE
)
374 strbuf_addf(&buf
, "fetch %s %s\n",
375 sha1_to_hex(posn
->old_sha1
), posn
->name
);
378 strbuf_addch(&buf
, '\n');
379 sendline(data
, &buf
);
382 recvline(data
, &buf
);
384 if (starts_with(buf
.buf
, "lock ")) {
385 const char *name
= buf
.buf
+ 5;
386 if (transport
->pack_lockfile
)
387 warning("%s also locked %s", data
->name
, name
);
389 transport
->pack_lockfile
= xstrdup(name
);
391 else if (data
->check_connectivity
&&
392 data
->transport_options
.check_self_contained_and_connected
&&
393 !strcmp(buf
.buf
, "connectivity-ok"))
394 data
->transport_options
.self_contained_and_connected
= 1;
398 warning("%s unexpectedly said: '%s'", data
->name
, buf
.buf
);
400 strbuf_release(&buf
);
404 static int get_importer(struct transport
*transport
, struct child_process
*fastimport
)
406 struct child_process
*helper
= get_helper(transport
);
407 struct helper_data
*data
= transport
->data
;
408 struct argv_array argv
= ARGV_ARRAY_INIT
;
409 int cat_blob_fd
, code
;
410 memset(fastimport
, 0, sizeof(*fastimport
));
411 fastimport
->in
= helper
->out
;
412 argv_array_push(&argv
, "fast-import");
413 argv_array_push(&argv
, debug
? "--stats" : "--quiet");
415 if (data
->bidi_import
) {
416 cat_blob_fd
= xdup(helper
->in
);
417 argv_array_pushf(&argv
, "--cat-blob-fd=%d", cat_blob_fd
);
419 fastimport
->argv
= argv
.argv
;
420 fastimport
->git_cmd
= 1;
422 code
= start_command(fastimport
);
426 static int get_exporter(struct transport
*transport
,
427 struct child_process
*fastexport
,
428 struct string_list
*revlist_args
)
430 struct helper_data
*data
= transport
->data
;
431 struct child_process
*helper
= get_helper(transport
);
433 memset(fastexport
, 0, sizeof(*fastexport
));
435 /* we need to duplicate helper->in because we want to use it after
436 * fastexport is done with it. */
437 fastexport
->out
= dup(helper
->in
);
438 fastexport
->argv
= xcalloc(6 + revlist_args
->nr
, sizeof(*fastexport
->argv
));
439 fastexport
->argv
[argc
++] = "fast-export";
440 fastexport
->argv
[argc
++] = "--use-done-feature";
441 fastexport
->argv
[argc
++] = data
->signed_tags
?
442 "--signed-tags=verbatim" : "--signed-tags=warn-strip";
443 if (data
->export_marks
)
444 fastexport
->argv
[argc
++] = data
->export_marks
;
445 if (data
->import_marks
)
446 fastexport
->argv
[argc
++] = data
->import_marks
;
448 for (i
= 0; i
< revlist_args
->nr
; i
++)
449 fastexport
->argv
[argc
++] = revlist_args
->items
[i
].string
;
451 fastexport
->git_cmd
= 1;
452 return start_command(fastexport
);
455 static int fetch_with_import(struct transport
*transport
,
456 int nr_heads
, struct ref
**to_fetch
)
458 struct child_process fastimport
;
459 struct helper_data
*data
= transport
->data
;
462 struct strbuf buf
= STRBUF_INIT
;
464 get_helper(transport
);
466 if (get_importer(transport
, &fastimport
))
467 die("Couldn't run fast-import");
469 for (i
= 0; i
< nr_heads
; i
++) {
471 if (posn
->status
& REF_STATUS_UPTODATE
)
474 strbuf_addf(&buf
, "import %s\n", posn
->name
);
475 sendline(data
, &buf
);
479 write_constant(data
->helper
->in
, "\n");
481 * remote-helpers that advertise the bidi-import capability are required to
482 * buffer the complete batch of import commands until this newline before
483 * sending data to fast-import.
484 * These helpers read back data from fast-import on their stdin, which could
485 * be mixed with import commands, otherwise.
488 if (finish_command(&fastimport
))
489 die("Error while running fast-import");
490 argv_array_free_detached(fastimport
.argv
);
493 * The fast-import stream of a remote helper that advertises
494 * the "refspec" capability writes to the refs named after the
495 * right hand side of the first refspec matching each ref we
498 * (If no "refspec" capability was specified, for historical
499 * reasons we default to the equivalent of *:*.)
501 * Store the result in to_fetch[i].old_sha1. Callers such
502 * as "git fetch" can use the value to write feedback to the
503 * terminal, populate FETCH_HEAD, and determine what new value
504 * should be written to peer_ref if the update is a
505 * fast-forward or this is a forced update.
507 for (i
= 0; i
< nr_heads
; i
++) {
510 if (posn
->status
& REF_STATUS_UPTODATE
)
513 private = apply_refspecs(data
->refspecs
, data
->refspec_nr
, posn
->name
);
515 private = xstrdup(posn
->name
);
517 read_ref(private, posn
->old_sha1
);
521 strbuf_release(&buf
);
525 static int process_connect_service(struct transport
*transport
,
526 const char *name
, const char *exec
)
528 struct helper_data
*data
= transport
->data
;
529 struct strbuf cmdbuf
= STRBUF_INIT
;
530 struct child_process
*helper
;
531 int r
, duped
, ret
= 0;
534 helper
= get_helper(transport
);
537 * Yes, dup the pipe another time, as we need unbuffered version
538 * of input pipe as FILE*. fclose() closes the underlying fd and
539 * stream buffering only can be changed before first I/O operation
542 duped
= dup(helper
->out
);
544 die_errno("Can't dup helper output fd");
545 input
= xfdopen(duped
, "r");
546 setvbuf(input
, NULL
, _IONBF
, 0);
549 * Handle --upload-pack and friends. This is fire and forget...
550 * just warn if it fails.
552 if (strcmp(name
, exec
)) {
553 r
= set_helper_option(transport
, "servpath", exec
);
555 warning("Setting remote service path not supported by protocol.");
557 warning("Invalid remote service path.");
561 strbuf_addf(&cmdbuf
, "connect %s\n", name
);
565 sendline(data
, &cmdbuf
);
566 recvline_fh(input
, &cmdbuf
, name
);
567 if (!strcmp(cmdbuf
.buf
, "")) {
568 data
->no_disconnect_req
= 1;
570 fprintf(stderr
, "Debug: Smart transport connection "
573 } else if (!strcmp(cmdbuf
.buf
, "fallback")) {
575 fprintf(stderr
, "Debug: Falling back to dumb "
578 die("Unknown response to connect: %s",
586 static int process_connect(struct transport
*transport
,
589 struct helper_data
*data
= transport
->data
;
593 name
= for_push
? "git-receive-pack" : "git-upload-pack";
595 exec
= data
->transport_options
.receivepack
;
597 exec
= data
->transport_options
.uploadpack
;
599 return process_connect_service(transport
, name
, exec
);
602 static int connect_helper(struct transport
*transport
, const char *name
,
603 const char *exec
, int fd
[2])
605 struct helper_data
*data
= transport
->data
;
607 /* Get_helper so connect is inited. */
608 get_helper(transport
);
610 die("Operation not supported by protocol.");
612 if (!process_connect_service(transport
, name
, exec
))
613 die("Can't connect to subservice %s.", name
);
615 fd
[0] = data
->helper
->out
;
616 fd
[1] = data
->helper
->in
;
620 static int fetch(struct transport
*transport
,
621 int nr_heads
, struct ref
**to_fetch
)
623 struct helper_data
*data
= transport
->data
;
626 if (process_connect(transport
, 0)) {
627 do_take_over(transport
);
628 return transport
->fetch(transport
, nr_heads
, to_fetch
);
632 for (i
= 0; i
< nr_heads
; i
++)
633 if (!(to_fetch
[i
]->status
& REF_STATUS_UPTODATE
))
640 return fetch_with_fetch(transport
, nr_heads
, to_fetch
);
643 return fetch_with_import(transport
, nr_heads
, to_fetch
);
648 static int push_update_ref_status(struct strbuf
*buf
,
650 struct ref
*remote_refs
)
653 int status
, forced
= 0;
655 if (starts_with(buf
->buf
, "ok ")) {
656 status
= REF_STATUS_OK
;
657 refname
= buf
->buf
+ 3;
658 } else if (starts_with(buf
->buf
, "error ")) {
659 status
= REF_STATUS_REMOTE_REJECT
;
660 refname
= buf
->buf
+ 6;
662 die("expected ok/error, helper said '%s'", buf
->buf
);
664 msg
= strchr(refname
, ' ');
666 struct strbuf msg_buf
= STRBUF_INIT
;
670 if (!unquote_c_style(&msg_buf
, msg
, &end
))
671 msg
= strbuf_detach(&msg_buf
, NULL
);
674 strbuf_release(&msg_buf
);
676 if (!strcmp(msg
, "no match")) {
677 status
= REF_STATUS_NONE
;
681 else if (!strcmp(msg
, "up to date")) {
682 status
= REF_STATUS_UPTODATE
;
686 else if (!strcmp(msg
, "non-fast forward")) {
687 status
= REF_STATUS_REJECT_NONFASTFORWARD
;
691 else if (!strcmp(msg
, "already exists")) {
692 status
= REF_STATUS_REJECT_ALREADY_EXISTS
;
696 else if (!strcmp(msg
, "fetch first")) {
697 status
= REF_STATUS_REJECT_FETCH_FIRST
;
701 else if (!strcmp(msg
, "needs force")) {
702 status
= REF_STATUS_REJECT_NEEDS_FORCE
;
706 else if (!strcmp(msg
, "stale info")) {
707 status
= REF_STATUS_REJECT_STALE
;
711 else if (!strcmp(msg
, "forced update")) {
719 *ref
= find_ref_by_name(*ref
, refname
);
721 *ref
= find_ref_by_name(remote_refs
, refname
);
723 warning("helper reported unexpected status of %s", refname
);
727 if ((*ref
)->status
!= REF_STATUS_NONE
) {
729 * Earlier, the ref was marked not to be pushed, so ignore the ref
730 * status reported by the remote helper if the latter is 'no match'.
732 if (status
== REF_STATUS_NONE
)
736 (*ref
)->status
= status
;
737 (*ref
)->forced_update
|= forced
;
738 (*ref
)->remote_status
= msg
;
739 return !(status
== REF_STATUS_OK
);
742 static void push_update_refs_status(struct helper_data
*data
,
743 struct ref
*remote_refs
,
746 struct strbuf buf
= STRBUF_INIT
;
747 struct ref
*ref
= remote_refs
;
751 recvline(data
, &buf
);
755 if (push_update_ref_status(&buf
, &ref
, remote_refs
))
758 if (flags
& TRANSPORT_PUSH_DRY_RUN
|| !data
->refspecs
|| data
->no_private_update
)
761 /* propagate back the update to the remote namespace */
762 private = apply_refspecs(data
->refspecs
, data
->refspec_nr
, ref
->name
);
765 update_ref("update by helper", private, ref
->new_sha1
, NULL
, 0, 0);
768 strbuf_release(&buf
);
771 static int push_refs_with_push(struct transport
*transport
,
772 struct ref
*remote_refs
, int flags
)
774 int force_all
= flags
& TRANSPORT_PUSH_FORCE
;
775 int mirror
= flags
& TRANSPORT_PUSH_MIRROR
;
776 struct helper_data
*data
= transport
->data
;
777 struct strbuf buf
= STRBUF_INIT
;
779 struct string_list cas_options
= STRING_LIST_INIT_DUP
;
780 struct string_list_item
*cas_option
;
782 get_helper(transport
);
786 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
787 if (!ref
->peer_ref
&& !mirror
)
790 /* Check for statuses set by set_ref_status_for_push() */
791 switch (ref
->status
) {
792 case REF_STATUS_REJECT_NONFASTFORWARD
:
793 case REF_STATUS_REJECT_STALE
:
794 case REF_STATUS_REJECT_ALREADY_EXISTS
:
795 case REF_STATUS_UPTODATE
:
804 strbuf_addstr(&buf
, "push ");
805 if (!ref
->deletion
) {
807 strbuf_addch(&buf
, '+');
809 strbuf_addstr(&buf
, ref
->peer_ref
->name
);
811 strbuf_addstr(&buf
, sha1_to_hex(ref
->new_sha1
));
813 strbuf_addch(&buf
, ':');
814 strbuf_addstr(&buf
, ref
->name
);
815 strbuf_addch(&buf
, '\n');
818 * The "--force-with-lease" options without explicit
819 * values to expect have already been expanded into
820 * the ref->old_sha1_expect[] field; we can ignore
821 * transport->smart_options->cas altogether and instead
822 * can enumerate them from the refs.
824 if (ref
->expect_old_sha1
) {
825 struct strbuf cas
= STRBUF_INIT
;
826 strbuf_addf(&cas
, "%s:%s",
827 ref
->name
, sha1_to_hex(ref
->old_sha1_expect
));
828 string_list_append(&cas_options
, strbuf_detach(&cas
, NULL
));
832 string_list_clear(&cas_options
, 0);
836 standard_options(transport
);
837 for_each_string_list_item(cas_option
, &cas_options
)
838 set_helper_option(transport
, "cas", cas_option
->string
);
840 if (flags
& TRANSPORT_PUSH_DRY_RUN
) {
841 if (set_helper_option(transport
, "dry-run", "true") != 0)
842 die("helper %s does not support dry-run", data
->name
);
845 strbuf_addch(&buf
, '\n');
846 sendline(data
, &buf
);
847 strbuf_release(&buf
);
849 push_update_refs_status(data
, remote_refs
, flags
);
853 static int push_refs_with_export(struct transport
*transport
,
854 struct ref
*remote_refs
, int flags
)
857 struct child_process
*helper
, exporter
;
858 struct helper_data
*data
= transport
->data
;
859 struct string_list revlist_args
= STRING_LIST_INIT_NODUP
;
860 struct strbuf buf
= STRBUF_INIT
;
863 die("remote-helper doesn't support push; refspec needed");
865 if (flags
& TRANSPORT_PUSH_DRY_RUN
) {
866 if (set_helper_option(transport
, "dry-run", "true") != 0)
867 die("helper %s does not support dry-run", data
->name
);
870 if (flags
& TRANSPORT_PUSH_FORCE
) {
871 if (set_helper_option(transport
, "force", "true") != 0)
872 warning("helper %s does not support 'force'", data
->name
);
875 helper
= get_helper(transport
);
877 write_constant(helper
->in
, "export\n");
881 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
883 unsigned char sha1
[20];
886 die("remote-helpers do not support ref deletion");
888 private = apply_refspecs(data
->refspecs
, data
->refspec_nr
, ref
->name
);
889 if (private && !get_sha1(private, sha1
)) {
890 strbuf_addf(&buf
, "^%s", private);
891 string_list_append(&revlist_args
, strbuf_detach(&buf
, NULL
));
892 hashcpy(ref
->old_sha1
, sha1
);
897 if (strcmp(ref
->peer_ref
->name
, ref
->name
))
898 die("remote-helpers do not support old:new syntax");
899 string_list_append(&revlist_args
, ref
->peer_ref
->name
);
903 if (get_exporter(transport
, &exporter
, &revlist_args
))
904 die("Couldn't run fast-export");
906 if (finish_command(&exporter
))
907 die("Error while running fast-export");
908 push_update_refs_status(data
, remote_refs
, flags
);
912 static int push_refs(struct transport
*transport
,
913 struct ref
*remote_refs
, int flags
)
915 struct helper_data
*data
= transport
->data
;
917 if (process_connect(transport
, 1)) {
918 do_take_over(transport
);
919 return transport
->push_refs(transport
, remote_refs
, flags
);
923 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
924 "Perhaps you should specify a branch such as 'master'.\n");
929 return push_refs_with_push(transport
, remote_refs
, flags
);
932 return push_refs_with_export(transport
, remote_refs
, flags
);
938 static int has_attribute(const char *attrs
, const char *attr
) {
945 const char *space
= strchrnul(attrs
, ' ');
946 if (len
== space
- attrs
&& !strncmp(attrs
, attr
, len
))
954 static struct ref
*get_refs_list(struct transport
*transport
, int for_push
)
956 struct helper_data
*data
= transport
->data
;
957 struct child_process
*helper
;
958 struct ref
*ret
= NULL
;
959 struct ref
**tail
= &ret
;
961 struct strbuf buf
= STRBUF_INIT
;
963 helper
= get_helper(transport
);
965 if (process_connect(transport
, for_push
)) {
966 do_take_over(transport
);
967 return transport
->get_refs_list(transport
, for_push
);
970 if (data
->push
&& for_push
)
971 write_str_in_full(helper
->in
, "list for-push\n");
973 write_str_in_full(helper
->in
, "list\n");
977 recvline(data
, &buf
);
982 eov
= strchr(buf
.buf
, ' ');
984 die("Malformed response in ref list: %s", buf
.buf
);
985 eon
= strchr(eov
+ 1, ' ');
989 *tail
= alloc_ref(eov
+ 1);
990 if (buf
.buf
[0] == '@')
991 (*tail
)->symref
= xstrdup(buf
.buf
+ 1);
992 else if (buf
.buf
[0] != '?')
993 get_sha1_hex(buf
.buf
, (*tail
)->old_sha1
);
995 if (has_attribute(eon
+ 1, "unchanged")) {
996 (*tail
)->status
|= REF_STATUS_UPTODATE
;
997 read_ref((*tail
)->name
, (*tail
)->old_sha1
);
1000 tail
= &((*tail
)->next
);
1003 fprintf(stderr
, "Debug: Read ref listing.\n");
1004 strbuf_release(&buf
);
1006 for (posn
= ret
; posn
; posn
= posn
->next
)
1007 resolve_remote_symref(posn
, ret
);
1012 int transport_helper_init(struct transport
*transport
, const char *name
)
1014 struct helper_data
*data
= xcalloc(sizeof(*data
), 1);
1017 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
1020 transport
->data
= data
;
1021 transport
->set_option
= set_helper_option
;
1022 transport
->get_refs_list
= get_refs_list
;
1023 transport
->fetch
= fetch
;
1024 transport
->push_refs
= push_refs
;
1025 transport
->disconnect
= release_helper
;
1026 transport
->connect
= connect_helper
;
1027 transport
->smart_options
= &(data
->transport_options
);
1032 * Linux pipes can buffer 65536 bytes at once (and most platforms can
1033 * buffer less), so attempt reads and writes with up to that size.
1035 #define BUFFERSIZE 65536
1036 /* This should be enough to hold debugging message. */
1037 #define PBUFFERSIZE 8192
1039 /* Print bidirectional transfer loop debug message. */
1040 __attribute__((format (printf
, 1, 2)))
1041 static void transfer_debug(const char *fmt
, ...)
1044 char msgbuf
[PBUFFERSIZE
];
1045 static int debug_enabled
= -1;
1047 if (debug_enabled
< 0)
1048 debug_enabled
= getenv("GIT_TRANSLOOP_DEBUG") ? 1 : 0;
1052 va_start(args
, fmt
);
1053 vsnprintf(msgbuf
, PBUFFERSIZE
, fmt
, args
);
1055 fprintf(stderr
, "Transfer loop debugging: %s\n", msgbuf
);
1058 /* Stream state: More data may be coming in this direction. */
1059 #define SSTATE_TRANSFERING 0
1061 * Stream state: No more data coming in this direction, flushing rest of
1064 #define SSTATE_FLUSHING 1
1065 /* Stream state: Transfer in this direction finished. */
1066 #define SSTATE_FINISHED 2
1068 #define STATE_NEEDS_READING(state) ((state) <= SSTATE_TRANSFERING)
1069 #define STATE_NEEDS_WRITING(state) ((state) <= SSTATE_FLUSHING)
1070 #define STATE_NEEDS_CLOSING(state) ((state) == SSTATE_FLUSHING)
1072 /* Unidirectional transfer. */
1073 struct unidirectional_transfer
{
1078 /* Is source socket? */
1080 /* Is destination socket? */
1082 /* Transfer state (TRANSFERRING/FLUSHING/FINISHED) */
1085 char buf
[BUFFERSIZE
];
1088 /* Name of source. */
1089 const char *src_name
;
1090 /* Name of destination. */
1091 const char *dest_name
;
1094 /* Closes the target (for writing) if transfer has finished. */
1095 static void udt_close_if_finished(struct unidirectional_transfer
*t
)
1097 if (STATE_NEEDS_CLOSING(t
->state
) && !t
->bufuse
) {
1098 t
->state
= SSTATE_FINISHED
;
1099 if (t
->dest_is_sock
)
1100 shutdown(t
->dest
, SHUT_WR
);
1103 transfer_debug("Closed %s.", t
->dest_name
);
1108 * Tries to read read data from source into buffer. If buffer is full,
1109 * no data is read. Returns 0 on success, -1 on error.
1111 static int udt_do_read(struct unidirectional_transfer
*t
)
1115 if (t
->bufuse
== BUFFERSIZE
)
1116 return 0; /* No space for more. */
1118 transfer_debug("%s is readable", t
->src_name
);
1119 bytes
= read(t
->src
, t
->buf
+ t
->bufuse
, BUFFERSIZE
- t
->bufuse
);
1120 if (bytes
< 0 && errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
&&
1122 error("read(%s) failed: %s", t
->src_name
, strerror(errno
));
1124 } else if (bytes
== 0) {
1125 transfer_debug("%s EOF (with %i bytes in buffer)",
1126 t
->src_name
, (int)t
->bufuse
);
1127 t
->state
= SSTATE_FLUSHING
;
1128 } else if (bytes
> 0) {
1130 transfer_debug("Read %i bytes from %s (buffer now at %i)",
1131 (int)bytes
, t
->src_name
, (int)t
->bufuse
);
1136 /* Tries to write data from buffer into destination. If buffer is empty,
1137 * no data is written. Returns 0 on success, -1 on error.
1139 static int udt_do_write(struct unidirectional_transfer
*t
)
1144 return 0; /* Nothing to write. */
1146 transfer_debug("%s is writable", t
->dest_name
);
1147 bytes
= xwrite(t
->dest
, t
->buf
, t
->bufuse
);
1148 if (bytes
< 0 && errno
!= EWOULDBLOCK
) {
1149 error("write(%s) failed: %s", t
->dest_name
, strerror(errno
));
1151 } else if (bytes
> 0) {
1154 memmove(t
->buf
, t
->buf
+ bytes
, t
->bufuse
);
1155 transfer_debug("Wrote %i bytes to %s (buffer now at %i)",
1156 (int)bytes
, t
->dest_name
, (int)t
->bufuse
);
1162 /* State of bidirectional transfer loop. */
1163 struct bidirectional_transfer_state
{
1164 /* Direction from program to git. */
1165 struct unidirectional_transfer ptg
;
1166 /* Direction from git to program. */
1167 struct unidirectional_transfer gtp
;
1170 static void *udt_copy_task_routine(void *udt
)
1172 struct unidirectional_transfer
*t
= (struct unidirectional_transfer
*)udt
;
1173 while (t
->state
!= SSTATE_FINISHED
) {
1174 if (STATE_NEEDS_READING(t
->state
))
1177 if (STATE_NEEDS_WRITING(t
->state
))
1178 if (udt_do_write(t
))
1180 if (STATE_NEEDS_CLOSING(t
->state
))
1181 udt_close_if_finished(t
);
1183 return udt
; /* Just some non-NULL value. */
1189 * Join thread, with appropriate errors on failure. Name is name for the
1190 * thread (for error messages). Returns 0 on success, 1 on failure.
1192 static int tloop_join(pthread_t thread
, const char *name
)
1196 err
= pthread_join(thread
, &tret
);
1198 error("%s thread failed", name
);
1202 error("%s thread failed to join: %s", name
, strerror(err
));
1209 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1212 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1214 pthread_t gtp_thread
;
1215 pthread_t ptg_thread
;
1218 err
= pthread_create(>p_thread
, NULL
, udt_copy_task_routine
,
1221 die("Can't start thread for copying data: %s", strerror(err
));
1222 err
= pthread_create(&ptg_thread
, NULL
, udt_copy_task_routine
,
1225 die("Can't start thread for copying data: %s", strerror(err
));
1227 ret
|= tloop_join(gtp_thread
, "Git to program copy");
1228 ret
|= tloop_join(ptg_thread
, "Program to git copy");
1233 /* Close the source and target (for writing) for transfer. */
1234 static void udt_kill_transfer(struct unidirectional_transfer
*t
)
1236 t
->state
= SSTATE_FINISHED
;
1238 * Socket read end left open isn't a disaster if nobody
1239 * attempts to read from it (mingw compat headers do not
1242 * We can't fully close the socket since otherwise gtp
1243 * task would first close the socket it sends data to
1244 * while closing the ptg file descriptors.
1246 if (!t
->src_is_sock
)
1248 if (t
->dest_is_sock
)
1249 shutdown(t
->dest
, SHUT_WR
);
1255 * Join process, with appropriate errors on failure. Name is name for the
1256 * process (for error messages). Returns 0 on success, 1 on failure.
1258 static int tloop_join(pid_t pid
, const char *name
)
1261 if (waitpid(pid
, &tret
, 0) < 0) {
1262 error("%s process failed to wait: %s", name
, strerror(errno
));
1265 if (!WIFEXITED(tret
) || WEXITSTATUS(tret
)) {
1266 error("%s process failed", name
);
1273 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1276 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1281 /* Fork thread #1: git to program. */
1284 die_errno("Can't start thread for copying data");
1285 else if (pid1
== 0) {
1286 udt_kill_transfer(&s
->ptg
);
1287 exit(udt_copy_task_routine(&s
->gtp
) ? 0 : 1);
1290 /* Fork thread #2: program to git. */
1293 die_errno("Can't start thread for copying data");
1294 else if (pid2
== 0) {
1295 udt_kill_transfer(&s
->gtp
);
1296 exit(udt_copy_task_routine(&s
->ptg
) ? 0 : 1);
1300 * Close both streams in parent as to not interfere with
1301 * end of file detection and wait for both tasks to finish.
1303 udt_kill_transfer(&s
->gtp
);
1304 udt_kill_transfer(&s
->ptg
);
1305 ret
|= tloop_join(pid1
, "Git to program copy");
1306 ret
|= tloop_join(pid2
, "Program to git copy");
1312 * Copies data from stdin to output and from input to stdout simultaneously.
1313 * Additionally filtering through given filter. If filter is NULL, uses
1316 int bidirectional_transfer_loop(int input
, int output
)
1318 struct bidirectional_transfer_state state
;
1320 /* Fill the state fields. */
1321 state
.ptg
.src
= input
;
1323 state
.ptg
.src_is_sock
= (input
== output
);
1324 state
.ptg
.dest_is_sock
= 0;
1325 state
.ptg
.state
= SSTATE_TRANSFERING
;
1326 state
.ptg
.bufuse
= 0;
1327 state
.ptg
.src_name
= "remote input";
1328 state
.ptg
.dest_name
= "stdout";
1331 state
.gtp
.dest
= output
;
1332 state
.gtp
.src_is_sock
= 0;
1333 state
.gtp
.dest_is_sock
= (input
== output
);
1334 state
.gtp
.state
= SSTATE_TRANSFERING
;
1335 state
.gtp
.bufuse
= 0;
1336 state
.gtp
.src_name
= "stdin";
1337 state
.gtp
.dest_name
= "remote output";
1339 return tloop_spawnwait_tasks(&state
);