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_NO_DEPENDENTS)) {
236 opts->no_dependents = !!value;
238 } else if (!strcmp(name, TRANS_OPT_LIST_OBJECTS_FILTER)) {
239 list_objects_filter_die_if_populated(&opts->filter_options);
240 parse_list_objects_filter(&opts->filter_options, value);
246 static int connect_setup(struct transport *transport, int for_push)
248 struct git_transport_data *data = transport->data;
249 int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
254 switch (transport->family) {
255 case TRANSPORT_FAMILY_ALL: break;
256 case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
257 case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
260 data->conn = git_connect(data->fd, transport->url,
261 for_push ? data->options.receivepack :
262 data->options.uploadpack,
268 static void die_if_server_options(struct transport *transport)
270 if (!transport->server_options || !transport->server_options->nr)
272 advise(_("see protocol.version in 'git help config' for more details"));
273 die(_("server options require protocol version 2 or later"));
277 * Obtains the protocol version from the transport and writes it to
278 * transport->data->version, first connecting if not already connected.
280 * If the protocol version is one that allows skipping the listing of remote
281 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
282 * this function returns NULL. Otherwise, this function returns the list of
285 static struct ref *handshake(struct transport *transport, int for_push,
286 const struct strvec *ref_prefixes,
289 struct git_transport_data *data = transport->data;
290 struct ref *refs = NULL;
291 struct packet_reader reader;
293 connect_setup(transport, for_push);
295 packet_reader_init(&reader, data->fd[0], NULL, 0,
296 PACKET_READ_CHOMP_NEWLINE |
297 PACKET_READ_GENTLE_ON_EOF |
298 PACKET_READ_DIE_ON_ERR_PACKET);
300 data->version = discover_version(&reader);
301 switch (data->version) {
304 get_remote_refs(data->fd[1], &reader, &refs, for_push,
306 transport->server_options,
307 transport->stateless_rpc);
311 die_if_server_options(transport);
312 get_remote_heads(&reader, &refs,
313 for_push ? REF_NORMAL : 0,
317 case protocol_unknown_version:
318 BUG("unknown protocol version");
320 data->got_remote_heads = 1;
321 transport->hash_algo = reader.hash_algo;
323 if (reader.line_peeked)
324 BUG("buffer must be empty at the end of handshake()");
329 static struct ref *get_refs_via_connect(struct transport *transport, int for_push,
330 const struct strvec *ref_prefixes)
332 return handshake(transport, for_push, ref_prefixes, 1);
335 static int fetch_refs_via_pack(struct transport *transport,
336 int nr_heads, struct ref **to_fetch)
339 struct git_transport_data *data = transport->data;
340 struct ref *refs = NULL;
341 struct fetch_pack_args args;
342 struct ref *refs_tmp = NULL;
344 memset(&args, 0, sizeof(args));
345 args.uploadpack = data->options.uploadpack;
346 args.keep_pack = data->options.keep;
348 args.use_thin_pack = data->options.thin;
349 args.include_tag = data->options.followtags;
350 args.verbose = (transport->verbose > 1);
351 args.quiet = (transport->verbose < 0);
352 args.no_progress = !transport->progress;
353 args.depth = data->options.depth;
354 args.deepen_since = data->options.deepen_since;
355 args.deepen_not = data->options.deepen_not;
356 args.deepen_relative = data->options.deepen_relative;
357 args.check_self_contained_and_connected =
358 data->options.check_self_contained_and_connected;
359 args.cloning = transport->cloning;
360 args.update_shallow = data->options.update_shallow;
361 args.from_promisor = data->options.from_promisor;
362 args.no_dependents = data->options.no_dependents;
363 args.filter_options = data->options.filter_options;
364 args.stateless_rpc = transport->stateless_rpc;
365 args.server_options = transport->server_options;
366 args.negotiation_tips = data->options.negotiation_tips;
368 if (!data->got_remote_heads) {
370 int must_list_refs = 0;
371 for (i = 0; i < nr_heads; i++) {
372 if (!to_fetch[i]->exact_oid) {
377 refs_tmp = handshake(transport, 0, NULL, must_list_refs);
380 if (data->version == protocol_unknown_version)
381 BUG("unknown protocol version");
382 else if (data->version <= protocol_v1)
383 die_if_server_options(transport);
385 refs = fetch_pack(&args, data->fd,
386 refs_tmp ? refs_tmp : transport->remote_refs,
387 to_fetch, nr_heads, &data->shallow,
388 &transport->pack_lockfiles, data->version);
392 if (finish_connect(data->conn))
395 data->got_remote_heads = 0;
396 data->options.self_contained_and_connected =
397 args.self_contained_and_connected;
398 data->options.connectivity_checked = args.connectivity_checked;
402 if (report_unmatched_refs(to_fetch, nr_heads))
410 static int push_had_errors(struct ref *ref)
412 for (; ref; ref = ref->next) {
413 switch (ref->status) {
414 case REF_STATUS_NONE:
415 case REF_STATUS_UPTODATE:
425 int transport_refs_pushed(struct ref *ref)
427 for (; ref; ref = ref->next) {
428 switch(ref->status) {
429 case REF_STATUS_NONE:
430 case REF_STATUS_UPTODATE:
439 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
441 struct refspec_item rs;
443 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
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, &ref->new_oid,
461 static void print_ref_status(char flag, const char *summary,
462 struct ref *to, struct ref *from, const char *msg,
463 int porcelain, int summary_width)
467 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
469 fprintf(stdout, "%c\t:%s\t", flag, to->name);
471 fprintf(stdout, "%s (%s)\n", summary, msg);
473 fprintf(stdout, "%s\n", summary);
475 const char *red = "", *reset = "";
476 if (push_had_errors(to)) {
477 red = transport_get_color(TRANSPORT_COLOR_REJECTED);
478 reset = transport_get_color(TRANSPORT_COLOR_RESET);
480 fprintf(stderr, " %s%c %-*s%s ", red, flag, summary_width,
483 fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
485 fputs(prettify_refname(to->name), stderr);
495 static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_width)
498 print_ref_status('-', "[deleted]", ref, NULL, NULL,
499 porcelain, summary_width);
500 else if (is_null_oid(&ref->old_oid))
501 print_ref_status('*',
502 (starts_with(ref->name, "refs/tags/") ? "[new tag]" :
504 ref, ref->peer_ref, NULL, porcelain, summary_width);
506 struct strbuf quickref = STRBUF_INIT;
510 strbuf_add_unique_abbrev(&quickref, &ref->old_oid,
512 if (ref->forced_update) {
513 strbuf_addstr(&quickref, "...");
515 msg = "forced update";
517 strbuf_addstr(&quickref, "..");
521 strbuf_add_unique_abbrev(&quickref, &ref->new_oid,
524 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
525 porcelain, summary_width);
526 strbuf_release(&quickref);
530 static int print_one_push_status(struct ref *ref, const char *dest, int count,
531 int porcelain, int summary_width)
534 char *url = transport_anonymize_url(dest);
535 fprintf(porcelain ? stdout : stderr, "To %s\n", url);
539 switch(ref->status) {
540 case REF_STATUS_NONE:
541 print_ref_status('X', "[no match]", ref, NULL, NULL,
542 porcelain, summary_width);
544 case REF_STATUS_REJECT_NODELETE:
545 print_ref_status('!', "[rejected]", ref, NULL,
546 "remote does not support deleting refs",
547 porcelain, summary_width);
549 case REF_STATUS_UPTODATE:
550 print_ref_status('=', "[up to date]", ref,
551 ref->peer_ref, NULL, porcelain, summary_width);
553 case REF_STATUS_REJECT_NONFASTFORWARD:
554 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
555 "non-fast-forward", porcelain, summary_width);
557 case REF_STATUS_REJECT_ALREADY_EXISTS:
558 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
559 "already exists", porcelain, summary_width);
561 case REF_STATUS_REJECT_FETCH_FIRST:
562 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
563 "fetch first", porcelain, summary_width);
565 case REF_STATUS_REJECT_NEEDS_FORCE:
566 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
567 "needs force", porcelain, summary_width);
569 case REF_STATUS_REJECT_STALE:
570 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
571 "stale info", porcelain, summary_width);
573 case REF_STATUS_REJECT_SHALLOW:
574 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
575 "new shallow roots not allowed",
576 porcelain, summary_width);
578 case REF_STATUS_REMOTE_REJECT:
579 print_ref_status('!', "[remote rejected]", ref,
580 ref->deletion ? NULL : ref->peer_ref,
581 ref->remote_status, porcelain, summary_width);
583 case REF_STATUS_EXPECTING_REPORT:
584 print_ref_status('!', "[remote failure]", ref,
585 ref->deletion ? NULL : ref->peer_ref,
586 "remote failed to report status",
587 porcelain, summary_width);
589 case REF_STATUS_ATOMIC_PUSH_FAILED:
590 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
591 "atomic push failed", porcelain, summary_width);
594 print_ok_ref_status(ref, porcelain, summary_width);
601 static int measure_abbrev(const struct object_id *oid, int sofar)
603 char hex[GIT_MAX_HEXSZ + 1];
604 int w = find_unique_abbrev_r(hex, oid, DEFAULT_ABBREV);
606 return (w < sofar) ? sofar : w;
609 int transport_summary_width(const struct ref *refs)
613 for (; refs; refs = refs->next) {
614 maxw = measure_abbrev(&refs->old_oid, maxw);
615 maxw = measure_abbrev(&refs->new_oid, maxw);
618 maxw = FALLBACK_DEFAULT_ABBREV;
619 return (2 * maxw + 3);
622 void transport_print_push_status(const char *dest, struct ref *refs,
623 int verbose, int porcelain, unsigned int *reject_reasons)
628 int summary_width = transport_summary_width(refs);
630 if (transport_color_config() < 0)
631 warning(_("could not parse transport.color.* config"));
633 head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
636 for (ref = refs; ref; ref = ref->next)
637 if (ref->status == REF_STATUS_UPTODATE)
638 n += print_one_push_status(ref, dest, n,
639 porcelain, summary_width);
642 for (ref = refs; ref; ref = ref->next)
643 if (ref->status == REF_STATUS_OK)
644 n += print_one_push_status(ref, dest, n,
645 porcelain, summary_width);
648 for (ref = refs; ref; ref = ref->next) {
649 if (ref->status != REF_STATUS_NONE &&
650 ref->status != REF_STATUS_UPTODATE &&
651 ref->status != REF_STATUS_OK)
652 n += print_one_push_status(ref, dest, n,
653 porcelain, summary_width);
654 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
655 if (head != NULL && !strcmp(head, ref->name))
656 *reject_reasons |= REJECT_NON_FF_HEAD;
658 *reject_reasons |= REJECT_NON_FF_OTHER;
659 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
660 *reject_reasons |= REJECT_ALREADY_EXISTS;
661 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
662 *reject_reasons |= REJECT_FETCH_FIRST;
663 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
664 *reject_reasons |= REJECT_NEEDS_FORCE;
670 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
672 struct git_transport_data *data = transport->data;
673 struct send_pack_args args;
676 if (transport_color_config() < 0)
679 if (!data->got_remote_heads)
680 get_refs_via_connect(transport, 1, NULL);
682 memset(&args, 0, sizeof(args));
683 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
684 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
685 args.use_thin_pack = data->options.thin;
686 args.verbose = (transport->verbose > 0);
687 args.quiet = (transport->verbose < 0);
688 args.progress = transport->progress;
689 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
690 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
691 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
692 args.push_options = transport->push_options;
693 args.url = transport->url;
695 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
696 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
697 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
698 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
700 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
702 switch (data->version) {
704 die(_("support for protocol v2 not implemented yet"));
708 ret = send_pack(&args, data->fd, data->conn, remote_refs,
711 case protocol_unknown_version:
712 BUG("unknown protocol version");
718 * Atomic push may abort the connection early and close the pipe,
719 * which may cause an error for `finish_connect()`. Ignore this error
720 * for atomic git-push.
722 if (ret || args.atomic)
723 finish_connect(data->conn);
725 ret = finish_connect(data->conn);
727 data->got_remote_heads = 0;
732 static int connect_git(struct transport *transport, const char *name,
733 const char *executable, int fd[2])
735 struct git_transport_data *data = transport->data;
736 data->conn = git_connect(data->fd, transport->url,
743 static int disconnect_git(struct transport *transport)
745 struct git_transport_data *data = transport->data;
747 if (data->got_remote_heads && !transport->stateless_rpc)
748 packet_flush(data->fd[1]);
751 finish_connect(data->conn);
758 static struct transport_vtable taken_over_vtable = {
760 get_refs_via_connect,
767 void transport_take_over(struct transport *transport,
768 struct child_process *child)
770 struct git_transport_data *data;
772 if (!transport->smart_options)
773 BUG("taking over transport requires non-NULL "
774 "smart_options field.");
776 data = xcalloc(1, sizeof(*data));
777 data->options = *transport->smart_options;
779 data->fd[0] = data->conn->out;
780 data->fd[1] = data->conn->in;
781 data->got_remote_heads = 0;
782 transport->data = data;
784 transport->vtable = &taken_over_vtable;
785 transport->smart_options = &(data->options);
787 transport->cannot_reuse = 1;
790 static int is_file(const char *url)
795 return S_ISREG(buf.st_mode);
798 static int external_specification_len(const char *url)
800 return strchr(url, ':') - url;
803 static const struct string_list *protocol_whitelist(void)
805 static int enabled = -1;
806 static struct string_list allowed = STRING_LIST_INIT_DUP;
809 const char *v = getenv("GIT_ALLOW_PROTOCOL");
811 string_list_split(&allowed, v, ':', -1);
812 string_list_sort(&allowed);
819 return enabled ? &allowed : NULL;
822 enum protocol_allow_config {
823 PROTOCOL_ALLOW_NEVER = 0,
824 PROTOCOL_ALLOW_USER_ONLY,
825 PROTOCOL_ALLOW_ALWAYS
828 static enum protocol_allow_config parse_protocol_config(const char *key,
831 if (!strcasecmp(value, "always"))
832 return PROTOCOL_ALLOW_ALWAYS;
833 else if (!strcasecmp(value, "never"))
834 return PROTOCOL_ALLOW_NEVER;
835 else if (!strcasecmp(value, "user"))
836 return PROTOCOL_ALLOW_USER_ONLY;
838 die(_("unknown value for config '%s': %s"), key, value);
841 static enum protocol_allow_config get_protocol_config(const char *type)
843 char *key = xstrfmt("protocol.%s.allow", type);
846 /* first check the per-protocol config */
847 if (!git_config_get_string(key, &value)) {
848 enum protocol_allow_config ret =
849 parse_protocol_config(key, value);
856 /* if defined, fallback to user-defined default for unknown protocols */
857 if (!git_config_get_string("protocol.allow", &value)) {
858 enum protocol_allow_config ret =
859 parse_protocol_config("protocol.allow", value);
864 /* fallback to built-in defaults */
866 if (!strcmp(type, "http") ||
867 !strcmp(type, "https") ||
868 !strcmp(type, "git") ||
869 !strcmp(type, "ssh") ||
870 !strcmp(type, "file"))
871 return PROTOCOL_ALLOW_ALWAYS;
873 /* known scary; err on the side of caution */
874 if (!strcmp(type, "ext"))
875 return PROTOCOL_ALLOW_NEVER;
877 /* unknown; by default let them be used only directly by the user */
878 return PROTOCOL_ALLOW_USER_ONLY;
881 int is_transport_allowed(const char *type, int from_user)
883 const struct string_list *whitelist = protocol_whitelist();
885 return string_list_has_string(whitelist, type);
887 switch (get_protocol_config(type)) {
888 case PROTOCOL_ALLOW_ALWAYS:
890 case PROTOCOL_ALLOW_NEVER:
892 case PROTOCOL_ALLOW_USER_ONLY:
894 from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
898 BUG("invalid protocol_allow_config type");
901 void transport_check_allowed(const char *type)
903 if (!is_transport_allowed(type, -1))
904 die(_("transport '%s' not allowed"), type);
907 static struct transport_vtable bundle_vtable = {
909 get_refs_from_bundle,
910 fetch_refs_from_bundle,
916 static struct transport_vtable builtin_smart_vtable = {
918 get_refs_via_connect,
925 struct transport *transport_get(struct remote *remote, const char *url)
928 struct transport *ret = xcalloc(1, sizeof(*ret));
930 ret->progress = isatty(2);
931 string_list_init(&ret->pack_lockfiles, 1);
934 BUG("No remote provided to transport_get()");
936 ret->got_remote_refs = 0;
937 ret->remote = remote;
938 helper = remote->foreign_vcs;
940 if (!url && remote->url)
941 url = remote->url[0];
944 /* maybe it is a foreign URL? */
948 while (is_urlschemechar(p == url, *p))
950 if (starts_with(p, "::"))
951 helper = xstrndup(url, p - url);
955 transport_helper_init(ret, helper);
956 } else if (starts_with(url, "rsync:")) {
957 die(_("git-over-rsync is no longer supported"));
958 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
959 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
960 transport_check_allowed("file");
962 ret->vtable = &bundle_vtable;
963 ret->smart_options = NULL;
964 } else if (!is_url(url)
965 || starts_with(url, "file://")
966 || starts_with(url, "git://")
967 || starts_with(url, "ssh://")
968 || starts_with(url, "git+ssh://") /* deprecated - do not use */
969 || starts_with(url, "ssh+git://") /* deprecated - do not use */
972 * These are builtin smart transports; "allowed" transports
973 * will be checked individually in git_connect.
975 struct git_transport_data *data = xcalloc(1, sizeof(*data));
977 ret->vtable = &builtin_smart_vtable;
978 ret->smart_options = &(data->options);
981 data->got_remote_heads = 0;
983 /* Unknown protocol in URL. Pass to external handler. */
984 int len = external_specification_len(url);
985 char *handler = xmemdupz(url, len);
986 transport_helper_init(ret, handler);
989 if (ret->smart_options) {
990 ret->smart_options->thin = 1;
991 ret->smart_options->uploadpack = "git-upload-pack";
992 if (remote->uploadpack)
993 ret->smart_options->uploadpack = remote->uploadpack;
994 ret->smart_options->receivepack = "git-receive-pack";
995 if (remote->receivepack)
996 ret->smart_options->receivepack = remote->receivepack;
999 ret->hash_algo = &hash_algos[GIT_HASH_SHA1];
1004 const struct git_hash_algo *transport_get_hash_algo(struct transport *transport)
1006 return transport->hash_algo;
1009 int transport_set_option(struct transport *transport,
1010 const char *name, const char *value)
1012 int git_reports = 1, protocol_reports = 1;
1014 if (transport->smart_options)
1015 git_reports = set_git_option(transport->smart_options,
1018 if (transport->vtable->set_option)
1019 protocol_reports = transport->vtable->set_option(transport,
1022 /* If either report is 0, report 0 (success). */
1023 if (!git_reports || !protocol_reports)
1025 /* If either reports -1 (invalid value), report -1. */
1026 if ((git_reports == -1) || (protocol_reports == -1))
1028 /* Otherwise if both report unknown, report unknown. */
1032 void transport_set_verbosity(struct transport *transport, int verbosity,
1036 transport->verbose = verbosity <= 3 ? verbosity : 3;
1038 transport->verbose = -1;
1041 * Rules used to determine whether to report progress (processing aborts
1042 * when a rule is satisfied):
1044 * . Report progress, if force_progress is 1 (ie. --progress).
1045 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1046 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1047 * . Report progress if isatty(2) is 1.
1049 if (force_progress >= 0)
1050 transport->progress = !!force_progress;
1052 transport->progress = verbosity >= 0 && isatty(2);
1055 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
1059 fprintf(stderr, _("The following submodule paths contain changes that can\n"
1060 "not be found on any remote:\n"));
1061 for (i = 0; i < needs_pushing->nr; i++)
1062 fprintf(stderr, " %s\n", needs_pushing->items[i].string);
1063 fprintf(stderr, _("\nPlease try\n\n"
1064 " git push --recurse-submodules=on-demand\n\n"
1065 "or cd to the path and use\n\n"
1067 "to push them to a remote.\n\n"));
1069 string_list_clear(needs_pushing, 0);
1071 die(_("Aborting."));
1074 static int run_pre_push_hook(struct transport *transport,
1075 struct ref *remote_refs)
1079 struct child_process proc = CHILD_PROCESS_INIT;
1081 const char *argv[4];
1083 if (!(argv[0] = find_hook("pre-push")))
1086 argv[1] = transport->remote->name;
1087 argv[2] = transport->url;
1092 proc.trace2_hook_name = "pre-push";
1094 if (start_command(&proc)) {
1095 finish_command(&proc);
1099 sigchain_push(SIGPIPE, SIG_IGN);
1101 strbuf_init(&buf, 256);
1103 for (r = remote_refs; r; r = r->next) {
1104 if (!r->peer_ref) continue;
1105 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
1106 if (r->status == REF_STATUS_REJECT_STALE) continue;
1107 if (r->status == REF_STATUS_UPTODATE) continue;
1110 strbuf_addf( &buf, "%s %s %s %s\n",
1111 r->peer_ref->name, oid_to_hex(&r->new_oid),
1112 r->name, oid_to_hex(&r->old_oid));
1114 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
1115 /* We do not mind if a hook does not read all refs. */
1122 strbuf_release(&buf);
1128 sigchain_pop(SIGPIPE);
1130 x = finish_command(&proc);
1137 int transport_push(struct repository *r,
1138 struct transport *transport,
1139 struct refspec *rs, int flags,
1140 unsigned int *reject_reasons)
1142 *reject_reasons = 0;
1144 if (transport_color_config() < 0)
1147 if (transport->vtable->push_refs) {
1148 struct ref *remote_refs;
1149 struct ref *local_refs = get_local_heads();
1150 int match_flags = MATCH_REFS_NONE;
1151 int verbose = (transport->verbose > 0);
1152 int quiet = (transport->verbose < 0);
1153 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1154 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1155 int push_ret, ret, err;
1156 struct strvec ref_prefixes = STRVEC_INIT;
1158 if (check_push_refs(local_refs, rs) < 0)
1161 refspec_ref_prefixes(rs, &ref_prefixes);
1163 trace2_region_enter("transport_push", "get_refs_list", r);
1164 remote_refs = transport->vtable->get_refs_list(transport, 1,
1166 trace2_region_leave("transport_push", "get_refs_list", r);
1168 strvec_clear(&ref_prefixes);
1170 if (flags & TRANSPORT_PUSH_ALL)
1171 match_flags |= MATCH_REFS_ALL;
1172 if (flags & TRANSPORT_PUSH_MIRROR)
1173 match_flags |= MATCH_REFS_MIRROR;
1174 if (flags & TRANSPORT_PUSH_PRUNE)
1175 match_flags |= MATCH_REFS_PRUNE;
1176 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1177 match_flags |= MATCH_REFS_FOLLOW_TAGS;
1179 if (match_push_refs(local_refs, &remote_refs, rs, match_flags))
1182 if (transport->smart_options &&
1183 transport->smart_options->cas &&
1184 !is_empty_cas(transport->smart_options->cas))
1185 apply_push_cas(transport->smart_options->cas,
1186 transport->remote, remote_refs);
1188 set_ref_status_for_push(remote_refs,
1189 flags & TRANSPORT_PUSH_MIRROR,
1190 flags & TRANSPORT_PUSH_FORCE);
1192 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1193 if (run_pre_push_hook(transport, remote_refs))
1196 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1197 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1198 !is_bare_repository()) {
1199 struct ref *ref = remote_refs;
1200 struct oid_array commits = OID_ARRAY_INIT;
1202 trace2_region_enter("transport_push", "push_submodules", r);
1203 for (; ref; ref = ref->next)
1204 if (!is_null_oid(&ref->new_oid))
1205 oid_array_append(&commits,
1208 if (!push_unpushed_submodules(r,
1212 transport->push_options,
1214 oid_array_clear(&commits);
1215 trace2_region_leave("transport_push", "push_submodules", r);
1216 die(_("failed to push all needed submodules"));
1218 oid_array_clear(&commits);
1219 trace2_region_leave("transport_push", "push_submodules", r);
1222 if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1223 ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1224 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1225 !pretend)) && !is_bare_repository()) {
1226 struct ref *ref = remote_refs;
1227 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1228 struct oid_array commits = OID_ARRAY_INIT;
1230 trace2_region_enter("transport_push", "check_submodules", r);
1231 for (; ref; ref = ref->next)
1232 if (!is_null_oid(&ref->new_oid))
1233 oid_array_append(&commits,
1236 if (find_unpushed_submodules(r,
1238 transport->remote->name,
1240 oid_array_clear(&commits);
1241 trace2_region_leave("transport_push", "check_submodules", r);
1242 die_with_unpushed_submodules(&needs_pushing);
1244 string_list_clear(&needs_pushing, 0);
1245 oid_array_clear(&commits);
1246 trace2_region_leave("transport_push", "check_submodules", r);
1249 if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY)) {
1250 trace2_region_enter("transport_push", "push_refs", r);
1251 push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1252 trace2_region_leave("transport_push", "push_refs", r);
1255 err = push_had_errors(remote_refs);
1256 ret = push_ret | err;
1259 transport_print_push_status(transport->url, remote_refs,
1260 verbose | porcelain, porcelain,
1263 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1264 set_upstreams(transport, remote_refs, pretend);
1266 if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1267 TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1269 for (ref = remote_refs; ref; ref = ref->next)
1270 transport_update_tracking_ref(transport->remote, ref, verbose);
1273 if (porcelain && !push_ret)
1275 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1276 fprintf(stderr, "Everything up-to-date\n");
1283 const struct ref *transport_get_remote_refs(struct transport *transport,
1284 const struct strvec *ref_prefixes)
1286 if (!transport->got_remote_refs) {
1287 transport->remote_refs =
1288 transport->vtable->get_refs_list(transport, 0,
1290 transport->got_remote_refs = 1;
1293 return transport->remote_refs;
1296 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1299 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1300 struct ref **heads = NULL;
1303 for (rm = refs; rm; rm = rm->next) {
1306 !is_null_oid(&rm->old_oid) &&
1307 oideq(&rm->peer_ref->old_oid, &rm->old_oid))
1309 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1310 heads[nr_heads++] = rm;
1315 * When deepening of a shallow repository is requested,
1316 * then local and remote refs are likely to still be equal.
1317 * Just feed them all to the fetch method in that case.
1318 * This condition shouldn't be met in a non-deepening fetch
1319 * (see builtin/fetch.c:quickfetch()).
1321 ALLOC_ARRAY(heads, nr_refs);
1322 for (rm = refs; rm; rm = rm->next)
1323 heads[nr_heads++] = rm;
1326 rc = transport->vtable->fetch(transport, nr_heads, heads);
1332 void transport_unlock_pack(struct transport *transport)
1336 for (i = 0; i < transport->pack_lockfiles.nr; i++)
1337 unlink_or_warn(transport->pack_lockfiles.items[i].string);
1338 string_list_clear(&transport->pack_lockfiles, 0);
1341 int transport_connect(struct transport *transport, const char *name,
1342 const char *exec, int fd[2])
1344 if (transport->vtable->connect)
1345 return transport->vtable->connect(transport, name, exec, fd);
1347 die(_("operation not supported by protocol"));
1350 int transport_disconnect(struct transport *transport)
1353 if (transport->vtable->disconnect)
1354 ret = transport->vtable->disconnect(transport);
1360 * Strip username (and password) from a URL and return
1361 * it in a newly allocated string.
1363 char *transport_anonymize_url(const char *url)
1365 char *scheme_prefix, *anon_part;
1366 size_t anon_len, prefix_len = 0;
1368 anon_part = strchr(url, '@');
1369 if (url_is_local_not_ssh(url) || !anon_part)
1372 anon_len = strlen(++anon_part);
1373 scheme_prefix = strstr(url, "://");
1374 if (!scheme_prefix) {
1375 if (!strchr(anon_part, ':'))
1376 /* cannot be "me@there:/path/name" */
1380 /* make sure scheme is reasonable */
1381 for (cp = url; cp < scheme_prefix; cp++) {
1384 case '+': case '.': case '-':
1393 /* @ past the first slash does not count */
1394 cp = strchr(scheme_prefix + 3, '/');
1395 if (cp && cp < anon_part)
1397 prefix_len = scheme_prefix - url + 3;
1399 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1400 (int)anon_len, anon_part);
1402 return xstrdup(url);