Merge branch 'fw/complete-cmd-idx-fix'
[git.git] / transport.c
blob50f5830eb6b98e955c5306843bfa38384c2c865a
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 ref_list_entry *e = data->header.references.list + i;
151 struct ref *ref = alloc_ref(e->name);
152 oidcpy(&ref->old_oid, &e->oid);
153 ref->next = result;
154 result = ref;
156 return result;
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;
163 int ret;
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;
170 return ret;
173 static int close_bundle(struct transport *transport)
175 struct bundle_transport_data *data = transport->data;
176 if (data->fd > 0)
177 close(data->fd);
178 free(data);
179 return 0;
182 struct git_transport_data {
183 struct git_transport_options options;
184 struct child_process *conn;
185 int fd[2];
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;
197 return 0;
198 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
199 opts->receivepack = value;
200 return 0;
201 } else if (!strcmp(name, TRANS_OPT_THIN)) {
202 opts->thin = !!value;
203 return 0;
204 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
205 opts->followtags = !!value;
206 return 0;
207 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
208 opts->keep = !!value;
209 return 0;
210 } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
211 opts->update_shallow = !!value;
212 return 0;
213 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
214 if (!value)
215 opts->depth = 0;
216 else {
217 char *end;
218 opts->depth = strtol(value, &end, 0);
219 if (*end)
220 die(_("transport: invalid depth option '%s'"), value);
222 return 0;
223 } else if (!strcmp(name, TRANS_OPT_DEEPEN_SINCE)) {
224 opts->deepen_since = value;
225 return 0;
226 } else if (!strcmp(name, TRANS_OPT_DEEPEN_NOT)) {
227 opts->deepen_not = (const struct string_list *)value;
228 return 0;
229 } else if (!strcmp(name, TRANS_OPT_DEEPEN_RELATIVE)) {
230 opts->deepen_relative = !!value;
231 return 0;
232 } else if (!strcmp(name, TRANS_OPT_FROM_PROMISOR)) {
233 opts->from_promisor = !!value;
234 return 0;
235 } else if (!strcmp(name, TRANS_OPT_LIST_OBJECTS_FILTER)) {
236 list_objects_filter_die_if_populated(&opts->filter_options);
237 parse_list_objects_filter(&opts->filter_options, value);
238 return 0;
239 } else if (!strcmp(name, TRANS_OPT_REJECT_SHALLOW)) {
240 opts->reject_shallow = !!value;
241 return 0;
243 return 1;
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;
251 if (data->conn)
252 return 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,
263 flags);
265 return 0;
268 static void die_if_server_options(struct transport *transport)
270 if (!transport->server_options || !transport->server_options->nr)
271 return;
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
283 * remote refs.
285 static struct ref *handshake(struct transport *transport, int for_push,
286 struct transport_ls_refs_options *options,
287 int must_list_refs)
289 struct git_transport_data *data = transport->data;
290 struct ref *refs = NULL;
291 struct packet_reader reader;
292 int sid_len;
293 const char *server_sid;
295 connect_setup(transport, for_push);
297 packet_reader_init(&reader, data->fd[0], NULL, 0,
298 PACKET_READ_CHOMP_NEWLINE |
299 PACKET_READ_GENTLE_ON_EOF |
300 PACKET_READ_DIE_ON_ERR_PACKET);
302 data->version = discover_version(&reader);
303 switch (data->version) {
304 case protocol_v2:
305 if (server_feature_v2("session-id", &server_sid))
306 trace2_data_string("transfer", NULL, "server-sid", server_sid);
307 if (must_list_refs)
308 get_remote_refs(data->fd[1], &reader, &refs, for_push,
309 options,
310 transport->server_options,
311 transport->stateless_rpc);
312 break;
313 case protocol_v1:
314 case protocol_v0:
315 die_if_server_options(transport);
316 get_remote_heads(&reader, &refs,
317 for_push ? REF_NORMAL : 0,
318 &data->extra_have,
319 &data->shallow);
320 server_sid = server_feature_value("session-id", &sid_len);
321 if (server_sid) {
322 char *sid = xstrndup(server_sid, sid_len);
323 trace2_data_string("transfer", NULL, "server-sid", sid);
324 free(sid);
326 break;
327 case protocol_unknown_version:
328 BUG("unknown protocol version");
330 data->got_remote_heads = 1;
331 transport->hash_algo = reader.hash_algo;
333 if (reader.line_peeked)
334 BUG("buffer must be empty at the end of handshake()");
336 return refs;
339 static struct ref *get_refs_via_connect(struct transport *transport, int for_push,
340 struct transport_ls_refs_options *options)
342 return handshake(transport, for_push, options, 1);
345 static int fetch_refs_via_pack(struct transport *transport,
346 int nr_heads, struct ref **to_fetch)
348 int ret = 0;
349 struct git_transport_data *data = transport->data;
350 struct ref *refs = NULL;
351 struct fetch_pack_args args;
352 struct ref *refs_tmp = NULL;
354 memset(&args, 0, sizeof(args));
355 args.uploadpack = data->options.uploadpack;
356 args.keep_pack = data->options.keep;
357 args.lock_pack = 1;
358 args.use_thin_pack = data->options.thin;
359 args.include_tag = data->options.followtags;
360 args.verbose = (transport->verbose > 1);
361 args.quiet = (transport->verbose < 0);
362 args.no_progress = !transport->progress;
363 args.depth = data->options.depth;
364 args.deepen_since = data->options.deepen_since;
365 args.deepen_not = data->options.deepen_not;
366 args.deepen_relative = data->options.deepen_relative;
367 args.check_self_contained_and_connected =
368 data->options.check_self_contained_and_connected;
369 args.cloning = transport->cloning;
370 args.update_shallow = data->options.update_shallow;
371 args.from_promisor = data->options.from_promisor;
372 args.filter_options = data->options.filter_options;
373 args.stateless_rpc = transport->stateless_rpc;
374 args.server_options = transport->server_options;
375 args.negotiation_tips = data->options.negotiation_tips;
376 args.reject_shallow_remote = transport->smart_options->reject_shallow;
378 if (!data->got_remote_heads) {
379 int i;
380 int must_list_refs = 0;
381 for (i = 0; i < nr_heads; i++) {
382 if (!to_fetch[i]->exact_oid) {
383 must_list_refs = 1;
384 break;
387 refs_tmp = handshake(transport, 0, NULL, must_list_refs);
390 if (data->version == protocol_unknown_version)
391 BUG("unknown protocol version");
392 else if (data->version <= protocol_v1)
393 die_if_server_options(transport);
395 if (data->options.acked_commits) {
396 if (data->version < protocol_v2) {
397 warning(_("--negotiate-only requires protocol v2"));
398 ret = -1;
399 } else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
400 warning(_("server does not support wait-for-done"));
401 ret = -1;
402 } else {
403 negotiate_using_fetch(data->options.negotiation_tips,
404 transport->server_options,
405 transport->stateless_rpc,
406 data->fd,
407 data->options.acked_commits);
408 ret = 0;
410 goto cleanup;
413 refs = fetch_pack(&args, data->fd,
414 refs_tmp ? refs_tmp : transport->remote_refs,
415 to_fetch, nr_heads, &data->shallow,
416 &transport->pack_lockfiles, data->version);
418 data->got_remote_heads = 0;
419 data->options.self_contained_and_connected =
420 args.self_contained_and_connected;
421 data->options.connectivity_checked = args.connectivity_checked;
423 if (refs == NULL)
424 ret = -1;
425 if (report_unmatched_refs(to_fetch, nr_heads))
426 ret = -1;
428 cleanup:
429 close(data->fd[0]);
430 if (data->fd[1] >= 0)
431 close(data->fd[1]);
432 if (finish_connect(data->conn))
433 ret = -1;
434 data->conn = NULL;
436 free_refs(refs_tmp);
437 free_refs(refs);
438 return ret;
441 static int push_had_errors(struct ref *ref)
443 for (; ref; ref = ref->next) {
444 switch (ref->status) {
445 case REF_STATUS_NONE:
446 case REF_STATUS_UPTODATE:
447 case REF_STATUS_OK:
448 break;
449 default:
450 return 1;
453 return 0;
456 int transport_refs_pushed(struct ref *ref)
458 for (; ref; ref = ref->next) {
459 switch(ref->status) {
460 case REF_STATUS_NONE:
461 case REF_STATUS_UPTODATE:
462 break;
463 default:
464 return 1;
467 return 0;
470 static void update_one_tracking_ref(struct remote *remote, char *refname,
471 struct object_id *new_oid, int deletion,
472 int verbose)
474 struct refspec_item rs;
476 memset(&rs, 0, sizeof(rs));
477 rs.src = refname;
478 rs.dst = NULL;
480 if (!remote_find_tracking(remote, &rs)) {
481 if (verbose)
482 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
483 if (deletion)
484 delete_ref(NULL, rs.dst, NULL, 0);
485 else
486 update_ref("update by push", rs.dst, new_oid,
487 NULL, 0, 0);
488 free(rs.dst);
492 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
494 char *refname;
495 struct object_id *new_oid;
496 struct ref_push_report *report;
498 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
499 return;
501 report = ref->report;
502 if (!report)
503 update_one_tracking_ref(remote, ref->name, &ref->new_oid,
504 ref->deletion, verbose);
505 else
506 for (; report; report = report->next) {
507 refname = report->ref_name ? (char *)report->ref_name : ref->name;
508 new_oid = report->new_oid ? report->new_oid : &ref->new_oid;
509 update_one_tracking_ref(remote, refname, new_oid,
510 is_null_oid(new_oid), verbose);
514 static void print_ref_status(char flag, const char *summary,
515 struct ref *to, struct ref *from, const char *msg,
516 struct ref_push_report *report,
517 int porcelain, int summary_width)
519 const char *to_name;
521 if (report && report->ref_name)
522 to_name = report->ref_name;
523 else
524 to_name = to->name;
526 if (porcelain) {
527 if (from)
528 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to_name);
529 else
530 fprintf(stdout, "%c\t:%s\t", flag, to_name);
531 if (msg)
532 fprintf(stdout, "%s (%s)\n", summary, msg);
533 else
534 fprintf(stdout, "%s\n", summary);
535 } else {
536 const char *red = "", *reset = "";
537 if (push_had_errors(to)) {
538 red = transport_get_color(TRANSPORT_COLOR_REJECTED);
539 reset = transport_get_color(TRANSPORT_COLOR_RESET);
541 fprintf(stderr, " %s%c %-*s%s ", red, flag, summary_width,
542 summary, reset);
543 if (from)
544 fprintf(stderr, "%s -> %s",
545 prettify_refname(from->name),
546 prettify_refname(to_name));
547 else
548 fputs(prettify_refname(to_name), stderr);
549 if (msg) {
550 fputs(" (", stderr);
551 fputs(msg, stderr);
552 fputc(')', stderr);
554 fputc('\n', stderr);
558 static void print_ok_ref_status(struct ref *ref,
559 struct ref_push_report *report,
560 int porcelain, int summary_width)
562 struct object_id *old_oid;
563 struct object_id *new_oid;
564 const char *ref_name;
565 int forced_update;
567 if (report && report->old_oid)
568 old_oid = report->old_oid;
569 else
570 old_oid = &ref->old_oid;
571 if (report && report->new_oid)
572 new_oid = report->new_oid;
573 else
574 new_oid = &ref->new_oid;
575 if (report && report->forced_update)
576 forced_update = report->forced_update;
577 else
578 forced_update = ref->forced_update;
579 if (report && report->ref_name)
580 ref_name = report->ref_name;
581 else
582 ref_name = ref->name;
584 if (ref->deletion)
585 print_ref_status('-', "[deleted]", ref, NULL, NULL,
586 report, porcelain, summary_width);
587 else if (is_null_oid(old_oid))
588 print_ref_status('*',
589 (starts_with(ref_name, "refs/tags/")
590 ? "[new tag]"
591 : (starts_with(ref_name, "refs/heads/")
592 ? "[new branch]"
593 : "[new reference]")),
594 ref, ref->peer_ref, NULL,
595 report, porcelain, summary_width);
596 else {
597 struct strbuf quickref = STRBUF_INIT;
598 char type;
599 const char *msg;
601 strbuf_add_unique_abbrev(&quickref, old_oid,
602 DEFAULT_ABBREV);
603 if (forced_update) {
604 strbuf_addstr(&quickref, "...");
605 type = '+';
606 msg = "forced update";
607 } else {
608 strbuf_addstr(&quickref, "..");
609 type = ' ';
610 msg = NULL;
612 strbuf_add_unique_abbrev(&quickref, new_oid,
613 DEFAULT_ABBREV);
615 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
616 report, porcelain, summary_width);
617 strbuf_release(&quickref);
621 static int print_one_push_report(struct ref *ref, const char *dest, int count,
622 struct ref_push_report *report,
623 int porcelain, int summary_width)
625 if (!count) {
626 char *url = transport_anonymize_url(dest);
627 fprintf(porcelain ? stdout : stderr, "To %s\n", url);
628 free(url);
631 switch(ref->status) {
632 case REF_STATUS_NONE:
633 print_ref_status('X', "[no match]", ref, NULL, NULL,
634 report, porcelain, summary_width);
635 break;
636 case REF_STATUS_REJECT_NODELETE:
637 print_ref_status('!', "[rejected]", ref, NULL,
638 "remote does not support deleting refs",
639 report, porcelain, summary_width);
640 break;
641 case REF_STATUS_UPTODATE:
642 print_ref_status('=', "[up to date]", ref,
643 ref->peer_ref, NULL,
644 report, porcelain, summary_width);
645 break;
646 case REF_STATUS_REJECT_NONFASTFORWARD:
647 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
648 "non-fast-forward",
649 report, porcelain, summary_width);
650 break;
651 case REF_STATUS_REJECT_ALREADY_EXISTS:
652 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
653 "already exists",
654 report, porcelain, summary_width);
655 break;
656 case REF_STATUS_REJECT_FETCH_FIRST:
657 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
658 "fetch first",
659 report, porcelain, summary_width);
660 break;
661 case REF_STATUS_REJECT_NEEDS_FORCE:
662 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
663 "needs force",
664 report, porcelain, summary_width);
665 break;
666 case REF_STATUS_REJECT_STALE:
667 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
668 "stale info",
669 report, porcelain, summary_width);
670 break;
671 case REF_STATUS_REJECT_REMOTE_UPDATED:
672 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
673 "remote ref updated since checkout",
674 report, porcelain, summary_width);
675 break;
676 case REF_STATUS_REJECT_SHALLOW:
677 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
678 "new shallow roots not allowed",
679 report, porcelain, summary_width);
680 break;
681 case REF_STATUS_REMOTE_REJECT:
682 print_ref_status('!', "[remote rejected]", ref,
683 ref->deletion ? NULL : ref->peer_ref,
684 ref->remote_status,
685 report, porcelain, summary_width);
686 break;
687 case REF_STATUS_EXPECTING_REPORT:
688 print_ref_status('!', "[remote failure]", ref,
689 ref->deletion ? NULL : ref->peer_ref,
690 "remote failed to report status",
691 report, porcelain, summary_width);
692 break;
693 case REF_STATUS_ATOMIC_PUSH_FAILED:
694 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
695 "atomic push failed",
696 report, porcelain, summary_width);
697 break;
698 case REF_STATUS_OK:
699 print_ok_ref_status(ref, report, porcelain, summary_width);
700 break;
703 return 1;
706 static int print_one_push_status(struct ref *ref, const char *dest, int count,
707 int porcelain, int summary_width)
709 struct ref_push_report *report;
710 int n = 0;
712 if (!ref->report)
713 return print_one_push_report(ref, dest, count,
714 NULL, porcelain, summary_width);
716 for (report = ref->report; report; report = report->next)
717 print_one_push_report(ref, dest, count + n++,
718 report, porcelain, summary_width);
719 return n;
722 static int measure_abbrev(const struct object_id *oid, int sofar)
724 char hex[GIT_MAX_HEXSZ + 1];
725 int w = find_unique_abbrev_r(hex, oid, DEFAULT_ABBREV);
727 return (w < sofar) ? sofar : w;
730 int transport_summary_width(const struct ref *refs)
732 int maxw = -1;
734 for (; refs; refs = refs->next) {
735 maxw = measure_abbrev(&refs->old_oid, maxw);
736 maxw = measure_abbrev(&refs->new_oid, maxw);
738 if (maxw < 0)
739 maxw = FALLBACK_DEFAULT_ABBREV;
740 return (2 * maxw + 3);
743 void transport_print_push_status(const char *dest, struct ref *refs,
744 int verbose, int porcelain, unsigned int *reject_reasons)
746 struct ref *ref;
747 int n = 0;
748 char *head;
749 int summary_width = transport_summary_width(refs);
751 if (transport_color_config() < 0)
752 warning(_("could not parse transport.color.* config"));
754 head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
756 if (verbose) {
757 for (ref = refs; ref; ref = ref->next)
758 if (ref->status == REF_STATUS_UPTODATE)
759 n += print_one_push_status(ref, dest, n,
760 porcelain, summary_width);
763 for (ref = refs; ref; ref = ref->next)
764 if (ref->status == REF_STATUS_OK)
765 n += print_one_push_status(ref, dest, n,
766 porcelain, summary_width);
768 *reject_reasons = 0;
769 for (ref = refs; ref; ref = ref->next) {
770 if (ref->status != REF_STATUS_NONE &&
771 ref->status != REF_STATUS_UPTODATE &&
772 ref->status != REF_STATUS_OK)
773 n += print_one_push_status(ref, dest, n,
774 porcelain, summary_width);
775 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
776 if (head != NULL && !strcmp(head, ref->name))
777 *reject_reasons |= REJECT_NON_FF_HEAD;
778 else
779 *reject_reasons |= REJECT_NON_FF_OTHER;
780 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
781 *reject_reasons |= REJECT_ALREADY_EXISTS;
782 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
783 *reject_reasons |= REJECT_FETCH_FIRST;
784 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
785 *reject_reasons |= REJECT_NEEDS_FORCE;
786 } else if (ref->status == REF_STATUS_REJECT_REMOTE_UPDATED) {
787 *reject_reasons |= REJECT_REF_NEEDS_UPDATE;
790 free(head);
793 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
795 struct git_transport_data *data = transport->data;
796 struct send_pack_args args;
797 int ret = 0;
799 if (transport_color_config() < 0)
800 return -1;
802 if (!data->got_remote_heads)
803 get_refs_via_connect(transport, 1, NULL);
805 memset(&args, 0, sizeof(args));
806 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
807 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
808 args.use_thin_pack = data->options.thin;
809 args.verbose = (transport->verbose > 0);
810 args.quiet = (transport->verbose < 0);
811 args.progress = transport->progress;
812 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
813 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
814 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
815 args.push_options = transport->push_options;
816 args.url = transport->url;
818 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
819 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
820 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
821 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
822 else
823 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
825 switch (data->version) {
826 case protocol_v2:
827 die(_("support for protocol v2 not implemented yet"));
828 break;
829 case protocol_v1:
830 case protocol_v0:
831 ret = send_pack(&args, data->fd, data->conn, remote_refs,
832 &data->extra_have);
833 break;
834 case protocol_unknown_version:
835 BUG("unknown protocol version");
838 close(data->fd[1]);
839 close(data->fd[0]);
841 * Atomic push may abort the connection early and close the pipe,
842 * which may cause an error for `finish_connect()`. Ignore this error
843 * for atomic git-push.
845 if (ret || args.atomic)
846 finish_connect(data->conn);
847 else
848 ret = finish_connect(data->conn);
849 data->conn = NULL;
850 data->got_remote_heads = 0;
852 return ret;
855 static int connect_git(struct transport *transport, const char *name,
856 const char *executable, int fd[2])
858 struct git_transport_data *data = transport->data;
859 data->conn = git_connect(data->fd, transport->url,
860 executable, 0);
861 fd[0] = data->fd[0];
862 fd[1] = data->fd[1];
863 return 0;
866 static int disconnect_git(struct transport *transport)
868 struct git_transport_data *data = transport->data;
869 if (data->conn) {
870 if (data->got_remote_heads && !transport->stateless_rpc)
871 packet_flush(data->fd[1]);
872 close(data->fd[0]);
873 if (data->fd[1] >= 0)
874 close(data->fd[1]);
875 finish_connect(data->conn);
878 free(data);
879 return 0;
882 static struct transport_vtable taken_over_vtable = {
883 NULL,
884 get_refs_via_connect,
885 fetch_refs_via_pack,
886 git_transport_push,
887 NULL,
888 disconnect_git
891 void transport_take_over(struct transport *transport,
892 struct child_process *child)
894 struct git_transport_data *data;
896 if (!transport->smart_options)
897 BUG("taking over transport requires non-NULL "
898 "smart_options field.");
900 CALLOC_ARRAY(data, 1);
901 data->options = *transport->smart_options;
902 data->conn = child;
903 data->fd[0] = data->conn->out;
904 data->fd[1] = data->conn->in;
905 data->got_remote_heads = 0;
906 transport->data = data;
908 transport->vtable = &taken_over_vtable;
909 transport->smart_options = &(data->options);
911 transport->cannot_reuse = 1;
914 static int is_file(const char *url)
916 struct stat buf;
917 if (stat(url, &buf))
918 return 0;
919 return S_ISREG(buf.st_mode);
922 static int external_specification_len(const char *url)
924 return strchr(url, ':') - url;
927 static const struct string_list *protocol_whitelist(void)
929 static int enabled = -1;
930 static struct string_list allowed = STRING_LIST_INIT_DUP;
932 if (enabled < 0) {
933 const char *v = getenv("GIT_ALLOW_PROTOCOL");
934 if (v) {
935 string_list_split(&allowed, v, ':', -1);
936 string_list_sort(&allowed);
937 enabled = 1;
938 } else {
939 enabled = 0;
943 return enabled ? &allowed : NULL;
946 enum protocol_allow_config {
947 PROTOCOL_ALLOW_NEVER = 0,
948 PROTOCOL_ALLOW_USER_ONLY,
949 PROTOCOL_ALLOW_ALWAYS
952 static enum protocol_allow_config parse_protocol_config(const char *key,
953 const char *value)
955 if (!strcasecmp(value, "always"))
956 return PROTOCOL_ALLOW_ALWAYS;
957 else if (!strcasecmp(value, "never"))
958 return PROTOCOL_ALLOW_NEVER;
959 else if (!strcasecmp(value, "user"))
960 return PROTOCOL_ALLOW_USER_ONLY;
962 die(_("unknown value for config '%s': %s"), key, value);
965 static enum protocol_allow_config get_protocol_config(const char *type)
967 char *key = xstrfmt("protocol.%s.allow", type);
968 char *value;
970 /* first check the per-protocol config */
971 if (!git_config_get_string(key, &value)) {
972 enum protocol_allow_config ret =
973 parse_protocol_config(key, value);
974 free(key);
975 free(value);
976 return ret;
978 free(key);
980 /* if defined, fallback to user-defined default for unknown protocols */
981 if (!git_config_get_string("protocol.allow", &value)) {
982 enum protocol_allow_config ret =
983 parse_protocol_config("protocol.allow", value);
984 free(value);
985 return ret;
988 /* fallback to built-in defaults */
989 /* known safe */
990 if (!strcmp(type, "http") ||
991 !strcmp(type, "https") ||
992 !strcmp(type, "git") ||
993 !strcmp(type, "ssh") ||
994 !strcmp(type, "file"))
995 return PROTOCOL_ALLOW_ALWAYS;
997 /* known scary; err on the side of caution */
998 if (!strcmp(type, "ext"))
999 return PROTOCOL_ALLOW_NEVER;
1001 /* unknown; by default let them be used only directly by the user */
1002 return PROTOCOL_ALLOW_USER_ONLY;
1005 int is_transport_allowed(const char *type, int from_user)
1007 const struct string_list *whitelist = protocol_whitelist();
1008 if (whitelist)
1009 return string_list_has_string(whitelist, type);
1011 switch (get_protocol_config(type)) {
1012 case PROTOCOL_ALLOW_ALWAYS:
1013 return 1;
1014 case PROTOCOL_ALLOW_NEVER:
1015 return 0;
1016 case PROTOCOL_ALLOW_USER_ONLY:
1017 if (from_user < 0)
1018 from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1019 return from_user;
1022 BUG("invalid protocol_allow_config type");
1025 void transport_check_allowed(const char *type)
1027 if (!is_transport_allowed(type, -1))
1028 die(_("transport '%s' not allowed"), type);
1031 static struct transport_vtable bundle_vtable = {
1032 NULL,
1033 get_refs_from_bundle,
1034 fetch_refs_from_bundle,
1035 NULL,
1036 NULL,
1037 close_bundle
1040 static struct transport_vtable builtin_smart_vtable = {
1041 NULL,
1042 get_refs_via_connect,
1043 fetch_refs_via_pack,
1044 git_transport_push,
1045 connect_git,
1046 disconnect_git
1049 struct transport *transport_get(struct remote *remote, const char *url)
1051 const char *helper;
1052 struct transport *ret = xcalloc(1, sizeof(*ret));
1054 ret->progress = isatty(2);
1055 string_list_init(&ret->pack_lockfiles, 1);
1057 if (!remote)
1058 BUG("No remote provided to transport_get()");
1060 ret->got_remote_refs = 0;
1061 ret->remote = remote;
1062 helper = remote->foreign_vcs;
1064 if (!url && remote->url)
1065 url = remote->url[0];
1066 ret->url = url;
1068 /* maybe it is a foreign URL? */
1069 if (url) {
1070 const char *p = url;
1072 while (is_urlschemechar(p == url, *p))
1073 p++;
1074 if (starts_with(p, "::"))
1075 helper = xstrndup(url, p - url);
1078 if (helper) {
1079 transport_helper_init(ret, helper);
1080 } else if (starts_with(url, "rsync:")) {
1081 die(_("git-over-rsync is no longer supported"));
1082 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
1083 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
1084 transport_check_allowed("file");
1085 ret->data = data;
1086 ret->vtable = &bundle_vtable;
1087 ret->smart_options = NULL;
1088 } else if (!is_url(url)
1089 || starts_with(url, "file://")
1090 || starts_with(url, "git://")
1091 || starts_with(url, "ssh://")
1092 || starts_with(url, "git+ssh://") /* deprecated - do not use */
1093 || starts_with(url, "ssh+git://") /* deprecated - do not use */
1096 * These are builtin smart transports; "allowed" transports
1097 * will be checked individually in git_connect.
1099 struct git_transport_data *data = xcalloc(1, sizeof(*data));
1100 ret->data = data;
1101 ret->vtable = &builtin_smart_vtable;
1102 ret->smart_options = &(data->options);
1104 data->conn = NULL;
1105 data->got_remote_heads = 0;
1106 } else {
1107 /* Unknown protocol in URL. Pass to external handler. */
1108 int len = external_specification_len(url);
1109 char *handler = xmemdupz(url, len);
1110 transport_helper_init(ret, handler);
1113 if (ret->smart_options) {
1114 ret->smart_options->thin = 1;
1115 ret->smart_options->uploadpack = "git-upload-pack";
1116 if (remote->uploadpack)
1117 ret->smart_options->uploadpack = remote->uploadpack;
1118 ret->smart_options->receivepack = "git-receive-pack";
1119 if (remote->receivepack)
1120 ret->smart_options->receivepack = remote->receivepack;
1123 ret->hash_algo = &hash_algos[GIT_HASH_SHA1];
1125 return ret;
1128 const struct git_hash_algo *transport_get_hash_algo(struct transport *transport)
1130 return transport->hash_algo;
1133 int transport_set_option(struct transport *transport,
1134 const char *name, const char *value)
1136 int git_reports = 1, protocol_reports = 1;
1138 if (transport->smart_options)
1139 git_reports = set_git_option(transport->smart_options,
1140 name, value);
1142 if (transport->vtable->set_option)
1143 protocol_reports = transport->vtable->set_option(transport,
1144 name, value);
1146 /* If either report is 0, report 0 (success). */
1147 if (!git_reports || !protocol_reports)
1148 return 0;
1149 /* If either reports -1 (invalid value), report -1. */
1150 if ((git_reports == -1) || (protocol_reports == -1))
1151 return -1;
1152 /* Otherwise if both report unknown, report unknown. */
1153 return 1;
1156 void transport_set_verbosity(struct transport *transport, int verbosity,
1157 int force_progress)
1159 if (verbosity >= 1)
1160 transport->verbose = verbosity <= 3 ? verbosity : 3;
1161 if (verbosity < 0)
1162 transport->verbose = -1;
1165 * Rules used to determine whether to report progress (processing aborts
1166 * when a rule is satisfied):
1168 * . Report progress, if force_progress is 1 (ie. --progress).
1169 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1170 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1171 * . Report progress if isatty(2) is 1.
1173 if (force_progress >= 0)
1174 transport->progress = !!force_progress;
1175 else
1176 transport->progress = verbosity >= 0 && isatty(2);
1179 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
1181 int i;
1183 fprintf(stderr, _("The following submodule paths contain changes that can\n"
1184 "not be found on any remote:\n"));
1185 for (i = 0; i < needs_pushing->nr; i++)
1186 fprintf(stderr, " %s\n", needs_pushing->items[i].string);
1187 fprintf(stderr, _("\nPlease try\n\n"
1188 " git push --recurse-submodules=on-demand\n\n"
1189 "or cd to the path and use\n\n"
1190 " git push\n\n"
1191 "to push them to a remote.\n\n"));
1193 string_list_clear(needs_pushing, 0);
1195 die(_("Aborting."));
1198 static int run_pre_push_hook(struct transport *transport,
1199 struct ref *remote_refs)
1201 int ret = 0, x;
1202 struct ref *r;
1203 struct child_process proc = CHILD_PROCESS_INIT;
1204 struct strbuf buf;
1205 const char *argv[4];
1207 if (!(argv[0] = find_hook("pre-push")))
1208 return 0;
1210 argv[1] = transport->remote->name;
1211 argv[2] = transport->url;
1212 argv[3] = NULL;
1214 proc.argv = argv;
1215 proc.in = -1;
1216 proc.trace2_hook_name = "pre-push";
1218 if (start_command(&proc)) {
1219 finish_command(&proc);
1220 return -1;
1223 sigchain_push(SIGPIPE, SIG_IGN);
1225 strbuf_init(&buf, 256);
1227 for (r = remote_refs; r; r = r->next) {
1228 if (!r->peer_ref) continue;
1229 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
1230 if (r->status == REF_STATUS_REJECT_STALE) continue;
1231 if (r->status == REF_STATUS_REJECT_REMOTE_UPDATED) continue;
1232 if (r->status == REF_STATUS_UPTODATE) continue;
1234 strbuf_reset(&buf);
1235 strbuf_addf( &buf, "%s %s %s %s\n",
1236 r->peer_ref->name, oid_to_hex(&r->new_oid),
1237 r->name, oid_to_hex(&r->old_oid));
1239 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
1240 /* We do not mind if a hook does not read all refs. */
1241 if (errno != EPIPE)
1242 ret = -1;
1243 break;
1247 strbuf_release(&buf);
1249 x = close(proc.in);
1250 if (!ret)
1251 ret = x;
1253 sigchain_pop(SIGPIPE);
1255 x = finish_command(&proc);
1256 if (!ret)
1257 ret = x;
1259 return ret;
1262 int transport_push(struct repository *r,
1263 struct transport *transport,
1264 struct refspec *rs, int flags,
1265 unsigned int *reject_reasons)
1267 *reject_reasons = 0;
1269 if (transport_color_config() < 0)
1270 return -1;
1272 if (transport->vtable->push_refs) {
1273 struct ref *remote_refs;
1274 struct ref *local_refs = get_local_heads();
1275 int match_flags = MATCH_REFS_NONE;
1276 int verbose = (transport->verbose > 0);
1277 int quiet = (transport->verbose < 0);
1278 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1279 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1280 int push_ret, ret, err;
1281 struct transport_ls_refs_options transport_options =
1282 TRANSPORT_LS_REFS_OPTIONS_INIT;
1284 if (check_push_refs(local_refs, rs) < 0)
1285 return -1;
1287 refspec_ref_prefixes(rs, &transport_options.ref_prefixes);
1289 trace2_region_enter("transport_push", "get_refs_list", r);
1290 remote_refs = transport->vtable->get_refs_list(transport, 1,
1291 &transport_options);
1292 trace2_region_leave("transport_push", "get_refs_list", r);
1294 strvec_clear(&transport_options.ref_prefixes);
1296 if (flags & TRANSPORT_PUSH_ALL)
1297 match_flags |= MATCH_REFS_ALL;
1298 if (flags & TRANSPORT_PUSH_MIRROR)
1299 match_flags |= MATCH_REFS_MIRROR;
1300 if (flags & TRANSPORT_PUSH_PRUNE)
1301 match_flags |= MATCH_REFS_PRUNE;
1302 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1303 match_flags |= MATCH_REFS_FOLLOW_TAGS;
1305 if (match_push_refs(local_refs, &remote_refs, rs, match_flags))
1306 return -1;
1308 if (transport->smart_options &&
1309 transport->smart_options->cas &&
1310 !is_empty_cas(transport->smart_options->cas))
1311 apply_push_cas(transport->smart_options->cas,
1312 transport->remote, remote_refs);
1314 set_ref_status_for_push(remote_refs,
1315 flags & TRANSPORT_PUSH_MIRROR,
1316 flags & TRANSPORT_PUSH_FORCE);
1318 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1319 if (run_pre_push_hook(transport, remote_refs))
1320 return -1;
1322 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1323 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1324 !is_bare_repository()) {
1325 struct ref *ref = remote_refs;
1326 struct oid_array commits = OID_ARRAY_INIT;
1328 trace2_region_enter("transport_push", "push_submodules", r);
1329 for (; ref; ref = ref->next)
1330 if (!is_null_oid(&ref->new_oid))
1331 oid_array_append(&commits,
1332 &ref->new_oid);
1334 if (!push_unpushed_submodules(r,
1335 &commits,
1336 transport->remote,
1338 transport->push_options,
1339 pretend)) {
1340 oid_array_clear(&commits);
1341 trace2_region_leave("transport_push", "push_submodules", r);
1342 die(_("failed to push all needed submodules"));
1344 oid_array_clear(&commits);
1345 trace2_region_leave("transport_push", "push_submodules", r);
1348 if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1349 ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1350 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1351 !pretend)) && !is_bare_repository()) {
1352 struct ref *ref = remote_refs;
1353 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1354 struct oid_array commits = OID_ARRAY_INIT;
1356 trace2_region_enter("transport_push", "check_submodules", r);
1357 for (; ref; ref = ref->next)
1358 if (!is_null_oid(&ref->new_oid))
1359 oid_array_append(&commits,
1360 &ref->new_oid);
1362 if (find_unpushed_submodules(r,
1363 &commits,
1364 transport->remote->name,
1365 &needs_pushing)) {
1366 oid_array_clear(&commits);
1367 trace2_region_leave("transport_push", "check_submodules", r);
1368 die_with_unpushed_submodules(&needs_pushing);
1370 string_list_clear(&needs_pushing, 0);
1371 oid_array_clear(&commits);
1372 trace2_region_leave("transport_push", "check_submodules", r);
1375 if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY)) {
1376 trace2_region_enter("transport_push", "push_refs", r);
1377 push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1378 trace2_region_leave("transport_push", "push_refs", r);
1379 } else
1380 push_ret = 0;
1381 err = push_had_errors(remote_refs);
1382 ret = push_ret | err;
1384 if (!quiet || err)
1385 transport_print_push_status(transport->url, remote_refs,
1386 verbose | porcelain, porcelain,
1387 reject_reasons);
1389 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1390 set_upstreams(transport, remote_refs, pretend);
1392 if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1393 TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1394 struct ref *ref;
1395 for (ref = remote_refs; ref; ref = ref->next)
1396 transport_update_tracking_ref(transport->remote, ref, verbose);
1399 if (porcelain && !push_ret)
1400 puts("Done");
1401 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1402 fprintf(stderr, "Everything up-to-date\n");
1404 return ret;
1406 return 1;
1409 const struct ref *transport_get_remote_refs(struct transport *transport,
1410 struct transport_ls_refs_options *transport_options)
1412 if (!transport->got_remote_refs) {
1413 transport->remote_refs =
1414 transport->vtable->get_refs_list(transport, 0,
1415 transport_options);
1416 transport->got_remote_refs = 1;
1419 return transport->remote_refs;
1422 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1424 int rc;
1425 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1426 struct ref **heads = NULL;
1427 struct ref *rm;
1429 for (rm = refs; rm; rm = rm->next) {
1430 nr_refs++;
1431 if (rm->peer_ref &&
1432 !is_null_oid(&rm->old_oid) &&
1433 oideq(&rm->peer_ref->old_oid, &rm->old_oid))
1434 continue;
1435 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1436 heads[nr_heads++] = rm;
1439 if (!nr_heads) {
1441 * When deepening of a shallow repository is requested,
1442 * then local and remote refs are likely to still be equal.
1443 * Just feed them all to the fetch method in that case.
1444 * This condition shouldn't be met in a non-deepening fetch
1445 * (see builtin/fetch.c:quickfetch()).
1447 ALLOC_ARRAY(heads, nr_refs);
1448 for (rm = refs; rm; rm = rm->next)
1449 heads[nr_heads++] = rm;
1452 rc = transport->vtable->fetch(transport, nr_heads, heads);
1454 free(heads);
1455 return rc;
1458 void transport_unlock_pack(struct transport *transport)
1460 int i;
1462 for (i = 0; i < transport->pack_lockfiles.nr; i++)
1463 unlink_or_warn(transport->pack_lockfiles.items[i].string);
1464 string_list_clear(&transport->pack_lockfiles, 0);
1467 int transport_connect(struct transport *transport, const char *name,
1468 const char *exec, int fd[2])
1470 if (transport->vtable->connect)
1471 return transport->vtable->connect(transport, name, exec, fd);
1472 else
1473 die(_("operation not supported by protocol"));
1476 int transport_disconnect(struct transport *transport)
1478 int ret = 0;
1479 if (transport->vtable->disconnect)
1480 ret = transport->vtable->disconnect(transport);
1481 if (transport->got_remote_refs)
1482 free_refs((void *)transport->remote_refs);
1483 free(transport);
1484 return ret;
1488 * Strip username (and password) from a URL and return
1489 * it in a newly allocated string.
1491 char *transport_anonymize_url(const char *url)
1493 char *scheme_prefix, *anon_part;
1494 size_t anon_len, prefix_len = 0;
1496 anon_part = strchr(url, '@');
1497 if (url_is_local_not_ssh(url) || !anon_part)
1498 goto literal_copy;
1500 anon_len = strlen(++anon_part);
1501 scheme_prefix = strstr(url, "://");
1502 if (!scheme_prefix) {
1503 if (!strchr(anon_part, ':'))
1504 /* cannot be "me@there:/path/name" */
1505 goto literal_copy;
1506 } else {
1507 const char *cp;
1508 /* make sure scheme is reasonable */
1509 for (cp = url; cp < scheme_prefix; cp++) {
1510 switch (*cp) {
1511 /* RFC 1738 2.1 */
1512 case '+': case '.': case '-':
1513 break; /* ok */
1514 default:
1515 if (isalnum(*cp))
1516 break;
1517 /* it isn't */
1518 goto literal_copy;
1521 /* @ past the first slash does not count */
1522 cp = strchr(scheme_prefix + 3, '/');
1523 if (cp && cp < anon_part)
1524 goto literal_copy;
1525 prefix_len = scheme_prefix - url + 3;
1527 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1528 (int)anon_len, anon_part);
1529 literal_copy:
1530 return xstrdup(url);