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"
25 struct child_process
*helper
;
34 stateless_connect
: 1,
36 check_connectivity
: 1,
37 no_disconnect_req
: 1,
38 no_private_update
: 1,
42 * As an optimization, the transport code may invoke fetch before
43 * get_refs_list. If this happens, and if the transport helper doesn't
44 * support connect or stateless_connect, we need to invoke
45 * get_refs_list ourselves if we haven't already done so. Keep track of
46 * whether we have invoked get_refs_list.
48 unsigned get_refs_list_called
: 1;
52 /* These go from remote name (as in "list") to private name */
54 /* Transport options for fetch-pack/send-pack (should one of
57 struct git_transport_options transport_options
;
60 static void sendline(struct helper_data
*helper
, struct strbuf
*buffer
)
63 fprintf(stderr
, "Debug: Remote helper: -> %s", buffer
->buf
);
64 if (write_in_full(helper
->helper
->in
, buffer
->buf
, buffer
->len
) < 0)
65 die_errno(_("full write to remote helper failed"));
68 static int recvline_fh(FILE *helper
, struct strbuf
*buffer
)
72 fprintf(stderr
, "Debug: Remote helper: Waiting...\n");
73 if (strbuf_getline(buffer
, helper
) == EOF
) {
75 fprintf(stderr
, "Debug: Remote helper quit.\n");
80 fprintf(stderr
, "Debug: Remote helper: <- %s\n", buffer
->buf
);
84 static int recvline(struct helper_data
*helper
, struct strbuf
*buffer
)
86 return recvline_fh(helper
->out
, buffer
);
89 static void write_constant(int fd
, const char *str
)
92 fprintf(stderr
, "Debug: Remote helper: -> %s", str
);
93 if (write_in_full(fd
, str
, strlen(str
)) < 0)
94 die_errno(_("full write to remote helper failed"));
97 static const char *remove_ext_force(const char *url
)
100 const char *colon
= strchr(url
, ':');
101 if (colon
&& colon
[1] == ':')
107 static void do_take_over(struct transport
*transport
)
109 struct helper_data
*data
;
110 data
= (struct helper_data
*)transport
->data
;
111 transport_take_over(transport
, data
->helper
);
116 static void standard_options(struct transport
*t
);
118 static struct child_process
*get_helper(struct transport
*transport
)
120 struct helper_data
*data
= transport
->data
;
121 struct strbuf buf
= STRBUF_INIT
;
122 struct child_process
*helper
;
129 helper
= xmalloc(sizeof(*helper
));
130 child_process_init(helper
);
134 strvec_pushf(&helper
->args
, "remote-%s", data
->name
);
135 strvec_push(&helper
->args
, transport
->remote
->name
);
136 strvec_push(&helper
->args
, remove_ext_force(transport
->url
));
138 helper
->silent_exec_failure
= 1;
141 strvec_pushf(&helper
->env
, "%s=%s",
142 GIT_DIR_ENVIRONMENT
, get_git_dir());
144 helper
->trace2_child_class
= helper
->args
.v
[0]; /* "remote-<name>" */
146 code
= start_command(helper
);
147 if (code
< 0 && errno
== ENOENT
)
148 die(_("unable to find remote helper for '%s'"), data
->name
);
152 data
->helper
= helper
;
153 data
->no_disconnect_req
= 0;
154 refspec_init(&data
->rs
, REFSPEC_FETCH
);
157 * Open the output as FILE* so strbuf_getline_*() family of
158 * functions can be used.
159 * Do this with duped fd because fclose() will close the fd,
160 * and stuff like taking over will require the fd to remain.
162 duped
= dup(helper
->out
);
164 die_errno(_("can't dup helper output fd"));
165 data
->out
= xfdopen(duped
, "r");
167 write_constant(helper
->in
, "capabilities\n");
170 const char *capname
, *arg
;
172 if (recvline(data
, &buf
))
178 if (*buf
.buf
== '*') {
179 capname
= buf
.buf
+ 1;
185 fprintf(stderr
, "Debug: Got cap %s\n", capname
);
186 if (!strcmp(capname
, "fetch"))
188 else if (!strcmp(capname
, "option"))
190 else if (!strcmp(capname
, "push"))
192 else if (!strcmp(capname
, "import"))
194 else if (!strcmp(capname
, "bidi-import"))
195 data
->bidi_import
= 1;
196 else if (!strcmp(capname
, "export"))
198 else if (!strcmp(capname
, "check-connectivity"))
199 data
->check_connectivity
= 1;
200 else if (skip_prefix(capname
, "refspec ", &arg
)) {
201 refspec_append(&data
->rs
, arg
);
202 } else if (!strcmp(capname
, "connect")) {
204 } else if (!strcmp(capname
, "stateless-connect")) {
205 data
->stateless_connect
= 1;
206 } else if (!strcmp(capname
, "signed-tags")) {
207 data
->signed_tags
= 1;
208 } else if (skip_prefix(capname
, "export-marks ", &arg
)) {
209 data
->export_marks
= xstrdup(arg
);
210 } else if (skip_prefix(capname
, "import-marks ", &arg
)) {
211 data
->import_marks
= xstrdup(arg
);
212 } else if (starts_with(capname
, "no-private-update")) {
213 data
->no_private_update
= 1;
214 } else if (starts_with(capname
, "object-format")) {
215 data
->object_format
= 1;
216 } else if (mandatory
) {
217 die(_("unknown mandatory capability %s; this remote "
218 "helper probably needs newer version of Git"),
222 if (!data
->rs
.nr
&& (data
->import
|| data
->bidi_import
|| data
->export
)) {
223 warning(_("this remote helper should implement refspec capability"));
225 strbuf_release(&buf
);
227 fprintf(stderr
, "Debug: Capabilities complete.\n");
228 standard_options(transport
);
232 static int disconnect_helper(struct transport
*transport
)
234 struct helper_data
*data
= transport
->data
;
239 fprintf(stderr
, "Debug: Disconnecting.\n");
240 if (!data
->no_disconnect_req
) {
242 * Ignore write errors; there's nothing we can do,
243 * since we're about to close the pipe anyway. And the
244 * most likely error is EPIPE due to the helper dying
245 * to report an error itself.
247 sigchain_push(SIGPIPE
, SIG_IGN
);
248 xwrite(data
->helper
->in
, "\n", 1);
249 sigchain_pop(SIGPIPE
);
251 close(data
->helper
->in
);
252 close(data
->helper
->out
);
254 res
= finish_command(data
->helper
);
255 FREE_AND_NULL(data
->helper
);
260 static const char *unsupported_options
[] = {
261 TRANS_OPT_UPLOADPACK
,
262 TRANS_OPT_RECEIVEPACK
,
267 static const char *boolean_options
[] = {
270 TRANS_OPT_FOLLOWTAGS
,
271 TRANS_OPT_DEEPEN_RELATIVE
274 static int strbuf_set_helper_option(struct helper_data
*data
,
280 if (recvline(data
, buf
))
283 if (!strcmp(buf
->buf
, "ok"))
285 else if (starts_with(buf
->buf
, "error"))
287 else if (!strcmp(buf
->buf
, "unsupported"))
290 warning(_("%s unexpectedly said: '%s'"), data
->name
, buf
->buf
);
296 static int string_list_set_helper_option(struct helper_data
*data
,
298 struct string_list
*list
)
300 struct strbuf buf
= STRBUF_INIT
;
303 for (i
= 0; i
< list
->nr
; i
++) {
304 strbuf_addf(&buf
, "option %s ", name
);
305 quote_c_style(list
->items
[i
].string
, &buf
, NULL
, 0);
306 strbuf_addch(&buf
, '\n');
308 if ((ret
= strbuf_set_helper_option(data
, &buf
)))
312 strbuf_release(&buf
);
316 static int set_helper_option(struct transport
*transport
,
317 const char *name
, const char *value
)
319 struct helper_data
*data
= transport
->data
;
320 struct strbuf buf
= STRBUF_INIT
;
321 int i
, ret
, is_bool
= 0;
323 get_helper(transport
);
328 if (!strcmp(name
, "deepen-not"))
329 return string_list_set_helper_option(data
, name
,
330 (struct string_list
*)value
);
332 for (i
= 0; i
< ARRAY_SIZE(unsupported_options
); i
++) {
333 if (!strcmp(name
, unsupported_options
[i
]))
337 for (i
= 0; i
< ARRAY_SIZE(boolean_options
); i
++) {
338 if (!strcmp(name
, boolean_options
[i
])) {
344 strbuf_addf(&buf
, "option %s ", name
);
346 strbuf_addstr(&buf
, value
? "true" : "false");
348 quote_c_style(value
, &buf
, NULL
, 0);
349 strbuf_addch(&buf
, '\n');
351 ret
= strbuf_set_helper_option(data
, &buf
);
352 strbuf_release(&buf
);
356 static void standard_options(struct transport
*t
)
361 set_helper_option(t
, "progress", t
->progress
? "true" : "false");
363 xsnprintf(buf
, sizeof(buf
), "%d", v
+ 1);
364 set_helper_option(t
, "verbosity", buf
);
367 case TRANSPORT_FAMILY_ALL
:
369 * this is already the default,
370 * do not break old remote helpers by setting "all" here
373 case TRANSPORT_FAMILY_IPV4
:
374 set_helper_option(t
, "family", "ipv4");
376 case TRANSPORT_FAMILY_IPV6
:
377 set_helper_option(t
, "family", "ipv6");
382 static int release_helper(struct transport
*transport
)
385 struct helper_data
*data
= transport
->data
;
386 refspec_clear(&data
->rs
);
387 res
= disconnect_helper(transport
);
388 free(transport
->data
);
392 static int fetch_with_fetch(struct transport
*transport
,
393 int nr_heads
, struct ref
**to_fetch
)
395 struct helper_data
*data
= transport
->data
;
397 struct strbuf buf
= STRBUF_INIT
;
399 for (i
= 0; i
< nr_heads
; i
++) {
400 const struct ref
*posn
= to_fetch
[i
];
401 if (posn
->status
& REF_STATUS_UPTODATE
)
404 strbuf_addf(&buf
, "fetch %s %s\n",
405 oid_to_hex(&posn
->old_oid
),
406 posn
->symref
? posn
->symref
: posn
->name
);
409 strbuf_addch(&buf
, '\n');
410 sendline(data
, &buf
);
415 if (recvline(data
, &buf
))
418 if (skip_prefix(buf
.buf
, "lock ", &name
)) {
419 if (transport
->pack_lockfiles
.nr
)
420 warning(_("%s also locked %s"), data
->name
, name
);
422 string_list_append(&transport
->pack_lockfiles
,
425 else if (data
->check_connectivity
&&
426 data
->transport_options
.check_self_contained_and_connected
&&
427 !strcmp(buf
.buf
, "connectivity-ok"))
428 data
->transport_options
.self_contained_and_connected
= 1;
432 warning(_("%s unexpectedly said: '%s'"), data
->name
, buf
.buf
);
434 strbuf_release(&buf
);
438 static int get_importer(struct transport
*transport
, struct child_process
*fastimport
)
440 struct child_process
*helper
= get_helper(transport
);
441 struct helper_data
*data
= transport
->data
;
442 int cat_blob_fd
, code
;
443 child_process_init(fastimport
);
444 fastimport
->in
= xdup(helper
->out
);
445 strvec_push(&fastimport
->args
, "fast-import");
446 strvec_push(&fastimport
->args
, "--allow-unsafe-features");
447 strvec_push(&fastimport
->args
, debug
? "--stats" : "--quiet");
449 if (data
->bidi_import
) {
450 cat_blob_fd
= xdup(helper
->in
);
451 strvec_pushf(&fastimport
->args
, "--cat-blob-fd=%d", cat_blob_fd
);
453 fastimport
->git_cmd
= 1;
455 code
= start_command(fastimport
);
459 static int get_exporter(struct transport
*transport
,
460 struct child_process
*fastexport
,
461 struct string_list
*revlist_args
)
463 struct helper_data
*data
= transport
->data
;
464 struct child_process
*helper
= get_helper(transport
);
467 child_process_init(fastexport
);
469 /* we need to duplicate helper->in because we want to use it after
470 * fastexport is done with it. */
471 fastexport
->out
= dup(helper
->in
);
472 strvec_push(&fastexport
->args
, "fast-export");
473 strvec_push(&fastexport
->args
, "--use-done-feature");
474 strvec_push(&fastexport
->args
, data
->signed_tags
?
475 "--signed-tags=verbatim" : "--signed-tags=warn-strip");
476 if (data
->export_marks
)
477 strvec_pushf(&fastexport
->args
, "--export-marks=%s.tmp", data
->export_marks
);
478 if (data
->import_marks
)
479 strvec_pushf(&fastexport
->args
, "--import-marks=%s", data
->import_marks
);
481 for (i
= 0; i
< revlist_args
->nr
; i
++)
482 strvec_push(&fastexport
->args
, revlist_args
->items
[i
].string
);
484 fastexport
->git_cmd
= 1;
485 return start_command(fastexport
);
488 static int fetch_with_import(struct transport
*transport
,
489 int nr_heads
, struct ref
**to_fetch
)
491 struct child_process fastimport
;
492 struct helper_data
*data
= transport
->data
;
495 struct strbuf buf
= STRBUF_INIT
;
497 get_helper(transport
);
499 if (get_importer(transport
, &fastimport
))
500 die(_("couldn't run fast-import"));
502 for (i
= 0; i
< nr_heads
; i
++) {
504 if (posn
->status
& REF_STATUS_UPTODATE
)
507 strbuf_addf(&buf
, "import %s\n",
508 posn
->symref
? posn
->symref
: posn
->name
);
509 sendline(data
, &buf
);
513 write_constant(data
->helper
->in
, "\n");
515 * remote-helpers that advertise the bidi-import capability are required to
516 * buffer the complete batch of import commands until this newline before
517 * sending data to fast-import.
518 * These helpers read back data from fast-import on their stdin, which could
519 * be mixed with import commands, otherwise.
522 if (finish_command(&fastimport
))
523 die(_("error while running fast-import"));
526 * The fast-import stream of a remote helper that advertises
527 * the "refspec" capability writes to the refs named after the
528 * right hand side of the first refspec matching each ref we
531 * (If no "refspec" capability was specified, for historical
532 * reasons we default to the equivalent of *:*.)
534 * Store the result in to_fetch[i].old_sha1. Callers such
535 * as "git fetch" can use the value to write feedback to the
536 * terminal, populate FETCH_HEAD, and determine what new value
537 * should be written to peer_ref if the update is a
538 * fast-forward or this is a forced update.
540 for (i
= 0; i
< nr_heads
; i
++) {
541 char *private, *name
;
543 if (posn
->status
& REF_STATUS_UPTODATE
)
545 name
= posn
->symref
? posn
->symref
: posn
->name
;
547 private = apply_refspecs(&data
->rs
, name
);
549 private = xstrdup(name
);
551 if (read_ref(private, &posn
->old_oid
) < 0)
552 die(_("could not read ref %s"), private);
556 strbuf_release(&buf
);
560 static int run_connect(struct transport
*transport
, struct strbuf
*cmdbuf
)
562 struct helper_data
*data
= transport
->data
;
566 struct child_process
*helper
;
568 helper
= get_helper(transport
);
571 * Yes, dup the pipe another time, as we need unbuffered version
572 * of input pipe as FILE*. fclose() closes the underlying fd and
573 * stream buffering only can be changed before first I/O operation
576 duped
= dup(helper
->out
);
578 die_errno(_("can't dup helper output fd"));
579 input
= xfdopen(duped
, "r");
580 setvbuf(input
, NULL
, _IONBF
, 0);
582 sendline(data
, cmdbuf
);
583 if (recvline_fh(input
, cmdbuf
))
586 if (!strcmp(cmdbuf
->buf
, "")) {
587 data
->no_disconnect_req
= 1;
589 fprintf(stderr
, "Debug: Smart transport connection "
592 } else if (!strcmp(cmdbuf
->buf
, "fallback")) {
594 fprintf(stderr
, "Debug: Falling back to dumb "
597 die(_("unknown response to connect: %s"),
605 static int process_connect_service(struct transport
*transport
,
606 const char *name
, const char *exec
)
608 struct helper_data
*data
= transport
->data
;
609 struct strbuf cmdbuf
= STRBUF_INIT
;
613 * Handle --upload-pack and friends. This is fire and forget...
614 * just warn if it fails.
616 if (strcmp(name
, exec
)) {
617 int r
= set_helper_option(transport
, "servpath", exec
);
619 warning(_("setting remote service path not supported by protocol"));
621 warning(_("invalid remote service path"));
625 strbuf_addf(&cmdbuf
, "connect %s\n", name
);
626 ret
= run_connect(transport
, &cmdbuf
);
627 } else if (data
->stateless_connect
&&
628 (get_protocol_version_config() == protocol_v2
) &&
629 (!strcmp("git-upload-pack", name
) ||
630 !strcmp("git-upload-archive", name
))) {
631 strbuf_addf(&cmdbuf
, "stateless-connect %s\n", name
);
632 ret
= run_connect(transport
, &cmdbuf
);
634 transport
->stateless_rpc
= 1;
637 strbuf_release(&cmdbuf
);
641 static int process_connect(struct transport
*transport
,
644 struct helper_data
*data
= transport
->data
;
649 name
= for_push
? "git-receive-pack" : "git-upload-pack";
651 exec
= data
->transport_options
.receivepack
;
653 exec
= data
->transport_options
.uploadpack
;
655 ret
= process_connect_service(transport
, name
, exec
);
657 do_take_over(transport
);
661 static int connect_helper(struct transport
*transport
, const char *name
,
662 const char *exec
, int fd
[2])
664 struct helper_data
*data
= transport
->data
;
666 /* Get_helper so connect is inited. */
667 get_helper(transport
);
669 if (!process_connect_service(transport
, name
, exec
))
670 die(_("can't connect to subservice %s"), name
);
672 fd
[0] = data
->helper
->out
;
673 fd
[1] = data
->helper
->in
;
675 do_take_over(transport
);
679 static struct ref
*get_refs_list_using_list(struct transport
*transport
,
682 static int fetch_refs(struct transport
*transport
,
683 int nr_heads
, struct ref
**to_fetch
)
685 struct helper_data
*data
= transport
->data
;
688 get_helper(transport
);
690 if (process_connect(transport
, 0))
691 return transport
->vtable
->fetch_refs(transport
, nr_heads
, to_fetch
);
694 * If we reach here, then the server, the client, and/or the transport
695 * helper does not support protocol v2. --negotiate-only requires
698 if (data
->transport_options
.acked_commits
) {
699 warning(_("--negotiate-only requires protocol v2"));
703 if (!data
->get_refs_list_called
)
704 get_refs_list_using_list(transport
, 0);
707 for (i
= 0; i
< nr_heads
; i
++)
708 if (!(to_fetch
[i
]->status
& REF_STATUS_UPTODATE
))
714 if (data
->check_connectivity
&&
715 data
->transport_options
.check_self_contained_and_connected
)
716 set_helper_option(transport
, "check-connectivity", "true");
718 if (transport
->cloning
)
719 set_helper_option(transport
, "cloning", "true");
721 if (data
->transport_options
.update_shallow
)
722 set_helper_option(transport
, "update-shallow", "true");
724 if (data
->transport_options
.refetch
)
725 set_helper_option(transport
, "refetch", "true");
727 if (data
->transport_options
.filter_options
.choice
) {
728 const char *spec
= expand_list_objects_filter_spec(
729 &data
->transport_options
.filter_options
);
730 set_helper_option(transport
, "filter", spec
);
733 if (data
->transport_options
.negotiation_tips
)
734 warning("Ignoring --negotiation-tip because the protocol does not support it.");
737 return fetch_with_fetch(transport
, nr_heads
, to_fetch
);
740 return fetch_with_import(transport
, nr_heads
, to_fetch
);
745 struct push_update_ref_state
{
747 struct ref_push_report
*report
;
751 static int push_update_ref_status(struct strbuf
*buf
,
752 struct push_update_ref_state
*state
,
753 struct ref
*remote_refs
)
756 int status
, forced
= 0;
758 if (starts_with(buf
->buf
, "option ")) {
759 struct object_id old_oid
, new_oid
;
760 const char *key
, *val
;
763 if (!state
->hint
|| !(state
->report
|| state
->new_report
))
764 die(_("'option' without a matching 'ok/error' directive"));
765 if (state
->new_report
) {
766 if (!state
->hint
->report
) {
767 CALLOC_ARRAY(state
->hint
->report
, 1);
768 state
->report
= state
->hint
->report
;
770 state
->report
= state
->hint
->report
;
771 while (state
->report
->next
)
772 state
->report
= state
->report
->next
;
773 CALLOC_ARRAY(state
->report
->next
, 1);
774 state
->report
= state
->report
->next
;
776 state
->new_report
= 0;
779 p
= strchr(key
, ' ');
783 if (!strcmp(key
, "refname"))
784 state
->report
->ref_name
= xstrdup_or_null(val
);
785 else if (!strcmp(key
, "old-oid") && val
&&
786 !parse_oid_hex(val
, &old_oid
, &val
))
787 state
->report
->old_oid
= oiddup(&old_oid
);
788 else if (!strcmp(key
, "new-oid") && val
&&
789 !parse_oid_hex(val
, &new_oid
, &val
))
790 state
->report
->new_oid
= oiddup(&new_oid
);
791 else if (!strcmp(key
, "forced-update"))
792 state
->report
->forced_update
= 1;
793 /* Not update remote namespace again. */
797 state
->report
= NULL
;
798 state
->new_report
= 0;
800 if (starts_with(buf
->buf
, "ok ")) {
801 status
= REF_STATUS_OK
;
802 refname
= buf
->buf
+ 3;
803 } else if (starts_with(buf
->buf
, "error ")) {
804 status
= REF_STATUS_REMOTE_REJECT
;
805 refname
= buf
->buf
+ 6;
807 die(_("expected ok/error, helper said '%s'"), buf
->buf
);
809 msg
= strchr(refname
, ' ');
811 struct strbuf msg_buf
= STRBUF_INIT
;
815 if (!unquote_c_style(&msg_buf
, msg
, &end
))
816 msg
= strbuf_detach(&msg_buf
, NULL
);
819 strbuf_release(&msg_buf
);
821 if (!strcmp(msg
, "no match")) {
822 status
= REF_STATUS_NONE
;
825 else if (!strcmp(msg
, "up to date")) {
826 status
= REF_STATUS_UPTODATE
;
829 else if (!strcmp(msg
, "non-fast forward")) {
830 status
= REF_STATUS_REJECT_NONFASTFORWARD
;
833 else if (!strcmp(msg
, "already exists")) {
834 status
= REF_STATUS_REJECT_ALREADY_EXISTS
;
837 else if (!strcmp(msg
, "fetch first")) {
838 status
= REF_STATUS_REJECT_FETCH_FIRST
;
841 else if (!strcmp(msg
, "needs force")) {
842 status
= REF_STATUS_REJECT_NEEDS_FORCE
;
845 else if (!strcmp(msg
, "stale info")) {
846 status
= REF_STATUS_REJECT_STALE
;
849 else if (!strcmp(msg
, "remote ref updated since checkout")) {
850 status
= REF_STATUS_REJECT_REMOTE_UPDATED
;
853 else if (!strcmp(msg
, "forced update")) {
857 else if (!strcmp(msg
, "expecting report")) {
858 status
= REF_STATUS_EXPECTING_REPORT
;
864 state
->hint
= find_ref_by_name(state
->hint
, refname
);
866 state
->hint
= find_ref_by_name(remote_refs
, refname
);
868 warning(_("helper reported unexpected status of %s"), refname
);
872 if (state
->hint
->status
!= REF_STATUS_NONE
) {
874 * Earlier, the ref was marked not to be pushed, so ignore the ref
875 * status reported by the remote helper if the latter is 'no match'.
877 if (status
== REF_STATUS_NONE
)
881 if (status
== REF_STATUS_OK
)
882 state
->new_report
= 1;
883 state
->hint
->status
= status
;
884 state
->hint
->forced_update
|= forced
;
885 state
->hint
->remote_status
= msg
;
886 return !(status
== REF_STATUS_OK
);
889 static int push_update_refs_status(struct helper_data
*data
,
890 struct ref
*remote_refs
,
894 struct ref_push_report
*report
;
895 struct strbuf buf
= STRBUF_INIT
;
896 struct push_update_ref_state state
= { remote_refs
, NULL
, 0 };
899 if (recvline(data
, &buf
)) {
900 strbuf_release(&buf
);
905 push_update_ref_status(&buf
, &state
, remote_refs
);
907 strbuf_release(&buf
);
909 if (flags
& TRANSPORT_PUSH_DRY_RUN
|| !data
->rs
.nr
|| data
->no_private_update
)
912 /* propagate back the update to the remote namespace */
913 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
916 if (ref
->status
!= REF_STATUS_OK
)
920 private = apply_refspecs(&data
->rs
, ref
->name
);
923 update_ref("update by helper", private, &(ref
->new_oid
),
927 for (report
= ref
->report
; report
; report
= report
->next
) {
928 private = apply_refspecs(&data
->rs
,
934 update_ref("update by helper", private,
946 static void set_common_push_options(struct transport
*transport
,
947 const char *name
, int flags
)
949 if (flags
& TRANSPORT_PUSH_DRY_RUN
) {
950 if (set_helper_option(transport
, "dry-run", "true") != 0)
951 die(_("helper %s does not support dry-run"), name
);
952 } else if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
) {
953 if (set_helper_option(transport
, TRANS_OPT_PUSH_CERT
, "true") != 0)
954 die(_("helper %s does not support --signed"), name
);
955 } else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
) {
956 if (set_helper_option(transport
, TRANS_OPT_PUSH_CERT
, "if-asked") != 0)
957 die(_("helper %s does not support --signed=if-asked"), name
);
960 if (flags
& TRANSPORT_PUSH_ATOMIC
)
961 if (set_helper_option(transport
, TRANS_OPT_ATOMIC
, "true") != 0)
962 die(_("helper %s does not support --atomic"), name
);
964 if (flags
& TRANSPORT_PUSH_FORCE_IF_INCLUDES
)
965 if (set_helper_option(transport
, TRANS_OPT_FORCE_IF_INCLUDES
, "true") != 0)
966 die(_("helper %s does not support --%s"),
967 name
, TRANS_OPT_FORCE_IF_INCLUDES
);
969 if (flags
& TRANSPORT_PUSH_OPTIONS
) {
970 struct string_list_item
*item
;
971 for_each_string_list_item(item
, transport
->push_options
)
972 if (set_helper_option(transport
, "push-option", item
->string
) != 0)
973 die(_("helper %s does not support 'push-option'"), name
);
977 static int push_refs_with_push(struct transport
*transport
,
978 struct ref
*remote_refs
, int flags
)
980 int force_all
= flags
& TRANSPORT_PUSH_FORCE
;
981 int mirror
= flags
& TRANSPORT_PUSH_MIRROR
;
982 int atomic
= flags
& TRANSPORT_PUSH_ATOMIC
;
983 struct helper_data
*data
= transport
->data
;
984 struct strbuf buf
= STRBUF_INIT
;
986 struct string_list cas_options
= STRING_LIST_INIT_DUP
;
987 struct string_list_item
*cas_option
;
989 get_helper(transport
);
993 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
994 if (!ref
->peer_ref
&& !mirror
)
997 /* Check for statuses set by set_ref_status_for_push() */
998 switch (ref
->status
) {
999 case REF_STATUS_REJECT_NONFASTFORWARD
:
1000 case REF_STATUS_REJECT_STALE
:
1001 case REF_STATUS_REJECT_ALREADY_EXISTS
:
1002 case REF_STATUS_REJECT_REMOTE_UPDATED
:
1004 reject_atomic_push(remote_refs
, mirror
);
1005 string_list_clear(&cas_options
, 0);
1009 case REF_STATUS_UPTODATE
:
1018 strbuf_addstr(&buf
, "push ");
1019 if (!ref
->deletion
) {
1021 strbuf_addch(&buf
, '+');
1023 strbuf_addstr(&buf
, ref
->peer_ref
->name
);
1025 strbuf_addstr(&buf
, oid_to_hex(&ref
->new_oid
));
1027 strbuf_addch(&buf
, ':');
1028 strbuf_addstr(&buf
, ref
->name
);
1029 strbuf_addch(&buf
, '\n');
1032 * The "--force-with-lease" options without explicit
1033 * values to expect have already been expanded into
1034 * the ref->old_oid_expect[] field; we can ignore
1035 * transport->smart_options->cas altogether and instead
1036 * can enumerate them from the refs.
1038 if (ref
->expect_old_sha1
) {
1039 struct strbuf cas
= STRBUF_INIT
;
1040 strbuf_addf(&cas
, "%s:%s",
1041 ref
->name
, oid_to_hex(&ref
->old_oid_expect
));
1042 string_list_append_nodup(&cas_options
,
1043 strbuf_detach(&cas
, NULL
));
1047 string_list_clear(&cas_options
, 0);
1051 for_each_string_list_item(cas_option
, &cas_options
)
1052 set_helper_option(transport
, "cas", cas_option
->string
);
1053 set_common_push_options(transport
, data
->name
, flags
);
1055 strbuf_addch(&buf
, '\n');
1056 sendline(data
, &buf
);
1057 strbuf_release(&buf
);
1058 string_list_clear(&cas_options
, 0);
1060 return push_update_refs_status(data
, remote_refs
, flags
);
1063 static int push_refs_with_export(struct transport
*transport
,
1064 struct ref
*remote_refs
, int flags
)
1067 struct child_process
*helper
, exporter
;
1068 struct helper_data
*data
= transport
->data
;
1069 struct string_list revlist_args
= STRING_LIST_INIT_DUP
;
1070 struct strbuf buf
= STRBUF_INIT
;
1073 die(_("remote-helper doesn't support push; refspec needed"));
1075 set_common_push_options(transport
, data
->name
, flags
);
1076 if (flags
& TRANSPORT_PUSH_FORCE
) {
1077 if (set_helper_option(transport
, "force", "true") != 0)
1078 warning(_("helper %s does not support 'force'"), data
->name
);
1081 helper
= get_helper(transport
);
1083 write_constant(helper
->in
, "export\n");
1085 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1087 struct object_id oid
;
1089 private = apply_refspecs(&data
->rs
, ref
->name
);
1090 if (private && !repo_get_oid(the_repository
, private, &oid
)) {
1091 strbuf_addf(&buf
, "^%s", private);
1092 string_list_append_nodup(&revlist_args
,
1093 strbuf_detach(&buf
, NULL
));
1094 oidcpy(&ref
->old_oid
, &oid
);
1098 if (ref
->peer_ref
) {
1099 if (strcmp(ref
->name
, ref
->peer_ref
->name
)) {
1100 if (!ref
->deletion
) {
1104 /* Follow symbolic refs (mainly for HEAD). */
1105 name
= resolve_ref_unsafe(ref
->peer_ref
->name
,
1106 RESOLVE_REF_READING
,
1108 if (!name
|| !(flag
& REF_ISSYMREF
))
1109 name
= ref
->peer_ref
->name
;
1111 strbuf_addf(&buf
, "%s:%s", name
, ref
->name
);
1113 strbuf_addf(&buf
, ":%s", ref
->name
);
1115 string_list_append(&revlist_args
, "--refspec");
1116 string_list_append(&revlist_args
, buf
.buf
);
1117 strbuf_release(&buf
);
1120 string_list_append(&revlist_args
, ref
->peer_ref
->name
);
1124 if (get_exporter(transport
, &exporter
, &revlist_args
))
1125 die(_("couldn't run fast-export"));
1127 string_list_clear(&revlist_args
, 1);
1129 if (finish_command(&exporter
))
1130 die(_("error while running fast-export"));
1131 if (push_update_refs_status(data
, remote_refs
, flags
))
1134 if (data
->export_marks
) {
1135 strbuf_addf(&buf
, "%s.tmp", data
->export_marks
);
1136 rename(buf
.buf
, data
->export_marks
);
1137 strbuf_release(&buf
);
1143 static int push_refs(struct transport
*transport
,
1144 struct ref
*remote_refs
, int flags
)
1146 struct helper_data
*data
= transport
->data
;
1148 if (process_connect(transport
, 1))
1149 return transport
->vtable
->push_refs(transport
, remote_refs
, flags
);
1153 _("No refs in common and none specified; doing nothing.\n"
1154 "Perhaps you should specify a branch.\n"));
1159 return push_refs_with_push(transport
, remote_refs
, flags
);
1162 return push_refs_with_export(transport
, remote_refs
, flags
);
1168 static int has_attribute(const char *attrs
, const char *attr
)
1176 const char *space
= strchrnul(attrs
, ' ');
1177 if (len
== space
- attrs
&& !strncmp(attrs
, attr
, len
))
1185 static struct ref
*get_refs_list(struct transport
*transport
, int for_push
,
1186 struct transport_ls_refs_options
*transport_options
)
1188 get_helper(transport
);
1190 if (process_connect(transport
, for_push
))
1191 return transport
->vtable
->get_refs_list(transport
, for_push
,
1194 return get_refs_list_using_list(transport
, for_push
);
1197 static struct ref
*get_refs_list_using_list(struct transport
*transport
,
1200 struct helper_data
*data
= transport
->data
;
1201 struct child_process
*helper
;
1202 struct ref
*ret
= NULL
;
1203 struct ref
**tail
= &ret
;
1205 struct strbuf buf
= STRBUF_INIT
;
1207 data
->get_refs_list_called
= 1;
1208 helper
= get_helper(transport
);
1210 if (data
->object_format
) {
1211 write_str_in_full(helper
->in
, "option object-format\n");
1212 if (recvline(data
, &buf
) || strcmp(buf
.buf
, "ok"))
1216 if (data
->push
&& for_push
)
1217 write_str_in_full(helper
->in
, "list for-push\n");
1219 write_str_in_full(helper
->in
, "list\n");
1223 if (recvline(data
, &buf
))
1228 else if (buf
.buf
[0] == ':') {
1230 if (skip_prefix(buf
.buf
, ":object-format ", &value
)) {
1231 int algo
= hash_algo_by_name(value
);
1232 if (algo
== GIT_HASH_UNKNOWN
)
1233 die(_("unsupported object format '%s'"),
1235 transport
->hash_algo
= &hash_algos
[algo
];
1240 eov
= strchr(buf
.buf
, ' ');
1242 die(_("malformed response in ref list: %s"), buf
.buf
);
1243 eon
= strchr(eov
+ 1, ' ');
1247 *tail
= alloc_ref(eov
+ 1);
1248 if (buf
.buf
[0] == '@')
1249 (*tail
)->symref
= xstrdup(buf
.buf
+ 1);
1250 else if (buf
.buf
[0] != '?')
1251 get_oid_hex_algop(buf
.buf
, &(*tail
)->old_oid
, transport
->hash_algo
);
1253 if (has_attribute(eon
+ 1, "unchanged")) {
1254 (*tail
)->status
|= REF_STATUS_UPTODATE
;
1255 if (read_ref((*tail
)->name
, &(*tail
)->old_oid
) < 0)
1256 die(_("could not read ref %s"),
1260 tail
= &((*tail
)->next
);
1263 fprintf(stderr
, "Debug: Read ref listing.\n");
1264 strbuf_release(&buf
);
1266 for (posn
= ret
; posn
; posn
= posn
->next
)
1267 resolve_remote_symref(posn
, ret
);
1272 static int get_bundle_uri(struct transport
*transport
)
1274 get_helper(transport
);
1276 if (process_connect(transport
, 0))
1277 return transport
->vtable
->get_bundle_uri(transport
);
1282 static struct transport_vtable vtable
= {
1283 .set_option
= set_helper_option
,
1284 .get_refs_list
= get_refs_list
,
1285 .get_bundle_uri
= get_bundle_uri
,
1286 .fetch_refs
= fetch_refs
,
1287 .push_refs
= push_refs
,
1288 .connect
= connect_helper
,
1289 .disconnect
= release_helper
1292 int transport_helper_init(struct transport
*transport
, const char *name
)
1294 struct helper_data
*data
= xcalloc(1, sizeof(*data
));
1297 transport_check_allowed(name
);
1299 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
1302 list_objects_filter_init(&data
->transport_options
.filter_options
);
1304 transport
->data
= data
;
1305 transport
->vtable
= &vtable
;
1306 transport
->smart_options
= &(data
->transport_options
);
1311 * Linux pipes can buffer 65536 bytes at once (and most platforms can
1312 * buffer less), so attempt reads and writes with up to that size.
1314 #define BUFFERSIZE 65536
1315 /* This should be enough to hold debugging message. */
1316 #define PBUFFERSIZE 8192
1318 /* Print bidirectional transfer loop debug message. */
1319 __attribute__((format (printf
, 1, 2)))
1320 static void transfer_debug(const char *fmt
, ...)
1323 * NEEDSWORK: This function is sometimes used from multiple threads, and
1324 * we end up using debug_enabled racily. That "should not matter" since
1325 * we always write the same value, but it's still wrong. This function
1326 * is listed in .tsan-suppressions for the time being.
1330 char msgbuf
[PBUFFERSIZE
];
1331 static int debug_enabled
= -1;
1333 if (debug_enabled
< 0)
1334 debug_enabled
= getenv("GIT_TRANSLOOP_DEBUG") ? 1 : 0;
1338 va_start(args
, fmt
);
1339 vsnprintf(msgbuf
, PBUFFERSIZE
, fmt
, args
);
1341 fprintf(stderr
, "Transfer loop debugging: %s\n", msgbuf
);
1344 /* Stream state: More data may be coming in this direction. */
1345 #define SSTATE_TRANSFERRING 0
1347 * Stream state: No more data coming in this direction, flushing rest of
1350 #define SSTATE_FLUSHING 1
1351 /* Stream state: Transfer in this direction finished. */
1352 #define SSTATE_FINISHED 2
1354 #define STATE_NEEDS_READING(state) ((state) <= SSTATE_TRANSFERRING)
1355 #define STATE_NEEDS_WRITING(state) ((state) <= SSTATE_FLUSHING)
1356 #define STATE_NEEDS_CLOSING(state) ((state) == SSTATE_FLUSHING)
1358 /* Unidirectional transfer. */
1359 struct unidirectional_transfer
{
1364 /* Is source socket? */
1366 /* Is destination socket? */
1368 /* Transfer state (TRANSFERRING/FLUSHING/FINISHED) */
1371 char buf
[BUFFERSIZE
];
1374 /* Name of source. */
1375 const char *src_name
;
1376 /* Name of destination. */
1377 const char *dest_name
;
1380 /* Closes the target (for writing) if transfer has finished. */
1381 static void udt_close_if_finished(struct unidirectional_transfer
*t
)
1383 if (STATE_NEEDS_CLOSING(t
->state
) && !t
->bufuse
) {
1384 t
->state
= SSTATE_FINISHED
;
1385 if (t
->dest_is_sock
)
1386 shutdown(t
->dest
, SHUT_WR
);
1389 transfer_debug("Closed %s.", t
->dest_name
);
1394 * Tries to read data from source into buffer. If buffer is full,
1395 * no data is read. Returns 0 on success, -1 on error.
1397 static int udt_do_read(struct unidirectional_transfer
*t
)
1401 if (t
->bufuse
== BUFFERSIZE
)
1402 return 0; /* No space for more. */
1404 transfer_debug("%s is readable", t
->src_name
);
1405 bytes
= xread(t
->src
, t
->buf
+ t
->bufuse
, BUFFERSIZE
- t
->bufuse
);
1407 error_errno(_("read(%s) failed"), t
->src_name
);
1409 } else if (bytes
== 0) {
1410 transfer_debug("%s EOF (with %i bytes in buffer)",
1411 t
->src_name
, (int)t
->bufuse
);
1412 t
->state
= SSTATE_FLUSHING
;
1413 } else if (bytes
> 0) {
1415 transfer_debug("Read %i bytes from %s (buffer now at %i)",
1416 (int)bytes
, t
->src_name
, (int)t
->bufuse
);
1421 /* Tries to write data from buffer into destination. If buffer is empty,
1422 * no data is written. Returns 0 on success, -1 on error.
1424 static int udt_do_write(struct unidirectional_transfer
*t
)
1429 return 0; /* Nothing to write. */
1431 transfer_debug("%s is writable", t
->dest_name
);
1432 bytes
= xwrite(t
->dest
, t
->buf
, t
->bufuse
);
1434 error_errno(_("write(%s) failed"), t
->dest_name
);
1436 } else if (bytes
> 0) {
1439 memmove(t
->buf
, t
->buf
+ bytes
, t
->bufuse
);
1440 transfer_debug("Wrote %i bytes to %s (buffer now at %i)",
1441 (int)bytes
, t
->dest_name
, (int)t
->bufuse
);
1447 /* State of bidirectional transfer loop. */
1448 struct bidirectional_transfer_state
{
1449 /* Direction from program to git. */
1450 struct unidirectional_transfer ptg
;
1451 /* Direction from git to program. */
1452 struct unidirectional_transfer gtp
;
1455 static void *udt_copy_task_routine(void *udt
)
1457 struct unidirectional_transfer
*t
= (struct unidirectional_transfer
*)udt
;
1458 while (t
->state
!= SSTATE_FINISHED
) {
1459 if (STATE_NEEDS_READING(t
->state
))
1462 if (STATE_NEEDS_WRITING(t
->state
))
1463 if (udt_do_write(t
))
1465 if (STATE_NEEDS_CLOSING(t
->state
))
1466 udt_close_if_finished(t
);
1468 return udt
; /* Just some non-NULL value. */
1474 * Join thread, with appropriate errors on failure. Name is name for the
1475 * thread (for error messages). Returns 0 on success, 1 on failure.
1477 static int tloop_join(pthread_t thread
, const char *name
)
1481 err
= pthread_join(thread
, &tret
);
1483 error(_("%s thread failed"), name
);
1487 error(_("%s thread failed to join: %s"), name
, strerror(err
));
1494 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1497 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1499 pthread_t gtp_thread
;
1500 pthread_t ptg_thread
;
1503 err
= pthread_create(>p_thread
, NULL
, udt_copy_task_routine
,
1506 die(_("can't start thread for copying data: %s"), strerror(err
));
1507 err
= pthread_create(&ptg_thread
, NULL
, udt_copy_task_routine
,
1510 die(_("can't start thread for copying data: %s"), strerror(err
));
1512 ret
|= tloop_join(gtp_thread
, "Git to program copy");
1513 ret
|= tloop_join(ptg_thread
, "Program to git copy");
1518 /* Close the source and target (for writing) for transfer. */
1519 static void udt_kill_transfer(struct unidirectional_transfer
*t
)
1521 t
->state
= SSTATE_FINISHED
;
1523 * Socket read end left open isn't a disaster if nobody
1524 * attempts to read from it (mingw compat headers do not
1527 * We can't fully close the socket since otherwise gtp
1528 * task would first close the socket it sends data to
1529 * while closing the ptg file descriptors.
1531 if (!t
->src_is_sock
)
1533 if (t
->dest_is_sock
)
1534 shutdown(t
->dest
, SHUT_WR
);
1540 * Join process, with appropriate errors on failure. Name is name for the
1541 * process (for error messages). Returns 0 on success, 1 on failure.
1543 static int tloop_join(pid_t pid
, const char *name
)
1546 if (waitpid(pid
, &tret
, 0) < 0) {
1547 error_errno(_("%s process failed to wait"), name
);
1550 if (!WIFEXITED(tret
) || WEXITSTATUS(tret
)) {
1551 error(_("%s process failed"), name
);
1558 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1561 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1566 /* Fork thread #1: git to program. */
1569 die_errno(_("can't start thread for copying data"));
1570 else if (pid1
== 0) {
1571 udt_kill_transfer(&s
->ptg
);
1572 exit(udt_copy_task_routine(&s
->gtp
) ? 0 : 1);
1575 /* Fork thread #2: program to git. */
1578 die_errno(_("can't start thread for copying data"));
1579 else if (pid2
== 0) {
1580 udt_kill_transfer(&s
->gtp
);
1581 exit(udt_copy_task_routine(&s
->ptg
) ? 0 : 1);
1585 * Close both streams in parent as to not interfere with
1586 * end of file detection and wait for both tasks to finish.
1588 udt_kill_transfer(&s
->gtp
);
1589 udt_kill_transfer(&s
->ptg
);
1590 ret
|= tloop_join(pid1
, "Git to program copy");
1591 ret
|= tloop_join(pid2
, "Program to git copy");
1597 * Copies data from stdin to output and from input to stdout simultaneously.
1598 * Additionally filtering through given filter. If filter is NULL, uses
1601 int bidirectional_transfer_loop(int input
, int output
)
1603 struct bidirectional_transfer_state state
;
1605 /* Fill the state fields. */
1606 state
.ptg
.src
= input
;
1608 state
.ptg
.src_is_sock
= (input
== output
);
1609 state
.ptg
.dest_is_sock
= 0;
1610 state
.ptg
.state
= SSTATE_TRANSFERRING
;
1611 state
.ptg
.bufuse
= 0;
1612 state
.ptg
.src_name
= "remote input";
1613 state
.ptg
.dest_name
= "stdout";
1616 state
.gtp
.dest
= output
;
1617 state
.gtp
.src_is_sock
= 0;
1618 state
.gtp
.dest_is_sock
= (input
== output
);
1619 state
.gtp
.state
= SSTATE_TRANSFERRING
;
1620 state
.gtp
.bufuse
= 0;
1621 state
.gtp
.src_name
= "stdin";
1622 state
.gtp
.dest_name
= "remote output";
1624 return tloop_spawnwait_tasks(&state
);
1627 void reject_atomic_push(struct ref
*remote_refs
, int mirror_mode
)
1631 /* Mark other refs as failed */
1632 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1633 if (!ref
->peer_ref
&& !mirror_mode
)
1636 switch (ref
->status
) {
1637 case REF_STATUS_NONE
:
1639 case REF_STATUS_EXPECTING_REPORT
:
1640 ref
->status
= REF_STATUS_ATOMIC_PUSH_FAILED
;
1643 break; /* do nothing */