Merge branch 'l10n-de-2.38-rnd3' of github.com:ralfth/git
[alt-git.git] / transport.c
blobf78e29058fc272d2345c9d4c7d4026da9e1b1bc2
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 UNUSED)
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 list_objects_filter_copy(&args.filter_options,
390 &data->options.filter_options);
391 args.refetch = data->options.refetch;
392 args.stateless_rpc = transport->stateless_rpc;
393 args.server_options = transport->server_options;
394 args.negotiation_tips = data->options.negotiation_tips;
395 args.reject_shallow_remote = transport->smart_options->reject_shallow;
397 if (!data->got_remote_heads) {
398 int i;
399 int must_list_refs = 0;
400 for (i = 0; i < nr_heads; i++) {
401 if (!to_fetch[i]->exact_oid) {
402 must_list_refs = 1;
403 break;
406 refs_tmp = handshake(transport, 0, NULL, must_list_refs);
409 if (data->version == protocol_unknown_version)
410 BUG("unknown protocol version");
411 else if (data->version <= protocol_v1)
412 die_if_server_options(transport);
414 if (data->options.acked_commits) {
415 if (data->version < protocol_v2) {
416 warning(_("--negotiate-only requires protocol v2"));
417 ret = -1;
418 } else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
419 warning(_("server does not support wait-for-done"));
420 ret = -1;
421 } else {
422 negotiate_using_fetch(data->options.negotiation_tips,
423 transport->server_options,
424 transport->stateless_rpc,
425 data->fd,
426 data->options.acked_commits);
427 ret = 0;
429 goto cleanup;
432 refs = fetch_pack(&args, data->fd,
433 refs_tmp ? refs_tmp : transport->remote_refs,
434 to_fetch, nr_heads, &data->shallow,
435 &transport->pack_lockfiles, data->version);
437 data->got_remote_heads = 0;
438 data->options.self_contained_and_connected =
439 args.self_contained_and_connected;
440 data->options.connectivity_checked = args.connectivity_checked;
442 if (!refs)
443 ret = -1;
444 if (report_unmatched_refs(to_fetch, nr_heads))
445 ret = -1;
447 cleanup:
448 close(data->fd[0]);
449 if (data->fd[1] >= 0)
450 close(data->fd[1]);
451 if (finish_connect(data->conn))
452 ret = -1;
453 data->conn = NULL;
455 free_refs(refs_tmp);
456 free_refs(refs);
457 list_objects_filter_release(&args.filter_options);
458 return ret;
461 static int push_had_errors(struct ref *ref)
463 for (; ref; ref = ref->next) {
464 switch (ref->status) {
465 case REF_STATUS_NONE:
466 case REF_STATUS_UPTODATE:
467 case REF_STATUS_OK:
468 break;
469 default:
470 return 1;
473 return 0;
476 int transport_refs_pushed(struct ref *ref)
478 for (; ref; ref = ref->next) {
479 switch(ref->status) {
480 case REF_STATUS_NONE:
481 case REF_STATUS_UPTODATE:
482 break;
483 default:
484 return 1;
487 return 0;
490 static void update_one_tracking_ref(struct remote *remote, char *refname,
491 struct object_id *new_oid, int deletion,
492 int verbose)
494 struct refspec_item rs;
496 memset(&rs, 0, sizeof(rs));
497 rs.src = refname;
498 rs.dst = NULL;
500 if (!remote_find_tracking(remote, &rs)) {
501 if (verbose)
502 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
503 if (deletion)
504 delete_ref(NULL, rs.dst, NULL, 0);
505 else
506 update_ref("update by push", rs.dst, new_oid,
507 NULL, 0, 0);
508 free(rs.dst);
512 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
514 char *refname;
515 struct object_id *new_oid;
516 struct ref_push_report *report;
518 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
519 return;
521 report = ref->report;
522 if (!report)
523 update_one_tracking_ref(remote, ref->name, &ref->new_oid,
524 ref->deletion, verbose);
525 else
526 for (; report; report = report->next) {
527 refname = report->ref_name ? (char *)report->ref_name : ref->name;
528 new_oid = report->new_oid ? report->new_oid : &ref->new_oid;
529 update_one_tracking_ref(remote, refname, new_oid,
530 is_null_oid(new_oid), verbose);
534 static void print_ref_status(char flag, const char *summary,
535 struct ref *to, struct ref *from, const char *msg,
536 struct ref_push_report *report,
537 int porcelain, int summary_width)
539 const char *to_name;
541 if (report && report->ref_name)
542 to_name = report->ref_name;
543 else
544 to_name = to->name;
546 if (porcelain) {
547 if (from)
548 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to_name);
549 else
550 fprintf(stdout, "%c\t:%s\t", flag, to_name);
551 if (msg)
552 fprintf(stdout, "%s (%s)\n", summary, msg);
553 else
554 fprintf(stdout, "%s\n", summary);
555 } else {
556 const char *red = "", *reset = "";
557 if (push_had_errors(to)) {
558 red = transport_get_color(TRANSPORT_COLOR_REJECTED);
559 reset = transport_get_color(TRANSPORT_COLOR_RESET);
561 fprintf(stderr, " %s%c %-*s%s ", red, flag, summary_width,
562 summary, reset);
563 if (from)
564 fprintf(stderr, "%s -> %s",
565 prettify_refname(from->name),
566 prettify_refname(to_name));
567 else
568 fputs(prettify_refname(to_name), stderr);
569 if (msg) {
570 fputs(" (", stderr);
571 fputs(msg, stderr);
572 fputc(')', stderr);
574 fputc('\n', stderr);
578 static void print_ok_ref_status(struct ref *ref,
579 struct ref_push_report *report,
580 int porcelain, int summary_width)
582 struct object_id *old_oid;
583 struct object_id *new_oid;
584 const char *ref_name;
585 int forced_update;
587 if (report && report->old_oid)
588 old_oid = report->old_oid;
589 else
590 old_oid = &ref->old_oid;
591 if (report && report->new_oid)
592 new_oid = report->new_oid;
593 else
594 new_oid = &ref->new_oid;
595 if (report && report->forced_update)
596 forced_update = report->forced_update;
597 else
598 forced_update = ref->forced_update;
599 if (report && report->ref_name)
600 ref_name = report->ref_name;
601 else
602 ref_name = ref->name;
604 if (ref->deletion)
605 print_ref_status('-', "[deleted]", ref, NULL, NULL,
606 report, porcelain, summary_width);
607 else if (is_null_oid(old_oid))
608 print_ref_status('*',
609 (starts_with(ref_name, "refs/tags/")
610 ? "[new tag]"
611 : (starts_with(ref_name, "refs/heads/")
612 ? "[new branch]"
613 : "[new reference]")),
614 ref, ref->peer_ref, NULL,
615 report, porcelain, summary_width);
616 else {
617 struct strbuf quickref = STRBUF_INIT;
618 char type;
619 const char *msg;
621 strbuf_add_unique_abbrev(&quickref, old_oid,
622 DEFAULT_ABBREV);
623 if (forced_update) {
624 strbuf_addstr(&quickref, "...");
625 type = '+';
626 msg = "forced update";
627 } else {
628 strbuf_addstr(&quickref, "..");
629 type = ' ';
630 msg = NULL;
632 strbuf_add_unique_abbrev(&quickref, new_oid,
633 DEFAULT_ABBREV);
635 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
636 report, porcelain, summary_width);
637 strbuf_release(&quickref);
641 static int print_one_push_report(struct ref *ref, const char *dest, int count,
642 struct ref_push_report *report,
643 int porcelain, int summary_width)
645 if (!count) {
646 char *url = transport_anonymize_url(dest);
647 fprintf(porcelain ? stdout : stderr, "To %s\n", url);
648 free(url);
651 switch(ref->status) {
652 case REF_STATUS_NONE:
653 print_ref_status('X', "[no match]", ref, NULL, NULL,
654 report, porcelain, summary_width);
655 break;
656 case REF_STATUS_REJECT_NODELETE:
657 print_ref_status('!', "[rejected]", ref, NULL,
658 "remote does not support deleting refs",
659 report, porcelain, summary_width);
660 break;
661 case REF_STATUS_UPTODATE:
662 print_ref_status('=', "[up to date]", ref,
663 ref->peer_ref, NULL,
664 report, porcelain, summary_width);
665 break;
666 case REF_STATUS_REJECT_NONFASTFORWARD:
667 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
668 "non-fast-forward",
669 report, porcelain, summary_width);
670 break;
671 case REF_STATUS_REJECT_ALREADY_EXISTS:
672 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
673 "already exists",
674 report, porcelain, summary_width);
675 break;
676 case REF_STATUS_REJECT_FETCH_FIRST:
677 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
678 "fetch first",
679 report, porcelain, summary_width);
680 break;
681 case REF_STATUS_REJECT_NEEDS_FORCE:
682 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
683 "needs force",
684 report, porcelain, summary_width);
685 break;
686 case REF_STATUS_REJECT_STALE:
687 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
688 "stale info",
689 report, porcelain, summary_width);
690 break;
691 case REF_STATUS_REJECT_REMOTE_UPDATED:
692 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
693 "remote ref updated since checkout",
694 report, porcelain, summary_width);
695 break;
696 case REF_STATUS_REJECT_SHALLOW:
697 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
698 "new shallow roots not allowed",
699 report, porcelain, summary_width);
700 break;
701 case REF_STATUS_REMOTE_REJECT:
702 print_ref_status('!', "[remote rejected]", ref,
703 ref->deletion ? NULL : ref->peer_ref,
704 ref->remote_status,
705 report, porcelain, summary_width);
706 break;
707 case REF_STATUS_EXPECTING_REPORT:
708 print_ref_status('!', "[remote failure]", ref,
709 ref->deletion ? NULL : ref->peer_ref,
710 "remote failed to report status",
711 report, porcelain, summary_width);
712 break;
713 case REF_STATUS_ATOMIC_PUSH_FAILED:
714 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
715 "atomic push failed",
716 report, porcelain, summary_width);
717 break;
718 case REF_STATUS_OK:
719 print_ok_ref_status(ref, report, porcelain, summary_width);
720 break;
723 return 1;
726 static int print_one_push_status(struct ref *ref, const char *dest, int count,
727 int porcelain, int summary_width)
729 struct ref_push_report *report;
730 int n = 0;
732 if (!ref->report)
733 return print_one_push_report(ref, dest, count,
734 NULL, porcelain, summary_width);
736 for (report = ref->report; report; report = report->next)
737 print_one_push_report(ref, dest, count + n++,
738 report, porcelain, summary_width);
739 return n;
742 static int measure_abbrev(const struct object_id *oid, int sofar)
744 char hex[GIT_MAX_HEXSZ + 1];
745 int w = find_unique_abbrev_r(hex, oid, DEFAULT_ABBREV);
747 return (w < sofar) ? sofar : w;
750 int transport_summary_width(const struct ref *refs)
752 int maxw = -1;
754 for (; refs; refs = refs->next) {
755 maxw = measure_abbrev(&refs->old_oid, maxw);
756 maxw = measure_abbrev(&refs->new_oid, maxw);
758 if (maxw < 0)
759 maxw = FALLBACK_DEFAULT_ABBREV;
760 return (2 * maxw + 3);
763 void transport_print_push_status(const char *dest, struct ref *refs,
764 int verbose, int porcelain, unsigned int *reject_reasons)
766 struct ref *ref;
767 int n = 0;
768 char *head;
769 int summary_width = transport_summary_width(refs);
771 if (transport_color_config() < 0)
772 warning(_("could not parse transport.color.* config"));
774 head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
776 if (verbose) {
777 for (ref = refs; ref; ref = ref->next)
778 if (ref->status == REF_STATUS_UPTODATE)
779 n += print_one_push_status(ref, dest, n,
780 porcelain, summary_width);
783 for (ref = refs; ref; ref = ref->next)
784 if (ref->status == REF_STATUS_OK)
785 n += print_one_push_status(ref, dest, n,
786 porcelain, summary_width);
788 *reject_reasons = 0;
789 for (ref = refs; ref; ref = ref->next) {
790 if (ref->status != REF_STATUS_NONE &&
791 ref->status != REF_STATUS_UPTODATE &&
792 ref->status != REF_STATUS_OK)
793 n += print_one_push_status(ref, dest, n,
794 porcelain, summary_width);
795 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
796 if (head != NULL && !strcmp(head, ref->name))
797 *reject_reasons |= REJECT_NON_FF_HEAD;
798 else
799 *reject_reasons |= REJECT_NON_FF_OTHER;
800 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
801 *reject_reasons |= REJECT_ALREADY_EXISTS;
802 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
803 *reject_reasons |= REJECT_FETCH_FIRST;
804 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
805 *reject_reasons |= REJECT_NEEDS_FORCE;
806 } else if (ref->status == REF_STATUS_REJECT_REMOTE_UPDATED) {
807 *reject_reasons |= REJECT_REF_NEEDS_UPDATE;
810 free(head);
813 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
815 struct git_transport_data *data = transport->data;
816 struct send_pack_args args;
817 int ret = 0;
819 if (transport_color_config() < 0)
820 return -1;
822 if (!data->got_remote_heads)
823 get_refs_via_connect(transport, 1, NULL);
825 memset(&args, 0, sizeof(args));
826 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
827 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
828 args.use_thin_pack = data->options.thin;
829 args.verbose = (transport->verbose > 0);
830 args.quiet = (transport->verbose < 0);
831 args.progress = transport->progress;
832 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
833 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
834 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
835 args.push_options = transport->push_options;
836 args.url = transport->url;
838 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
839 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
840 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
841 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
842 else
843 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
845 switch (data->version) {
846 case protocol_v2:
847 die(_("support for protocol v2 not implemented yet"));
848 break;
849 case protocol_v1:
850 case protocol_v0:
851 ret = send_pack(&args, data->fd, data->conn, remote_refs,
852 &data->extra_have);
853 break;
854 case protocol_unknown_version:
855 BUG("unknown protocol version");
858 close(data->fd[1]);
859 close(data->fd[0]);
861 * Atomic push may abort the connection early and close the pipe,
862 * which may cause an error for `finish_connect()`. Ignore this error
863 * for atomic git-push.
865 if (ret || args.atomic)
866 finish_connect(data->conn);
867 else
868 ret = finish_connect(data->conn);
869 data->conn = NULL;
870 data->got_remote_heads = 0;
872 return ret;
875 static int connect_git(struct transport *transport, const char *name,
876 const char *executable, int fd[2])
878 struct git_transport_data *data = transport->data;
879 data->conn = git_connect(data->fd, transport->url,
880 executable, 0);
881 fd[0] = data->fd[0];
882 fd[1] = data->fd[1];
883 return 0;
886 static int disconnect_git(struct transport *transport)
888 struct git_transport_data *data = transport->data;
889 if (data->conn) {
890 if (data->got_remote_heads && !transport->stateless_rpc)
891 packet_flush(data->fd[1]);
892 close(data->fd[0]);
893 if (data->fd[1] >= 0)
894 close(data->fd[1]);
895 finish_connect(data->conn);
898 list_objects_filter_release(&data->options.filter_options);
899 free(data);
900 return 0;
903 static struct transport_vtable taken_over_vtable = {
904 .get_refs_list = get_refs_via_connect,
905 .fetch_refs = fetch_refs_via_pack,
906 .push_refs = git_transport_push,
907 .disconnect = disconnect_git
910 void transport_take_over(struct transport *transport,
911 struct child_process *child)
913 struct git_transport_data *data;
915 if (!transport->smart_options)
916 BUG("taking over transport requires non-NULL "
917 "smart_options field.");
919 CALLOC_ARRAY(data, 1);
920 data->options = *transport->smart_options;
921 data->conn = child;
922 data->fd[0] = data->conn->out;
923 data->fd[1] = data->conn->in;
924 data->got_remote_heads = 0;
925 transport->data = data;
927 transport->vtable = &taken_over_vtable;
928 transport->smart_options = &(data->options);
930 transport->cannot_reuse = 1;
933 static int is_file(const char *url)
935 struct stat buf;
936 if (stat(url, &buf))
937 return 0;
938 return S_ISREG(buf.st_mode);
941 static int external_specification_len(const char *url)
943 return strchr(url, ':') - url;
946 static const struct string_list *protocol_allow_list(void)
948 static int enabled = -1;
949 static struct string_list allowed = STRING_LIST_INIT_DUP;
951 if (enabled < 0) {
952 const char *v = getenv("GIT_ALLOW_PROTOCOL");
953 if (v) {
954 string_list_split(&allowed, v, ':', -1);
955 string_list_sort(&allowed);
956 enabled = 1;
957 } else {
958 enabled = 0;
962 return enabled ? &allowed : NULL;
965 enum protocol_allow_config {
966 PROTOCOL_ALLOW_NEVER = 0,
967 PROTOCOL_ALLOW_USER_ONLY,
968 PROTOCOL_ALLOW_ALWAYS
971 static enum protocol_allow_config parse_protocol_config(const char *key,
972 const char *value)
974 if (!strcasecmp(value, "always"))
975 return PROTOCOL_ALLOW_ALWAYS;
976 else if (!strcasecmp(value, "never"))
977 return PROTOCOL_ALLOW_NEVER;
978 else if (!strcasecmp(value, "user"))
979 return PROTOCOL_ALLOW_USER_ONLY;
981 die(_("unknown value for config '%s': %s"), key, value);
984 static enum protocol_allow_config get_protocol_config(const char *type)
986 char *key = xstrfmt("protocol.%s.allow", type);
987 char *value;
989 /* first check the per-protocol config */
990 if (!git_config_get_string(key, &value)) {
991 enum protocol_allow_config ret =
992 parse_protocol_config(key, value);
993 free(key);
994 free(value);
995 return ret;
997 free(key);
999 /* if defined, fallback to user-defined default for unknown protocols */
1000 if (!git_config_get_string("protocol.allow", &value)) {
1001 enum protocol_allow_config ret =
1002 parse_protocol_config("protocol.allow", value);
1003 free(value);
1004 return ret;
1007 /* fallback to built-in defaults */
1008 /* known safe */
1009 if (!strcmp(type, "http") ||
1010 !strcmp(type, "https") ||
1011 !strcmp(type, "git") ||
1012 !strcmp(type, "ssh") ||
1013 !strcmp(type, "file"))
1014 return PROTOCOL_ALLOW_ALWAYS;
1016 /* known scary; err on the side of caution */
1017 if (!strcmp(type, "ext"))
1018 return PROTOCOL_ALLOW_NEVER;
1020 /* unknown; by default let them be used only directly by the user */
1021 return PROTOCOL_ALLOW_USER_ONLY;
1024 int is_transport_allowed(const char *type, int from_user)
1026 const struct string_list *allow_list = protocol_allow_list();
1027 if (allow_list)
1028 return string_list_has_string(allow_list, type);
1030 switch (get_protocol_config(type)) {
1031 case PROTOCOL_ALLOW_ALWAYS:
1032 return 1;
1033 case PROTOCOL_ALLOW_NEVER:
1034 return 0;
1035 case PROTOCOL_ALLOW_USER_ONLY:
1036 if (from_user < 0)
1037 from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1038 return from_user;
1041 BUG("invalid protocol_allow_config type");
1044 void transport_check_allowed(const char *type)
1046 if (!is_transport_allowed(type, -1))
1047 die(_("transport '%s' not allowed"), type);
1050 static struct transport_vtable bundle_vtable = {
1051 .get_refs_list = get_refs_from_bundle,
1052 .fetch_refs = fetch_refs_from_bundle,
1053 .disconnect = close_bundle
1056 static struct transport_vtable builtin_smart_vtable = {
1057 .get_refs_list = get_refs_via_connect,
1058 .fetch_refs = fetch_refs_via_pack,
1059 .push_refs = git_transport_push,
1060 .connect = connect_git,
1061 .disconnect = disconnect_git
1064 struct transport *transport_get(struct remote *remote, const char *url)
1066 const char *helper;
1067 struct transport *ret = xcalloc(1, sizeof(*ret));
1069 ret->progress = isatty(2);
1070 string_list_init_dup(&ret->pack_lockfiles);
1072 if (!remote)
1073 BUG("No remote provided to transport_get()");
1075 ret->got_remote_refs = 0;
1076 ret->remote = remote;
1077 helper = remote->foreign_vcs;
1079 if (!url && remote->url)
1080 url = remote->url[0];
1081 ret->url = url;
1083 /* maybe it is a foreign URL? */
1084 if (url) {
1085 const char *p = url;
1087 while (is_urlschemechar(p == url, *p))
1088 p++;
1089 if (starts_with(p, "::"))
1090 helper = xstrndup(url, p - url);
1093 if (helper) {
1094 transport_helper_init(ret, helper);
1095 } else if (starts_with(url, "rsync:")) {
1096 die(_("git-over-rsync is no longer supported"));
1097 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
1098 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
1099 bundle_header_init(&data->header);
1100 transport_check_allowed("file");
1101 ret->data = data;
1102 ret->vtable = &bundle_vtable;
1103 ret->smart_options = NULL;
1104 } else if (!is_url(url)
1105 || starts_with(url, "file://")
1106 || starts_with(url, "git://")
1107 || starts_with(url, "ssh://")
1108 || starts_with(url, "git+ssh://") /* deprecated - do not use */
1109 || starts_with(url, "ssh+git://") /* deprecated - do not use */
1112 * These are builtin smart transports; "allowed" transports
1113 * will be checked individually in git_connect.
1115 struct git_transport_data *data = xcalloc(1, sizeof(*data));
1116 list_objects_filter_init(&data->options.filter_options);
1117 ret->data = data;
1118 ret->vtable = &builtin_smart_vtable;
1119 ret->smart_options = &(data->options);
1121 data->conn = NULL;
1122 data->got_remote_heads = 0;
1123 } else {
1124 /* Unknown protocol in URL. Pass to external handler. */
1125 int len = external_specification_len(url);
1126 char *handler = xmemdupz(url, len);
1127 transport_helper_init(ret, handler);
1130 if (ret->smart_options) {
1131 ret->smart_options->thin = 1;
1132 ret->smart_options->uploadpack = "git-upload-pack";
1133 if (remote->uploadpack)
1134 ret->smart_options->uploadpack = remote->uploadpack;
1135 ret->smart_options->receivepack = "git-receive-pack";
1136 if (remote->receivepack)
1137 ret->smart_options->receivepack = remote->receivepack;
1140 ret->hash_algo = &hash_algos[GIT_HASH_SHA1];
1142 return ret;
1145 const struct git_hash_algo *transport_get_hash_algo(struct transport *transport)
1147 return transport->hash_algo;
1150 int transport_set_option(struct transport *transport,
1151 const char *name, const char *value)
1153 int git_reports = 1, protocol_reports = 1;
1155 if (transport->smart_options)
1156 git_reports = set_git_option(transport->smart_options,
1157 name, value);
1159 if (transport->vtable->set_option)
1160 protocol_reports = transport->vtable->set_option(transport,
1161 name, value);
1163 /* If either report is 0, report 0 (success). */
1164 if (!git_reports || !protocol_reports)
1165 return 0;
1166 /* If either reports -1 (invalid value), report -1. */
1167 if ((git_reports == -1) || (protocol_reports == -1))
1168 return -1;
1169 /* Otherwise if both report unknown, report unknown. */
1170 return 1;
1173 void transport_set_verbosity(struct transport *transport, int verbosity,
1174 int force_progress)
1176 if (verbosity >= 1)
1177 transport->verbose = verbosity <= 3 ? verbosity : 3;
1178 if (verbosity < 0)
1179 transport->verbose = -1;
1182 * Rules used to determine whether to report progress (processing aborts
1183 * when a rule is satisfied):
1185 * . Report progress, if force_progress is 1 (ie. --progress).
1186 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1187 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1188 * . Report progress if isatty(2) is 1.
1190 if (force_progress >= 0)
1191 transport->progress = !!force_progress;
1192 else
1193 transport->progress = verbosity >= 0 && isatty(2);
1196 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
1198 int i;
1200 fprintf(stderr, _("The following submodule paths contain changes that can\n"
1201 "not be found on any remote:\n"));
1202 for (i = 0; i < needs_pushing->nr; i++)
1203 fprintf(stderr, " %s\n", needs_pushing->items[i].string);
1204 fprintf(stderr, _("\nPlease try\n\n"
1205 " git push --recurse-submodules=on-demand\n\n"
1206 "or cd to the path and use\n\n"
1207 " git push\n\n"
1208 "to push them to a remote.\n\n"));
1210 string_list_clear(needs_pushing, 0);
1212 die(_("Aborting."));
1215 static int run_pre_push_hook(struct transport *transport,
1216 struct ref *remote_refs)
1218 int ret = 0, x;
1219 struct ref *r;
1220 struct child_process proc = CHILD_PROCESS_INIT;
1221 struct strbuf buf;
1222 const char *hook_path = find_hook("pre-push");
1224 if (!hook_path)
1225 return 0;
1227 strvec_push(&proc.args, hook_path);
1228 strvec_push(&proc.args, transport->remote->name);
1229 strvec_push(&proc.args, transport->url);
1231 proc.in = -1;
1232 proc.trace2_hook_name = "pre-push";
1234 if (start_command(&proc)) {
1235 finish_command(&proc);
1236 return -1;
1239 sigchain_push(SIGPIPE, SIG_IGN);
1241 strbuf_init(&buf, 256);
1243 for (r = remote_refs; r; r = r->next) {
1244 if (!r->peer_ref) continue;
1245 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
1246 if (r->status == REF_STATUS_REJECT_STALE) continue;
1247 if (r->status == REF_STATUS_REJECT_REMOTE_UPDATED) continue;
1248 if (r->status == REF_STATUS_UPTODATE) continue;
1250 strbuf_reset(&buf);
1251 strbuf_addf( &buf, "%s %s %s %s\n",
1252 r->peer_ref->name, oid_to_hex(&r->new_oid),
1253 r->name, oid_to_hex(&r->old_oid));
1255 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
1256 /* We do not mind if a hook does not read all refs. */
1257 if (errno != EPIPE)
1258 ret = -1;
1259 break;
1263 strbuf_release(&buf);
1265 x = close(proc.in);
1266 if (!ret)
1267 ret = x;
1269 sigchain_pop(SIGPIPE);
1271 x = finish_command(&proc);
1272 if (!ret)
1273 ret = x;
1275 return ret;
1278 int transport_push(struct repository *r,
1279 struct transport *transport,
1280 struct refspec *rs, int flags,
1281 unsigned int *reject_reasons)
1283 struct ref *remote_refs = NULL;
1284 struct ref *local_refs = NULL;
1285 int match_flags = MATCH_REFS_NONE;
1286 int verbose = (transport->verbose > 0);
1287 int quiet = (transport->verbose < 0);
1288 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1289 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1290 int push_ret, err;
1291 int ret = -1;
1292 struct transport_ls_refs_options transport_options =
1293 TRANSPORT_LS_REFS_OPTIONS_INIT;
1295 *reject_reasons = 0;
1297 if (transport_color_config() < 0)
1298 goto done;
1300 if (!transport->vtable->push_refs)
1301 goto done;
1303 local_refs = get_local_heads();
1305 if (check_push_refs(local_refs, rs) < 0)
1306 goto done;
1308 refspec_ref_prefixes(rs, &transport_options.ref_prefixes);
1310 trace2_region_enter("transport_push", "get_refs_list", r);
1311 remote_refs = transport->vtable->get_refs_list(transport, 1,
1312 &transport_options);
1313 trace2_region_leave("transport_push", "get_refs_list", r);
1315 transport_ls_refs_options_release(&transport_options);
1317 if (flags & TRANSPORT_PUSH_ALL)
1318 match_flags |= MATCH_REFS_ALL;
1319 if (flags & TRANSPORT_PUSH_MIRROR)
1320 match_flags |= MATCH_REFS_MIRROR;
1321 if (flags & TRANSPORT_PUSH_PRUNE)
1322 match_flags |= MATCH_REFS_PRUNE;
1323 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1324 match_flags |= MATCH_REFS_FOLLOW_TAGS;
1326 if (match_push_refs(local_refs, &remote_refs, rs, match_flags))
1327 goto done;
1329 if (transport->smart_options &&
1330 transport->smart_options->cas &&
1331 !is_empty_cas(transport->smart_options->cas))
1332 apply_push_cas(transport->smart_options->cas,
1333 transport->remote, remote_refs);
1335 set_ref_status_for_push(remote_refs,
1336 flags & TRANSPORT_PUSH_MIRROR,
1337 flags & TRANSPORT_PUSH_FORCE);
1339 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1340 if (run_pre_push_hook(transport, remote_refs))
1341 goto done;
1343 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1344 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1345 !is_bare_repository()) {
1346 struct ref *ref = remote_refs;
1347 struct oid_array commits = OID_ARRAY_INIT;
1349 trace2_region_enter("transport_push", "push_submodules", r);
1350 for (; ref; ref = ref->next)
1351 if (!is_null_oid(&ref->new_oid))
1352 oid_array_append(&commits,
1353 &ref->new_oid);
1355 if (!push_unpushed_submodules(r,
1356 &commits,
1357 transport->remote,
1359 transport->push_options,
1360 pretend)) {
1361 oid_array_clear(&commits);
1362 trace2_region_leave("transport_push", "push_submodules", r);
1363 die(_("failed to push all needed submodules"));
1365 oid_array_clear(&commits);
1366 trace2_region_leave("transport_push", "push_submodules", r);
1369 if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1370 ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1371 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1372 !pretend)) && !is_bare_repository()) {
1373 struct ref *ref = remote_refs;
1374 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1375 struct oid_array commits = OID_ARRAY_INIT;
1377 trace2_region_enter("transport_push", "check_submodules", r);
1378 for (; ref; ref = ref->next)
1379 if (!is_null_oid(&ref->new_oid))
1380 oid_array_append(&commits,
1381 &ref->new_oid);
1383 if (find_unpushed_submodules(r,
1384 &commits,
1385 transport->remote->name,
1386 &needs_pushing)) {
1387 oid_array_clear(&commits);
1388 trace2_region_leave("transport_push", "check_submodules", r);
1389 die_with_unpushed_submodules(&needs_pushing);
1391 string_list_clear(&needs_pushing, 0);
1392 oid_array_clear(&commits);
1393 trace2_region_leave("transport_push", "check_submodules", r);
1396 if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY)) {
1397 trace2_region_enter("transport_push", "push_refs", r);
1398 push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1399 trace2_region_leave("transport_push", "push_refs", r);
1400 } else
1401 push_ret = 0;
1402 err = push_had_errors(remote_refs);
1403 ret = push_ret | err;
1405 if (!quiet || err)
1406 transport_print_push_status(transport->url, remote_refs,
1407 verbose | porcelain, porcelain,
1408 reject_reasons);
1410 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1411 set_upstreams(transport, remote_refs, pretend);
1413 if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1414 TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1415 struct ref *ref;
1416 for (ref = remote_refs; ref; ref = ref->next)
1417 transport_update_tracking_ref(transport->remote, ref, verbose);
1420 if (porcelain && !push_ret)
1421 puts("Done");
1422 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1423 fprintf(stderr, "Everything up-to-date\n");
1425 done:
1426 free_refs(local_refs);
1427 free_refs(remote_refs);
1428 return ret;
1431 const struct ref *transport_get_remote_refs(struct transport *transport,
1432 struct transport_ls_refs_options *transport_options)
1434 if (!transport->got_remote_refs) {
1435 transport->remote_refs =
1436 transport->vtable->get_refs_list(transport, 0,
1437 transport_options);
1438 transport->got_remote_refs = 1;
1441 return transport->remote_refs;
1444 void transport_ls_refs_options_release(struct transport_ls_refs_options *opts)
1446 strvec_clear(&opts->ref_prefixes);
1447 free((char *)opts->unborn_head_target);
1450 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1452 int rc;
1453 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1454 struct ref **heads = NULL;
1455 struct ref *rm;
1457 for (rm = refs; rm; rm = rm->next) {
1458 nr_refs++;
1459 if (rm->peer_ref &&
1460 !is_null_oid(&rm->old_oid) &&
1461 oideq(&rm->peer_ref->old_oid, &rm->old_oid))
1462 continue;
1463 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1464 heads[nr_heads++] = rm;
1467 if (!nr_heads) {
1469 * When deepening of a shallow repository is requested,
1470 * then local and remote refs are likely to still be equal.
1471 * Just feed them all to the fetch method in that case.
1472 * This condition shouldn't be met in a non-deepening fetch
1473 * (see builtin/fetch.c:quickfetch()).
1475 ALLOC_ARRAY(heads, nr_refs);
1476 for (rm = refs; rm; rm = rm->next)
1477 heads[nr_heads++] = rm;
1480 rc = transport->vtable->fetch_refs(transport, nr_heads, heads);
1482 free(heads);
1483 return rc;
1486 void transport_unlock_pack(struct transport *transport, unsigned int flags)
1488 int in_signal_handler = !!(flags & TRANSPORT_UNLOCK_PACK_IN_SIGNAL_HANDLER);
1489 int i;
1491 for (i = 0; i < transport->pack_lockfiles.nr; i++)
1492 if (in_signal_handler)
1493 unlink(transport->pack_lockfiles.items[i].string);
1494 else
1495 unlink_or_warn(transport->pack_lockfiles.items[i].string);
1496 if (!in_signal_handler)
1497 string_list_clear(&transport->pack_lockfiles, 0);
1500 int transport_connect(struct transport *transport, const char *name,
1501 const char *exec, int fd[2])
1503 if (transport->vtable->connect)
1504 return transport->vtable->connect(transport, name, exec, fd);
1505 else
1506 die(_("operation not supported by protocol"));
1509 int transport_disconnect(struct transport *transport)
1511 int ret = 0;
1512 if (transport->vtable->disconnect)
1513 ret = transport->vtable->disconnect(transport);
1514 if (transport->got_remote_refs)
1515 free_refs((void *)transport->remote_refs);
1516 free(transport);
1517 return ret;
1521 * Strip username (and password) from a URL and return
1522 * it in a newly allocated string.
1524 char *transport_anonymize_url(const char *url)
1526 char *scheme_prefix, *anon_part;
1527 size_t anon_len, prefix_len = 0;
1529 anon_part = strchr(url, '@');
1530 if (url_is_local_not_ssh(url) || !anon_part)
1531 goto literal_copy;
1533 anon_len = strlen(++anon_part);
1534 scheme_prefix = strstr(url, "://");
1535 if (!scheme_prefix) {
1536 if (!strchr(anon_part, ':'))
1537 /* cannot be "me@there:/path/name" */
1538 goto literal_copy;
1539 } else {
1540 const char *cp;
1541 /* make sure scheme is reasonable */
1542 for (cp = url; cp < scheme_prefix; cp++) {
1543 switch (*cp) {
1544 /* RFC 1738 2.1 */
1545 case '+': case '.': case '-':
1546 break; /* ok */
1547 default:
1548 if (isalnum(*cp))
1549 break;
1550 /* it isn't */
1551 goto literal_copy;
1554 /* @ past the first slash does not count */
1555 cp = strchr(scheme_prefix + 3, '/');
1556 if (cp && cp < anon_part)
1557 goto literal_copy;
1558 prefix_len = scheme_prefix - url + 3;
1560 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1561 (int)anon_len, anon_part);
1562 literal_copy:
1563 return xstrdup(url);