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 strvec *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);
147 transport->hash_algo = data->header.hash_algo;
149 for (i = 0; i < data->header.references.nr; i++) {
150 struct ref_list_entry *e = data->header.references.list + i;
151 struct ref *ref = alloc_ref(e->name);
152 oidcpy(&ref->old_oid, &e->oid);
159 static int fetch_refs_from_bundle(struct transport *transport,
160 int nr_heads, struct ref **to_fetch)
162 struct bundle_transport_data *data = transport->data;
165 if (!data->get_refs_from_bundle_called)
166 get_refs_from_bundle(transport, 0, NULL);
167 ret = unbundle(the_repository, &data->header, data->fd,
168 transport->progress ? BUNDLE_VERBOSE : 0);
169 transport->hash_algo = data->header.hash_algo;
173 static int close_bundle(struct transport *transport)
175 struct bundle_transport_data *data = transport->data;
182 struct git_transport_data {
183 struct git_transport_options options;
184 struct child_process *conn;
186 unsigned got_remote_heads : 1;
187 enum protocol_version version;
188 struct oid_array extra_have;
189 struct oid_array shallow;
192 static int set_git_option(struct git_transport_options *opts,
193 const char *name, const char *value)
195 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
196 opts->uploadpack = value;
198 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
199 opts->receivepack = value;
201 } else if (!strcmp(name, TRANS_OPT_THIN)) {
202 opts->thin = !!value;
204 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
205 opts->followtags = !!value;
207 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
208 opts->keep = !!value;
210 } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
211 opts->update_shallow = !!value;
213 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
218 opts->depth = strtol(value, &end, 0);
220 die(_("transport: invalid depth option '%s'"), value);
223 } else if (!strcmp(name, TRANS_OPT_DEEPEN_SINCE)) {
224 opts->deepen_since = value;
226 } else if (!strcmp(name, TRANS_OPT_DEEPEN_NOT)) {
227 opts->deepen_not = (const struct string_list *)value;
229 } else if (!strcmp(name, TRANS_OPT_DEEPEN_RELATIVE)) {
230 opts->deepen_relative = !!value;
232 } else if (!strcmp(name, TRANS_OPT_FROM_PROMISOR)) {
233 opts->from_promisor = !!value;
235 } else if (!strcmp(name, TRANS_OPT_LIST_OBJECTS_FILTER)) {
236 list_objects_filter_die_if_populated(&opts->filter_options);
237 parse_list_objects_filter(&opts->filter_options, value);
243 static int connect_setup(struct transport *transport, int for_push)
245 struct git_transport_data *data = transport->data;
246 int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
251 switch (transport->family) {
252 case TRANSPORT_FAMILY_ALL: break;
253 case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
254 case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
257 data->conn = git_connect(data->fd, transport->url,
258 for_push ? data->options.receivepack :
259 data->options.uploadpack,
265 static void die_if_server_options(struct transport *transport)
267 if (!transport->server_options || !transport->server_options->nr)
269 advise(_("see protocol.version in 'git help config' for more details"));
270 die(_("server options require protocol version 2 or later"));
274 * Obtains the protocol version from the transport and writes it to
275 * transport->data->version, first connecting if not already connected.
277 * If the protocol version is one that allows skipping the listing of remote
278 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
279 * this function returns NULL. Otherwise, this function returns the list of
282 static struct ref *handshake(struct transport *transport, int for_push,
283 const struct strvec *ref_prefixes,
286 struct git_transport_data *data = transport->data;
287 struct ref *refs = NULL;
288 struct packet_reader reader;
290 connect_setup(transport, for_push);
292 packet_reader_init(&reader, data->fd[0], NULL, 0,
293 PACKET_READ_CHOMP_NEWLINE |
294 PACKET_READ_GENTLE_ON_EOF |
295 PACKET_READ_DIE_ON_ERR_PACKET);
297 data->version = discover_version(&reader);
298 switch (data->version) {
301 get_remote_refs(data->fd[1], &reader, &refs, for_push,
303 transport->server_options,
304 transport->stateless_rpc);
308 die_if_server_options(transport);
309 get_remote_heads(&reader, &refs,
310 for_push ? REF_NORMAL : 0,
314 case protocol_unknown_version:
315 BUG("unknown protocol version");
317 data->got_remote_heads = 1;
318 transport->hash_algo = reader.hash_algo;
320 if (reader.line_peeked)
321 BUG("buffer must be empty at the end of handshake()");
326 static struct ref *get_refs_via_connect(struct transport *transport, int for_push,
327 const struct strvec *ref_prefixes)
329 return handshake(transport, for_push, ref_prefixes, 1);
332 static int fetch_refs_via_pack(struct transport *transport,
333 int nr_heads, struct ref **to_fetch)
336 struct git_transport_data *data = transport->data;
337 struct ref *refs = NULL;
338 struct fetch_pack_args args;
339 struct ref *refs_tmp = NULL;
341 memset(&args, 0, sizeof(args));
342 args.uploadpack = data->options.uploadpack;
343 args.keep_pack = data->options.keep;
345 args.use_thin_pack = data->options.thin;
346 args.include_tag = data->options.followtags;
347 args.verbose = (transport->verbose > 1);
348 args.quiet = (transport->verbose < 0);
349 args.no_progress = !transport->progress;
350 args.depth = data->options.depth;
351 args.deepen_since = data->options.deepen_since;
352 args.deepen_not = data->options.deepen_not;
353 args.deepen_relative = data->options.deepen_relative;
354 args.check_self_contained_and_connected =
355 data->options.check_self_contained_and_connected;
356 args.cloning = transport->cloning;
357 args.update_shallow = data->options.update_shallow;
358 args.from_promisor = data->options.from_promisor;
359 args.filter_options = data->options.filter_options;
360 args.stateless_rpc = transport->stateless_rpc;
361 args.server_options = transport->server_options;
362 args.negotiation_tips = data->options.negotiation_tips;
364 if (!data->got_remote_heads) {
366 int must_list_refs = 0;
367 for (i = 0; i < nr_heads; i++) {
368 if (!to_fetch[i]->exact_oid) {
373 refs_tmp = handshake(transport, 0, NULL, must_list_refs);
376 if (data->version == protocol_unknown_version)
377 BUG("unknown protocol version");
378 else if (data->version <= protocol_v1)
379 die_if_server_options(transport);
381 refs = fetch_pack(&args, data->fd,
382 refs_tmp ? refs_tmp : transport->remote_refs,
383 to_fetch, nr_heads, &data->shallow,
384 &transport->pack_lockfiles, data->version);
388 if (finish_connect(data->conn))
391 data->got_remote_heads = 0;
392 data->options.self_contained_and_connected =
393 args.self_contained_and_connected;
394 data->options.connectivity_checked = args.connectivity_checked;
398 if (report_unmatched_refs(to_fetch, nr_heads))
406 static int push_had_errors(struct ref *ref)
408 for (; ref; ref = ref->next) {
409 switch (ref->status) {
410 case REF_STATUS_NONE:
411 case REF_STATUS_UPTODATE:
421 int transport_refs_pushed(struct ref *ref)
423 for (; ref; ref = ref->next) {
424 switch(ref->status) {
425 case REF_STATUS_NONE:
426 case REF_STATUS_UPTODATE:
435 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
437 struct refspec_item rs;
439 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
445 if (!remote_find_tracking(remote, &rs)) {
447 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
449 delete_ref(NULL, rs.dst, NULL, 0);
451 update_ref("update by push", rs.dst, &ref->new_oid,
457 static void print_ref_status(char flag, const char *summary,
458 struct ref *to, struct ref *from, const char *msg,
459 int porcelain, int summary_width)
463 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
465 fprintf(stdout, "%c\t:%s\t", flag, to->name);
467 fprintf(stdout, "%s (%s)\n", summary, msg);
469 fprintf(stdout, "%s\n", summary);
471 const char *red = "", *reset = "";
472 if (push_had_errors(to)) {
473 red = transport_get_color(TRANSPORT_COLOR_REJECTED);
474 reset = transport_get_color(TRANSPORT_COLOR_RESET);
476 fprintf(stderr, " %s%c %-*s%s ", red, flag, summary_width,
479 fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
481 fputs(prettify_refname(to->name), stderr);
491 static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_width)
494 print_ref_status('-', "[deleted]", ref, NULL, NULL,
495 porcelain, summary_width);
496 else if (is_null_oid(&ref->old_oid))
497 print_ref_status('*',
498 (starts_with(ref->name, "refs/tags/") ? "[new tag]" :
500 ref, ref->peer_ref, NULL, porcelain, summary_width);
502 struct strbuf quickref = STRBUF_INIT;
506 strbuf_add_unique_abbrev(&quickref, &ref->old_oid,
508 if (ref->forced_update) {
509 strbuf_addstr(&quickref, "...");
511 msg = "forced update";
513 strbuf_addstr(&quickref, "..");
517 strbuf_add_unique_abbrev(&quickref, &ref->new_oid,
520 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
521 porcelain, summary_width);
522 strbuf_release(&quickref);
526 static int print_one_push_status(struct ref *ref, const char *dest, int count,
527 int porcelain, int summary_width)
530 char *url = transport_anonymize_url(dest);
531 fprintf(porcelain ? stdout : stderr, "To %s\n", url);
535 switch(ref->status) {
536 case REF_STATUS_NONE:
537 print_ref_status('X', "[no match]", ref, NULL, NULL,
538 porcelain, summary_width);
540 case REF_STATUS_REJECT_NODELETE:
541 print_ref_status('!', "[rejected]", ref, NULL,
542 "remote does not support deleting refs",
543 porcelain, summary_width);
545 case REF_STATUS_UPTODATE:
546 print_ref_status('=', "[up to date]", ref,
547 ref->peer_ref, NULL, porcelain, summary_width);
549 case REF_STATUS_REJECT_NONFASTFORWARD:
550 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
551 "non-fast-forward", porcelain, summary_width);
553 case REF_STATUS_REJECT_ALREADY_EXISTS:
554 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
555 "already exists", porcelain, summary_width);
557 case REF_STATUS_REJECT_FETCH_FIRST:
558 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
559 "fetch first", porcelain, summary_width);
561 case REF_STATUS_REJECT_NEEDS_FORCE:
562 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
563 "needs force", porcelain, summary_width);
565 case REF_STATUS_REJECT_STALE:
566 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
567 "stale info", porcelain, summary_width);
569 case REF_STATUS_REJECT_SHALLOW:
570 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
571 "new shallow roots not allowed",
572 porcelain, summary_width);
574 case REF_STATUS_REMOTE_REJECT:
575 print_ref_status('!', "[remote rejected]", ref,
576 ref->deletion ? NULL : ref->peer_ref,
577 ref->remote_status, porcelain, summary_width);
579 case REF_STATUS_EXPECTING_REPORT:
580 print_ref_status('!', "[remote failure]", ref,
581 ref->deletion ? NULL : ref->peer_ref,
582 "remote failed to report status",
583 porcelain, summary_width);
585 case REF_STATUS_ATOMIC_PUSH_FAILED:
586 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
587 "atomic push failed", porcelain, summary_width);
590 print_ok_ref_status(ref, porcelain, summary_width);
597 static int measure_abbrev(const struct object_id *oid, int sofar)
599 char hex[GIT_MAX_HEXSZ + 1];
600 int w = find_unique_abbrev_r(hex, oid, DEFAULT_ABBREV);
602 return (w < sofar) ? sofar : w;
605 int transport_summary_width(const struct ref *refs)
609 for (; refs; refs = refs->next) {
610 maxw = measure_abbrev(&refs->old_oid, maxw);
611 maxw = measure_abbrev(&refs->new_oid, maxw);
614 maxw = FALLBACK_DEFAULT_ABBREV;
615 return (2 * maxw + 3);
618 void transport_print_push_status(const char *dest, struct ref *refs,
619 int verbose, int porcelain, unsigned int *reject_reasons)
624 int summary_width = transport_summary_width(refs);
626 if (transport_color_config() < 0)
627 warning(_("could not parse transport.color.* config"));
629 head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
632 for (ref = refs; ref; ref = ref->next)
633 if (ref->status == REF_STATUS_UPTODATE)
634 n += print_one_push_status(ref, dest, n,
635 porcelain, summary_width);
638 for (ref = refs; ref; ref = ref->next)
639 if (ref->status == REF_STATUS_OK)
640 n += print_one_push_status(ref, dest, n,
641 porcelain, summary_width);
644 for (ref = refs; ref; ref = ref->next) {
645 if (ref->status != REF_STATUS_NONE &&
646 ref->status != REF_STATUS_UPTODATE &&
647 ref->status != REF_STATUS_OK)
648 n += print_one_push_status(ref, dest, n,
649 porcelain, summary_width);
650 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
651 if (head != NULL && !strcmp(head, ref->name))
652 *reject_reasons |= REJECT_NON_FF_HEAD;
654 *reject_reasons |= REJECT_NON_FF_OTHER;
655 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
656 *reject_reasons |= REJECT_ALREADY_EXISTS;
657 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
658 *reject_reasons |= REJECT_FETCH_FIRST;
659 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
660 *reject_reasons |= REJECT_NEEDS_FORCE;
666 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
668 struct git_transport_data *data = transport->data;
669 struct send_pack_args args;
672 if (transport_color_config() < 0)
675 if (!data->got_remote_heads)
676 get_refs_via_connect(transport, 1, NULL);
678 memset(&args, 0, sizeof(args));
679 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
680 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
681 args.use_thin_pack = data->options.thin;
682 args.verbose = (transport->verbose > 0);
683 args.quiet = (transport->verbose < 0);
684 args.progress = transport->progress;
685 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
686 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
687 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
688 args.push_options = transport->push_options;
689 args.url = transport->url;
691 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
692 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
693 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
694 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
696 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
698 switch (data->version) {
700 die(_("support for protocol v2 not implemented yet"));
704 ret = send_pack(&args, data->fd, data->conn, remote_refs,
707 case protocol_unknown_version:
708 BUG("unknown protocol version");
714 * Atomic push may abort the connection early and close the pipe,
715 * which may cause an error for `finish_connect()`. Ignore this error
716 * for atomic git-push.
718 if (ret || args.atomic)
719 finish_connect(data->conn);
721 ret = finish_connect(data->conn);
723 data->got_remote_heads = 0;
728 static int connect_git(struct transport *transport, const char *name,
729 const char *executable, int fd[2])
731 struct git_transport_data *data = transport->data;
732 data->conn = git_connect(data->fd, transport->url,
739 static int disconnect_git(struct transport *transport)
741 struct git_transport_data *data = transport->data;
743 if (data->got_remote_heads && !transport->stateless_rpc)
744 packet_flush(data->fd[1]);
747 finish_connect(data->conn);
754 static struct transport_vtable taken_over_vtable = {
756 get_refs_via_connect,
763 void transport_take_over(struct transport *transport,
764 struct child_process *child)
766 struct git_transport_data *data;
768 if (!transport->smart_options)
769 BUG("taking over transport requires non-NULL "
770 "smart_options field.");
772 data = xcalloc(1, sizeof(*data));
773 data->options = *transport->smart_options;
775 data->fd[0] = data->conn->out;
776 data->fd[1] = data->conn->in;
777 data->got_remote_heads = 0;
778 transport->data = data;
780 transport->vtable = &taken_over_vtable;
781 transport->smart_options = &(data->options);
783 transport->cannot_reuse = 1;
786 static int is_file(const char *url)
791 return S_ISREG(buf.st_mode);
794 static int external_specification_len(const char *url)
796 return strchr(url, ':') - url;
799 static const struct string_list *protocol_whitelist(void)
801 static int enabled = -1;
802 static struct string_list allowed = STRING_LIST_INIT_DUP;
805 const char *v = getenv("GIT_ALLOW_PROTOCOL");
807 string_list_split(&allowed, v, ':', -1);
808 string_list_sort(&allowed);
815 return enabled ? &allowed : NULL;
818 enum protocol_allow_config {
819 PROTOCOL_ALLOW_NEVER = 0,
820 PROTOCOL_ALLOW_USER_ONLY,
821 PROTOCOL_ALLOW_ALWAYS
824 static enum protocol_allow_config parse_protocol_config(const char *key,
827 if (!strcasecmp(value, "always"))
828 return PROTOCOL_ALLOW_ALWAYS;
829 else if (!strcasecmp(value, "never"))
830 return PROTOCOL_ALLOW_NEVER;
831 else if (!strcasecmp(value, "user"))
832 return PROTOCOL_ALLOW_USER_ONLY;
834 die(_("unknown value for config '%s': %s"), key, value);
837 static enum protocol_allow_config get_protocol_config(const char *type)
839 char *key = xstrfmt("protocol.%s.allow", type);
842 /* first check the per-protocol config */
843 if (!git_config_get_string(key, &value)) {
844 enum protocol_allow_config ret =
845 parse_protocol_config(key, value);
852 /* if defined, fallback to user-defined default for unknown protocols */
853 if (!git_config_get_string("protocol.allow", &value)) {
854 enum protocol_allow_config ret =
855 parse_protocol_config("protocol.allow", value);
860 /* fallback to built-in defaults */
862 if (!strcmp(type, "http") ||
863 !strcmp(type, "https") ||
864 !strcmp(type, "git") ||
865 !strcmp(type, "ssh") ||
866 !strcmp(type, "file"))
867 return PROTOCOL_ALLOW_ALWAYS;
869 /* known scary; err on the side of caution */
870 if (!strcmp(type, "ext"))
871 return PROTOCOL_ALLOW_NEVER;
873 /* unknown; by default let them be used only directly by the user */
874 return PROTOCOL_ALLOW_USER_ONLY;
877 int is_transport_allowed(const char *type, int from_user)
879 const struct string_list *whitelist = protocol_whitelist();
881 return string_list_has_string(whitelist, type);
883 switch (get_protocol_config(type)) {
884 case PROTOCOL_ALLOW_ALWAYS:
886 case PROTOCOL_ALLOW_NEVER:
888 case PROTOCOL_ALLOW_USER_ONLY:
890 from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
894 BUG("invalid protocol_allow_config type");
897 void transport_check_allowed(const char *type)
899 if (!is_transport_allowed(type, -1))
900 die(_("transport '%s' not allowed"), type);
903 static struct transport_vtable bundle_vtable = {
905 get_refs_from_bundle,
906 fetch_refs_from_bundle,
912 static struct transport_vtable builtin_smart_vtable = {
914 get_refs_via_connect,
921 struct transport *transport_get(struct remote *remote, const char *url)
924 struct transport *ret = xcalloc(1, sizeof(*ret));
926 ret->progress = isatty(2);
927 string_list_init(&ret->pack_lockfiles, 1);
930 BUG("No remote provided to transport_get()");
932 ret->got_remote_refs = 0;
933 ret->remote = remote;
934 helper = remote->foreign_vcs;
936 if (!url && remote->url)
937 url = remote->url[0];
940 /* maybe it is a foreign URL? */
944 while (is_urlschemechar(p == url, *p))
946 if (starts_with(p, "::"))
947 helper = xstrndup(url, p - url);
951 transport_helper_init(ret, helper);
952 } else if (starts_with(url, "rsync:")) {
953 die(_("git-over-rsync is no longer supported"));
954 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
955 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
956 transport_check_allowed("file");
958 ret->vtable = &bundle_vtable;
959 ret->smart_options = NULL;
960 } else if (!is_url(url)
961 || starts_with(url, "file://")
962 || starts_with(url, "git://")
963 || starts_with(url, "ssh://")
964 || starts_with(url, "git+ssh://") /* deprecated - do not use */
965 || starts_with(url, "ssh+git://") /* deprecated - do not use */
968 * These are builtin smart transports; "allowed" transports
969 * will be checked individually in git_connect.
971 struct git_transport_data *data = xcalloc(1, sizeof(*data));
973 ret->vtable = &builtin_smart_vtable;
974 ret->smart_options = &(data->options);
977 data->got_remote_heads = 0;
979 /* Unknown protocol in URL. Pass to external handler. */
980 int len = external_specification_len(url);
981 char *handler = xmemdupz(url, len);
982 transport_helper_init(ret, handler);
985 if (ret->smart_options) {
986 ret->smart_options->thin = 1;
987 ret->smart_options->uploadpack = "git-upload-pack";
988 if (remote->uploadpack)
989 ret->smart_options->uploadpack = remote->uploadpack;
990 ret->smart_options->receivepack = "git-receive-pack";
991 if (remote->receivepack)
992 ret->smart_options->receivepack = remote->receivepack;
995 ret->hash_algo = &hash_algos[GIT_HASH_SHA1];
1000 const struct git_hash_algo *transport_get_hash_algo(struct transport *transport)
1002 return transport->hash_algo;
1005 int transport_set_option(struct transport *transport,
1006 const char *name, const char *value)
1008 int git_reports = 1, protocol_reports = 1;
1010 if (transport->smart_options)
1011 git_reports = set_git_option(transport->smart_options,
1014 if (transport->vtable->set_option)
1015 protocol_reports = transport->vtable->set_option(transport,
1018 /* If either report is 0, report 0 (success). */
1019 if (!git_reports || !protocol_reports)
1021 /* If either reports -1 (invalid value), report -1. */
1022 if ((git_reports == -1) || (protocol_reports == -1))
1024 /* Otherwise if both report unknown, report unknown. */
1028 void transport_set_verbosity(struct transport *transport, int verbosity,
1032 transport->verbose = verbosity <= 3 ? verbosity : 3;
1034 transport->verbose = -1;
1037 * Rules used to determine whether to report progress (processing aborts
1038 * when a rule is satisfied):
1040 * . Report progress, if force_progress is 1 (ie. --progress).
1041 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1042 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1043 * . Report progress if isatty(2) is 1.
1045 if (force_progress >= 0)
1046 transport->progress = !!force_progress;
1048 transport->progress = verbosity >= 0 && isatty(2);
1051 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
1055 fprintf(stderr, _("The following submodule paths contain changes that can\n"
1056 "not be found on any remote:\n"));
1057 for (i = 0; i < needs_pushing->nr; i++)
1058 fprintf(stderr, " %s\n", needs_pushing->items[i].string);
1059 fprintf(stderr, _("\nPlease try\n\n"
1060 " git push --recurse-submodules=on-demand\n\n"
1061 "or cd to the path and use\n\n"
1063 "to push them to a remote.\n\n"));
1065 string_list_clear(needs_pushing, 0);
1067 die(_("Aborting."));
1070 static int run_pre_push_hook(struct transport *transport,
1071 struct ref *remote_refs)
1075 struct child_process proc = CHILD_PROCESS_INIT;
1077 const char *argv[4];
1079 if (!(argv[0] = find_hook("pre-push")))
1082 argv[1] = transport->remote->name;
1083 argv[2] = transport->url;
1088 proc.trace2_hook_name = "pre-push";
1090 if (start_command(&proc)) {
1091 finish_command(&proc);
1095 sigchain_push(SIGPIPE, SIG_IGN);
1097 strbuf_init(&buf, 256);
1099 for (r = remote_refs; r; r = r->next) {
1100 if (!r->peer_ref) continue;
1101 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
1102 if (r->status == REF_STATUS_REJECT_STALE) continue;
1103 if (r->status == REF_STATUS_UPTODATE) continue;
1106 strbuf_addf( &buf, "%s %s %s %s\n",
1107 r->peer_ref->name, oid_to_hex(&r->new_oid),
1108 r->name, oid_to_hex(&r->old_oid));
1110 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
1111 /* We do not mind if a hook does not read all refs. */
1118 strbuf_release(&buf);
1124 sigchain_pop(SIGPIPE);
1126 x = finish_command(&proc);
1133 int transport_push(struct repository *r,
1134 struct transport *transport,
1135 struct refspec *rs, int flags,
1136 unsigned int *reject_reasons)
1138 *reject_reasons = 0;
1140 if (transport_color_config() < 0)
1143 if (transport->vtable->push_refs) {
1144 struct ref *remote_refs;
1145 struct ref *local_refs = get_local_heads();
1146 int match_flags = MATCH_REFS_NONE;
1147 int verbose = (transport->verbose > 0);
1148 int quiet = (transport->verbose < 0);
1149 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1150 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1151 int push_ret, ret, err;
1152 struct strvec ref_prefixes = STRVEC_INIT;
1154 if (check_push_refs(local_refs, rs) < 0)
1157 refspec_ref_prefixes(rs, &ref_prefixes);
1159 trace2_region_enter("transport_push", "get_refs_list", r);
1160 remote_refs = transport->vtable->get_refs_list(transport, 1,
1162 trace2_region_leave("transport_push", "get_refs_list", r);
1164 strvec_clear(&ref_prefixes);
1166 if (flags & TRANSPORT_PUSH_ALL)
1167 match_flags |= MATCH_REFS_ALL;
1168 if (flags & TRANSPORT_PUSH_MIRROR)
1169 match_flags |= MATCH_REFS_MIRROR;
1170 if (flags & TRANSPORT_PUSH_PRUNE)
1171 match_flags |= MATCH_REFS_PRUNE;
1172 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1173 match_flags |= MATCH_REFS_FOLLOW_TAGS;
1175 if (match_push_refs(local_refs, &remote_refs, rs, match_flags))
1178 if (transport->smart_options &&
1179 transport->smart_options->cas &&
1180 !is_empty_cas(transport->smart_options->cas))
1181 apply_push_cas(transport->smart_options->cas,
1182 transport->remote, remote_refs);
1184 set_ref_status_for_push(remote_refs,
1185 flags & TRANSPORT_PUSH_MIRROR,
1186 flags & TRANSPORT_PUSH_FORCE);
1188 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1189 if (run_pre_push_hook(transport, remote_refs))
1192 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1193 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1194 !is_bare_repository()) {
1195 struct ref *ref = remote_refs;
1196 struct oid_array commits = OID_ARRAY_INIT;
1198 trace2_region_enter("transport_push", "push_submodules", r);
1199 for (; ref; ref = ref->next)
1200 if (!is_null_oid(&ref->new_oid))
1201 oid_array_append(&commits,
1204 if (!push_unpushed_submodules(r,
1208 transport->push_options,
1210 oid_array_clear(&commits);
1211 trace2_region_leave("transport_push", "push_submodules", r);
1212 die(_("failed to push all needed submodules"));
1214 oid_array_clear(&commits);
1215 trace2_region_leave("transport_push", "push_submodules", r);
1218 if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1219 ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1220 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1221 !pretend)) && !is_bare_repository()) {
1222 struct ref *ref = remote_refs;
1223 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1224 struct oid_array commits = OID_ARRAY_INIT;
1226 trace2_region_enter("transport_push", "check_submodules", r);
1227 for (; ref; ref = ref->next)
1228 if (!is_null_oid(&ref->new_oid))
1229 oid_array_append(&commits,
1232 if (find_unpushed_submodules(r,
1234 transport->remote->name,
1236 oid_array_clear(&commits);
1237 trace2_region_leave("transport_push", "check_submodules", r);
1238 die_with_unpushed_submodules(&needs_pushing);
1240 string_list_clear(&needs_pushing, 0);
1241 oid_array_clear(&commits);
1242 trace2_region_leave("transport_push", "check_submodules", r);
1245 if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY)) {
1246 trace2_region_enter("transport_push", "push_refs", r);
1247 push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1248 trace2_region_leave("transport_push", "push_refs", r);
1251 err = push_had_errors(remote_refs);
1252 ret = push_ret | err;
1255 transport_print_push_status(transport->url, remote_refs,
1256 verbose | porcelain, porcelain,
1259 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1260 set_upstreams(transport, remote_refs, pretend);
1262 if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1263 TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1265 for (ref = remote_refs; ref; ref = ref->next)
1266 transport_update_tracking_ref(transport->remote, ref, verbose);
1269 if (porcelain && !push_ret)
1271 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1272 fprintf(stderr, "Everything up-to-date\n");
1279 const struct ref *transport_get_remote_refs(struct transport *transport,
1280 const struct strvec *ref_prefixes)
1282 if (!transport->got_remote_refs) {
1283 transport->remote_refs =
1284 transport->vtable->get_refs_list(transport, 0,
1286 transport->got_remote_refs = 1;
1289 return transport->remote_refs;
1292 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1295 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1296 struct ref **heads = NULL;
1299 for (rm = refs; rm; rm = rm->next) {
1302 !is_null_oid(&rm->old_oid) &&
1303 oideq(&rm->peer_ref->old_oid, &rm->old_oid))
1305 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1306 heads[nr_heads++] = rm;
1311 * When deepening of a shallow repository is requested,
1312 * then local and remote refs are likely to still be equal.
1313 * Just feed them all to the fetch method in that case.
1314 * This condition shouldn't be met in a non-deepening fetch
1315 * (see builtin/fetch.c:quickfetch()).
1317 ALLOC_ARRAY(heads, nr_refs);
1318 for (rm = refs; rm; rm = rm->next)
1319 heads[nr_heads++] = rm;
1322 rc = transport->vtable->fetch(transport, nr_heads, heads);
1328 void transport_unlock_pack(struct transport *transport)
1332 for (i = 0; i < transport->pack_lockfiles.nr; i++)
1333 unlink_or_warn(transport->pack_lockfiles.items[i].string);
1334 string_list_clear(&transport->pack_lockfiles, 0);
1337 int transport_connect(struct transport *transport, const char *name,
1338 const char *exec, int fd[2])
1340 if (transport->vtable->connect)
1341 return transport->vtable->connect(transport, name, exec, fd);
1343 die(_("operation not supported by protocol"));
1346 int transport_disconnect(struct transport *transport)
1349 if (transport->vtable->disconnect)
1350 ret = transport->vtable->disconnect(transport);
1356 * Strip username (and password) from a URL and return
1357 * it in a newly allocated string.
1359 char *transport_anonymize_url(const char *url)
1361 char *scheme_prefix, *anon_part;
1362 size_t anon_len, prefix_len = 0;
1364 anon_part = strchr(url, '@');
1365 if (url_is_local_not_ssh(url) || !anon_part)
1368 anon_len = strlen(++anon_part);
1369 scheme_prefix = strstr(url, "://");
1370 if (!scheme_prefix) {
1371 if (!strchr(anon_part, ':'))
1372 /* cannot be "me@there:/path/name" */
1376 /* make sure scheme is reasonable */
1377 for (cp = url; cp < scheme_prefix; cp++) {
1380 case '+': case '.': case '-':
1389 /* @ past the first slash does not count */
1390 cp = strchr(scheme_prefix + 3, '/');
1391 if (cp && cp < anon_part)
1393 prefix_len = scheme_prefix - url + 3;
1395 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1396 (int)anon_len, anon_part);
1398 return xstrdup(url);