Merge branch 'dd/mailinfo-quoted-cr'
[alt-git.git] / transport.c
blob6cf3da19ebd26f7c641000ca1011ce437873319f
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 close(data->fd[1]);
431 if (finish_connect(data->conn))
432 ret = -1;
433 data->conn = NULL;
435 free_refs(refs_tmp);
436 free_refs(refs);
437 return ret;
440 static int push_had_errors(struct ref *ref)
442 for (; ref; ref = ref->next) {
443 switch (ref->status) {
444 case REF_STATUS_NONE:
445 case REF_STATUS_UPTODATE:
446 case REF_STATUS_OK:
447 break;
448 default:
449 return 1;
452 return 0;
455 int transport_refs_pushed(struct ref *ref)
457 for (; ref; ref = ref->next) {
458 switch(ref->status) {
459 case REF_STATUS_NONE:
460 case REF_STATUS_UPTODATE:
461 break;
462 default:
463 return 1;
466 return 0;
469 static void update_one_tracking_ref(struct remote *remote, char *refname,
470 struct object_id *new_oid, int deletion,
471 int verbose)
473 struct refspec_item rs;
475 memset(&rs, 0, sizeof(rs));
476 rs.src = refname;
477 rs.dst = NULL;
479 if (!remote_find_tracking(remote, &rs)) {
480 if (verbose)
481 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
482 if (deletion)
483 delete_ref(NULL, rs.dst, NULL, 0);
484 else
485 update_ref("update by push", rs.dst, new_oid,
486 NULL, 0, 0);
487 free(rs.dst);
491 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
493 char *refname;
494 struct object_id *new_oid;
495 struct ref_push_report *report;
497 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
498 return;
500 report = ref->report;
501 if (!report)
502 update_one_tracking_ref(remote, ref->name, &ref->new_oid,
503 ref->deletion, verbose);
504 else
505 for (; report; report = report->next) {
506 refname = report->ref_name ? (char *)report->ref_name : ref->name;
507 new_oid = report->new_oid ? report->new_oid : &ref->new_oid;
508 update_one_tracking_ref(remote, refname, new_oid,
509 is_null_oid(new_oid), verbose);
513 static void print_ref_status(char flag, const char *summary,
514 struct ref *to, struct ref *from, const char *msg,
515 struct ref_push_report *report,
516 int porcelain, int summary_width)
518 const char *to_name;
520 if (report && report->ref_name)
521 to_name = report->ref_name;
522 else
523 to_name = to->name;
525 if (porcelain) {
526 if (from)
527 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to_name);
528 else
529 fprintf(stdout, "%c\t:%s\t", flag, to_name);
530 if (msg)
531 fprintf(stdout, "%s (%s)\n", summary, msg);
532 else
533 fprintf(stdout, "%s\n", summary);
534 } else {
535 const char *red = "", *reset = "";
536 if (push_had_errors(to)) {
537 red = transport_get_color(TRANSPORT_COLOR_REJECTED);
538 reset = transport_get_color(TRANSPORT_COLOR_RESET);
540 fprintf(stderr, " %s%c %-*s%s ", red, flag, summary_width,
541 summary, reset);
542 if (from)
543 fprintf(stderr, "%s -> %s",
544 prettify_refname(from->name),
545 prettify_refname(to_name));
546 else
547 fputs(prettify_refname(to_name), stderr);
548 if (msg) {
549 fputs(" (", stderr);
550 fputs(msg, stderr);
551 fputc(')', stderr);
553 fputc('\n', stderr);
557 static void print_ok_ref_status(struct ref *ref,
558 struct ref_push_report *report,
559 int porcelain, int summary_width)
561 struct object_id *old_oid;
562 struct object_id *new_oid;
563 const char *ref_name;
564 int forced_update;
566 if (report && report->old_oid)
567 old_oid = report->old_oid;
568 else
569 old_oid = &ref->old_oid;
570 if (report && report->new_oid)
571 new_oid = report->new_oid;
572 else
573 new_oid = &ref->new_oid;
574 if (report && report->forced_update)
575 forced_update = report->forced_update;
576 else
577 forced_update = ref->forced_update;
578 if (report && report->ref_name)
579 ref_name = report->ref_name;
580 else
581 ref_name = ref->name;
583 if (ref->deletion)
584 print_ref_status('-', "[deleted]", ref, NULL, NULL,
585 report, porcelain, summary_width);
586 else if (is_null_oid(old_oid))
587 print_ref_status('*',
588 (starts_with(ref_name, "refs/tags/")
589 ? "[new tag]"
590 : (starts_with(ref_name, "refs/heads/")
591 ? "[new branch]"
592 : "[new reference]")),
593 ref, ref->peer_ref, NULL,
594 report, porcelain, summary_width);
595 else {
596 struct strbuf quickref = STRBUF_INIT;
597 char type;
598 const char *msg;
600 strbuf_add_unique_abbrev(&quickref, old_oid,
601 DEFAULT_ABBREV);
602 if (forced_update) {
603 strbuf_addstr(&quickref, "...");
604 type = '+';
605 msg = "forced update";
606 } else {
607 strbuf_addstr(&quickref, "..");
608 type = ' ';
609 msg = NULL;
611 strbuf_add_unique_abbrev(&quickref, new_oid,
612 DEFAULT_ABBREV);
614 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
615 report, porcelain, summary_width);
616 strbuf_release(&quickref);
620 static int print_one_push_report(struct ref *ref, const char *dest, int count,
621 struct ref_push_report *report,
622 int porcelain, int summary_width)
624 if (!count) {
625 char *url = transport_anonymize_url(dest);
626 fprintf(porcelain ? stdout : stderr, "To %s\n", url);
627 free(url);
630 switch(ref->status) {
631 case REF_STATUS_NONE:
632 print_ref_status('X', "[no match]", ref, NULL, NULL,
633 report, porcelain, summary_width);
634 break;
635 case REF_STATUS_REJECT_NODELETE:
636 print_ref_status('!', "[rejected]", ref, NULL,
637 "remote does not support deleting refs",
638 report, porcelain, summary_width);
639 break;
640 case REF_STATUS_UPTODATE:
641 print_ref_status('=', "[up to date]", ref,
642 ref->peer_ref, NULL,
643 report, porcelain, summary_width);
644 break;
645 case REF_STATUS_REJECT_NONFASTFORWARD:
646 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
647 "non-fast-forward",
648 report, porcelain, summary_width);
649 break;
650 case REF_STATUS_REJECT_ALREADY_EXISTS:
651 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
652 "already exists",
653 report, porcelain, summary_width);
654 break;
655 case REF_STATUS_REJECT_FETCH_FIRST:
656 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
657 "fetch first",
658 report, porcelain, summary_width);
659 break;
660 case REF_STATUS_REJECT_NEEDS_FORCE:
661 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
662 "needs force",
663 report, porcelain, summary_width);
664 break;
665 case REF_STATUS_REJECT_STALE:
666 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
667 "stale info",
668 report, porcelain, summary_width);
669 break;
670 case REF_STATUS_REJECT_REMOTE_UPDATED:
671 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
672 "remote ref updated since checkout",
673 report, porcelain, summary_width);
674 break;
675 case REF_STATUS_REJECT_SHALLOW:
676 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
677 "new shallow roots not allowed",
678 report, porcelain, summary_width);
679 break;
680 case REF_STATUS_REMOTE_REJECT:
681 print_ref_status('!', "[remote rejected]", ref,
682 ref->deletion ? NULL : ref->peer_ref,
683 ref->remote_status,
684 report, porcelain, summary_width);
685 break;
686 case REF_STATUS_EXPECTING_REPORT:
687 print_ref_status('!', "[remote failure]", ref,
688 ref->deletion ? NULL : ref->peer_ref,
689 "remote failed to report status",
690 report, porcelain, summary_width);
691 break;
692 case REF_STATUS_ATOMIC_PUSH_FAILED:
693 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
694 "atomic push failed",
695 report, porcelain, summary_width);
696 break;
697 case REF_STATUS_OK:
698 print_ok_ref_status(ref, report, porcelain, summary_width);
699 break;
702 return 1;
705 static int print_one_push_status(struct ref *ref, const char *dest, int count,
706 int porcelain, int summary_width)
708 struct ref_push_report *report;
709 int n = 0;
711 if (!ref->report)
712 return print_one_push_report(ref, dest, count,
713 NULL, porcelain, summary_width);
715 for (report = ref->report; report; report = report->next)
716 print_one_push_report(ref, dest, count + n++,
717 report, porcelain, summary_width);
718 return n;
721 static int measure_abbrev(const struct object_id *oid, int sofar)
723 char hex[GIT_MAX_HEXSZ + 1];
724 int w = find_unique_abbrev_r(hex, oid, DEFAULT_ABBREV);
726 return (w < sofar) ? sofar : w;
729 int transport_summary_width(const struct ref *refs)
731 int maxw = -1;
733 for (; refs; refs = refs->next) {
734 maxw = measure_abbrev(&refs->old_oid, maxw);
735 maxw = measure_abbrev(&refs->new_oid, maxw);
737 if (maxw < 0)
738 maxw = FALLBACK_DEFAULT_ABBREV;
739 return (2 * maxw + 3);
742 void transport_print_push_status(const char *dest, struct ref *refs,
743 int verbose, int porcelain, unsigned int *reject_reasons)
745 struct ref *ref;
746 int n = 0;
747 char *head;
748 int summary_width = transport_summary_width(refs);
750 if (transport_color_config() < 0)
751 warning(_("could not parse transport.color.* config"));
753 head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
755 if (verbose) {
756 for (ref = refs; ref; ref = ref->next)
757 if (ref->status == REF_STATUS_UPTODATE)
758 n += print_one_push_status(ref, dest, n,
759 porcelain, summary_width);
762 for (ref = refs; ref; ref = ref->next)
763 if (ref->status == REF_STATUS_OK)
764 n += print_one_push_status(ref, dest, n,
765 porcelain, summary_width);
767 *reject_reasons = 0;
768 for (ref = refs; ref; ref = ref->next) {
769 if (ref->status != REF_STATUS_NONE &&
770 ref->status != REF_STATUS_UPTODATE &&
771 ref->status != REF_STATUS_OK)
772 n += print_one_push_status(ref, dest, n,
773 porcelain, summary_width);
774 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
775 if (head != NULL && !strcmp(head, ref->name))
776 *reject_reasons |= REJECT_NON_FF_HEAD;
777 else
778 *reject_reasons |= REJECT_NON_FF_OTHER;
779 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
780 *reject_reasons |= REJECT_ALREADY_EXISTS;
781 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
782 *reject_reasons |= REJECT_FETCH_FIRST;
783 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
784 *reject_reasons |= REJECT_NEEDS_FORCE;
785 } else if (ref->status == REF_STATUS_REJECT_REMOTE_UPDATED) {
786 *reject_reasons |= REJECT_REF_NEEDS_UPDATE;
789 free(head);
792 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
794 struct git_transport_data *data = transport->data;
795 struct send_pack_args args;
796 int ret = 0;
798 if (transport_color_config() < 0)
799 return -1;
801 if (!data->got_remote_heads)
802 get_refs_via_connect(transport, 1, NULL);
804 memset(&args, 0, sizeof(args));
805 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
806 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
807 args.use_thin_pack = data->options.thin;
808 args.verbose = (transport->verbose > 0);
809 args.quiet = (transport->verbose < 0);
810 args.progress = transport->progress;
811 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
812 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
813 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
814 args.push_options = transport->push_options;
815 args.url = transport->url;
817 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
818 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
819 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
820 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
821 else
822 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
824 switch (data->version) {
825 case protocol_v2:
826 die(_("support for protocol v2 not implemented yet"));
827 break;
828 case protocol_v1:
829 case protocol_v0:
830 ret = send_pack(&args, data->fd, data->conn, remote_refs,
831 &data->extra_have);
832 break;
833 case protocol_unknown_version:
834 BUG("unknown protocol version");
837 close(data->fd[1]);
838 close(data->fd[0]);
840 * Atomic push may abort the connection early and close the pipe,
841 * which may cause an error for `finish_connect()`. Ignore this error
842 * for atomic git-push.
844 if (ret || args.atomic)
845 finish_connect(data->conn);
846 else
847 ret = finish_connect(data->conn);
848 data->conn = NULL;
849 data->got_remote_heads = 0;
851 return ret;
854 static int connect_git(struct transport *transport, const char *name,
855 const char *executable, int fd[2])
857 struct git_transport_data *data = transport->data;
858 data->conn = git_connect(data->fd, transport->url,
859 executable, 0);
860 fd[0] = data->fd[0];
861 fd[1] = data->fd[1];
862 return 0;
865 static int disconnect_git(struct transport *transport)
867 struct git_transport_data *data = transport->data;
868 if (data->conn) {
869 if (data->got_remote_heads && !transport->stateless_rpc)
870 packet_flush(data->fd[1]);
871 close(data->fd[0]);
872 close(data->fd[1]);
873 finish_connect(data->conn);
876 free(data);
877 return 0;
880 static struct transport_vtable taken_over_vtable = {
881 NULL,
882 get_refs_via_connect,
883 fetch_refs_via_pack,
884 git_transport_push,
885 NULL,
886 disconnect_git
889 void transport_take_over(struct transport *transport,
890 struct child_process *child)
892 struct git_transport_data *data;
894 if (!transport->smart_options)
895 BUG("taking over transport requires non-NULL "
896 "smart_options field.");
898 CALLOC_ARRAY(data, 1);
899 data->options = *transport->smart_options;
900 data->conn = child;
901 data->fd[0] = data->conn->out;
902 data->fd[1] = data->conn->in;
903 data->got_remote_heads = 0;
904 transport->data = data;
906 transport->vtable = &taken_over_vtable;
907 transport->smart_options = &(data->options);
909 transport->cannot_reuse = 1;
912 static int is_file(const char *url)
914 struct stat buf;
915 if (stat(url, &buf))
916 return 0;
917 return S_ISREG(buf.st_mode);
920 static int external_specification_len(const char *url)
922 return strchr(url, ':') - url;
925 static const struct string_list *protocol_whitelist(void)
927 static int enabled = -1;
928 static struct string_list allowed = STRING_LIST_INIT_DUP;
930 if (enabled < 0) {
931 const char *v = getenv("GIT_ALLOW_PROTOCOL");
932 if (v) {
933 string_list_split(&allowed, v, ':', -1);
934 string_list_sort(&allowed);
935 enabled = 1;
936 } else {
937 enabled = 0;
941 return enabled ? &allowed : NULL;
944 enum protocol_allow_config {
945 PROTOCOL_ALLOW_NEVER = 0,
946 PROTOCOL_ALLOW_USER_ONLY,
947 PROTOCOL_ALLOW_ALWAYS
950 static enum protocol_allow_config parse_protocol_config(const char *key,
951 const char *value)
953 if (!strcasecmp(value, "always"))
954 return PROTOCOL_ALLOW_ALWAYS;
955 else if (!strcasecmp(value, "never"))
956 return PROTOCOL_ALLOW_NEVER;
957 else if (!strcasecmp(value, "user"))
958 return PROTOCOL_ALLOW_USER_ONLY;
960 die(_("unknown value for config '%s': %s"), key, value);
963 static enum protocol_allow_config get_protocol_config(const char *type)
965 char *key = xstrfmt("protocol.%s.allow", type);
966 char *value;
968 /* first check the per-protocol config */
969 if (!git_config_get_string(key, &value)) {
970 enum protocol_allow_config ret =
971 parse_protocol_config(key, value);
972 free(key);
973 free(value);
974 return ret;
976 free(key);
978 /* if defined, fallback to user-defined default for unknown protocols */
979 if (!git_config_get_string("protocol.allow", &value)) {
980 enum protocol_allow_config ret =
981 parse_protocol_config("protocol.allow", value);
982 free(value);
983 return ret;
986 /* fallback to built-in defaults */
987 /* known safe */
988 if (!strcmp(type, "http") ||
989 !strcmp(type, "https") ||
990 !strcmp(type, "git") ||
991 !strcmp(type, "ssh") ||
992 !strcmp(type, "file"))
993 return PROTOCOL_ALLOW_ALWAYS;
995 /* known scary; err on the side of caution */
996 if (!strcmp(type, "ext"))
997 return PROTOCOL_ALLOW_NEVER;
999 /* unknown; by default let them be used only directly by the user */
1000 return PROTOCOL_ALLOW_USER_ONLY;
1003 int is_transport_allowed(const char *type, int from_user)
1005 const struct string_list *whitelist = protocol_whitelist();
1006 if (whitelist)
1007 return string_list_has_string(whitelist, type);
1009 switch (get_protocol_config(type)) {
1010 case PROTOCOL_ALLOW_ALWAYS:
1011 return 1;
1012 case PROTOCOL_ALLOW_NEVER:
1013 return 0;
1014 case PROTOCOL_ALLOW_USER_ONLY:
1015 if (from_user < 0)
1016 from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1017 return from_user;
1020 BUG("invalid protocol_allow_config type");
1023 void transport_check_allowed(const char *type)
1025 if (!is_transport_allowed(type, -1))
1026 die(_("transport '%s' not allowed"), type);
1029 static struct transport_vtable bundle_vtable = {
1030 NULL,
1031 get_refs_from_bundle,
1032 fetch_refs_from_bundle,
1033 NULL,
1034 NULL,
1035 close_bundle
1038 static struct transport_vtable builtin_smart_vtable = {
1039 NULL,
1040 get_refs_via_connect,
1041 fetch_refs_via_pack,
1042 git_transport_push,
1043 connect_git,
1044 disconnect_git
1047 struct transport *transport_get(struct remote *remote, const char *url)
1049 const char *helper;
1050 struct transport *ret = xcalloc(1, sizeof(*ret));
1052 ret->progress = isatty(2);
1053 string_list_init(&ret->pack_lockfiles, 1);
1055 if (!remote)
1056 BUG("No remote provided to transport_get()");
1058 ret->got_remote_refs = 0;
1059 ret->remote = remote;
1060 helper = remote->foreign_vcs;
1062 if (!url && remote->url)
1063 url = remote->url[0];
1064 ret->url = url;
1066 /* maybe it is a foreign URL? */
1067 if (url) {
1068 const char *p = url;
1070 while (is_urlschemechar(p == url, *p))
1071 p++;
1072 if (starts_with(p, "::"))
1073 helper = xstrndup(url, p - url);
1076 if (helper) {
1077 transport_helper_init(ret, helper);
1078 } else if (starts_with(url, "rsync:")) {
1079 die(_("git-over-rsync is no longer supported"));
1080 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
1081 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
1082 transport_check_allowed("file");
1083 ret->data = data;
1084 ret->vtable = &bundle_vtable;
1085 ret->smart_options = NULL;
1086 } else if (!is_url(url)
1087 || starts_with(url, "file://")
1088 || starts_with(url, "git://")
1089 || starts_with(url, "ssh://")
1090 || starts_with(url, "git+ssh://") /* deprecated - do not use */
1091 || starts_with(url, "ssh+git://") /* deprecated - do not use */
1094 * These are builtin smart transports; "allowed" transports
1095 * will be checked individually in git_connect.
1097 struct git_transport_data *data = xcalloc(1, sizeof(*data));
1098 ret->data = data;
1099 ret->vtable = &builtin_smart_vtable;
1100 ret->smart_options = &(data->options);
1102 data->conn = NULL;
1103 data->got_remote_heads = 0;
1104 } else {
1105 /* Unknown protocol in URL. Pass to external handler. */
1106 int len = external_specification_len(url);
1107 char *handler = xmemdupz(url, len);
1108 transport_helper_init(ret, handler);
1111 if (ret->smart_options) {
1112 ret->smart_options->thin = 1;
1113 ret->smart_options->uploadpack = "git-upload-pack";
1114 if (remote->uploadpack)
1115 ret->smart_options->uploadpack = remote->uploadpack;
1116 ret->smart_options->receivepack = "git-receive-pack";
1117 if (remote->receivepack)
1118 ret->smart_options->receivepack = remote->receivepack;
1121 ret->hash_algo = &hash_algos[GIT_HASH_SHA1];
1123 return ret;
1126 const struct git_hash_algo *transport_get_hash_algo(struct transport *transport)
1128 return transport->hash_algo;
1131 int transport_set_option(struct transport *transport,
1132 const char *name, const char *value)
1134 int git_reports = 1, protocol_reports = 1;
1136 if (transport->smart_options)
1137 git_reports = set_git_option(transport->smart_options,
1138 name, value);
1140 if (transport->vtable->set_option)
1141 protocol_reports = transport->vtable->set_option(transport,
1142 name, value);
1144 /* If either report is 0, report 0 (success). */
1145 if (!git_reports || !protocol_reports)
1146 return 0;
1147 /* If either reports -1 (invalid value), report -1. */
1148 if ((git_reports == -1) || (protocol_reports == -1))
1149 return -1;
1150 /* Otherwise if both report unknown, report unknown. */
1151 return 1;
1154 void transport_set_verbosity(struct transport *transport, int verbosity,
1155 int force_progress)
1157 if (verbosity >= 1)
1158 transport->verbose = verbosity <= 3 ? verbosity : 3;
1159 if (verbosity < 0)
1160 transport->verbose = -1;
1163 * Rules used to determine whether to report progress (processing aborts
1164 * when a rule is satisfied):
1166 * . Report progress, if force_progress is 1 (ie. --progress).
1167 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1168 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1169 * . Report progress if isatty(2) is 1.
1171 if (force_progress >= 0)
1172 transport->progress = !!force_progress;
1173 else
1174 transport->progress = verbosity >= 0 && isatty(2);
1177 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
1179 int i;
1181 fprintf(stderr, _("The following submodule paths contain changes that can\n"
1182 "not be found on any remote:\n"));
1183 for (i = 0; i < needs_pushing->nr; i++)
1184 fprintf(stderr, " %s\n", needs_pushing->items[i].string);
1185 fprintf(stderr, _("\nPlease try\n\n"
1186 " git push --recurse-submodules=on-demand\n\n"
1187 "or cd to the path and use\n\n"
1188 " git push\n\n"
1189 "to push them to a remote.\n\n"));
1191 string_list_clear(needs_pushing, 0);
1193 die(_("Aborting."));
1196 static int run_pre_push_hook(struct transport *transport,
1197 struct ref *remote_refs)
1199 int ret = 0, x;
1200 struct ref *r;
1201 struct child_process proc = CHILD_PROCESS_INIT;
1202 struct strbuf buf;
1203 const char *argv[4];
1205 if (!(argv[0] = find_hook("pre-push")))
1206 return 0;
1208 argv[1] = transport->remote->name;
1209 argv[2] = transport->url;
1210 argv[3] = NULL;
1212 proc.argv = argv;
1213 proc.in = -1;
1214 proc.trace2_hook_name = "pre-push";
1216 if (start_command(&proc)) {
1217 finish_command(&proc);
1218 return -1;
1221 sigchain_push(SIGPIPE, SIG_IGN);
1223 strbuf_init(&buf, 256);
1225 for (r = remote_refs; r; r = r->next) {
1226 if (!r->peer_ref) continue;
1227 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
1228 if (r->status == REF_STATUS_REJECT_STALE) continue;
1229 if (r->status == REF_STATUS_REJECT_REMOTE_UPDATED) continue;
1230 if (r->status == REF_STATUS_UPTODATE) continue;
1232 strbuf_reset(&buf);
1233 strbuf_addf( &buf, "%s %s %s %s\n",
1234 r->peer_ref->name, oid_to_hex(&r->new_oid),
1235 r->name, oid_to_hex(&r->old_oid));
1237 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
1238 /* We do not mind if a hook does not read all refs. */
1239 if (errno != EPIPE)
1240 ret = -1;
1241 break;
1245 strbuf_release(&buf);
1247 x = close(proc.in);
1248 if (!ret)
1249 ret = x;
1251 sigchain_pop(SIGPIPE);
1253 x = finish_command(&proc);
1254 if (!ret)
1255 ret = x;
1257 return ret;
1260 int transport_push(struct repository *r,
1261 struct transport *transport,
1262 struct refspec *rs, int flags,
1263 unsigned int *reject_reasons)
1265 *reject_reasons = 0;
1267 if (transport_color_config() < 0)
1268 return -1;
1270 if (transport->vtable->push_refs) {
1271 struct ref *remote_refs;
1272 struct ref *local_refs = get_local_heads();
1273 int match_flags = MATCH_REFS_NONE;
1274 int verbose = (transport->verbose > 0);
1275 int quiet = (transport->verbose < 0);
1276 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1277 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1278 int push_ret, ret, err;
1279 struct transport_ls_refs_options transport_options =
1280 TRANSPORT_LS_REFS_OPTIONS_INIT;
1282 if (check_push_refs(local_refs, rs) < 0)
1283 return -1;
1285 refspec_ref_prefixes(rs, &transport_options.ref_prefixes);
1287 trace2_region_enter("transport_push", "get_refs_list", r);
1288 remote_refs = transport->vtable->get_refs_list(transport, 1,
1289 &transport_options);
1290 trace2_region_leave("transport_push", "get_refs_list", r);
1292 strvec_clear(&transport_options.ref_prefixes);
1294 if (flags & TRANSPORT_PUSH_ALL)
1295 match_flags |= MATCH_REFS_ALL;
1296 if (flags & TRANSPORT_PUSH_MIRROR)
1297 match_flags |= MATCH_REFS_MIRROR;
1298 if (flags & TRANSPORT_PUSH_PRUNE)
1299 match_flags |= MATCH_REFS_PRUNE;
1300 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1301 match_flags |= MATCH_REFS_FOLLOW_TAGS;
1303 if (match_push_refs(local_refs, &remote_refs, rs, match_flags))
1304 return -1;
1306 if (transport->smart_options &&
1307 transport->smart_options->cas &&
1308 !is_empty_cas(transport->smart_options->cas))
1309 apply_push_cas(transport->smart_options->cas,
1310 transport->remote, remote_refs);
1312 set_ref_status_for_push(remote_refs,
1313 flags & TRANSPORT_PUSH_MIRROR,
1314 flags & TRANSPORT_PUSH_FORCE);
1316 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1317 if (run_pre_push_hook(transport, remote_refs))
1318 return -1;
1320 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1321 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1322 !is_bare_repository()) {
1323 struct ref *ref = remote_refs;
1324 struct oid_array commits = OID_ARRAY_INIT;
1326 trace2_region_enter("transport_push", "push_submodules", r);
1327 for (; ref; ref = ref->next)
1328 if (!is_null_oid(&ref->new_oid))
1329 oid_array_append(&commits,
1330 &ref->new_oid);
1332 if (!push_unpushed_submodules(r,
1333 &commits,
1334 transport->remote,
1336 transport->push_options,
1337 pretend)) {
1338 oid_array_clear(&commits);
1339 trace2_region_leave("transport_push", "push_submodules", r);
1340 die(_("failed to push all needed submodules"));
1342 oid_array_clear(&commits);
1343 trace2_region_leave("transport_push", "push_submodules", r);
1346 if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1347 ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1348 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1349 !pretend)) && !is_bare_repository()) {
1350 struct ref *ref = remote_refs;
1351 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1352 struct oid_array commits = OID_ARRAY_INIT;
1354 trace2_region_enter("transport_push", "check_submodules", r);
1355 for (; ref; ref = ref->next)
1356 if (!is_null_oid(&ref->new_oid))
1357 oid_array_append(&commits,
1358 &ref->new_oid);
1360 if (find_unpushed_submodules(r,
1361 &commits,
1362 transport->remote->name,
1363 &needs_pushing)) {
1364 oid_array_clear(&commits);
1365 trace2_region_leave("transport_push", "check_submodules", r);
1366 die_with_unpushed_submodules(&needs_pushing);
1368 string_list_clear(&needs_pushing, 0);
1369 oid_array_clear(&commits);
1370 trace2_region_leave("transport_push", "check_submodules", r);
1373 if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY)) {
1374 trace2_region_enter("transport_push", "push_refs", r);
1375 push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1376 trace2_region_leave("transport_push", "push_refs", r);
1377 } else
1378 push_ret = 0;
1379 err = push_had_errors(remote_refs);
1380 ret = push_ret | err;
1382 if (!quiet || err)
1383 transport_print_push_status(transport->url, remote_refs,
1384 verbose | porcelain, porcelain,
1385 reject_reasons);
1387 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1388 set_upstreams(transport, remote_refs, pretend);
1390 if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1391 TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1392 struct ref *ref;
1393 for (ref = remote_refs; ref; ref = ref->next)
1394 transport_update_tracking_ref(transport->remote, ref, verbose);
1397 if (porcelain && !push_ret)
1398 puts("Done");
1399 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1400 fprintf(stderr, "Everything up-to-date\n");
1402 return ret;
1404 return 1;
1407 const struct ref *transport_get_remote_refs(struct transport *transport,
1408 struct transport_ls_refs_options *transport_options)
1410 if (!transport->got_remote_refs) {
1411 transport->remote_refs =
1412 transport->vtable->get_refs_list(transport, 0,
1413 transport_options);
1414 transport->got_remote_refs = 1;
1417 return transport->remote_refs;
1420 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1422 int rc;
1423 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1424 struct ref **heads = NULL;
1425 struct ref *rm;
1427 for (rm = refs; rm; rm = rm->next) {
1428 nr_refs++;
1429 if (rm->peer_ref &&
1430 !is_null_oid(&rm->old_oid) &&
1431 oideq(&rm->peer_ref->old_oid, &rm->old_oid))
1432 continue;
1433 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1434 heads[nr_heads++] = rm;
1437 if (!nr_heads) {
1439 * When deepening of a shallow repository is requested,
1440 * then local and remote refs are likely to still be equal.
1441 * Just feed them all to the fetch method in that case.
1442 * This condition shouldn't be met in a non-deepening fetch
1443 * (see builtin/fetch.c:quickfetch()).
1445 ALLOC_ARRAY(heads, nr_refs);
1446 for (rm = refs; rm; rm = rm->next)
1447 heads[nr_heads++] = rm;
1450 rc = transport->vtable->fetch(transport, nr_heads, heads);
1452 free(heads);
1453 return rc;
1456 void transport_unlock_pack(struct transport *transport)
1458 int i;
1460 for (i = 0; i < transport->pack_lockfiles.nr; i++)
1461 unlink_or_warn(transport->pack_lockfiles.items[i].string);
1462 string_list_clear(&transport->pack_lockfiles, 0);
1465 int transport_connect(struct transport *transport, const char *name,
1466 const char *exec, int fd[2])
1468 if (transport->vtable->connect)
1469 return transport->vtable->connect(transport, name, exec, fd);
1470 else
1471 die(_("operation not supported by protocol"));
1474 int transport_disconnect(struct transport *transport)
1476 int ret = 0;
1477 if (transport->vtable->disconnect)
1478 ret = transport->vtable->disconnect(transport);
1479 if (transport->got_remote_refs)
1480 free_refs((void *)transport->remote_refs);
1481 free(transport);
1482 return ret;
1486 * Strip username (and password) from a URL and return
1487 * it in a newly allocated string.
1489 char *transport_anonymize_url(const char *url)
1491 char *scheme_prefix, *anon_part;
1492 size_t anon_len, prefix_len = 0;
1494 anon_part = strchr(url, '@');
1495 if (url_is_local_not_ssh(url) || !anon_part)
1496 goto literal_copy;
1498 anon_len = strlen(++anon_part);
1499 scheme_prefix = strstr(url, "://");
1500 if (!scheme_prefix) {
1501 if (!strchr(anon_part, ':'))
1502 /* cannot be "me@there:/path/name" */
1503 goto literal_copy;
1504 } else {
1505 const char *cp;
1506 /* make sure scheme is reasonable */
1507 for (cp = url; cp < scheme_prefix; cp++) {
1508 switch (*cp) {
1509 /* RFC 1738 2.1 */
1510 case '+': case '.': case '-':
1511 break; /* ok */
1512 default:
1513 if (isalnum(*cp))
1514 break;
1515 /* it isn't */
1516 goto literal_copy;
1519 /* @ past the first slash does not count */
1520 cp = strchr(scheme_prefix + 3, '/');
1521 if (cp && cp < anon_part)
1522 goto literal_copy;
1523 prefix_len = scheme_prefix - url + 3;
1525 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1526 (int)anon_len, anon_part);
1527 literal_copy:
1528 return xstrdup(url);