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