use fspathhash() everywhere
[git/debian.git] / transport.c
blob17e9629710a2f8053c2657be91425b1aab840991
1 #include "cache.h"
2 #include "config.h"
3 #include "transport.h"
4 #include "run-command.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 int ret;
167 if (!data->get_refs_from_bundle_called)
168 get_refs_from_bundle(transport, 0, NULL);
169 ret = unbundle(the_repository, &data->header, data->fd,
170 transport->progress ? BUNDLE_VERBOSE : 0);
171 transport->hash_algo = data->header.hash_algo;
172 return ret;
175 static int close_bundle(struct transport *transport)
177 struct bundle_transport_data *data = transport->data;
178 if (data->fd > 0)
179 close(data->fd);
180 bundle_header_release(&data->header);
181 free(data);
182 return 0;
185 struct git_transport_data {
186 struct git_transport_options options;
187 struct child_process *conn;
188 int fd[2];
189 unsigned got_remote_heads : 1;
190 enum protocol_version version;
191 struct oid_array extra_have;
192 struct oid_array shallow;
195 static int set_git_option(struct git_transport_options *opts,
196 const char *name, const char *value)
198 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
199 opts->uploadpack = value;
200 return 0;
201 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
202 opts->receivepack = value;
203 return 0;
204 } else if (!strcmp(name, TRANS_OPT_THIN)) {
205 opts->thin = !!value;
206 return 0;
207 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
208 opts->followtags = !!value;
209 return 0;
210 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
211 opts->keep = !!value;
212 return 0;
213 } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
214 opts->update_shallow = !!value;
215 return 0;
216 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
217 if (!value)
218 opts->depth = 0;
219 else {
220 char *end;
221 opts->depth = strtol(value, &end, 0);
222 if (*end)
223 die(_("transport: invalid depth option '%s'"), value);
225 return 0;
226 } else if (!strcmp(name, TRANS_OPT_DEEPEN_SINCE)) {
227 opts->deepen_since = value;
228 return 0;
229 } else if (!strcmp(name, TRANS_OPT_DEEPEN_NOT)) {
230 opts->deepen_not = (const struct string_list *)value;
231 return 0;
232 } else if (!strcmp(name, TRANS_OPT_DEEPEN_RELATIVE)) {
233 opts->deepen_relative = !!value;
234 return 0;
235 } else if (!strcmp(name, TRANS_OPT_FROM_PROMISOR)) {
236 opts->from_promisor = !!value;
237 return 0;
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);
241 return 0;
242 } else if (!strcmp(name, TRANS_OPT_REJECT_SHALLOW)) {
243 opts->reject_shallow = !!value;
244 return 0;
246 return 1;
249 static int connect_setup(struct transport *transport, int for_push)
251 struct git_transport_data *data = transport->data;
252 int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
254 if (data->conn)
255 return 0;
257 switch (transport->family) {
258 case TRANSPORT_FAMILY_ALL: break;
259 case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
260 case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
263 data->conn = git_connect(data->fd, transport->url,
264 for_push ? data->options.receivepack :
265 data->options.uploadpack,
266 flags);
268 return 0;
271 static void die_if_server_options(struct transport *transport)
273 if (!transport->server_options || !transport->server_options->nr)
274 return;
275 advise(_("see protocol.version in 'git help config' for more details"));
276 die(_("server options require protocol version 2 or later"));
280 * Obtains the protocol version from the transport and writes it to
281 * transport->data->version, first connecting if not already connected.
283 * If the protocol version is one that allows skipping the listing of remote
284 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
285 * this function returns NULL. Otherwise, this function returns the list of
286 * remote refs.
288 static struct ref *handshake(struct transport *transport, int for_push,
289 struct transport_ls_refs_options *options,
290 int must_list_refs)
292 struct git_transport_data *data = transport->data;
293 struct ref *refs = NULL;
294 struct packet_reader reader;
295 int sid_len;
296 const char *server_sid;
298 connect_setup(transport, for_push);
300 packet_reader_init(&reader, data->fd[0], NULL, 0,
301 PACKET_READ_CHOMP_NEWLINE |
302 PACKET_READ_GENTLE_ON_EOF |
303 PACKET_READ_DIE_ON_ERR_PACKET);
305 data->version = discover_version(&reader);
306 switch (data->version) {
307 case protocol_v2:
308 if (server_feature_v2("session-id", &server_sid))
309 trace2_data_string("transfer", NULL, "server-sid", server_sid);
310 if (must_list_refs)
311 get_remote_refs(data->fd[1], &reader, &refs, for_push,
312 options,
313 transport->server_options,
314 transport->stateless_rpc);
315 break;
316 case protocol_v1:
317 case protocol_v0:
318 die_if_server_options(transport);
319 get_remote_heads(&reader, &refs,
320 for_push ? REF_NORMAL : 0,
321 &data->extra_have,
322 &data->shallow);
323 server_sid = server_feature_value("session-id", &sid_len);
324 if (server_sid) {
325 char *sid = xstrndup(server_sid, sid_len);
326 trace2_data_string("transfer", NULL, "server-sid", sid);
327 free(sid);
329 break;
330 case protocol_unknown_version:
331 BUG("unknown protocol version");
333 data->got_remote_heads = 1;
334 transport->hash_algo = reader.hash_algo;
336 if (reader.line_peeked)
337 BUG("buffer must be empty at the end of handshake()");
339 return refs;
342 static struct ref *get_refs_via_connect(struct transport *transport, int for_push,
343 struct transport_ls_refs_options *options)
345 return handshake(transport, for_push, options, 1);
348 static int fetch_refs_via_pack(struct transport *transport,
349 int nr_heads, struct ref **to_fetch)
351 int ret = 0;
352 struct git_transport_data *data = transport->data;
353 struct ref *refs = NULL;
354 struct fetch_pack_args args;
355 struct ref *refs_tmp = NULL;
357 memset(&args, 0, sizeof(args));
358 args.uploadpack = data->options.uploadpack;
359 args.keep_pack = data->options.keep;
360 args.lock_pack = 1;
361 args.use_thin_pack = data->options.thin;
362 args.include_tag = data->options.followtags;
363 args.verbose = (transport->verbose > 1);
364 args.quiet = (transport->verbose < 0);
365 args.no_progress = !transport->progress;
366 args.depth = data->options.depth;
367 args.deepen_since = data->options.deepen_since;
368 args.deepen_not = data->options.deepen_not;
369 args.deepen_relative = data->options.deepen_relative;
370 args.check_self_contained_and_connected =
371 data->options.check_self_contained_and_connected;
372 args.cloning = transport->cloning;
373 args.update_shallow = data->options.update_shallow;
374 args.from_promisor = data->options.from_promisor;
375 args.filter_options = data->options.filter_options;
376 args.stateless_rpc = transport->stateless_rpc;
377 args.server_options = transport->server_options;
378 args.negotiation_tips = data->options.negotiation_tips;
379 args.reject_shallow_remote = transport->smart_options->reject_shallow;
381 if (!data->got_remote_heads) {
382 int i;
383 int must_list_refs = 0;
384 for (i = 0; i < nr_heads; i++) {
385 if (!to_fetch[i]->exact_oid) {
386 must_list_refs = 1;
387 break;
390 refs_tmp = handshake(transport, 0, NULL, must_list_refs);
393 if (data->version == protocol_unknown_version)
394 BUG("unknown protocol version");
395 else if (data->version <= protocol_v1)
396 die_if_server_options(transport);
398 if (data->options.acked_commits) {
399 if (data->version < protocol_v2) {
400 warning(_("--negotiate-only requires protocol v2"));
401 ret = -1;
402 } else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
403 warning(_("server does not support wait-for-done"));
404 ret = -1;
405 } else {
406 negotiate_using_fetch(data->options.negotiation_tips,
407 transport->server_options,
408 transport->stateless_rpc,
409 data->fd,
410 data->options.acked_commits);
411 ret = 0;
413 goto cleanup;
416 refs = fetch_pack(&args, data->fd,
417 refs_tmp ? refs_tmp : transport->remote_refs,
418 to_fetch, nr_heads, &data->shallow,
419 &transport->pack_lockfiles, data->version);
421 data->got_remote_heads = 0;
422 data->options.self_contained_and_connected =
423 args.self_contained_and_connected;
424 data->options.connectivity_checked = args.connectivity_checked;
426 if (refs == NULL)
427 ret = -1;
428 if (report_unmatched_refs(to_fetch, nr_heads))
429 ret = -1;
431 cleanup:
432 close(data->fd[0]);
433 if (data->fd[1] >= 0)
434 close(data->fd[1]);
435 if (finish_connect(data->conn))
436 ret = -1;
437 data->conn = NULL;
439 free_refs(refs_tmp);
440 free_refs(refs);
441 return ret;
444 static int push_had_errors(struct ref *ref)
446 for (; ref; ref = ref->next) {
447 switch (ref->status) {
448 case REF_STATUS_NONE:
449 case REF_STATUS_UPTODATE:
450 case REF_STATUS_OK:
451 break;
452 default:
453 return 1;
456 return 0;
459 int transport_refs_pushed(struct ref *ref)
461 for (; ref; ref = ref->next) {
462 switch(ref->status) {
463 case REF_STATUS_NONE:
464 case REF_STATUS_UPTODATE:
465 break;
466 default:
467 return 1;
470 return 0;
473 static void update_one_tracking_ref(struct remote *remote, char *refname,
474 struct object_id *new_oid, int deletion,
475 int verbose)
477 struct refspec_item rs;
479 memset(&rs, 0, sizeof(rs));
480 rs.src = refname;
481 rs.dst = NULL;
483 if (!remote_find_tracking(remote, &rs)) {
484 if (verbose)
485 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
486 if (deletion)
487 delete_ref(NULL, rs.dst, NULL, 0);
488 else
489 update_ref("update by push", rs.dst, new_oid,
490 NULL, 0, 0);
491 free(rs.dst);
495 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
497 char *refname;
498 struct object_id *new_oid;
499 struct ref_push_report *report;
501 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
502 return;
504 report = ref->report;
505 if (!report)
506 update_one_tracking_ref(remote, ref->name, &ref->new_oid,
507 ref->deletion, verbose);
508 else
509 for (; report; report = report->next) {
510 refname = report->ref_name ? (char *)report->ref_name : ref->name;
511 new_oid = report->new_oid ? report->new_oid : &ref->new_oid;
512 update_one_tracking_ref(remote, refname, new_oid,
513 is_null_oid(new_oid), verbose);
517 static void print_ref_status(char flag, const char *summary,
518 struct ref *to, struct ref *from, const char *msg,
519 struct ref_push_report *report,
520 int porcelain, int summary_width)
522 const char *to_name;
524 if (report && report->ref_name)
525 to_name = report->ref_name;
526 else
527 to_name = to->name;
529 if (porcelain) {
530 if (from)
531 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to_name);
532 else
533 fprintf(stdout, "%c\t:%s\t", flag, to_name);
534 if (msg)
535 fprintf(stdout, "%s (%s)\n", summary, msg);
536 else
537 fprintf(stdout, "%s\n", summary);
538 } else {
539 const char *red = "", *reset = "";
540 if (push_had_errors(to)) {
541 red = transport_get_color(TRANSPORT_COLOR_REJECTED);
542 reset = transport_get_color(TRANSPORT_COLOR_RESET);
544 fprintf(stderr, " %s%c %-*s%s ", red, flag, summary_width,
545 summary, reset);
546 if (from)
547 fprintf(stderr, "%s -> %s",
548 prettify_refname(from->name),
549 prettify_refname(to_name));
550 else
551 fputs(prettify_refname(to_name), stderr);
552 if (msg) {
553 fputs(" (", stderr);
554 fputs(msg, stderr);
555 fputc(')', stderr);
557 fputc('\n', stderr);
561 static void print_ok_ref_status(struct ref *ref,
562 struct ref_push_report *report,
563 int porcelain, int summary_width)
565 struct object_id *old_oid;
566 struct object_id *new_oid;
567 const char *ref_name;
568 int forced_update;
570 if (report && report->old_oid)
571 old_oid = report->old_oid;
572 else
573 old_oid = &ref->old_oid;
574 if (report && report->new_oid)
575 new_oid = report->new_oid;
576 else
577 new_oid = &ref->new_oid;
578 if (report && report->forced_update)
579 forced_update = report->forced_update;
580 else
581 forced_update = ref->forced_update;
582 if (report && report->ref_name)
583 ref_name = report->ref_name;
584 else
585 ref_name = ref->name;
587 if (ref->deletion)
588 print_ref_status('-', "[deleted]", ref, NULL, NULL,
589 report, porcelain, summary_width);
590 else if (is_null_oid(old_oid))
591 print_ref_status('*',
592 (starts_with(ref_name, "refs/tags/")
593 ? "[new tag]"
594 : (starts_with(ref_name, "refs/heads/")
595 ? "[new branch]"
596 : "[new reference]")),
597 ref, ref->peer_ref, NULL,
598 report, porcelain, summary_width);
599 else {
600 struct strbuf quickref = STRBUF_INIT;
601 char type;
602 const char *msg;
604 strbuf_add_unique_abbrev(&quickref, old_oid,
605 DEFAULT_ABBREV);
606 if (forced_update) {
607 strbuf_addstr(&quickref, "...");
608 type = '+';
609 msg = "forced update";
610 } else {
611 strbuf_addstr(&quickref, "..");
612 type = ' ';
613 msg = NULL;
615 strbuf_add_unique_abbrev(&quickref, new_oid,
616 DEFAULT_ABBREV);
618 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
619 report, porcelain, summary_width);
620 strbuf_release(&quickref);
624 static int print_one_push_report(struct ref *ref, const char *dest, int count,
625 struct ref_push_report *report,
626 int porcelain, int summary_width)
628 if (!count) {
629 char *url = transport_anonymize_url(dest);
630 fprintf(porcelain ? stdout : stderr, "To %s\n", url);
631 free(url);
634 switch(ref->status) {
635 case REF_STATUS_NONE:
636 print_ref_status('X', "[no match]", ref, NULL, NULL,
637 report, porcelain, summary_width);
638 break;
639 case REF_STATUS_REJECT_NODELETE:
640 print_ref_status('!', "[rejected]", ref, NULL,
641 "remote does not support deleting refs",
642 report, porcelain, summary_width);
643 break;
644 case REF_STATUS_UPTODATE:
645 print_ref_status('=', "[up to date]", ref,
646 ref->peer_ref, NULL,
647 report, porcelain, summary_width);
648 break;
649 case REF_STATUS_REJECT_NONFASTFORWARD:
650 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
651 "non-fast-forward",
652 report, porcelain, summary_width);
653 break;
654 case REF_STATUS_REJECT_ALREADY_EXISTS:
655 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
656 "already exists",
657 report, porcelain, summary_width);
658 break;
659 case REF_STATUS_REJECT_FETCH_FIRST:
660 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
661 "fetch first",
662 report, porcelain, summary_width);
663 break;
664 case REF_STATUS_REJECT_NEEDS_FORCE:
665 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
666 "needs force",
667 report, porcelain, summary_width);
668 break;
669 case REF_STATUS_REJECT_STALE:
670 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
671 "stale info",
672 report, porcelain, summary_width);
673 break;
674 case REF_STATUS_REJECT_REMOTE_UPDATED:
675 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
676 "remote ref updated since checkout",
677 report, porcelain, summary_width);
678 break;
679 case REF_STATUS_REJECT_SHALLOW:
680 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
681 "new shallow roots not allowed",
682 report, porcelain, summary_width);
683 break;
684 case REF_STATUS_REMOTE_REJECT:
685 print_ref_status('!', "[remote rejected]", ref,
686 ref->deletion ? NULL : ref->peer_ref,
687 ref->remote_status,
688 report, porcelain, summary_width);
689 break;
690 case REF_STATUS_EXPECTING_REPORT:
691 print_ref_status('!', "[remote failure]", ref,
692 ref->deletion ? NULL : ref->peer_ref,
693 "remote failed to report status",
694 report, porcelain, summary_width);
695 break;
696 case REF_STATUS_ATOMIC_PUSH_FAILED:
697 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
698 "atomic push failed",
699 report, porcelain, summary_width);
700 break;
701 case REF_STATUS_OK:
702 print_ok_ref_status(ref, report, porcelain, summary_width);
703 break;
706 return 1;
709 static int print_one_push_status(struct ref *ref, const char *dest, int count,
710 int porcelain, int summary_width)
712 struct ref_push_report *report;
713 int n = 0;
715 if (!ref->report)
716 return print_one_push_report(ref, dest, count,
717 NULL, porcelain, summary_width);
719 for (report = ref->report; report; report = report->next)
720 print_one_push_report(ref, dest, count + n++,
721 report, porcelain, summary_width);
722 return n;
725 static int measure_abbrev(const struct object_id *oid, int sofar)
727 char hex[GIT_MAX_HEXSZ + 1];
728 int w = find_unique_abbrev_r(hex, oid, DEFAULT_ABBREV);
730 return (w < sofar) ? sofar : w;
733 int transport_summary_width(const struct ref *refs)
735 int maxw = -1;
737 for (; refs; refs = refs->next) {
738 maxw = measure_abbrev(&refs->old_oid, maxw);
739 maxw = measure_abbrev(&refs->new_oid, maxw);
741 if (maxw < 0)
742 maxw = FALLBACK_DEFAULT_ABBREV;
743 return (2 * maxw + 3);
746 void transport_print_push_status(const char *dest, struct ref *refs,
747 int verbose, int porcelain, unsigned int *reject_reasons)
749 struct ref *ref;
750 int n = 0;
751 char *head;
752 int summary_width = transport_summary_width(refs);
754 if (transport_color_config() < 0)
755 warning(_("could not parse transport.color.* config"));
757 head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
759 if (verbose) {
760 for (ref = refs; ref; ref = ref->next)
761 if (ref->status == REF_STATUS_UPTODATE)
762 n += print_one_push_status(ref, dest, n,
763 porcelain, summary_width);
766 for (ref = refs; ref; ref = ref->next)
767 if (ref->status == REF_STATUS_OK)
768 n += print_one_push_status(ref, dest, n,
769 porcelain, summary_width);
771 *reject_reasons = 0;
772 for (ref = refs; ref; ref = ref->next) {
773 if (ref->status != REF_STATUS_NONE &&
774 ref->status != REF_STATUS_UPTODATE &&
775 ref->status != REF_STATUS_OK)
776 n += print_one_push_status(ref, dest, n,
777 porcelain, summary_width);
778 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
779 if (head != NULL && !strcmp(head, ref->name))
780 *reject_reasons |= REJECT_NON_FF_HEAD;
781 else
782 *reject_reasons |= REJECT_NON_FF_OTHER;
783 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
784 *reject_reasons |= REJECT_ALREADY_EXISTS;
785 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
786 *reject_reasons |= REJECT_FETCH_FIRST;
787 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
788 *reject_reasons |= REJECT_NEEDS_FORCE;
789 } else if (ref->status == REF_STATUS_REJECT_REMOTE_UPDATED) {
790 *reject_reasons |= REJECT_REF_NEEDS_UPDATE;
793 free(head);
796 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
798 struct git_transport_data *data = transport->data;
799 struct send_pack_args args;
800 int ret = 0;
802 if (transport_color_config() < 0)
803 return -1;
805 if (!data->got_remote_heads)
806 get_refs_via_connect(transport, 1, NULL);
808 memset(&args, 0, sizeof(args));
809 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
810 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
811 args.use_thin_pack = data->options.thin;
812 args.verbose = (transport->verbose > 0);
813 args.quiet = (transport->verbose < 0);
814 args.progress = transport->progress;
815 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
816 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
817 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
818 args.push_options = transport->push_options;
819 args.url = transport->url;
821 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
822 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
823 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
824 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
825 else
826 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
828 switch (data->version) {
829 case protocol_v2:
830 die(_("support for protocol v2 not implemented yet"));
831 break;
832 case protocol_v1:
833 case protocol_v0:
834 ret = send_pack(&args, data->fd, data->conn, remote_refs,
835 &data->extra_have);
836 break;
837 case protocol_unknown_version:
838 BUG("unknown protocol version");
841 close(data->fd[1]);
842 close(data->fd[0]);
844 * Atomic push may abort the connection early and close the pipe,
845 * which may cause an error for `finish_connect()`. Ignore this error
846 * for atomic git-push.
848 if (ret || args.atomic)
849 finish_connect(data->conn);
850 else
851 ret = finish_connect(data->conn);
852 data->conn = NULL;
853 data->got_remote_heads = 0;
855 return ret;
858 static int connect_git(struct transport *transport, const char *name,
859 const char *executable, int fd[2])
861 struct git_transport_data *data = transport->data;
862 data->conn = git_connect(data->fd, transport->url,
863 executable, 0);
864 fd[0] = data->fd[0];
865 fd[1] = data->fd[1];
866 return 0;
869 static int disconnect_git(struct transport *transport)
871 struct git_transport_data *data = transport->data;
872 if (data->conn) {
873 if (data->got_remote_heads && !transport->stateless_rpc)
874 packet_flush(data->fd[1]);
875 close(data->fd[0]);
876 if (data->fd[1] >= 0)
877 close(data->fd[1]);
878 finish_connect(data->conn);
881 free(data);
882 return 0;
885 static struct transport_vtable taken_over_vtable = {
886 NULL,
887 get_refs_via_connect,
888 fetch_refs_via_pack,
889 git_transport_push,
890 NULL,
891 disconnect_git
894 void transport_take_over(struct transport *transport,
895 struct child_process *child)
897 struct git_transport_data *data;
899 if (!transport->smart_options)
900 BUG("taking over transport requires non-NULL "
901 "smart_options field.");
903 CALLOC_ARRAY(data, 1);
904 data->options = *transport->smart_options;
905 data->conn = child;
906 data->fd[0] = data->conn->out;
907 data->fd[1] = data->conn->in;
908 data->got_remote_heads = 0;
909 transport->data = data;
911 transport->vtable = &taken_over_vtable;
912 transport->smart_options = &(data->options);
914 transport->cannot_reuse = 1;
917 static int is_file(const char *url)
919 struct stat buf;
920 if (stat(url, &buf))
921 return 0;
922 return S_ISREG(buf.st_mode);
925 static int external_specification_len(const char *url)
927 return strchr(url, ':') - url;
930 static const struct string_list *protocol_whitelist(void)
932 static int enabled = -1;
933 static struct string_list allowed = STRING_LIST_INIT_DUP;
935 if (enabled < 0) {
936 const char *v = getenv("GIT_ALLOW_PROTOCOL");
937 if (v) {
938 string_list_split(&allowed, v, ':', -1);
939 string_list_sort(&allowed);
940 enabled = 1;
941 } else {
942 enabled = 0;
946 return enabled ? &allowed : NULL;
949 enum protocol_allow_config {
950 PROTOCOL_ALLOW_NEVER = 0,
951 PROTOCOL_ALLOW_USER_ONLY,
952 PROTOCOL_ALLOW_ALWAYS
955 static enum protocol_allow_config parse_protocol_config(const char *key,
956 const char *value)
958 if (!strcasecmp(value, "always"))
959 return PROTOCOL_ALLOW_ALWAYS;
960 else if (!strcasecmp(value, "never"))
961 return PROTOCOL_ALLOW_NEVER;
962 else if (!strcasecmp(value, "user"))
963 return PROTOCOL_ALLOW_USER_ONLY;
965 die(_("unknown value for config '%s': %s"), key, value);
968 static enum protocol_allow_config get_protocol_config(const char *type)
970 char *key = xstrfmt("protocol.%s.allow", type);
971 char *value;
973 /* first check the per-protocol config */
974 if (!git_config_get_string(key, &value)) {
975 enum protocol_allow_config ret =
976 parse_protocol_config(key, value);
977 free(key);
978 free(value);
979 return ret;
981 free(key);
983 /* if defined, fallback to user-defined default for unknown protocols */
984 if (!git_config_get_string("protocol.allow", &value)) {
985 enum protocol_allow_config ret =
986 parse_protocol_config("protocol.allow", value);
987 free(value);
988 return ret;
991 /* fallback to built-in defaults */
992 /* known safe */
993 if (!strcmp(type, "http") ||
994 !strcmp(type, "https") ||
995 !strcmp(type, "git") ||
996 !strcmp(type, "ssh") ||
997 !strcmp(type, "file"))
998 return PROTOCOL_ALLOW_ALWAYS;
1000 /* known scary; err on the side of caution */
1001 if (!strcmp(type, "ext"))
1002 return PROTOCOL_ALLOW_NEVER;
1004 /* unknown; by default let them be used only directly by the user */
1005 return PROTOCOL_ALLOW_USER_ONLY;
1008 int is_transport_allowed(const char *type, int from_user)
1010 const struct string_list *whitelist = protocol_whitelist();
1011 if (whitelist)
1012 return string_list_has_string(whitelist, type);
1014 switch (get_protocol_config(type)) {
1015 case PROTOCOL_ALLOW_ALWAYS:
1016 return 1;
1017 case PROTOCOL_ALLOW_NEVER:
1018 return 0;
1019 case PROTOCOL_ALLOW_USER_ONLY:
1020 if (from_user < 0)
1021 from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1022 return from_user;
1025 BUG("invalid protocol_allow_config type");
1028 void transport_check_allowed(const char *type)
1030 if (!is_transport_allowed(type, -1))
1031 die(_("transport '%s' not allowed"), type);
1034 static struct transport_vtable bundle_vtable = {
1035 NULL,
1036 get_refs_from_bundle,
1037 fetch_refs_from_bundle,
1038 NULL,
1039 NULL,
1040 close_bundle
1043 static struct transport_vtable builtin_smart_vtable = {
1044 NULL,
1045 get_refs_via_connect,
1046 fetch_refs_via_pack,
1047 git_transport_push,
1048 connect_git,
1049 disconnect_git
1052 struct transport *transport_get(struct remote *remote, const char *url)
1054 const char *helper;
1055 struct transport *ret = xcalloc(1, sizeof(*ret));
1057 ret->progress = isatty(2);
1058 string_list_init_dup(&ret->pack_lockfiles);
1060 if (!remote)
1061 BUG("No remote provided to transport_get()");
1063 ret->got_remote_refs = 0;
1064 ret->remote = remote;
1065 helper = remote->foreign_vcs;
1067 if (!url && remote->url)
1068 url = remote->url[0];
1069 ret->url = url;
1071 /* maybe it is a foreign URL? */
1072 if (url) {
1073 const char *p = url;
1075 while (is_urlschemechar(p == url, *p))
1076 p++;
1077 if (starts_with(p, "::"))
1078 helper = xstrndup(url, p - url);
1081 if (helper) {
1082 transport_helper_init(ret, helper);
1083 } else if (starts_with(url, "rsync:")) {
1084 die(_("git-over-rsync is no longer supported"));
1085 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
1086 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
1087 bundle_header_init(&data->header);
1088 transport_check_allowed("file");
1089 ret->data = data;
1090 ret->vtable = &bundle_vtable;
1091 ret->smart_options = NULL;
1092 } else if (!is_url(url)
1093 || starts_with(url, "file://")
1094 || starts_with(url, "git://")
1095 || starts_with(url, "ssh://")
1096 || starts_with(url, "git+ssh://") /* deprecated - do not use */
1097 || starts_with(url, "ssh+git://") /* deprecated - do not use */
1100 * These are builtin smart transports; "allowed" transports
1101 * will be checked individually in git_connect.
1103 struct git_transport_data *data = xcalloc(1, sizeof(*data));
1104 ret->data = data;
1105 ret->vtable = &builtin_smart_vtable;
1106 ret->smart_options = &(data->options);
1108 data->conn = NULL;
1109 data->got_remote_heads = 0;
1110 } else {
1111 /* Unknown protocol in URL. Pass to external handler. */
1112 int len = external_specification_len(url);
1113 char *handler = xmemdupz(url, len);
1114 transport_helper_init(ret, handler);
1117 if (ret->smart_options) {
1118 ret->smart_options->thin = 1;
1119 ret->smart_options->uploadpack = "git-upload-pack";
1120 if (remote->uploadpack)
1121 ret->smart_options->uploadpack = remote->uploadpack;
1122 ret->smart_options->receivepack = "git-receive-pack";
1123 if (remote->receivepack)
1124 ret->smart_options->receivepack = remote->receivepack;
1127 ret->hash_algo = &hash_algos[GIT_HASH_SHA1];
1129 return ret;
1132 const struct git_hash_algo *transport_get_hash_algo(struct transport *transport)
1134 return transport->hash_algo;
1137 int transport_set_option(struct transport *transport,
1138 const char *name, const char *value)
1140 int git_reports = 1, protocol_reports = 1;
1142 if (transport->smart_options)
1143 git_reports = set_git_option(transport->smart_options,
1144 name, value);
1146 if (transport->vtable->set_option)
1147 protocol_reports = transport->vtable->set_option(transport,
1148 name, value);
1150 /* If either report is 0, report 0 (success). */
1151 if (!git_reports || !protocol_reports)
1152 return 0;
1153 /* If either reports -1 (invalid value), report -1. */
1154 if ((git_reports == -1) || (protocol_reports == -1))
1155 return -1;
1156 /* Otherwise if both report unknown, report unknown. */
1157 return 1;
1160 void transport_set_verbosity(struct transport *transport, int verbosity,
1161 int force_progress)
1163 if (verbosity >= 1)
1164 transport->verbose = verbosity <= 3 ? verbosity : 3;
1165 if (verbosity < 0)
1166 transport->verbose = -1;
1169 * Rules used to determine whether to report progress (processing aborts
1170 * when a rule is satisfied):
1172 * . Report progress, if force_progress is 1 (ie. --progress).
1173 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1174 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1175 * . Report progress if isatty(2) is 1.
1177 if (force_progress >= 0)
1178 transport->progress = !!force_progress;
1179 else
1180 transport->progress = verbosity >= 0 && isatty(2);
1183 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
1185 int i;
1187 fprintf(stderr, _("The following submodule paths contain changes that can\n"
1188 "not be found on any remote:\n"));
1189 for (i = 0; i < needs_pushing->nr; i++)
1190 fprintf(stderr, " %s\n", needs_pushing->items[i].string);
1191 fprintf(stderr, _("\nPlease try\n\n"
1192 " git push --recurse-submodules=on-demand\n\n"
1193 "or cd to the path and use\n\n"
1194 " git push\n\n"
1195 "to push them to a remote.\n\n"));
1197 string_list_clear(needs_pushing, 0);
1199 die(_("Aborting."));
1202 static int run_pre_push_hook(struct transport *transport,
1203 struct ref *remote_refs)
1205 int ret = 0, x;
1206 struct ref *r;
1207 struct child_process proc = CHILD_PROCESS_INIT;
1208 struct strbuf buf;
1209 const char *argv[4];
1211 if (!(argv[0] = find_hook("pre-push")))
1212 return 0;
1214 argv[1] = transport->remote->name;
1215 argv[2] = transport->url;
1216 argv[3] = NULL;
1218 proc.argv = argv;
1219 proc.in = -1;
1220 proc.trace2_hook_name = "pre-push";
1222 if (start_command(&proc)) {
1223 finish_command(&proc);
1224 return -1;
1227 sigchain_push(SIGPIPE, SIG_IGN);
1229 strbuf_init(&buf, 256);
1231 for (r = remote_refs; r; r = r->next) {
1232 if (!r->peer_ref) continue;
1233 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
1234 if (r->status == REF_STATUS_REJECT_STALE) continue;
1235 if (r->status == REF_STATUS_REJECT_REMOTE_UPDATED) continue;
1236 if (r->status == REF_STATUS_UPTODATE) continue;
1238 strbuf_reset(&buf);
1239 strbuf_addf( &buf, "%s %s %s %s\n",
1240 r->peer_ref->name, oid_to_hex(&r->new_oid),
1241 r->name, oid_to_hex(&r->old_oid));
1243 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
1244 /* We do not mind if a hook does not read all refs. */
1245 if (errno != EPIPE)
1246 ret = -1;
1247 break;
1251 strbuf_release(&buf);
1253 x = close(proc.in);
1254 if (!ret)
1255 ret = x;
1257 sigchain_pop(SIGPIPE);
1259 x = finish_command(&proc);
1260 if (!ret)
1261 ret = x;
1263 return ret;
1266 int transport_push(struct repository *r,
1267 struct transport *transport,
1268 struct refspec *rs, int flags,
1269 unsigned int *reject_reasons)
1271 *reject_reasons = 0;
1273 if (transport_color_config() < 0)
1274 return -1;
1276 if (transport->vtable->push_refs) {
1277 struct ref *remote_refs;
1278 struct ref *local_refs = get_local_heads();
1279 int match_flags = MATCH_REFS_NONE;
1280 int verbose = (transport->verbose > 0);
1281 int quiet = (transport->verbose < 0);
1282 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1283 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1284 int push_ret, ret, err;
1285 struct transport_ls_refs_options transport_options =
1286 TRANSPORT_LS_REFS_OPTIONS_INIT;
1288 if (check_push_refs(local_refs, rs) < 0)
1289 return -1;
1291 refspec_ref_prefixes(rs, &transport_options.ref_prefixes);
1293 trace2_region_enter("transport_push", "get_refs_list", r);
1294 remote_refs = transport->vtable->get_refs_list(transport, 1,
1295 &transport_options);
1296 trace2_region_leave("transport_push", "get_refs_list", r);
1298 strvec_clear(&transport_options.ref_prefixes);
1300 if (flags & TRANSPORT_PUSH_ALL)
1301 match_flags |= MATCH_REFS_ALL;
1302 if (flags & TRANSPORT_PUSH_MIRROR)
1303 match_flags |= MATCH_REFS_MIRROR;
1304 if (flags & TRANSPORT_PUSH_PRUNE)
1305 match_flags |= MATCH_REFS_PRUNE;
1306 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1307 match_flags |= MATCH_REFS_FOLLOW_TAGS;
1309 if (match_push_refs(local_refs, &remote_refs, rs, match_flags))
1310 return -1;
1312 if (transport->smart_options &&
1313 transport->smart_options->cas &&
1314 !is_empty_cas(transport->smart_options->cas))
1315 apply_push_cas(transport->smart_options->cas,
1316 transport->remote, remote_refs);
1318 set_ref_status_for_push(remote_refs,
1319 flags & TRANSPORT_PUSH_MIRROR,
1320 flags & TRANSPORT_PUSH_FORCE);
1322 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1323 if (run_pre_push_hook(transport, remote_refs))
1324 return -1;
1326 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1327 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1328 !is_bare_repository()) {
1329 struct ref *ref = remote_refs;
1330 struct oid_array commits = OID_ARRAY_INIT;
1332 trace2_region_enter("transport_push", "push_submodules", r);
1333 for (; ref; ref = ref->next)
1334 if (!is_null_oid(&ref->new_oid))
1335 oid_array_append(&commits,
1336 &ref->new_oid);
1338 if (!push_unpushed_submodules(r,
1339 &commits,
1340 transport->remote,
1342 transport->push_options,
1343 pretend)) {
1344 oid_array_clear(&commits);
1345 trace2_region_leave("transport_push", "push_submodules", r);
1346 die(_("failed to push all needed submodules"));
1348 oid_array_clear(&commits);
1349 trace2_region_leave("transport_push", "push_submodules", r);
1352 if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1353 ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1354 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1355 !pretend)) && !is_bare_repository()) {
1356 struct ref *ref = remote_refs;
1357 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1358 struct oid_array commits = OID_ARRAY_INIT;
1360 trace2_region_enter("transport_push", "check_submodules", r);
1361 for (; ref; ref = ref->next)
1362 if (!is_null_oid(&ref->new_oid))
1363 oid_array_append(&commits,
1364 &ref->new_oid);
1366 if (find_unpushed_submodules(r,
1367 &commits,
1368 transport->remote->name,
1369 &needs_pushing)) {
1370 oid_array_clear(&commits);
1371 trace2_region_leave("transport_push", "check_submodules", r);
1372 die_with_unpushed_submodules(&needs_pushing);
1374 string_list_clear(&needs_pushing, 0);
1375 oid_array_clear(&commits);
1376 trace2_region_leave("transport_push", "check_submodules", r);
1379 if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY)) {
1380 trace2_region_enter("transport_push", "push_refs", r);
1381 push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1382 trace2_region_leave("transport_push", "push_refs", r);
1383 } else
1384 push_ret = 0;
1385 err = push_had_errors(remote_refs);
1386 ret = push_ret | err;
1388 if (!quiet || err)
1389 transport_print_push_status(transport->url, remote_refs,
1390 verbose | porcelain, porcelain,
1391 reject_reasons);
1393 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1394 set_upstreams(transport, remote_refs, pretend);
1396 if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1397 TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1398 struct ref *ref;
1399 for (ref = remote_refs; ref; ref = ref->next)
1400 transport_update_tracking_ref(transport->remote, ref, verbose);
1403 if (porcelain && !push_ret)
1404 puts("Done");
1405 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1406 fprintf(stderr, "Everything up-to-date\n");
1408 return ret;
1410 return 1;
1413 const struct ref *transport_get_remote_refs(struct transport *transport,
1414 struct transport_ls_refs_options *transport_options)
1416 if (!transport->got_remote_refs) {
1417 transport->remote_refs =
1418 transport->vtable->get_refs_list(transport, 0,
1419 transport_options);
1420 transport->got_remote_refs = 1;
1423 return transport->remote_refs;
1426 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1428 int rc;
1429 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1430 struct ref **heads = NULL;
1431 struct ref *rm;
1433 for (rm = refs; rm; rm = rm->next) {
1434 nr_refs++;
1435 if (rm->peer_ref &&
1436 !is_null_oid(&rm->old_oid) &&
1437 oideq(&rm->peer_ref->old_oid, &rm->old_oid))
1438 continue;
1439 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1440 heads[nr_heads++] = rm;
1443 if (!nr_heads) {
1445 * When deepening of a shallow repository is requested,
1446 * then local and remote refs are likely to still be equal.
1447 * Just feed them all to the fetch method in that case.
1448 * This condition shouldn't be met in a non-deepening fetch
1449 * (see builtin/fetch.c:quickfetch()).
1451 ALLOC_ARRAY(heads, nr_refs);
1452 for (rm = refs; rm; rm = rm->next)
1453 heads[nr_heads++] = rm;
1456 rc = transport->vtable->fetch(transport, nr_heads, heads);
1458 free(heads);
1459 return rc;
1462 void transport_unlock_pack(struct transport *transport)
1464 int i;
1466 for (i = 0; i < transport->pack_lockfiles.nr; i++)
1467 unlink_or_warn(transport->pack_lockfiles.items[i].string);
1468 string_list_clear(&transport->pack_lockfiles, 0);
1471 int transport_connect(struct transport *transport, const char *name,
1472 const char *exec, int fd[2])
1474 if (transport->vtable->connect)
1475 return transport->vtable->connect(transport, name, exec, fd);
1476 else
1477 die(_("operation not supported by protocol"));
1480 int transport_disconnect(struct transport *transport)
1482 int ret = 0;
1483 if (transport->vtable->disconnect)
1484 ret = transport->vtable->disconnect(transport);
1485 if (transport->got_remote_refs)
1486 free_refs((void *)transport->remote_refs);
1487 free(transport);
1488 return ret;
1492 * Strip username (and password) from a URL and return
1493 * it in a newly allocated string.
1495 char *transport_anonymize_url(const char *url)
1497 char *scheme_prefix, *anon_part;
1498 size_t anon_len, prefix_len = 0;
1500 anon_part = strchr(url, '@');
1501 if (url_is_local_not_ssh(url) || !anon_part)
1502 goto literal_copy;
1504 anon_len = strlen(++anon_part);
1505 scheme_prefix = strstr(url, "://");
1506 if (!scheme_prefix) {
1507 if (!strchr(anon_part, ':'))
1508 /* cannot be "me@there:/path/name" */
1509 goto literal_copy;
1510 } else {
1511 const char *cp;
1512 /* make sure scheme is reasonable */
1513 for (cp = url; cp < scheme_prefix; cp++) {
1514 switch (*cp) {
1515 /* RFC 1738 2.1 */
1516 case '+': case '.': case '-':
1517 break; /* ok */
1518 default:
1519 if (isalnum(*cp))
1520 break;
1521 /* it isn't */
1522 goto literal_copy;
1525 /* @ past the first slash does not count */
1526 cp = strchr(scheme_prefix + 3, '/');
1527 if (cp && cp < anon_part)
1528 goto literal_copy;
1529 prefix_len = scheme_prefix - url + 3;
1531 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1532 (int)anon_len, anon_part);
1533 literal_copy:
1534 return xstrdup(url);