4 #include "run-command.h"
10 #include "string-list.h"
11 #include "thread-utils.h"
16 #include "transport-internal.h"
23 struct child_process
*helper
;
32 stateless_connect
: 1,
34 check_connectivity
: 1,
35 no_disconnect_req
: 1,
36 no_private_update
: 1,
40 * As an optimization, the transport code may invoke fetch before
41 * get_refs_list. If this happens, and if the transport helper doesn't
42 * support connect or stateless_connect, we need to invoke
43 * get_refs_list ourselves if we haven't already done so. Keep track of
44 * whether we have invoked get_refs_list.
46 unsigned get_refs_list_called
: 1;
50 /* These go from remote name (as in "list") to private name */
52 /* Transport options for fetch-pack/send-pack (should one of
55 struct git_transport_options transport_options
;
58 static void sendline(struct helper_data
*helper
, struct strbuf
*buffer
)
61 fprintf(stderr
, "Debug: Remote helper: -> %s", buffer
->buf
);
62 if (write_in_full(helper
->helper
->in
, buffer
->buf
, buffer
->len
) < 0)
63 die_errno(_("full write to remote helper failed"));
66 static int recvline_fh(FILE *helper
, struct strbuf
*buffer
)
70 fprintf(stderr
, "Debug: Remote helper: Waiting...\n");
71 if (strbuf_getline(buffer
, helper
) == EOF
) {
73 fprintf(stderr
, "Debug: Remote helper quit.\n");
78 fprintf(stderr
, "Debug: Remote helper: <- %s\n", buffer
->buf
);
82 static int recvline(struct helper_data
*helper
, struct strbuf
*buffer
)
84 return recvline_fh(helper
->out
, buffer
);
87 static void write_constant(int fd
, const char *str
)
90 fprintf(stderr
, "Debug: Remote helper: -> %s", str
);
91 if (write_in_full(fd
, str
, strlen(str
)) < 0)
92 die_errno(_("full write to remote helper failed"));
95 static const char *remove_ext_force(const char *url
)
98 const char *colon
= strchr(url
, ':');
99 if (colon
&& colon
[1] == ':')
105 static void do_take_over(struct transport
*transport
)
107 struct helper_data
*data
;
108 data
= (struct helper_data
*)transport
->data
;
109 transport_take_over(transport
, data
->helper
);
114 static void standard_options(struct transport
*t
);
116 static struct child_process
*get_helper(struct transport
*transport
)
118 struct helper_data
*data
= transport
->data
;
119 struct strbuf buf
= STRBUF_INIT
;
120 struct child_process
*helper
;
127 helper
= xmalloc(sizeof(*helper
));
128 child_process_init(helper
);
132 strvec_pushf(&helper
->args
, "remote-%s", data
->name
);
133 strvec_push(&helper
->args
, transport
->remote
->name
);
134 strvec_push(&helper
->args
, remove_ext_force(transport
->url
));
136 helper
->silent_exec_failure
= 1;
139 strvec_pushf(&helper
->env
, "%s=%s",
140 GIT_DIR_ENVIRONMENT
, get_git_dir());
142 helper
->trace2_child_class
= helper
->args
.v
[0]; /* "remote-<name>" */
144 code
= start_command(helper
);
145 if (code
< 0 && errno
== ENOENT
)
146 die(_("unable to find remote helper for '%s'"), data
->name
);
150 data
->helper
= helper
;
151 data
->no_disconnect_req
= 0;
152 refspec_init(&data
->rs
, REFSPEC_FETCH
);
155 * Open the output as FILE* so strbuf_getline_*() family of
156 * functions can be used.
157 * Do this with duped fd because fclose() will close the fd,
158 * and stuff like taking over will require the fd to remain.
160 duped
= dup(helper
->out
);
162 die_errno(_("can't dup helper output fd"));
163 data
->out
= xfdopen(duped
, "r");
165 write_constant(helper
->in
, "capabilities\n");
168 const char *capname
, *arg
;
170 if (recvline(data
, &buf
))
176 if (*buf
.buf
== '*') {
177 capname
= buf
.buf
+ 1;
183 fprintf(stderr
, "Debug: Got cap %s\n", capname
);
184 if (!strcmp(capname
, "fetch"))
186 else if (!strcmp(capname
, "option"))
188 else if (!strcmp(capname
, "push"))
190 else if (!strcmp(capname
, "import"))
192 else if (!strcmp(capname
, "bidi-import"))
193 data
->bidi_import
= 1;
194 else if (!strcmp(capname
, "export"))
196 else if (!strcmp(capname
, "check-connectivity"))
197 data
->check_connectivity
= 1;
198 else if (skip_prefix(capname
, "refspec ", &arg
)) {
199 refspec_append(&data
->rs
, arg
);
200 } else if (!strcmp(capname
, "connect")) {
202 } else if (!strcmp(capname
, "stateless-connect")) {
203 data
->stateless_connect
= 1;
204 } else if (!strcmp(capname
, "signed-tags")) {
205 data
->signed_tags
= 1;
206 } else if (skip_prefix(capname
, "export-marks ", &arg
)) {
207 data
->export_marks
= xstrdup(arg
);
208 } else if (skip_prefix(capname
, "import-marks ", &arg
)) {
209 data
->import_marks
= xstrdup(arg
);
210 } else if (starts_with(capname
, "no-private-update")) {
211 data
->no_private_update
= 1;
212 } else if (starts_with(capname
, "object-format")) {
213 data
->object_format
= 1;
214 } else if (mandatory
) {
215 die(_("unknown mandatory capability %s; this remote "
216 "helper probably needs newer version of Git"),
220 if (!data
->rs
.nr
&& (data
->import
|| data
->bidi_import
|| data
->export
)) {
221 warning(_("this remote helper should implement refspec capability"));
223 strbuf_release(&buf
);
225 fprintf(stderr
, "Debug: Capabilities complete.\n");
226 standard_options(transport
);
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 FREE_AND_NULL(data
->helper
);
258 static const char *unsupported_options
[] = {
259 TRANS_OPT_UPLOADPACK
,
260 TRANS_OPT_RECEIVEPACK
,
265 static const char *boolean_options
[] = {
268 TRANS_OPT_FOLLOWTAGS
,
269 TRANS_OPT_DEEPEN_RELATIVE
272 static int strbuf_set_helper_option(struct helper_data
*data
,
278 if (recvline(data
, buf
))
281 if (!strcmp(buf
->buf
, "ok"))
283 else if (starts_with(buf
->buf
, "error"))
285 else if (!strcmp(buf
->buf
, "unsupported"))
288 warning(_("%s unexpectedly said: '%s'"), data
->name
, buf
->buf
);
294 static int string_list_set_helper_option(struct helper_data
*data
,
296 struct string_list
*list
)
298 struct strbuf buf
= STRBUF_INIT
;
301 for (i
= 0; i
< list
->nr
; i
++) {
302 strbuf_addf(&buf
, "option %s ", name
);
303 quote_c_style(list
->items
[i
].string
, &buf
, NULL
, 0);
304 strbuf_addch(&buf
, '\n');
306 if ((ret
= strbuf_set_helper_option(data
, &buf
)))
310 strbuf_release(&buf
);
314 static int set_helper_option(struct transport
*transport
,
315 const char *name
, const char *value
)
317 struct helper_data
*data
= transport
->data
;
318 struct strbuf buf
= STRBUF_INIT
;
319 int i
, ret
, is_bool
= 0;
321 get_helper(transport
);
326 if (!strcmp(name
, "deepen-not"))
327 return string_list_set_helper_option(data
, name
,
328 (struct string_list
*)value
);
330 for (i
= 0; i
< ARRAY_SIZE(unsupported_options
); i
++) {
331 if (!strcmp(name
, unsupported_options
[i
]))
335 for (i
= 0; i
< ARRAY_SIZE(boolean_options
); i
++) {
336 if (!strcmp(name
, boolean_options
[i
])) {
342 strbuf_addf(&buf
, "option %s ", name
);
344 strbuf_addstr(&buf
, value
? "true" : "false");
346 quote_c_style(value
, &buf
, NULL
, 0);
347 strbuf_addch(&buf
, '\n');
349 ret
= strbuf_set_helper_option(data
, &buf
);
350 strbuf_release(&buf
);
354 static void standard_options(struct transport
*t
)
359 set_helper_option(t
, "progress", t
->progress
? "true" : "false");
361 xsnprintf(buf
, sizeof(buf
), "%d", v
+ 1);
362 set_helper_option(t
, "verbosity", buf
);
365 case TRANSPORT_FAMILY_ALL
:
367 * this is already the default,
368 * do not break old remote helpers by setting "all" here
371 case TRANSPORT_FAMILY_IPV4
:
372 set_helper_option(t
, "family", "ipv4");
374 case TRANSPORT_FAMILY_IPV6
:
375 set_helper_option(t
, "family", "ipv6");
380 static int release_helper(struct transport
*transport
)
383 struct helper_data
*data
= transport
->data
;
384 refspec_clear(&data
->rs
);
385 res
= disconnect_helper(transport
);
386 free(transport
->data
);
390 static int fetch_with_fetch(struct transport
*transport
,
391 int nr_heads
, struct ref
**to_fetch
)
393 struct helper_data
*data
= transport
->data
;
395 struct strbuf buf
= STRBUF_INIT
;
397 for (i
= 0; i
< nr_heads
; i
++) {
398 const struct ref
*posn
= to_fetch
[i
];
399 if (posn
->status
& REF_STATUS_UPTODATE
)
402 strbuf_addf(&buf
, "fetch %s %s\n",
403 oid_to_hex(&posn
->old_oid
),
404 posn
->symref
? posn
->symref
: posn
->name
);
407 strbuf_addch(&buf
, '\n');
408 sendline(data
, &buf
);
413 if (recvline(data
, &buf
))
416 if (skip_prefix(buf
.buf
, "lock ", &name
)) {
417 if (transport
->pack_lockfiles
.nr
)
418 warning(_("%s also locked %s"), data
->name
, name
);
420 string_list_append(&transport
->pack_lockfiles
,
423 else if (data
->check_connectivity
&&
424 data
->transport_options
.check_self_contained_and_connected
&&
425 !strcmp(buf
.buf
, "connectivity-ok"))
426 data
->transport_options
.self_contained_and_connected
= 1;
430 warning(_("%s unexpectedly said: '%s'"), data
->name
, buf
.buf
);
432 strbuf_release(&buf
);
436 static int get_importer(struct transport
*transport
, struct child_process
*fastimport
)
438 struct child_process
*helper
= get_helper(transport
);
439 struct helper_data
*data
= transport
->data
;
440 int cat_blob_fd
, code
;
441 child_process_init(fastimport
);
442 fastimport
->in
= xdup(helper
->out
);
443 strvec_push(&fastimport
->args
, "fast-import");
444 strvec_push(&fastimport
->args
, "--allow-unsafe-features");
445 strvec_push(&fastimport
->args
, debug
? "--stats" : "--quiet");
447 if (data
->bidi_import
) {
448 cat_blob_fd
= xdup(helper
->in
);
449 strvec_pushf(&fastimport
->args
, "--cat-blob-fd=%d", cat_blob_fd
);
451 fastimport
->git_cmd
= 1;
453 code
= start_command(fastimport
);
457 static int get_exporter(struct transport
*transport
,
458 struct child_process
*fastexport
,
459 struct string_list
*revlist_args
)
461 struct helper_data
*data
= transport
->data
;
462 struct child_process
*helper
= get_helper(transport
);
465 child_process_init(fastexport
);
467 /* we need to duplicate helper->in because we want to use it after
468 * fastexport is done with it. */
469 fastexport
->out
= dup(helper
->in
);
470 strvec_push(&fastexport
->args
, "fast-export");
471 strvec_push(&fastexport
->args
, "--use-done-feature");
472 strvec_push(&fastexport
->args
, data
->signed_tags
?
473 "--signed-tags=verbatim" : "--signed-tags=warn-strip");
474 if (data
->export_marks
)
475 strvec_pushf(&fastexport
->args
, "--export-marks=%s.tmp", data
->export_marks
);
476 if (data
->import_marks
)
477 strvec_pushf(&fastexport
->args
, "--import-marks=%s", data
->import_marks
);
479 for (i
= 0; i
< revlist_args
->nr
; i
++)
480 strvec_push(&fastexport
->args
, revlist_args
->items
[i
].string
);
482 fastexport
->git_cmd
= 1;
483 return start_command(fastexport
);
486 static int fetch_with_import(struct transport
*transport
,
487 int nr_heads
, struct ref
**to_fetch
)
489 struct child_process fastimport
;
490 struct helper_data
*data
= transport
->data
;
493 struct strbuf buf
= STRBUF_INIT
;
495 get_helper(transport
);
497 if (get_importer(transport
, &fastimport
))
498 die(_("couldn't run fast-import"));
500 for (i
= 0; i
< nr_heads
; i
++) {
502 if (posn
->status
& REF_STATUS_UPTODATE
)
505 strbuf_addf(&buf
, "import %s\n",
506 posn
->symref
? posn
->symref
: posn
->name
);
507 sendline(data
, &buf
);
511 write_constant(data
->helper
->in
, "\n");
513 * remote-helpers that advertise the bidi-import capability are required to
514 * buffer the complete batch of import commands until this newline before
515 * sending data to fast-import.
516 * These helpers read back data from fast-import on their stdin, which could
517 * be mixed with import commands, otherwise.
520 if (finish_command(&fastimport
))
521 die(_("error while running fast-import"));
524 * The fast-import stream of a remote helper that advertises
525 * the "refspec" capability writes to the refs named after the
526 * right hand side of the first refspec matching each ref we
529 * (If no "refspec" capability was specified, for historical
530 * reasons we default to the equivalent of *:*.)
532 * Store the result in to_fetch[i].old_sha1. Callers such
533 * as "git fetch" can use the value to write feedback to the
534 * terminal, populate FETCH_HEAD, and determine what new value
535 * should be written to peer_ref if the update is a
536 * fast-forward or this is a forced update.
538 for (i
= 0; i
< nr_heads
; i
++) {
539 char *private, *name
;
541 if (posn
->status
& REF_STATUS_UPTODATE
)
543 name
= posn
->symref
? posn
->symref
: posn
->name
;
545 private = apply_refspecs(&data
->rs
, name
);
547 private = xstrdup(name
);
549 if (read_ref(private, &posn
->old_oid
) < 0)
550 die(_("could not read ref %s"), private);
554 strbuf_release(&buf
);
558 static int run_connect(struct transport
*transport
, struct strbuf
*cmdbuf
)
560 struct helper_data
*data
= transport
->data
;
564 struct child_process
*helper
;
566 helper
= get_helper(transport
);
569 * Yes, dup the pipe another time, as we need unbuffered version
570 * of input pipe as FILE*. fclose() closes the underlying fd and
571 * stream buffering only can be changed before first I/O operation
574 duped
= dup(helper
->out
);
576 die_errno(_("can't dup helper output fd"));
577 input
= xfdopen(duped
, "r");
578 setvbuf(input
, NULL
, _IONBF
, 0);
580 sendline(data
, cmdbuf
);
581 if (recvline_fh(input
, cmdbuf
))
584 if (!strcmp(cmdbuf
->buf
, "")) {
585 data
->no_disconnect_req
= 1;
587 fprintf(stderr
, "Debug: Smart transport connection "
590 } else if (!strcmp(cmdbuf
->buf
, "fallback")) {
592 fprintf(stderr
, "Debug: Falling back to dumb "
595 die(_("unknown response to connect: %s"),
603 static int process_connect_service(struct transport
*transport
,
604 const char *name
, const char *exec
)
606 struct helper_data
*data
= transport
->data
;
607 struct strbuf cmdbuf
= STRBUF_INIT
;
611 * Handle --upload-pack and friends. This is fire and forget...
612 * just warn if it fails.
614 if (strcmp(name
, exec
)) {
615 int r
= set_helper_option(transport
, "servpath", exec
);
617 warning(_("setting remote service path not supported by protocol"));
619 warning(_("invalid remote service path"));
623 strbuf_addf(&cmdbuf
, "connect %s\n", name
);
624 ret
= run_connect(transport
, &cmdbuf
);
625 } else if (data
->stateless_connect
&&
626 (get_protocol_version_config() == protocol_v2
) &&
627 !strcmp("git-upload-pack", name
)) {
628 strbuf_addf(&cmdbuf
, "stateless-connect %s\n", name
);
629 ret
= run_connect(transport
, &cmdbuf
);
631 transport
->stateless_rpc
= 1;
634 strbuf_release(&cmdbuf
);
638 static int process_connect(struct transport
*transport
,
641 struct helper_data
*data
= transport
->data
;
645 name
= for_push
? "git-receive-pack" : "git-upload-pack";
647 exec
= data
->transport_options
.receivepack
;
649 exec
= data
->transport_options
.uploadpack
;
651 return process_connect_service(transport
, name
, exec
);
654 static int connect_helper(struct transport
*transport
, const char *name
,
655 const char *exec
, int fd
[2])
657 struct helper_data
*data
= transport
->data
;
659 /* Get_helper so connect is inited. */
660 get_helper(transport
);
662 die(_("operation not supported by protocol"));
664 if (!process_connect_service(transport
, name
, exec
))
665 die(_("can't connect to subservice %s"), name
);
667 fd
[0] = data
->helper
->out
;
668 fd
[1] = data
->helper
->in
;
672 static struct ref
*get_refs_list_using_list(struct transport
*transport
,
675 static int fetch_refs(struct transport
*transport
,
676 int nr_heads
, struct ref
**to_fetch
)
678 struct helper_data
*data
= transport
->data
;
681 get_helper(transport
);
683 if (process_connect(transport
, 0)) {
684 do_take_over(transport
);
685 return transport
->vtable
->fetch_refs(transport
, nr_heads
, to_fetch
);
689 * If we reach here, then the server, the client, and/or the transport
690 * helper does not support protocol v2. --negotiate-only requires
693 if (data
->transport_options
.acked_commits
) {
694 warning(_("--negotiate-only requires protocol v2"));
698 if (!data
->get_refs_list_called
)
699 get_refs_list_using_list(transport
, 0);
702 for (i
= 0; i
< nr_heads
; i
++)
703 if (!(to_fetch
[i
]->status
& REF_STATUS_UPTODATE
))
709 if (data
->check_connectivity
&&
710 data
->transport_options
.check_self_contained_and_connected
)
711 set_helper_option(transport
, "check-connectivity", "true");
713 if (transport
->cloning
)
714 set_helper_option(transport
, "cloning", "true");
716 if (data
->transport_options
.update_shallow
)
717 set_helper_option(transport
, "update-shallow", "true");
719 if (data
->transport_options
.refetch
)
720 set_helper_option(transport
, "refetch", "true");
722 if (data
->transport_options
.filter_options
.choice
) {
723 const char *spec
= expand_list_objects_filter_spec(
724 &data
->transport_options
.filter_options
);
725 set_helper_option(transport
, "filter", spec
);
728 if (data
->transport_options
.negotiation_tips
)
729 warning("Ignoring --negotiation-tip because the protocol does not support it.");
732 return fetch_with_fetch(transport
, nr_heads
, to_fetch
);
735 return fetch_with_import(transport
, nr_heads
, to_fetch
);
740 struct push_update_ref_state
{
742 struct ref_push_report
*report
;
746 static int push_update_ref_status(struct strbuf
*buf
,
747 struct push_update_ref_state
*state
,
748 struct ref
*remote_refs
)
751 int status
, forced
= 0;
753 if (starts_with(buf
->buf
, "option ")) {
754 struct object_id old_oid
, new_oid
;
755 const char *key
, *val
;
758 if (!state
->hint
|| !(state
->report
|| state
->new_report
))
759 die(_("'option' without a matching 'ok/error' directive"));
760 if (state
->new_report
) {
761 if (!state
->hint
->report
) {
762 CALLOC_ARRAY(state
->hint
->report
, 1);
763 state
->report
= state
->hint
->report
;
765 state
->report
= state
->hint
->report
;
766 while (state
->report
->next
)
767 state
->report
= state
->report
->next
;
768 CALLOC_ARRAY(state
->report
->next
, 1);
769 state
->report
= state
->report
->next
;
771 state
->new_report
= 0;
774 p
= strchr(key
, ' ');
778 if (!strcmp(key
, "refname"))
779 state
->report
->ref_name
= xstrdup_or_null(val
);
780 else if (!strcmp(key
, "old-oid") && val
&&
781 !parse_oid_hex(val
, &old_oid
, &val
))
782 state
->report
->old_oid
= oiddup(&old_oid
);
783 else if (!strcmp(key
, "new-oid") && val
&&
784 !parse_oid_hex(val
, &new_oid
, &val
))
785 state
->report
->new_oid
= oiddup(&new_oid
);
786 else if (!strcmp(key
, "forced-update"))
787 state
->report
->forced_update
= 1;
788 /* Not update remote namespace again. */
792 state
->report
= NULL
;
793 state
->new_report
= 0;
795 if (starts_with(buf
->buf
, "ok ")) {
796 status
= REF_STATUS_OK
;
797 refname
= buf
->buf
+ 3;
798 } else if (starts_with(buf
->buf
, "error ")) {
799 status
= REF_STATUS_REMOTE_REJECT
;
800 refname
= buf
->buf
+ 6;
802 die(_("expected ok/error, helper said '%s'"), buf
->buf
);
804 msg
= strchr(refname
, ' ');
806 struct strbuf msg_buf
= STRBUF_INIT
;
810 if (!unquote_c_style(&msg_buf
, msg
, &end
))
811 msg
= strbuf_detach(&msg_buf
, NULL
);
814 strbuf_release(&msg_buf
);
816 if (!strcmp(msg
, "no match")) {
817 status
= REF_STATUS_NONE
;
820 else if (!strcmp(msg
, "up to date")) {
821 status
= REF_STATUS_UPTODATE
;
824 else if (!strcmp(msg
, "non-fast forward")) {
825 status
= REF_STATUS_REJECT_NONFASTFORWARD
;
828 else if (!strcmp(msg
, "already exists")) {
829 status
= REF_STATUS_REJECT_ALREADY_EXISTS
;
832 else if (!strcmp(msg
, "fetch first")) {
833 status
= REF_STATUS_REJECT_FETCH_FIRST
;
836 else if (!strcmp(msg
, "needs force")) {
837 status
= REF_STATUS_REJECT_NEEDS_FORCE
;
840 else if (!strcmp(msg
, "stale info")) {
841 status
= REF_STATUS_REJECT_STALE
;
844 else if (!strcmp(msg
, "remote ref updated since checkout")) {
845 status
= REF_STATUS_REJECT_REMOTE_UPDATED
;
848 else if (!strcmp(msg
, "forced update")) {
852 else if (!strcmp(msg
, "expecting report")) {
853 status
= REF_STATUS_EXPECTING_REPORT
;
859 state
->hint
= find_ref_by_name(state
->hint
, refname
);
861 state
->hint
= find_ref_by_name(remote_refs
, refname
);
863 warning(_("helper reported unexpected status of %s"), refname
);
867 if (state
->hint
->status
!= REF_STATUS_NONE
) {
869 * Earlier, the ref was marked not to be pushed, so ignore the ref
870 * status reported by the remote helper if the latter is 'no match'.
872 if (status
== REF_STATUS_NONE
)
876 if (status
== REF_STATUS_OK
)
877 state
->new_report
= 1;
878 state
->hint
->status
= status
;
879 state
->hint
->forced_update
|= forced
;
880 state
->hint
->remote_status
= msg
;
881 return !(status
== REF_STATUS_OK
);
884 static int push_update_refs_status(struct helper_data
*data
,
885 struct ref
*remote_refs
,
889 struct ref_push_report
*report
;
890 struct strbuf buf
= STRBUF_INIT
;
891 struct push_update_ref_state state
= { remote_refs
, NULL
, 0 };
894 if (recvline(data
, &buf
)) {
895 strbuf_release(&buf
);
900 push_update_ref_status(&buf
, &state
, remote_refs
);
902 strbuf_release(&buf
);
904 if (flags
& TRANSPORT_PUSH_DRY_RUN
|| !data
->rs
.nr
|| data
->no_private_update
)
907 /* propagate back the update to the remote namespace */
908 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
911 if (ref
->status
!= REF_STATUS_OK
)
915 private = apply_refspecs(&data
->rs
, ref
->name
);
918 update_ref("update by helper", private, &(ref
->new_oid
),
922 for (report
= ref
->report
; report
; report
= report
->next
) {
923 private = apply_refspecs(&data
->rs
,
929 update_ref("update by helper", private,
941 static void set_common_push_options(struct transport
*transport
,
942 const char *name
, int flags
)
944 if (flags
& TRANSPORT_PUSH_DRY_RUN
) {
945 if (set_helper_option(transport
, "dry-run", "true") != 0)
946 die(_("helper %s does not support dry-run"), name
);
947 } else if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
) {
948 if (set_helper_option(transport
, TRANS_OPT_PUSH_CERT
, "true") != 0)
949 die(_("helper %s does not support --signed"), name
);
950 } else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
) {
951 if (set_helper_option(transport
, TRANS_OPT_PUSH_CERT
, "if-asked") != 0)
952 die(_("helper %s does not support --signed=if-asked"), name
);
955 if (flags
& TRANSPORT_PUSH_ATOMIC
)
956 if (set_helper_option(transport
, TRANS_OPT_ATOMIC
, "true") != 0)
957 die(_("helper %s does not support --atomic"), name
);
959 if (flags
& TRANSPORT_PUSH_FORCE_IF_INCLUDES
)
960 if (set_helper_option(transport
, TRANS_OPT_FORCE_IF_INCLUDES
, "true") != 0)
961 die(_("helper %s does not support --%s"),
962 name
, TRANS_OPT_FORCE_IF_INCLUDES
);
964 if (flags
& TRANSPORT_PUSH_OPTIONS
) {
965 struct string_list_item
*item
;
966 for_each_string_list_item(item
, transport
->push_options
)
967 if (set_helper_option(transport
, "push-option", item
->string
) != 0)
968 die(_("helper %s does not support 'push-option'"), name
);
972 static int push_refs_with_push(struct transport
*transport
,
973 struct ref
*remote_refs
, int flags
)
975 int force_all
= flags
& TRANSPORT_PUSH_FORCE
;
976 int mirror
= flags
& TRANSPORT_PUSH_MIRROR
;
977 int atomic
= flags
& TRANSPORT_PUSH_ATOMIC
;
978 struct helper_data
*data
= transport
->data
;
979 struct strbuf buf
= STRBUF_INIT
;
981 struct string_list cas_options
= STRING_LIST_INIT_DUP
;
982 struct string_list_item
*cas_option
;
984 get_helper(transport
);
988 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
989 if (!ref
->peer_ref
&& !mirror
)
992 /* Check for statuses set by set_ref_status_for_push() */
993 switch (ref
->status
) {
994 case REF_STATUS_REJECT_NONFASTFORWARD
:
995 case REF_STATUS_REJECT_STALE
:
996 case REF_STATUS_REJECT_ALREADY_EXISTS
:
997 case REF_STATUS_REJECT_REMOTE_UPDATED
:
999 reject_atomic_push(remote_refs
, mirror
);
1000 string_list_clear(&cas_options
, 0);
1004 case REF_STATUS_UPTODATE
:
1013 strbuf_addstr(&buf
, "push ");
1014 if (!ref
->deletion
) {
1016 strbuf_addch(&buf
, '+');
1018 strbuf_addstr(&buf
, ref
->peer_ref
->name
);
1020 strbuf_addstr(&buf
, oid_to_hex(&ref
->new_oid
));
1022 strbuf_addch(&buf
, ':');
1023 strbuf_addstr(&buf
, ref
->name
);
1024 strbuf_addch(&buf
, '\n');
1027 * The "--force-with-lease" options without explicit
1028 * values to expect have already been expanded into
1029 * the ref->old_oid_expect[] field; we can ignore
1030 * transport->smart_options->cas altogether and instead
1031 * can enumerate them from the refs.
1033 if (ref
->expect_old_sha1
) {
1034 struct strbuf cas
= STRBUF_INIT
;
1035 strbuf_addf(&cas
, "%s:%s",
1036 ref
->name
, oid_to_hex(&ref
->old_oid_expect
));
1037 string_list_append_nodup(&cas_options
,
1038 strbuf_detach(&cas
, NULL
));
1042 string_list_clear(&cas_options
, 0);
1046 for_each_string_list_item(cas_option
, &cas_options
)
1047 set_helper_option(transport
, "cas", cas_option
->string
);
1048 set_common_push_options(transport
, data
->name
, flags
);
1050 strbuf_addch(&buf
, '\n');
1051 sendline(data
, &buf
);
1052 strbuf_release(&buf
);
1053 string_list_clear(&cas_options
, 0);
1055 return push_update_refs_status(data
, remote_refs
, flags
);
1058 static int push_refs_with_export(struct transport
*transport
,
1059 struct ref
*remote_refs
, int flags
)
1062 struct child_process
*helper
, exporter
;
1063 struct helper_data
*data
= transport
->data
;
1064 struct string_list revlist_args
= STRING_LIST_INIT_DUP
;
1065 struct strbuf buf
= STRBUF_INIT
;
1068 die(_("remote-helper doesn't support push; refspec needed"));
1070 set_common_push_options(transport
, data
->name
, flags
);
1071 if (flags
& TRANSPORT_PUSH_FORCE
) {
1072 if (set_helper_option(transport
, "force", "true") != 0)
1073 warning(_("helper %s does not support 'force'"), data
->name
);
1076 helper
= get_helper(transport
);
1078 write_constant(helper
->in
, "export\n");
1080 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1082 struct object_id oid
;
1084 private = apply_refspecs(&data
->rs
, ref
->name
);
1085 if (private && !get_oid(private, &oid
)) {
1086 strbuf_addf(&buf
, "^%s", private);
1087 string_list_append_nodup(&revlist_args
,
1088 strbuf_detach(&buf
, NULL
));
1089 oidcpy(&ref
->old_oid
, &oid
);
1093 if (ref
->peer_ref
) {
1094 if (strcmp(ref
->name
, ref
->peer_ref
->name
)) {
1095 if (!ref
->deletion
) {
1099 /* Follow symbolic refs (mainly for HEAD). */
1100 name
= resolve_ref_unsafe(ref
->peer_ref
->name
,
1101 RESOLVE_REF_READING
,
1103 if (!name
|| !(flag
& REF_ISSYMREF
))
1104 name
= ref
->peer_ref
->name
;
1106 strbuf_addf(&buf
, "%s:%s", name
, ref
->name
);
1108 strbuf_addf(&buf
, ":%s", ref
->name
);
1110 string_list_append(&revlist_args
, "--refspec");
1111 string_list_append(&revlist_args
, buf
.buf
);
1112 strbuf_release(&buf
);
1115 string_list_append(&revlist_args
, ref
->peer_ref
->name
);
1119 if (get_exporter(transport
, &exporter
, &revlist_args
))
1120 die(_("couldn't run fast-export"));
1122 string_list_clear(&revlist_args
, 1);
1124 if (finish_command(&exporter
))
1125 die(_("error while running fast-export"));
1126 if (push_update_refs_status(data
, remote_refs
, flags
))
1129 if (data
->export_marks
) {
1130 strbuf_addf(&buf
, "%s.tmp", data
->export_marks
);
1131 rename(buf
.buf
, data
->export_marks
);
1132 strbuf_release(&buf
);
1138 static int push_refs(struct transport
*transport
,
1139 struct ref
*remote_refs
, int flags
)
1141 struct helper_data
*data
= transport
->data
;
1143 if (process_connect(transport
, 1)) {
1144 do_take_over(transport
);
1145 return transport
->vtable
->push_refs(transport
, remote_refs
, flags
);
1150 _("No refs in common and none specified; doing nothing.\n"
1151 "Perhaps you should specify a branch.\n"));
1156 return push_refs_with_push(transport
, remote_refs
, flags
);
1159 return push_refs_with_export(transport
, remote_refs
, flags
);
1165 static int has_attribute(const char *attrs
, const char *attr
)
1173 const char *space
= strchrnul(attrs
, ' ');
1174 if (len
== space
- attrs
&& !strncmp(attrs
, attr
, len
))
1182 static struct ref
*get_refs_list(struct transport
*transport
, int for_push
,
1183 struct transport_ls_refs_options
*transport_options
)
1185 get_helper(transport
);
1187 if (process_connect(transport
, for_push
)) {
1188 do_take_over(transport
);
1189 return transport
->vtable
->get_refs_list(transport
, for_push
,
1193 return get_refs_list_using_list(transport
, for_push
);
1196 static struct ref
*get_refs_list_using_list(struct transport
*transport
,
1199 struct helper_data
*data
= transport
->data
;
1200 struct child_process
*helper
;
1201 struct ref
*ret
= NULL
;
1202 struct ref
**tail
= &ret
;
1204 struct strbuf buf
= STRBUF_INIT
;
1206 data
->get_refs_list_called
= 1;
1207 helper
= get_helper(transport
);
1209 if (data
->object_format
) {
1210 write_str_in_full(helper
->in
, "option object-format\n");
1211 if (recvline(data
, &buf
) || strcmp(buf
.buf
, "ok"))
1215 if (data
->push
&& for_push
)
1216 write_str_in_full(helper
->in
, "list for-push\n");
1218 write_str_in_full(helper
->in
, "list\n");
1222 if (recvline(data
, &buf
))
1227 else if (buf
.buf
[0] == ':') {
1229 if (skip_prefix(buf
.buf
, ":object-format ", &value
)) {
1230 int algo
= hash_algo_by_name(value
);
1231 if (algo
== GIT_HASH_UNKNOWN
)
1232 die(_("unsupported object format '%s'"),
1234 transport
->hash_algo
= &hash_algos
[algo
];
1239 eov
= strchr(buf
.buf
, ' ');
1241 die(_("malformed response in ref list: %s"), buf
.buf
);
1242 eon
= strchr(eov
+ 1, ' ');
1246 *tail
= alloc_ref(eov
+ 1);
1247 if (buf
.buf
[0] == '@')
1248 (*tail
)->symref
= xstrdup(buf
.buf
+ 1);
1249 else if (buf
.buf
[0] != '?')
1250 get_oid_hex_algop(buf
.buf
, &(*tail
)->old_oid
, transport
->hash_algo
);
1252 if (has_attribute(eon
+ 1, "unchanged")) {
1253 (*tail
)->status
|= REF_STATUS_UPTODATE
;
1254 if (read_ref((*tail
)->name
, &(*tail
)->old_oid
) < 0)
1255 die(_("could not read ref %s"),
1259 tail
= &((*tail
)->next
);
1262 fprintf(stderr
, "Debug: Read ref listing.\n");
1263 strbuf_release(&buf
);
1265 for (posn
= ret
; posn
; posn
= posn
->next
)
1266 resolve_remote_symref(posn
, ret
);
1271 static int get_bundle_uri(struct transport
*transport
)
1273 get_helper(transport
);
1275 if (process_connect(transport
, 0)) {
1276 do_take_over(transport
);
1277 return transport
->vtable
->get_bundle_uri(transport
);
1283 static struct transport_vtable vtable
= {
1284 .set_option
= set_helper_option
,
1285 .get_refs_list
= get_refs_list
,
1286 .get_bundle_uri
= get_bundle_uri
,
1287 .fetch_refs
= fetch_refs
,
1288 .push_refs
= push_refs
,
1289 .connect
= connect_helper
,
1290 .disconnect
= release_helper
1293 int transport_helper_init(struct transport
*transport
, const char *name
)
1295 struct helper_data
*data
= xcalloc(1, sizeof(*data
));
1298 transport_check_allowed(name
);
1300 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
1303 list_objects_filter_init(&data
->transport_options
.filter_options
);
1305 transport
->data
= data
;
1306 transport
->vtable
= &vtable
;
1307 transport
->smart_options
= &(data
->transport_options
);
1312 * Linux pipes can buffer 65536 bytes at once (and most platforms can
1313 * buffer less), so attempt reads and writes with up to that size.
1315 #define BUFFERSIZE 65536
1316 /* This should be enough to hold debugging message. */
1317 #define PBUFFERSIZE 8192
1319 /* Print bidirectional transfer loop debug message. */
1320 __attribute__((format (printf
, 1, 2)))
1321 static void transfer_debug(const char *fmt
, ...)
1324 * NEEDSWORK: This function is sometimes used from multiple threads, and
1325 * we end up using debug_enabled racily. That "should not matter" since
1326 * we always write the same value, but it's still wrong. This function
1327 * is listed in .tsan-suppressions for the time being.
1331 char msgbuf
[PBUFFERSIZE
];
1332 static int debug_enabled
= -1;
1334 if (debug_enabled
< 0)
1335 debug_enabled
= getenv("GIT_TRANSLOOP_DEBUG") ? 1 : 0;
1339 va_start(args
, fmt
);
1340 vsnprintf(msgbuf
, PBUFFERSIZE
, fmt
, args
);
1342 fprintf(stderr
, "Transfer loop debugging: %s\n", msgbuf
);
1345 /* Stream state: More data may be coming in this direction. */
1346 #define SSTATE_TRANSFERRING 0
1348 * Stream state: No more data coming in this direction, flushing rest of
1351 #define SSTATE_FLUSHING 1
1352 /* Stream state: Transfer in this direction finished. */
1353 #define SSTATE_FINISHED 2
1355 #define STATE_NEEDS_READING(state) ((state) <= SSTATE_TRANSFERRING)
1356 #define STATE_NEEDS_WRITING(state) ((state) <= SSTATE_FLUSHING)
1357 #define STATE_NEEDS_CLOSING(state) ((state) == SSTATE_FLUSHING)
1359 /* Unidirectional transfer. */
1360 struct unidirectional_transfer
{
1365 /* Is source socket? */
1367 /* Is destination socket? */
1369 /* Transfer state (TRANSFERRING/FLUSHING/FINISHED) */
1372 char buf
[BUFFERSIZE
];
1375 /* Name of source. */
1376 const char *src_name
;
1377 /* Name of destination. */
1378 const char *dest_name
;
1381 /* Closes the target (for writing) if transfer has finished. */
1382 static void udt_close_if_finished(struct unidirectional_transfer
*t
)
1384 if (STATE_NEEDS_CLOSING(t
->state
) && !t
->bufuse
) {
1385 t
->state
= SSTATE_FINISHED
;
1386 if (t
->dest_is_sock
)
1387 shutdown(t
->dest
, SHUT_WR
);
1390 transfer_debug("Closed %s.", t
->dest_name
);
1395 * Tries to read data from source into buffer. If buffer is full,
1396 * no data is read. Returns 0 on success, -1 on error.
1398 static int udt_do_read(struct unidirectional_transfer
*t
)
1402 if (t
->bufuse
== BUFFERSIZE
)
1403 return 0; /* No space for more. */
1405 transfer_debug("%s is readable", t
->src_name
);
1406 bytes
= xread(t
->src
, t
->buf
+ t
->bufuse
, BUFFERSIZE
- t
->bufuse
);
1408 error_errno(_("read(%s) failed"), t
->src_name
);
1410 } else if (bytes
== 0) {
1411 transfer_debug("%s EOF (with %i bytes in buffer)",
1412 t
->src_name
, (int)t
->bufuse
);
1413 t
->state
= SSTATE_FLUSHING
;
1414 } else if (bytes
> 0) {
1416 transfer_debug("Read %i bytes from %s (buffer now at %i)",
1417 (int)bytes
, t
->src_name
, (int)t
->bufuse
);
1422 /* Tries to write data from buffer into destination. If buffer is empty,
1423 * no data is written. Returns 0 on success, -1 on error.
1425 static int udt_do_write(struct unidirectional_transfer
*t
)
1430 return 0; /* Nothing to write. */
1432 transfer_debug("%s is writable", t
->dest_name
);
1433 bytes
= xwrite(t
->dest
, t
->buf
, t
->bufuse
);
1435 error_errno(_("write(%s) failed"), t
->dest_name
);
1437 } else if (bytes
> 0) {
1440 memmove(t
->buf
, t
->buf
+ bytes
, t
->bufuse
);
1441 transfer_debug("Wrote %i bytes to %s (buffer now at %i)",
1442 (int)bytes
, t
->dest_name
, (int)t
->bufuse
);
1448 /* State of bidirectional transfer loop. */
1449 struct bidirectional_transfer_state
{
1450 /* Direction from program to git. */
1451 struct unidirectional_transfer ptg
;
1452 /* Direction from git to program. */
1453 struct unidirectional_transfer gtp
;
1456 static void *udt_copy_task_routine(void *udt
)
1458 struct unidirectional_transfer
*t
= (struct unidirectional_transfer
*)udt
;
1459 while (t
->state
!= SSTATE_FINISHED
) {
1460 if (STATE_NEEDS_READING(t
->state
))
1463 if (STATE_NEEDS_WRITING(t
->state
))
1464 if (udt_do_write(t
))
1466 if (STATE_NEEDS_CLOSING(t
->state
))
1467 udt_close_if_finished(t
);
1469 return udt
; /* Just some non-NULL value. */
1475 * Join thread, with appropriate errors on failure. Name is name for the
1476 * thread (for error messages). Returns 0 on success, 1 on failure.
1478 static int tloop_join(pthread_t thread
, const char *name
)
1482 err
= pthread_join(thread
, &tret
);
1484 error(_("%s thread failed"), name
);
1488 error(_("%s thread failed to join: %s"), name
, strerror(err
));
1495 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1498 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1500 pthread_t gtp_thread
;
1501 pthread_t ptg_thread
;
1504 err
= pthread_create(>p_thread
, NULL
, udt_copy_task_routine
,
1507 die(_("can't start thread for copying data: %s"), strerror(err
));
1508 err
= pthread_create(&ptg_thread
, NULL
, udt_copy_task_routine
,
1511 die(_("can't start thread for copying data: %s"), strerror(err
));
1513 ret
|= tloop_join(gtp_thread
, "Git to program copy");
1514 ret
|= tloop_join(ptg_thread
, "Program to git copy");
1519 /* Close the source and target (for writing) for transfer. */
1520 static void udt_kill_transfer(struct unidirectional_transfer
*t
)
1522 t
->state
= SSTATE_FINISHED
;
1524 * Socket read end left open isn't a disaster if nobody
1525 * attempts to read from it (mingw compat headers do not
1528 * We can't fully close the socket since otherwise gtp
1529 * task would first close the socket it sends data to
1530 * while closing the ptg file descriptors.
1532 if (!t
->src_is_sock
)
1534 if (t
->dest_is_sock
)
1535 shutdown(t
->dest
, SHUT_WR
);
1541 * Join process, with appropriate errors on failure. Name is name for the
1542 * process (for error messages). Returns 0 on success, 1 on failure.
1544 static int tloop_join(pid_t pid
, const char *name
)
1547 if (waitpid(pid
, &tret
, 0) < 0) {
1548 error_errno(_("%s process failed to wait"), name
);
1551 if (!WIFEXITED(tret
) || WEXITSTATUS(tret
)) {
1552 error(_("%s process failed"), name
);
1559 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1562 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1567 /* Fork thread #1: git to program. */
1570 die_errno(_("can't start thread for copying data"));
1571 else if (pid1
== 0) {
1572 udt_kill_transfer(&s
->ptg
);
1573 exit(udt_copy_task_routine(&s
->gtp
) ? 0 : 1);
1576 /* Fork thread #2: program to git. */
1579 die_errno(_("can't start thread for copying data"));
1580 else if (pid2
== 0) {
1581 udt_kill_transfer(&s
->gtp
);
1582 exit(udt_copy_task_routine(&s
->ptg
) ? 0 : 1);
1586 * Close both streams in parent as to not interfere with
1587 * end of file detection and wait for both tasks to finish.
1589 udt_kill_transfer(&s
->gtp
);
1590 udt_kill_transfer(&s
->ptg
);
1591 ret
|= tloop_join(pid1
, "Git to program copy");
1592 ret
|= tloop_join(pid2
, "Program to git copy");
1598 * Copies data from stdin to output and from input to stdout simultaneously.
1599 * Additionally filtering through given filter. If filter is NULL, uses
1602 int bidirectional_transfer_loop(int input
, int output
)
1604 struct bidirectional_transfer_state state
;
1606 /* Fill the state fields. */
1607 state
.ptg
.src
= input
;
1609 state
.ptg
.src_is_sock
= (input
== output
);
1610 state
.ptg
.dest_is_sock
= 0;
1611 state
.ptg
.state
= SSTATE_TRANSFERRING
;
1612 state
.ptg
.bufuse
= 0;
1613 state
.ptg
.src_name
= "remote input";
1614 state
.ptg
.dest_name
= "stdout";
1617 state
.gtp
.dest
= output
;
1618 state
.gtp
.src_is_sock
= 0;
1619 state
.gtp
.dest_is_sock
= (input
== output
);
1620 state
.gtp
.state
= SSTATE_TRANSFERRING
;
1621 state
.gtp
.bufuse
= 0;
1622 state
.gtp
.src_name
= "stdin";
1623 state
.gtp
.dest_name
= "remote output";
1625 return tloop_spawnwait_tasks(&state
);
1628 void reject_atomic_push(struct ref
*remote_refs
, int mirror_mode
)
1632 /* Mark other refs as failed */
1633 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1634 if (!ref
->peer_ref
&& !mirror_mode
)
1637 switch (ref
->status
) {
1638 case REF_STATUS_NONE
:
1640 case REF_STATUS_EXPECTING_REPORT
:
1641 ref
->status
= REF_STATUS_ATOMIC_PUSH_FAILED
;
1644 break; /* do nothing */