1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
6 #include "run-command.h"
8 #include "environment.h"
11 #include "object-name.h"
12 #include "repository.h"
14 #include "string-list.h"
15 #include "thread-utils.h"
20 #include "transport-internal.h"
28 struct child_process
*helper
;
37 stateless_connect
: 1,
39 check_connectivity
: 1,
40 no_disconnect_req
: 1,
41 no_private_update
: 1,
45 * As an optimization, the transport code may invoke fetch before
46 * get_refs_list. If this happens, and if the transport helper doesn't
47 * support connect or stateless_connect, we need to invoke
48 * get_refs_list ourselves if we haven't already done so. Keep track of
49 * whether we have invoked get_refs_list.
51 unsigned get_refs_list_called
: 1;
55 /* These go from remote name (as in "list") to private name */
57 /* Transport options for fetch-pack/send-pack (should one of
60 struct git_transport_options transport_options
;
63 static void sendline(struct helper_data
*helper
, struct strbuf
*buffer
)
66 fprintf(stderr
, "Debug: Remote helper: -> %s", buffer
->buf
);
67 if (write_in_full(helper
->helper
->in
, buffer
->buf
, buffer
->len
) < 0)
68 die_errno(_("full write to remote helper failed"));
71 static int recvline_fh(FILE *helper
, struct strbuf
*buffer
)
75 fprintf(stderr
, "Debug: Remote helper: Waiting...\n");
76 if (strbuf_getline(buffer
, helper
) == EOF
) {
78 fprintf(stderr
, "Debug: Remote helper quit.\n");
83 fprintf(stderr
, "Debug: Remote helper: <- %s\n", buffer
->buf
);
87 static int recvline(struct helper_data
*helper
, struct strbuf
*buffer
)
89 return recvline_fh(helper
->out
, buffer
);
92 static void write_constant(int fd
, const char *str
)
95 fprintf(stderr
, "Debug: Remote helper: -> %s", str
);
96 if (write_in_full(fd
, str
, strlen(str
)) < 0)
97 die_errno(_("full write to remote helper failed"));
100 static const char *remove_ext_force(const char *url
)
103 const char *colon
= strchr(url
, ':');
104 if (colon
&& colon
[1] == ':')
110 static void do_take_over(struct transport
*transport
)
112 struct helper_data
*data
;
113 data
= (struct helper_data
*)transport
->data
;
114 transport_take_over(transport
, data
->helper
);
120 static void standard_options(struct transport
*t
);
122 static struct child_process
*get_helper(struct transport
*transport
)
124 struct helper_data
*data
= transport
->data
;
125 struct strbuf buf
= STRBUF_INIT
;
126 struct child_process
*helper
;
133 helper
= xmalloc(sizeof(*helper
));
134 child_process_init(helper
);
138 strvec_pushf(&helper
->args
, "remote-%s", data
->name
);
139 strvec_push(&helper
->args
, transport
->remote
->name
);
140 strvec_push(&helper
->args
, remove_ext_force(transport
->url
));
142 helper
->silent_exec_failure
= 1;
145 strvec_pushf(&helper
->env
, "%s=%s",
146 GIT_DIR_ENVIRONMENT
, get_git_dir());
148 helper
->trace2_child_class
= helper
->args
.v
[0]; /* "remote-<name>" */
150 code
= start_command(helper
);
151 if (code
< 0 && errno
== ENOENT
)
152 die(_("unable to find remote helper for '%s'"), data
->name
);
156 data
->helper
= helper
;
157 data
->no_disconnect_req
= 0;
158 refspec_init(&data
->rs
, REFSPEC_FETCH
);
161 * Open the output as FILE* so strbuf_getline_*() family of
162 * functions can be used.
163 * Do this with duped fd because fclose() will close the fd,
164 * and stuff like taking over will require the fd to remain.
166 duped
= dup(helper
->out
);
168 die_errno(_("can't dup helper output fd"));
169 data
->out
= xfdopen(duped
, "r");
171 write_constant(helper
->in
, "capabilities\n");
174 const char *capname
, *arg
;
176 if (recvline(data
, &buf
))
182 if (*buf
.buf
== '*') {
183 capname
= buf
.buf
+ 1;
189 fprintf(stderr
, "Debug: Got cap %s\n", capname
);
190 if (!strcmp(capname
, "fetch"))
192 else if (!strcmp(capname
, "option"))
194 else if (!strcmp(capname
, "push"))
196 else if (!strcmp(capname
, "import"))
198 else if (!strcmp(capname
, "bidi-import"))
199 data
->bidi_import
= 1;
200 else if (!strcmp(capname
, "export"))
202 else if (!strcmp(capname
, "check-connectivity"))
203 data
->check_connectivity
= 1;
204 else if (skip_prefix(capname
, "refspec ", &arg
)) {
205 refspec_append(&data
->rs
, arg
);
206 } else if (!strcmp(capname
, "connect")) {
208 } else if (!strcmp(capname
, "stateless-connect")) {
209 data
->stateless_connect
= 1;
210 } else if (!strcmp(capname
, "signed-tags")) {
211 data
->signed_tags
= 1;
212 } else if (skip_prefix(capname
, "export-marks ", &arg
)) {
213 data
->export_marks
= xstrdup(arg
);
214 } else if (skip_prefix(capname
, "import-marks ", &arg
)) {
215 data
->import_marks
= xstrdup(arg
);
216 } else if (starts_with(capname
, "no-private-update")) {
217 data
->no_private_update
= 1;
218 } else if (starts_with(capname
, "object-format")) {
219 data
->object_format
= 1;
220 } else if (mandatory
) {
221 die(_("unknown mandatory capability %s; this remote "
222 "helper probably needs newer version of Git"),
226 if (!data
->rs
.nr
&& (data
->import
|| data
->bidi_import
|| data
->export
)) {
227 warning(_("this remote helper should implement refspec capability"));
229 strbuf_release(&buf
);
231 fprintf(stderr
, "Debug: Capabilities complete.\n");
232 standard_options(transport
);
236 static int disconnect_helper(struct transport
*transport
)
238 struct helper_data
*data
= transport
->data
;
243 fprintf(stderr
, "Debug: Disconnecting.\n");
244 if (!data
->no_disconnect_req
) {
246 * Ignore write errors; there's nothing we can do,
247 * since we're about to close the pipe anyway. And the
248 * most likely error is EPIPE due to the helper dying
249 * to report an error itself.
251 sigchain_push(SIGPIPE
, SIG_IGN
);
252 xwrite(data
->helper
->in
, "\n", 1);
253 sigchain_pop(SIGPIPE
);
255 close(data
->helper
->in
);
256 close(data
->helper
->out
);
258 res
= finish_command(data
->helper
);
259 FREE_AND_NULL(data
->name
);
260 FREE_AND_NULL(data
->helper
);
265 static const char *unsupported_options
[] = {
266 TRANS_OPT_UPLOADPACK
,
267 TRANS_OPT_RECEIVEPACK
,
272 static const char *boolean_options
[] = {
275 TRANS_OPT_FOLLOWTAGS
,
276 TRANS_OPT_DEEPEN_RELATIVE
279 static int strbuf_set_helper_option(struct helper_data
*data
,
285 if (recvline(data
, buf
))
288 if (!strcmp(buf
->buf
, "ok"))
290 else if (starts_with(buf
->buf
, "error"))
292 else if (!strcmp(buf
->buf
, "unsupported"))
295 warning(_("%s unexpectedly said: '%s'"), data
->name
, buf
->buf
);
301 static int string_list_set_helper_option(struct helper_data
*data
,
303 struct string_list
*list
)
305 struct strbuf buf
= STRBUF_INIT
;
308 for (i
= 0; i
< list
->nr
; i
++) {
309 strbuf_addf(&buf
, "option %s ", name
);
310 quote_c_style(list
->items
[i
].string
, &buf
, NULL
, 0);
311 strbuf_addch(&buf
, '\n');
313 if ((ret
= strbuf_set_helper_option(data
, &buf
)))
317 strbuf_release(&buf
);
321 static int set_helper_option(struct transport
*transport
,
322 const char *name
, const char *value
)
324 struct helper_data
*data
= transport
->data
;
325 struct strbuf buf
= STRBUF_INIT
;
326 int i
, ret
, is_bool
= 0;
328 get_helper(transport
);
333 if (!strcmp(name
, "deepen-not"))
334 return string_list_set_helper_option(data
, name
,
335 (struct string_list
*)value
);
337 for (i
= 0; i
< ARRAY_SIZE(unsupported_options
); i
++) {
338 if (!strcmp(name
, unsupported_options
[i
]))
342 for (i
= 0; i
< ARRAY_SIZE(boolean_options
); i
++) {
343 if (!strcmp(name
, boolean_options
[i
])) {
349 strbuf_addf(&buf
, "option %s ", name
);
351 strbuf_addstr(&buf
, value
? "true" : "false");
353 quote_c_style(value
, &buf
, NULL
, 0);
354 strbuf_addch(&buf
, '\n');
356 ret
= strbuf_set_helper_option(data
, &buf
);
357 strbuf_release(&buf
);
361 static void standard_options(struct transport
*t
)
366 set_helper_option(t
, "progress", t
->progress
? "true" : "false");
368 xsnprintf(buf
, sizeof(buf
), "%d", v
+ 1);
369 set_helper_option(t
, "verbosity", buf
);
372 case TRANSPORT_FAMILY_ALL
:
374 * this is already the default,
375 * do not break old remote helpers by setting "all" here
378 case TRANSPORT_FAMILY_IPV4
:
379 set_helper_option(t
, "family", "ipv4");
381 case TRANSPORT_FAMILY_IPV6
:
382 set_helper_option(t
, "family", "ipv6");
387 static int release_helper(struct transport
*transport
)
390 struct helper_data
*data
= transport
->data
;
391 refspec_clear(&data
->rs
);
392 res
= disconnect_helper(transport
);
393 free(transport
->data
);
397 static int fetch_with_fetch(struct transport
*transport
,
398 int nr_heads
, struct ref
**to_fetch
)
400 struct helper_data
*data
= transport
->data
;
402 struct strbuf buf
= STRBUF_INIT
;
404 for (i
= 0; i
< nr_heads
; i
++) {
405 const struct ref
*posn
= to_fetch
[i
];
406 if (posn
->status
& REF_STATUS_UPTODATE
)
409 strbuf_addf(&buf
, "fetch %s %s\n",
410 oid_to_hex(&posn
->old_oid
),
411 posn
->symref
? posn
->symref
: posn
->name
);
414 strbuf_addch(&buf
, '\n');
415 sendline(data
, &buf
);
420 if (recvline(data
, &buf
))
423 if (skip_prefix(buf
.buf
, "lock ", &name
)) {
424 if (transport
->pack_lockfiles
.nr
)
425 warning(_("%s also locked %s"), data
->name
, name
);
427 string_list_append(&transport
->pack_lockfiles
,
430 else if (data
->check_connectivity
&&
431 data
->transport_options
.check_self_contained_and_connected
&&
432 !strcmp(buf
.buf
, "connectivity-ok"))
433 data
->transport_options
.self_contained_and_connected
= 1;
437 warning(_("%s unexpectedly said: '%s'"), data
->name
, buf
.buf
);
439 strbuf_release(&buf
);
441 reprepare_packed_git(the_repository
);
445 static int get_importer(struct transport
*transport
, struct child_process
*fastimport
)
447 struct child_process
*helper
= get_helper(transport
);
448 struct helper_data
*data
= transport
->data
;
449 int cat_blob_fd
, code
;
450 child_process_init(fastimport
);
451 fastimport
->in
= xdup(helper
->out
);
452 strvec_push(&fastimport
->args
, "fast-import");
453 strvec_push(&fastimport
->args
, "--allow-unsafe-features");
454 strvec_push(&fastimport
->args
, debug
? "--stats" : "--quiet");
456 if (data
->bidi_import
) {
457 cat_blob_fd
= xdup(helper
->in
);
458 strvec_pushf(&fastimport
->args
, "--cat-blob-fd=%d", cat_blob_fd
);
460 fastimport
->git_cmd
= 1;
462 code
= start_command(fastimport
);
466 static int get_exporter(struct transport
*transport
,
467 struct child_process
*fastexport
,
468 struct string_list
*revlist_args
)
470 struct helper_data
*data
= transport
->data
;
471 struct child_process
*helper
= get_helper(transport
);
474 child_process_init(fastexport
);
476 /* we need to duplicate helper->in because we want to use it after
477 * fastexport is done with it. */
478 fastexport
->out
= dup(helper
->in
);
479 strvec_push(&fastexport
->args
, "fast-export");
480 strvec_push(&fastexport
->args
, "--use-done-feature");
481 strvec_push(&fastexport
->args
, data
->signed_tags
?
482 "--signed-tags=verbatim" : "--signed-tags=warn-strip");
483 if (data
->export_marks
)
484 strvec_pushf(&fastexport
->args
, "--export-marks=%s.tmp", data
->export_marks
);
485 if (data
->import_marks
)
486 strvec_pushf(&fastexport
->args
, "--import-marks=%s", data
->import_marks
);
488 for (i
= 0; i
< revlist_args
->nr
; i
++)
489 strvec_push(&fastexport
->args
, revlist_args
->items
[i
].string
);
491 fastexport
->git_cmd
= 1;
492 return start_command(fastexport
);
495 static int fetch_with_import(struct transport
*transport
,
496 int nr_heads
, struct ref
**to_fetch
)
498 struct child_process fastimport
;
499 struct helper_data
*data
= transport
->data
;
502 struct strbuf buf
= STRBUF_INIT
;
504 get_helper(transport
);
506 if (get_importer(transport
, &fastimport
))
507 die(_("couldn't run fast-import"));
509 for (i
= 0; i
< nr_heads
; i
++) {
511 if (posn
->status
& REF_STATUS_UPTODATE
)
514 strbuf_addf(&buf
, "import %s\n",
515 posn
->symref
? posn
->symref
: posn
->name
);
516 sendline(data
, &buf
);
520 write_constant(data
->helper
->in
, "\n");
522 * remote-helpers that advertise the bidi-import capability are required to
523 * buffer the complete batch of import commands until this newline before
524 * sending data to fast-import.
525 * These helpers read back data from fast-import on their stdin, which could
526 * be mixed with import commands, otherwise.
529 if (finish_command(&fastimport
))
530 die(_("error while running fast-import"));
533 * The fast-import stream of a remote helper that advertises
534 * the "refspec" capability writes to the refs named after the
535 * right hand side of the first refspec matching each ref we
538 * (If no "refspec" capability was specified, for historical
539 * reasons we default to the equivalent of *:*.)
541 * Store the result in to_fetch[i].old_sha1. Callers such
542 * as "git fetch" can use the value to write feedback to the
543 * terminal, populate FETCH_HEAD, and determine what new value
544 * should be written to peer_ref if the update is a
545 * fast-forward or this is a forced update.
547 for (i
= 0; i
< nr_heads
; i
++) {
548 char *private, *name
;
550 if (posn
->status
& REF_STATUS_UPTODATE
)
552 name
= posn
->symref
? posn
->symref
: posn
->name
;
554 private = apply_refspecs(&data
->rs
, name
);
556 private = xstrdup(name
);
558 if (refs_read_ref(get_main_ref_store(the_repository
), private, &posn
->old_oid
) < 0)
559 die(_("could not read ref %s"), private);
563 strbuf_release(&buf
);
567 static int run_connect(struct transport
*transport
, struct strbuf
*cmdbuf
)
569 struct helper_data
*data
= transport
->data
;
573 struct child_process
*helper
;
575 helper
= get_helper(transport
);
578 * Yes, dup the pipe another time, as we need unbuffered version
579 * of input pipe as FILE*. fclose() closes the underlying fd and
580 * stream buffering only can be changed before first I/O operation
583 duped
= dup(helper
->out
);
585 die_errno(_("can't dup helper output fd"));
586 input
= xfdopen(duped
, "r");
587 setvbuf(input
, NULL
, _IONBF
, 0);
589 sendline(data
, cmdbuf
);
590 if (recvline_fh(input
, cmdbuf
))
593 if (!strcmp(cmdbuf
->buf
, "")) {
594 data
->no_disconnect_req
= 1;
596 fprintf(stderr
, "Debug: Smart transport connection "
599 } else if (!strcmp(cmdbuf
->buf
, "fallback")) {
601 fprintf(stderr
, "Debug: Falling back to dumb "
604 die(_("unknown response to connect: %s"),
612 static int process_connect_service(struct transport
*transport
,
613 const char *name
, const char *exec
)
615 struct helper_data
*data
= transport
->data
;
616 struct strbuf cmdbuf
= STRBUF_INIT
;
620 * Handle --upload-pack and friends. This is fire and forget...
621 * just warn if it fails.
623 if (strcmp(name
, exec
)) {
624 int r
= set_helper_option(transport
, "servpath", exec
);
626 warning(_("setting remote service path not supported by protocol"));
628 warning(_("invalid remote service path"));
632 strbuf_addf(&cmdbuf
, "connect %s\n", name
);
633 ret
= run_connect(transport
, &cmdbuf
);
634 } else if (data
->stateless_connect
&&
635 (get_protocol_version_config() == protocol_v2
) &&
636 (!strcmp("git-upload-pack", name
) ||
637 !strcmp("git-upload-archive", name
))) {
638 strbuf_addf(&cmdbuf
, "stateless-connect %s\n", name
);
639 ret
= run_connect(transport
, &cmdbuf
);
641 transport
->stateless_rpc
= 1;
644 strbuf_release(&cmdbuf
);
648 static int process_connect(struct transport
*transport
,
651 struct helper_data
*data
= transport
->data
;
656 name
= for_push
? "git-receive-pack" : "git-upload-pack";
658 exec
= data
->transport_options
.receivepack
;
660 exec
= data
->transport_options
.uploadpack
;
662 ret
= process_connect_service(transport
, name
, exec
);
664 do_take_over(transport
);
668 static int connect_helper(struct transport
*transport
, const char *name
,
669 const char *exec
, int fd
[2])
671 struct helper_data
*data
= transport
->data
;
673 /* Get_helper so connect is inited. */
674 get_helper(transport
);
676 if (!process_connect_service(transport
, name
, exec
))
677 die(_("can't connect to subservice %s"), name
);
679 fd
[0] = data
->helper
->out
;
680 fd
[1] = data
->helper
->in
;
682 do_take_over(transport
);
686 static struct ref
*get_refs_list_using_list(struct transport
*transport
,
689 static int fetch_refs(struct transport
*transport
,
690 int nr_heads
, struct ref
**to_fetch
)
692 struct helper_data
*data
= transport
->data
;
695 get_helper(transport
);
697 if (process_connect(transport
, 0))
698 return transport
->vtable
->fetch_refs(transport
, nr_heads
, to_fetch
);
701 * If we reach here, then the server, the client, and/or the transport
702 * helper does not support protocol v2. --negotiate-only requires
705 if (data
->transport_options
.acked_commits
) {
706 warning(_("--negotiate-only requires protocol v2"));
710 if (!data
->get_refs_list_called
)
711 get_refs_list_using_list(transport
, 0);
714 for (i
= 0; i
< nr_heads
; i
++)
715 if (!(to_fetch
[i
]->status
& REF_STATUS_UPTODATE
))
721 if (data
->check_connectivity
&&
722 data
->transport_options
.check_self_contained_and_connected
)
723 set_helper_option(transport
, "check-connectivity", "true");
725 if (transport
->cloning
)
726 set_helper_option(transport
, "cloning", "true");
728 if (data
->transport_options
.update_shallow
)
729 set_helper_option(transport
, "update-shallow", "true");
731 if (data
->transport_options
.refetch
)
732 set_helper_option(transport
, "refetch", "true");
734 if (data
->transport_options
.filter_options
.choice
) {
735 const char *spec
= expand_list_objects_filter_spec(
736 &data
->transport_options
.filter_options
);
737 set_helper_option(transport
, "filter", spec
);
740 if (data
->transport_options
.negotiation_tips
)
741 warning("Ignoring --negotiation-tip because the protocol does not support it.");
744 return fetch_with_fetch(transport
, nr_heads
, to_fetch
);
747 return fetch_with_import(transport
, nr_heads
, to_fetch
);
752 struct push_update_ref_state
{
754 struct ref_push_report
*report
;
758 static int push_update_ref_status(struct strbuf
*buf
,
759 struct push_update_ref_state
*state
,
760 struct ref
*remote_refs
)
763 int status
, forced
= 0;
765 if (starts_with(buf
->buf
, "option ")) {
766 struct object_id old_oid
, new_oid
;
767 const char *key
, *val
;
770 if (!state
->hint
|| !(state
->report
|| state
->new_report
))
771 die(_("'option' without a matching 'ok/error' directive"));
772 if (state
->new_report
) {
773 if (!state
->hint
->report
) {
774 CALLOC_ARRAY(state
->hint
->report
, 1);
775 state
->report
= state
->hint
->report
;
777 state
->report
= state
->hint
->report
;
778 while (state
->report
->next
)
779 state
->report
= state
->report
->next
;
780 CALLOC_ARRAY(state
->report
->next
, 1);
781 state
->report
= state
->report
->next
;
783 state
->new_report
= 0;
786 p
= strchr(key
, ' ');
790 if (!strcmp(key
, "refname"))
791 state
->report
->ref_name
= xstrdup_or_null(val
);
792 else if (!strcmp(key
, "old-oid") && val
&&
793 !parse_oid_hex(val
, &old_oid
, &val
))
794 state
->report
->old_oid
= oiddup(&old_oid
);
795 else if (!strcmp(key
, "new-oid") && val
&&
796 !parse_oid_hex(val
, &new_oid
, &val
))
797 state
->report
->new_oid
= oiddup(&new_oid
);
798 else if (!strcmp(key
, "forced-update"))
799 state
->report
->forced_update
= 1;
800 /* Not update remote namespace again. */
804 state
->report
= NULL
;
805 state
->new_report
= 0;
807 if (starts_with(buf
->buf
, "ok ")) {
808 status
= REF_STATUS_OK
;
809 refname
= buf
->buf
+ 3;
810 } else if (starts_with(buf
->buf
, "error ")) {
811 status
= REF_STATUS_REMOTE_REJECT
;
812 refname
= buf
->buf
+ 6;
814 die(_("expected ok/error, helper said '%s'"), buf
->buf
);
816 msg
= strchr(refname
, ' ');
818 struct strbuf msg_buf
= STRBUF_INIT
;
822 if (!unquote_c_style(&msg_buf
, msg
, &end
))
823 msg
= strbuf_detach(&msg_buf
, NULL
);
826 strbuf_release(&msg_buf
);
828 if (!strcmp(msg
, "no match")) {
829 status
= REF_STATUS_NONE
;
832 else if (!strcmp(msg
, "up to date")) {
833 status
= REF_STATUS_UPTODATE
;
836 else if (!strcmp(msg
, "non-fast forward")) {
837 status
= REF_STATUS_REJECT_NONFASTFORWARD
;
840 else if (!strcmp(msg
, "already exists")) {
841 status
= REF_STATUS_REJECT_ALREADY_EXISTS
;
844 else if (!strcmp(msg
, "fetch first")) {
845 status
= REF_STATUS_REJECT_FETCH_FIRST
;
848 else if (!strcmp(msg
, "needs force")) {
849 status
= REF_STATUS_REJECT_NEEDS_FORCE
;
852 else if (!strcmp(msg
, "stale info")) {
853 status
= REF_STATUS_REJECT_STALE
;
856 else if (!strcmp(msg
, "remote ref updated since checkout")) {
857 status
= REF_STATUS_REJECT_REMOTE_UPDATED
;
860 else if (!strcmp(msg
, "forced update")) {
864 else if (!strcmp(msg
, "expecting report")) {
865 status
= REF_STATUS_EXPECTING_REPORT
;
871 state
->hint
= find_ref_by_name(state
->hint
, refname
);
873 state
->hint
= find_ref_by_name(remote_refs
, refname
);
875 warning(_("helper reported unexpected status of %s"), refname
);
879 if (state
->hint
->status
!= REF_STATUS_NONE
) {
881 * Earlier, the ref was marked not to be pushed, so ignore the ref
882 * status reported by the remote helper if the latter is 'no match'.
884 if (status
== REF_STATUS_NONE
)
888 if (status
== REF_STATUS_OK
)
889 state
->new_report
= 1;
890 state
->hint
->status
= status
;
891 state
->hint
->forced_update
|= forced
;
892 state
->hint
->remote_status
= msg
;
893 return !(status
== REF_STATUS_OK
);
896 static int push_update_refs_status(struct helper_data
*data
,
897 struct ref
*remote_refs
,
901 struct ref_push_report
*report
;
902 struct strbuf buf
= STRBUF_INIT
;
903 struct push_update_ref_state state
= { remote_refs
, NULL
, 0 };
906 if (recvline(data
, &buf
)) {
907 strbuf_release(&buf
);
912 push_update_ref_status(&buf
, &state
, remote_refs
);
914 strbuf_release(&buf
);
916 if (flags
& TRANSPORT_PUSH_DRY_RUN
|| !data
->rs
.nr
|| data
->no_private_update
)
919 /* propagate back the update to the remote namespace */
920 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
923 if (ref
->status
!= REF_STATUS_OK
)
927 private = apply_refspecs(&data
->rs
, ref
->name
);
930 refs_update_ref(get_main_ref_store(the_repository
),
931 "update by helper", private,
936 for (report
= ref
->report
; report
; report
= report
->next
) {
937 private = apply_refspecs(&data
->rs
,
943 refs_update_ref(get_main_ref_store(the_repository
),
944 "update by helper", private,
956 static void set_common_push_options(struct transport
*transport
,
957 const char *name
, int flags
)
959 if (flags
& TRANSPORT_PUSH_DRY_RUN
) {
960 if (set_helper_option(transport
, "dry-run", "true") != 0)
961 die(_("helper %s does not support dry-run"), name
);
962 } else if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
) {
963 if (set_helper_option(transport
, TRANS_OPT_PUSH_CERT
, "true") != 0)
964 die(_("helper %s does not support --signed"), name
);
965 } else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
) {
966 if (set_helper_option(transport
, TRANS_OPT_PUSH_CERT
, "if-asked") != 0)
967 die(_("helper %s does not support --signed=if-asked"), name
);
970 if (flags
& TRANSPORT_PUSH_ATOMIC
)
971 if (set_helper_option(transport
, TRANS_OPT_ATOMIC
, "true") != 0)
972 die(_("helper %s does not support --atomic"), name
);
974 if (flags
& TRANSPORT_PUSH_FORCE_IF_INCLUDES
)
975 if (set_helper_option(transport
, TRANS_OPT_FORCE_IF_INCLUDES
, "true") != 0)
976 die(_("helper %s does not support --%s"),
977 name
, TRANS_OPT_FORCE_IF_INCLUDES
);
979 if (flags
& TRANSPORT_PUSH_OPTIONS
) {
980 struct string_list_item
*item
;
981 for_each_string_list_item(item
, transport
->push_options
)
982 if (set_helper_option(transport
, "push-option", item
->string
) != 0)
983 die(_("helper %s does not support 'push-option'"), name
);
987 static int push_refs_with_push(struct transport
*transport
,
988 struct ref
*remote_refs
, int flags
)
990 int force_all
= flags
& TRANSPORT_PUSH_FORCE
;
991 int mirror
= flags
& TRANSPORT_PUSH_MIRROR
;
992 int atomic
= flags
& TRANSPORT_PUSH_ATOMIC
;
993 struct helper_data
*data
= transport
->data
;
994 struct strbuf buf
= STRBUF_INIT
;
996 struct string_list cas_options
= STRING_LIST_INIT_DUP
;
997 struct string_list_item
*cas_option
;
999 get_helper(transport
);
1003 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1004 if (!ref
->peer_ref
&& !mirror
)
1007 /* Check for statuses set by set_ref_status_for_push() */
1008 switch (ref
->status
) {
1009 case REF_STATUS_REJECT_NONFASTFORWARD
:
1010 case REF_STATUS_REJECT_STALE
:
1011 case REF_STATUS_REJECT_ALREADY_EXISTS
:
1012 case REF_STATUS_REJECT_REMOTE_UPDATED
:
1014 reject_atomic_push(remote_refs
, mirror
);
1015 string_list_clear(&cas_options
, 0);
1019 case REF_STATUS_UPTODATE
:
1028 strbuf_addstr(&buf
, "push ");
1029 if (!ref
->deletion
) {
1031 strbuf_addch(&buf
, '+');
1033 strbuf_addstr(&buf
, ref
->peer_ref
->name
);
1035 strbuf_addstr(&buf
, oid_to_hex(&ref
->new_oid
));
1037 strbuf_addch(&buf
, ':');
1038 strbuf_addstr(&buf
, ref
->name
);
1039 strbuf_addch(&buf
, '\n');
1042 * The "--force-with-lease" options without explicit
1043 * values to expect have already been expanded into
1044 * the ref->old_oid_expect[] field; we can ignore
1045 * transport->smart_options->cas altogether and instead
1046 * can enumerate them from the refs.
1048 if (ref
->expect_old_sha1
) {
1049 struct strbuf cas
= STRBUF_INIT
;
1050 strbuf_addf(&cas
, "%s:%s",
1051 ref
->name
, oid_to_hex(&ref
->old_oid_expect
));
1052 string_list_append_nodup(&cas_options
,
1053 strbuf_detach(&cas
, NULL
));
1057 string_list_clear(&cas_options
, 0);
1061 for_each_string_list_item(cas_option
, &cas_options
)
1062 set_helper_option(transport
, "cas", cas_option
->string
);
1063 set_common_push_options(transport
, data
->name
, flags
);
1065 strbuf_addch(&buf
, '\n');
1066 sendline(data
, &buf
);
1067 strbuf_release(&buf
);
1068 string_list_clear(&cas_options
, 0);
1070 return push_update_refs_status(data
, remote_refs
, flags
);
1073 static int push_refs_with_export(struct transport
*transport
,
1074 struct ref
*remote_refs
, int flags
)
1077 struct child_process
*helper
, exporter
;
1078 struct helper_data
*data
= transport
->data
;
1079 struct string_list revlist_args
= STRING_LIST_INIT_DUP
;
1080 struct strbuf buf
= STRBUF_INIT
;
1083 die(_("remote-helper doesn't support push; refspec needed"));
1085 set_common_push_options(transport
, data
->name
, flags
);
1086 if (flags
& TRANSPORT_PUSH_FORCE
) {
1087 if (set_helper_option(transport
, "force", "true") != 0)
1088 warning(_("helper %s does not support '--force'"), data
->name
);
1091 helper
= get_helper(transport
);
1093 write_constant(helper
->in
, "export\n");
1095 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1097 struct object_id oid
;
1099 private = apply_refspecs(&data
->rs
, ref
->name
);
1100 if (private && !repo_get_oid(the_repository
, private, &oid
)) {
1101 strbuf_addf(&buf
, "^%s", private);
1102 string_list_append_nodup(&revlist_args
,
1103 strbuf_detach(&buf
, NULL
));
1104 oidcpy(&ref
->old_oid
, &oid
);
1108 if (ref
->peer_ref
) {
1109 if (strcmp(ref
->name
, ref
->peer_ref
->name
)) {
1110 if (!ref
->deletion
) {
1114 /* Follow symbolic refs (mainly for HEAD). */
1115 name
= refs_resolve_ref_unsafe(get_main_ref_store(the_repository
),
1116 ref
->peer_ref
->name
,
1117 RESOLVE_REF_READING
,
1120 if (!name
|| !(flag
& REF_ISSYMREF
))
1121 name
= ref
->peer_ref
->name
;
1123 strbuf_addf(&buf
, "%s:%s", name
, ref
->name
);
1125 strbuf_addf(&buf
, ":%s", ref
->name
);
1127 string_list_append(&revlist_args
, "--refspec");
1128 string_list_append(&revlist_args
, buf
.buf
);
1129 strbuf_release(&buf
);
1132 string_list_append(&revlist_args
, ref
->peer_ref
->name
);
1136 if (get_exporter(transport
, &exporter
, &revlist_args
))
1137 die(_("couldn't run fast-export"));
1139 string_list_clear(&revlist_args
, 1);
1141 if (finish_command(&exporter
))
1142 die(_("error while running fast-export"));
1143 if (push_update_refs_status(data
, remote_refs
, flags
))
1146 if (data
->export_marks
) {
1147 strbuf_addf(&buf
, "%s.tmp", data
->export_marks
);
1148 rename(buf
.buf
, data
->export_marks
);
1149 strbuf_release(&buf
);
1155 static int push_refs(struct transport
*transport
,
1156 struct ref
*remote_refs
, int flags
)
1158 struct helper_data
*data
= transport
->data
;
1160 if (process_connect(transport
, 1))
1161 return transport
->vtable
->push_refs(transport
, remote_refs
, flags
);
1165 _("No refs in common and none specified; doing nothing.\n"
1166 "Perhaps you should specify a branch.\n"));
1171 return push_refs_with_push(transport
, remote_refs
, flags
);
1174 return push_refs_with_export(transport
, remote_refs
, flags
);
1180 static int has_attribute(const char *attrs
, const char *attr
)
1188 const char *space
= strchrnul(attrs
, ' ');
1189 if (len
== space
- attrs
&& !strncmp(attrs
, attr
, len
))
1197 static struct ref
*get_refs_list(struct transport
*transport
, int for_push
,
1198 struct transport_ls_refs_options
*transport_options
)
1200 get_helper(transport
);
1202 if (process_connect(transport
, for_push
))
1203 return transport
->vtable
->get_refs_list(transport
, for_push
,
1206 return get_refs_list_using_list(transport
, for_push
);
1209 static struct ref
*get_refs_list_using_list(struct transport
*transport
,
1212 struct helper_data
*data
= transport
->data
;
1213 struct child_process
*helper
;
1214 struct ref
*ret
= NULL
;
1215 struct ref
**tail
= &ret
;
1217 struct strbuf buf
= STRBUF_INIT
;
1219 data
->get_refs_list_called
= 1;
1220 helper
= get_helper(transport
);
1222 if (data
->object_format
)
1223 set_helper_option(transport
, "object-format", "true");
1225 if (data
->push
&& for_push
)
1226 write_constant(helper
->in
, "list for-push\n");
1228 write_constant(helper
->in
, "list\n");
1232 if (recvline(data
, &buf
))
1237 else if (buf
.buf
[0] == ':') {
1239 if (skip_prefix(buf
.buf
, ":object-format ", &value
)) {
1240 int algo
= hash_algo_by_name(value
);
1241 if (algo
== GIT_HASH_UNKNOWN
)
1242 die(_("unsupported object format '%s'"),
1244 transport
->hash_algo
= &hash_algos
[algo
];
1249 eov
= strchr(buf
.buf
, ' ');
1251 die(_("malformed response in ref list: %s"), buf
.buf
);
1252 eon
= strchr(eov
+ 1, ' ');
1256 *tail
= alloc_ref(eov
+ 1);
1257 if (buf
.buf
[0] == '@')
1258 (*tail
)->symref
= xstrdup(buf
.buf
+ 1);
1259 else if (buf
.buf
[0] != '?')
1260 get_oid_hex_algop(buf
.buf
, &(*tail
)->old_oid
, transport
->hash_algo
);
1262 if (has_attribute(eon
+ 1, "unchanged")) {
1263 (*tail
)->status
|= REF_STATUS_UPTODATE
;
1264 if (refs_read_ref(get_main_ref_store(the_repository
), (*tail
)->name
, &(*tail
)->old_oid
) < 0)
1265 die(_("could not read ref %s"),
1269 tail
= &((*tail
)->next
);
1272 fprintf(stderr
, "Debug: Read ref listing.\n");
1273 strbuf_release(&buf
);
1275 for (posn
= ret
; posn
; posn
= posn
->next
)
1276 resolve_remote_symref(posn
, ret
);
1281 static int get_bundle_uri(struct transport
*transport
)
1283 get_helper(transport
);
1285 if (process_connect(transport
, 0))
1286 return transport
->vtable
->get_bundle_uri(transport
);
1291 static struct transport_vtable vtable
= {
1292 .set_option
= set_helper_option
,
1293 .get_refs_list
= get_refs_list
,
1294 .get_bundle_uri
= get_bundle_uri
,
1295 .fetch_refs
= fetch_refs
,
1296 .push_refs
= push_refs
,
1297 .connect
= connect_helper
,
1298 .disconnect
= release_helper
1301 int transport_helper_init(struct transport
*transport
, const char *name
)
1303 struct helper_data
*data
= xcalloc(1, sizeof(*data
));
1304 data
->name
= xstrdup(name
);
1306 transport_check_allowed(name
);
1308 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
1311 list_objects_filter_init(&data
->transport_options
.filter_options
);
1313 transport
->data
= data
;
1314 transport
->vtable
= &vtable
;
1315 transport
->smart_options
= &(data
->transport_options
);
1320 * Linux pipes can buffer 65536 bytes at once (and most platforms can
1321 * buffer less), so attempt reads and writes with up to that size.
1323 #define BUFFERSIZE 65536
1324 /* This should be enough to hold debugging message. */
1325 #define PBUFFERSIZE 8192
1327 /* Print bidirectional transfer loop debug message. */
1328 __attribute__((format (printf
, 1, 2)))
1329 static void transfer_debug(const char *fmt
, ...)
1332 * NEEDSWORK: This function is sometimes used from multiple threads, and
1333 * we end up using debug_enabled racily. That "should not matter" since
1334 * we always write the same value, but it's still wrong. This function
1335 * is listed in .tsan-suppressions for the time being.
1339 char msgbuf
[PBUFFERSIZE
];
1340 static int debug_enabled
= -1;
1342 if (debug_enabled
< 0)
1343 debug_enabled
= getenv("GIT_TRANSLOOP_DEBUG") ? 1 : 0;
1347 va_start(args
, fmt
);
1348 vsnprintf(msgbuf
, PBUFFERSIZE
, fmt
, args
);
1350 fprintf(stderr
, "Transfer loop debugging: %s\n", msgbuf
);
1353 /* Stream state: More data may be coming in this direction. */
1354 #define SSTATE_TRANSFERRING 0
1356 * Stream state: No more data coming in this direction, flushing rest of
1359 #define SSTATE_FLUSHING 1
1360 /* Stream state: Transfer in this direction finished. */
1361 #define SSTATE_FINISHED 2
1363 #define STATE_NEEDS_READING(state) ((state) <= SSTATE_TRANSFERRING)
1364 #define STATE_NEEDS_WRITING(state) ((state) <= SSTATE_FLUSHING)
1365 #define STATE_NEEDS_CLOSING(state) ((state) == SSTATE_FLUSHING)
1367 /* Unidirectional transfer. */
1368 struct unidirectional_transfer
{
1373 /* Is source socket? */
1375 /* Is destination socket? */
1377 /* Transfer state (TRANSFERRING/FLUSHING/FINISHED) */
1380 char buf
[BUFFERSIZE
];
1383 /* Name of source. */
1384 const char *src_name
;
1385 /* Name of destination. */
1386 const char *dest_name
;
1389 /* Closes the target (for writing) if transfer has finished. */
1390 static void udt_close_if_finished(struct unidirectional_transfer
*t
)
1392 if (STATE_NEEDS_CLOSING(t
->state
) && !t
->bufuse
) {
1393 t
->state
= SSTATE_FINISHED
;
1394 if (t
->dest_is_sock
)
1395 shutdown(t
->dest
, SHUT_WR
);
1398 transfer_debug("Closed %s.", t
->dest_name
);
1403 * Tries to read data from source into buffer. If buffer is full,
1404 * no data is read. Returns 0 on success, -1 on error.
1406 static int udt_do_read(struct unidirectional_transfer
*t
)
1410 if (t
->bufuse
== BUFFERSIZE
)
1411 return 0; /* No space for more. */
1413 transfer_debug("%s is readable", t
->src_name
);
1414 bytes
= xread(t
->src
, t
->buf
+ t
->bufuse
, BUFFERSIZE
- t
->bufuse
);
1416 error_errno(_("read(%s) failed"), t
->src_name
);
1418 } else if (bytes
== 0) {
1419 transfer_debug("%s EOF (with %i bytes in buffer)",
1420 t
->src_name
, (int)t
->bufuse
);
1421 t
->state
= SSTATE_FLUSHING
;
1422 } else if (bytes
> 0) {
1424 transfer_debug("Read %i bytes from %s (buffer now at %i)",
1425 (int)bytes
, t
->src_name
, (int)t
->bufuse
);
1430 /* Tries to write data from buffer into destination. If buffer is empty,
1431 * no data is written. Returns 0 on success, -1 on error.
1433 static int udt_do_write(struct unidirectional_transfer
*t
)
1438 return 0; /* Nothing to write. */
1440 transfer_debug("%s is writable", t
->dest_name
);
1441 bytes
= xwrite(t
->dest
, t
->buf
, t
->bufuse
);
1443 error_errno(_("write(%s) failed"), t
->dest_name
);
1445 } else if (bytes
> 0) {
1448 memmove(t
->buf
, t
->buf
+ bytes
, t
->bufuse
);
1449 transfer_debug("Wrote %i bytes to %s (buffer now at %i)",
1450 (int)bytes
, t
->dest_name
, (int)t
->bufuse
);
1456 /* State of bidirectional transfer loop. */
1457 struct bidirectional_transfer_state
{
1458 /* Direction from program to git. */
1459 struct unidirectional_transfer ptg
;
1460 /* Direction from git to program. */
1461 struct unidirectional_transfer gtp
;
1464 static void *udt_copy_task_routine(void *udt
)
1466 struct unidirectional_transfer
*t
= (struct unidirectional_transfer
*)udt
;
1467 while (t
->state
!= SSTATE_FINISHED
) {
1468 if (STATE_NEEDS_READING(t
->state
))
1471 if (STATE_NEEDS_WRITING(t
->state
))
1472 if (udt_do_write(t
))
1474 if (STATE_NEEDS_CLOSING(t
->state
))
1475 udt_close_if_finished(t
);
1477 return udt
; /* Just some non-NULL value. */
1483 * Join thread, with appropriate errors on failure. Name is name for the
1484 * thread (for error messages). Returns 0 on success, 1 on failure.
1486 static int tloop_join(pthread_t thread
, const char *name
)
1490 err
= pthread_join(thread
, &tret
);
1492 error(_("%s thread failed"), name
);
1496 error(_("%s thread failed to join: %s"), name
, strerror(err
));
1503 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1506 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1508 pthread_t gtp_thread
;
1509 pthread_t ptg_thread
;
1512 err
= pthread_create(>p_thread
, NULL
, udt_copy_task_routine
,
1515 die(_("can't start thread for copying data: %s"), strerror(err
));
1516 err
= pthread_create(&ptg_thread
, NULL
, udt_copy_task_routine
,
1519 die(_("can't start thread for copying data: %s"), strerror(err
));
1521 ret
|= tloop_join(gtp_thread
, "Git to program copy");
1522 ret
|= tloop_join(ptg_thread
, "Program to git copy");
1527 /* Close the source and target (for writing) for transfer. */
1528 static void udt_kill_transfer(struct unidirectional_transfer
*t
)
1530 t
->state
= SSTATE_FINISHED
;
1532 * Socket read end left open isn't a disaster if nobody
1533 * attempts to read from it (mingw compat headers do not
1536 * We can't fully close the socket since otherwise gtp
1537 * task would first close the socket it sends data to
1538 * while closing the ptg file descriptors.
1540 if (!t
->src_is_sock
)
1542 if (t
->dest_is_sock
)
1543 shutdown(t
->dest
, SHUT_WR
);
1549 * Join process, with appropriate errors on failure. Name is name for the
1550 * process (for error messages). Returns 0 on success, 1 on failure.
1552 static int tloop_join(pid_t pid
, const char *name
)
1555 if (waitpid(pid
, &tret
, 0) < 0) {
1556 error_errno(_("%s process failed to wait"), name
);
1559 if (!WIFEXITED(tret
) || WEXITSTATUS(tret
)) {
1560 error(_("%s process failed"), name
);
1567 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1570 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1575 /* Fork thread #1: git to program. */
1578 die_errno(_("can't start thread for copying data"));
1579 else if (pid1
== 0) {
1580 udt_kill_transfer(&s
->ptg
);
1581 exit(udt_copy_task_routine(&s
->gtp
) ? 0 : 1);
1584 /* Fork thread #2: program to git. */
1587 die_errno(_("can't start thread for copying data"));
1588 else if (pid2
== 0) {
1589 udt_kill_transfer(&s
->gtp
);
1590 exit(udt_copy_task_routine(&s
->ptg
) ? 0 : 1);
1594 * Close both streams in parent as to not interfere with
1595 * end of file detection and wait for both tasks to finish.
1597 udt_kill_transfer(&s
->gtp
);
1598 udt_kill_transfer(&s
->ptg
);
1599 ret
|= tloop_join(pid1
, "Git to program copy");
1600 ret
|= tloop_join(pid2
, "Program to git copy");
1606 * Copies data from stdin to output and from input to stdout simultaneously.
1607 * Additionally filtering through given filter. If filter is NULL, uses
1610 int bidirectional_transfer_loop(int input
, int output
)
1612 struct bidirectional_transfer_state state
;
1614 /* Fill the state fields. */
1615 state
.ptg
.src
= input
;
1617 state
.ptg
.src_is_sock
= (input
== output
);
1618 state
.ptg
.dest_is_sock
= 0;
1619 state
.ptg
.state
= SSTATE_TRANSFERRING
;
1620 state
.ptg
.bufuse
= 0;
1621 state
.ptg
.src_name
= "remote input";
1622 state
.ptg
.dest_name
= "stdout";
1625 state
.gtp
.dest
= output
;
1626 state
.gtp
.src_is_sock
= 0;
1627 state
.gtp
.dest_is_sock
= (input
== output
);
1628 state
.gtp
.state
= SSTATE_TRANSFERRING
;
1629 state
.gtp
.bufuse
= 0;
1630 state
.gtp
.src_name
= "stdin";
1631 state
.gtp
.dest_name
= "remote output";
1633 return tloop_spawnwait_tasks(&state
);
1636 void reject_atomic_push(struct ref
*remote_refs
, int mirror_mode
)
1640 /* Mark other refs as failed */
1641 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1642 if (!ref
->peer_ref
&& !mirror_mode
)
1645 switch (ref
->status
) {
1646 case REF_STATUS_NONE
:
1648 case REF_STATUS_EXPECTING_REPORT
:
1649 ref
->status
= REF_STATUS_ATOMIC_PUSH_FAILED
;
1652 break; /* do nothing */