4 #include "run-command.h"
6 #include "fetch-pack.h"
17 #include "submodule.h"
18 #include "string-list.h"
19 #include "oid-array.h"
21 #include "transport-internal.h"
23 #include "object-store.h"
26 static int transport_use_color = -1;
27 static char transport_colors[][COLOR_MAXLEN] = {
29 GIT_COLOR_RED /* REJECTED */
32 enum color_transport {
33 TRANSPORT_COLOR_RESET = 0,
34 TRANSPORT_COLOR_REJECTED = 1
37 static int transport_color_config(void)
39 const char *keys[] = {
40 "color.transport.reset",
41 "color.transport.rejected"
42 }, *key = "color.transport";
45 static int initialized;
51 if (!git_config_get_string(key, &value))
52 transport_use_color = git_config_colorbool(key, value);
54 if (!want_color_stderr(transport_use_color))
57 for (i = 0; i < ARRAY_SIZE(keys); i++)
58 if (!git_config_get_string(keys[i], &value)) {
60 return config_error_nonbool(keys[i]);
61 if (color_parse(value, transport_colors[i]) < 0)
68 static const char *transport_get_color(enum color_transport ix)
70 if (want_color_stderr(transport_use_color))
71 return transport_colors[ix];
75 static void set_upstreams(struct transport *transport, struct ref *refs,
79 for (ref = refs; ref; ref = ref->next) {
80 const char *localname;
82 const char *remotename;
85 * Check suitability for tracking. Must be successful /
86 * already up-to-date ref create/modify (not delete).
88 if (ref->status != REF_STATUS_OK &&
89 ref->status != REF_STATUS_UPTODATE)
93 if (is_null_oid(&ref->new_oid))
96 /* Follow symbolic refs (mainly for HEAD). */
97 localname = ref->peer_ref->name;
98 remotename = ref->name;
99 tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING,
101 if (tmp && flag & REF_ISSYMREF &&
102 starts_with(tmp, "refs/heads/"))
105 /* Both source and destination must be local branches. */
106 if (!localname || !starts_with(localname, "refs/heads/"))
108 if (!remotename || !starts_with(remotename, "refs/heads/"))
112 install_branch_config(BRANCH_CONFIG_VERBOSE,
113 localname + 11, transport->remote->name,
116 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
117 localname + 11, remotename + 11,
118 transport->remote->name);
122 struct bundle_transport_data {
124 struct bundle_header header;
125 unsigned get_refs_from_bundle_called : 1;
128 static struct ref *get_refs_from_bundle(struct transport *transport,
130 const struct argv_array *ref_prefixes)
132 struct bundle_transport_data *data = transport->data;
133 struct ref *result = NULL;
139 data->get_refs_from_bundle_called = 1;
143 data->fd = read_bundle_header(transport->url, &data->header);
145 die(_("could not read bundle '%s'"), transport->url);
146 for (i = 0; i < data->header.references.nr; i++) {
147 struct ref_list_entry *e = data->header.references.list + i;
148 struct ref *ref = alloc_ref(e->name);
149 oidcpy(&ref->old_oid, &e->oid);
156 static int fetch_refs_from_bundle(struct transport *transport,
157 int nr_heads, struct ref **to_fetch)
159 struct bundle_transport_data *data = transport->data;
161 if (!data->get_refs_from_bundle_called)
162 get_refs_from_bundle(transport, 0, NULL);
163 return unbundle(the_repository, &data->header, data->fd,
164 transport->progress ? BUNDLE_VERBOSE : 0);
167 static int close_bundle(struct transport *transport)
169 struct bundle_transport_data *data = transport->data;
176 struct git_transport_data {
177 struct git_transport_options options;
178 struct child_process *conn;
180 unsigned got_remote_heads : 1;
181 enum protocol_version version;
182 struct oid_array extra_have;
183 struct oid_array shallow;
186 static int set_git_option(struct git_transport_options *opts,
187 const char *name, const char *value)
189 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
190 opts->uploadpack = value;
192 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
193 opts->receivepack = value;
195 } else if (!strcmp(name, TRANS_OPT_THIN)) {
196 opts->thin = !!value;
198 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
199 opts->followtags = !!value;
201 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
202 opts->keep = !!value;
204 } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
205 opts->update_shallow = !!value;
207 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
212 opts->depth = strtol(value, &end, 0);
214 die(_("transport: invalid depth option '%s'"), value);
217 } else if (!strcmp(name, TRANS_OPT_DEEPEN_SINCE)) {
218 opts->deepen_since = value;
220 } else if (!strcmp(name, TRANS_OPT_DEEPEN_NOT)) {
221 opts->deepen_not = (const struct string_list *)value;
223 } else if (!strcmp(name, TRANS_OPT_DEEPEN_RELATIVE)) {
224 opts->deepen_relative = !!value;
226 } else if (!strcmp(name, TRANS_OPT_FROM_PROMISOR)) {
227 opts->from_promisor = !!value;
229 } else if (!strcmp(name, TRANS_OPT_NO_DEPENDENTS)) {
230 opts->no_dependents = !!value;
232 } else if (!strcmp(name, TRANS_OPT_LIST_OBJECTS_FILTER)) {
233 list_objects_filter_die_if_populated(&opts->filter_options);
234 parse_list_objects_filter(&opts->filter_options, value);
240 static int connect_setup(struct transport *transport, int for_push)
242 struct git_transport_data *data = transport->data;
243 int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
248 switch (transport->family) {
249 case TRANSPORT_FAMILY_ALL: break;
250 case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
251 case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
254 data->conn = git_connect(data->fd, transport->url,
255 for_push ? data->options.receivepack :
256 data->options.uploadpack,
262 static void die_if_server_options(struct transport *transport)
264 if (!transport->server_options || !transport->server_options->nr)
266 advise(_("see protocol.version in 'git help config' for more details"));
267 die(_("server options require protocol version 2 or later"));
271 * Obtains the protocol version from the transport and writes it to
272 * transport->data->version, first connecting if not already connected.
274 * If the protocol version is one that allows skipping the listing of remote
275 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
276 * this function returns NULL. Otherwise, this function returns the list of
279 static struct ref *handshake(struct transport *transport, int for_push,
280 const struct argv_array *ref_prefixes,
283 struct git_transport_data *data = transport->data;
284 struct ref *refs = NULL;
285 struct packet_reader reader;
287 connect_setup(transport, for_push);
289 packet_reader_init(&reader, data->fd[0], NULL, 0,
290 PACKET_READ_CHOMP_NEWLINE |
291 PACKET_READ_GENTLE_ON_EOF |
292 PACKET_READ_DIE_ON_ERR_PACKET);
294 data->version = discover_version(&reader);
295 switch (data->version) {
298 get_remote_refs(data->fd[1], &reader, &refs, for_push,
300 transport->server_options);
304 die_if_server_options(transport);
305 get_remote_heads(&reader, &refs,
306 for_push ? REF_NORMAL : 0,
310 case protocol_unknown_version:
311 BUG("unknown protocol version");
313 data->got_remote_heads = 1;
315 if (reader.line_peeked)
316 BUG("buffer must be empty at the end of handshake()");
321 static struct ref *get_refs_via_connect(struct transport *transport, int for_push,
322 const struct argv_array *ref_prefixes)
324 return handshake(transport, for_push, ref_prefixes, 1);
327 static int fetch_refs_via_pack(struct transport *transport,
328 int nr_heads, struct ref **to_fetch)
331 struct git_transport_data *data = transport->data;
332 struct ref *refs = NULL;
333 struct fetch_pack_args args;
334 struct ref *refs_tmp = NULL;
336 memset(&args, 0, sizeof(args));
337 args.uploadpack = data->options.uploadpack;
338 args.keep_pack = data->options.keep;
340 args.use_thin_pack = data->options.thin;
341 args.include_tag = data->options.followtags;
342 args.verbose = (transport->verbose > 1);
343 args.quiet = (transport->verbose < 0);
344 args.no_progress = !transport->progress;
345 args.depth = data->options.depth;
346 args.deepen_since = data->options.deepen_since;
347 args.deepen_not = data->options.deepen_not;
348 args.deepen_relative = data->options.deepen_relative;
349 args.check_self_contained_and_connected =
350 data->options.check_self_contained_and_connected;
351 args.cloning = transport->cloning;
352 args.update_shallow = data->options.update_shallow;
353 args.from_promisor = data->options.from_promisor;
354 args.no_dependents = data->options.no_dependents;
355 args.filter_options = data->options.filter_options;
356 args.stateless_rpc = transport->stateless_rpc;
357 args.server_options = transport->server_options;
358 args.negotiation_tips = data->options.negotiation_tips;
360 if (!data->got_remote_heads) {
362 int must_list_refs = 0;
363 for (i = 0; i < nr_heads; i++) {
364 if (!to_fetch[i]->exact_oid) {
369 refs_tmp = handshake(transport, 0, NULL, must_list_refs);
372 switch (data->version) {
374 refs = fetch_pack(&args, data->fd,
375 refs_tmp ? refs_tmp : transport->remote_refs,
376 to_fetch, nr_heads, &data->shallow,
377 &transport->pack_lockfile, data->version);
381 die_if_server_options(transport);
382 refs = fetch_pack(&args, data->fd,
383 refs_tmp ? refs_tmp : transport->remote_refs,
384 to_fetch, nr_heads, &data->shallow,
385 &transport->pack_lockfile, data->version);
387 case protocol_unknown_version:
388 BUG("unknown protocol version");
393 if (finish_connect(data->conn))
396 data->got_remote_heads = 0;
397 data->options.self_contained_and_connected =
398 args.self_contained_and_connected;
399 data->options.connectivity_checked = args.connectivity_checked;
403 if (report_unmatched_refs(to_fetch, nr_heads))
411 static int push_had_errors(struct ref *ref)
413 for (; ref; ref = ref->next) {
414 switch (ref->status) {
415 case REF_STATUS_NONE:
416 case REF_STATUS_UPTODATE:
426 int transport_refs_pushed(struct ref *ref)
428 for (; ref; ref = ref->next) {
429 switch(ref->status) {
430 case REF_STATUS_NONE:
431 case REF_STATUS_UPTODATE:
440 static void update_one_tracking_ref(struct remote *remote, char *refname,
441 struct object_id *new_oid, int deletion,
444 struct refspec_item rs;
449 if (!remote_find_tracking(remote, &rs)) {
451 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
453 delete_ref(NULL, rs.dst, NULL, 0);
455 update_ref("update by push", rs.dst, new_oid,
461 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
464 struct object_id *new_oid;
465 struct ref_push_report *report;
467 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
470 report = ref->report;
472 update_one_tracking_ref(remote, ref->name, &ref->new_oid,
473 ref->deletion, verbose);
475 for (; report; report = report->next) {
476 refname = report->ref_name ? (char *)report->ref_name : ref->name;
477 new_oid = report->new_oid ? report->new_oid : &ref->new_oid;
478 update_one_tracking_ref(remote, refname, new_oid,
479 is_null_oid(new_oid), verbose);
483 static void print_ref_status(char flag, const char *summary,
484 struct ref *to, struct ref *from, const char *msg,
485 struct ref_push_report *report,
486 int porcelain, int summary_width)
490 if (report && report->ref_name)
491 to_name = report->ref_name;
497 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to_name);
499 fprintf(stdout, "%c\t:%s\t", flag, to_name);
501 fprintf(stdout, "%s (%s)\n", summary, msg);
503 fprintf(stdout, "%s\n", summary);
505 const char *red = "", *reset = "";
506 if (push_had_errors(to)) {
507 red = transport_get_color(TRANSPORT_COLOR_REJECTED);
508 reset = transport_get_color(TRANSPORT_COLOR_RESET);
510 fprintf(stderr, " %s%c %-*s%s ", red, flag, summary_width,
513 fprintf(stderr, "%s -> %s",
514 prettify_refname(from->name),
515 prettify_refname(to_name));
517 fputs(prettify_refname(to_name), stderr);
527 static void print_ok_ref_status(struct ref *ref,
528 struct ref_push_report *report,
529 int porcelain, int summary_width)
531 struct object_id *old_oid;
532 struct object_id *new_oid;
533 const char *ref_name;
536 if (report && report->old_oid)
537 old_oid = report->old_oid;
539 old_oid = &ref->old_oid;
540 if (report && report->new_oid)
541 new_oid = report->new_oid;
543 new_oid = &ref->new_oid;
544 if (report && report->forced_update)
545 forced_update = report->forced_update;
547 forced_update = ref->forced_update;
548 if (report && report->ref_name)
549 ref_name = report->ref_name;
551 ref_name = ref->name;
554 print_ref_status('-', "[deleted]", ref, NULL, NULL,
555 report, porcelain, summary_width);
556 else if (is_null_oid(old_oid))
557 print_ref_status('*',
558 (starts_with(ref_name, "refs/tags/")
560 : (starts_with(ref_name, "refs/heads/")
562 : "[new reference]")),
563 ref, ref->peer_ref, NULL,
564 report, porcelain, summary_width);
566 struct strbuf quickref = STRBUF_INIT;
570 strbuf_add_unique_abbrev(&quickref, old_oid,
573 strbuf_addstr(&quickref, "...");
575 msg = "forced update";
577 strbuf_addstr(&quickref, "..");
581 strbuf_add_unique_abbrev(&quickref, new_oid,
584 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
585 report, porcelain, summary_width);
586 strbuf_release(&quickref);
590 static int print_one_push_report(struct ref *ref, const char *dest, int count,
591 struct ref_push_report *report,
592 int porcelain, int summary_width)
595 char *url = transport_anonymize_url(dest);
596 fprintf(porcelain ? stdout : stderr, "To %s\n", url);
600 switch(ref->status) {
601 case REF_STATUS_NONE:
602 print_ref_status('X', "[no match]", ref, NULL, NULL,
603 report, porcelain, summary_width);
605 case REF_STATUS_REJECT_NODELETE:
606 print_ref_status('!', "[rejected]", ref, NULL,
607 "remote does not support deleting refs",
608 report, porcelain, summary_width);
610 case REF_STATUS_UPTODATE:
611 print_ref_status('=', "[up to date]", ref,
613 report, porcelain, summary_width);
615 case REF_STATUS_REJECT_NONFASTFORWARD:
616 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
618 report, porcelain, summary_width);
620 case REF_STATUS_REJECT_ALREADY_EXISTS:
621 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
623 report, porcelain, summary_width);
625 case REF_STATUS_REJECT_FETCH_FIRST:
626 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
628 report, porcelain, summary_width);
630 case REF_STATUS_REJECT_NEEDS_FORCE:
631 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
633 report, porcelain, summary_width);
635 case REF_STATUS_REJECT_STALE:
636 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
638 report, porcelain, summary_width);
640 case REF_STATUS_REJECT_SHALLOW:
641 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
642 "new shallow roots not allowed",
643 report, porcelain, summary_width);
645 case REF_STATUS_REMOTE_REJECT:
646 print_ref_status('!', "[remote rejected]", ref,
647 ref->deletion ? NULL : ref->peer_ref,
649 report, porcelain, summary_width);
651 case REF_STATUS_EXPECTING_REPORT:
652 print_ref_status('!', "[remote failure]", ref,
653 ref->deletion ? NULL : ref->peer_ref,
654 "remote failed to report status",
655 report, porcelain, summary_width);
657 case REF_STATUS_ATOMIC_PUSH_FAILED:
658 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
659 "atomic push failed",
660 report, porcelain, summary_width);
663 print_ok_ref_status(ref, report, porcelain, summary_width);
670 static int print_one_push_status(struct ref *ref, const char *dest, int count,
671 int porcelain, int summary_width)
673 struct ref_push_report *report;
677 return print_one_push_report(ref, dest, count,
678 NULL, porcelain, summary_width);
680 for (report = ref->report; report; report = report->next)
681 print_one_push_report(ref, dest, count + n++,
682 report, porcelain, summary_width);
686 static int measure_abbrev(const struct object_id *oid, int sofar)
688 char hex[GIT_MAX_HEXSZ + 1];
689 int w = find_unique_abbrev_r(hex, oid, DEFAULT_ABBREV);
691 return (w < sofar) ? sofar : w;
694 int transport_summary_width(const struct ref *refs)
698 for (; refs; refs = refs->next) {
699 maxw = measure_abbrev(&refs->old_oid, maxw);
700 maxw = measure_abbrev(&refs->new_oid, maxw);
703 maxw = FALLBACK_DEFAULT_ABBREV;
704 return (2 * maxw + 3);
707 void transport_print_push_status(const char *dest, struct ref *refs,
708 int verbose, int porcelain, unsigned int *reject_reasons)
713 int summary_width = transport_summary_width(refs);
715 if (transport_color_config() < 0)
716 warning(_("could not parse transport.color.* config"));
718 head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
721 for (ref = refs; ref; ref = ref->next)
722 if (ref->status == REF_STATUS_UPTODATE)
723 n += print_one_push_status(ref, dest, n,
724 porcelain, summary_width);
727 for (ref = refs; ref; ref = ref->next)
728 if (ref->status == REF_STATUS_OK)
729 n += print_one_push_status(ref, dest, n,
730 porcelain, summary_width);
733 for (ref = refs; ref; ref = ref->next) {
734 if (ref->status != REF_STATUS_NONE &&
735 ref->status != REF_STATUS_UPTODATE &&
736 ref->status != REF_STATUS_OK)
737 n += print_one_push_status(ref, dest, n,
738 porcelain, summary_width);
739 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
740 if (head != NULL && !strcmp(head, ref->name))
741 *reject_reasons |= REJECT_NON_FF_HEAD;
743 *reject_reasons |= REJECT_NON_FF_OTHER;
744 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
745 *reject_reasons |= REJECT_ALREADY_EXISTS;
746 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
747 *reject_reasons |= REJECT_FETCH_FIRST;
748 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
749 *reject_reasons |= REJECT_NEEDS_FORCE;
755 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
757 struct git_transport_data *data = transport->data;
758 struct send_pack_args args;
761 if (transport_color_config() < 0)
764 if (!data->got_remote_heads)
765 get_refs_via_connect(transport, 1, NULL);
767 memset(&args, 0, sizeof(args));
768 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
769 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
770 args.use_thin_pack = data->options.thin;
771 args.verbose = (transport->verbose > 0);
772 args.quiet = (transport->verbose < 0);
773 args.progress = transport->progress;
774 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
775 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
776 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
777 args.push_options = transport->push_options;
778 args.url = transport->url;
780 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
781 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
782 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
783 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
785 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
787 switch (data->version) {
789 die(_("support for protocol v2 not implemented yet"));
793 ret = send_pack(&args, data->fd, data->conn, remote_refs,
796 case protocol_unknown_version:
797 BUG("unknown protocol version");
803 * Atomic push may abort the connection early and close the pipe,
804 * which may cause an error for `finish_connect()`. Ignore this error
805 * for atomic git-push.
807 if (ret || args.atomic)
808 finish_connect(data->conn);
810 ret = finish_connect(data->conn);
812 data->got_remote_heads = 0;
817 static int connect_git(struct transport *transport, const char *name,
818 const char *executable, int fd[2])
820 struct git_transport_data *data = transport->data;
821 data->conn = git_connect(data->fd, transport->url,
828 static int disconnect_git(struct transport *transport)
830 struct git_transport_data *data = transport->data;
832 if (data->got_remote_heads && !transport->stateless_rpc)
833 packet_flush(data->fd[1]);
836 finish_connect(data->conn);
843 static struct transport_vtable taken_over_vtable = {
845 get_refs_via_connect,
852 void transport_take_over(struct transport *transport,
853 struct child_process *child)
855 struct git_transport_data *data;
857 if (!transport->smart_options)
858 BUG("taking over transport requires non-NULL "
859 "smart_options field.");
861 data = xcalloc(1, sizeof(*data));
862 data->options = *transport->smart_options;
864 data->fd[0] = data->conn->out;
865 data->fd[1] = data->conn->in;
866 data->got_remote_heads = 0;
867 transport->data = data;
869 transport->vtable = &taken_over_vtable;
870 transport->smart_options = &(data->options);
872 transport->cannot_reuse = 1;
875 static int is_file(const char *url)
880 return S_ISREG(buf.st_mode);
883 static int external_specification_len(const char *url)
885 return strchr(url, ':') - url;
888 static const struct string_list *protocol_whitelist(void)
890 static int enabled = -1;
891 static struct string_list allowed = STRING_LIST_INIT_DUP;
894 const char *v = getenv("GIT_ALLOW_PROTOCOL");
896 string_list_split(&allowed, v, ':', -1);
897 string_list_sort(&allowed);
904 return enabled ? &allowed : NULL;
907 enum protocol_allow_config {
908 PROTOCOL_ALLOW_NEVER = 0,
909 PROTOCOL_ALLOW_USER_ONLY,
910 PROTOCOL_ALLOW_ALWAYS
913 static enum protocol_allow_config parse_protocol_config(const char *key,
916 if (!strcasecmp(value, "always"))
917 return PROTOCOL_ALLOW_ALWAYS;
918 else if (!strcasecmp(value, "never"))
919 return PROTOCOL_ALLOW_NEVER;
920 else if (!strcasecmp(value, "user"))
921 return PROTOCOL_ALLOW_USER_ONLY;
923 die(_("unknown value for config '%s': %s"), key, value);
926 static enum protocol_allow_config get_protocol_config(const char *type)
928 char *key = xstrfmt("protocol.%s.allow", type);
931 /* first check the per-protocol config */
932 if (!git_config_get_string(key, &value)) {
933 enum protocol_allow_config ret =
934 parse_protocol_config(key, value);
941 /* if defined, fallback to user-defined default for unknown protocols */
942 if (!git_config_get_string("protocol.allow", &value)) {
943 enum protocol_allow_config ret =
944 parse_protocol_config("protocol.allow", value);
949 /* fallback to built-in defaults */
951 if (!strcmp(type, "http") ||
952 !strcmp(type, "https") ||
953 !strcmp(type, "git") ||
954 !strcmp(type, "ssh") ||
955 !strcmp(type, "file"))
956 return PROTOCOL_ALLOW_ALWAYS;
958 /* known scary; err on the side of caution */
959 if (!strcmp(type, "ext"))
960 return PROTOCOL_ALLOW_NEVER;
962 /* unknown; by default let them be used only directly by the user */
963 return PROTOCOL_ALLOW_USER_ONLY;
966 int is_transport_allowed(const char *type, int from_user)
968 const struct string_list *whitelist = protocol_whitelist();
970 return string_list_has_string(whitelist, type);
972 switch (get_protocol_config(type)) {
973 case PROTOCOL_ALLOW_ALWAYS:
975 case PROTOCOL_ALLOW_NEVER:
977 case PROTOCOL_ALLOW_USER_ONLY:
979 from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
983 BUG("invalid protocol_allow_config type");
986 void transport_check_allowed(const char *type)
988 if (!is_transport_allowed(type, -1))
989 die(_("transport '%s' not allowed"), type);
992 static struct transport_vtable bundle_vtable = {
994 get_refs_from_bundle,
995 fetch_refs_from_bundle,
1001 static struct transport_vtable builtin_smart_vtable = {
1003 get_refs_via_connect,
1004 fetch_refs_via_pack,
1010 struct transport *transport_get(struct remote *remote, const char *url)
1013 struct transport *ret = xcalloc(1, sizeof(*ret));
1015 ret->progress = isatty(2);
1018 BUG("No remote provided to transport_get()");
1020 ret->got_remote_refs = 0;
1021 ret->remote = remote;
1022 helper = remote->foreign_vcs;
1024 if (!url && remote->url)
1025 url = remote->url[0];
1028 /* maybe it is a foreign URL? */
1030 const char *p = url;
1032 while (is_urlschemechar(p == url, *p))
1034 if (starts_with(p, "::"))
1035 helper = xstrndup(url, p - url);
1039 transport_helper_init(ret, helper);
1040 } else if (starts_with(url, "rsync:")) {
1041 die(_("git-over-rsync is no longer supported"));
1042 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
1043 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
1044 transport_check_allowed("file");
1046 ret->vtable = &bundle_vtable;
1047 ret->smart_options = NULL;
1048 } else if (!is_url(url)
1049 || starts_with(url, "file://")
1050 || starts_with(url, "git://")
1051 || starts_with(url, "ssh://")
1052 || starts_with(url, "git+ssh://") /* deprecated - do not use */
1053 || starts_with(url, "ssh+git://") /* deprecated - do not use */
1056 * These are builtin smart transports; "allowed" transports
1057 * will be checked individually in git_connect.
1059 struct git_transport_data *data = xcalloc(1, sizeof(*data));
1061 ret->vtable = &builtin_smart_vtable;
1062 ret->smart_options = &(data->options);
1065 data->got_remote_heads = 0;
1067 /* Unknown protocol in URL. Pass to external handler. */
1068 int len = external_specification_len(url);
1069 char *handler = xmemdupz(url, len);
1070 transport_helper_init(ret, handler);
1073 if (ret->smart_options) {
1074 ret->smart_options->thin = 1;
1075 ret->smart_options->uploadpack = "git-upload-pack";
1076 if (remote->uploadpack)
1077 ret->smart_options->uploadpack = remote->uploadpack;
1078 ret->smart_options->receivepack = "git-receive-pack";
1079 if (remote->receivepack)
1080 ret->smart_options->receivepack = remote->receivepack;
1086 int transport_set_option(struct transport *transport,
1087 const char *name, const char *value)
1089 int git_reports = 1, protocol_reports = 1;
1091 if (transport->smart_options)
1092 git_reports = set_git_option(transport->smart_options,
1095 if (transport->vtable->set_option)
1096 protocol_reports = transport->vtable->set_option(transport,
1099 /* If either report is 0, report 0 (success). */
1100 if (!git_reports || !protocol_reports)
1102 /* If either reports -1 (invalid value), report -1. */
1103 if ((git_reports == -1) || (protocol_reports == -1))
1105 /* Otherwise if both report unknown, report unknown. */
1109 void transport_set_verbosity(struct transport *transport, int verbosity,
1113 transport->verbose = verbosity <= 3 ? verbosity : 3;
1115 transport->verbose = -1;
1118 * Rules used to determine whether to report progress (processing aborts
1119 * when a rule is satisfied):
1121 * . Report progress, if force_progress is 1 (ie. --progress).
1122 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1123 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1124 * . Report progress if isatty(2) is 1.
1126 if (force_progress >= 0)
1127 transport->progress = !!force_progress;
1129 transport->progress = verbosity >= 0 && isatty(2);
1132 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
1136 fprintf(stderr, _("The following submodule paths contain changes that can\n"
1137 "not be found on any remote:\n"));
1138 for (i = 0; i < needs_pushing->nr; i++)
1139 fprintf(stderr, " %s\n", needs_pushing->items[i].string);
1140 fprintf(stderr, _("\nPlease try\n\n"
1141 " git push --recurse-submodules=on-demand\n\n"
1142 "or cd to the path and use\n\n"
1144 "to push them to a remote.\n\n"));
1146 string_list_clear(needs_pushing, 0);
1148 die(_("Aborting."));
1151 static int run_pre_push_hook(struct transport *transport,
1152 struct ref *remote_refs)
1156 struct child_process proc = CHILD_PROCESS_INIT;
1158 const char *argv[4];
1160 if (!(argv[0] = find_hook("pre-push")))
1163 argv[1] = transport->remote->name;
1164 argv[2] = transport->url;
1169 proc.trace2_hook_name = "pre-push";
1171 if (start_command(&proc)) {
1172 finish_command(&proc);
1176 sigchain_push(SIGPIPE, SIG_IGN);
1178 strbuf_init(&buf, 256);
1180 for (r = remote_refs; r; r = r->next) {
1181 if (!r->peer_ref) continue;
1182 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
1183 if (r->status == REF_STATUS_REJECT_STALE) continue;
1184 if (r->status == REF_STATUS_UPTODATE) continue;
1187 strbuf_addf( &buf, "%s %s %s %s\n",
1188 r->peer_ref->name, oid_to_hex(&r->new_oid),
1189 r->name, oid_to_hex(&r->old_oid));
1191 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
1192 /* We do not mind if a hook does not read all refs. */
1199 strbuf_release(&buf);
1205 sigchain_pop(SIGPIPE);
1207 x = finish_command(&proc);
1214 int transport_push(struct repository *r,
1215 struct transport *transport,
1216 struct refspec *rs, int flags,
1217 unsigned int *reject_reasons)
1219 *reject_reasons = 0;
1221 if (transport_color_config() < 0)
1224 if (transport->vtable->push_refs) {
1225 struct ref *remote_refs;
1226 struct ref *local_refs = get_local_heads();
1227 int match_flags = MATCH_REFS_NONE;
1228 int verbose = (transport->verbose > 0);
1229 int quiet = (transport->verbose < 0);
1230 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1231 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1232 int push_ret, ret, err;
1233 struct argv_array ref_prefixes = ARGV_ARRAY_INIT;
1235 if (check_push_refs(local_refs, rs) < 0)
1238 refspec_ref_prefixes(rs, &ref_prefixes);
1240 trace2_region_enter("transport_push", "get_refs_list", r);
1241 remote_refs = transport->vtable->get_refs_list(transport, 1,
1243 trace2_region_leave("transport_push", "get_refs_list", r);
1245 argv_array_clear(&ref_prefixes);
1247 if (flags & TRANSPORT_PUSH_ALL)
1248 match_flags |= MATCH_REFS_ALL;
1249 if (flags & TRANSPORT_PUSH_MIRROR)
1250 match_flags |= MATCH_REFS_MIRROR;
1251 if (flags & TRANSPORT_PUSH_PRUNE)
1252 match_flags |= MATCH_REFS_PRUNE;
1253 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1254 match_flags |= MATCH_REFS_FOLLOW_TAGS;
1256 if (match_push_refs(local_refs, &remote_refs, rs, match_flags))
1259 if (transport->smart_options &&
1260 transport->smart_options->cas &&
1261 !is_empty_cas(transport->smart_options->cas))
1262 apply_push_cas(transport->smart_options->cas,
1263 transport->remote, remote_refs);
1265 set_ref_status_for_push(remote_refs,
1266 flags & TRANSPORT_PUSH_MIRROR,
1267 flags & TRANSPORT_PUSH_FORCE);
1269 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1270 if (run_pre_push_hook(transport, remote_refs))
1273 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1274 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1275 !is_bare_repository()) {
1276 struct ref *ref = remote_refs;
1277 struct oid_array commits = OID_ARRAY_INIT;
1279 trace2_region_enter("transport_push", "push_submodules", r);
1280 for (; ref; ref = ref->next)
1281 if (!is_null_oid(&ref->new_oid))
1282 oid_array_append(&commits,
1285 if (!push_unpushed_submodules(r,
1289 transport->push_options,
1291 oid_array_clear(&commits);
1292 trace2_region_leave("transport_push", "push_submodules", r);
1293 die(_("failed to push all needed submodules"));
1295 oid_array_clear(&commits);
1296 trace2_region_leave("transport_push", "push_submodules", r);
1299 if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1300 ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1301 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1302 !pretend)) && !is_bare_repository()) {
1303 struct ref *ref = remote_refs;
1304 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1305 struct oid_array commits = OID_ARRAY_INIT;
1307 trace2_region_enter("transport_push", "check_submodules", r);
1308 for (; ref; ref = ref->next)
1309 if (!is_null_oid(&ref->new_oid))
1310 oid_array_append(&commits,
1313 if (find_unpushed_submodules(r,
1315 transport->remote->name,
1317 oid_array_clear(&commits);
1318 trace2_region_leave("transport_push", "check_submodules", r);
1319 die_with_unpushed_submodules(&needs_pushing);
1321 string_list_clear(&needs_pushing, 0);
1322 oid_array_clear(&commits);
1323 trace2_region_leave("transport_push", "check_submodules", r);
1326 if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY)) {
1327 trace2_region_enter("transport_push", "push_refs", r);
1328 push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1329 trace2_region_leave("transport_push", "push_refs", r);
1332 err = push_had_errors(remote_refs);
1333 ret = push_ret | err;
1336 transport_print_push_status(transport->url, remote_refs,
1337 verbose | porcelain, porcelain,
1340 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1341 set_upstreams(transport, remote_refs, pretend);
1343 if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1344 TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1346 for (ref = remote_refs; ref; ref = ref->next)
1347 transport_update_tracking_ref(transport->remote, ref, verbose);
1350 if (porcelain && !push_ret)
1352 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1353 fprintf(stderr, "Everything up-to-date\n");
1360 const struct ref *transport_get_remote_refs(struct transport *transport,
1361 const struct argv_array *ref_prefixes)
1363 if (!transport->got_remote_refs) {
1364 transport->remote_refs =
1365 transport->vtable->get_refs_list(transport, 0,
1367 transport->got_remote_refs = 1;
1370 return transport->remote_refs;
1373 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1376 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1377 struct ref **heads = NULL;
1380 for (rm = refs; rm; rm = rm->next) {
1383 !is_null_oid(&rm->old_oid) &&
1384 oideq(&rm->peer_ref->old_oid, &rm->old_oid))
1386 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1387 heads[nr_heads++] = rm;
1392 * When deepening of a shallow repository is requested,
1393 * then local and remote refs are likely to still be equal.
1394 * Just feed them all to the fetch method in that case.
1395 * This condition shouldn't be met in a non-deepening fetch
1396 * (see builtin/fetch.c:quickfetch()).
1398 ALLOC_ARRAY(heads, nr_refs);
1399 for (rm = refs; rm; rm = rm->next)
1400 heads[nr_heads++] = rm;
1403 rc = transport->vtable->fetch(transport, nr_heads, heads);
1409 void transport_unlock_pack(struct transport *transport)
1411 if (transport->pack_lockfile) {
1412 unlink_or_warn(transport->pack_lockfile);
1413 FREE_AND_NULL(transport->pack_lockfile);
1417 int transport_connect(struct transport *transport, const char *name,
1418 const char *exec, int fd[2])
1420 if (transport->vtable->connect)
1421 return transport->vtable->connect(transport, name, exec, fd);
1423 die(_("operation not supported by protocol"));
1426 int transport_disconnect(struct transport *transport)
1429 if (transport->vtable->disconnect)
1430 ret = transport->vtable->disconnect(transport);
1436 * Strip username (and password) from a URL and return
1437 * it in a newly allocated string.
1439 char *transport_anonymize_url(const char *url)
1441 char *scheme_prefix, *anon_part;
1442 size_t anon_len, prefix_len = 0;
1444 anon_part = strchr(url, '@');
1445 if (url_is_local_not_ssh(url) || !anon_part)
1448 anon_len = strlen(++anon_part);
1449 scheme_prefix = strstr(url, "://");
1450 if (!scheme_prefix) {
1451 if (!strchr(anon_part, ':'))
1452 /* cannot be "me@there:/path/name" */
1456 /* make sure scheme is reasonable */
1457 for (cp = url; cp < scheme_prefix; cp++) {
1460 case '+': case '.': case '-':
1469 /* @ past the first slash does not count */
1470 cp = strchr(scheme_prefix + 3, '/');
1471 if (cp && cp < anon_part)
1473 prefix_len = scheme_prefix - url + 3;
1475 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1476 (int)anon_len, anon_part);
1478 return xstrdup(url);