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 no_disconnect_req
: 1;
33 /* These go from remote name (as in "list") to private name */
34 struct refspec
*refspecs
;
36 /* Transport options for fetch-pack/send-pack (should one of
39 struct git_transport_options transport_options
;
42 static void sendline(struct helper_data
*helper
, struct strbuf
*buffer
)
45 fprintf(stderr
, "Debug: Remote helper: -> %s", buffer
->buf
);
46 if (write_in_full(helper
->helper
->in
, buffer
->buf
, buffer
->len
)
48 die_errno("Full write to remote helper failed");
51 static int recvline_fh(FILE *helper
, struct strbuf
*buffer
, const char *name
)
55 fprintf(stderr
, "Debug: Remote helper: Waiting...\n");
56 if (strbuf_getline(buffer
, helper
, '\n') == EOF
) {
58 fprintf(stderr
, "Debug: Remote helper quit.\n");
63 fprintf(stderr
, "Debug: Remote helper: <- %s\n", buffer
->buf
);
67 static int recvline(struct helper_data
*helper
, struct strbuf
*buffer
)
69 return recvline_fh(helper
->out
, buffer
, helper
->name
);
72 static void xchgline(struct helper_data
*helper
, struct strbuf
*buffer
)
74 sendline(helper
, buffer
);
75 recvline(helper
, buffer
);
78 static void write_constant(int fd
, const char *str
)
81 fprintf(stderr
, "Debug: Remote helper: -> %s", str
);
82 if (write_in_full(fd
, str
, strlen(str
)) != strlen(str
))
83 die_errno("Full write to remote helper failed");
86 static const char *remove_ext_force(const char *url
)
89 const char *colon
= strchr(url
, ':');
90 if (colon
&& colon
[1] == ':')
96 static void do_take_over(struct transport
*transport
)
98 struct helper_data
*data
;
99 data
= (struct helper_data
*)transport
->data
;
100 transport_take_over(transport
, data
->helper
);
105 static struct child_process
*get_helper(struct transport
*transport
)
107 struct helper_data
*data
= transport
->data
;
108 struct argv_array argv
= ARGV_ARRAY_INIT
;
109 struct strbuf buf
= STRBUF_INIT
;
110 struct child_process
*helper
;
111 const char **refspecs
= NULL
;
113 int refspec_alloc
= 0;
116 char git_dir_buf
[sizeof(GIT_DIR_ENVIRONMENT
) + PATH_MAX
+ 1];
117 const char *helper_env
[] = {
126 helper
= xcalloc(1, sizeof(*helper
));
130 argv_array_pushf(&argv
, "git-remote-%s", data
->name
);
131 argv_array_push(&argv
, transport
->remote
->name
);
132 argv_array_push(&argv
, remove_ext_force(transport
->url
));
133 helper
->argv
= argv_array_detach(&argv
, NULL
);
135 helper
->silent_exec_failure
= 1;
137 snprintf(git_dir_buf
, sizeof(git_dir_buf
), "%s=%s", GIT_DIR_ENVIRONMENT
, get_git_dir());
138 helper
->env
= helper_env
;
140 code
= start_command(helper
);
141 if (code
< 0 && errno
== ENOENT
)
142 die("Unable to find remote helper for '%s'", data
->name
);
146 data
->helper
= helper
;
147 data
->no_disconnect_req
= 0;
150 * Open the output as FILE* so strbuf_getline() can be used.
151 * Do this with duped fd because fclose() will close the fd,
152 * and stuff like taking over will require the fd to remain.
154 duped
= dup(helper
->out
);
156 die_errno("Can't dup helper output fd");
157 data
->out
= xfdopen(duped
, "r");
159 write_constant(helper
->in
, "capabilities\n");
164 recvline(data
, &buf
);
169 if (*buf
.buf
== '*') {
170 capname
= buf
.buf
+ 1;
176 fprintf(stderr
, "Debug: Got cap %s\n", capname
);
177 if (!strcmp(capname
, "fetch"))
179 else if (!strcmp(capname
, "option"))
181 else if (!strcmp(capname
, "push"))
183 else if (!strcmp(capname
, "import"))
185 else if (!strcmp(capname
, "bidi-import"))
186 data
->bidi_import
= 1;
187 else if (!strcmp(capname
, "export"))
189 else if (!data
->refspecs
&& !prefixcmp(capname
, "refspec ")) {
193 refspecs
[refspec_nr
++] = xstrdup(capname
+ strlen("refspec "));
194 } else if (!strcmp(capname
, "connect")) {
196 } else if (!strcmp(capname
, "signed-tags")) {
197 data
->signed_tags
= 1;
198 } else if (!prefixcmp(capname
, "export-marks ")) {
199 struct strbuf arg
= STRBUF_INIT
;
200 strbuf_addstr(&arg
, "--export-marks=");
201 strbuf_addstr(&arg
, capname
+ strlen("export-marks "));
202 data
->export_marks
= strbuf_detach(&arg
, NULL
);
203 } else if (!prefixcmp(capname
, "import-marks")) {
204 struct strbuf arg
= STRBUF_INIT
;
205 strbuf_addstr(&arg
, "--import-marks=");
206 strbuf_addstr(&arg
, capname
+ strlen("import-marks "));
207 data
->import_marks
= strbuf_detach(&arg
, NULL
);
208 } else if (mandatory
) {
209 die("Unknown mandatory capability %s. This remote "
210 "helper probably needs newer version of Git.",
216 data
->refspec_nr
= refspec_nr
;
217 data
->refspecs
= parse_fetch_refspec(refspec_nr
, refspecs
);
218 for (i
= 0; i
< refspec_nr
; i
++)
219 free((char *)refspecs
[i
]);
221 } else if (data
->import
|| data
->bidi_import
|| data
->export
) {
222 warning("This remote helper should implement refspec capability.");
224 strbuf_release(&buf
);
226 fprintf(stderr
, "Debug: Capabilities complete.\n");
230 static int disconnect_helper(struct transport
*transport
)
232 struct helper_data
*data
= transport
->data
;
237 fprintf(stderr
, "Debug: Disconnecting.\n");
238 if (!data
->no_disconnect_req
) {
240 * Ignore write errors; there's nothing we can do,
241 * since we're about to close the pipe anyway. And the
242 * most likely error is EPIPE due to the helper dying
243 * to report an error itself.
245 sigchain_push(SIGPIPE
, SIG_IGN
);
246 xwrite(data
->helper
->in
, "\n", 1);
247 sigchain_pop(SIGPIPE
);
249 close(data
->helper
->in
);
250 close(data
->helper
->out
);
252 res
= finish_command(data
->helper
);
253 argv_array_free_detached(data
->helper
->argv
);
260 static const char *unsupported_options
[] = {
261 TRANS_OPT_UPLOADPACK
,
262 TRANS_OPT_RECEIVEPACK
,
266 static const char *boolean_options
[] = {
272 static int set_helper_option(struct transport
*transport
,
273 const char *name
, const char *value
)
275 struct helper_data
*data
= transport
->data
;
276 struct strbuf buf
= STRBUF_INIT
;
277 int i
, ret
, is_bool
= 0;
279 get_helper(transport
);
284 for (i
= 0; i
< ARRAY_SIZE(unsupported_options
); i
++) {
285 if (!strcmp(name
, unsupported_options
[i
]))
289 for (i
= 0; i
< ARRAY_SIZE(boolean_options
); i
++) {
290 if (!strcmp(name
, boolean_options
[i
])) {
296 strbuf_addf(&buf
, "option %s ", name
);
298 strbuf_addstr(&buf
, value
? "true" : "false");
300 quote_c_style(value
, &buf
, NULL
, 0);
301 strbuf_addch(&buf
, '\n');
303 xchgline(data
, &buf
);
305 if (!strcmp(buf
.buf
, "ok"))
307 else if (!prefixcmp(buf
.buf
, "error")) {
309 } else if (!strcmp(buf
.buf
, "unsupported"))
312 warning("%s unexpectedly said: '%s'", data
->name
, buf
.buf
);
315 strbuf_release(&buf
);
319 static void standard_options(struct transport
*t
)
325 set_helper_option(t
, "progress", t
->progress
? "true" : "false");
327 n
= snprintf(buf
, sizeof(buf
), "%d", v
+ 1);
328 if (n
>= sizeof(buf
))
329 die("impossibly large verbosity value");
330 set_helper_option(t
, "verbosity", buf
);
333 static int release_helper(struct transport
*transport
)
336 struct helper_data
*data
= transport
->data
;
337 free_refspec(data
->refspec_nr
, data
->refspecs
);
338 data
->refspecs
= NULL
;
339 res
= disconnect_helper(transport
);
340 free(transport
->data
);
344 static int fetch_with_fetch(struct transport
*transport
,
345 int nr_heads
, struct ref
**to_fetch
)
347 struct helper_data
*data
= transport
->data
;
349 struct strbuf buf
= STRBUF_INIT
;
351 standard_options(transport
);
353 for (i
= 0; i
< nr_heads
; i
++) {
354 const struct ref
*posn
= to_fetch
[i
];
355 if (posn
->status
& REF_STATUS_UPTODATE
)
358 strbuf_addf(&buf
, "fetch %s %s\n",
359 sha1_to_hex(posn
->old_sha1
), posn
->name
);
362 strbuf_addch(&buf
, '\n');
363 sendline(data
, &buf
);
366 recvline(data
, &buf
);
368 if (!prefixcmp(buf
.buf
, "lock ")) {
369 const char *name
= buf
.buf
+ 5;
370 if (transport
->pack_lockfile
)
371 warning("%s also locked %s", data
->name
, name
);
373 transport
->pack_lockfile
= xstrdup(name
);
378 warning("%s unexpectedly said: '%s'", data
->name
, buf
.buf
);
380 strbuf_release(&buf
);
384 static int get_importer(struct transport
*transport
, struct child_process
*fastimport
)
386 struct child_process
*helper
= get_helper(transport
);
387 struct helper_data
*data
= transport
->data
;
388 struct argv_array argv
= ARGV_ARRAY_INIT
;
389 int cat_blob_fd
, code
;
390 memset(fastimport
, 0, sizeof(*fastimport
));
391 fastimport
->in
= helper
->out
;
392 argv_array_push(&argv
, "fast-import");
393 argv_array_push(&argv
, debug
? "--stats" : "--quiet");
395 if (data
->bidi_import
) {
396 cat_blob_fd
= xdup(helper
->in
);
397 argv_array_pushf(&argv
, "--cat-blob-fd=%d", cat_blob_fd
);
399 fastimport
->argv
= argv
.argv
;
400 fastimport
->git_cmd
= 1;
402 code
= start_command(fastimport
);
406 static int get_exporter(struct transport
*transport
,
407 struct child_process
*fastexport
,
408 struct string_list
*revlist_args
)
410 struct helper_data
*data
= transport
->data
;
411 struct child_process
*helper
= get_helper(transport
);
413 memset(fastexport
, 0, sizeof(*fastexport
));
415 /* we need to duplicate helper->in because we want to use it after
416 * fastexport is done with it. */
417 fastexport
->out
= dup(helper
->in
);
418 fastexport
->argv
= xcalloc(6 + revlist_args
->nr
, sizeof(*fastexport
->argv
));
419 fastexport
->argv
[argc
++] = "fast-export";
420 fastexport
->argv
[argc
++] = "--use-done-feature";
421 fastexport
->argv
[argc
++] = data
->signed_tags
?
422 "--signed-tags=verbatim" : "--signed-tags=warn-strip";
423 if (data
->export_marks
)
424 fastexport
->argv
[argc
++] = data
->export_marks
;
425 if (data
->import_marks
)
426 fastexport
->argv
[argc
++] = data
->import_marks
;
428 for (i
= 0; i
< revlist_args
->nr
; i
++)
429 fastexport
->argv
[argc
++] = revlist_args
->items
[i
].string
;
431 fastexport
->git_cmd
= 1;
432 return start_command(fastexport
);
435 static int fetch_with_import(struct transport
*transport
,
436 int nr_heads
, struct ref
**to_fetch
)
438 struct child_process fastimport
;
439 struct helper_data
*data
= transport
->data
;
442 struct strbuf buf
= STRBUF_INIT
;
444 get_helper(transport
);
446 if (get_importer(transport
, &fastimport
))
447 die("Couldn't run fast-import");
449 for (i
= 0; i
< nr_heads
; i
++) {
451 if (posn
->status
& REF_STATUS_UPTODATE
)
454 strbuf_addf(&buf
, "import %s\n", posn
->name
);
455 sendline(data
, &buf
);
459 write_constant(data
->helper
->in
, "\n");
461 * remote-helpers that advertise the bidi-import capability are required to
462 * buffer the complete batch of import commands until this newline before
463 * sending data to fast-import.
464 * These helpers read back data from fast-import on their stdin, which could
465 * be mixed with import commands, otherwise.
468 if (finish_command(&fastimport
))
469 die("Error while running fast-import");
470 argv_array_free_detached(fastimport
.argv
);
473 * The fast-import stream of a remote helper that advertises
474 * the "refspec" capability writes to the refs named after the
475 * right hand side of the first refspec matching each ref we
478 * (If no "refspec" capability was specified, for historical
479 * reasons we default to the equivalent of *:*.)
481 * Store the result in to_fetch[i].old_sha1. Callers such
482 * as "git fetch" can use the value to write feedback to the
483 * terminal, populate FETCH_HEAD, and determine what new value
484 * should be written to peer_ref if the update is a
485 * fast-forward or this is a forced update.
487 for (i
= 0; i
< nr_heads
; i
++) {
490 if (posn
->status
& REF_STATUS_UPTODATE
)
493 private = apply_refspecs(data
->refspecs
, data
->refspec_nr
, posn
->name
);
495 private = xstrdup(posn
->name
);
497 read_ref(private, posn
->old_sha1
);
501 strbuf_release(&buf
);
505 static int process_connect_service(struct transport
*transport
,
506 const char *name
, const char *exec
)
508 struct helper_data
*data
= transport
->data
;
509 struct strbuf cmdbuf
= STRBUF_INIT
;
510 struct child_process
*helper
;
511 int r
, duped
, ret
= 0;
514 helper
= get_helper(transport
);
517 * Yes, dup the pipe another time, as we need unbuffered version
518 * of input pipe as FILE*. fclose() closes the underlying fd and
519 * stream buffering only can be changed before first I/O operation
522 duped
= dup(helper
->out
);
524 die_errno("Can't dup helper output fd");
525 input
= xfdopen(duped
, "r");
526 setvbuf(input
, NULL
, _IONBF
, 0);
529 * Handle --upload-pack and friends. This is fire and forget...
530 * just warn if it fails.
532 if (strcmp(name
, exec
)) {
533 r
= set_helper_option(transport
, "servpath", exec
);
535 warning("Setting remote service path not supported by protocol.");
537 warning("Invalid remote service path.");
541 strbuf_addf(&cmdbuf
, "connect %s\n", name
);
545 sendline(data
, &cmdbuf
);
546 recvline_fh(input
, &cmdbuf
, name
);
547 if (!strcmp(cmdbuf
.buf
, "")) {
548 data
->no_disconnect_req
= 1;
550 fprintf(stderr
, "Debug: Smart transport connection "
553 } else if (!strcmp(cmdbuf
.buf
, "fallback")) {
555 fprintf(stderr
, "Debug: Falling back to dumb "
558 die("Unknown response to connect: %s",
566 static int process_connect(struct transport
*transport
,
569 struct helper_data
*data
= transport
->data
;
573 name
= for_push
? "git-receive-pack" : "git-upload-pack";
575 exec
= data
->transport_options
.receivepack
;
577 exec
= data
->transport_options
.uploadpack
;
579 return process_connect_service(transport
, name
, exec
);
582 static int connect_helper(struct transport
*transport
, const char *name
,
583 const char *exec
, int fd
[2])
585 struct helper_data
*data
= transport
->data
;
587 /* Get_helper so connect is inited. */
588 get_helper(transport
);
590 die("Operation not supported by protocol.");
592 if (!process_connect_service(transport
, name
, exec
))
593 die("Can't connect to subservice %s.", name
);
595 fd
[0] = data
->helper
->out
;
596 fd
[1] = data
->helper
->in
;
600 static int fetch(struct transport
*transport
,
601 int nr_heads
, struct ref
**to_fetch
)
603 struct helper_data
*data
= transport
->data
;
606 if (process_connect(transport
, 0)) {
607 do_take_over(transport
);
608 return transport
->fetch(transport
, nr_heads
, to_fetch
);
612 for (i
= 0; i
< nr_heads
; i
++)
613 if (!(to_fetch
[i
]->status
& REF_STATUS_UPTODATE
))
620 return fetch_with_fetch(transport
, nr_heads
, to_fetch
);
623 return fetch_with_import(transport
, nr_heads
, to_fetch
);
628 static int push_update_ref_status(struct strbuf
*buf
,
630 struct ref
*remote_refs
)
635 if (!prefixcmp(buf
->buf
, "ok ")) {
636 status
= REF_STATUS_OK
;
637 refname
= buf
->buf
+ 3;
638 } else if (!prefixcmp(buf
->buf
, "error ")) {
639 status
= REF_STATUS_REMOTE_REJECT
;
640 refname
= buf
->buf
+ 6;
642 die("expected ok/error, helper said '%s'", buf
->buf
);
644 msg
= strchr(refname
, ' ');
646 struct strbuf msg_buf
= STRBUF_INIT
;
650 if (!unquote_c_style(&msg_buf
, msg
, &end
))
651 msg
= strbuf_detach(&msg_buf
, NULL
);
654 strbuf_release(&msg_buf
);
656 if (!strcmp(msg
, "no match")) {
657 status
= REF_STATUS_NONE
;
661 else if (!strcmp(msg
, "up to date")) {
662 status
= REF_STATUS_UPTODATE
;
666 else if (!strcmp(msg
, "non-fast forward")) {
667 status
= REF_STATUS_REJECT_NONFASTFORWARD
;
671 else if (!strcmp(msg
, "already exists")) {
672 status
= REF_STATUS_REJECT_ALREADY_EXISTS
;
676 else if (!strcmp(msg
, "fetch first")) {
677 status
= REF_STATUS_REJECT_FETCH_FIRST
;
681 else if (!strcmp(msg
, "needs force")) {
682 status
= REF_STATUS_REJECT_NEEDS_FORCE
;
686 else if (!strcmp(msg
, "stale info")) {
687 status
= REF_STATUS_REJECT_STALE
;
694 *ref
= find_ref_by_name(*ref
, refname
);
696 *ref
= find_ref_by_name(remote_refs
, refname
);
698 warning("helper reported unexpected status of %s", refname
);
702 if ((*ref
)->status
!= REF_STATUS_NONE
) {
704 * Earlier, the ref was marked not to be pushed, so ignore the ref
705 * status reported by the remote helper if the latter is 'no match'.
707 if (status
== REF_STATUS_NONE
)
711 (*ref
)->status
= status
;
712 (*ref
)->remote_status
= msg
;
713 return !(status
== REF_STATUS_OK
);
716 static void push_update_refs_status(struct helper_data
*data
,
717 struct ref
*remote_refs
)
719 struct strbuf buf
= STRBUF_INIT
;
720 struct ref
*ref
= remote_refs
;
724 recvline(data
, &buf
);
728 if (push_update_ref_status(&buf
, &ref
, remote_refs
))
734 /* propagate back the update to the remote namespace */
735 private = apply_refspecs(data
->refspecs
, data
->refspec_nr
, ref
->name
);
738 update_ref("update by helper", private, ref
->new_sha1
, NULL
, 0, 0);
741 strbuf_release(&buf
);
744 static int push_refs_with_push(struct transport
*transport
,
745 struct ref
*remote_refs
, int flags
)
747 int force_all
= flags
& TRANSPORT_PUSH_FORCE
;
748 int mirror
= flags
& TRANSPORT_PUSH_MIRROR
;
749 struct helper_data
*data
= transport
->data
;
750 struct strbuf buf
= STRBUF_INIT
;
752 struct string_list cas_options
= STRING_LIST_INIT_DUP
;
753 struct string_list_item
*cas_option
;
755 get_helper(transport
);
759 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
760 if (!ref
->peer_ref
&& !mirror
)
763 /* Check for statuses set by set_ref_status_for_push() */
764 switch (ref
->status
) {
765 case REF_STATUS_REJECT_NONFASTFORWARD
:
766 case REF_STATUS_REJECT_STALE
:
767 case REF_STATUS_REJECT_ALREADY_EXISTS
:
768 case REF_STATUS_UPTODATE
:
777 strbuf_addstr(&buf
, "push ");
778 if (!ref
->deletion
) {
780 strbuf_addch(&buf
, '+');
782 strbuf_addstr(&buf
, ref
->peer_ref
->name
);
784 strbuf_addstr(&buf
, sha1_to_hex(ref
->new_sha1
));
786 strbuf_addch(&buf
, ':');
787 strbuf_addstr(&buf
, ref
->name
);
788 strbuf_addch(&buf
, '\n');
791 * The "--force-with-lease" options without explicit
792 * values to expect have already been expanded into
793 * the ref->old_sha1_expect[] field; we can ignore
794 * transport->smart_options->cas altogether and instead
795 * can enumerate them from the refs.
797 if (ref
->expect_old_sha1
) {
798 struct strbuf cas
= STRBUF_INIT
;
799 strbuf_addf(&cas
, "%s:%s",
800 ref
->name
, sha1_to_hex(ref
->old_sha1_expect
));
801 string_list_append(&cas_options
, strbuf_detach(&cas
, NULL
));
805 string_list_clear(&cas_options
, 0);
809 standard_options(transport
);
810 for_each_string_list_item(cas_option
, &cas_options
)
811 set_helper_option(transport
, "cas", cas_option
->string
);
813 if (flags
& TRANSPORT_PUSH_DRY_RUN
) {
814 if (set_helper_option(transport
, "dry-run", "true") != 0)
815 die("helper %s does not support dry-run", data
->name
);
818 strbuf_addch(&buf
, '\n');
819 sendline(data
, &buf
);
820 strbuf_release(&buf
);
822 push_update_refs_status(data
, remote_refs
);
826 static int push_refs_with_export(struct transport
*transport
,
827 struct ref
*remote_refs
, int flags
)
830 struct child_process
*helper
, exporter
;
831 struct helper_data
*data
= transport
->data
;
832 struct string_list revlist_args
= STRING_LIST_INIT_NODUP
;
833 struct strbuf buf
= STRBUF_INIT
;
836 die("remote-helper doesn't support push; refspec needed");
838 if (flags
& TRANSPORT_PUSH_DRY_RUN
) {
839 if (set_helper_option(transport
, "dry-run", "true") != 0)
840 die("helper %s does not support dry-run", data
->name
);
843 helper
= get_helper(transport
);
845 write_constant(helper
->in
, "export\n");
849 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
851 unsigned char sha1
[20];
854 die("remote-helpers do not support ref deletion");
856 private = apply_refspecs(data
->refspecs
, data
->refspec_nr
, ref
->name
);
857 if (private && !get_sha1(private, sha1
)) {
858 strbuf_addf(&buf
, "^%s", private);
859 string_list_append(&revlist_args
, strbuf_detach(&buf
, NULL
));
860 hashcpy(ref
->old_sha1
, sha1
);
865 die("remote-helpers do not support ref deletion");
868 if (strcmp(ref
->peer_ref
->name
, ref
->name
))
869 die("remote-helpers do not support old:new syntax");
870 string_list_append(&revlist_args
, ref
->peer_ref
->name
);
874 if (get_exporter(transport
, &exporter
, &revlist_args
))
875 die("Couldn't run fast-export");
877 if (finish_command(&exporter
))
878 die("Error while running fast-export");
879 push_update_refs_status(data
, remote_refs
);
883 static int push_refs(struct transport
*transport
,
884 struct ref
*remote_refs
, int flags
)
886 struct helper_data
*data
= transport
->data
;
888 if (process_connect(transport
, 1)) {
889 do_take_over(transport
);
890 return transport
->push_refs(transport
, remote_refs
, flags
);
894 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
895 "Perhaps you should specify a branch such as 'master'.\n");
900 return push_refs_with_push(transport
, remote_refs
, flags
);
903 return push_refs_with_export(transport
, remote_refs
, flags
);
909 static int has_attribute(const char *attrs
, const char *attr
) {
916 const char *space
= strchrnul(attrs
, ' ');
917 if (len
== space
- attrs
&& !strncmp(attrs
, attr
, len
))
925 static struct ref
*get_refs_list(struct transport
*transport
, int for_push
)
927 struct helper_data
*data
= transport
->data
;
928 struct child_process
*helper
;
929 struct ref
*ret
= NULL
;
930 struct ref
**tail
= &ret
;
932 struct strbuf buf
= STRBUF_INIT
;
934 helper
= get_helper(transport
);
936 if (process_connect(transport
, for_push
)) {
937 do_take_over(transport
);
938 return transport
->get_refs_list(transport
, for_push
);
941 if (data
->push
&& for_push
)
942 write_str_in_full(helper
->in
, "list for-push\n");
944 write_str_in_full(helper
->in
, "list\n");
948 recvline(data
, &buf
);
953 eov
= strchr(buf
.buf
, ' ');
955 die("Malformed response in ref list: %s", buf
.buf
);
956 eon
= strchr(eov
+ 1, ' ');
960 *tail
= alloc_ref(eov
+ 1);
961 if (buf
.buf
[0] == '@')
962 (*tail
)->symref
= xstrdup(buf
.buf
+ 1);
963 else if (buf
.buf
[0] != '?')
964 get_sha1_hex(buf
.buf
, (*tail
)->old_sha1
);
966 if (has_attribute(eon
+ 1, "unchanged")) {
967 (*tail
)->status
|= REF_STATUS_UPTODATE
;
968 read_ref((*tail
)->name
, (*tail
)->old_sha1
);
971 tail
= &((*tail
)->next
);
974 fprintf(stderr
, "Debug: Read ref listing.\n");
975 strbuf_release(&buf
);
977 for (posn
= ret
; posn
; posn
= posn
->next
)
978 resolve_remote_symref(posn
, ret
);
983 int transport_helper_init(struct transport
*transport
, const char *name
)
985 struct helper_data
*data
= xcalloc(sizeof(*data
), 1);
988 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
991 transport
->data
= data
;
992 transport
->set_option
= set_helper_option
;
993 transport
->get_refs_list
= get_refs_list
;
994 transport
->fetch
= fetch
;
995 transport
->push_refs
= push_refs
;
996 transport
->disconnect
= release_helper
;
997 transport
->connect
= connect_helper
;
998 transport
->smart_options
= &(data
->transport_options
);
1003 * Linux pipes can buffer 65536 bytes at once (and most platforms can
1004 * buffer less), so attempt reads and writes with up to that size.
1006 #define BUFFERSIZE 65536
1007 /* This should be enough to hold debugging message. */
1008 #define PBUFFERSIZE 8192
1010 /* Print bidirectional transfer loop debug message. */
1011 static void transfer_debug(const char *fmt
, ...)
1014 char msgbuf
[PBUFFERSIZE
];
1015 static int debug_enabled
= -1;
1017 if (debug_enabled
< 0)
1018 debug_enabled
= getenv("GIT_TRANSLOOP_DEBUG") ? 1 : 0;
1022 va_start(args
, fmt
);
1023 vsnprintf(msgbuf
, PBUFFERSIZE
, fmt
, args
);
1025 fprintf(stderr
, "Transfer loop debugging: %s\n", msgbuf
);
1028 /* Stream state: More data may be coming in this direction. */
1029 #define SSTATE_TRANSFERING 0
1031 * Stream state: No more data coming in this direction, flushing rest of
1034 #define SSTATE_FLUSHING 1
1035 /* Stream state: Transfer in this direction finished. */
1036 #define SSTATE_FINISHED 2
1038 #define STATE_NEEDS_READING(state) ((state) <= SSTATE_TRANSFERING)
1039 #define STATE_NEEDS_WRITING(state) ((state) <= SSTATE_FLUSHING)
1040 #define STATE_NEEDS_CLOSING(state) ((state) == SSTATE_FLUSHING)
1042 /* Unidirectional transfer. */
1043 struct unidirectional_transfer
{
1048 /* Is source socket? */
1050 /* Is destination socket? */
1052 /* Transfer state (TRANSFERRING/FLUSHING/FINISHED) */
1055 char buf
[BUFFERSIZE
];
1058 /* Name of source. */
1059 const char *src_name
;
1060 /* Name of destination. */
1061 const char *dest_name
;
1064 /* Closes the target (for writing) if transfer has finished. */
1065 static void udt_close_if_finished(struct unidirectional_transfer
*t
)
1067 if (STATE_NEEDS_CLOSING(t
->state
) && !t
->bufuse
) {
1068 t
->state
= SSTATE_FINISHED
;
1069 if (t
->dest_is_sock
)
1070 shutdown(t
->dest
, SHUT_WR
);
1073 transfer_debug("Closed %s.", t
->dest_name
);
1078 * Tries to read read data from source into buffer. If buffer is full,
1079 * no data is read. Returns 0 on success, -1 on error.
1081 static int udt_do_read(struct unidirectional_transfer
*t
)
1085 if (t
->bufuse
== BUFFERSIZE
)
1086 return 0; /* No space for more. */
1088 transfer_debug("%s is readable", t
->src_name
);
1089 bytes
= read(t
->src
, t
->buf
+ t
->bufuse
, BUFFERSIZE
- t
->bufuse
);
1090 if (bytes
< 0 && errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
&&
1092 error("read(%s) failed: %s", t
->src_name
, strerror(errno
));
1094 } else if (bytes
== 0) {
1095 transfer_debug("%s EOF (with %i bytes in buffer)",
1096 t
->src_name
, t
->bufuse
);
1097 t
->state
= SSTATE_FLUSHING
;
1098 } else if (bytes
> 0) {
1100 transfer_debug("Read %i bytes from %s (buffer now at %i)",
1101 (int)bytes
, t
->src_name
, (int)t
->bufuse
);
1106 /* Tries to write data from buffer into destination. If buffer is empty,
1107 * no data is written. Returns 0 on success, -1 on error.
1109 static int udt_do_write(struct unidirectional_transfer
*t
)
1114 return 0; /* Nothing to write. */
1116 transfer_debug("%s is writable", t
->dest_name
);
1117 bytes
= write(t
->dest
, t
->buf
, t
->bufuse
);
1118 if (bytes
< 0 && errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
&&
1120 error("write(%s) failed: %s", t
->dest_name
, strerror(errno
));
1122 } else if (bytes
> 0) {
1125 memmove(t
->buf
, t
->buf
+ bytes
, t
->bufuse
);
1126 transfer_debug("Wrote %i bytes to %s (buffer now at %i)",
1127 (int)bytes
, t
->dest_name
, (int)t
->bufuse
);
1133 /* State of bidirectional transfer loop. */
1134 struct bidirectional_transfer_state
{
1135 /* Direction from program to git. */
1136 struct unidirectional_transfer ptg
;
1137 /* Direction from git to program. */
1138 struct unidirectional_transfer gtp
;
1141 static void *udt_copy_task_routine(void *udt
)
1143 struct unidirectional_transfer
*t
= (struct unidirectional_transfer
*)udt
;
1144 while (t
->state
!= SSTATE_FINISHED
) {
1145 if (STATE_NEEDS_READING(t
->state
))
1148 if (STATE_NEEDS_WRITING(t
->state
))
1149 if (udt_do_write(t
))
1151 if (STATE_NEEDS_CLOSING(t
->state
))
1152 udt_close_if_finished(t
);
1154 return udt
; /* Just some non-NULL value. */
1160 * Join thread, with apporiate errors on failure. Name is name for the
1161 * thread (for error messages). Returns 0 on success, 1 on failure.
1163 static int tloop_join(pthread_t thread
, const char *name
)
1167 err
= pthread_join(thread
, &tret
);
1169 error("%s thread failed", name
);
1173 error("%s thread failed to join: %s", name
, strerror(err
));
1180 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1183 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1185 pthread_t gtp_thread
;
1186 pthread_t ptg_thread
;
1189 err
= pthread_create(>p_thread
, NULL
, udt_copy_task_routine
,
1192 die("Can't start thread for copying data: %s", strerror(err
));
1193 err
= pthread_create(&ptg_thread
, NULL
, udt_copy_task_routine
,
1196 die("Can't start thread for copying data: %s", strerror(err
));
1198 ret
|= tloop_join(gtp_thread
, "Git to program copy");
1199 ret
|= tloop_join(ptg_thread
, "Program to git copy");
1204 /* Close the source and target (for writing) for transfer. */
1205 static void udt_kill_transfer(struct unidirectional_transfer
*t
)
1207 t
->state
= SSTATE_FINISHED
;
1209 * Socket read end left open isn't a disaster if nobody
1210 * attempts to read from it (mingw compat headers do not
1213 * We can't fully close the socket since otherwise gtp
1214 * task would first close the socket it sends data to
1215 * while closing the ptg file descriptors.
1217 if (!t
->src_is_sock
)
1219 if (t
->dest_is_sock
)
1220 shutdown(t
->dest
, SHUT_WR
);
1226 * Join process, with apporiate errors on failure. Name is name for the
1227 * process (for error messages). Returns 0 on success, 1 on failure.
1229 static int tloop_join(pid_t pid
, const char *name
)
1232 if (waitpid(pid
, &tret
, 0) < 0) {
1233 error("%s process failed to wait: %s", name
, strerror(errno
));
1236 if (!WIFEXITED(tret
) || WEXITSTATUS(tret
)) {
1237 error("%s process failed", name
);
1244 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1247 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1252 /* Fork thread #1: git to program. */
1255 die_errno("Can't start thread for copying data");
1256 else if (pid1
== 0) {
1257 udt_kill_transfer(&s
->ptg
);
1258 exit(udt_copy_task_routine(&s
->gtp
) ? 0 : 1);
1261 /* Fork thread #2: program to git. */
1264 die_errno("Can't start thread for copying data");
1265 else if (pid2
== 0) {
1266 udt_kill_transfer(&s
->gtp
);
1267 exit(udt_copy_task_routine(&s
->ptg
) ? 0 : 1);
1271 * Close both streams in parent as to not interfere with
1272 * end of file detection and wait for both tasks to finish.
1274 udt_kill_transfer(&s
->gtp
);
1275 udt_kill_transfer(&s
->ptg
);
1276 ret
|= tloop_join(pid1
, "Git to program copy");
1277 ret
|= tloop_join(pid2
, "Program to git copy");
1283 * Copies data from stdin to output and from input to stdout simultaneously.
1284 * Additionally filtering through given filter. If filter is NULL, uses
1287 int bidirectional_transfer_loop(int input
, int output
)
1289 struct bidirectional_transfer_state state
;
1291 /* Fill the state fields. */
1292 state
.ptg
.src
= input
;
1294 state
.ptg
.src_is_sock
= (input
== output
);
1295 state
.ptg
.dest_is_sock
= 0;
1296 state
.ptg
.state
= SSTATE_TRANSFERING
;
1297 state
.ptg
.bufuse
= 0;
1298 state
.ptg
.src_name
= "remote input";
1299 state
.ptg
.dest_name
= "stdout";
1302 state
.gtp
.dest
= output
;
1303 state
.gtp
.src_is_sock
= 0;
1304 state
.gtp
.dest_is_sock
= (input
== output
);
1305 state
.gtp
.state
= SSTATE_TRANSFERING
;
1306 state
.gtp
.bufuse
= 0;
1307 state
.gtp
.src_name
= "stdin";
1308 state
.gtp
.dest_name
= "remote output";
1310 return tloop_spawnwait_tasks(&state
);