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
&& !prefixcmp(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 (!prefixcmp(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 (!prefixcmp(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 (!prefixcmp(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
,
272 static const char *boolean_options
[] = {
278 static int set_helper_option(struct transport
*transport
,
279 const char *name
, const char *value
)
281 struct helper_data
*data
= transport
->data
;
282 struct strbuf buf
= STRBUF_INIT
;
283 int i
, ret
, is_bool
= 0;
285 get_helper(transport
);
290 for (i
= 0; i
< ARRAY_SIZE(unsupported_options
); i
++) {
291 if (!strcmp(name
, unsupported_options
[i
]))
295 for (i
= 0; i
< ARRAY_SIZE(boolean_options
); i
++) {
296 if (!strcmp(name
, boolean_options
[i
])) {
302 strbuf_addf(&buf
, "option %s ", name
);
304 strbuf_addstr(&buf
, value
? "true" : "false");
306 quote_c_style(value
, &buf
, NULL
, 0);
307 strbuf_addch(&buf
, '\n');
309 xchgline(data
, &buf
);
311 if (!strcmp(buf
.buf
, "ok"))
313 else if (!prefixcmp(buf
.buf
, "error")) {
315 } else if (!strcmp(buf
.buf
, "unsupported"))
318 warning("%s unexpectedly said: '%s'", data
->name
, buf
.buf
);
321 strbuf_release(&buf
);
325 static void standard_options(struct transport
*t
)
331 set_helper_option(t
, "progress", t
->progress
? "true" : "false");
333 n
= snprintf(buf
, sizeof(buf
), "%d", v
+ 1);
334 if (n
>= sizeof(buf
))
335 die("impossibly large verbosity value");
336 set_helper_option(t
, "verbosity", buf
);
339 static int release_helper(struct transport
*transport
)
342 struct helper_data
*data
= transport
->data
;
343 free_refspec(data
->refspec_nr
, data
->refspecs
);
344 data
->refspecs
= NULL
;
345 res
= disconnect_helper(transport
);
346 free(transport
->data
);
350 static int fetch_with_fetch(struct transport
*transport
,
351 int nr_heads
, struct ref
**to_fetch
)
353 struct helper_data
*data
= transport
->data
;
355 struct strbuf buf
= STRBUF_INIT
;
357 standard_options(transport
);
358 if (data
->check_connectivity
&&
359 data
->transport_options
.check_self_contained_and_connected
)
360 set_helper_option(transport
, "check-connectivity", "true");
362 for (i
= 0; i
< nr_heads
; i
++) {
363 const struct ref
*posn
= to_fetch
[i
];
364 if (posn
->status
& REF_STATUS_UPTODATE
)
367 strbuf_addf(&buf
, "fetch %s %s\n",
368 sha1_to_hex(posn
->old_sha1
), posn
->name
);
371 strbuf_addch(&buf
, '\n');
372 sendline(data
, &buf
);
375 recvline(data
, &buf
);
377 if (!prefixcmp(buf
.buf
, "lock ")) {
378 const char *name
= buf
.buf
+ 5;
379 if (transport
->pack_lockfile
)
380 warning("%s also locked %s", data
->name
, name
);
382 transport
->pack_lockfile
= xstrdup(name
);
384 else if (data
->check_connectivity
&&
385 data
->transport_options
.check_self_contained_and_connected
&&
386 !strcmp(buf
.buf
, "connectivity-ok"))
387 data
->transport_options
.self_contained_and_connected
= 1;
391 warning("%s unexpectedly said: '%s'", data
->name
, buf
.buf
);
393 strbuf_release(&buf
);
397 static int get_importer(struct transport
*transport
, struct child_process
*fastimport
)
399 struct child_process
*helper
= get_helper(transport
);
400 struct helper_data
*data
= transport
->data
;
401 struct argv_array argv
= ARGV_ARRAY_INIT
;
402 int cat_blob_fd
, code
;
403 memset(fastimport
, 0, sizeof(*fastimport
));
404 fastimport
->in
= helper
->out
;
405 argv_array_push(&argv
, "fast-import");
406 argv_array_push(&argv
, debug
? "--stats" : "--quiet");
408 if (data
->bidi_import
) {
409 cat_blob_fd
= xdup(helper
->in
);
410 argv_array_pushf(&argv
, "--cat-blob-fd=%d", cat_blob_fd
);
412 fastimport
->argv
= argv
.argv
;
413 fastimport
->git_cmd
= 1;
415 code
= start_command(fastimport
);
419 static int get_exporter(struct transport
*transport
,
420 struct child_process
*fastexport
,
421 struct string_list
*revlist_args
)
423 struct helper_data
*data
= transport
->data
;
424 struct child_process
*helper
= get_helper(transport
);
426 memset(fastexport
, 0, sizeof(*fastexport
));
428 /* we need to duplicate helper->in because we want to use it after
429 * fastexport is done with it. */
430 fastexport
->out
= dup(helper
->in
);
431 fastexport
->argv
= xcalloc(6 + revlist_args
->nr
, sizeof(*fastexport
->argv
));
432 fastexport
->argv
[argc
++] = "fast-export";
433 fastexport
->argv
[argc
++] = "--use-done-feature";
434 fastexport
->argv
[argc
++] = data
->signed_tags
?
435 "--signed-tags=verbatim" : "--signed-tags=warn-strip";
436 if (data
->export_marks
)
437 fastexport
->argv
[argc
++] = data
->export_marks
;
438 if (data
->import_marks
)
439 fastexport
->argv
[argc
++] = data
->import_marks
;
441 for (i
= 0; i
< revlist_args
->nr
; i
++)
442 fastexport
->argv
[argc
++] = revlist_args
->items
[i
].string
;
444 fastexport
->git_cmd
= 1;
445 return start_command(fastexport
);
448 static int fetch_with_import(struct transport
*transport
,
449 int nr_heads
, struct ref
**to_fetch
)
451 struct child_process fastimport
;
452 struct helper_data
*data
= transport
->data
;
455 struct strbuf buf
= STRBUF_INIT
;
457 get_helper(transport
);
459 if (get_importer(transport
, &fastimport
))
460 die("Couldn't run fast-import");
462 for (i
= 0; i
< nr_heads
; i
++) {
464 if (posn
->status
& REF_STATUS_UPTODATE
)
467 strbuf_addf(&buf
, "import %s\n", posn
->name
);
468 sendline(data
, &buf
);
472 write_constant(data
->helper
->in
, "\n");
474 * remote-helpers that advertise the bidi-import capability are required to
475 * buffer the complete batch of import commands until this newline before
476 * sending data to fast-import.
477 * These helpers read back data from fast-import on their stdin, which could
478 * be mixed with import commands, otherwise.
481 if (finish_command(&fastimport
))
482 die("Error while running fast-import");
483 argv_array_free_detached(fastimport
.argv
);
486 * The fast-import stream of a remote helper that advertises
487 * the "refspec" capability writes to the refs named after the
488 * right hand side of the first refspec matching each ref we
491 * (If no "refspec" capability was specified, for historical
492 * reasons we default to the equivalent of *:*.)
494 * Store the result in to_fetch[i].old_sha1. Callers such
495 * as "git fetch" can use the value to write feedback to the
496 * terminal, populate FETCH_HEAD, and determine what new value
497 * should be written to peer_ref if the update is a
498 * fast-forward or this is a forced update.
500 for (i
= 0; i
< nr_heads
; i
++) {
503 if (posn
->status
& REF_STATUS_UPTODATE
)
506 private = apply_refspecs(data
->refspecs
, data
->refspec_nr
, posn
->name
);
508 private = xstrdup(posn
->name
);
510 read_ref(private, posn
->old_sha1
);
514 strbuf_release(&buf
);
518 static int process_connect_service(struct transport
*transport
,
519 const char *name
, const char *exec
)
521 struct helper_data
*data
= transport
->data
;
522 struct strbuf cmdbuf
= STRBUF_INIT
;
523 struct child_process
*helper
;
524 int r
, duped
, ret
= 0;
527 helper
= get_helper(transport
);
530 * Yes, dup the pipe another time, as we need unbuffered version
531 * of input pipe as FILE*. fclose() closes the underlying fd and
532 * stream buffering only can be changed before first I/O operation
535 duped
= dup(helper
->out
);
537 die_errno("Can't dup helper output fd");
538 input
= xfdopen(duped
, "r");
539 setvbuf(input
, NULL
, _IONBF
, 0);
542 * Handle --upload-pack and friends. This is fire and forget...
543 * just warn if it fails.
545 if (strcmp(name
, exec
)) {
546 r
= set_helper_option(transport
, "servpath", exec
);
548 warning("Setting remote service path not supported by protocol.");
550 warning("Invalid remote service path.");
554 strbuf_addf(&cmdbuf
, "connect %s\n", name
);
558 sendline(data
, &cmdbuf
);
559 recvline_fh(input
, &cmdbuf
, name
);
560 if (!strcmp(cmdbuf
.buf
, "")) {
561 data
->no_disconnect_req
= 1;
563 fprintf(stderr
, "Debug: Smart transport connection "
566 } else if (!strcmp(cmdbuf
.buf
, "fallback")) {
568 fprintf(stderr
, "Debug: Falling back to dumb "
571 die("Unknown response to connect: %s",
579 static int process_connect(struct transport
*transport
,
582 struct helper_data
*data
= transport
->data
;
586 name
= for_push
? "git-receive-pack" : "git-upload-pack";
588 exec
= data
->transport_options
.receivepack
;
590 exec
= data
->transport_options
.uploadpack
;
592 return process_connect_service(transport
, name
, exec
);
595 static int connect_helper(struct transport
*transport
, const char *name
,
596 const char *exec
, int fd
[2])
598 struct helper_data
*data
= transport
->data
;
600 /* Get_helper so connect is inited. */
601 get_helper(transport
);
603 die("Operation not supported by protocol.");
605 if (!process_connect_service(transport
, name
, exec
))
606 die("Can't connect to subservice %s.", name
);
608 fd
[0] = data
->helper
->out
;
609 fd
[1] = data
->helper
->in
;
613 static int fetch(struct transport
*transport
,
614 int nr_heads
, struct ref
**to_fetch
)
616 struct helper_data
*data
= transport
->data
;
619 if (process_connect(transport
, 0)) {
620 do_take_over(transport
);
621 return transport
->fetch(transport
, nr_heads
, to_fetch
);
625 for (i
= 0; i
< nr_heads
; i
++)
626 if (!(to_fetch
[i
]->status
& REF_STATUS_UPTODATE
))
633 return fetch_with_fetch(transport
, nr_heads
, to_fetch
);
636 return fetch_with_import(transport
, nr_heads
, to_fetch
);
641 static int push_update_ref_status(struct strbuf
*buf
,
643 struct ref
*remote_refs
)
648 if (!prefixcmp(buf
->buf
, "ok ")) {
649 status
= REF_STATUS_OK
;
650 refname
= buf
->buf
+ 3;
651 } else if (!prefixcmp(buf
->buf
, "error ")) {
652 status
= REF_STATUS_REMOTE_REJECT
;
653 refname
= buf
->buf
+ 6;
655 die("expected ok/error, helper said '%s'", buf
->buf
);
657 msg
= strchr(refname
, ' ');
659 struct strbuf msg_buf
= STRBUF_INIT
;
663 if (!unquote_c_style(&msg_buf
, msg
, &end
))
664 msg
= strbuf_detach(&msg_buf
, NULL
);
667 strbuf_release(&msg_buf
);
669 if (!strcmp(msg
, "no match")) {
670 status
= REF_STATUS_NONE
;
674 else if (!strcmp(msg
, "up to date")) {
675 status
= REF_STATUS_UPTODATE
;
679 else if (!strcmp(msg
, "non-fast forward")) {
680 status
= REF_STATUS_REJECT_NONFASTFORWARD
;
684 else if (!strcmp(msg
, "already exists")) {
685 status
= REF_STATUS_REJECT_ALREADY_EXISTS
;
689 else if (!strcmp(msg
, "fetch first")) {
690 status
= REF_STATUS_REJECT_FETCH_FIRST
;
694 else if (!strcmp(msg
, "needs force")) {
695 status
= REF_STATUS_REJECT_NEEDS_FORCE
;
699 else if (!strcmp(msg
, "stale info")) {
700 status
= REF_STATUS_REJECT_STALE
;
707 *ref
= find_ref_by_name(*ref
, refname
);
709 *ref
= find_ref_by_name(remote_refs
, refname
);
711 warning("helper reported unexpected status of %s", refname
);
715 if ((*ref
)->status
!= REF_STATUS_NONE
) {
717 * Earlier, the ref was marked not to be pushed, so ignore the ref
718 * status reported by the remote helper if the latter is 'no match'.
720 if (status
== REF_STATUS_NONE
)
724 (*ref
)->status
= status
;
725 (*ref
)->remote_status
= msg
;
726 return !(status
== REF_STATUS_OK
);
729 static void push_update_refs_status(struct helper_data
*data
,
730 struct ref
*remote_refs
,
733 struct strbuf buf
= STRBUF_INIT
;
734 struct ref
*ref
= remote_refs
;
738 recvline(data
, &buf
);
742 if (push_update_ref_status(&buf
, &ref
, remote_refs
))
745 if (flags
& TRANSPORT_PUSH_DRY_RUN
|| !data
->refspecs
|| data
->no_private_update
)
748 /* propagate back the update to the remote namespace */
749 private = apply_refspecs(data
->refspecs
, data
->refspec_nr
, ref
->name
);
752 update_ref("update by helper", private, ref
->new_sha1
, NULL
, 0, 0);
755 strbuf_release(&buf
);
758 static int push_refs_with_push(struct transport
*transport
,
759 struct ref
*remote_refs
, int flags
)
761 int force_all
= flags
& TRANSPORT_PUSH_FORCE
;
762 int mirror
= flags
& TRANSPORT_PUSH_MIRROR
;
763 struct helper_data
*data
= transport
->data
;
764 struct strbuf buf
= STRBUF_INIT
;
766 struct string_list cas_options
= STRING_LIST_INIT_DUP
;
767 struct string_list_item
*cas_option
;
769 get_helper(transport
);
773 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
774 if (!ref
->peer_ref
&& !mirror
)
777 /* Check for statuses set by set_ref_status_for_push() */
778 switch (ref
->status
) {
779 case REF_STATUS_REJECT_NONFASTFORWARD
:
780 case REF_STATUS_REJECT_STALE
:
781 case REF_STATUS_REJECT_ALREADY_EXISTS
:
782 case REF_STATUS_UPTODATE
:
791 strbuf_addstr(&buf
, "push ");
792 if (!ref
->deletion
) {
794 strbuf_addch(&buf
, '+');
796 strbuf_addstr(&buf
, ref
->peer_ref
->name
);
798 strbuf_addstr(&buf
, sha1_to_hex(ref
->new_sha1
));
800 strbuf_addch(&buf
, ':');
801 strbuf_addstr(&buf
, ref
->name
);
802 strbuf_addch(&buf
, '\n');
805 * The "--force-with-lease" options without explicit
806 * values to expect have already been expanded into
807 * the ref->old_sha1_expect[] field; we can ignore
808 * transport->smart_options->cas altogether and instead
809 * can enumerate them from the refs.
811 if (ref
->expect_old_sha1
) {
812 struct strbuf cas
= STRBUF_INIT
;
813 strbuf_addf(&cas
, "%s:%s",
814 ref
->name
, sha1_to_hex(ref
->old_sha1_expect
));
815 string_list_append(&cas_options
, strbuf_detach(&cas
, NULL
));
819 string_list_clear(&cas_options
, 0);
823 standard_options(transport
);
824 for_each_string_list_item(cas_option
, &cas_options
)
825 set_helper_option(transport
, "cas", cas_option
->string
);
827 if (flags
& TRANSPORT_PUSH_DRY_RUN
) {
828 if (set_helper_option(transport
, "dry-run", "true") != 0)
829 die("helper %s does not support dry-run", data
->name
);
832 strbuf_addch(&buf
, '\n');
833 sendline(data
, &buf
);
834 strbuf_release(&buf
);
836 push_update_refs_status(data
, remote_refs
, flags
);
840 static int push_refs_with_export(struct transport
*transport
,
841 struct ref
*remote_refs
, int flags
)
844 struct child_process
*helper
, exporter
;
845 struct helper_data
*data
= transport
->data
;
846 struct string_list revlist_args
= STRING_LIST_INIT_NODUP
;
847 struct strbuf buf
= STRBUF_INIT
;
850 die("remote-helper doesn't support push; refspec needed");
852 if (flags
& TRANSPORT_PUSH_DRY_RUN
) {
853 if (set_helper_option(transport
, "dry-run", "true") != 0)
854 die("helper %s does not support dry-run", data
->name
);
857 helper
= get_helper(transport
);
859 write_constant(helper
->in
, "export\n");
863 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
865 unsigned char sha1
[20];
868 die("remote-helpers do not support ref deletion");
870 private = apply_refspecs(data
->refspecs
, data
->refspec_nr
, ref
->name
);
871 if (private && !get_sha1(private, sha1
)) {
872 strbuf_addf(&buf
, "^%s", private);
873 string_list_append(&revlist_args
, strbuf_detach(&buf
, NULL
));
874 hashcpy(ref
->old_sha1
, sha1
);
879 if (strcmp(ref
->peer_ref
->name
, ref
->name
))
880 die("remote-helpers do not support old:new syntax");
881 string_list_append(&revlist_args
, ref
->peer_ref
->name
);
885 if (get_exporter(transport
, &exporter
, &revlist_args
))
886 die("Couldn't run fast-export");
888 if (finish_command(&exporter
))
889 die("Error while running fast-export");
890 push_update_refs_status(data
, remote_refs
, flags
);
894 static int push_refs(struct transport
*transport
,
895 struct ref
*remote_refs
, int flags
)
897 struct helper_data
*data
= transport
->data
;
899 if (process_connect(transport
, 1)) {
900 do_take_over(transport
);
901 return transport
->push_refs(transport
, remote_refs
, flags
);
905 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
906 "Perhaps you should specify a branch such as 'master'.\n");
911 return push_refs_with_push(transport
, remote_refs
, flags
);
914 return push_refs_with_export(transport
, remote_refs
, flags
);
920 static int has_attribute(const char *attrs
, const char *attr
) {
927 const char *space
= strchrnul(attrs
, ' ');
928 if (len
== space
- attrs
&& !strncmp(attrs
, attr
, len
))
936 static struct ref
*get_refs_list(struct transport
*transport
, int for_push
)
938 struct helper_data
*data
= transport
->data
;
939 struct child_process
*helper
;
940 struct ref
*ret
= NULL
;
941 struct ref
**tail
= &ret
;
943 struct strbuf buf
= STRBUF_INIT
;
945 helper
= get_helper(transport
);
947 if (process_connect(transport
, for_push
)) {
948 do_take_over(transport
);
949 return transport
->get_refs_list(transport
, for_push
);
952 if (data
->push
&& for_push
)
953 write_str_in_full(helper
->in
, "list for-push\n");
955 write_str_in_full(helper
->in
, "list\n");
959 recvline(data
, &buf
);
964 eov
= strchr(buf
.buf
, ' ');
966 die("Malformed response in ref list: %s", buf
.buf
);
967 eon
= strchr(eov
+ 1, ' ');
971 *tail
= alloc_ref(eov
+ 1);
972 if (buf
.buf
[0] == '@')
973 (*tail
)->symref
= xstrdup(buf
.buf
+ 1);
974 else if (buf
.buf
[0] != '?')
975 get_sha1_hex(buf
.buf
, (*tail
)->old_sha1
);
977 if (has_attribute(eon
+ 1, "unchanged")) {
978 (*tail
)->status
|= REF_STATUS_UPTODATE
;
979 read_ref((*tail
)->name
, (*tail
)->old_sha1
);
982 tail
= &((*tail
)->next
);
985 fprintf(stderr
, "Debug: Read ref listing.\n");
986 strbuf_release(&buf
);
988 for (posn
= ret
; posn
; posn
= posn
->next
)
989 resolve_remote_symref(posn
, ret
);
994 int transport_helper_init(struct transport
*transport
, const char *name
)
996 struct helper_data
*data
= xcalloc(sizeof(*data
), 1);
999 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
1002 transport
->data
= data
;
1003 transport
->set_option
= set_helper_option
;
1004 transport
->get_refs_list
= get_refs_list
;
1005 transport
->fetch
= fetch
;
1006 transport
->push_refs
= push_refs
;
1007 transport
->disconnect
= release_helper
;
1008 transport
->connect
= connect_helper
;
1009 transport
->smart_options
= &(data
->transport_options
);
1014 * Linux pipes can buffer 65536 bytes at once (and most platforms can
1015 * buffer less), so attempt reads and writes with up to that size.
1017 #define BUFFERSIZE 65536
1018 /* This should be enough to hold debugging message. */
1019 #define PBUFFERSIZE 8192
1021 /* Print bidirectional transfer loop debug message. */
1022 __attribute__((format (printf
, 1, 2)))
1023 static void transfer_debug(const char *fmt
, ...)
1026 char msgbuf
[PBUFFERSIZE
];
1027 static int debug_enabled
= -1;
1029 if (debug_enabled
< 0)
1030 debug_enabled
= getenv("GIT_TRANSLOOP_DEBUG") ? 1 : 0;
1034 va_start(args
, fmt
);
1035 vsnprintf(msgbuf
, PBUFFERSIZE
, fmt
, args
);
1037 fprintf(stderr
, "Transfer loop debugging: %s\n", msgbuf
);
1040 /* Stream state: More data may be coming in this direction. */
1041 #define SSTATE_TRANSFERING 0
1043 * Stream state: No more data coming in this direction, flushing rest of
1046 #define SSTATE_FLUSHING 1
1047 /* Stream state: Transfer in this direction finished. */
1048 #define SSTATE_FINISHED 2
1050 #define STATE_NEEDS_READING(state) ((state) <= SSTATE_TRANSFERING)
1051 #define STATE_NEEDS_WRITING(state) ((state) <= SSTATE_FLUSHING)
1052 #define STATE_NEEDS_CLOSING(state) ((state) == SSTATE_FLUSHING)
1054 /* Unidirectional transfer. */
1055 struct unidirectional_transfer
{
1060 /* Is source socket? */
1062 /* Is destination socket? */
1064 /* Transfer state (TRANSFERRING/FLUSHING/FINISHED) */
1067 char buf
[BUFFERSIZE
];
1070 /* Name of source. */
1071 const char *src_name
;
1072 /* Name of destination. */
1073 const char *dest_name
;
1076 /* Closes the target (for writing) if transfer has finished. */
1077 static void udt_close_if_finished(struct unidirectional_transfer
*t
)
1079 if (STATE_NEEDS_CLOSING(t
->state
) && !t
->bufuse
) {
1080 t
->state
= SSTATE_FINISHED
;
1081 if (t
->dest_is_sock
)
1082 shutdown(t
->dest
, SHUT_WR
);
1085 transfer_debug("Closed %s.", t
->dest_name
);
1090 * Tries to read read data from source into buffer. If buffer is full,
1091 * no data is read. Returns 0 on success, -1 on error.
1093 static int udt_do_read(struct unidirectional_transfer
*t
)
1097 if (t
->bufuse
== BUFFERSIZE
)
1098 return 0; /* No space for more. */
1100 transfer_debug("%s is readable", t
->src_name
);
1101 bytes
= read(t
->src
, t
->buf
+ t
->bufuse
, BUFFERSIZE
- t
->bufuse
);
1102 if (bytes
< 0 && errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
&&
1104 error("read(%s) failed: %s", t
->src_name
, strerror(errno
));
1106 } else if (bytes
== 0) {
1107 transfer_debug("%s EOF (with %i bytes in buffer)",
1108 t
->src_name
, (int)t
->bufuse
);
1109 t
->state
= SSTATE_FLUSHING
;
1110 } else if (bytes
> 0) {
1112 transfer_debug("Read %i bytes from %s (buffer now at %i)",
1113 (int)bytes
, t
->src_name
, (int)t
->bufuse
);
1118 /* Tries to write data from buffer into destination. If buffer is empty,
1119 * no data is written. Returns 0 on success, -1 on error.
1121 static int udt_do_write(struct unidirectional_transfer
*t
)
1126 return 0; /* Nothing to write. */
1128 transfer_debug("%s is writable", t
->dest_name
);
1129 bytes
= write(t
->dest
, t
->buf
, t
->bufuse
);
1130 if (bytes
< 0 && errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
&&
1132 error("write(%s) failed: %s", t
->dest_name
, strerror(errno
));
1134 } else if (bytes
> 0) {
1137 memmove(t
->buf
, t
->buf
+ bytes
, t
->bufuse
);
1138 transfer_debug("Wrote %i bytes to %s (buffer now at %i)",
1139 (int)bytes
, t
->dest_name
, (int)t
->bufuse
);
1145 /* State of bidirectional transfer loop. */
1146 struct bidirectional_transfer_state
{
1147 /* Direction from program to git. */
1148 struct unidirectional_transfer ptg
;
1149 /* Direction from git to program. */
1150 struct unidirectional_transfer gtp
;
1153 static void *udt_copy_task_routine(void *udt
)
1155 struct unidirectional_transfer
*t
= (struct unidirectional_transfer
*)udt
;
1156 while (t
->state
!= SSTATE_FINISHED
) {
1157 if (STATE_NEEDS_READING(t
->state
))
1160 if (STATE_NEEDS_WRITING(t
->state
))
1161 if (udt_do_write(t
))
1163 if (STATE_NEEDS_CLOSING(t
->state
))
1164 udt_close_if_finished(t
);
1166 return udt
; /* Just some non-NULL value. */
1172 * Join thread, with appropriate errors on failure. Name is name for the
1173 * thread (for error messages). Returns 0 on success, 1 on failure.
1175 static int tloop_join(pthread_t thread
, const char *name
)
1179 err
= pthread_join(thread
, &tret
);
1181 error("%s thread failed", name
);
1185 error("%s thread failed to join: %s", name
, strerror(err
));
1192 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1195 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1197 pthread_t gtp_thread
;
1198 pthread_t ptg_thread
;
1201 err
= pthread_create(>p_thread
, NULL
, udt_copy_task_routine
,
1204 die("Can't start thread for copying data: %s", strerror(err
));
1205 err
= pthread_create(&ptg_thread
, NULL
, udt_copy_task_routine
,
1208 die("Can't start thread for copying data: %s", strerror(err
));
1210 ret
|= tloop_join(gtp_thread
, "Git to program copy");
1211 ret
|= tloop_join(ptg_thread
, "Program to git copy");
1216 /* Close the source and target (for writing) for transfer. */
1217 static void udt_kill_transfer(struct unidirectional_transfer
*t
)
1219 t
->state
= SSTATE_FINISHED
;
1221 * Socket read end left open isn't a disaster if nobody
1222 * attempts to read from it (mingw compat headers do not
1225 * We can't fully close the socket since otherwise gtp
1226 * task would first close the socket it sends data to
1227 * while closing the ptg file descriptors.
1229 if (!t
->src_is_sock
)
1231 if (t
->dest_is_sock
)
1232 shutdown(t
->dest
, SHUT_WR
);
1238 * Join process, with appropriate errors on failure. Name is name for the
1239 * process (for error messages). Returns 0 on success, 1 on failure.
1241 static int tloop_join(pid_t pid
, const char *name
)
1244 if (waitpid(pid
, &tret
, 0) < 0) {
1245 error("%s process failed to wait: %s", name
, strerror(errno
));
1248 if (!WIFEXITED(tret
) || WEXITSTATUS(tret
)) {
1249 error("%s process failed", name
);
1256 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1259 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1264 /* Fork thread #1: git to program. */
1267 die_errno("Can't start thread for copying data");
1268 else if (pid1
== 0) {
1269 udt_kill_transfer(&s
->ptg
);
1270 exit(udt_copy_task_routine(&s
->gtp
) ? 0 : 1);
1273 /* Fork thread #2: program to git. */
1276 die_errno("Can't start thread for copying data");
1277 else if (pid2
== 0) {
1278 udt_kill_transfer(&s
->gtp
);
1279 exit(udt_copy_task_routine(&s
->ptg
) ? 0 : 1);
1283 * Close both streams in parent as to not interfere with
1284 * end of file detection and wait for both tasks to finish.
1286 udt_kill_transfer(&s
->gtp
);
1287 udt_kill_transfer(&s
->ptg
);
1288 ret
|= tloop_join(pid1
, "Git to program copy");
1289 ret
|= tloop_join(pid2
, "Program to git copy");
1295 * Copies data from stdin to output and from input to stdout simultaneously.
1296 * Additionally filtering through given filter. If filter is NULL, uses
1299 int bidirectional_transfer_loop(int input
, int output
)
1301 struct bidirectional_transfer_state state
;
1303 /* Fill the state fields. */
1304 state
.ptg
.src
= input
;
1306 state
.ptg
.src_is_sock
= (input
== output
);
1307 state
.ptg
.dest_is_sock
= 0;
1308 state
.ptg
.state
= SSTATE_TRANSFERING
;
1309 state
.ptg
.bufuse
= 0;
1310 state
.ptg
.src_name
= "remote input";
1311 state
.ptg
.dest_name
= "stdout";
1314 state
.gtp
.dest
= output
;
1315 state
.gtp
.src_is_sock
= 0;
1316 state
.gtp
.dest_is_sock
= (input
== output
);
1317 state
.gtp
.state
= SSTATE_TRANSFERING
;
1318 state
.gtp
.bufuse
= 0;
1319 state
.gtp
.src_name
= "stdin";
1320 state
.gtp
.dest_name
= "remote output";
1322 return tloop_spawnwait_tasks(&state
);