revisions API users: add "goto cleanup" for release_revisions()
[alt-git.git] / transport.c
blob70e9840a90e4cce41b4921fca38a472281bf3bec
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_REJECT_SHALLOW)) {
254 opts->reject_shallow = !!value;
255 return 0;
257 return 1;
260 static int connect_setup(struct transport *transport, int for_push)
262 struct git_transport_data *data = transport->data;
263 int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
265 if (data->conn)
266 return 0;
268 switch (transport->family) {
269 case TRANSPORT_FAMILY_ALL: break;
270 case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
271 case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
274 data->conn = git_connect(data->fd, transport->url,
275 for_push ? data->options.receivepack :
276 data->options.uploadpack,
277 flags);
279 return 0;
282 static void die_if_server_options(struct transport *transport)
284 if (!transport->server_options || !transport->server_options->nr)
285 return;
286 advise(_("see protocol.version in 'git help config' for more details"));
287 die(_("server options require protocol version 2 or later"));
291 * Obtains the protocol version from the transport and writes it to
292 * transport->data->version, first connecting if not already connected.
294 * If the protocol version is one that allows skipping the listing of remote
295 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
296 * this function returns NULL. Otherwise, this function returns the list of
297 * remote refs.
299 static struct ref *handshake(struct transport *transport, int for_push,
300 struct transport_ls_refs_options *options,
301 int must_list_refs)
303 struct git_transport_data *data = transport->data;
304 struct ref *refs = NULL;
305 struct packet_reader reader;
306 int sid_len;
307 const char *server_sid;
309 connect_setup(transport, for_push);
311 packet_reader_init(&reader, data->fd[0], NULL, 0,
312 PACKET_READ_CHOMP_NEWLINE |
313 PACKET_READ_GENTLE_ON_EOF |
314 PACKET_READ_DIE_ON_ERR_PACKET);
316 data->version = discover_version(&reader);
317 switch (data->version) {
318 case protocol_v2:
319 if (server_feature_v2("session-id", &server_sid))
320 trace2_data_string("transfer", NULL, "server-sid", server_sid);
321 if (must_list_refs)
322 get_remote_refs(data->fd[1], &reader, &refs, for_push,
323 options,
324 transport->server_options,
325 transport->stateless_rpc);
326 break;
327 case protocol_v1:
328 case protocol_v0:
329 die_if_server_options(transport);
330 get_remote_heads(&reader, &refs,
331 for_push ? REF_NORMAL : 0,
332 &data->extra_have,
333 &data->shallow);
334 server_sid = server_feature_value("session-id", &sid_len);
335 if (server_sid) {
336 char *sid = xstrndup(server_sid, sid_len);
337 trace2_data_string("transfer", NULL, "server-sid", sid);
338 free(sid);
340 break;
341 case protocol_unknown_version:
342 BUG("unknown protocol version");
344 data->got_remote_heads = 1;
345 transport->hash_algo = reader.hash_algo;
347 if (reader.line_peeked)
348 BUG("buffer must be empty at the end of handshake()");
350 return refs;
353 static struct ref *get_refs_via_connect(struct transport *transport, int for_push,
354 struct transport_ls_refs_options *options)
356 return handshake(transport, for_push, options, 1);
359 static int fetch_refs_via_pack(struct transport *transport,
360 int nr_heads, struct ref **to_fetch)
362 int ret = 0;
363 struct git_transport_data *data = transport->data;
364 struct ref *refs = NULL;
365 struct fetch_pack_args args;
366 struct ref *refs_tmp = NULL;
368 memset(&args, 0, sizeof(args));
369 args.uploadpack = data->options.uploadpack;
370 args.keep_pack = data->options.keep;
371 args.lock_pack = 1;
372 args.use_thin_pack = data->options.thin;
373 args.include_tag = data->options.followtags;
374 args.verbose = (transport->verbose > 1);
375 args.quiet = (transport->verbose < 0);
376 args.no_progress = !transport->progress;
377 args.depth = data->options.depth;
378 args.deepen_since = data->options.deepen_since;
379 args.deepen_not = data->options.deepen_not;
380 args.deepen_relative = data->options.deepen_relative;
381 args.check_self_contained_and_connected =
382 data->options.check_self_contained_and_connected;
383 args.cloning = transport->cloning;
384 args.update_shallow = data->options.update_shallow;
385 args.from_promisor = data->options.from_promisor;
386 args.filter_options = data->options.filter_options;
387 args.stateless_rpc = transport->stateless_rpc;
388 args.server_options = transport->server_options;
389 args.negotiation_tips = data->options.negotiation_tips;
390 args.reject_shallow_remote = transport->smart_options->reject_shallow;
392 if (!data->got_remote_heads) {
393 int i;
394 int must_list_refs = 0;
395 for (i = 0; i < nr_heads; i++) {
396 if (!to_fetch[i]->exact_oid) {
397 must_list_refs = 1;
398 break;
401 refs_tmp = handshake(transport, 0, NULL, must_list_refs);
404 if (data->version == protocol_unknown_version)
405 BUG("unknown protocol version");
406 else if (data->version <= protocol_v1)
407 die_if_server_options(transport);
409 if (data->options.acked_commits) {
410 if (data->version < protocol_v2) {
411 warning(_("--negotiate-only requires protocol v2"));
412 ret = -1;
413 } else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
414 warning(_("server does not support wait-for-done"));
415 ret = -1;
416 } else {
417 negotiate_using_fetch(data->options.negotiation_tips,
418 transport->server_options,
419 transport->stateless_rpc,
420 data->fd,
421 data->options.acked_commits);
422 ret = 0;
424 goto cleanup;
427 refs = fetch_pack(&args, data->fd,
428 refs_tmp ? refs_tmp : transport->remote_refs,
429 to_fetch, nr_heads, &data->shallow,
430 &transport->pack_lockfiles, data->version);
432 data->got_remote_heads = 0;
433 data->options.self_contained_and_connected =
434 args.self_contained_and_connected;
435 data->options.connectivity_checked = args.connectivity_checked;
437 if (refs == NULL)
438 ret = -1;
439 if (report_unmatched_refs(to_fetch, nr_heads))
440 ret = -1;
442 cleanup:
443 close(data->fd[0]);
444 if (data->fd[1] >= 0)
445 close(data->fd[1]);
446 if (finish_connect(data->conn))
447 ret = -1;
448 data->conn = NULL;
450 free_refs(refs_tmp);
451 free_refs(refs);
452 return ret;
455 static int push_had_errors(struct ref *ref)
457 for (; ref; ref = ref->next) {
458 switch (ref->status) {
459 case REF_STATUS_NONE:
460 case REF_STATUS_UPTODATE:
461 case REF_STATUS_OK:
462 break;
463 default:
464 return 1;
467 return 0;
470 int transport_refs_pushed(struct ref *ref)
472 for (; ref; ref = ref->next) {
473 switch(ref->status) {
474 case REF_STATUS_NONE:
475 case REF_STATUS_UPTODATE:
476 break;
477 default:
478 return 1;
481 return 0;
484 static void update_one_tracking_ref(struct remote *remote, char *refname,
485 struct object_id *new_oid, int deletion,
486 int verbose)
488 struct refspec_item rs;
490 memset(&rs, 0, sizeof(rs));
491 rs.src = refname;
492 rs.dst = NULL;
494 if (!remote_find_tracking(remote, &rs)) {
495 if (verbose)
496 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
497 if (deletion)
498 delete_ref(NULL, rs.dst, NULL, 0);
499 else
500 update_ref("update by push", rs.dst, new_oid,
501 NULL, 0, 0);
502 free(rs.dst);
506 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
508 char *refname;
509 struct object_id *new_oid;
510 struct ref_push_report *report;
512 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
513 return;
515 report = ref->report;
516 if (!report)
517 update_one_tracking_ref(remote, ref->name, &ref->new_oid,
518 ref->deletion, verbose);
519 else
520 for (; report; report = report->next) {
521 refname = report->ref_name ? (char *)report->ref_name : ref->name;
522 new_oid = report->new_oid ? report->new_oid : &ref->new_oid;
523 update_one_tracking_ref(remote, refname, new_oid,
524 is_null_oid(new_oid), verbose);
528 static void print_ref_status(char flag, const char *summary,
529 struct ref *to, struct ref *from, const char *msg,
530 struct ref_push_report *report,
531 int porcelain, int summary_width)
533 const char *to_name;
535 if (report && report->ref_name)
536 to_name = report->ref_name;
537 else
538 to_name = to->name;
540 if (porcelain) {
541 if (from)
542 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to_name);
543 else
544 fprintf(stdout, "%c\t:%s\t", flag, to_name);
545 if (msg)
546 fprintf(stdout, "%s (%s)\n", summary, msg);
547 else
548 fprintf(stdout, "%s\n", summary);
549 } else {
550 const char *red = "", *reset = "";
551 if (push_had_errors(to)) {
552 red = transport_get_color(TRANSPORT_COLOR_REJECTED);
553 reset = transport_get_color(TRANSPORT_COLOR_RESET);
555 fprintf(stderr, " %s%c %-*s%s ", red, flag, summary_width,
556 summary, reset);
557 if (from)
558 fprintf(stderr, "%s -> %s",
559 prettify_refname(from->name),
560 prettify_refname(to_name));
561 else
562 fputs(prettify_refname(to_name), stderr);
563 if (msg) {
564 fputs(" (", stderr);
565 fputs(msg, stderr);
566 fputc(')', stderr);
568 fputc('\n', stderr);
572 static void print_ok_ref_status(struct ref *ref,
573 struct ref_push_report *report,
574 int porcelain, int summary_width)
576 struct object_id *old_oid;
577 struct object_id *new_oid;
578 const char *ref_name;
579 int forced_update;
581 if (report && report->old_oid)
582 old_oid = report->old_oid;
583 else
584 old_oid = &ref->old_oid;
585 if (report && report->new_oid)
586 new_oid = report->new_oid;
587 else
588 new_oid = &ref->new_oid;
589 if (report && report->forced_update)
590 forced_update = report->forced_update;
591 else
592 forced_update = ref->forced_update;
593 if (report && report->ref_name)
594 ref_name = report->ref_name;
595 else
596 ref_name = ref->name;
598 if (ref->deletion)
599 print_ref_status('-', "[deleted]", ref, NULL, NULL,
600 report, porcelain, summary_width);
601 else if (is_null_oid(old_oid))
602 print_ref_status('*',
603 (starts_with(ref_name, "refs/tags/")
604 ? "[new tag]"
605 : (starts_with(ref_name, "refs/heads/")
606 ? "[new branch]"
607 : "[new reference]")),
608 ref, ref->peer_ref, NULL,
609 report, porcelain, summary_width);
610 else {
611 struct strbuf quickref = STRBUF_INIT;
612 char type;
613 const char *msg;
615 strbuf_add_unique_abbrev(&quickref, old_oid,
616 DEFAULT_ABBREV);
617 if (forced_update) {
618 strbuf_addstr(&quickref, "...");
619 type = '+';
620 msg = "forced update";
621 } else {
622 strbuf_addstr(&quickref, "..");
623 type = ' ';
624 msg = NULL;
626 strbuf_add_unique_abbrev(&quickref, new_oid,
627 DEFAULT_ABBREV);
629 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
630 report, porcelain, summary_width);
631 strbuf_release(&quickref);
635 static int print_one_push_report(struct ref *ref, const char *dest, int count,
636 struct ref_push_report *report,
637 int porcelain, int summary_width)
639 if (!count) {
640 char *url = transport_anonymize_url(dest);
641 fprintf(porcelain ? stdout : stderr, "To %s\n", url);
642 free(url);
645 switch(ref->status) {
646 case REF_STATUS_NONE:
647 print_ref_status('X', "[no match]", ref, NULL, NULL,
648 report, porcelain, summary_width);
649 break;
650 case REF_STATUS_REJECT_NODELETE:
651 print_ref_status('!', "[rejected]", ref, NULL,
652 "remote does not support deleting refs",
653 report, porcelain, summary_width);
654 break;
655 case REF_STATUS_UPTODATE:
656 print_ref_status('=', "[up to date]", ref,
657 ref->peer_ref, NULL,
658 report, porcelain, summary_width);
659 break;
660 case REF_STATUS_REJECT_NONFASTFORWARD:
661 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
662 "non-fast-forward",
663 report, porcelain, summary_width);
664 break;
665 case REF_STATUS_REJECT_ALREADY_EXISTS:
666 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
667 "already exists",
668 report, porcelain, summary_width);
669 break;
670 case REF_STATUS_REJECT_FETCH_FIRST:
671 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
672 "fetch first",
673 report, porcelain, summary_width);
674 break;
675 case REF_STATUS_REJECT_NEEDS_FORCE:
676 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
677 "needs force",
678 report, porcelain, summary_width);
679 break;
680 case REF_STATUS_REJECT_STALE:
681 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
682 "stale info",
683 report, porcelain, summary_width);
684 break;
685 case REF_STATUS_REJECT_REMOTE_UPDATED:
686 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
687 "remote ref updated since checkout",
688 report, porcelain, summary_width);
689 break;
690 case REF_STATUS_REJECT_SHALLOW:
691 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
692 "new shallow roots not allowed",
693 report, porcelain, summary_width);
694 break;
695 case REF_STATUS_REMOTE_REJECT:
696 print_ref_status('!', "[remote rejected]", ref,
697 ref->deletion ? NULL : ref->peer_ref,
698 ref->remote_status,
699 report, porcelain, summary_width);
700 break;
701 case REF_STATUS_EXPECTING_REPORT:
702 print_ref_status('!', "[remote failure]", ref,
703 ref->deletion ? NULL : ref->peer_ref,
704 "remote failed to report status",
705 report, porcelain, summary_width);
706 break;
707 case REF_STATUS_ATOMIC_PUSH_FAILED:
708 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
709 "atomic push failed",
710 report, porcelain, summary_width);
711 break;
712 case REF_STATUS_OK:
713 print_ok_ref_status(ref, report, porcelain, summary_width);
714 break;
717 return 1;
720 static int print_one_push_status(struct ref *ref, const char *dest, int count,
721 int porcelain, int summary_width)
723 struct ref_push_report *report;
724 int n = 0;
726 if (!ref->report)
727 return print_one_push_report(ref, dest, count,
728 NULL, porcelain, summary_width);
730 for (report = ref->report; report; report = report->next)
731 print_one_push_report(ref, dest, count + n++,
732 report, porcelain, summary_width);
733 return n;
736 static int measure_abbrev(const struct object_id *oid, int sofar)
738 char hex[GIT_MAX_HEXSZ + 1];
739 int w = find_unique_abbrev_r(hex, oid, DEFAULT_ABBREV);
741 return (w < sofar) ? sofar : w;
744 int transport_summary_width(const struct ref *refs)
746 int maxw = -1;
748 for (; refs; refs = refs->next) {
749 maxw = measure_abbrev(&refs->old_oid, maxw);
750 maxw = measure_abbrev(&refs->new_oid, maxw);
752 if (maxw < 0)
753 maxw = FALLBACK_DEFAULT_ABBREV;
754 return (2 * maxw + 3);
757 void transport_print_push_status(const char *dest, struct ref *refs,
758 int verbose, int porcelain, unsigned int *reject_reasons)
760 struct ref *ref;
761 int n = 0;
762 char *head;
763 int summary_width = transport_summary_width(refs);
765 if (transport_color_config() < 0)
766 warning(_("could not parse transport.color.* config"));
768 head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
770 if (verbose) {
771 for (ref = refs; ref; ref = ref->next)
772 if (ref->status == REF_STATUS_UPTODATE)
773 n += print_one_push_status(ref, dest, n,
774 porcelain, summary_width);
777 for (ref = refs; ref; ref = ref->next)
778 if (ref->status == REF_STATUS_OK)
779 n += print_one_push_status(ref, dest, n,
780 porcelain, summary_width);
782 *reject_reasons = 0;
783 for (ref = refs; ref; ref = ref->next) {
784 if (ref->status != REF_STATUS_NONE &&
785 ref->status != REF_STATUS_UPTODATE &&
786 ref->status != REF_STATUS_OK)
787 n += print_one_push_status(ref, dest, n,
788 porcelain, summary_width);
789 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
790 if (head != NULL && !strcmp(head, ref->name))
791 *reject_reasons |= REJECT_NON_FF_HEAD;
792 else
793 *reject_reasons |= REJECT_NON_FF_OTHER;
794 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
795 *reject_reasons |= REJECT_ALREADY_EXISTS;
796 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
797 *reject_reasons |= REJECT_FETCH_FIRST;
798 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
799 *reject_reasons |= REJECT_NEEDS_FORCE;
800 } else if (ref->status == REF_STATUS_REJECT_REMOTE_UPDATED) {
801 *reject_reasons |= REJECT_REF_NEEDS_UPDATE;
804 free(head);
807 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
809 struct git_transport_data *data = transport->data;
810 struct send_pack_args args;
811 int ret = 0;
813 if (transport_color_config() < 0)
814 return -1;
816 if (!data->got_remote_heads)
817 get_refs_via_connect(transport, 1, NULL);
819 memset(&args, 0, sizeof(args));
820 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
821 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
822 args.use_thin_pack = data->options.thin;
823 args.verbose = (transport->verbose > 0);
824 args.quiet = (transport->verbose < 0);
825 args.progress = transport->progress;
826 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
827 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
828 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
829 args.push_options = transport->push_options;
830 args.url = transport->url;
832 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
833 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
834 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
835 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
836 else
837 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
839 switch (data->version) {
840 case protocol_v2:
841 die(_("support for protocol v2 not implemented yet"));
842 break;
843 case protocol_v1:
844 case protocol_v0:
845 ret = send_pack(&args, data->fd, data->conn, remote_refs,
846 &data->extra_have);
847 break;
848 case protocol_unknown_version:
849 BUG("unknown protocol version");
852 close(data->fd[1]);
853 close(data->fd[0]);
855 * Atomic push may abort the connection early and close the pipe,
856 * which may cause an error for `finish_connect()`. Ignore this error
857 * for atomic git-push.
859 if (ret || args.atomic)
860 finish_connect(data->conn);
861 else
862 ret = finish_connect(data->conn);
863 data->conn = NULL;
864 data->got_remote_heads = 0;
866 return ret;
869 static int connect_git(struct transport *transport, const char *name,
870 const char *executable, int fd[2])
872 struct git_transport_data *data = transport->data;
873 data->conn = git_connect(data->fd, transport->url,
874 executable, 0);
875 fd[0] = data->fd[0];
876 fd[1] = data->fd[1];
877 return 0;
880 static int disconnect_git(struct transport *transport)
882 struct git_transport_data *data = transport->data;
883 if (data->conn) {
884 if (data->got_remote_heads && !transport->stateless_rpc)
885 packet_flush(data->fd[1]);
886 close(data->fd[0]);
887 if (data->fd[1] >= 0)
888 close(data->fd[1]);
889 finish_connect(data->conn);
892 free(data);
893 return 0;
896 static struct transport_vtable taken_over_vtable = {
897 .get_refs_list = get_refs_via_connect,
898 .fetch_refs = fetch_refs_via_pack,
899 .push_refs = git_transport_push,
900 .disconnect = disconnect_git
903 void transport_take_over(struct transport *transport,
904 struct child_process *child)
906 struct git_transport_data *data;
908 if (!transport->smart_options)
909 BUG("taking over transport requires non-NULL "
910 "smart_options field.");
912 CALLOC_ARRAY(data, 1);
913 data->options = *transport->smart_options;
914 data->conn = child;
915 data->fd[0] = data->conn->out;
916 data->fd[1] = data->conn->in;
917 data->got_remote_heads = 0;
918 transport->data = data;
920 transport->vtable = &taken_over_vtable;
921 transport->smart_options = &(data->options);
923 transport->cannot_reuse = 1;
926 static int is_file(const char *url)
928 struct stat buf;
929 if (stat(url, &buf))
930 return 0;
931 return S_ISREG(buf.st_mode);
934 static int external_specification_len(const char *url)
936 return strchr(url, ':') - url;
939 static const struct string_list *protocol_whitelist(void)
941 static int enabled = -1;
942 static struct string_list allowed = STRING_LIST_INIT_DUP;
944 if (enabled < 0) {
945 const char *v = getenv("GIT_ALLOW_PROTOCOL");
946 if (v) {
947 string_list_split(&allowed, v, ':', -1);
948 string_list_sort(&allowed);
949 enabled = 1;
950 } else {
951 enabled = 0;
955 return enabled ? &allowed : NULL;
958 enum protocol_allow_config {
959 PROTOCOL_ALLOW_NEVER = 0,
960 PROTOCOL_ALLOW_USER_ONLY,
961 PROTOCOL_ALLOW_ALWAYS
964 static enum protocol_allow_config parse_protocol_config(const char *key,
965 const char *value)
967 if (!strcasecmp(value, "always"))
968 return PROTOCOL_ALLOW_ALWAYS;
969 else if (!strcasecmp(value, "never"))
970 return PROTOCOL_ALLOW_NEVER;
971 else if (!strcasecmp(value, "user"))
972 return PROTOCOL_ALLOW_USER_ONLY;
974 die(_("unknown value for config '%s': %s"), key, value);
977 static enum protocol_allow_config get_protocol_config(const char *type)
979 char *key = xstrfmt("protocol.%s.allow", type);
980 char *value;
982 /* first check the per-protocol config */
983 if (!git_config_get_string(key, &value)) {
984 enum protocol_allow_config ret =
985 parse_protocol_config(key, value);
986 free(key);
987 free(value);
988 return ret;
990 free(key);
992 /* if defined, fallback to user-defined default for unknown protocols */
993 if (!git_config_get_string("protocol.allow", &value)) {
994 enum protocol_allow_config ret =
995 parse_protocol_config("protocol.allow", value);
996 free(value);
997 return ret;
1000 /* fallback to built-in defaults */
1001 /* known safe */
1002 if (!strcmp(type, "http") ||
1003 !strcmp(type, "https") ||
1004 !strcmp(type, "git") ||
1005 !strcmp(type, "ssh") ||
1006 !strcmp(type, "file"))
1007 return PROTOCOL_ALLOW_ALWAYS;
1009 /* known scary; err on the side of caution */
1010 if (!strcmp(type, "ext"))
1011 return PROTOCOL_ALLOW_NEVER;
1013 /* unknown; by default let them be used only directly by the user */
1014 return PROTOCOL_ALLOW_USER_ONLY;
1017 int is_transport_allowed(const char *type, int from_user)
1019 const struct string_list *whitelist = protocol_whitelist();
1020 if (whitelist)
1021 return string_list_has_string(whitelist, type);
1023 switch (get_protocol_config(type)) {
1024 case PROTOCOL_ALLOW_ALWAYS:
1025 return 1;
1026 case PROTOCOL_ALLOW_NEVER:
1027 return 0;
1028 case PROTOCOL_ALLOW_USER_ONLY:
1029 if (from_user < 0)
1030 from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1031 return from_user;
1034 BUG("invalid protocol_allow_config type");
1037 void transport_check_allowed(const char *type)
1039 if (!is_transport_allowed(type, -1))
1040 die(_("transport '%s' not allowed"), type);
1043 static struct transport_vtable bundle_vtable = {
1044 .get_refs_list = get_refs_from_bundle,
1045 .fetch_refs = fetch_refs_from_bundle,
1046 .disconnect = close_bundle
1049 static struct transport_vtable builtin_smart_vtable = {
1050 .get_refs_list = get_refs_via_connect,
1051 .fetch_refs = fetch_refs_via_pack,
1052 .push_refs = git_transport_push,
1053 .connect = connect_git,
1054 .disconnect = disconnect_git
1057 struct transport *transport_get(struct remote *remote, const char *url)
1059 const char *helper;
1060 struct transport *ret = xcalloc(1, sizeof(*ret));
1062 ret->progress = isatty(2);
1063 string_list_init_dup(&ret->pack_lockfiles);
1065 if (!remote)
1066 BUG("No remote provided to transport_get()");
1068 ret->got_remote_refs = 0;
1069 ret->remote = remote;
1070 helper = remote->foreign_vcs;
1072 if (!url && remote->url)
1073 url = remote->url[0];
1074 ret->url = url;
1076 /* maybe it is a foreign URL? */
1077 if (url) {
1078 const char *p = url;
1080 while (is_urlschemechar(p == url, *p))
1081 p++;
1082 if (starts_with(p, "::"))
1083 helper = xstrndup(url, p - url);
1086 if (helper) {
1087 transport_helper_init(ret, helper);
1088 } else if (starts_with(url, "rsync:")) {
1089 die(_("git-over-rsync is no longer supported"));
1090 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
1091 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
1092 bundle_header_init(&data->header);
1093 transport_check_allowed("file");
1094 ret->data = data;
1095 ret->vtable = &bundle_vtable;
1096 ret->smart_options = NULL;
1097 } else if (!is_url(url)
1098 || starts_with(url, "file://")
1099 || starts_with(url, "git://")
1100 || starts_with(url, "ssh://")
1101 || starts_with(url, "git+ssh://") /* deprecated - do not use */
1102 || starts_with(url, "ssh+git://") /* deprecated - do not use */
1105 * These are builtin smart transports; "allowed" transports
1106 * will be checked individually in git_connect.
1108 struct git_transport_data *data = xcalloc(1, sizeof(*data));
1109 ret->data = data;
1110 ret->vtable = &builtin_smart_vtable;
1111 ret->smart_options = &(data->options);
1113 data->conn = NULL;
1114 data->got_remote_heads = 0;
1115 } else {
1116 /* Unknown protocol in URL. Pass to external handler. */
1117 int len = external_specification_len(url);
1118 char *handler = xmemdupz(url, len);
1119 transport_helper_init(ret, handler);
1122 if (ret->smart_options) {
1123 ret->smart_options->thin = 1;
1124 ret->smart_options->uploadpack = "git-upload-pack";
1125 if (remote->uploadpack)
1126 ret->smart_options->uploadpack = remote->uploadpack;
1127 ret->smart_options->receivepack = "git-receive-pack";
1128 if (remote->receivepack)
1129 ret->smart_options->receivepack = remote->receivepack;
1132 ret->hash_algo = &hash_algos[GIT_HASH_SHA1];
1134 return ret;
1137 const struct git_hash_algo *transport_get_hash_algo(struct transport *transport)
1139 return transport->hash_algo;
1142 int transport_set_option(struct transport *transport,
1143 const char *name, const char *value)
1145 int git_reports = 1, protocol_reports = 1;
1147 if (transport->smart_options)
1148 git_reports = set_git_option(transport->smart_options,
1149 name, value);
1151 if (transport->vtable->set_option)
1152 protocol_reports = transport->vtable->set_option(transport,
1153 name, value);
1155 /* If either report is 0, report 0 (success). */
1156 if (!git_reports || !protocol_reports)
1157 return 0;
1158 /* If either reports -1 (invalid value), report -1. */
1159 if ((git_reports == -1) || (protocol_reports == -1))
1160 return -1;
1161 /* Otherwise if both report unknown, report unknown. */
1162 return 1;
1165 void transport_set_verbosity(struct transport *transport, int verbosity,
1166 int force_progress)
1168 if (verbosity >= 1)
1169 transport->verbose = verbosity <= 3 ? verbosity : 3;
1170 if (verbosity < 0)
1171 transport->verbose = -1;
1174 * Rules used to determine whether to report progress (processing aborts
1175 * when a rule is satisfied):
1177 * . Report progress, if force_progress is 1 (ie. --progress).
1178 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1179 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1180 * . Report progress if isatty(2) is 1.
1182 if (force_progress >= 0)
1183 transport->progress = !!force_progress;
1184 else
1185 transport->progress = verbosity >= 0 && isatty(2);
1188 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
1190 int i;
1192 fprintf(stderr, _("The following submodule paths contain changes that can\n"
1193 "not be found on any remote:\n"));
1194 for (i = 0; i < needs_pushing->nr; i++)
1195 fprintf(stderr, " %s\n", needs_pushing->items[i].string);
1196 fprintf(stderr, _("\nPlease try\n\n"
1197 " git push --recurse-submodules=on-demand\n\n"
1198 "or cd to the path and use\n\n"
1199 " git push\n\n"
1200 "to push them to a remote.\n\n"));
1202 string_list_clear(needs_pushing, 0);
1204 die(_("Aborting."));
1207 static int run_pre_push_hook(struct transport *transport,
1208 struct ref *remote_refs)
1210 int ret = 0, x;
1211 struct ref *r;
1212 struct child_process proc = CHILD_PROCESS_INIT;
1213 struct strbuf buf;
1214 const char *hook_path = find_hook("pre-push");
1216 if (!hook_path)
1217 return 0;
1219 strvec_push(&proc.args, hook_path);
1220 strvec_push(&proc.args, transport->remote->name);
1221 strvec_push(&proc.args, transport->url);
1223 proc.in = -1;
1224 proc.trace2_hook_name = "pre-push";
1226 if (start_command(&proc)) {
1227 finish_command(&proc);
1228 return -1;
1231 sigchain_push(SIGPIPE, SIG_IGN);
1233 strbuf_init(&buf, 256);
1235 for (r = remote_refs; r; r = r->next) {
1236 if (!r->peer_ref) continue;
1237 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
1238 if (r->status == REF_STATUS_REJECT_STALE) continue;
1239 if (r->status == REF_STATUS_REJECT_REMOTE_UPDATED) continue;
1240 if (r->status == REF_STATUS_UPTODATE) continue;
1242 strbuf_reset(&buf);
1243 strbuf_addf( &buf, "%s %s %s %s\n",
1244 r->peer_ref->name, oid_to_hex(&r->new_oid),
1245 r->name, oid_to_hex(&r->old_oid));
1247 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
1248 /* We do not mind if a hook does not read all refs. */
1249 if (errno != EPIPE)
1250 ret = -1;
1251 break;
1255 strbuf_release(&buf);
1257 x = close(proc.in);
1258 if (!ret)
1259 ret = x;
1261 sigchain_pop(SIGPIPE);
1263 x = finish_command(&proc);
1264 if (!ret)
1265 ret = x;
1267 return ret;
1270 int transport_push(struct repository *r,
1271 struct transport *transport,
1272 struct refspec *rs, int flags,
1273 unsigned int *reject_reasons)
1275 *reject_reasons = 0;
1277 if (transport_color_config() < 0)
1278 return -1;
1280 if (transport->vtable->push_refs) {
1281 struct ref *remote_refs;
1282 struct ref *local_refs = get_local_heads();
1283 int match_flags = MATCH_REFS_NONE;
1284 int verbose = (transport->verbose > 0);
1285 int quiet = (transport->verbose < 0);
1286 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1287 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1288 int push_ret, ret, err;
1289 struct transport_ls_refs_options transport_options =
1290 TRANSPORT_LS_REFS_OPTIONS_INIT;
1292 if (check_push_refs(local_refs, rs) < 0)
1293 return -1;
1295 refspec_ref_prefixes(rs, &transport_options.ref_prefixes);
1297 trace2_region_enter("transport_push", "get_refs_list", r);
1298 remote_refs = transport->vtable->get_refs_list(transport, 1,
1299 &transport_options);
1300 trace2_region_leave("transport_push", "get_refs_list", r);
1302 transport_ls_refs_options_release(&transport_options);
1304 if (flags & TRANSPORT_PUSH_ALL)
1305 match_flags |= MATCH_REFS_ALL;
1306 if (flags & TRANSPORT_PUSH_MIRROR)
1307 match_flags |= MATCH_REFS_MIRROR;
1308 if (flags & TRANSPORT_PUSH_PRUNE)
1309 match_flags |= MATCH_REFS_PRUNE;
1310 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1311 match_flags |= MATCH_REFS_FOLLOW_TAGS;
1313 if (match_push_refs(local_refs, &remote_refs, rs, match_flags))
1314 return -1;
1316 if (transport->smart_options &&
1317 transport->smart_options->cas &&
1318 !is_empty_cas(transport->smart_options->cas))
1319 apply_push_cas(transport->smart_options->cas,
1320 transport->remote, remote_refs);
1322 set_ref_status_for_push(remote_refs,
1323 flags & TRANSPORT_PUSH_MIRROR,
1324 flags & TRANSPORT_PUSH_FORCE);
1326 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1327 if (run_pre_push_hook(transport, remote_refs))
1328 return -1;
1330 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1331 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1332 !is_bare_repository()) {
1333 struct ref *ref = remote_refs;
1334 struct oid_array commits = OID_ARRAY_INIT;
1336 trace2_region_enter("transport_push", "push_submodules", r);
1337 for (; ref; ref = ref->next)
1338 if (!is_null_oid(&ref->new_oid))
1339 oid_array_append(&commits,
1340 &ref->new_oid);
1342 if (!push_unpushed_submodules(r,
1343 &commits,
1344 transport->remote,
1346 transport->push_options,
1347 pretend)) {
1348 oid_array_clear(&commits);
1349 trace2_region_leave("transport_push", "push_submodules", r);
1350 die(_("failed to push all needed submodules"));
1352 oid_array_clear(&commits);
1353 trace2_region_leave("transport_push", "push_submodules", r);
1356 if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1357 ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1358 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1359 !pretend)) && !is_bare_repository()) {
1360 struct ref *ref = remote_refs;
1361 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1362 struct oid_array commits = OID_ARRAY_INIT;
1364 trace2_region_enter("transport_push", "check_submodules", r);
1365 for (; ref; ref = ref->next)
1366 if (!is_null_oid(&ref->new_oid))
1367 oid_array_append(&commits,
1368 &ref->new_oid);
1370 if (find_unpushed_submodules(r,
1371 &commits,
1372 transport->remote->name,
1373 &needs_pushing)) {
1374 oid_array_clear(&commits);
1375 trace2_region_leave("transport_push", "check_submodules", r);
1376 die_with_unpushed_submodules(&needs_pushing);
1378 string_list_clear(&needs_pushing, 0);
1379 oid_array_clear(&commits);
1380 trace2_region_leave("transport_push", "check_submodules", r);
1383 if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY)) {
1384 trace2_region_enter("transport_push", "push_refs", r);
1385 push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1386 trace2_region_leave("transport_push", "push_refs", r);
1387 } else
1388 push_ret = 0;
1389 err = push_had_errors(remote_refs);
1390 ret = push_ret | err;
1392 if (!quiet || err)
1393 transport_print_push_status(transport->url, remote_refs,
1394 verbose | porcelain, porcelain,
1395 reject_reasons);
1397 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1398 set_upstreams(transport, remote_refs, pretend);
1400 if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1401 TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1402 struct ref *ref;
1403 for (ref = remote_refs; ref; ref = ref->next)
1404 transport_update_tracking_ref(transport->remote, ref, verbose);
1407 if (porcelain && !push_ret)
1408 puts("Done");
1409 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1410 fprintf(stderr, "Everything up-to-date\n");
1412 return ret;
1414 return 1;
1417 const struct ref *transport_get_remote_refs(struct transport *transport,
1418 struct transport_ls_refs_options *transport_options)
1420 if (!transport->got_remote_refs) {
1421 transport->remote_refs =
1422 transport->vtable->get_refs_list(transport, 0,
1423 transport_options);
1424 transport->got_remote_refs = 1;
1427 return transport->remote_refs;
1430 void transport_ls_refs_options_release(struct transport_ls_refs_options *opts)
1432 strvec_clear(&opts->ref_prefixes);
1433 free((char *)opts->unborn_head_target);
1436 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1438 int rc;
1439 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1440 struct ref **heads = NULL;
1441 struct ref *rm;
1443 for (rm = refs; rm; rm = rm->next) {
1444 nr_refs++;
1445 if (rm->peer_ref &&
1446 !is_null_oid(&rm->old_oid) &&
1447 oideq(&rm->peer_ref->old_oid, &rm->old_oid))
1448 continue;
1449 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1450 heads[nr_heads++] = rm;
1453 if (!nr_heads) {
1455 * When deepening of a shallow repository is requested,
1456 * then local and remote refs are likely to still be equal.
1457 * Just feed them all to the fetch method in that case.
1458 * This condition shouldn't be met in a non-deepening fetch
1459 * (see builtin/fetch.c:quickfetch()).
1461 ALLOC_ARRAY(heads, nr_refs);
1462 for (rm = refs; rm; rm = rm->next)
1463 heads[nr_heads++] = rm;
1466 rc = transport->vtable->fetch_refs(transport, nr_heads, heads);
1468 free(heads);
1469 return rc;
1472 void transport_unlock_pack(struct transport *transport, unsigned int flags)
1474 int in_signal_handler = !!(flags & TRANSPORT_UNLOCK_PACK_IN_SIGNAL_HANDLER);
1475 int i;
1477 for (i = 0; i < transport->pack_lockfiles.nr; i++)
1478 if (in_signal_handler)
1479 unlink(transport->pack_lockfiles.items[i].string);
1480 else
1481 unlink_or_warn(transport->pack_lockfiles.items[i].string);
1482 if (!in_signal_handler)
1483 string_list_clear(&transport->pack_lockfiles, 0);
1486 int transport_connect(struct transport *transport, const char *name,
1487 const char *exec, int fd[2])
1489 if (transport->vtable->connect)
1490 return transport->vtable->connect(transport, name, exec, fd);
1491 else
1492 die(_("operation not supported by protocol"));
1495 int transport_disconnect(struct transport *transport)
1497 int ret = 0;
1498 if (transport->vtable->disconnect)
1499 ret = transport->vtable->disconnect(transport);
1500 if (transport->got_remote_refs)
1501 free_refs((void *)transport->remote_refs);
1502 free(transport);
1503 return ret;
1507 * Strip username (and password) from a URL and return
1508 * it in a newly allocated string.
1510 char *transport_anonymize_url(const char *url)
1512 char *scheme_prefix, *anon_part;
1513 size_t anon_len, prefix_len = 0;
1515 anon_part = strchr(url, '@');
1516 if (url_is_local_not_ssh(url) || !anon_part)
1517 goto literal_copy;
1519 anon_len = strlen(++anon_part);
1520 scheme_prefix = strstr(url, "://");
1521 if (!scheme_prefix) {
1522 if (!strchr(anon_part, ':'))
1523 /* cannot be "me@there:/path/name" */
1524 goto literal_copy;
1525 } else {
1526 const char *cp;
1527 /* make sure scheme is reasonable */
1528 for (cp = url; cp < scheme_prefix; cp++) {
1529 switch (*cp) {
1530 /* RFC 1738 2.1 */
1531 case '+': case '.': case '-':
1532 break; /* ok */
1533 default:
1534 if (isalnum(*cp))
1535 break;
1536 /* it isn't */
1537 goto literal_copy;
1540 /* @ past the first slash does not count */
1541 cp = strchr(scheme_prefix + 3, '/');
1542 if (cp && cp < anon_part)
1543 goto literal_copy;
1544 prefix_len = scheme_prefix - url + 3;
1546 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1547 (int)anon_len, anon_part);
1548 literal_copy:
1549 return xstrdup(url);