transport: not report a non-head push as a branch
[git] / transport.c
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"
25
26 static int transport_use_color = -1;
27 static char transport_colors[][COLOR_MAXLEN] = {
28         GIT_COLOR_RESET,
29         GIT_COLOR_RED           /* REJECTED */
30 };
31
32 enum color_transport {
33         TRANSPORT_COLOR_RESET = 0,
34         TRANSPORT_COLOR_REJECTED = 1
35 };
36
37 static int transport_color_config(void)
38 {
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;
46
47         if (initialized)
48                 return 0;
49         initialized = 1;
50
51         if (!git_config_get_string(key, &value))
52                 transport_use_color = git_config_colorbool(key, value);
53
54         if (!want_color_stderr(transport_use_color))
55                 return 0;
56
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;
63                 }
64
65         return 0;
66 }
67
68 static const char *transport_get_color(enum color_transport ix)
69 {
70         if (want_color_stderr(transport_use_color))
71                 return transport_colors[ix];
72         return "";
73 }
74
75 static void set_upstreams(struct transport *transport, struct ref *refs,
76         int pretend)
77 {
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;
84                 /*
85                  * Check suitability for tracking. Must be successful /
86                  * already up-to-date ref create/modify (not delete).
87                  */
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;
95
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;
104
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;
110
111                 if (!pretend)
112                         install_branch_config(BRANCH_CONFIG_VERBOSE,
113                                 localname + 11, transport->remote->name,
114                                 remotename);
115                 else
116                         printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
117                                 localname + 11, remotename + 11,
118                                 transport->remote->name);
119         }
120 }
121
122 struct bundle_transport_data {
123         int fd;
124         struct bundle_header header;
125         unsigned get_refs_from_bundle_called : 1;
126 };
127
128 static struct ref *get_refs_from_bundle(struct transport *transport,
129                                         int for_push,
130                                         const struct argv_array *ref_prefixes)
131 {
132         struct bundle_transport_data *data = transport->data;
133         struct ref *result = NULL;
134         int i;
135
136         if (for_push)
137                 return NULL;
138
139         data->get_refs_from_bundle_called = 1;
140
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);
146         for (i = 0; i < data->header.references.nr; i++) {
147                 struct ref_list_entry *e = data->header.references.list + i;
148                 struct ref *ref = alloc_ref(e->name);
149                 oidcpy(&ref->old_oid, &e->oid);
150                 ref->next = result;
151                 result = ref;
152         }
153         return result;
154 }
155
156 static int fetch_refs_from_bundle(struct transport *transport,
157                                int nr_heads, struct ref **to_fetch)
158 {
159         struct bundle_transport_data *data = transport->data;
160
161         if (!data->get_refs_from_bundle_called)
162                 get_refs_from_bundle(transport, 0, NULL);
163         return unbundle(the_repository, &data->header, data->fd,
164                         transport->progress ? BUNDLE_VERBOSE : 0);
165 }
166
167 static int close_bundle(struct transport *transport)
168 {
169         struct bundle_transport_data *data = transport->data;
170         if (data->fd > 0)
171                 close(data->fd);
172         free(data);
173         return 0;
174 }
175
176 struct git_transport_data {
177         struct git_transport_options options;
178         struct child_process *conn;
179         int fd[2];
180         unsigned got_remote_heads : 1;
181         enum protocol_version version;
182         struct oid_array extra_have;
183         struct oid_array shallow;
184 };
185
186 static int set_git_option(struct git_transport_options *opts,
187                           const char *name, const char *value)
188 {
189         if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
190                 opts->uploadpack = value;
191                 return 0;
192         } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
193                 opts->receivepack = value;
194                 return 0;
195         } else if (!strcmp(name, TRANS_OPT_THIN)) {
196                 opts->thin = !!value;
197                 return 0;
198         } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
199                 opts->followtags = !!value;
200                 return 0;
201         } else if (!strcmp(name, TRANS_OPT_KEEP)) {
202                 opts->keep = !!value;
203                 return 0;
204         } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
205                 opts->update_shallow = !!value;
206                 return 0;
207         } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
208                 if (!value)
209                         opts->depth = 0;
210                 else {
211                         char *end;
212                         opts->depth = strtol(value, &end, 0);
213                         if (*end)
214                                 die(_("transport: invalid depth option '%s'"), value);
215                 }
216                 return 0;
217         } else if (!strcmp(name, TRANS_OPT_DEEPEN_SINCE)) {
218                 opts->deepen_since = value;
219                 return 0;
220         } else if (!strcmp(name, TRANS_OPT_DEEPEN_NOT)) {
221                 opts->deepen_not = (const struct string_list *)value;
222                 return 0;
223         } else if (!strcmp(name, TRANS_OPT_DEEPEN_RELATIVE)) {
224                 opts->deepen_relative = !!value;
225                 return 0;
226         } else if (!strcmp(name, TRANS_OPT_FROM_PROMISOR)) {
227                 opts->from_promisor = !!value;
228                 return 0;
229         } else if (!strcmp(name, TRANS_OPT_NO_DEPENDENTS)) {
230                 opts->no_dependents = !!value;
231                 return 0;
232         } else if (!strcmp(name, TRANS_OPT_LIST_OBJECTS_FILTER)) {
233                 list_objects_filter_die_if_populated(&opts->filter_options);
234                 parse_list_objects_filter(&opts->filter_options, value);
235                 return 0;
236         }
237         return 1;
238 }
239
240 static int connect_setup(struct transport *transport, int for_push)
241 {
242         struct git_transport_data *data = transport->data;
243         int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
244
245         if (data->conn)
246                 return 0;
247
248         switch (transport->family) {
249         case TRANSPORT_FAMILY_ALL: break;
250         case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
251         case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
252         }
253
254         data->conn = git_connect(data->fd, transport->url,
255                                  for_push ? data->options.receivepack :
256                                  data->options.uploadpack,
257                                  flags);
258
259         return 0;
260 }
261
262 static void die_if_server_options(struct transport *transport)
263 {
264         if (!transport->server_options || !transport->server_options->nr)
265                 return;
266         advise(_("see protocol.version in 'git help config' for more details"));
267         die(_("server options require protocol version 2 or later"));
268 }
269
270 /*
271  * Obtains the protocol version from the transport and writes it to
272  * transport->data->version, first connecting if not already connected.
273  *
274  * If the protocol version is one that allows skipping the listing of remote
275  * refs, and must_list_refs is 0, the listing of remote refs is skipped and
276  * this function returns NULL. Otherwise, this function returns the list of
277  * remote refs.
278  */
279 static struct ref *handshake(struct transport *transport, int for_push,
280                              const struct argv_array *ref_prefixes,
281                              int must_list_refs)
282 {
283         struct git_transport_data *data = transport->data;
284         struct ref *refs = NULL;
285         struct packet_reader reader;
286
287         connect_setup(transport, for_push);
288
289         packet_reader_init(&reader, data->fd[0], NULL, 0,
290                            PACKET_READ_CHOMP_NEWLINE |
291                            PACKET_READ_GENTLE_ON_EOF |
292                            PACKET_READ_DIE_ON_ERR_PACKET);
293
294         data->version = discover_version(&reader);
295         switch (data->version) {
296         case protocol_v2:
297                 if (must_list_refs)
298                         get_remote_refs(data->fd[1], &reader, &refs, for_push,
299                                         ref_prefixes,
300                                         transport->server_options);
301                 break;
302         case protocol_v1:
303         case protocol_v0:
304                 die_if_server_options(transport);
305                 get_remote_heads(&reader, &refs,
306                                  for_push ? REF_NORMAL : 0,
307                                  &data->extra_have,
308                                  &data->shallow);
309                 break;
310         case protocol_unknown_version:
311                 BUG("unknown protocol version");
312         }
313         data->got_remote_heads = 1;
314
315         if (reader.line_peeked)
316                 BUG("buffer must be empty at the end of handshake()");
317
318         return refs;
319 }
320
321 static struct ref *get_refs_via_connect(struct transport *transport, int for_push,
322                                         const struct argv_array *ref_prefixes)
323 {
324         return handshake(transport, for_push, ref_prefixes, 1);
325 }
326
327 static int fetch_refs_via_pack(struct transport *transport,
328                                int nr_heads, struct ref **to_fetch)
329 {
330         int ret = 0;
331         struct git_transport_data *data = transport->data;
332         struct ref *refs = NULL;
333         struct fetch_pack_args args;
334         struct ref *refs_tmp = NULL;
335
336         memset(&args, 0, sizeof(args));
337         args.uploadpack = data->options.uploadpack;
338         args.keep_pack = data->options.keep;
339         args.lock_pack = 1;
340         args.use_thin_pack = data->options.thin;
341         args.include_tag = data->options.followtags;
342         args.verbose = (transport->verbose > 1);
343         args.quiet = (transport->verbose < 0);
344         args.no_progress = !transport->progress;
345         args.depth = data->options.depth;
346         args.deepen_since = data->options.deepen_since;
347         args.deepen_not = data->options.deepen_not;
348         args.deepen_relative = data->options.deepen_relative;
349         args.check_self_contained_and_connected =
350                 data->options.check_self_contained_and_connected;
351         args.cloning = transport->cloning;
352         args.update_shallow = data->options.update_shallow;
353         args.from_promisor = data->options.from_promisor;
354         args.no_dependents = data->options.no_dependents;
355         args.filter_options = data->options.filter_options;
356         args.stateless_rpc = transport->stateless_rpc;
357         args.server_options = transport->server_options;
358         args.negotiation_tips = data->options.negotiation_tips;
359
360         if (!data->got_remote_heads) {
361                 int i;
362                 int must_list_refs = 0;
363                 for (i = 0; i < nr_heads; i++) {
364                         if (!to_fetch[i]->exact_oid) {
365                                 must_list_refs = 1;
366                                 break;
367                         }
368                 }
369                 refs_tmp = handshake(transport, 0, NULL, must_list_refs);
370         }
371
372         switch (data->version) {
373         case protocol_v2:
374                 refs = fetch_pack(&args, data->fd,
375                                   refs_tmp ? refs_tmp : transport->remote_refs,
376                                   to_fetch, nr_heads, &data->shallow,
377                                   &transport->pack_lockfile, data->version);
378                 break;
379         case protocol_v1:
380         case protocol_v0:
381                 die_if_server_options(transport);
382                 refs = fetch_pack(&args, data->fd,
383                                   refs_tmp ? refs_tmp : transport->remote_refs,
384                                   to_fetch, nr_heads, &data->shallow,
385                                   &transport->pack_lockfile, data->version);
386                 break;
387         case protocol_unknown_version:
388                 BUG("unknown protocol version");
389         }
390
391         close(data->fd[0]);
392         close(data->fd[1]);
393         if (finish_connect(data->conn))
394                 ret = -1;
395         data->conn = NULL;
396         data->got_remote_heads = 0;
397         data->options.self_contained_and_connected =
398                 args.self_contained_and_connected;
399         data->options.connectivity_checked = args.connectivity_checked;
400
401         if (refs == NULL)
402                 ret = -1;
403         if (report_unmatched_refs(to_fetch, nr_heads))
404                 ret = -1;
405
406         free_refs(refs_tmp);
407         free_refs(refs);
408         return ret;
409 }
410
411 static int push_had_errors(struct ref *ref)
412 {
413         for (; ref; ref = ref->next) {
414                 switch (ref->status) {
415                 case REF_STATUS_NONE:
416                 case REF_STATUS_UPTODATE:
417                 case REF_STATUS_OK:
418                         break;
419                 default:
420                         return 1;
421                 }
422         }
423         return 0;
424 }
425
426 int transport_refs_pushed(struct ref *ref)
427 {
428         for (; ref; ref = ref->next) {
429                 switch(ref->status) {
430                 case REF_STATUS_NONE:
431                 case REF_STATUS_UPTODATE:
432                         break;
433                 default:
434                         return 1;
435                 }
436         }
437         return 0;
438 }
439
440 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
441 {
442         struct refspec_item rs;
443
444         if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
445                 return;
446
447         rs.src = ref->name;
448         rs.dst = NULL;
449
450         if (!remote_find_tracking(remote, &rs)) {
451                 if (verbose)
452                         fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
453                 if (ref->deletion) {
454                         delete_ref(NULL, rs.dst, NULL, 0);
455                 } else
456                         update_ref("update by push", rs.dst, &ref->new_oid,
457                                    NULL, 0, 0);
458                 free(rs.dst);
459         }
460 }
461
462 static void print_ref_status(char flag, const char *summary,
463                              struct ref *to, struct ref *from, const char *msg,
464                              int porcelain, int summary_width)
465 {
466         if (porcelain) {
467                 if (from)
468                         fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
469                 else
470                         fprintf(stdout, "%c\t:%s\t", flag, to->name);
471                 if (msg)
472                         fprintf(stdout, "%s (%s)\n", summary, msg);
473                 else
474                         fprintf(stdout, "%s\n", summary);
475         } else {
476                 const char *red = "", *reset = "";
477                 if (push_had_errors(to)) {
478                         red = transport_get_color(TRANSPORT_COLOR_REJECTED);
479                         reset = transport_get_color(TRANSPORT_COLOR_RESET);
480                 }
481                 fprintf(stderr, " %s%c %-*s%s ", red, flag, summary_width,
482                         summary, reset);
483                 if (from)
484                         fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
485                 else
486                         fputs(prettify_refname(to->name), stderr);
487                 if (msg) {
488                         fputs(" (", stderr);
489                         fputs(msg, stderr);
490                         fputc(')', stderr);
491                 }
492                 fputc('\n', stderr);
493         }
494 }
495
496 static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_width)
497 {
498         if (ref->deletion)
499                 print_ref_status('-', "[deleted]", ref, NULL, NULL,
500                                  porcelain, summary_width);
501         else if (is_null_oid(&ref->old_oid))
502                 print_ref_status('*',
503                                  (starts_with(ref->name, "refs/tags/")
504                                   ? "[new tag]"
505                                   : (starts_with(ref->name, "refs/heads/")
506                                      ? "[new branch]"
507                                      : "[new reference]")),
508                                  ref, ref->peer_ref, NULL, porcelain, summary_width);
509         else {
510                 struct strbuf quickref = STRBUF_INIT;
511                 char type;
512                 const char *msg;
513
514                 strbuf_add_unique_abbrev(&quickref, &ref->old_oid,
515                                          DEFAULT_ABBREV);
516                 if (ref->forced_update) {
517                         strbuf_addstr(&quickref, "...");
518                         type = '+';
519                         msg = "forced update";
520                 } else {
521                         strbuf_addstr(&quickref, "..");
522                         type = ' ';
523                         msg = NULL;
524                 }
525                 strbuf_add_unique_abbrev(&quickref, &ref->new_oid,
526                                          DEFAULT_ABBREV);
527
528                 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
529                                  porcelain, summary_width);
530                 strbuf_release(&quickref);
531         }
532 }
533
534 static int print_one_push_status(struct ref *ref, const char *dest, int count,
535                                  int porcelain, int summary_width)
536 {
537         if (!count) {
538                 char *url = transport_anonymize_url(dest);
539                 fprintf(porcelain ? stdout : stderr, "To %s\n", url);
540                 free(url);
541         }
542
543         switch(ref->status) {
544         case REF_STATUS_NONE:
545                 print_ref_status('X', "[no match]", ref, NULL, NULL,
546                                  porcelain, summary_width);
547                 break;
548         case REF_STATUS_REJECT_NODELETE:
549                 print_ref_status('!', "[rejected]", ref, NULL,
550                                  "remote does not support deleting refs",
551                                  porcelain, summary_width);
552                 break;
553         case REF_STATUS_UPTODATE:
554                 print_ref_status('=', "[up to date]", ref,
555                                  ref->peer_ref, NULL, porcelain, summary_width);
556                 break;
557         case REF_STATUS_REJECT_NONFASTFORWARD:
558                 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
559                                  "non-fast-forward", porcelain, summary_width);
560                 break;
561         case REF_STATUS_REJECT_ALREADY_EXISTS:
562                 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
563                                  "already exists", porcelain, summary_width);
564                 break;
565         case REF_STATUS_REJECT_FETCH_FIRST:
566                 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
567                                  "fetch first", porcelain, summary_width);
568                 break;
569         case REF_STATUS_REJECT_NEEDS_FORCE:
570                 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
571                                  "needs force", porcelain, summary_width);
572                 break;
573         case REF_STATUS_REJECT_STALE:
574                 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
575                                  "stale info", porcelain, summary_width);
576                 break;
577         case REF_STATUS_REJECT_SHALLOW:
578                 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
579                                  "new shallow roots not allowed",
580                                  porcelain, summary_width);
581                 break;
582         case REF_STATUS_REMOTE_REJECT:
583                 print_ref_status('!', "[remote rejected]", ref,
584                                  ref->deletion ? NULL : ref->peer_ref,
585                                  ref->remote_status, porcelain, summary_width);
586                 break;
587         case REF_STATUS_EXPECTING_REPORT:
588                 print_ref_status('!', "[remote failure]", ref,
589                                  ref->deletion ? NULL : ref->peer_ref,
590                                  "remote failed to report status",
591                                  porcelain, summary_width);
592                 break;
593         case REF_STATUS_ATOMIC_PUSH_FAILED:
594                 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
595                                  "atomic push failed", porcelain, summary_width);
596                 break;
597         case REF_STATUS_OK:
598                 print_ok_ref_status(ref, porcelain, summary_width);
599                 break;
600         }
601
602         return 1;
603 }
604
605 static int measure_abbrev(const struct object_id *oid, int sofar)
606 {
607         char hex[GIT_MAX_HEXSZ + 1];
608         int w = find_unique_abbrev_r(hex, oid, DEFAULT_ABBREV);
609
610         return (w < sofar) ? sofar : w;
611 }
612
613 int transport_summary_width(const struct ref *refs)
614 {
615         int maxw = -1;
616
617         for (; refs; refs = refs->next) {
618                 maxw = measure_abbrev(&refs->old_oid, maxw);
619                 maxw = measure_abbrev(&refs->new_oid, maxw);
620         }
621         if (maxw < 0)
622                 maxw = FALLBACK_DEFAULT_ABBREV;
623         return (2 * maxw + 3);
624 }
625
626 void transport_print_push_status(const char *dest, struct ref *refs,
627                                   int verbose, int porcelain, unsigned int *reject_reasons)
628 {
629         struct ref *ref;
630         int n = 0;
631         char *head;
632         int summary_width = transport_summary_width(refs);
633
634         if (transport_color_config() < 0)
635                 warning(_("could not parse transport.color.* config"));
636
637         head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
638
639         if (verbose) {
640                 for (ref = refs; ref; ref = ref->next)
641                         if (ref->status == REF_STATUS_UPTODATE)
642                                 n += print_one_push_status(ref, dest, n,
643                                                            porcelain, summary_width);
644         }
645
646         for (ref = refs; ref; ref = ref->next)
647                 if (ref->status == REF_STATUS_OK)
648                         n += print_one_push_status(ref, dest, n,
649                                                    porcelain, summary_width);
650
651         *reject_reasons = 0;
652         for (ref = refs; ref; ref = ref->next) {
653                 if (ref->status != REF_STATUS_NONE &&
654                     ref->status != REF_STATUS_UPTODATE &&
655                     ref->status != REF_STATUS_OK)
656                         n += print_one_push_status(ref, dest, n,
657                                                    porcelain, summary_width);
658                 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
659                         if (head != NULL && !strcmp(head, ref->name))
660                                 *reject_reasons |= REJECT_NON_FF_HEAD;
661                         else
662                                 *reject_reasons |= REJECT_NON_FF_OTHER;
663                 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
664                         *reject_reasons |= REJECT_ALREADY_EXISTS;
665                 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
666                         *reject_reasons |= REJECT_FETCH_FIRST;
667                 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
668                         *reject_reasons |= REJECT_NEEDS_FORCE;
669                 }
670         }
671         free(head);
672 }
673
674 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
675 {
676         struct git_transport_data *data = transport->data;
677         struct send_pack_args args;
678         int ret = 0;
679
680         if (transport_color_config() < 0)
681                 return -1;
682
683         if (!data->got_remote_heads)
684                 get_refs_via_connect(transport, 1, NULL);
685
686         memset(&args, 0, sizeof(args));
687         args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
688         args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
689         args.use_thin_pack = data->options.thin;
690         args.verbose = (transport->verbose > 0);
691         args.quiet = (transport->verbose < 0);
692         args.progress = transport->progress;
693         args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
694         args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
695         args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
696         args.push_options = transport->push_options;
697         args.url = transport->url;
698
699         if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
700                 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
701         else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
702                 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
703         else
704                 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
705
706         switch (data->version) {
707         case protocol_v2:
708                 die(_("support for protocol v2 not implemented yet"));
709                 break;
710         case protocol_v1:
711         case protocol_v0:
712                 ret = send_pack(&args, data->fd, data->conn, remote_refs,
713                                 &data->extra_have);
714                 break;
715         case protocol_unknown_version:
716                 BUG("unknown protocol version");
717         }
718
719         close(data->fd[1]);
720         close(data->fd[0]);
721         /*
722          * Atomic push may abort the connection early and close the pipe,
723          * which may cause an error for `finish_connect()`. Ignore this error
724          * for atomic git-push.
725          */
726         if (ret || args.atomic)
727                 finish_connect(data->conn);
728         else
729                 ret = finish_connect(data->conn);
730         data->conn = NULL;
731         data->got_remote_heads = 0;
732
733         return ret;
734 }
735
736 static int connect_git(struct transport *transport, const char *name,
737                        const char *executable, int fd[2])
738 {
739         struct git_transport_data *data = transport->data;
740         data->conn = git_connect(data->fd, transport->url,
741                                  executable, 0);
742         fd[0] = data->fd[0];
743         fd[1] = data->fd[1];
744         return 0;
745 }
746
747 static int disconnect_git(struct transport *transport)
748 {
749         struct git_transport_data *data = transport->data;
750         if (data->conn) {
751                 if (data->got_remote_heads && !transport->stateless_rpc)
752                         packet_flush(data->fd[1]);
753                 close(data->fd[0]);
754                 close(data->fd[1]);
755                 finish_connect(data->conn);
756         }
757
758         free(data);
759         return 0;
760 }
761
762 static struct transport_vtable taken_over_vtable = {
763         NULL,
764         get_refs_via_connect,
765         fetch_refs_via_pack,
766         git_transport_push,
767         NULL,
768         disconnect_git
769 };
770
771 void transport_take_over(struct transport *transport,
772                          struct child_process *child)
773 {
774         struct git_transport_data *data;
775
776         if (!transport->smart_options)
777                 BUG("taking over transport requires non-NULL "
778                     "smart_options field.");
779
780         data = xcalloc(1, sizeof(*data));
781         data->options = *transport->smart_options;
782         data->conn = child;
783         data->fd[0] = data->conn->out;
784         data->fd[1] = data->conn->in;
785         data->got_remote_heads = 0;
786         transport->data = data;
787
788         transport->vtable = &taken_over_vtable;
789         transport->smart_options = &(data->options);
790
791         transport->cannot_reuse = 1;
792 }
793
794 static int is_file(const char *url)
795 {
796         struct stat buf;
797         if (stat(url, &buf))
798                 return 0;
799         return S_ISREG(buf.st_mode);
800 }
801
802 static int external_specification_len(const char *url)
803 {
804         return strchr(url, ':') - url;
805 }
806
807 static const struct string_list *protocol_whitelist(void)
808 {
809         static int enabled = -1;
810         static struct string_list allowed = STRING_LIST_INIT_DUP;
811
812         if (enabled < 0) {
813                 const char *v = getenv("GIT_ALLOW_PROTOCOL");
814                 if (v) {
815                         string_list_split(&allowed, v, ':', -1);
816                         string_list_sort(&allowed);
817                         enabled = 1;
818                 } else {
819                         enabled = 0;
820                 }
821         }
822
823         return enabled ? &allowed : NULL;
824 }
825
826 enum protocol_allow_config {
827         PROTOCOL_ALLOW_NEVER = 0,
828         PROTOCOL_ALLOW_USER_ONLY,
829         PROTOCOL_ALLOW_ALWAYS
830 };
831
832 static enum protocol_allow_config parse_protocol_config(const char *key,
833                                                         const char *value)
834 {
835         if (!strcasecmp(value, "always"))
836                 return PROTOCOL_ALLOW_ALWAYS;
837         else if (!strcasecmp(value, "never"))
838                 return PROTOCOL_ALLOW_NEVER;
839         else if (!strcasecmp(value, "user"))
840                 return PROTOCOL_ALLOW_USER_ONLY;
841
842         die(_("unknown value for config '%s': %s"), key, value);
843 }
844
845 static enum protocol_allow_config get_protocol_config(const char *type)
846 {
847         char *key = xstrfmt("protocol.%s.allow", type);
848         char *value;
849
850         /* first check the per-protocol config */
851         if (!git_config_get_string(key, &value)) {
852                 enum protocol_allow_config ret =
853                         parse_protocol_config(key, value);
854                 free(key);
855                 free(value);
856                 return ret;
857         }
858         free(key);
859
860         /* if defined, fallback to user-defined default for unknown protocols */
861         if (!git_config_get_string("protocol.allow", &value)) {
862                 enum protocol_allow_config ret =
863                         parse_protocol_config("protocol.allow", value);
864                 free(value);
865                 return ret;
866         }
867
868         /* fallback to built-in defaults */
869         /* known safe */
870         if (!strcmp(type, "http") ||
871             !strcmp(type, "https") ||
872             !strcmp(type, "git") ||
873             !strcmp(type, "ssh") ||
874             !strcmp(type, "file"))
875                 return PROTOCOL_ALLOW_ALWAYS;
876
877         /* known scary; err on the side of caution */
878         if (!strcmp(type, "ext"))
879                 return PROTOCOL_ALLOW_NEVER;
880
881         /* unknown; by default let them be used only directly by the user */
882         return PROTOCOL_ALLOW_USER_ONLY;
883 }
884
885 int is_transport_allowed(const char *type, int from_user)
886 {
887         const struct string_list *whitelist = protocol_whitelist();
888         if (whitelist)
889                 return string_list_has_string(whitelist, type);
890
891         switch (get_protocol_config(type)) {
892         case PROTOCOL_ALLOW_ALWAYS:
893                 return 1;
894         case PROTOCOL_ALLOW_NEVER:
895                 return 0;
896         case PROTOCOL_ALLOW_USER_ONLY:
897                 if (from_user < 0)
898                         from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
899                 return from_user;
900         }
901
902         BUG("invalid protocol_allow_config type");
903 }
904
905 void transport_check_allowed(const char *type)
906 {
907         if (!is_transport_allowed(type, -1))
908                 die(_("transport '%s' not allowed"), type);
909 }
910
911 static struct transport_vtable bundle_vtable = {
912         NULL,
913         get_refs_from_bundle,
914         fetch_refs_from_bundle,
915         NULL,
916         NULL,
917         close_bundle
918 };
919
920 static struct transport_vtable builtin_smart_vtable = {
921         NULL,
922         get_refs_via_connect,
923         fetch_refs_via_pack,
924         git_transport_push,
925         connect_git,
926         disconnect_git
927 };
928
929 struct transport *transport_get(struct remote *remote, const char *url)
930 {
931         const char *helper;
932         struct transport *ret = xcalloc(1, sizeof(*ret));
933
934         ret->progress = isatty(2);
935
936         if (!remote)
937                 BUG("No remote provided to transport_get()");
938
939         ret->got_remote_refs = 0;
940         ret->remote = remote;
941         helper = remote->foreign_vcs;
942
943         if (!url && remote->url)
944                 url = remote->url[0];
945         ret->url = url;
946
947         /* maybe it is a foreign URL? */
948         if (url) {
949                 const char *p = url;
950
951                 while (is_urlschemechar(p == url, *p))
952                         p++;
953                 if (starts_with(p, "::"))
954                         helper = xstrndup(url, p - url);
955         }
956
957         if (helper) {
958                 transport_helper_init(ret, helper);
959         } else if (starts_with(url, "rsync:")) {
960                 die(_("git-over-rsync is no longer supported"));
961         } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
962                 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
963                 transport_check_allowed("file");
964                 ret->data = data;
965                 ret->vtable = &bundle_vtable;
966                 ret->smart_options = NULL;
967         } else if (!is_url(url)
968                 || starts_with(url, "file://")
969                 || starts_with(url, "git://")
970                 || starts_with(url, "ssh://")
971                 || starts_with(url, "git+ssh://") /* deprecated - do not use */
972                 || starts_with(url, "ssh+git://") /* deprecated - do not use */
973                 ) {
974                 /*
975                  * These are builtin smart transports; "allowed" transports
976                  * will be checked individually in git_connect.
977                  */
978                 struct git_transport_data *data = xcalloc(1, sizeof(*data));
979                 ret->data = data;
980                 ret->vtable = &builtin_smart_vtable;
981                 ret->smart_options = &(data->options);
982
983                 data->conn = NULL;
984                 data->got_remote_heads = 0;
985         } else {
986                 /* Unknown protocol in URL. Pass to external handler. */
987                 int len = external_specification_len(url);
988                 char *handler = xmemdupz(url, len);
989                 transport_helper_init(ret, handler);
990         }
991
992         if (ret->smart_options) {
993                 ret->smart_options->thin = 1;
994                 ret->smart_options->uploadpack = "git-upload-pack";
995                 if (remote->uploadpack)
996                         ret->smart_options->uploadpack = remote->uploadpack;
997                 ret->smart_options->receivepack = "git-receive-pack";
998                 if (remote->receivepack)
999                         ret->smart_options->receivepack = remote->receivepack;
1000         }
1001
1002         return ret;
1003 }
1004
1005 int transport_set_option(struct transport *transport,
1006                          const char *name, const char *value)
1007 {
1008         int git_reports = 1, protocol_reports = 1;
1009
1010         if (transport->smart_options)
1011                 git_reports = set_git_option(transport->smart_options,
1012                                              name, value);
1013
1014         if (transport->vtable->set_option)
1015                 protocol_reports = transport->vtable->set_option(transport,
1016                                                                  name, value);
1017
1018         /* If either report is 0, report 0 (success). */
1019         if (!git_reports || !protocol_reports)
1020                 return 0;
1021         /* If either reports -1 (invalid value), report -1. */
1022         if ((git_reports == -1) || (protocol_reports == -1))
1023                 return -1;
1024         /* Otherwise if both report unknown, report unknown. */
1025         return 1;
1026 }
1027
1028 void transport_set_verbosity(struct transport *transport, int verbosity,
1029         int force_progress)
1030 {
1031         if (verbosity >= 1)
1032                 transport->verbose = verbosity <= 3 ? verbosity : 3;
1033         if (verbosity < 0)
1034                 transport->verbose = -1;
1035
1036         /**
1037          * Rules used to determine whether to report progress (processing aborts
1038          * when a rule is satisfied):
1039          *
1040          *   . Report progress, if force_progress is 1 (ie. --progress).
1041          *   . Don't report progress, if force_progress is 0 (ie. --no-progress).
1042          *   . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1043          *   . Report progress if isatty(2) is 1.
1044          **/
1045         if (force_progress >= 0)
1046                 transport->progress = !!force_progress;
1047         else
1048                 transport->progress = verbosity >= 0 && isatty(2);
1049 }
1050
1051 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
1052 {
1053         int i;
1054
1055         fprintf(stderr, _("The following submodule paths contain changes that can\n"
1056                         "not be found on any remote:\n"));
1057         for (i = 0; i < needs_pushing->nr; i++)
1058                 fprintf(stderr, "  %s\n", needs_pushing->items[i].string);
1059         fprintf(stderr, _("\nPlease try\n\n"
1060                           "     git push --recurse-submodules=on-demand\n\n"
1061                           "or cd to the path and use\n\n"
1062                           "     git push\n\n"
1063                           "to push them to a remote.\n\n"));
1064
1065         string_list_clear(needs_pushing, 0);
1066
1067         die(_("Aborting."));
1068 }
1069
1070 static int run_pre_push_hook(struct transport *transport,
1071                              struct ref *remote_refs)
1072 {
1073         int ret = 0, x;
1074         struct ref *r;
1075         struct child_process proc = CHILD_PROCESS_INIT;
1076         struct strbuf buf;
1077         const char *argv[4];
1078
1079         if (!(argv[0] = find_hook("pre-push")))
1080                 return 0;
1081
1082         argv[1] = transport->remote->name;
1083         argv[2] = transport->url;
1084         argv[3] = NULL;
1085
1086         proc.argv = argv;
1087         proc.in = -1;
1088         proc.trace2_hook_name = "pre-push";
1089
1090         if (start_command(&proc)) {
1091                 finish_command(&proc);
1092                 return -1;
1093         }
1094
1095         sigchain_push(SIGPIPE, SIG_IGN);
1096
1097         strbuf_init(&buf, 256);
1098
1099         for (r = remote_refs; r; r = r->next) {
1100                 if (!r->peer_ref) continue;
1101                 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
1102                 if (r->status == REF_STATUS_REJECT_STALE) continue;
1103                 if (r->status == REF_STATUS_UPTODATE) continue;
1104
1105                 strbuf_reset(&buf);
1106                 strbuf_addf( &buf, "%s %s %s %s\n",
1107                          r->peer_ref->name, oid_to_hex(&r->new_oid),
1108                          r->name, oid_to_hex(&r->old_oid));
1109
1110                 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
1111                         /* We do not mind if a hook does not read all refs. */
1112                         if (errno != EPIPE)
1113                                 ret = -1;
1114                         break;
1115                 }
1116         }
1117
1118         strbuf_release(&buf);
1119
1120         x = close(proc.in);
1121         if (!ret)
1122                 ret = x;
1123
1124         sigchain_pop(SIGPIPE);
1125
1126         x = finish_command(&proc);
1127         if (!ret)
1128                 ret = x;
1129
1130         return ret;
1131 }
1132
1133 int transport_push(struct repository *r,
1134                    struct transport *transport,
1135                    struct refspec *rs, int flags,
1136                    unsigned int *reject_reasons)
1137 {
1138         *reject_reasons = 0;
1139
1140         if (transport_color_config() < 0)
1141                 return -1;
1142
1143         if (transport->vtable->push_refs) {
1144                 struct ref *remote_refs;
1145                 struct ref *local_refs = get_local_heads();
1146                 int match_flags = MATCH_REFS_NONE;
1147                 int verbose = (transport->verbose > 0);
1148                 int quiet = (transport->verbose < 0);
1149                 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1150                 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1151                 int push_ret, ret, err;
1152                 struct argv_array ref_prefixes = ARGV_ARRAY_INIT;
1153
1154                 if (check_push_refs(local_refs, rs) < 0)
1155                         return -1;
1156
1157                 refspec_ref_prefixes(rs, &ref_prefixes);
1158
1159                 trace2_region_enter("transport_push", "get_refs_list", r);
1160                 remote_refs = transport->vtable->get_refs_list(transport, 1,
1161                                                                &ref_prefixes);
1162                 trace2_region_leave("transport_push", "get_refs_list", r);
1163
1164                 argv_array_clear(&ref_prefixes);
1165
1166                 if (flags & TRANSPORT_PUSH_ALL)
1167                         match_flags |= MATCH_REFS_ALL;
1168                 if (flags & TRANSPORT_PUSH_MIRROR)
1169                         match_flags |= MATCH_REFS_MIRROR;
1170                 if (flags & TRANSPORT_PUSH_PRUNE)
1171                         match_flags |= MATCH_REFS_PRUNE;
1172                 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1173                         match_flags |= MATCH_REFS_FOLLOW_TAGS;
1174
1175                 if (match_push_refs(local_refs, &remote_refs, rs, match_flags))
1176                         return -1;
1177
1178                 if (transport->smart_options &&
1179                     transport->smart_options->cas &&
1180                     !is_empty_cas(transport->smart_options->cas))
1181                         apply_push_cas(transport->smart_options->cas,
1182                                        transport->remote, remote_refs);
1183
1184                 set_ref_status_for_push(remote_refs,
1185                         flags & TRANSPORT_PUSH_MIRROR,
1186                         flags & TRANSPORT_PUSH_FORCE);
1187
1188                 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1189                         if (run_pre_push_hook(transport, remote_refs))
1190                                 return -1;
1191
1192                 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1193                               TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1194                     !is_bare_repository()) {
1195                         struct ref *ref = remote_refs;
1196                         struct oid_array commits = OID_ARRAY_INIT;
1197
1198                         trace2_region_enter("transport_push", "push_submodules", r);
1199                         for (; ref; ref = ref->next)
1200                                 if (!is_null_oid(&ref->new_oid))
1201                                         oid_array_append(&commits,
1202                                                           &ref->new_oid);
1203
1204                         if (!push_unpushed_submodules(r,
1205                                                       &commits,
1206                                                       transport->remote,
1207                                                       rs,
1208                                                       transport->push_options,
1209                                                       pretend)) {
1210                                 oid_array_clear(&commits);
1211                                 trace2_region_leave("transport_push", "push_submodules", r);
1212                                 die(_("failed to push all needed submodules"));
1213                         }
1214                         oid_array_clear(&commits);
1215                         trace2_region_leave("transport_push", "push_submodules", r);
1216                 }
1217
1218                 if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1219                      ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1220                                 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1221                       !pretend)) && !is_bare_repository()) {
1222                         struct ref *ref = remote_refs;
1223                         struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1224                         struct oid_array commits = OID_ARRAY_INIT;
1225
1226                         trace2_region_enter("transport_push", "check_submodules", r);
1227                         for (; ref; ref = ref->next)
1228                                 if (!is_null_oid(&ref->new_oid))
1229                                         oid_array_append(&commits,
1230                                                           &ref->new_oid);
1231
1232                         if (find_unpushed_submodules(r,
1233                                                      &commits,
1234                                                      transport->remote->name,
1235                                                      &needs_pushing)) {
1236                                 oid_array_clear(&commits);
1237                                 trace2_region_leave("transport_push", "check_submodules", r);
1238                                 die_with_unpushed_submodules(&needs_pushing);
1239                         }
1240                         string_list_clear(&needs_pushing, 0);
1241                         oid_array_clear(&commits);
1242                         trace2_region_leave("transport_push", "check_submodules", r);
1243                 }
1244
1245                 if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY)) {
1246                         trace2_region_enter("transport_push", "push_refs", r);
1247                         push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1248                         trace2_region_leave("transport_push", "push_refs", r);
1249                 } else
1250                         push_ret = 0;
1251                 err = push_had_errors(remote_refs);
1252                 ret = push_ret | err;
1253
1254                 if (!quiet || err)
1255                         transport_print_push_status(transport->url, remote_refs,
1256                                         verbose | porcelain, porcelain,
1257                                         reject_reasons);
1258
1259                 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1260                         set_upstreams(transport, remote_refs, pretend);
1261
1262                 if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1263                                TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1264                         struct ref *ref;
1265                         for (ref = remote_refs; ref; ref = ref->next)
1266                                 transport_update_tracking_ref(transport->remote, ref, verbose);
1267                 }
1268
1269                 if (porcelain && !push_ret)
1270                         puts("Done");
1271                 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1272                         fprintf(stderr, "Everything up-to-date\n");
1273
1274                 return ret;
1275         }
1276         return 1;
1277 }
1278
1279 const struct ref *transport_get_remote_refs(struct transport *transport,
1280                                             const struct argv_array *ref_prefixes)
1281 {
1282         if (!transport->got_remote_refs) {
1283                 transport->remote_refs =
1284                         transport->vtable->get_refs_list(transport, 0,
1285                                                          ref_prefixes);
1286                 transport->got_remote_refs = 1;
1287         }
1288
1289         return transport->remote_refs;
1290 }
1291
1292 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1293 {
1294         int rc;
1295         int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1296         struct ref **heads = NULL;
1297         struct ref *rm;
1298
1299         for (rm = refs; rm; rm = rm->next) {
1300                 nr_refs++;
1301                 if (rm->peer_ref &&
1302                     !is_null_oid(&rm->old_oid) &&
1303                     oideq(&rm->peer_ref->old_oid, &rm->old_oid))
1304                         continue;
1305                 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1306                 heads[nr_heads++] = rm;
1307         }
1308
1309         if (!nr_heads) {
1310                 /*
1311                  * When deepening of a shallow repository is requested,
1312                  * then local and remote refs are likely to still be equal.
1313                  * Just feed them all to the fetch method in that case.
1314                  * This condition shouldn't be met in a non-deepening fetch
1315                  * (see builtin/fetch.c:quickfetch()).
1316                  */
1317                 ALLOC_ARRAY(heads, nr_refs);
1318                 for (rm = refs; rm; rm = rm->next)
1319                         heads[nr_heads++] = rm;
1320         }
1321
1322         rc = transport->vtable->fetch(transport, nr_heads, heads);
1323
1324         free(heads);
1325         return rc;
1326 }
1327
1328 void transport_unlock_pack(struct transport *transport)
1329 {
1330         if (transport->pack_lockfile) {
1331                 unlink_or_warn(transport->pack_lockfile);
1332                 FREE_AND_NULL(transport->pack_lockfile);
1333         }
1334 }
1335
1336 int transport_connect(struct transport *transport, const char *name,
1337                       const char *exec, int fd[2])
1338 {
1339         if (transport->vtable->connect)
1340                 return transport->vtable->connect(transport, name, exec, fd);
1341         else
1342                 die(_("operation not supported by protocol"));
1343 }
1344
1345 int transport_disconnect(struct transport *transport)
1346 {
1347         int ret = 0;
1348         if (transport->vtable->disconnect)
1349                 ret = transport->vtable->disconnect(transport);
1350         free(transport);
1351         return ret;
1352 }
1353
1354 /*
1355  * Strip username (and password) from a URL and return
1356  * it in a newly allocated string.
1357  */
1358 char *transport_anonymize_url(const char *url)
1359 {
1360         char *scheme_prefix, *anon_part;
1361         size_t anon_len, prefix_len = 0;
1362
1363         anon_part = strchr(url, '@');
1364         if (url_is_local_not_ssh(url) || !anon_part)
1365                 goto literal_copy;
1366
1367         anon_len = strlen(++anon_part);
1368         scheme_prefix = strstr(url, "://");
1369         if (!scheme_prefix) {
1370                 if (!strchr(anon_part, ':'))
1371                         /* cannot be "me@there:/path/name" */
1372                         goto literal_copy;
1373         } else {
1374                 const char *cp;
1375                 /* make sure scheme is reasonable */
1376                 for (cp = url; cp < scheme_prefix; cp++) {
1377                         switch (*cp) {
1378                                 /* RFC 1738 2.1 */
1379                         case '+': case '.': case '-':
1380                                 break; /* ok */
1381                         default:
1382                                 if (isalnum(*cp))
1383                                         break;
1384                                 /* it isn't */
1385                                 goto literal_copy;
1386                         }
1387                 }
1388                 /* @ past the first slash does not count */
1389                 cp = strchr(scheme_prefix + 3, '/');
1390                 if (cp && cp < anon_part)
1391                         goto literal_copy;
1392                 prefix_len = scheme_prefix - url + 3;
1393         }
1394         return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1395                        (int)anon_len, anon_part);
1396 literal_copy:
1397         return xstrdup(url);
1398 }