1 #include "git-compat-util.h"
4 #include "run-command.h"
6 #include "environment.h"
9 #include "object-name.h"
10 #include "repository.h"
12 #include "string-list.h"
13 #include "thread-utils.h"
18 #include "transport-internal.h"
26 struct child_process
*helper
;
35 stateless_connect
: 1,
37 check_connectivity
: 1,
38 no_disconnect_req
: 1,
39 no_private_update
: 1,
43 * As an optimization, the transport code may invoke fetch before
44 * get_refs_list. If this happens, and if the transport helper doesn't
45 * support connect or stateless_connect, we need to invoke
46 * get_refs_list ourselves if we haven't already done so. Keep track of
47 * whether we have invoked get_refs_list.
49 unsigned get_refs_list_called
: 1;
53 /* These go from remote name (as in "list") to private name */
55 /* Transport options for fetch-pack/send-pack (should one of
58 struct git_transport_options transport_options
;
61 static void sendline(struct helper_data
*helper
, struct strbuf
*buffer
)
64 fprintf(stderr
, "Debug: Remote helper: -> %s", buffer
->buf
);
65 if (write_in_full(helper
->helper
->in
, buffer
->buf
, buffer
->len
) < 0)
66 die_errno(_("full write to remote helper failed"));
69 static int recvline_fh(FILE *helper
, struct strbuf
*buffer
)
73 fprintf(stderr
, "Debug: Remote helper: Waiting...\n");
74 if (strbuf_getline(buffer
, helper
) == EOF
) {
76 fprintf(stderr
, "Debug: Remote helper quit.\n");
81 fprintf(stderr
, "Debug: Remote helper: <- %s\n", buffer
->buf
);
85 static int recvline(struct helper_data
*helper
, struct strbuf
*buffer
)
87 return recvline_fh(helper
->out
, buffer
);
90 static void write_constant(int fd
, const char *str
)
93 fprintf(stderr
, "Debug: Remote helper: -> %s", str
);
94 if (write_in_full(fd
, str
, strlen(str
)) < 0)
95 die_errno(_("full write to remote helper failed"));
98 static const char *remove_ext_force(const char *url
)
101 const char *colon
= strchr(url
, ':');
102 if (colon
&& colon
[1] == ':')
108 static void do_take_over(struct transport
*transport
)
110 struct helper_data
*data
;
111 data
= (struct helper_data
*)transport
->data
;
112 transport_take_over(transport
, data
->helper
);
117 static void standard_options(struct transport
*t
);
119 static struct child_process
*get_helper(struct transport
*transport
)
121 struct helper_data
*data
= transport
->data
;
122 struct strbuf buf
= STRBUF_INIT
;
123 struct child_process
*helper
;
130 helper
= xmalloc(sizeof(*helper
));
131 child_process_init(helper
);
135 strvec_pushf(&helper
->args
, "remote-%s", data
->name
);
136 strvec_push(&helper
->args
, transport
->remote
->name
);
137 strvec_push(&helper
->args
, remove_ext_force(transport
->url
));
139 helper
->silent_exec_failure
= 1;
142 strvec_pushf(&helper
->env
, "%s=%s",
143 GIT_DIR_ENVIRONMENT
, get_git_dir());
145 helper
->trace2_child_class
= helper
->args
.v
[0]; /* "remote-<name>" */
147 code
= start_command(helper
);
148 if (code
< 0 && errno
== ENOENT
)
149 die(_("unable to find remote helper for '%s'"), data
->name
);
153 data
->helper
= helper
;
154 data
->no_disconnect_req
= 0;
155 refspec_init(&data
->rs
, REFSPEC_FETCH
);
158 * Open the output as FILE* so strbuf_getline_*() family of
159 * functions can be used.
160 * Do this with duped fd because fclose() will close the fd,
161 * and stuff like taking over will require the fd to remain.
163 duped
= dup(helper
->out
);
165 die_errno(_("can't dup helper output fd"));
166 data
->out
= xfdopen(duped
, "r");
168 write_constant(helper
->in
, "capabilities\n");
171 const char *capname
, *arg
;
173 if (recvline(data
, &buf
))
179 if (*buf
.buf
== '*') {
180 capname
= buf
.buf
+ 1;
186 fprintf(stderr
, "Debug: Got cap %s\n", capname
);
187 if (!strcmp(capname
, "fetch"))
189 else if (!strcmp(capname
, "option"))
191 else if (!strcmp(capname
, "push"))
193 else if (!strcmp(capname
, "import"))
195 else if (!strcmp(capname
, "bidi-import"))
196 data
->bidi_import
= 1;
197 else if (!strcmp(capname
, "export"))
199 else if (!strcmp(capname
, "check-connectivity"))
200 data
->check_connectivity
= 1;
201 else if (skip_prefix(capname
, "refspec ", &arg
)) {
202 refspec_append(&data
->rs
, arg
);
203 } else if (!strcmp(capname
, "connect")) {
205 } else if (!strcmp(capname
, "stateless-connect")) {
206 data
->stateless_connect
= 1;
207 } else if (!strcmp(capname
, "signed-tags")) {
208 data
->signed_tags
= 1;
209 } else if (skip_prefix(capname
, "export-marks ", &arg
)) {
210 data
->export_marks
= xstrdup(arg
);
211 } else if (skip_prefix(capname
, "import-marks ", &arg
)) {
212 data
->import_marks
= xstrdup(arg
);
213 } else if (starts_with(capname
, "no-private-update")) {
214 data
->no_private_update
= 1;
215 } else if (starts_with(capname
, "object-format")) {
216 data
->object_format
= 1;
217 } else if (mandatory
) {
218 die(_("unknown mandatory capability %s; this remote "
219 "helper probably needs newer version of Git"),
223 if (!data
->rs
.nr
&& (data
->import
|| data
->bidi_import
|| data
->export
)) {
224 warning(_("this remote helper should implement refspec capability"));
226 strbuf_release(&buf
);
228 fprintf(stderr
, "Debug: Capabilities complete.\n");
229 standard_options(transport
);
233 static int disconnect_helper(struct transport
*transport
)
235 struct helper_data
*data
= transport
->data
;
240 fprintf(stderr
, "Debug: Disconnecting.\n");
241 if (!data
->no_disconnect_req
) {
243 * Ignore write errors; there's nothing we can do,
244 * since we're about to close the pipe anyway. And the
245 * most likely error is EPIPE due to the helper dying
246 * to report an error itself.
248 sigchain_push(SIGPIPE
, SIG_IGN
);
249 xwrite(data
->helper
->in
, "\n", 1);
250 sigchain_pop(SIGPIPE
);
252 close(data
->helper
->in
);
253 close(data
->helper
->out
);
255 res
= finish_command(data
->helper
);
256 FREE_AND_NULL(data
->helper
);
261 static const char *unsupported_options
[] = {
262 TRANS_OPT_UPLOADPACK
,
263 TRANS_OPT_RECEIVEPACK
,
268 static const char *boolean_options
[] = {
271 TRANS_OPT_FOLLOWTAGS
,
272 TRANS_OPT_DEEPEN_RELATIVE
275 static int strbuf_set_helper_option(struct helper_data
*data
,
281 if (recvline(data
, buf
))
284 if (!strcmp(buf
->buf
, "ok"))
286 else if (starts_with(buf
->buf
, "error"))
288 else if (!strcmp(buf
->buf
, "unsupported"))
291 warning(_("%s unexpectedly said: '%s'"), data
->name
, buf
->buf
);
297 static int string_list_set_helper_option(struct helper_data
*data
,
299 struct string_list
*list
)
301 struct strbuf buf
= STRBUF_INIT
;
304 for (i
= 0; i
< list
->nr
; i
++) {
305 strbuf_addf(&buf
, "option %s ", name
);
306 quote_c_style(list
->items
[i
].string
, &buf
, NULL
, 0);
307 strbuf_addch(&buf
, '\n');
309 if ((ret
= strbuf_set_helper_option(data
, &buf
)))
313 strbuf_release(&buf
);
317 static int set_helper_option(struct transport
*transport
,
318 const char *name
, const char *value
)
320 struct helper_data
*data
= transport
->data
;
321 struct strbuf buf
= STRBUF_INIT
;
322 int i
, ret
, is_bool
= 0;
324 get_helper(transport
);
329 if (!strcmp(name
, "deepen-not"))
330 return string_list_set_helper_option(data
, name
,
331 (struct string_list
*)value
);
333 for (i
= 0; i
< ARRAY_SIZE(unsupported_options
); i
++) {
334 if (!strcmp(name
, unsupported_options
[i
]))
338 for (i
= 0; i
< ARRAY_SIZE(boolean_options
); i
++) {
339 if (!strcmp(name
, boolean_options
[i
])) {
345 strbuf_addf(&buf
, "option %s ", name
);
347 strbuf_addstr(&buf
, value
? "true" : "false");
349 quote_c_style(value
, &buf
, NULL
, 0);
350 strbuf_addch(&buf
, '\n');
352 ret
= strbuf_set_helper_option(data
, &buf
);
353 strbuf_release(&buf
);
357 static void standard_options(struct transport
*t
)
362 set_helper_option(t
, "progress", t
->progress
? "true" : "false");
364 xsnprintf(buf
, sizeof(buf
), "%d", v
+ 1);
365 set_helper_option(t
, "verbosity", buf
);
368 case TRANSPORT_FAMILY_ALL
:
370 * this is already the default,
371 * do not break old remote helpers by setting "all" here
374 case TRANSPORT_FAMILY_IPV4
:
375 set_helper_option(t
, "family", "ipv4");
377 case TRANSPORT_FAMILY_IPV6
:
378 set_helper_option(t
, "family", "ipv6");
383 static int release_helper(struct transport
*transport
)
386 struct helper_data
*data
= transport
->data
;
387 refspec_clear(&data
->rs
);
388 res
= disconnect_helper(transport
);
389 free(transport
->data
);
393 static int fetch_with_fetch(struct transport
*transport
,
394 int nr_heads
, struct ref
**to_fetch
)
396 struct helper_data
*data
= transport
->data
;
398 struct strbuf buf
= STRBUF_INIT
;
400 for (i
= 0; i
< nr_heads
; i
++) {
401 const struct ref
*posn
= to_fetch
[i
];
402 if (posn
->status
& REF_STATUS_UPTODATE
)
405 strbuf_addf(&buf
, "fetch %s %s\n",
406 oid_to_hex(&posn
->old_oid
),
407 posn
->symref
? posn
->symref
: posn
->name
);
410 strbuf_addch(&buf
, '\n');
411 sendline(data
, &buf
);
416 if (recvline(data
, &buf
))
419 if (skip_prefix(buf
.buf
, "lock ", &name
)) {
420 if (transport
->pack_lockfiles
.nr
)
421 warning(_("%s also locked %s"), data
->name
, name
);
423 string_list_append(&transport
->pack_lockfiles
,
426 else if (data
->check_connectivity
&&
427 data
->transport_options
.check_self_contained_and_connected
&&
428 !strcmp(buf
.buf
, "connectivity-ok"))
429 data
->transport_options
.self_contained_and_connected
= 1;
433 warning(_("%s unexpectedly said: '%s'"), data
->name
, buf
.buf
);
435 strbuf_release(&buf
);
437 reprepare_packed_git(the_repository
);
441 static int get_importer(struct transport
*transport
, struct child_process
*fastimport
)
443 struct child_process
*helper
= get_helper(transport
);
444 struct helper_data
*data
= transport
->data
;
445 int cat_blob_fd
, code
;
446 child_process_init(fastimport
);
447 fastimport
->in
= xdup(helper
->out
);
448 strvec_push(&fastimport
->args
, "fast-import");
449 strvec_push(&fastimport
->args
, "--allow-unsafe-features");
450 strvec_push(&fastimport
->args
, debug
? "--stats" : "--quiet");
452 if (data
->bidi_import
) {
453 cat_blob_fd
= xdup(helper
->in
);
454 strvec_pushf(&fastimport
->args
, "--cat-blob-fd=%d", cat_blob_fd
);
456 fastimport
->git_cmd
= 1;
458 code
= start_command(fastimport
);
462 static int get_exporter(struct transport
*transport
,
463 struct child_process
*fastexport
,
464 struct string_list
*revlist_args
)
466 struct helper_data
*data
= transport
->data
;
467 struct child_process
*helper
= get_helper(transport
);
470 child_process_init(fastexport
);
472 /* we need to duplicate helper->in because we want to use it after
473 * fastexport is done with it. */
474 fastexport
->out
= dup(helper
->in
);
475 strvec_push(&fastexport
->args
, "fast-export");
476 strvec_push(&fastexport
->args
, "--use-done-feature");
477 strvec_push(&fastexport
->args
, data
->signed_tags
?
478 "--signed-tags=verbatim" : "--signed-tags=warn-strip");
479 if (data
->export_marks
)
480 strvec_pushf(&fastexport
->args
, "--export-marks=%s.tmp", data
->export_marks
);
481 if (data
->import_marks
)
482 strvec_pushf(&fastexport
->args
, "--import-marks=%s", data
->import_marks
);
484 for (i
= 0; i
< revlist_args
->nr
; i
++)
485 strvec_push(&fastexport
->args
, revlist_args
->items
[i
].string
);
487 fastexport
->git_cmd
= 1;
488 return start_command(fastexport
);
491 static int fetch_with_import(struct transport
*transport
,
492 int nr_heads
, struct ref
**to_fetch
)
494 struct child_process fastimport
;
495 struct helper_data
*data
= transport
->data
;
498 struct strbuf buf
= STRBUF_INIT
;
500 get_helper(transport
);
502 if (get_importer(transport
, &fastimport
))
503 die(_("couldn't run fast-import"));
505 for (i
= 0; i
< nr_heads
; i
++) {
507 if (posn
->status
& REF_STATUS_UPTODATE
)
510 strbuf_addf(&buf
, "import %s\n",
511 posn
->symref
? posn
->symref
: posn
->name
);
512 sendline(data
, &buf
);
516 write_constant(data
->helper
->in
, "\n");
518 * remote-helpers that advertise the bidi-import capability are required to
519 * buffer the complete batch of import commands until this newline before
520 * sending data to fast-import.
521 * These helpers read back data from fast-import on their stdin, which could
522 * be mixed with import commands, otherwise.
525 if (finish_command(&fastimport
))
526 die(_("error while running fast-import"));
529 * The fast-import stream of a remote helper that advertises
530 * the "refspec" capability writes to the refs named after the
531 * right hand side of the first refspec matching each ref we
534 * (If no "refspec" capability was specified, for historical
535 * reasons we default to the equivalent of *:*.)
537 * Store the result in to_fetch[i].old_sha1. Callers such
538 * as "git fetch" can use the value to write feedback to the
539 * terminal, populate FETCH_HEAD, and determine what new value
540 * should be written to peer_ref if the update is a
541 * fast-forward or this is a forced update.
543 for (i
= 0; i
< nr_heads
; i
++) {
544 char *private, *name
;
546 if (posn
->status
& REF_STATUS_UPTODATE
)
548 name
= posn
->symref
? posn
->symref
: posn
->name
;
550 private = apply_refspecs(&data
->rs
, name
);
552 private = xstrdup(name
);
554 if (refs_read_ref(get_main_ref_store(the_repository
), private, &posn
->old_oid
) < 0)
555 die(_("could not read ref %s"), private);
559 strbuf_release(&buf
);
563 static int run_connect(struct transport
*transport
, struct strbuf
*cmdbuf
)
565 struct helper_data
*data
= transport
->data
;
569 struct child_process
*helper
;
571 helper
= get_helper(transport
);
574 * Yes, dup the pipe another time, as we need unbuffered version
575 * of input pipe as FILE*. fclose() closes the underlying fd and
576 * stream buffering only can be changed before first I/O operation
579 duped
= dup(helper
->out
);
581 die_errno(_("can't dup helper output fd"));
582 input
= xfdopen(duped
, "r");
583 setvbuf(input
, NULL
, _IONBF
, 0);
585 sendline(data
, cmdbuf
);
586 if (recvline_fh(input
, cmdbuf
))
589 if (!strcmp(cmdbuf
->buf
, "")) {
590 data
->no_disconnect_req
= 1;
592 fprintf(stderr
, "Debug: Smart transport connection "
595 } else if (!strcmp(cmdbuf
->buf
, "fallback")) {
597 fprintf(stderr
, "Debug: Falling back to dumb "
600 die(_("unknown response to connect: %s"),
608 static int process_connect_service(struct transport
*transport
,
609 const char *name
, const char *exec
)
611 struct helper_data
*data
= transport
->data
;
612 struct strbuf cmdbuf
= STRBUF_INIT
;
616 * Handle --upload-pack and friends. This is fire and forget...
617 * just warn if it fails.
619 if (strcmp(name
, exec
)) {
620 int r
= set_helper_option(transport
, "servpath", exec
);
622 warning(_("setting remote service path not supported by protocol"));
624 warning(_("invalid remote service path"));
628 strbuf_addf(&cmdbuf
, "connect %s\n", name
);
629 ret
= run_connect(transport
, &cmdbuf
);
630 } else if (data
->stateless_connect
&&
631 (get_protocol_version_config() == protocol_v2
) &&
632 (!strcmp("git-upload-pack", name
) ||
633 !strcmp("git-upload-archive", name
))) {
634 strbuf_addf(&cmdbuf
, "stateless-connect %s\n", name
);
635 ret
= run_connect(transport
, &cmdbuf
);
637 transport
->stateless_rpc
= 1;
640 strbuf_release(&cmdbuf
);
644 static int process_connect(struct transport
*transport
,
647 struct helper_data
*data
= transport
->data
;
652 name
= for_push
? "git-receive-pack" : "git-upload-pack";
654 exec
= data
->transport_options
.receivepack
;
656 exec
= data
->transport_options
.uploadpack
;
658 ret
= process_connect_service(transport
, name
, exec
);
660 do_take_over(transport
);
664 static int connect_helper(struct transport
*transport
, const char *name
,
665 const char *exec
, int fd
[2])
667 struct helper_data
*data
= transport
->data
;
669 /* Get_helper so connect is inited. */
670 get_helper(transport
);
672 if (!process_connect_service(transport
, name
, exec
))
673 die(_("can't connect to subservice %s"), name
);
675 fd
[0] = data
->helper
->out
;
676 fd
[1] = data
->helper
->in
;
678 do_take_over(transport
);
682 static struct ref
*get_refs_list_using_list(struct transport
*transport
,
685 static int fetch_refs(struct transport
*transport
,
686 int nr_heads
, struct ref
**to_fetch
)
688 struct helper_data
*data
= transport
->data
;
691 get_helper(transport
);
693 if (process_connect(transport
, 0))
694 return transport
->vtable
->fetch_refs(transport
, nr_heads
, to_fetch
);
697 * If we reach here, then the server, the client, and/or the transport
698 * helper does not support protocol v2. --negotiate-only requires
701 if (data
->transport_options
.acked_commits
) {
702 warning(_("--negotiate-only requires protocol v2"));
706 if (!data
->get_refs_list_called
)
707 get_refs_list_using_list(transport
, 0);
710 for (i
= 0; i
< nr_heads
; i
++)
711 if (!(to_fetch
[i
]->status
& REF_STATUS_UPTODATE
))
717 if (data
->check_connectivity
&&
718 data
->transport_options
.check_self_contained_and_connected
)
719 set_helper_option(transport
, "check-connectivity", "true");
721 if (transport
->cloning
)
722 set_helper_option(transport
, "cloning", "true");
724 if (data
->transport_options
.update_shallow
)
725 set_helper_option(transport
, "update-shallow", "true");
727 if (data
->transport_options
.refetch
)
728 set_helper_option(transport
, "refetch", "true");
730 if (data
->transport_options
.filter_options
.choice
) {
731 const char *spec
= expand_list_objects_filter_spec(
732 &data
->transport_options
.filter_options
);
733 set_helper_option(transport
, "filter", spec
);
736 if (data
->transport_options
.negotiation_tips
)
737 warning("Ignoring --negotiation-tip because the protocol does not support it.");
740 return fetch_with_fetch(transport
, nr_heads
, to_fetch
);
743 return fetch_with_import(transport
, nr_heads
, to_fetch
);
748 struct push_update_ref_state
{
750 struct ref_push_report
*report
;
754 static int push_update_ref_status(struct strbuf
*buf
,
755 struct push_update_ref_state
*state
,
756 struct ref
*remote_refs
)
759 int status
, forced
= 0;
761 if (starts_with(buf
->buf
, "option ")) {
762 struct object_id old_oid
, new_oid
;
763 const char *key
, *val
;
766 if (!state
->hint
|| !(state
->report
|| state
->new_report
))
767 die(_("'option' without a matching 'ok/error' directive"));
768 if (state
->new_report
) {
769 if (!state
->hint
->report
) {
770 CALLOC_ARRAY(state
->hint
->report
, 1);
771 state
->report
= state
->hint
->report
;
773 state
->report
= state
->hint
->report
;
774 while (state
->report
->next
)
775 state
->report
= state
->report
->next
;
776 CALLOC_ARRAY(state
->report
->next
, 1);
777 state
->report
= state
->report
->next
;
779 state
->new_report
= 0;
782 p
= strchr(key
, ' ');
786 if (!strcmp(key
, "refname"))
787 state
->report
->ref_name
= xstrdup_or_null(val
);
788 else if (!strcmp(key
, "old-oid") && val
&&
789 !parse_oid_hex(val
, &old_oid
, &val
))
790 state
->report
->old_oid
= oiddup(&old_oid
);
791 else if (!strcmp(key
, "new-oid") && val
&&
792 !parse_oid_hex(val
, &new_oid
, &val
))
793 state
->report
->new_oid
= oiddup(&new_oid
);
794 else if (!strcmp(key
, "forced-update"))
795 state
->report
->forced_update
= 1;
796 /* Not update remote namespace again. */
800 state
->report
= NULL
;
801 state
->new_report
= 0;
803 if (starts_with(buf
->buf
, "ok ")) {
804 status
= REF_STATUS_OK
;
805 refname
= buf
->buf
+ 3;
806 } else if (starts_with(buf
->buf
, "error ")) {
807 status
= REF_STATUS_REMOTE_REJECT
;
808 refname
= buf
->buf
+ 6;
810 die(_("expected ok/error, helper said '%s'"), buf
->buf
);
812 msg
= strchr(refname
, ' ');
814 struct strbuf msg_buf
= STRBUF_INIT
;
818 if (!unquote_c_style(&msg_buf
, msg
, &end
))
819 msg
= strbuf_detach(&msg_buf
, NULL
);
822 strbuf_release(&msg_buf
);
824 if (!strcmp(msg
, "no match")) {
825 status
= REF_STATUS_NONE
;
828 else if (!strcmp(msg
, "up to date")) {
829 status
= REF_STATUS_UPTODATE
;
832 else if (!strcmp(msg
, "non-fast forward")) {
833 status
= REF_STATUS_REJECT_NONFASTFORWARD
;
836 else if (!strcmp(msg
, "already exists")) {
837 status
= REF_STATUS_REJECT_ALREADY_EXISTS
;
840 else if (!strcmp(msg
, "fetch first")) {
841 status
= REF_STATUS_REJECT_FETCH_FIRST
;
844 else if (!strcmp(msg
, "needs force")) {
845 status
= REF_STATUS_REJECT_NEEDS_FORCE
;
848 else if (!strcmp(msg
, "stale info")) {
849 status
= REF_STATUS_REJECT_STALE
;
852 else if (!strcmp(msg
, "remote ref updated since checkout")) {
853 status
= REF_STATUS_REJECT_REMOTE_UPDATED
;
856 else if (!strcmp(msg
, "forced update")) {
860 else if (!strcmp(msg
, "expecting report")) {
861 status
= REF_STATUS_EXPECTING_REPORT
;
867 state
->hint
= find_ref_by_name(state
->hint
, refname
);
869 state
->hint
= find_ref_by_name(remote_refs
, refname
);
871 warning(_("helper reported unexpected status of %s"), refname
);
875 if (state
->hint
->status
!= REF_STATUS_NONE
) {
877 * Earlier, the ref was marked not to be pushed, so ignore the ref
878 * status reported by the remote helper if the latter is 'no match'.
880 if (status
== REF_STATUS_NONE
)
884 if (status
== REF_STATUS_OK
)
885 state
->new_report
= 1;
886 state
->hint
->status
= status
;
887 state
->hint
->forced_update
|= forced
;
888 state
->hint
->remote_status
= msg
;
889 return !(status
== REF_STATUS_OK
);
892 static int push_update_refs_status(struct helper_data
*data
,
893 struct ref
*remote_refs
,
897 struct ref_push_report
*report
;
898 struct strbuf buf
= STRBUF_INIT
;
899 struct push_update_ref_state state
= { remote_refs
, NULL
, 0 };
902 if (recvline(data
, &buf
)) {
903 strbuf_release(&buf
);
908 push_update_ref_status(&buf
, &state
, remote_refs
);
910 strbuf_release(&buf
);
912 if (flags
& TRANSPORT_PUSH_DRY_RUN
|| !data
->rs
.nr
|| data
->no_private_update
)
915 /* propagate back the update to the remote namespace */
916 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
919 if (ref
->status
!= REF_STATUS_OK
)
923 private = apply_refspecs(&data
->rs
, ref
->name
);
926 refs_update_ref(get_main_ref_store(the_repository
),
927 "update by helper", private,
932 for (report
= ref
->report
; report
; report
= report
->next
) {
933 private = apply_refspecs(&data
->rs
,
939 refs_update_ref(get_main_ref_store(the_repository
),
940 "update by helper", private,
952 static void set_common_push_options(struct transport
*transport
,
953 const char *name
, int flags
)
955 if (flags
& TRANSPORT_PUSH_DRY_RUN
) {
956 if (set_helper_option(transport
, "dry-run", "true") != 0)
957 die(_("helper %s does not support dry-run"), name
);
958 } else if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
) {
959 if (set_helper_option(transport
, TRANS_OPT_PUSH_CERT
, "true") != 0)
960 die(_("helper %s does not support --signed"), name
);
961 } else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
) {
962 if (set_helper_option(transport
, TRANS_OPT_PUSH_CERT
, "if-asked") != 0)
963 die(_("helper %s does not support --signed=if-asked"), name
);
966 if (flags
& TRANSPORT_PUSH_ATOMIC
)
967 if (set_helper_option(transport
, TRANS_OPT_ATOMIC
, "true") != 0)
968 die(_("helper %s does not support --atomic"), name
);
970 if (flags
& TRANSPORT_PUSH_FORCE_IF_INCLUDES
)
971 if (set_helper_option(transport
, TRANS_OPT_FORCE_IF_INCLUDES
, "true") != 0)
972 die(_("helper %s does not support --%s"),
973 name
, TRANS_OPT_FORCE_IF_INCLUDES
);
975 if (flags
& TRANSPORT_PUSH_OPTIONS
) {
976 struct string_list_item
*item
;
977 for_each_string_list_item(item
, transport
->push_options
)
978 if (set_helper_option(transport
, "push-option", item
->string
) != 0)
979 die(_("helper %s does not support 'push-option'"), name
);
983 static int push_refs_with_push(struct transport
*transport
,
984 struct ref
*remote_refs
, int flags
)
986 int force_all
= flags
& TRANSPORT_PUSH_FORCE
;
987 int mirror
= flags
& TRANSPORT_PUSH_MIRROR
;
988 int atomic
= flags
& TRANSPORT_PUSH_ATOMIC
;
989 struct helper_data
*data
= transport
->data
;
990 struct strbuf buf
= STRBUF_INIT
;
992 struct string_list cas_options
= STRING_LIST_INIT_DUP
;
993 struct string_list_item
*cas_option
;
995 get_helper(transport
);
999 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1000 if (!ref
->peer_ref
&& !mirror
)
1003 /* Check for statuses set by set_ref_status_for_push() */
1004 switch (ref
->status
) {
1005 case REF_STATUS_REJECT_NONFASTFORWARD
:
1006 case REF_STATUS_REJECT_STALE
:
1007 case REF_STATUS_REJECT_ALREADY_EXISTS
:
1008 case REF_STATUS_REJECT_REMOTE_UPDATED
:
1010 reject_atomic_push(remote_refs
, mirror
);
1011 string_list_clear(&cas_options
, 0);
1015 case REF_STATUS_UPTODATE
:
1024 strbuf_addstr(&buf
, "push ");
1025 if (!ref
->deletion
) {
1027 strbuf_addch(&buf
, '+');
1029 strbuf_addstr(&buf
, ref
->peer_ref
->name
);
1031 strbuf_addstr(&buf
, oid_to_hex(&ref
->new_oid
));
1033 strbuf_addch(&buf
, ':');
1034 strbuf_addstr(&buf
, ref
->name
);
1035 strbuf_addch(&buf
, '\n');
1038 * The "--force-with-lease" options without explicit
1039 * values to expect have already been expanded into
1040 * the ref->old_oid_expect[] field; we can ignore
1041 * transport->smart_options->cas altogether and instead
1042 * can enumerate them from the refs.
1044 if (ref
->expect_old_sha1
) {
1045 struct strbuf cas
= STRBUF_INIT
;
1046 strbuf_addf(&cas
, "%s:%s",
1047 ref
->name
, oid_to_hex(&ref
->old_oid_expect
));
1048 string_list_append_nodup(&cas_options
,
1049 strbuf_detach(&cas
, NULL
));
1053 string_list_clear(&cas_options
, 0);
1057 for_each_string_list_item(cas_option
, &cas_options
)
1058 set_helper_option(transport
, "cas", cas_option
->string
);
1059 set_common_push_options(transport
, data
->name
, flags
);
1061 strbuf_addch(&buf
, '\n');
1062 sendline(data
, &buf
);
1063 strbuf_release(&buf
);
1064 string_list_clear(&cas_options
, 0);
1066 return push_update_refs_status(data
, remote_refs
, flags
);
1069 static int push_refs_with_export(struct transport
*transport
,
1070 struct ref
*remote_refs
, int flags
)
1073 struct child_process
*helper
, exporter
;
1074 struct helper_data
*data
= transport
->data
;
1075 struct string_list revlist_args
= STRING_LIST_INIT_DUP
;
1076 struct strbuf buf
= STRBUF_INIT
;
1079 die(_("remote-helper doesn't support push; refspec needed"));
1081 set_common_push_options(transport
, data
->name
, flags
);
1082 if (flags
& TRANSPORT_PUSH_FORCE
) {
1083 if (set_helper_option(transport
, "force", "true") != 0)
1084 warning(_("helper %s does not support '--force'"), data
->name
);
1087 helper
= get_helper(transport
);
1089 write_constant(helper
->in
, "export\n");
1091 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1093 struct object_id oid
;
1095 private = apply_refspecs(&data
->rs
, ref
->name
);
1096 if (private && !repo_get_oid(the_repository
, private, &oid
)) {
1097 strbuf_addf(&buf
, "^%s", private);
1098 string_list_append_nodup(&revlist_args
,
1099 strbuf_detach(&buf
, NULL
));
1100 oidcpy(&ref
->old_oid
, &oid
);
1104 if (ref
->peer_ref
) {
1105 if (strcmp(ref
->name
, ref
->peer_ref
->name
)) {
1106 if (!ref
->deletion
) {
1110 /* Follow symbolic refs (mainly for HEAD). */
1111 name
= refs_resolve_ref_unsafe(get_main_ref_store(the_repository
),
1112 ref
->peer_ref
->name
,
1113 RESOLVE_REF_READING
,
1116 if (!name
|| !(flag
& REF_ISSYMREF
))
1117 name
= ref
->peer_ref
->name
;
1119 strbuf_addf(&buf
, "%s:%s", name
, ref
->name
);
1121 strbuf_addf(&buf
, ":%s", ref
->name
);
1123 string_list_append(&revlist_args
, "--refspec");
1124 string_list_append(&revlist_args
, buf
.buf
);
1125 strbuf_release(&buf
);
1128 string_list_append(&revlist_args
, ref
->peer_ref
->name
);
1132 if (get_exporter(transport
, &exporter
, &revlist_args
))
1133 die(_("couldn't run fast-export"));
1135 string_list_clear(&revlist_args
, 1);
1137 if (finish_command(&exporter
))
1138 die(_("error while running fast-export"));
1139 if (push_update_refs_status(data
, remote_refs
, flags
))
1142 if (data
->export_marks
) {
1143 strbuf_addf(&buf
, "%s.tmp", data
->export_marks
);
1144 rename(buf
.buf
, data
->export_marks
);
1145 strbuf_release(&buf
);
1151 static int push_refs(struct transport
*transport
,
1152 struct ref
*remote_refs
, int flags
)
1154 struct helper_data
*data
= transport
->data
;
1156 if (process_connect(transport
, 1))
1157 return transport
->vtable
->push_refs(transport
, remote_refs
, flags
);
1161 _("No refs in common and none specified; doing nothing.\n"
1162 "Perhaps you should specify a branch.\n"));
1167 return push_refs_with_push(transport
, remote_refs
, flags
);
1170 return push_refs_with_export(transport
, remote_refs
, flags
);
1176 static int has_attribute(const char *attrs
, const char *attr
)
1184 const char *space
= strchrnul(attrs
, ' ');
1185 if (len
== space
- attrs
&& !strncmp(attrs
, attr
, len
))
1193 static struct ref
*get_refs_list(struct transport
*transport
, int for_push
,
1194 struct transport_ls_refs_options
*transport_options
)
1196 get_helper(transport
);
1198 if (process_connect(transport
, for_push
))
1199 return transport
->vtable
->get_refs_list(transport
, for_push
,
1202 return get_refs_list_using_list(transport
, for_push
);
1205 static struct ref
*get_refs_list_using_list(struct transport
*transport
,
1208 struct helper_data
*data
= transport
->data
;
1209 struct child_process
*helper
;
1210 struct ref
*ret
= NULL
;
1211 struct ref
**tail
= &ret
;
1213 struct strbuf buf
= STRBUF_INIT
;
1215 data
->get_refs_list_called
= 1;
1216 helper
= get_helper(transport
);
1218 if (data
->object_format
)
1219 set_helper_option(transport
, "object-format", "true");
1221 if (data
->push
&& for_push
)
1222 write_constant(helper
->in
, "list for-push\n");
1224 write_constant(helper
->in
, "list\n");
1228 if (recvline(data
, &buf
))
1233 else if (buf
.buf
[0] == ':') {
1235 if (skip_prefix(buf
.buf
, ":object-format ", &value
)) {
1236 int algo
= hash_algo_by_name(value
);
1237 if (algo
== GIT_HASH_UNKNOWN
)
1238 die(_("unsupported object format '%s'"),
1240 transport
->hash_algo
= &hash_algos
[algo
];
1245 eov
= strchr(buf
.buf
, ' ');
1247 die(_("malformed response in ref list: %s"), buf
.buf
);
1248 eon
= strchr(eov
+ 1, ' ');
1252 *tail
= alloc_ref(eov
+ 1);
1253 if (buf
.buf
[0] == '@')
1254 (*tail
)->symref
= xstrdup(buf
.buf
+ 1);
1255 else if (buf
.buf
[0] != '?')
1256 get_oid_hex_algop(buf
.buf
, &(*tail
)->old_oid
, transport
->hash_algo
);
1258 if (has_attribute(eon
+ 1, "unchanged")) {
1259 (*tail
)->status
|= REF_STATUS_UPTODATE
;
1260 if (refs_read_ref(get_main_ref_store(the_repository
), (*tail
)->name
, &(*tail
)->old_oid
) < 0)
1261 die(_("could not read ref %s"),
1265 tail
= &((*tail
)->next
);
1268 fprintf(stderr
, "Debug: Read ref listing.\n");
1269 strbuf_release(&buf
);
1271 for (posn
= ret
; posn
; posn
= posn
->next
)
1272 resolve_remote_symref(posn
, ret
);
1277 static int get_bundle_uri(struct transport
*transport
)
1279 get_helper(transport
);
1281 if (process_connect(transport
, 0))
1282 return transport
->vtable
->get_bundle_uri(transport
);
1287 static struct transport_vtable vtable
= {
1288 .set_option
= set_helper_option
,
1289 .get_refs_list
= get_refs_list
,
1290 .get_bundle_uri
= get_bundle_uri
,
1291 .fetch_refs
= fetch_refs
,
1292 .push_refs
= push_refs
,
1293 .connect
= connect_helper
,
1294 .disconnect
= release_helper
1297 int transport_helper_init(struct transport
*transport
, const char *name
)
1299 struct helper_data
*data
= xcalloc(1, sizeof(*data
));
1302 transport_check_allowed(name
);
1304 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
1307 list_objects_filter_init(&data
->transport_options
.filter_options
);
1309 transport
->data
= data
;
1310 transport
->vtable
= &vtable
;
1311 transport
->smart_options
= &(data
->transport_options
);
1316 * Linux pipes can buffer 65536 bytes at once (and most platforms can
1317 * buffer less), so attempt reads and writes with up to that size.
1319 #define BUFFERSIZE 65536
1320 /* This should be enough to hold debugging message. */
1321 #define PBUFFERSIZE 8192
1323 /* Print bidirectional transfer loop debug message. */
1324 __attribute__((format (printf
, 1, 2)))
1325 static void transfer_debug(const char *fmt
, ...)
1328 * NEEDSWORK: This function is sometimes used from multiple threads, and
1329 * we end up using debug_enabled racily. That "should not matter" since
1330 * we always write the same value, but it's still wrong. This function
1331 * is listed in .tsan-suppressions for the time being.
1335 char msgbuf
[PBUFFERSIZE
];
1336 static int debug_enabled
= -1;
1338 if (debug_enabled
< 0)
1339 debug_enabled
= getenv("GIT_TRANSLOOP_DEBUG") ? 1 : 0;
1343 va_start(args
, fmt
);
1344 vsnprintf(msgbuf
, PBUFFERSIZE
, fmt
, args
);
1346 fprintf(stderr
, "Transfer loop debugging: %s\n", msgbuf
);
1349 /* Stream state: More data may be coming in this direction. */
1350 #define SSTATE_TRANSFERRING 0
1352 * Stream state: No more data coming in this direction, flushing rest of
1355 #define SSTATE_FLUSHING 1
1356 /* Stream state: Transfer in this direction finished. */
1357 #define SSTATE_FINISHED 2
1359 #define STATE_NEEDS_READING(state) ((state) <= SSTATE_TRANSFERRING)
1360 #define STATE_NEEDS_WRITING(state) ((state) <= SSTATE_FLUSHING)
1361 #define STATE_NEEDS_CLOSING(state) ((state) == SSTATE_FLUSHING)
1363 /* Unidirectional transfer. */
1364 struct unidirectional_transfer
{
1369 /* Is source socket? */
1371 /* Is destination socket? */
1373 /* Transfer state (TRANSFERRING/FLUSHING/FINISHED) */
1376 char buf
[BUFFERSIZE
];
1379 /* Name of source. */
1380 const char *src_name
;
1381 /* Name of destination. */
1382 const char *dest_name
;
1385 /* Closes the target (for writing) if transfer has finished. */
1386 static void udt_close_if_finished(struct unidirectional_transfer
*t
)
1388 if (STATE_NEEDS_CLOSING(t
->state
) && !t
->bufuse
) {
1389 t
->state
= SSTATE_FINISHED
;
1390 if (t
->dest_is_sock
)
1391 shutdown(t
->dest
, SHUT_WR
);
1394 transfer_debug("Closed %s.", t
->dest_name
);
1399 * Tries to read data from source into buffer. If buffer is full,
1400 * no data is read. Returns 0 on success, -1 on error.
1402 static int udt_do_read(struct unidirectional_transfer
*t
)
1406 if (t
->bufuse
== BUFFERSIZE
)
1407 return 0; /* No space for more. */
1409 transfer_debug("%s is readable", t
->src_name
);
1410 bytes
= xread(t
->src
, t
->buf
+ t
->bufuse
, BUFFERSIZE
- t
->bufuse
);
1412 error_errno(_("read(%s) failed"), t
->src_name
);
1414 } else if (bytes
== 0) {
1415 transfer_debug("%s EOF (with %i bytes in buffer)",
1416 t
->src_name
, (int)t
->bufuse
);
1417 t
->state
= SSTATE_FLUSHING
;
1418 } else if (bytes
> 0) {
1420 transfer_debug("Read %i bytes from %s (buffer now at %i)",
1421 (int)bytes
, t
->src_name
, (int)t
->bufuse
);
1426 /* Tries to write data from buffer into destination. If buffer is empty,
1427 * no data is written. Returns 0 on success, -1 on error.
1429 static int udt_do_write(struct unidirectional_transfer
*t
)
1434 return 0; /* Nothing to write. */
1436 transfer_debug("%s is writable", t
->dest_name
);
1437 bytes
= xwrite(t
->dest
, t
->buf
, t
->bufuse
);
1439 error_errno(_("write(%s) failed"), t
->dest_name
);
1441 } else if (bytes
> 0) {
1444 memmove(t
->buf
, t
->buf
+ bytes
, t
->bufuse
);
1445 transfer_debug("Wrote %i bytes to %s (buffer now at %i)",
1446 (int)bytes
, t
->dest_name
, (int)t
->bufuse
);
1452 /* State of bidirectional transfer loop. */
1453 struct bidirectional_transfer_state
{
1454 /* Direction from program to git. */
1455 struct unidirectional_transfer ptg
;
1456 /* Direction from git to program. */
1457 struct unidirectional_transfer gtp
;
1460 static void *udt_copy_task_routine(void *udt
)
1462 struct unidirectional_transfer
*t
= (struct unidirectional_transfer
*)udt
;
1463 while (t
->state
!= SSTATE_FINISHED
) {
1464 if (STATE_NEEDS_READING(t
->state
))
1467 if (STATE_NEEDS_WRITING(t
->state
))
1468 if (udt_do_write(t
))
1470 if (STATE_NEEDS_CLOSING(t
->state
))
1471 udt_close_if_finished(t
);
1473 return udt
; /* Just some non-NULL value. */
1479 * Join thread, with appropriate errors on failure. Name is name for the
1480 * thread (for error messages). Returns 0 on success, 1 on failure.
1482 static int tloop_join(pthread_t thread
, const char *name
)
1486 err
= pthread_join(thread
, &tret
);
1488 error(_("%s thread failed"), name
);
1492 error(_("%s thread failed to join: %s"), name
, strerror(err
));
1499 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1502 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1504 pthread_t gtp_thread
;
1505 pthread_t ptg_thread
;
1508 err
= pthread_create(>p_thread
, NULL
, udt_copy_task_routine
,
1511 die(_("can't start thread for copying data: %s"), strerror(err
));
1512 err
= pthread_create(&ptg_thread
, NULL
, udt_copy_task_routine
,
1515 die(_("can't start thread for copying data: %s"), strerror(err
));
1517 ret
|= tloop_join(gtp_thread
, "Git to program copy");
1518 ret
|= tloop_join(ptg_thread
, "Program to git copy");
1523 /* Close the source and target (for writing) for transfer. */
1524 static void udt_kill_transfer(struct unidirectional_transfer
*t
)
1526 t
->state
= SSTATE_FINISHED
;
1528 * Socket read end left open isn't a disaster if nobody
1529 * attempts to read from it (mingw compat headers do not
1532 * We can't fully close the socket since otherwise gtp
1533 * task would first close the socket it sends data to
1534 * while closing the ptg file descriptors.
1536 if (!t
->src_is_sock
)
1538 if (t
->dest_is_sock
)
1539 shutdown(t
->dest
, SHUT_WR
);
1545 * Join process, with appropriate errors on failure. Name is name for the
1546 * process (for error messages). Returns 0 on success, 1 on failure.
1548 static int tloop_join(pid_t pid
, const char *name
)
1551 if (waitpid(pid
, &tret
, 0) < 0) {
1552 error_errno(_("%s process failed to wait"), name
);
1555 if (!WIFEXITED(tret
) || WEXITSTATUS(tret
)) {
1556 error(_("%s process failed"), name
);
1563 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1566 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1571 /* Fork thread #1: git to program. */
1574 die_errno(_("can't start thread for copying data"));
1575 else if (pid1
== 0) {
1576 udt_kill_transfer(&s
->ptg
);
1577 exit(udt_copy_task_routine(&s
->gtp
) ? 0 : 1);
1580 /* Fork thread #2: program to git. */
1583 die_errno(_("can't start thread for copying data"));
1584 else if (pid2
== 0) {
1585 udt_kill_transfer(&s
->gtp
);
1586 exit(udt_copy_task_routine(&s
->ptg
) ? 0 : 1);
1590 * Close both streams in parent as to not interfere with
1591 * end of file detection and wait for both tasks to finish.
1593 udt_kill_transfer(&s
->gtp
);
1594 udt_kill_transfer(&s
->ptg
);
1595 ret
|= tloop_join(pid1
, "Git to program copy");
1596 ret
|= tloop_join(pid2
, "Program to git copy");
1602 * Copies data from stdin to output and from input to stdout simultaneously.
1603 * Additionally filtering through given filter. If filter is NULL, uses
1606 int bidirectional_transfer_loop(int input
, int output
)
1608 struct bidirectional_transfer_state state
;
1610 /* Fill the state fields. */
1611 state
.ptg
.src
= input
;
1613 state
.ptg
.src_is_sock
= (input
== output
);
1614 state
.ptg
.dest_is_sock
= 0;
1615 state
.ptg
.state
= SSTATE_TRANSFERRING
;
1616 state
.ptg
.bufuse
= 0;
1617 state
.ptg
.src_name
= "remote input";
1618 state
.ptg
.dest_name
= "stdout";
1621 state
.gtp
.dest
= output
;
1622 state
.gtp
.src_is_sock
= 0;
1623 state
.gtp
.dest_is_sock
= (input
== output
);
1624 state
.gtp
.state
= SSTATE_TRANSFERRING
;
1625 state
.gtp
.bufuse
= 0;
1626 state
.gtp
.src_name
= "stdin";
1627 state
.gtp
.dest_name
= "remote output";
1629 return tloop_spawnwait_tasks(&state
);
1632 void reject_atomic_push(struct ref
*remote_refs
, int mirror_mode
)
1636 /* Mark other refs as failed */
1637 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1638 if (!ref
->peer_ref
&& !mirror_mode
)
1641 switch (ref
->status
) {
1642 case REF_STATUS_NONE
:
1644 case REF_STATUS_EXPECTING_REPORT
:
1645 ref
->status
= REF_STATUS_ATOMIC_PUSH_FAILED
;
1648 break; /* do nothing */