Second batch
[git/debian.git] / transport.c
blob01e24bd578f29f79b5afad8bad04f6c64fde590d
1 #include "cache.h"
2 #include "config.h"
3 #include "transport.h"
4 #include "hook.h"
5 #include "pkt-line.h"
6 #include "fetch-pack.h"
7 #include "remote.h"
8 #include "connect.h"
9 #include "send-pack.h"
10 #include "walker.h"
11 #include "bundle.h"
12 #include "dir.h"
13 #include "refs.h"
14 #include "refspec.h"
15 #include "branch.h"
16 #include "url.h"
17 #include "submodule.h"
18 #include "string-list.h"
19 #include "oid-array.h"
20 #include "sigchain.h"
21 #include "transport-internal.h"
22 #include "protocol.h"
23 #include "object-store.h"
24 #include "color.h"
26 static int transport_use_color = -1;
27 static char transport_colors[][COLOR_MAXLEN] = {
28 GIT_COLOR_RESET,
29 GIT_COLOR_RED /* REJECTED */
32 enum color_transport {
33 TRANSPORT_COLOR_RESET = 0,
34 TRANSPORT_COLOR_REJECTED = 1
37 static int transport_color_config(void)
39 const char *keys[] = {
40 "color.transport.reset",
41 "color.transport.rejected"
42 }, *key = "color.transport";
43 char *value;
44 int i;
45 static int initialized;
47 if (initialized)
48 return 0;
49 initialized = 1;
51 if (!git_config_get_string(key, &value))
52 transport_use_color = git_config_colorbool(key, value);
54 if (!want_color_stderr(transport_use_color))
55 return 0;
57 for (i = 0; i < ARRAY_SIZE(keys); i++)
58 if (!git_config_get_string(keys[i], &value)) {
59 if (!value)
60 return config_error_nonbool(keys[i]);
61 if (color_parse(value, transport_colors[i]) < 0)
62 return -1;
65 return 0;
68 static const char *transport_get_color(enum color_transport ix)
70 if (want_color_stderr(transport_use_color))
71 return transport_colors[ix];
72 return "";
75 static void set_upstreams(struct transport *transport, struct ref *refs,
76 int pretend)
78 struct ref *ref;
79 for (ref = refs; ref; ref = ref->next) {
80 const char *localname;
81 const char *tmp;
82 const char *remotename;
83 int flag = 0;
85 * Check suitability for tracking. Must be successful /
86 * already up-to-date ref create/modify (not delete).
88 if (ref->status != REF_STATUS_OK &&
89 ref->status != REF_STATUS_UPTODATE)
90 continue;
91 if (!ref->peer_ref)
92 continue;
93 if (is_null_oid(&ref->new_oid))
94 continue;
96 /* Follow symbolic refs (mainly for HEAD). */
97 localname = ref->peer_ref->name;
98 remotename = ref->name;
99 tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING,
100 NULL, &flag);
101 if (tmp && flag & REF_ISSYMREF &&
102 starts_with(tmp, "refs/heads/"))
103 localname = tmp;
105 /* Both source and destination must be local branches. */
106 if (!localname || !starts_with(localname, "refs/heads/"))
107 continue;
108 if (!remotename || !starts_with(remotename, "refs/heads/"))
109 continue;
111 if (!pretend) {
112 int flag = transport->verbose < 0 ? 0 : BRANCH_CONFIG_VERBOSE;
113 install_branch_config(flag, localname + 11,
114 transport->remote->name, remotename);
115 } else if (transport->verbose >= 0)
116 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
117 localname + 11, remotename + 11,
118 transport->remote->name);
122 struct bundle_transport_data {
123 int fd;
124 struct bundle_header header;
125 unsigned get_refs_from_bundle_called : 1;
128 static void get_refs_from_bundle_inner(struct transport *transport)
130 struct bundle_transport_data *data = transport->data;
132 data->get_refs_from_bundle_called = 1;
134 if (data->fd > 0)
135 close(data->fd);
136 data->fd = read_bundle_header(transport->url, &data->header);
137 if (data->fd < 0)
138 die(_("could not read bundle '%s'"), transport->url);
140 transport->hash_algo = data->header.hash_algo;
143 static struct ref *get_refs_from_bundle(struct transport *transport,
144 int for_push,
145 struct transport_ls_refs_options *transport_options)
147 struct bundle_transport_data *data = transport->data;
148 struct ref *result = NULL;
149 int i;
151 if (for_push)
152 return NULL;
154 get_refs_from_bundle_inner(transport);
156 for (i = 0; i < data->header.references.nr; i++) {
157 struct string_list_item *e = data->header.references.items + i;
158 const char *name = e->string;
159 struct ref *ref = alloc_ref(name);
160 struct object_id *oid = e->util;
161 oidcpy(&ref->old_oid, oid);
162 ref->next = result;
163 result = ref;
165 return result;
168 static int fetch_refs_from_bundle(struct transport *transport,
169 int nr_heads, struct ref **to_fetch)
171 struct bundle_transport_data *data = transport->data;
172 struct strvec extra_index_pack_args = STRVEC_INIT;
173 int ret;
175 if (transport->progress)
176 strvec_push(&extra_index_pack_args, "-v");
178 if (!data->get_refs_from_bundle_called)
179 get_refs_from_bundle_inner(transport);
180 ret = unbundle(the_repository, &data->header, data->fd,
181 &extra_index_pack_args);
182 transport->hash_algo = data->header.hash_algo;
183 return ret;
186 static int close_bundle(struct transport *transport)
188 struct bundle_transport_data *data = transport->data;
189 if (data->fd > 0)
190 close(data->fd);
191 bundle_header_release(&data->header);
192 free(data);
193 return 0;
196 struct git_transport_data {
197 struct git_transport_options options;
198 struct child_process *conn;
199 int fd[2];
200 unsigned got_remote_heads : 1;
201 enum protocol_version version;
202 struct oid_array extra_have;
203 struct oid_array shallow;
206 static int set_git_option(struct git_transport_options *opts,
207 const char *name, const char *value)
209 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
210 opts->uploadpack = value;
211 return 0;
212 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
213 opts->receivepack = value;
214 return 0;
215 } else if (!strcmp(name, TRANS_OPT_THIN)) {
216 opts->thin = !!value;
217 return 0;
218 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
219 opts->followtags = !!value;
220 return 0;
221 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
222 opts->keep = !!value;
223 return 0;
224 } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
225 opts->update_shallow = !!value;
226 return 0;
227 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
228 if (!value)
229 opts->depth = 0;
230 else {
231 char *end;
232 opts->depth = strtol(value, &end, 0);
233 if (*end)
234 die(_("transport: invalid depth option '%s'"), value);
236 return 0;
237 } else if (!strcmp(name, TRANS_OPT_DEEPEN_SINCE)) {
238 opts->deepen_since = value;
239 return 0;
240 } else if (!strcmp(name, TRANS_OPT_DEEPEN_NOT)) {
241 opts->deepen_not = (const struct string_list *)value;
242 return 0;
243 } else if (!strcmp(name, TRANS_OPT_DEEPEN_RELATIVE)) {
244 opts->deepen_relative = !!value;
245 return 0;
246 } else if (!strcmp(name, TRANS_OPT_FROM_PROMISOR)) {
247 opts->from_promisor = !!value;
248 return 0;
249 } else if (!strcmp(name, TRANS_OPT_LIST_OBJECTS_FILTER)) {
250 list_objects_filter_die_if_populated(&opts->filter_options);
251 parse_list_objects_filter(&opts->filter_options, value);
252 return 0;
253 } else if (!strcmp(name, TRANS_OPT_REFETCH)) {
254 opts->refetch = !!value;
255 return 0;
256 } else if (!strcmp(name, TRANS_OPT_REJECT_SHALLOW)) {
257 opts->reject_shallow = !!value;
258 return 0;
260 return 1;
263 static int connect_setup(struct transport *transport, int for_push)
265 struct git_transport_data *data = transport->data;
266 int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
268 if (data->conn)
269 return 0;
271 switch (transport->family) {
272 case TRANSPORT_FAMILY_ALL: break;
273 case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
274 case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
277 data->conn = git_connect(data->fd, transport->url,
278 for_push ? data->options.receivepack :
279 data->options.uploadpack,
280 flags);
282 return 0;
285 static void die_if_server_options(struct transport *transport)
287 if (!transport->server_options || !transport->server_options->nr)
288 return;
289 advise(_("see protocol.version in 'git help config' for more details"));
290 die(_("server options require protocol version 2 or later"));
294 * Obtains the protocol version from the transport and writes it to
295 * transport->data->version, first connecting if not already connected.
297 * If the protocol version is one that allows skipping the listing of remote
298 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
299 * this function returns NULL. Otherwise, this function returns the list of
300 * remote refs.
302 static struct ref *handshake(struct transport *transport, int for_push,
303 struct transport_ls_refs_options *options,
304 int must_list_refs)
306 struct git_transport_data *data = transport->data;
307 struct ref *refs = NULL;
308 struct packet_reader reader;
309 int sid_len;
310 const char *server_sid;
312 connect_setup(transport, for_push);
314 packet_reader_init(&reader, data->fd[0], NULL, 0,
315 PACKET_READ_CHOMP_NEWLINE |
316 PACKET_READ_GENTLE_ON_EOF |
317 PACKET_READ_DIE_ON_ERR_PACKET);
319 data->version = discover_version(&reader);
320 switch (data->version) {
321 case protocol_v2:
322 if (server_feature_v2("session-id", &server_sid))
323 trace2_data_string("transfer", NULL, "server-sid", server_sid);
324 if (must_list_refs)
325 get_remote_refs(data->fd[1], &reader, &refs, for_push,
326 options,
327 transport->server_options,
328 transport->stateless_rpc);
329 break;
330 case protocol_v1:
331 case protocol_v0:
332 die_if_server_options(transport);
333 get_remote_heads(&reader, &refs,
334 for_push ? REF_NORMAL : 0,
335 &data->extra_have,
336 &data->shallow);
337 server_sid = server_feature_value("session-id", &sid_len);
338 if (server_sid) {
339 char *sid = xstrndup(server_sid, sid_len);
340 trace2_data_string("transfer", NULL, "server-sid", sid);
341 free(sid);
343 break;
344 case protocol_unknown_version:
345 BUG("unknown protocol version");
347 data->got_remote_heads = 1;
348 transport->hash_algo = reader.hash_algo;
350 if (reader.line_peeked)
351 BUG("buffer must be empty at the end of handshake()");
353 return refs;
356 static struct ref *get_refs_via_connect(struct transport *transport, int for_push,
357 struct transport_ls_refs_options *options)
359 return handshake(transport, for_push, options, 1);
362 static int fetch_refs_via_pack(struct transport *transport,
363 int nr_heads, struct ref **to_fetch)
365 int ret = 0;
366 struct git_transport_data *data = transport->data;
367 struct ref *refs = NULL;
368 struct fetch_pack_args args;
369 struct ref *refs_tmp = NULL;
371 memset(&args, 0, sizeof(args));
372 args.uploadpack = data->options.uploadpack;
373 args.keep_pack = data->options.keep;
374 args.lock_pack = 1;
375 args.use_thin_pack = data->options.thin;
376 args.include_tag = data->options.followtags;
377 args.verbose = (transport->verbose > 1);
378 args.quiet = (transport->verbose < 0);
379 args.no_progress = !transport->progress;
380 args.depth = data->options.depth;
381 args.deepen_since = data->options.deepen_since;
382 args.deepen_not = data->options.deepen_not;
383 args.deepen_relative = data->options.deepen_relative;
384 args.check_self_contained_and_connected =
385 data->options.check_self_contained_and_connected;
386 args.cloning = transport->cloning;
387 args.update_shallow = data->options.update_shallow;
388 args.from_promisor = data->options.from_promisor;
389 args.filter_options = data->options.filter_options;
390 args.refetch = data->options.refetch;
391 args.stateless_rpc = transport->stateless_rpc;
392 args.server_options = transport->server_options;
393 args.negotiation_tips = data->options.negotiation_tips;
394 args.reject_shallow_remote = transport->smart_options->reject_shallow;
396 if (!data->got_remote_heads) {
397 int i;
398 int must_list_refs = 0;
399 for (i = 0; i < nr_heads; i++) {
400 if (!to_fetch[i]->exact_oid) {
401 must_list_refs = 1;
402 break;
405 refs_tmp = handshake(transport, 0, NULL, must_list_refs);
408 if (data->version == protocol_unknown_version)
409 BUG("unknown protocol version");
410 else if (data->version <= protocol_v1)
411 die_if_server_options(transport);
413 if (data->options.acked_commits) {
414 if (data->version < protocol_v2) {
415 warning(_("--negotiate-only requires protocol v2"));
416 ret = -1;
417 } else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
418 warning(_("server does not support wait-for-done"));
419 ret = -1;
420 } else {
421 negotiate_using_fetch(data->options.negotiation_tips,
422 transport->server_options,
423 transport->stateless_rpc,
424 data->fd,
425 data->options.acked_commits);
426 ret = 0;
428 goto cleanup;
431 refs = fetch_pack(&args, data->fd,
432 refs_tmp ? refs_tmp : transport->remote_refs,
433 to_fetch, nr_heads, &data->shallow,
434 &transport->pack_lockfiles, data->version);
436 data->got_remote_heads = 0;
437 data->options.self_contained_and_connected =
438 args.self_contained_and_connected;
439 data->options.connectivity_checked = args.connectivity_checked;
441 if (!refs)
442 ret = -1;
443 if (report_unmatched_refs(to_fetch, nr_heads))
444 ret = -1;
446 cleanup:
447 close(data->fd[0]);
448 if (data->fd[1] >= 0)
449 close(data->fd[1]);
450 if (finish_connect(data->conn))
451 ret = -1;
452 data->conn = NULL;
454 free_refs(refs_tmp);
455 free_refs(refs);
456 return ret;
459 static int push_had_errors(struct ref *ref)
461 for (; ref; ref = ref->next) {
462 switch (ref->status) {
463 case REF_STATUS_NONE:
464 case REF_STATUS_UPTODATE:
465 case REF_STATUS_OK:
466 break;
467 default:
468 return 1;
471 return 0;
474 int transport_refs_pushed(struct ref *ref)
476 for (; ref; ref = ref->next) {
477 switch(ref->status) {
478 case REF_STATUS_NONE:
479 case REF_STATUS_UPTODATE:
480 break;
481 default:
482 return 1;
485 return 0;
488 static void update_one_tracking_ref(struct remote *remote, char *refname,
489 struct object_id *new_oid, int deletion,
490 int verbose)
492 struct refspec_item rs;
494 memset(&rs, 0, sizeof(rs));
495 rs.src = refname;
496 rs.dst = NULL;
498 if (!remote_find_tracking(remote, &rs)) {
499 if (verbose)
500 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
501 if (deletion)
502 delete_ref(NULL, rs.dst, NULL, 0);
503 else
504 update_ref("update by push", rs.dst, new_oid,
505 NULL, 0, 0);
506 free(rs.dst);
510 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
512 char *refname;
513 struct object_id *new_oid;
514 struct ref_push_report *report;
516 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
517 return;
519 report = ref->report;
520 if (!report)
521 update_one_tracking_ref(remote, ref->name, &ref->new_oid,
522 ref->deletion, verbose);
523 else
524 for (; report; report = report->next) {
525 refname = report->ref_name ? (char *)report->ref_name : ref->name;
526 new_oid = report->new_oid ? report->new_oid : &ref->new_oid;
527 update_one_tracking_ref(remote, refname, new_oid,
528 is_null_oid(new_oid), verbose);
532 static void print_ref_status(char flag, const char *summary,
533 struct ref *to, struct ref *from, const char *msg,
534 struct ref_push_report *report,
535 int porcelain, int summary_width)
537 const char *to_name;
539 if (report && report->ref_name)
540 to_name = report->ref_name;
541 else
542 to_name = to->name;
544 if (porcelain) {
545 if (from)
546 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to_name);
547 else
548 fprintf(stdout, "%c\t:%s\t", flag, to_name);
549 if (msg)
550 fprintf(stdout, "%s (%s)\n", summary, msg);
551 else
552 fprintf(stdout, "%s\n", summary);
553 } else {
554 const char *red = "", *reset = "";
555 if (push_had_errors(to)) {
556 red = transport_get_color(TRANSPORT_COLOR_REJECTED);
557 reset = transport_get_color(TRANSPORT_COLOR_RESET);
559 fprintf(stderr, " %s%c %-*s%s ", red, flag, summary_width,
560 summary, reset);
561 if (from)
562 fprintf(stderr, "%s -> %s",
563 prettify_refname(from->name),
564 prettify_refname(to_name));
565 else
566 fputs(prettify_refname(to_name), stderr);
567 if (msg) {
568 fputs(" (", stderr);
569 fputs(msg, stderr);
570 fputc(')', stderr);
572 fputc('\n', stderr);
576 static void print_ok_ref_status(struct ref *ref,
577 struct ref_push_report *report,
578 int porcelain, int summary_width)
580 struct object_id *old_oid;
581 struct object_id *new_oid;
582 const char *ref_name;
583 int forced_update;
585 if (report && report->old_oid)
586 old_oid = report->old_oid;
587 else
588 old_oid = &ref->old_oid;
589 if (report && report->new_oid)
590 new_oid = report->new_oid;
591 else
592 new_oid = &ref->new_oid;
593 if (report && report->forced_update)
594 forced_update = report->forced_update;
595 else
596 forced_update = ref->forced_update;
597 if (report && report->ref_name)
598 ref_name = report->ref_name;
599 else
600 ref_name = ref->name;
602 if (ref->deletion)
603 print_ref_status('-', "[deleted]", ref, NULL, NULL,
604 report, porcelain, summary_width);
605 else if (is_null_oid(old_oid))
606 print_ref_status('*',
607 (starts_with(ref_name, "refs/tags/")
608 ? "[new tag]"
609 : (starts_with(ref_name, "refs/heads/")
610 ? "[new branch]"
611 : "[new reference]")),
612 ref, ref->peer_ref, NULL,
613 report, porcelain, summary_width);
614 else {
615 struct strbuf quickref = STRBUF_INIT;
616 char type;
617 const char *msg;
619 strbuf_add_unique_abbrev(&quickref, old_oid,
620 DEFAULT_ABBREV);
621 if (forced_update) {
622 strbuf_addstr(&quickref, "...");
623 type = '+';
624 msg = "forced update";
625 } else {
626 strbuf_addstr(&quickref, "..");
627 type = ' ';
628 msg = NULL;
630 strbuf_add_unique_abbrev(&quickref, new_oid,
631 DEFAULT_ABBREV);
633 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
634 report, porcelain, summary_width);
635 strbuf_release(&quickref);
639 static int print_one_push_report(struct ref *ref, const char *dest, int count,
640 struct ref_push_report *report,
641 int porcelain, int summary_width)
643 if (!count) {
644 char *url = transport_anonymize_url(dest);
645 fprintf(porcelain ? stdout : stderr, "To %s\n", url);
646 free(url);
649 switch(ref->status) {
650 case REF_STATUS_NONE:
651 print_ref_status('X', "[no match]", ref, NULL, NULL,
652 report, porcelain, summary_width);
653 break;
654 case REF_STATUS_REJECT_NODELETE:
655 print_ref_status('!', "[rejected]", ref, NULL,
656 "remote does not support deleting refs",
657 report, porcelain, summary_width);
658 break;
659 case REF_STATUS_UPTODATE:
660 print_ref_status('=', "[up to date]", ref,
661 ref->peer_ref, NULL,
662 report, porcelain, summary_width);
663 break;
664 case REF_STATUS_REJECT_NONFASTFORWARD:
665 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
666 "non-fast-forward",
667 report, porcelain, summary_width);
668 break;
669 case REF_STATUS_REJECT_ALREADY_EXISTS:
670 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
671 "already exists",
672 report, porcelain, summary_width);
673 break;
674 case REF_STATUS_REJECT_FETCH_FIRST:
675 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
676 "fetch first",
677 report, porcelain, summary_width);
678 break;
679 case REF_STATUS_REJECT_NEEDS_FORCE:
680 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
681 "needs force",
682 report, porcelain, summary_width);
683 break;
684 case REF_STATUS_REJECT_STALE:
685 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
686 "stale info",
687 report, porcelain, summary_width);
688 break;
689 case REF_STATUS_REJECT_REMOTE_UPDATED:
690 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
691 "remote ref updated since checkout",
692 report, porcelain, summary_width);
693 break;
694 case REF_STATUS_REJECT_SHALLOW:
695 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
696 "new shallow roots not allowed",
697 report, porcelain, summary_width);
698 break;
699 case REF_STATUS_REMOTE_REJECT:
700 print_ref_status('!', "[remote rejected]", ref,
701 ref->deletion ? NULL : ref->peer_ref,
702 ref->remote_status,
703 report, porcelain, summary_width);
704 break;
705 case REF_STATUS_EXPECTING_REPORT:
706 print_ref_status('!', "[remote failure]", ref,
707 ref->deletion ? NULL : ref->peer_ref,
708 "remote failed to report status",
709 report, porcelain, summary_width);
710 break;
711 case REF_STATUS_ATOMIC_PUSH_FAILED:
712 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
713 "atomic push failed",
714 report, porcelain, summary_width);
715 break;
716 case REF_STATUS_OK:
717 print_ok_ref_status(ref, report, porcelain, summary_width);
718 break;
721 return 1;
724 static int print_one_push_status(struct ref *ref, const char *dest, int count,
725 int porcelain, int summary_width)
727 struct ref_push_report *report;
728 int n = 0;
730 if (!ref->report)
731 return print_one_push_report(ref, dest, count,
732 NULL, porcelain, summary_width);
734 for (report = ref->report; report; report = report->next)
735 print_one_push_report(ref, dest, count + n++,
736 report, porcelain, summary_width);
737 return n;
740 static int measure_abbrev(const struct object_id *oid, int sofar)
742 char hex[GIT_MAX_HEXSZ + 1];
743 int w = find_unique_abbrev_r(hex, oid, DEFAULT_ABBREV);
745 return (w < sofar) ? sofar : w;
748 int transport_summary_width(const struct ref *refs)
750 int maxw = -1;
752 for (; refs; refs = refs->next) {
753 maxw = measure_abbrev(&refs->old_oid, maxw);
754 maxw = measure_abbrev(&refs->new_oid, maxw);
756 if (maxw < 0)
757 maxw = FALLBACK_DEFAULT_ABBREV;
758 return (2 * maxw + 3);
761 void transport_print_push_status(const char *dest, struct ref *refs,
762 int verbose, int porcelain, unsigned int *reject_reasons)
764 struct ref *ref;
765 int n = 0;
766 char *head;
767 int summary_width = transport_summary_width(refs);
769 if (transport_color_config() < 0)
770 warning(_("could not parse transport.color.* config"));
772 head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
774 if (verbose) {
775 for (ref = refs; ref; ref = ref->next)
776 if (ref->status == REF_STATUS_UPTODATE)
777 n += print_one_push_status(ref, dest, n,
778 porcelain, summary_width);
781 for (ref = refs; ref; ref = ref->next)
782 if (ref->status == REF_STATUS_OK)
783 n += print_one_push_status(ref, dest, n,
784 porcelain, summary_width);
786 *reject_reasons = 0;
787 for (ref = refs; ref; ref = ref->next) {
788 if (ref->status != REF_STATUS_NONE &&
789 ref->status != REF_STATUS_UPTODATE &&
790 ref->status != REF_STATUS_OK)
791 n += print_one_push_status(ref, dest, n,
792 porcelain, summary_width);
793 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
794 if (head != NULL && !strcmp(head, ref->name))
795 *reject_reasons |= REJECT_NON_FF_HEAD;
796 else
797 *reject_reasons |= REJECT_NON_FF_OTHER;
798 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
799 *reject_reasons |= REJECT_ALREADY_EXISTS;
800 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
801 *reject_reasons |= REJECT_FETCH_FIRST;
802 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
803 *reject_reasons |= REJECT_NEEDS_FORCE;
804 } else if (ref->status == REF_STATUS_REJECT_REMOTE_UPDATED) {
805 *reject_reasons |= REJECT_REF_NEEDS_UPDATE;
808 free(head);
811 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
813 struct git_transport_data *data = transport->data;
814 struct send_pack_args args;
815 int ret = 0;
817 if (transport_color_config() < 0)
818 return -1;
820 if (!data->got_remote_heads)
821 get_refs_via_connect(transport, 1, NULL);
823 memset(&args, 0, sizeof(args));
824 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
825 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
826 args.use_thin_pack = data->options.thin;
827 args.verbose = (transport->verbose > 0);
828 args.quiet = (transport->verbose < 0);
829 args.progress = transport->progress;
830 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
831 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
832 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
833 args.push_options = transport->push_options;
834 args.url = transport->url;
836 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
837 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
838 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
839 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
840 else
841 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
843 switch (data->version) {
844 case protocol_v2:
845 die(_("support for protocol v2 not implemented yet"));
846 break;
847 case protocol_v1:
848 case protocol_v0:
849 ret = send_pack(&args, data->fd, data->conn, remote_refs,
850 &data->extra_have);
851 break;
852 case protocol_unknown_version:
853 BUG("unknown protocol version");
856 close(data->fd[1]);
857 close(data->fd[0]);
859 * Atomic push may abort the connection early and close the pipe,
860 * which may cause an error for `finish_connect()`. Ignore this error
861 * for atomic git-push.
863 if (ret || args.atomic)
864 finish_connect(data->conn);
865 else
866 ret = finish_connect(data->conn);
867 data->conn = NULL;
868 data->got_remote_heads = 0;
870 return ret;
873 static int connect_git(struct transport *transport, const char *name,
874 const char *executable, int fd[2])
876 struct git_transport_data *data = transport->data;
877 data->conn = git_connect(data->fd, transport->url,
878 executable, 0);
879 fd[0] = data->fd[0];
880 fd[1] = data->fd[1];
881 return 0;
884 static int disconnect_git(struct transport *transport)
886 struct git_transport_data *data = transport->data;
887 if (data->conn) {
888 if (data->got_remote_heads && !transport->stateless_rpc)
889 packet_flush(data->fd[1]);
890 close(data->fd[0]);
891 if (data->fd[1] >= 0)
892 close(data->fd[1]);
893 finish_connect(data->conn);
896 free(data);
897 return 0;
900 static struct transport_vtable taken_over_vtable = {
901 .get_refs_list = get_refs_via_connect,
902 .fetch_refs = fetch_refs_via_pack,
903 .push_refs = git_transport_push,
904 .disconnect = disconnect_git
907 void transport_take_over(struct transport *transport,
908 struct child_process *child)
910 struct git_transport_data *data;
912 if (!transport->smart_options)
913 BUG("taking over transport requires non-NULL "
914 "smart_options field.");
916 CALLOC_ARRAY(data, 1);
917 data->options = *transport->smart_options;
918 data->conn = child;
919 data->fd[0] = data->conn->out;
920 data->fd[1] = data->conn->in;
921 data->got_remote_heads = 0;
922 transport->data = data;
924 transport->vtable = &taken_over_vtable;
925 transport->smart_options = &(data->options);
927 transport->cannot_reuse = 1;
930 static int is_file(const char *url)
932 struct stat buf;
933 if (stat(url, &buf))
934 return 0;
935 return S_ISREG(buf.st_mode);
938 static int external_specification_len(const char *url)
940 return strchr(url, ':') - url;
943 static const struct string_list *protocol_whitelist(void)
945 static int enabled = -1;
946 static struct string_list allowed = STRING_LIST_INIT_DUP;
948 if (enabled < 0) {
949 const char *v = getenv("GIT_ALLOW_PROTOCOL");
950 if (v) {
951 string_list_split(&allowed, v, ':', -1);
952 string_list_sort(&allowed);
953 enabled = 1;
954 } else {
955 enabled = 0;
959 return enabled ? &allowed : NULL;
962 enum protocol_allow_config {
963 PROTOCOL_ALLOW_NEVER = 0,
964 PROTOCOL_ALLOW_USER_ONLY,
965 PROTOCOL_ALLOW_ALWAYS
968 static enum protocol_allow_config parse_protocol_config(const char *key,
969 const char *value)
971 if (!strcasecmp(value, "always"))
972 return PROTOCOL_ALLOW_ALWAYS;
973 else if (!strcasecmp(value, "never"))
974 return PROTOCOL_ALLOW_NEVER;
975 else if (!strcasecmp(value, "user"))
976 return PROTOCOL_ALLOW_USER_ONLY;
978 die(_("unknown value for config '%s': %s"), key, value);
981 static enum protocol_allow_config get_protocol_config(const char *type)
983 char *key = xstrfmt("protocol.%s.allow", type);
984 char *value;
986 /* first check the per-protocol config */
987 if (!git_config_get_string(key, &value)) {
988 enum protocol_allow_config ret =
989 parse_protocol_config(key, value);
990 free(key);
991 free(value);
992 return ret;
994 free(key);
996 /* if defined, fallback to user-defined default for unknown protocols */
997 if (!git_config_get_string("protocol.allow", &value)) {
998 enum protocol_allow_config ret =
999 parse_protocol_config("protocol.allow", value);
1000 free(value);
1001 return ret;
1004 /* fallback to built-in defaults */
1005 /* known safe */
1006 if (!strcmp(type, "http") ||
1007 !strcmp(type, "https") ||
1008 !strcmp(type, "git") ||
1009 !strcmp(type, "ssh") ||
1010 !strcmp(type, "file"))
1011 return PROTOCOL_ALLOW_ALWAYS;
1013 /* known scary; err on the side of caution */
1014 if (!strcmp(type, "ext"))
1015 return PROTOCOL_ALLOW_NEVER;
1017 /* unknown; by default let them be used only directly by the user */
1018 return PROTOCOL_ALLOW_USER_ONLY;
1021 int is_transport_allowed(const char *type, int from_user)
1023 const struct string_list *whitelist = protocol_whitelist();
1024 if (whitelist)
1025 return string_list_has_string(whitelist, type);
1027 switch (get_protocol_config(type)) {
1028 case PROTOCOL_ALLOW_ALWAYS:
1029 return 1;
1030 case PROTOCOL_ALLOW_NEVER:
1031 return 0;
1032 case PROTOCOL_ALLOW_USER_ONLY:
1033 if (from_user < 0)
1034 from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1035 return from_user;
1038 BUG("invalid protocol_allow_config type");
1041 void transport_check_allowed(const char *type)
1043 if (!is_transport_allowed(type, -1))
1044 die(_("transport '%s' not allowed"), type);
1047 static struct transport_vtable bundle_vtable = {
1048 .get_refs_list = get_refs_from_bundle,
1049 .fetch_refs = fetch_refs_from_bundle,
1050 .disconnect = close_bundle
1053 static struct transport_vtable builtin_smart_vtable = {
1054 .get_refs_list = get_refs_via_connect,
1055 .fetch_refs = fetch_refs_via_pack,
1056 .push_refs = git_transport_push,
1057 .connect = connect_git,
1058 .disconnect = disconnect_git
1061 struct transport *transport_get(struct remote *remote, const char *url)
1063 const char *helper;
1064 struct transport *ret = xcalloc(1, sizeof(*ret));
1066 ret->progress = isatty(2);
1067 string_list_init_dup(&ret->pack_lockfiles);
1069 if (!remote)
1070 BUG("No remote provided to transport_get()");
1072 ret->got_remote_refs = 0;
1073 ret->remote = remote;
1074 helper = remote->foreign_vcs;
1076 if (!url && remote->url)
1077 url = remote->url[0];
1078 ret->url = url;
1080 /* maybe it is a foreign URL? */
1081 if (url) {
1082 const char *p = url;
1084 while (is_urlschemechar(p == url, *p))
1085 p++;
1086 if (starts_with(p, "::"))
1087 helper = xstrndup(url, p - url);
1090 if (helper) {
1091 transport_helper_init(ret, helper);
1092 } else if (starts_with(url, "rsync:")) {
1093 die(_("git-over-rsync is no longer supported"));
1094 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
1095 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
1096 bundle_header_init(&data->header);
1097 transport_check_allowed("file");
1098 ret->data = data;
1099 ret->vtable = &bundle_vtable;
1100 ret->smart_options = NULL;
1101 } else if (!is_url(url)
1102 || starts_with(url, "file://")
1103 || starts_with(url, "git://")
1104 || starts_with(url, "ssh://")
1105 || starts_with(url, "git+ssh://") /* deprecated - do not use */
1106 || starts_with(url, "ssh+git://") /* deprecated - do not use */
1109 * These are builtin smart transports; "allowed" transports
1110 * will be checked individually in git_connect.
1112 struct git_transport_data *data = xcalloc(1, sizeof(*data));
1113 ret->data = data;
1114 ret->vtable = &builtin_smart_vtable;
1115 ret->smart_options = &(data->options);
1117 data->conn = NULL;
1118 data->got_remote_heads = 0;
1119 } else {
1120 /* Unknown protocol in URL. Pass to external handler. */
1121 int len = external_specification_len(url);
1122 char *handler = xmemdupz(url, len);
1123 transport_helper_init(ret, handler);
1126 if (ret->smart_options) {
1127 ret->smart_options->thin = 1;
1128 ret->smart_options->uploadpack = "git-upload-pack";
1129 if (remote->uploadpack)
1130 ret->smart_options->uploadpack = remote->uploadpack;
1131 ret->smart_options->receivepack = "git-receive-pack";
1132 if (remote->receivepack)
1133 ret->smart_options->receivepack = remote->receivepack;
1136 ret->hash_algo = &hash_algos[GIT_HASH_SHA1];
1138 return ret;
1141 const struct git_hash_algo *transport_get_hash_algo(struct transport *transport)
1143 return transport->hash_algo;
1146 int transport_set_option(struct transport *transport,
1147 const char *name, const char *value)
1149 int git_reports = 1, protocol_reports = 1;
1151 if (transport->smart_options)
1152 git_reports = set_git_option(transport->smart_options,
1153 name, value);
1155 if (transport->vtable->set_option)
1156 protocol_reports = transport->vtable->set_option(transport,
1157 name, value);
1159 /* If either report is 0, report 0 (success). */
1160 if (!git_reports || !protocol_reports)
1161 return 0;
1162 /* If either reports -1 (invalid value), report -1. */
1163 if ((git_reports == -1) || (protocol_reports == -1))
1164 return -1;
1165 /* Otherwise if both report unknown, report unknown. */
1166 return 1;
1169 void transport_set_verbosity(struct transport *transport, int verbosity,
1170 int force_progress)
1172 if (verbosity >= 1)
1173 transport->verbose = verbosity <= 3 ? verbosity : 3;
1174 if (verbosity < 0)
1175 transport->verbose = -1;
1178 * Rules used to determine whether to report progress (processing aborts
1179 * when a rule is satisfied):
1181 * . Report progress, if force_progress is 1 (ie. --progress).
1182 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1183 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1184 * . Report progress if isatty(2) is 1.
1186 if (force_progress >= 0)
1187 transport->progress = !!force_progress;
1188 else
1189 transport->progress = verbosity >= 0 && isatty(2);
1192 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
1194 int i;
1196 fprintf(stderr, _("The following submodule paths contain changes that can\n"
1197 "not be found on any remote:\n"));
1198 for (i = 0; i < needs_pushing->nr; i++)
1199 fprintf(stderr, " %s\n", needs_pushing->items[i].string);
1200 fprintf(stderr, _("\nPlease try\n\n"
1201 " git push --recurse-submodules=on-demand\n\n"
1202 "or cd to the path and use\n\n"
1203 " git push\n\n"
1204 "to push them to a remote.\n\n"));
1206 string_list_clear(needs_pushing, 0);
1208 die(_("Aborting."));
1211 static int run_pre_push_hook(struct transport *transport,
1212 struct ref *remote_refs)
1214 int ret = 0, x;
1215 struct ref *r;
1216 struct child_process proc = CHILD_PROCESS_INIT;
1217 struct strbuf buf;
1218 const char *hook_path = find_hook("pre-push");
1220 if (!hook_path)
1221 return 0;
1223 strvec_push(&proc.args, hook_path);
1224 strvec_push(&proc.args, transport->remote->name);
1225 strvec_push(&proc.args, transport->url);
1227 proc.in = -1;
1228 proc.trace2_hook_name = "pre-push";
1230 if (start_command(&proc)) {
1231 finish_command(&proc);
1232 return -1;
1235 sigchain_push(SIGPIPE, SIG_IGN);
1237 strbuf_init(&buf, 256);
1239 for (r = remote_refs; r; r = r->next) {
1240 if (!r->peer_ref) continue;
1241 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
1242 if (r->status == REF_STATUS_REJECT_STALE) continue;
1243 if (r->status == REF_STATUS_REJECT_REMOTE_UPDATED) continue;
1244 if (r->status == REF_STATUS_UPTODATE) continue;
1246 strbuf_reset(&buf);
1247 strbuf_addf( &buf, "%s %s %s %s\n",
1248 r->peer_ref->name, oid_to_hex(&r->new_oid),
1249 r->name, oid_to_hex(&r->old_oid));
1251 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
1252 /* We do not mind if a hook does not read all refs. */
1253 if (errno != EPIPE)
1254 ret = -1;
1255 break;
1259 strbuf_release(&buf);
1261 x = close(proc.in);
1262 if (!ret)
1263 ret = x;
1265 sigchain_pop(SIGPIPE);
1267 x = finish_command(&proc);
1268 if (!ret)
1269 ret = x;
1271 return ret;
1274 int transport_push(struct repository *r,
1275 struct transport *transport,
1276 struct refspec *rs, int flags,
1277 unsigned int *reject_reasons)
1279 *reject_reasons = 0;
1281 if (transport_color_config() < 0)
1282 return -1;
1284 if (transport->vtable->push_refs) {
1285 struct ref *remote_refs;
1286 struct ref *local_refs = get_local_heads();
1287 int match_flags = MATCH_REFS_NONE;
1288 int verbose = (transport->verbose > 0);
1289 int quiet = (transport->verbose < 0);
1290 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1291 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1292 int push_ret, ret, err;
1293 struct transport_ls_refs_options transport_options =
1294 TRANSPORT_LS_REFS_OPTIONS_INIT;
1296 if (check_push_refs(local_refs, rs) < 0)
1297 return -1;
1299 refspec_ref_prefixes(rs, &transport_options.ref_prefixes);
1301 trace2_region_enter("transport_push", "get_refs_list", r);
1302 remote_refs = transport->vtable->get_refs_list(transport, 1,
1303 &transport_options);
1304 trace2_region_leave("transport_push", "get_refs_list", r);
1306 transport_ls_refs_options_release(&transport_options);
1308 if (flags & TRANSPORT_PUSH_ALL)
1309 match_flags |= MATCH_REFS_ALL;
1310 if (flags & TRANSPORT_PUSH_MIRROR)
1311 match_flags |= MATCH_REFS_MIRROR;
1312 if (flags & TRANSPORT_PUSH_PRUNE)
1313 match_flags |= MATCH_REFS_PRUNE;
1314 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1315 match_flags |= MATCH_REFS_FOLLOW_TAGS;
1317 if (match_push_refs(local_refs, &remote_refs, rs, match_flags))
1318 return -1;
1320 if (transport->smart_options &&
1321 transport->smart_options->cas &&
1322 !is_empty_cas(transport->smart_options->cas))
1323 apply_push_cas(transport->smart_options->cas,
1324 transport->remote, remote_refs);
1326 set_ref_status_for_push(remote_refs,
1327 flags & TRANSPORT_PUSH_MIRROR,
1328 flags & TRANSPORT_PUSH_FORCE);
1330 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1331 if (run_pre_push_hook(transport, remote_refs))
1332 return -1;
1334 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1335 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1336 !is_bare_repository()) {
1337 struct ref *ref = remote_refs;
1338 struct oid_array commits = OID_ARRAY_INIT;
1340 trace2_region_enter("transport_push", "push_submodules", r);
1341 for (; ref; ref = ref->next)
1342 if (!is_null_oid(&ref->new_oid))
1343 oid_array_append(&commits,
1344 &ref->new_oid);
1346 if (!push_unpushed_submodules(r,
1347 &commits,
1348 transport->remote,
1350 transport->push_options,
1351 pretend)) {
1352 oid_array_clear(&commits);
1353 trace2_region_leave("transport_push", "push_submodules", r);
1354 die(_("failed to push all needed submodules"));
1356 oid_array_clear(&commits);
1357 trace2_region_leave("transport_push", "push_submodules", r);
1360 if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1361 ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1362 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1363 !pretend)) && !is_bare_repository()) {
1364 struct ref *ref = remote_refs;
1365 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1366 struct oid_array commits = OID_ARRAY_INIT;
1368 trace2_region_enter("transport_push", "check_submodules", r);
1369 for (; ref; ref = ref->next)
1370 if (!is_null_oid(&ref->new_oid))
1371 oid_array_append(&commits,
1372 &ref->new_oid);
1374 if (find_unpushed_submodules(r,
1375 &commits,
1376 transport->remote->name,
1377 &needs_pushing)) {
1378 oid_array_clear(&commits);
1379 trace2_region_leave("transport_push", "check_submodules", r);
1380 die_with_unpushed_submodules(&needs_pushing);
1382 string_list_clear(&needs_pushing, 0);
1383 oid_array_clear(&commits);
1384 trace2_region_leave("transport_push", "check_submodules", r);
1387 if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY)) {
1388 trace2_region_enter("transport_push", "push_refs", r);
1389 push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1390 trace2_region_leave("transport_push", "push_refs", r);
1391 } else
1392 push_ret = 0;
1393 err = push_had_errors(remote_refs);
1394 ret = push_ret | err;
1396 if (!quiet || err)
1397 transport_print_push_status(transport->url, remote_refs,
1398 verbose | porcelain, porcelain,
1399 reject_reasons);
1401 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1402 set_upstreams(transport, remote_refs, pretend);
1404 if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1405 TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1406 struct ref *ref;
1407 for (ref = remote_refs; ref; ref = ref->next)
1408 transport_update_tracking_ref(transport->remote, ref, verbose);
1411 if (porcelain && !push_ret)
1412 puts("Done");
1413 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1414 fprintf(stderr, "Everything up-to-date\n");
1416 return ret;
1418 return 1;
1421 const struct ref *transport_get_remote_refs(struct transport *transport,
1422 struct transport_ls_refs_options *transport_options)
1424 if (!transport->got_remote_refs) {
1425 transport->remote_refs =
1426 transport->vtable->get_refs_list(transport, 0,
1427 transport_options);
1428 transport->got_remote_refs = 1;
1431 return transport->remote_refs;
1434 void transport_ls_refs_options_release(struct transport_ls_refs_options *opts)
1436 strvec_clear(&opts->ref_prefixes);
1437 free((char *)opts->unborn_head_target);
1440 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1442 int rc;
1443 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1444 struct ref **heads = NULL;
1445 struct ref *rm;
1447 for (rm = refs; rm; rm = rm->next) {
1448 nr_refs++;
1449 if (rm->peer_ref &&
1450 !is_null_oid(&rm->old_oid) &&
1451 oideq(&rm->peer_ref->old_oid, &rm->old_oid))
1452 continue;
1453 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1454 heads[nr_heads++] = rm;
1457 if (!nr_heads) {
1459 * When deepening of a shallow repository is requested,
1460 * then local and remote refs are likely to still be equal.
1461 * Just feed them all to the fetch method in that case.
1462 * This condition shouldn't be met in a non-deepening fetch
1463 * (see builtin/fetch.c:quickfetch()).
1465 ALLOC_ARRAY(heads, nr_refs);
1466 for (rm = refs; rm; rm = rm->next)
1467 heads[nr_heads++] = rm;
1470 rc = transport->vtable->fetch_refs(transport, nr_heads, heads);
1472 free(heads);
1473 return rc;
1476 void transport_unlock_pack(struct transport *transport, unsigned int flags)
1478 int in_signal_handler = !!(flags & TRANSPORT_UNLOCK_PACK_IN_SIGNAL_HANDLER);
1479 int i;
1481 for (i = 0; i < transport->pack_lockfiles.nr; i++)
1482 if (in_signal_handler)
1483 unlink(transport->pack_lockfiles.items[i].string);
1484 else
1485 unlink_or_warn(transport->pack_lockfiles.items[i].string);
1486 if (!in_signal_handler)
1487 string_list_clear(&transport->pack_lockfiles, 0);
1490 int transport_connect(struct transport *transport, const char *name,
1491 const char *exec, int fd[2])
1493 if (transport->vtable->connect)
1494 return transport->vtable->connect(transport, name, exec, fd);
1495 else
1496 die(_("operation not supported by protocol"));
1499 int transport_disconnect(struct transport *transport)
1501 int ret = 0;
1502 if (transport->vtable->disconnect)
1503 ret = transport->vtable->disconnect(transport);
1504 if (transport->got_remote_refs)
1505 free_refs((void *)transport->remote_refs);
1506 free(transport);
1507 return ret;
1511 * Strip username (and password) from a URL and return
1512 * it in a newly allocated string.
1514 char *transport_anonymize_url(const char *url)
1516 char *scheme_prefix, *anon_part;
1517 size_t anon_len, prefix_len = 0;
1519 anon_part = strchr(url, '@');
1520 if (url_is_local_not_ssh(url) || !anon_part)
1521 goto literal_copy;
1523 anon_len = strlen(++anon_part);
1524 scheme_prefix = strstr(url, "://");
1525 if (!scheme_prefix) {
1526 if (!strchr(anon_part, ':'))
1527 /* cannot be "me@there:/path/name" */
1528 goto literal_copy;
1529 } else {
1530 const char *cp;
1531 /* make sure scheme is reasonable */
1532 for (cp = url; cp < scheme_prefix; cp++) {
1533 switch (*cp) {
1534 /* RFC 1738 2.1 */
1535 case '+': case '.': case '-':
1536 break; /* ok */
1537 default:
1538 if (isalnum(*cp))
1539 break;
1540 /* it isn't */
1541 goto literal_copy;
1544 /* @ past the first slash does not count */
1545 cp = strchr(scheme_prefix + 3, '/');
1546 if (cp && cp < anon_part)
1547 goto literal_copy;
1548 prefix_len = scheme_prefix - url + 3;
1550 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1551 (int)anon_len, anon_part);
1552 literal_copy:
1553 return xstrdup(url);