scalar: allow reconfiguring an existing enlistment
[git/debian.git] / transport.c
blobe4f1decae2063ce8981344c19626141c8bcd866c
1 #include "cache.h"
2 #include "config.h"
3 #include "transport.h"
4 #include "hook.h"
5 #include "pkt-line.h"
6 #include "fetch-pack.h"
7 #include "remote.h"
8 #include "connect.h"
9 #include "send-pack.h"
10 #include "walker.h"
11 #include "bundle.h"
12 #include "dir.h"
13 #include "refs.h"
14 #include "refspec.h"
15 #include "branch.h"
16 #include "url.h"
17 #include "submodule.h"
18 #include "string-list.h"
19 #include "oid-array.h"
20 #include "sigchain.h"
21 #include "transport-internal.h"
22 #include "protocol.h"
23 #include "object-store.h"
24 #include "color.h"
26 static int transport_use_color = -1;
27 static char transport_colors[][COLOR_MAXLEN] = {
28 GIT_COLOR_RESET,
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";
43 char *value;
44 int i;
45 static int initialized;
47 if (initialized)
48 return 0;
49 initialized = 1;
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))
55 return 0;
57 for (i = 0; i < ARRAY_SIZE(keys); i++)
58 if (!git_config_get_string(keys[i], &value)) {
59 if (!value)
60 return config_error_nonbool(keys[i]);
61 if (color_parse(value, transport_colors[i]) < 0)
62 return -1;
65 return 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];
72 return "";
75 static void set_upstreams(struct transport *transport, struct ref *refs,
76 int pretend)
78 struct ref *ref;
79 for (ref = refs; ref; ref = ref->next) {
80 const char *localname;
81 const char *tmp;
82 const char *remotename;
83 int flag = 0;
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)
90 continue;
91 if (!ref->peer_ref)
92 continue;
93 if (is_null_oid(&ref->new_oid))
94 continue;
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,
100 NULL, &flag);
101 if (tmp && flag & REF_ISSYMREF &&
102 starts_with(tmp, "refs/heads/"))
103 localname = tmp;
105 /* Both source and destination must be local branches. */
106 if (!localname || !starts_with(localname, "refs/heads/"))
107 continue;
108 if (!remotename || !starts_with(remotename, "refs/heads/"))
109 continue;
111 if (!pretend) {
112 int flag = transport->verbose < 0 ? 0 : BRANCH_CONFIG_VERBOSE;
113 install_branch_config(flag, localname + 11,
114 transport->remote->name, remotename);
115 } else if (transport->verbose >= 0)
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 {
123 int fd;
124 struct bundle_header header;
125 unsigned get_refs_from_bundle_called : 1;
128 static struct ref *get_refs_from_bundle(struct transport *transport,
129 int for_push,
130 struct transport_ls_refs_options *transport_options)
132 struct bundle_transport_data *data = transport->data;
133 struct ref *result = NULL;
134 int i;
136 if (for_push)
137 return NULL;
139 data->get_refs_from_bundle_called = 1;
141 if (data->fd > 0)
142 close(data->fd);
143 data->fd = read_bundle_header(transport->url, &data->header);
144 if (data->fd < 0)
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 string_list_item *e = data->header.references.items + i;
151 const char *name = e->string;
152 struct ref *ref = alloc_ref(name);
153 struct object_id *oid = e->util;
154 oidcpy(&ref->old_oid, oid);
155 ref->next = result;
156 result = ref;
158 return result;
161 static int fetch_refs_from_bundle(struct transport *transport,
162 int nr_heads, struct ref **to_fetch)
164 struct bundle_transport_data *data = transport->data;
165 struct strvec extra_index_pack_args = STRVEC_INIT;
166 int ret;
168 if (transport->progress)
169 strvec_push(&extra_index_pack_args, "-v");
171 if (!data->get_refs_from_bundle_called)
172 get_refs_from_bundle(transport, 0, NULL);
173 ret = unbundle(the_repository, &data->header, data->fd,
174 &extra_index_pack_args);
175 transport->hash_algo = data->header.hash_algo;
176 return ret;
179 static int close_bundle(struct transport *transport)
181 struct bundle_transport_data *data = transport->data;
182 if (data->fd > 0)
183 close(data->fd);
184 bundle_header_release(&data->header);
185 free(data);
186 return 0;
189 struct git_transport_data {
190 struct git_transport_options options;
191 struct child_process *conn;
192 int fd[2];
193 unsigned got_remote_heads : 1;
194 enum protocol_version version;
195 struct oid_array extra_have;
196 struct oid_array shallow;
199 static int set_git_option(struct git_transport_options *opts,
200 const char *name, const char *value)
202 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
203 opts->uploadpack = value;
204 return 0;
205 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
206 opts->receivepack = value;
207 return 0;
208 } else if (!strcmp(name, TRANS_OPT_THIN)) {
209 opts->thin = !!value;
210 return 0;
211 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
212 opts->followtags = !!value;
213 return 0;
214 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
215 opts->keep = !!value;
216 return 0;
217 } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
218 opts->update_shallow = !!value;
219 return 0;
220 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
221 if (!value)
222 opts->depth = 0;
223 else {
224 char *end;
225 opts->depth = strtol(value, &end, 0);
226 if (*end)
227 die(_("transport: invalid depth option '%s'"), value);
229 return 0;
230 } else if (!strcmp(name, TRANS_OPT_DEEPEN_SINCE)) {
231 opts->deepen_since = value;
232 return 0;
233 } else if (!strcmp(name, TRANS_OPT_DEEPEN_NOT)) {
234 opts->deepen_not = (const struct string_list *)value;
235 return 0;
236 } else if (!strcmp(name, TRANS_OPT_DEEPEN_RELATIVE)) {
237 opts->deepen_relative = !!value;
238 return 0;
239 } else if (!strcmp(name, TRANS_OPT_FROM_PROMISOR)) {
240 opts->from_promisor = !!value;
241 return 0;
242 } else if (!strcmp(name, TRANS_OPT_LIST_OBJECTS_FILTER)) {
243 list_objects_filter_die_if_populated(&opts->filter_options);
244 parse_list_objects_filter(&opts->filter_options, value);
245 return 0;
246 } else if (!strcmp(name, TRANS_OPT_REJECT_SHALLOW)) {
247 opts->reject_shallow = !!value;
248 return 0;
250 return 1;
253 static int connect_setup(struct transport *transport, int for_push)
255 struct git_transport_data *data = transport->data;
256 int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
258 if (data->conn)
259 return 0;
261 switch (transport->family) {
262 case TRANSPORT_FAMILY_ALL: break;
263 case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
264 case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
267 data->conn = git_connect(data->fd, transport->url,
268 for_push ? data->options.receivepack :
269 data->options.uploadpack,
270 flags);
272 return 0;
275 static void die_if_server_options(struct transport *transport)
277 if (!transport->server_options || !transport->server_options->nr)
278 return;
279 advise(_("see protocol.version in 'git help config' for more details"));
280 die(_("server options require protocol version 2 or later"));
284 * Obtains the protocol version from the transport and writes it to
285 * transport->data->version, first connecting if not already connected.
287 * If the protocol version is one that allows skipping the listing of remote
288 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
289 * this function returns NULL. Otherwise, this function returns the list of
290 * remote refs.
292 static struct ref *handshake(struct transport *transport, int for_push,
293 struct transport_ls_refs_options *options,
294 int must_list_refs)
296 struct git_transport_data *data = transport->data;
297 struct ref *refs = NULL;
298 struct packet_reader reader;
299 int sid_len;
300 const char *server_sid;
302 connect_setup(transport, for_push);
304 packet_reader_init(&reader, data->fd[0], NULL, 0,
305 PACKET_READ_CHOMP_NEWLINE |
306 PACKET_READ_GENTLE_ON_EOF |
307 PACKET_READ_DIE_ON_ERR_PACKET);
309 data->version = discover_version(&reader);
310 switch (data->version) {
311 case protocol_v2:
312 if (server_feature_v2("session-id", &server_sid))
313 trace2_data_string("transfer", NULL, "server-sid", server_sid);
314 if (must_list_refs)
315 get_remote_refs(data->fd[1], &reader, &refs, for_push,
316 options,
317 transport->server_options,
318 transport->stateless_rpc);
319 break;
320 case protocol_v1:
321 case protocol_v0:
322 die_if_server_options(transport);
323 get_remote_heads(&reader, &refs,
324 for_push ? REF_NORMAL : 0,
325 &data->extra_have,
326 &data->shallow);
327 server_sid = server_feature_value("session-id", &sid_len);
328 if (server_sid) {
329 char *sid = xstrndup(server_sid, sid_len);
330 trace2_data_string("transfer", NULL, "server-sid", sid);
331 free(sid);
333 break;
334 case protocol_unknown_version:
335 BUG("unknown protocol version");
337 data->got_remote_heads = 1;
338 transport->hash_algo = reader.hash_algo;
340 if (reader.line_peeked)
341 BUG("buffer must be empty at the end of handshake()");
343 return refs;
346 static struct ref *get_refs_via_connect(struct transport *transport, int for_push,
347 struct transport_ls_refs_options *options)
349 return handshake(transport, for_push, options, 1);
352 static int fetch_refs_via_pack(struct transport *transport,
353 int nr_heads, struct ref **to_fetch)
355 int ret = 0;
356 struct git_transport_data *data = transport->data;
357 struct ref *refs = NULL;
358 struct fetch_pack_args args;
359 struct ref *refs_tmp = NULL;
361 memset(&args, 0, sizeof(args));
362 args.uploadpack = data->options.uploadpack;
363 args.keep_pack = data->options.keep;
364 args.lock_pack = 1;
365 args.use_thin_pack = data->options.thin;
366 args.include_tag = data->options.followtags;
367 args.verbose = (transport->verbose > 1);
368 args.quiet = (transport->verbose < 0);
369 args.no_progress = !transport->progress;
370 args.depth = data->options.depth;
371 args.deepen_since = data->options.deepen_since;
372 args.deepen_not = data->options.deepen_not;
373 args.deepen_relative = data->options.deepen_relative;
374 args.check_self_contained_and_connected =
375 data->options.check_self_contained_and_connected;
376 args.cloning = transport->cloning;
377 args.update_shallow = data->options.update_shallow;
378 args.from_promisor = data->options.from_promisor;
379 args.filter_options = data->options.filter_options;
380 args.stateless_rpc = transport->stateless_rpc;
381 args.server_options = transport->server_options;
382 args.negotiation_tips = data->options.negotiation_tips;
383 args.reject_shallow_remote = transport->smart_options->reject_shallow;
385 if (!data->got_remote_heads) {
386 int i;
387 int must_list_refs = 0;
388 for (i = 0; i < nr_heads; i++) {
389 if (!to_fetch[i]->exact_oid) {
390 must_list_refs = 1;
391 break;
394 refs_tmp = handshake(transport, 0, NULL, must_list_refs);
397 if (data->version == protocol_unknown_version)
398 BUG("unknown protocol version");
399 else if (data->version <= protocol_v1)
400 die_if_server_options(transport);
402 if (data->options.acked_commits) {
403 if (data->version < protocol_v2) {
404 warning(_("--negotiate-only requires protocol v2"));
405 ret = -1;
406 } else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
407 warning(_("server does not support wait-for-done"));
408 ret = -1;
409 } else {
410 negotiate_using_fetch(data->options.negotiation_tips,
411 transport->server_options,
412 transport->stateless_rpc,
413 data->fd,
414 data->options.acked_commits);
415 ret = 0;
417 goto cleanup;
420 refs = fetch_pack(&args, data->fd,
421 refs_tmp ? refs_tmp : transport->remote_refs,
422 to_fetch, nr_heads, &data->shallow,
423 &transport->pack_lockfiles, data->version);
425 data->got_remote_heads = 0;
426 data->options.self_contained_and_connected =
427 args.self_contained_and_connected;
428 data->options.connectivity_checked = args.connectivity_checked;
430 if (refs == NULL)
431 ret = -1;
432 if (report_unmatched_refs(to_fetch, nr_heads))
433 ret = -1;
435 cleanup:
436 close(data->fd[0]);
437 if (data->fd[1] >= 0)
438 close(data->fd[1]);
439 if (finish_connect(data->conn))
440 ret = -1;
441 data->conn = NULL;
443 free_refs(refs_tmp);
444 free_refs(refs);
445 return ret;
448 static int push_had_errors(struct ref *ref)
450 for (; ref; ref = ref->next) {
451 switch (ref->status) {
452 case REF_STATUS_NONE:
453 case REF_STATUS_UPTODATE:
454 case REF_STATUS_OK:
455 break;
456 default:
457 return 1;
460 return 0;
463 int transport_refs_pushed(struct ref *ref)
465 for (; ref; ref = ref->next) {
466 switch(ref->status) {
467 case REF_STATUS_NONE:
468 case REF_STATUS_UPTODATE:
469 break;
470 default:
471 return 1;
474 return 0;
477 static void update_one_tracking_ref(struct remote *remote, char *refname,
478 struct object_id *new_oid, int deletion,
479 int verbose)
481 struct refspec_item rs;
483 memset(&rs, 0, sizeof(rs));
484 rs.src = refname;
485 rs.dst = NULL;
487 if (!remote_find_tracking(remote, &rs)) {
488 if (verbose)
489 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
490 if (deletion)
491 delete_ref(NULL, rs.dst, NULL, 0);
492 else
493 update_ref("update by push", rs.dst, new_oid,
494 NULL, 0, 0);
495 free(rs.dst);
499 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
501 char *refname;
502 struct object_id *new_oid;
503 struct ref_push_report *report;
505 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
506 return;
508 report = ref->report;
509 if (!report)
510 update_one_tracking_ref(remote, ref->name, &ref->new_oid,
511 ref->deletion, verbose);
512 else
513 for (; report; report = report->next) {
514 refname = report->ref_name ? (char *)report->ref_name : ref->name;
515 new_oid = report->new_oid ? report->new_oid : &ref->new_oid;
516 update_one_tracking_ref(remote, refname, new_oid,
517 is_null_oid(new_oid), verbose);
521 static void print_ref_status(char flag, const char *summary,
522 struct ref *to, struct ref *from, const char *msg,
523 struct ref_push_report *report,
524 int porcelain, int summary_width)
526 const char *to_name;
528 if (report && report->ref_name)
529 to_name = report->ref_name;
530 else
531 to_name = to->name;
533 if (porcelain) {
534 if (from)
535 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to_name);
536 else
537 fprintf(stdout, "%c\t:%s\t", flag, to_name);
538 if (msg)
539 fprintf(stdout, "%s (%s)\n", summary, msg);
540 else
541 fprintf(stdout, "%s\n", summary);
542 } else {
543 const char *red = "", *reset = "";
544 if (push_had_errors(to)) {
545 red = transport_get_color(TRANSPORT_COLOR_REJECTED);
546 reset = transport_get_color(TRANSPORT_COLOR_RESET);
548 fprintf(stderr, " %s%c %-*s%s ", red, flag, summary_width,
549 summary, reset);
550 if (from)
551 fprintf(stderr, "%s -> %s",
552 prettify_refname(from->name),
553 prettify_refname(to_name));
554 else
555 fputs(prettify_refname(to_name), stderr);
556 if (msg) {
557 fputs(" (", stderr);
558 fputs(msg, stderr);
559 fputc(')', stderr);
561 fputc('\n', stderr);
565 static void print_ok_ref_status(struct ref *ref,
566 struct ref_push_report *report,
567 int porcelain, int summary_width)
569 struct object_id *old_oid;
570 struct object_id *new_oid;
571 const char *ref_name;
572 int forced_update;
574 if (report && report->old_oid)
575 old_oid = report->old_oid;
576 else
577 old_oid = &ref->old_oid;
578 if (report && report->new_oid)
579 new_oid = report->new_oid;
580 else
581 new_oid = &ref->new_oid;
582 if (report && report->forced_update)
583 forced_update = report->forced_update;
584 else
585 forced_update = ref->forced_update;
586 if (report && report->ref_name)
587 ref_name = report->ref_name;
588 else
589 ref_name = ref->name;
591 if (ref->deletion)
592 print_ref_status('-', "[deleted]", ref, NULL, NULL,
593 report, porcelain, summary_width);
594 else if (is_null_oid(old_oid))
595 print_ref_status('*',
596 (starts_with(ref_name, "refs/tags/")
597 ? "[new tag]"
598 : (starts_with(ref_name, "refs/heads/")
599 ? "[new branch]"
600 : "[new reference]")),
601 ref, ref->peer_ref, NULL,
602 report, porcelain, summary_width);
603 else {
604 struct strbuf quickref = STRBUF_INIT;
605 char type;
606 const char *msg;
608 strbuf_add_unique_abbrev(&quickref, old_oid,
609 DEFAULT_ABBREV);
610 if (forced_update) {
611 strbuf_addstr(&quickref, "...");
612 type = '+';
613 msg = "forced update";
614 } else {
615 strbuf_addstr(&quickref, "..");
616 type = ' ';
617 msg = NULL;
619 strbuf_add_unique_abbrev(&quickref, new_oid,
620 DEFAULT_ABBREV);
622 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
623 report, porcelain, summary_width);
624 strbuf_release(&quickref);
628 static int print_one_push_report(struct ref *ref, const char *dest, int count,
629 struct ref_push_report *report,
630 int porcelain, int summary_width)
632 if (!count) {
633 char *url = transport_anonymize_url(dest);
634 fprintf(porcelain ? stdout : stderr, "To %s\n", url);
635 free(url);
638 switch(ref->status) {
639 case REF_STATUS_NONE:
640 print_ref_status('X', "[no match]", ref, NULL, NULL,
641 report, porcelain, summary_width);
642 break;
643 case REF_STATUS_REJECT_NODELETE:
644 print_ref_status('!', "[rejected]", ref, NULL,
645 "remote does not support deleting refs",
646 report, porcelain, summary_width);
647 break;
648 case REF_STATUS_UPTODATE:
649 print_ref_status('=', "[up to date]", ref,
650 ref->peer_ref, NULL,
651 report, porcelain, summary_width);
652 break;
653 case REF_STATUS_REJECT_NONFASTFORWARD:
654 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
655 "non-fast-forward",
656 report, porcelain, summary_width);
657 break;
658 case REF_STATUS_REJECT_ALREADY_EXISTS:
659 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
660 "already exists",
661 report, porcelain, summary_width);
662 break;
663 case REF_STATUS_REJECT_FETCH_FIRST:
664 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
665 "fetch first",
666 report, porcelain, summary_width);
667 break;
668 case REF_STATUS_REJECT_NEEDS_FORCE:
669 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
670 "needs force",
671 report, porcelain, summary_width);
672 break;
673 case REF_STATUS_REJECT_STALE:
674 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
675 "stale info",
676 report, porcelain, summary_width);
677 break;
678 case REF_STATUS_REJECT_REMOTE_UPDATED:
679 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
680 "remote ref updated since checkout",
681 report, porcelain, summary_width);
682 break;
683 case REF_STATUS_REJECT_SHALLOW:
684 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
685 "new shallow roots not allowed",
686 report, porcelain, summary_width);
687 break;
688 case REF_STATUS_REMOTE_REJECT:
689 print_ref_status('!', "[remote rejected]", ref,
690 ref->deletion ? NULL : ref->peer_ref,
691 ref->remote_status,
692 report, porcelain, summary_width);
693 break;
694 case REF_STATUS_EXPECTING_REPORT:
695 print_ref_status('!', "[remote failure]", ref,
696 ref->deletion ? NULL : ref->peer_ref,
697 "remote failed to report status",
698 report, porcelain, summary_width);
699 break;
700 case REF_STATUS_ATOMIC_PUSH_FAILED:
701 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
702 "atomic push failed",
703 report, porcelain, summary_width);
704 break;
705 case REF_STATUS_OK:
706 print_ok_ref_status(ref, report, porcelain, summary_width);
707 break;
710 return 1;
713 static int print_one_push_status(struct ref *ref, const char *dest, int count,
714 int porcelain, int summary_width)
716 struct ref_push_report *report;
717 int n = 0;
719 if (!ref->report)
720 return print_one_push_report(ref, dest, count,
721 NULL, porcelain, summary_width);
723 for (report = ref->report; report; report = report->next)
724 print_one_push_report(ref, dest, count + n++,
725 report, porcelain, summary_width);
726 return n;
729 static int measure_abbrev(const struct object_id *oid, int sofar)
731 char hex[GIT_MAX_HEXSZ + 1];
732 int w = find_unique_abbrev_r(hex, oid, DEFAULT_ABBREV);
734 return (w < sofar) ? sofar : w;
737 int transport_summary_width(const struct ref *refs)
739 int maxw = -1;
741 for (; refs; refs = refs->next) {
742 maxw = measure_abbrev(&refs->old_oid, maxw);
743 maxw = measure_abbrev(&refs->new_oid, maxw);
745 if (maxw < 0)
746 maxw = FALLBACK_DEFAULT_ABBREV;
747 return (2 * maxw + 3);
750 void transport_print_push_status(const char *dest, struct ref *refs,
751 int verbose, int porcelain, unsigned int *reject_reasons)
753 struct ref *ref;
754 int n = 0;
755 char *head;
756 int summary_width = transport_summary_width(refs);
758 if (transport_color_config() < 0)
759 warning(_("could not parse transport.color.* config"));
761 head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
763 if (verbose) {
764 for (ref = refs; ref; ref = ref->next)
765 if (ref->status == REF_STATUS_UPTODATE)
766 n += print_one_push_status(ref, dest, n,
767 porcelain, summary_width);
770 for (ref = refs; ref; ref = ref->next)
771 if (ref->status == REF_STATUS_OK)
772 n += print_one_push_status(ref, dest, n,
773 porcelain, summary_width);
775 *reject_reasons = 0;
776 for (ref = refs; ref; ref = ref->next) {
777 if (ref->status != REF_STATUS_NONE &&
778 ref->status != REF_STATUS_UPTODATE &&
779 ref->status != REF_STATUS_OK)
780 n += print_one_push_status(ref, dest, n,
781 porcelain, summary_width);
782 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
783 if (head != NULL && !strcmp(head, ref->name))
784 *reject_reasons |= REJECT_NON_FF_HEAD;
785 else
786 *reject_reasons |= REJECT_NON_FF_OTHER;
787 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
788 *reject_reasons |= REJECT_ALREADY_EXISTS;
789 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
790 *reject_reasons |= REJECT_FETCH_FIRST;
791 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
792 *reject_reasons |= REJECT_NEEDS_FORCE;
793 } else if (ref->status == REF_STATUS_REJECT_REMOTE_UPDATED) {
794 *reject_reasons |= REJECT_REF_NEEDS_UPDATE;
797 free(head);
800 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
802 struct git_transport_data *data = transport->data;
803 struct send_pack_args args;
804 int ret = 0;
806 if (transport_color_config() < 0)
807 return -1;
809 if (!data->got_remote_heads)
810 get_refs_via_connect(transport, 1, NULL);
812 memset(&args, 0, sizeof(args));
813 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
814 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
815 args.use_thin_pack = data->options.thin;
816 args.verbose = (transport->verbose > 0);
817 args.quiet = (transport->verbose < 0);
818 args.progress = transport->progress;
819 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
820 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
821 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
822 args.push_options = transport->push_options;
823 args.url = transport->url;
825 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
826 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
827 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
828 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
829 else
830 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
832 switch (data->version) {
833 case protocol_v2:
834 die(_("support for protocol v2 not implemented yet"));
835 break;
836 case protocol_v1:
837 case protocol_v0:
838 ret = send_pack(&args, data->fd, data->conn, remote_refs,
839 &data->extra_have);
840 break;
841 case protocol_unknown_version:
842 BUG("unknown protocol version");
845 close(data->fd[1]);
846 close(data->fd[0]);
848 * Atomic push may abort the connection early and close the pipe,
849 * which may cause an error for `finish_connect()`. Ignore this error
850 * for atomic git-push.
852 if (ret || args.atomic)
853 finish_connect(data->conn);
854 else
855 ret = finish_connect(data->conn);
856 data->conn = NULL;
857 data->got_remote_heads = 0;
859 return ret;
862 static int connect_git(struct transport *transport, const char *name,
863 const char *executable, int fd[2])
865 struct git_transport_data *data = transport->data;
866 data->conn = git_connect(data->fd, transport->url,
867 executable, 0);
868 fd[0] = data->fd[0];
869 fd[1] = data->fd[1];
870 return 0;
873 static int disconnect_git(struct transport *transport)
875 struct git_transport_data *data = transport->data;
876 if (data->conn) {
877 if (data->got_remote_heads && !transport->stateless_rpc)
878 packet_flush(data->fd[1]);
879 close(data->fd[0]);
880 if (data->fd[1] >= 0)
881 close(data->fd[1]);
882 finish_connect(data->conn);
885 free(data);
886 return 0;
889 static struct transport_vtable taken_over_vtable = {
890 .get_refs_list = get_refs_via_connect,
891 .fetch_refs = fetch_refs_via_pack,
892 .push_refs = git_transport_push,
893 .disconnect = disconnect_git
896 void transport_take_over(struct transport *transport,
897 struct child_process *child)
899 struct git_transport_data *data;
901 if (!transport->smart_options)
902 BUG("taking over transport requires non-NULL "
903 "smart_options field.");
905 CALLOC_ARRAY(data, 1);
906 data->options = *transport->smart_options;
907 data->conn = child;
908 data->fd[0] = data->conn->out;
909 data->fd[1] = data->conn->in;
910 data->got_remote_heads = 0;
911 transport->data = data;
913 transport->vtable = &taken_over_vtable;
914 transport->smart_options = &(data->options);
916 transport->cannot_reuse = 1;
919 static int is_file(const char *url)
921 struct stat buf;
922 if (stat(url, &buf))
923 return 0;
924 return S_ISREG(buf.st_mode);
927 static int external_specification_len(const char *url)
929 return strchr(url, ':') - url;
932 static const struct string_list *protocol_whitelist(void)
934 static int enabled = -1;
935 static struct string_list allowed = STRING_LIST_INIT_DUP;
937 if (enabled < 0) {
938 const char *v = getenv("GIT_ALLOW_PROTOCOL");
939 if (v) {
940 string_list_split(&allowed, v, ':', -1);
941 string_list_sort(&allowed);
942 enabled = 1;
943 } else {
944 enabled = 0;
948 return enabled ? &allowed : NULL;
951 enum protocol_allow_config {
952 PROTOCOL_ALLOW_NEVER = 0,
953 PROTOCOL_ALLOW_USER_ONLY,
954 PROTOCOL_ALLOW_ALWAYS
957 static enum protocol_allow_config parse_protocol_config(const char *key,
958 const char *value)
960 if (!strcasecmp(value, "always"))
961 return PROTOCOL_ALLOW_ALWAYS;
962 else if (!strcasecmp(value, "never"))
963 return PROTOCOL_ALLOW_NEVER;
964 else if (!strcasecmp(value, "user"))
965 return PROTOCOL_ALLOW_USER_ONLY;
967 die(_("unknown value for config '%s': %s"), key, value);
970 static enum protocol_allow_config get_protocol_config(const char *type)
972 char *key = xstrfmt("protocol.%s.allow", type);
973 char *value;
975 /* first check the per-protocol config */
976 if (!git_config_get_string(key, &value)) {
977 enum protocol_allow_config ret =
978 parse_protocol_config(key, value);
979 free(key);
980 free(value);
981 return ret;
983 free(key);
985 /* if defined, fallback to user-defined default for unknown protocols */
986 if (!git_config_get_string("protocol.allow", &value)) {
987 enum protocol_allow_config ret =
988 parse_protocol_config("protocol.allow", value);
989 free(value);
990 return ret;
993 /* fallback to built-in defaults */
994 /* known safe */
995 if (!strcmp(type, "http") ||
996 !strcmp(type, "https") ||
997 !strcmp(type, "git") ||
998 !strcmp(type, "ssh") ||
999 !strcmp(type, "file"))
1000 return PROTOCOL_ALLOW_ALWAYS;
1002 /* known scary; err on the side of caution */
1003 if (!strcmp(type, "ext"))
1004 return PROTOCOL_ALLOW_NEVER;
1006 /* unknown; by default let them be used only directly by the user */
1007 return PROTOCOL_ALLOW_USER_ONLY;
1010 int is_transport_allowed(const char *type, int from_user)
1012 const struct string_list *whitelist = protocol_whitelist();
1013 if (whitelist)
1014 return string_list_has_string(whitelist, type);
1016 switch (get_protocol_config(type)) {
1017 case PROTOCOL_ALLOW_ALWAYS:
1018 return 1;
1019 case PROTOCOL_ALLOW_NEVER:
1020 return 0;
1021 case PROTOCOL_ALLOW_USER_ONLY:
1022 if (from_user < 0)
1023 from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1024 return from_user;
1027 BUG("invalid protocol_allow_config type");
1030 void transport_check_allowed(const char *type)
1032 if (!is_transport_allowed(type, -1))
1033 die(_("transport '%s' not allowed"), type);
1036 static struct transport_vtable bundle_vtable = {
1037 .get_refs_list = get_refs_from_bundle,
1038 .fetch_refs = fetch_refs_from_bundle,
1039 .disconnect = close_bundle
1042 static struct transport_vtable builtin_smart_vtable = {
1043 .get_refs_list = get_refs_via_connect,
1044 .fetch_refs = fetch_refs_via_pack,
1045 .push_refs = git_transport_push,
1046 .connect = connect_git,
1047 .disconnect = disconnect_git
1050 struct transport *transport_get(struct remote *remote, const char *url)
1052 const char *helper;
1053 struct transport *ret = xcalloc(1, sizeof(*ret));
1055 ret->progress = isatty(2);
1056 string_list_init_dup(&ret->pack_lockfiles);
1058 if (!remote)
1059 BUG("No remote provided to transport_get()");
1061 ret->got_remote_refs = 0;
1062 ret->remote = remote;
1063 helper = remote->foreign_vcs;
1065 if (!url && remote->url)
1066 url = remote->url[0];
1067 ret->url = url;
1069 /* maybe it is a foreign URL? */
1070 if (url) {
1071 const char *p = url;
1073 while (is_urlschemechar(p == url, *p))
1074 p++;
1075 if (starts_with(p, "::"))
1076 helper = xstrndup(url, p - url);
1079 if (helper) {
1080 transport_helper_init(ret, helper);
1081 } else if (starts_with(url, "rsync:")) {
1082 die(_("git-over-rsync is no longer supported"));
1083 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
1084 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
1085 bundle_header_init(&data->header);
1086 transport_check_allowed("file");
1087 ret->data = data;
1088 ret->vtable = &bundle_vtable;
1089 ret->smart_options = NULL;
1090 } else if (!is_url(url)
1091 || starts_with(url, "file://")
1092 || starts_with(url, "git://")
1093 || starts_with(url, "ssh://")
1094 || starts_with(url, "git+ssh://") /* deprecated - do not use */
1095 || starts_with(url, "ssh+git://") /* deprecated - do not use */
1098 * These are builtin smart transports; "allowed" transports
1099 * will be checked individually in git_connect.
1101 struct git_transport_data *data = xcalloc(1, sizeof(*data));
1102 ret->data = data;
1103 ret->vtable = &builtin_smart_vtable;
1104 ret->smart_options = &(data->options);
1106 data->conn = NULL;
1107 data->got_remote_heads = 0;
1108 } else {
1109 /* Unknown protocol in URL. Pass to external handler. */
1110 int len = external_specification_len(url);
1111 char *handler = xmemdupz(url, len);
1112 transport_helper_init(ret, handler);
1115 if (ret->smart_options) {
1116 ret->smart_options->thin = 1;
1117 ret->smart_options->uploadpack = "git-upload-pack";
1118 if (remote->uploadpack)
1119 ret->smart_options->uploadpack = remote->uploadpack;
1120 ret->smart_options->receivepack = "git-receive-pack";
1121 if (remote->receivepack)
1122 ret->smart_options->receivepack = remote->receivepack;
1125 ret->hash_algo = &hash_algos[GIT_HASH_SHA1];
1127 return ret;
1130 const struct git_hash_algo *transport_get_hash_algo(struct transport *transport)
1132 return transport->hash_algo;
1135 int transport_set_option(struct transport *transport,
1136 const char *name, const char *value)
1138 int git_reports = 1, protocol_reports = 1;
1140 if (transport->smart_options)
1141 git_reports = set_git_option(transport->smart_options,
1142 name, value);
1144 if (transport->vtable->set_option)
1145 protocol_reports = transport->vtable->set_option(transport,
1146 name, value);
1148 /* If either report is 0, report 0 (success). */
1149 if (!git_reports || !protocol_reports)
1150 return 0;
1151 /* If either reports -1 (invalid value), report -1. */
1152 if ((git_reports == -1) || (protocol_reports == -1))
1153 return -1;
1154 /* Otherwise if both report unknown, report unknown. */
1155 return 1;
1158 void transport_set_verbosity(struct transport *transport, int verbosity,
1159 int force_progress)
1161 if (verbosity >= 1)
1162 transport->verbose = verbosity <= 3 ? verbosity : 3;
1163 if (verbosity < 0)
1164 transport->verbose = -1;
1167 * Rules used to determine whether to report progress (processing aborts
1168 * when a rule is satisfied):
1170 * . Report progress, if force_progress is 1 (ie. --progress).
1171 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1172 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1173 * . Report progress if isatty(2) is 1.
1175 if (force_progress >= 0)
1176 transport->progress = !!force_progress;
1177 else
1178 transport->progress = verbosity >= 0 && isatty(2);
1181 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
1183 int i;
1185 fprintf(stderr, _("The following submodule paths contain changes that can\n"
1186 "not be found on any remote:\n"));
1187 for (i = 0; i < needs_pushing->nr; i++)
1188 fprintf(stderr, " %s\n", needs_pushing->items[i].string);
1189 fprintf(stderr, _("\nPlease try\n\n"
1190 " git push --recurse-submodules=on-demand\n\n"
1191 "or cd to the path and use\n\n"
1192 " git push\n\n"
1193 "to push them to a remote.\n\n"));
1195 string_list_clear(needs_pushing, 0);
1197 die(_("Aborting."));
1200 static int run_pre_push_hook(struct transport *transport,
1201 struct ref *remote_refs)
1203 int ret = 0, x;
1204 struct ref *r;
1205 struct child_process proc = CHILD_PROCESS_INIT;
1206 struct strbuf buf;
1207 const char *argv[4];
1209 if (!(argv[0] = find_hook("pre-push")))
1210 return 0;
1212 argv[1] = transport->remote->name;
1213 argv[2] = transport->url;
1214 argv[3] = NULL;
1216 proc.argv = argv;
1217 proc.in = -1;
1218 proc.trace2_hook_name = "pre-push";
1220 if (start_command(&proc)) {
1221 finish_command(&proc);
1222 return -1;
1225 sigchain_push(SIGPIPE, SIG_IGN);
1227 strbuf_init(&buf, 256);
1229 for (r = remote_refs; r; r = r->next) {
1230 if (!r->peer_ref) continue;
1231 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
1232 if (r->status == REF_STATUS_REJECT_STALE) continue;
1233 if (r->status == REF_STATUS_REJECT_REMOTE_UPDATED) continue;
1234 if (r->status == REF_STATUS_UPTODATE) continue;
1236 strbuf_reset(&buf);
1237 strbuf_addf( &buf, "%s %s %s %s\n",
1238 r->peer_ref->name, oid_to_hex(&r->new_oid),
1239 r->name, oid_to_hex(&r->old_oid));
1241 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
1242 /* We do not mind if a hook does not read all refs. */
1243 if (errno != EPIPE)
1244 ret = -1;
1245 break;
1249 strbuf_release(&buf);
1251 x = close(proc.in);
1252 if (!ret)
1253 ret = x;
1255 sigchain_pop(SIGPIPE);
1257 x = finish_command(&proc);
1258 if (!ret)
1259 ret = x;
1261 return ret;
1264 int transport_push(struct repository *r,
1265 struct transport *transport,
1266 struct refspec *rs, int flags,
1267 unsigned int *reject_reasons)
1269 *reject_reasons = 0;
1271 if (transport_color_config() < 0)
1272 return -1;
1274 if (transport->vtable->push_refs) {
1275 struct ref *remote_refs;
1276 struct ref *local_refs = get_local_heads();
1277 int match_flags = MATCH_REFS_NONE;
1278 int verbose = (transport->verbose > 0);
1279 int quiet = (transport->verbose < 0);
1280 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1281 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1282 int push_ret, ret, err;
1283 struct transport_ls_refs_options transport_options =
1284 TRANSPORT_LS_REFS_OPTIONS_INIT;
1286 if (check_push_refs(local_refs, rs) < 0)
1287 return -1;
1289 refspec_ref_prefixes(rs, &transport_options.ref_prefixes);
1291 trace2_region_enter("transport_push", "get_refs_list", r);
1292 remote_refs = transport->vtable->get_refs_list(transport, 1,
1293 &transport_options);
1294 trace2_region_leave("transport_push", "get_refs_list", r);
1296 strvec_clear(&transport_options.ref_prefixes);
1298 if (flags & TRANSPORT_PUSH_ALL)
1299 match_flags |= MATCH_REFS_ALL;
1300 if (flags & TRANSPORT_PUSH_MIRROR)
1301 match_flags |= MATCH_REFS_MIRROR;
1302 if (flags & TRANSPORT_PUSH_PRUNE)
1303 match_flags |= MATCH_REFS_PRUNE;
1304 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1305 match_flags |= MATCH_REFS_FOLLOW_TAGS;
1307 if (match_push_refs(local_refs, &remote_refs, rs, match_flags))
1308 return -1;
1310 if (transport->smart_options &&
1311 transport->smart_options->cas &&
1312 !is_empty_cas(transport->smart_options->cas))
1313 apply_push_cas(transport->smart_options->cas,
1314 transport->remote, remote_refs);
1316 set_ref_status_for_push(remote_refs,
1317 flags & TRANSPORT_PUSH_MIRROR,
1318 flags & TRANSPORT_PUSH_FORCE);
1320 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1321 if (run_pre_push_hook(transport, remote_refs))
1322 return -1;
1324 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1325 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1326 !is_bare_repository()) {
1327 struct ref *ref = remote_refs;
1328 struct oid_array commits = OID_ARRAY_INIT;
1330 trace2_region_enter("transport_push", "push_submodules", r);
1331 for (; ref; ref = ref->next)
1332 if (!is_null_oid(&ref->new_oid))
1333 oid_array_append(&commits,
1334 &ref->new_oid);
1336 if (!push_unpushed_submodules(r,
1337 &commits,
1338 transport->remote,
1340 transport->push_options,
1341 pretend)) {
1342 oid_array_clear(&commits);
1343 trace2_region_leave("transport_push", "push_submodules", r);
1344 die(_("failed to push all needed submodules"));
1346 oid_array_clear(&commits);
1347 trace2_region_leave("transport_push", "push_submodules", r);
1350 if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1351 ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1352 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1353 !pretend)) && !is_bare_repository()) {
1354 struct ref *ref = remote_refs;
1355 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1356 struct oid_array commits = OID_ARRAY_INIT;
1358 trace2_region_enter("transport_push", "check_submodules", r);
1359 for (; ref; ref = ref->next)
1360 if (!is_null_oid(&ref->new_oid))
1361 oid_array_append(&commits,
1362 &ref->new_oid);
1364 if (find_unpushed_submodules(r,
1365 &commits,
1366 transport->remote->name,
1367 &needs_pushing)) {
1368 oid_array_clear(&commits);
1369 trace2_region_leave("transport_push", "check_submodules", r);
1370 die_with_unpushed_submodules(&needs_pushing);
1372 string_list_clear(&needs_pushing, 0);
1373 oid_array_clear(&commits);
1374 trace2_region_leave("transport_push", "check_submodules", r);
1377 if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY)) {
1378 trace2_region_enter("transport_push", "push_refs", r);
1379 push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1380 trace2_region_leave("transport_push", "push_refs", r);
1381 } else
1382 push_ret = 0;
1383 err = push_had_errors(remote_refs);
1384 ret = push_ret | err;
1386 if (!quiet || err)
1387 transport_print_push_status(transport->url, remote_refs,
1388 verbose | porcelain, porcelain,
1389 reject_reasons);
1391 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1392 set_upstreams(transport, remote_refs, pretend);
1394 if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1395 TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1396 struct ref *ref;
1397 for (ref = remote_refs; ref; ref = ref->next)
1398 transport_update_tracking_ref(transport->remote, ref, verbose);
1401 if (porcelain && !push_ret)
1402 puts("Done");
1403 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1404 fprintf(stderr, "Everything up-to-date\n");
1406 return ret;
1408 return 1;
1411 const struct ref *transport_get_remote_refs(struct transport *transport,
1412 struct transport_ls_refs_options *transport_options)
1414 if (!transport->got_remote_refs) {
1415 transport->remote_refs =
1416 transport->vtable->get_refs_list(transport, 0,
1417 transport_options);
1418 transport->got_remote_refs = 1;
1421 return transport->remote_refs;
1424 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1426 int rc;
1427 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1428 struct ref **heads = NULL;
1429 struct ref *rm;
1431 for (rm = refs; rm; rm = rm->next) {
1432 nr_refs++;
1433 if (rm->peer_ref &&
1434 !is_null_oid(&rm->old_oid) &&
1435 oideq(&rm->peer_ref->old_oid, &rm->old_oid))
1436 continue;
1437 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1438 heads[nr_heads++] = rm;
1441 if (!nr_heads) {
1443 * When deepening of a shallow repository is requested,
1444 * then local and remote refs are likely to still be equal.
1445 * Just feed them all to the fetch method in that case.
1446 * This condition shouldn't be met in a non-deepening fetch
1447 * (see builtin/fetch.c:quickfetch()).
1449 ALLOC_ARRAY(heads, nr_refs);
1450 for (rm = refs; rm; rm = rm->next)
1451 heads[nr_heads++] = rm;
1454 rc = transport->vtable->fetch_refs(transport, nr_heads, heads);
1456 free(heads);
1457 return rc;
1460 void transport_unlock_pack(struct transport *transport)
1462 int i;
1464 for (i = 0; i < transport->pack_lockfiles.nr; i++)
1465 unlink_or_warn(transport->pack_lockfiles.items[i].string);
1466 string_list_clear(&transport->pack_lockfiles, 0);
1469 int transport_connect(struct transport *transport, const char *name,
1470 const char *exec, int fd[2])
1472 if (transport->vtable->connect)
1473 return transport->vtable->connect(transport, name, exec, fd);
1474 else
1475 die(_("operation not supported by protocol"));
1478 int transport_disconnect(struct transport *transport)
1480 int ret = 0;
1481 if (transport->vtable->disconnect)
1482 ret = transport->vtable->disconnect(transport);
1483 if (transport->got_remote_refs)
1484 free_refs((void *)transport->remote_refs);
1485 free(transport);
1486 return ret;
1490 * Strip username (and password) from a URL and return
1491 * it in a newly allocated string.
1493 char *transport_anonymize_url(const char *url)
1495 char *scheme_prefix, *anon_part;
1496 size_t anon_len, prefix_len = 0;
1498 anon_part = strchr(url, '@');
1499 if (url_is_local_not_ssh(url) || !anon_part)
1500 goto literal_copy;
1502 anon_len = strlen(++anon_part);
1503 scheme_prefix = strstr(url, "://");
1504 if (!scheme_prefix) {
1505 if (!strchr(anon_part, ':'))
1506 /* cannot be "me@there:/path/name" */
1507 goto literal_copy;
1508 } else {
1509 const char *cp;
1510 /* make sure scheme is reasonable */
1511 for (cp = url; cp < scheme_prefix; cp++) {
1512 switch (*cp) {
1513 /* RFC 1738 2.1 */
1514 case '+': case '.': case '-':
1515 break; /* ok */
1516 default:
1517 if (isalnum(*cp))
1518 break;
1519 /* it isn't */
1520 goto literal_copy;
1523 /* @ past the first slash does not count */
1524 cp = strchr(scheme_prefix + 3, '/');
1525 if (cp && cp < anon_part)
1526 goto literal_copy;
1527 prefix_len = scheme_prefix - url + 3;
1529 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1530 (int)anon_len, anon_part);
1531 literal_copy:
1532 return xstrdup(url);