Merge branch 'ak/typofixes'
[git/gitster.git] / transport.c
blob3c4714581f58eb7f934c48c543471eac862dec65
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "advice.h"
5 #include "config.h"
6 #include "environment.h"
7 #include "hex.h"
8 #include "transport.h"
9 #include "hook.h"
10 #include "pkt-line.h"
11 #include "fetch-pack.h"
12 #include "remote.h"
13 #include "connect.h"
14 #include "send-pack.h"
15 #include "bundle.h"
16 #include "gettext.h"
17 #include "refs.h"
18 #include "refspec.h"
19 #include "branch.h"
20 #include "url.h"
21 #include "submodule.h"
22 #include "string-list.h"
23 #include "oid-array.h"
24 #include "sigchain.h"
25 #include "trace2.h"
26 #include "transport-internal.h"
27 #include "protocol.h"
28 #include "object-name.h"
29 #include "color.h"
30 #include "bundle-uri.h"
32 static int transport_use_color = -1;
33 static char transport_colors[][COLOR_MAXLEN] = {
34 GIT_COLOR_RESET,
35 GIT_COLOR_RED /* REJECTED */
38 enum color_transport {
39 TRANSPORT_COLOR_RESET = 0,
40 TRANSPORT_COLOR_REJECTED = 1
43 static int transport_color_config(void)
45 const char *keys[] = {
46 "color.transport.reset",
47 "color.transport.rejected"
48 }, *key = "color.transport";
49 char *value;
50 int i;
51 static int initialized;
53 if (initialized)
54 return 0;
55 initialized = 1;
57 if (!git_config_get_string(key, &value))
58 transport_use_color = git_config_colorbool(key, value);
60 if (!want_color_stderr(transport_use_color))
61 return 0;
63 for (i = 0; i < ARRAY_SIZE(keys); i++)
64 if (!git_config_get_string(keys[i], &value)) {
65 if (!value)
66 return config_error_nonbool(keys[i]);
67 if (color_parse(value, transport_colors[i]) < 0)
68 return -1;
71 return 0;
74 static const char *transport_get_color(enum color_transport ix)
76 if (want_color_stderr(transport_use_color))
77 return transport_colors[ix];
78 return "";
81 static void set_upstreams(struct transport *transport, struct ref *refs,
82 int pretend)
84 struct ref *ref;
85 for (ref = refs; ref; ref = ref->next) {
86 const char *localname;
87 const char *tmp;
88 const char *remotename;
89 int flag = 0;
91 * Check suitability for tracking. Must be successful /
92 * already up-to-date ref create/modify (not delete).
94 if (ref->status != REF_STATUS_OK &&
95 ref->status != REF_STATUS_UPTODATE)
96 continue;
97 if (!ref->peer_ref)
98 continue;
99 if (is_null_oid(&ref->new_oid))
100 continue;
102 /* Follow symbolic refs (mainly for HEAD). */
103 localname = ref->peer_ref->name;
104 remotename = ref->name;
105 tmp = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
106 localname, RESOLVE_REF_READING,
107 NULL, &flag);
108 if (tmp && flag & REF_ISSYMREF &&
109 starts_with(tmp, "refs/heads/"))
110 localname = tmp;
112 /* Both source and destination must be local branches. */
113 if (!localname || !starts_with(localname, "refs/heads/"))
114 continue;
115 if (!remotename || !starts_with(remotename, "refs/heads/"))
116 continue;
118 if (!pretend) {
119 int flag = transport->verbose < 0 ? 0 : BRANCH_CONFIG_VERBOSE;
120 install_branch_config(flag, localname + 11,
121 transport->remote->name, remotename);
122 } else if (transport->verbose >= 0)
123 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
124 localname + 11, remotename + 11,
125 transport->remote->name);
129 struct bundle_transport_data {
130 int fd;
131 struct bundle_header header;
132 unsigned get_refs_from_bundle_called : 1;
135 static void get_refs_from_bundle_inner(struct transport *transport)
137 struct bundle_transport_data *data = transport->data;
139 data->get_refs_from_bundle_called = 1;
141 if (data->fd > 0)
142 close(data->fd);
143 data->fd = read_bundle_header(transport->url, &data->header);
144 if (data->fd < 0)
145 die(_("could not read bundle '%s'"), transport->url);
147 transport->hash_algo = data->header.hash_algo;
150 static struct ref *get_refs_from_bundle(struct transport *transport,
151 int for_push,
152 struct transport_ls_refs_options *transport_options UNUSED)
154 struct bundle_transport_data *data = transport->data;
155 struct ref *result = NULL;
156 int i;
158 if (for_push)
159 return NULL;
161 get_refs_from_bundle_inner(transport);
163 for (i = 0; i < data->header.references.nr; i++) {
164 struct string_list_item *e = data->header.references.items + i;
165 const char *name = e->string;
166 struct ref *ref = alloc_ref(name);
167 struct object_id *oid = e->util;
168 oidcpy(&ref->old_oid, oid);
169 ref->next = result;
170 result = ref;
172 return result;
175 static int fetch_refs_from_bundle(struct transport *transport,
176 int nr_heads UNUSED,
177 struct ref **to_fetch UNUSED)
179 struct bundle_transport_data *data = transport->data;
180 struct strvec extra_index_pack_args = STRVEC_INIT;
181 int ret;
183 if (transport->progress)
184 strvec_push(&extra_index_pack_args, "-v");
186 if (!data->get_refs_from_bundle_called)
187 get_refs_from_bundle_inner(transport);
188 ret = unbundle(the_repository, &data->header, data->fd,
189 &extra_index_pack_args,
190 fetch_pack_fsck_objects() ? VERIFY_BUNDLE_FSCK : 0);
191 transport->hash_algo = data->header.hash_algo;
193 strvec_clear(&extra_index_pack_args);
194 return ret;
197 static int close_bundle(struct transport *transport)
199 struct bundle_transport_data *data = transport->data;
200 if (data->fd > 0)
201 close(data->fd);
202 bundle_header_release(&data->header);
203 free(data);
204 return 0;
207 struct git_transport_data {
208 struct git_transport_options options;
209 struct child_process *conn;
210 int fd[2];
211 unsigned finished_handshake : 1;
212 enum protocol_version version;
213 struct oid_array extra_have;
214 struct oid_array shallow;
217 static int set_git_option(struct git_transport_options *opts,
218 const char *name, const char *value)
220 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
221 opts->uploadpack = value;
222 return 0;
223 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
224 opts->receivepack = value;
225 return 0;
226 } else if (!strcmp(name, TRANS_OPT_THIN)) {
227 opts->thin = !!value;
228 return 0;
229 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
230 opts->followtags = !!value;
231 return 0;
232 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
233 opts->keep = !!value;
234 return 0;
235 } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
236 opts->update_shallow = !!value;
237 return 0;
238 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
239 if (!value)
240 opts->depth = 0;
241 else {
242 char *end;
243 opts->depth = strtol(value, &end, 0);
244 if (*end)
245 die(_("transport: invalid depth option '%s'"), value);
247 return 0;
248 } else if (!strcmp(name, TRANS_OPT_DEEPEN_SINCE)) {
249 opts->deepen_since = value;
250 return 0;
251 } else if (!strcmp(name, TRANS_OPT_DEEPEN_NOT)) {
252 opts->deepen_not = (const struct string_list *)value;
253 return 0;
254 } else if (!strcmp(name, TRANS_OPT_DEEPEN_RELATIVE)) {
255 opts->deepen_relative = !!value;
256 return 0;
257 } else if (!strcmp(name, TRANS_OPT_FROM_PROMISOR)) {
258 opts->from_promisor = !!value;
259 return 0;
260 } else if (!strcmp(name, TRANS_OPT_LIST_OBJECTS_FILTER)) {
261 list_objects_filter_die_if_populated(&opts->filter_options);
262 parse_list_objects_filter(&opts->filter_options, value);
263 return 0;
264 } else if (!strcmp(name, TRANS_OPT_REFETCH)) {
265 opts->refetch = !!value;
266 return 0;
267 } else if (!strcmp(name, TRANS_OPT_REJECT_SHALLOW)) {
268 opts->reject_shallow = !!value;
269 return 0;
271 return 1;
274 static int connect_setup(struct transport *transport, int for_push)
276 struct git_transport_data *data = transport->data;
277 int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
279 if (data->conn)
280 return 0;
282 switch (transport->family) {
283 case TRANSPORT_FAMILY_ALL: break;
284 case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
285 case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
288 data->conn = git_connect(data->fd, transport->url,
289 for_push ?
290 "git-receive-pack" :
291 "git-upload-pack",
292 for_push ?
293 data->options.receivepack :
294 data->options.uploadpack,
295 flags);
297 return 0;
300 static void die_if_server_options(struct transport *transport)
302 if (!transport->server_options || !transport->server_options->nr)
303 return;
304 advise(_("see protocol.version in 'git help config' for more details"));
305 die(_("server options require protocol version 2 or later"));
309 * Obtains the protocol version from the transport and writes it to
310 * transport->data->version, first connecting if not already connected.
312 * If the protocol version is one that allows skipping the listing of remote
313 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
314 * this function returns NULL. Otherwise, this function returns the list of
315 * remote refs.
317 static struct ref *handshake(struct transport *transport, int for_push,
318 struct transport_ls_refs_options *options,
319 int must_list_refs)
321 struct git_transport_data *data = transport->data;
322 struct ref *refs = NULL;
323 struct packet_reader reader;
324 size_t sid_len;
325 const char *server_sid;
327 connect_setup(transport, for_push);
329 packet_reader_init(&reader, data->fd[0], NULL, 0,
330 PACKET_READ_CHOMP_NEWLINE |
331 PACKET_READ_GENTLE_ON_EOF |
332 PACKET_READ_DIE_ON_ERR_PACKET);
334 data->version = discover_version(&reader);
335 switch (data->version) {
336 case protocol_v2:
337 if (server_feature_v2("session-id", &server_sid))
338 trace2_data_string("transfer", NULL, "server-sid", server_sid);
339 if (must_list_refs)
340 get_remote_refs(data->fd[1], &reader, &refs, for_push,
341 options,
342 transport->server_options,
343 transport->stateless_rpc);
344 break;
345 case protocol_v1:
346 case protocol_v0:
347 die_if_server_options(transport);
348 get_remote_heads(&reader, &refs,
349 for_push ? REF_NORMAL : 0,
350 &data->extra_have,
351 &data->shallow);
352 server_sid = server_feature_value("session-id", &sid_len);
353 if (server_sid) {
354 char *sid = xstrndup(server_sid, sid_len);
355 trace2_data_string("transfer", NULL, "server-sid", sid);
356 free(sid);
358 break;
359 case protocol_unknown_version:
360 BUG("unknown protocol version");
362 data->finished_handshake = 1;
363 transport->hash_algo = reader.hash_algo;
365 if (reader.line_peeked)
366 BUG("buffer must be empty at the end of handshake()");
368 return refs;
371 static struct ref *get_refs_via_connect(struct transport *transport, int for_push,
372 struct transport_ls_refs_options *options)
374 return handshake(transport, for_push, options, 1);
377 static int get_bundle_uri(struct transport *transport)
379 struct git_transport_data *data = transport->data;
380 struct packet_reader reader;
381 int stateless_rpc = transport->stateless_rpc;
383 if (!transport->bundles) {
384 CALLOC_ARRAY(transport->bundles, 1);
385 init_bundle_list(transport->bundles);
388 if (!data->finished_handshake) {
389 struct ref *refs = handshake(transport, 0, NULL, 0);
391 if (refs)
392 free_refs(refs);
396 * "Support" protocol v0 and v2 without bundle-uri support by
397 * silently degrading to a NOOP.
399 if (!server_supports_v2("bundle-uri"))
400 return 0;
402 packet_reader_init(&reader, data->fd[0], NULL, 0,
403 PACKET_READ_CHOMP_NEWLINE |
404 PACKET_READ_GENTLE_ON_EOF);
406 return get_remote_bundle_uri(data->fd[1], &reader,
407 transport->bundles, stateless_rpc);
410 static int fetch_refs_via_pack(struct transport *transport,
411 int nr_heads, struct ref **to_fetch)
413 int ret = 0;
414 struct git_transport_data *data = transport->data;
415 struct ref *refs = NULL;
416 struct fetch_pack_args args;
417 struct ref *refs_tmp = NULL;
419 memset(&args, 0, sizeof(args));
420 args.uploadpack = data->options.uploadpack;
421 args.keep_pack = data->options.keep;
422 args.lock_pack = 1;
423 args.use_thin_pack = data->options.thin;
424 args.include_tag = data->options.followtags;
425 args.verbose = (transport->verbose > 1);
426 args.quiet = (transport->verbose < 0);
427 args.no_progress = !transport->progress;
428 args.depth = data->options.depth;
429 args.deepen_since = data->options.deepen_since;
430 args.deepen_not = data->options.deepen_not;
431 args.deepen_relative = data->options.deepen_relative;
432 args.check_self_contained_and_connected =
433 data->options.check_self_contained_and_connected;
434 args.cloning = transport->cloning;
435 args.update_shallow = data->options.update_shallow;
436 args.from_promisor = data->options.from_promisor;
437 list_objects_filter_copy(&args.filter_options,
438 &data->options.filter_options);
439 args.refetch = data->options.refetch;
440 args.stateless_rpc = transport->stateless_rpc;
441 args.server_options = transport->server_options;
442 args.negotiation_tips = data->options.negotiation_tips;
443 args.reject_shallow_remote = transport->smart_options->reject_shallow;
445 if (!data->finished_handshake) {
446 int i;
447 int must_list_refs = 0;
448 for (i = 0; i < nr_heads; i++) {
449 if (!to_fetch[i]->exact_oid) {
450 must_list_refs = 1;
451 break;
454 refs_tmp = handshake(transport, 0, NULL, must_list_refs);
457 if (data->version == protocol_unknown_version)
458 BUG("unknown protocol version");
459 else if (data->version <= protocol_v1)
460 die_if_server_options(transport);
462 if (data->options.acked_commits) {
463 if (data->version < protocol_v2) {
464 warning(_("--negotiate-only requires protocol v2"));
465 ret = -1;
466 } else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
467 warning(_("server does not support wait-for-done"));
468 ret = -1;
469 } else {
470 negotiate_using_fetch(data->options.negotiation_tips,
471 transport->server_options,
472 transport->stateless_rpc,
473 data->fd,
474 data->options.acked_commits);
475 ret = 0;
477 goto cleanup;
480 refs = fetch_pack(&args, data->fd,
481 refs_tmp ? refs_tmp : transport->remote_refs,
482 to_fetch, nr_heads, &data->shallow,
483 &transport->pack_lockfiles, data->version);
485 data->finished_handshake = 0;
486 data->options.self_contained_and_connected =
487 args.self_contained_and_connected;
488 data->options.connectivity_checked = args.connectivity_checked;
490 if (!refs)
491 ret = -1;
492 if (report_unmatched_refs(to_fetch, nr_heads))
493 ret = -1;
495 cleanup:
496 close(data->fd[0]);
497 if (data->fd[1] >= 0)
498 close(data->fd[1]);
499 if (finish_connect(data->conn))
500 ret = -1;
501 data->conn = NULL;
503 free_refs(refs_tmp);
504 free_refs(refs);
505 list_objects_filter_release(&args.filter_options);
506 return ret;
509 static int push_had_errors(struct ref *ref)
511 for (; ref; ref = ref->next) {
512 switch (ref->status) {
513 case REF_STATUS_NONE:
514 case REF_STATUS_UPTODATE:
515 case REF_STATUS_OK:
516 break;
517 default:
518 return 1;
521 return 0;
524 int transport_refs_pushed(struct ref *ref)
526 for (; ref; ref = ref->next) {
527 switch(ref->status) {
528 case REF_STATUS_NONE:
529 case REF_STATUS_UPTODATE:
530 break;
531 default:
532 return 1;
535 return 0;
538 static void update_one_tracking_ref(struct remote *remote, char *refname,
539 struct object_id *new_oid, int deletion,
540 int verbose)
542 struct refspec_item rs;
544 memset(&rs, 0, sizeof(rs));
545 rs.src = refname;
546 rs.dst = NULL;
548 if (!remote_find_tracking(remote, &rs)) {
549 if (verbose)
550 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
551 if (deletion)
552 refs_delete_ref(get_main_ref_store(the_repository),
553 NULL, rs.dst, NULL, 0);
554 else
555 refs_update_ref(get_main_ref_store(the_repository),
556 "update by push", rs.dst, new_oid,
557 NULL, 0, 0);
558 free(rs.dst);
562 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
564 char *refname;
565 struct object_id *new_oid;
566 struct ref_push_report *report;
568 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
569 return;
571 report = ref->report;
572 if (!report)
573 update_one_tracking_ref(remote, ref->name, &ref->new_oid,
574 ref->deletion, verbose);
575 else
576 for (; report; report = report->next) {
577 refname = report->ref_name ? (char *)report->ref_name : ref->name;
578 new_oid = report->new_oid ? report->new_oid : &ref->new_oid;
579 update_one_tracking_ref(remote, refname, new_oid,
580 is_null_oid(new_oid), verbose);
584 static void print_ref_status(char flag, const char *summary,
585 struct ref *to, struct ref *from, const char *msg,
586 struct ref_push_report *report,
587 int porcelain, int summary_width)
589 const char *to_name;
591 if (report && report->ref_name)
592 to_name = report->ref_name;
593 else
594 to_name = to->name;
596 if (porcelain) {
597 if (from)
598 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to_name);
599 else
600 fprintf(stdout, "%c\t:%s\t", flag, to_name);
601 if (msg)
602 fprintf(stdout, "%s (%s)\n", summary, msg);
603 else
604 fprintf(stdout, "%s\n", summary);
605 } else {
606 const char *red = "", *reset = "";
607 if (push_had_errors(to)) {
608 red = transport_get_color(TRANSPORT_COLOR_REJECTED);
609 reset = transport_get_color(TRANSPORT_COLOR_RESET);
611 fprintf(stderr, " %s%c %-*s%s ", red, flag, summary_width,
612 summary, reset);
613 if (from)
614 fprintf(stderr, "%s -> %s",
615 prettify_refname(from->name),
616 prettify_refname(to_name));
617 else
618 fputs(prettify_refname(to_name), stderr);
619 if (msg) {
620 fputs(" (", stderr);
621 fputs(msg, stderr);
622 fputc(')', stderr);
624 fputc('\n', stderr);
628 static void print_ok_ref_status(struct ref *ref,
629 struct ref_push_report *report,
630 int porcelain, int summary_width)
632 struct object_id *old_oid;
633 struct object_id *new_oid;
634 const char *ref_name;
635 int forced_update;
637 if (report && report->old_oid)
638 old_oid = report->old_oid;
639 else
640 old_oid = &ref->old_oid;
641 if (report && report->new_oid)
642 new_oid = report->new_oid;
643 else
644 new_oid = &ref->new_oid;
645 if (report && report->forced_update)
646 forced_update = report->forced_update;
647 else
648 forced_update = ref->forced_update;
649 if (report && report->ref_name)
650 ref_name = report->ref_name;
651 else
652 ref_name = ref->name;
654 if (ref->deletion)
655 print_ref_status('-', "[deleted]", ref, NULL, NULL,
656 report, porcelain, summary_width);
657 else if (is_null_oid(old_oid))
658 print_ref_status('*',
659 (starts_with(ref_name, "refs/tags/")
660 ? "[new tag]"
661 : (starts_with(ref_name, "refs/heads/")
662 ? "[new branch]"
663 : "[new reference]")),
664 ref, ref->peer_ref, NULL,
665 report, porcelain, summary_width);
666 else {
667 struct strbuf quickref = STRBUF_INIT;
668 char type;
669 const char *msg;
671 strbuf_add_unique_abbrev(&quickref, old_oid,
672 DEFAULT_ABBREV);
673 if (forced_update) {
674 strbuf_addstr(&quickref, "...");
675 type = '+';
676 msg = "forced update";
677 } else {
678 strbuf_addstr(&quickref, "..");
679 type = ' ';
680 msg = NULL;
682 strbuf_add_unique_abbrev(&quickref, new_oid,
683 DEFAULT_ABBREV);
685 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
686 report, porcelain, summary_width);
687 strbuf_release(&quickref);
691 static int print_one_push_report(struct ref *ref, const char *dest, int count,
692 struct ref_push_report *report,
693 int porcelain, int summary_width)
695 if (!count) {
696 char *url = transport_anonymize_url(dest);
697 fprintf(porcelain ? stdout : stderr, "To %s\n", url);
698 free(url);
701 switch(ref->status) {
702 case REF_STATUS_NONE:
703 print_ref_status('X', "[no match]", ref, NULL, NULL,
704 report, porcelain, summary_width);
705 break;
706 case REF_STATUS_REJECT_NODELETE:
707 print_ref_status('!', "[rejected]", ref, NULL,
708 "remote does not support deleting refs",
709 report, porcelain, summary_width);
710 break;
711 case REF_STATUS_UPTODATE:
712 print_ref_status('=', "[up to date]", ref,
713 ref->peer_ref, NULL,
714 report, porcelain, summary_width);
715 break;
716 case REF_STATUS_REJECT_NONFASTFORWARD:
717 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
718 "non-fast-forward",
719 report, porcelain, summary_width);
720 break;
721 case REF_STATUS_REJECT_ALREADY_EXISTS:
722 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
723 "already exists",
724 report, porcelain, summary_width);
725 break;
726 case REF_STATUS_REJECT_FETCH_FIRST:
727 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
728 "fetch first",
729 report, porcelain, summary_width);
730 break;
731 case REF_STATUS_REJECT_NEEDS_FORCE:
732 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
733 "needs force",
734 report, porcelain, summary_width);
735 break;
736 case REF_STATUS_REJECT_STALE:
737 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
738 "stale info",
739 report, porcelain, summary_width);
740 break;
741 case REF_STATUS_REJECT_REMOTE_UPDATED:
742 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
743 "remote ref updated since checkout",
744 report, porcelain, summary_width);
745 break;
746 case REF_STATUS_REJECT_SHALLOW:
747 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
748 "new shallow roots not allowed",
749 report, porcelain, summary_width);
750 break;
751 case REF_STATUS_REMOTE_REJECT:
752 print_ref_status('!', "[remote rejected]", ref,
753 ref->deletion ? NULL : ref->peer_ref,
754 ref->remote_status,
755 report, porcelain, summary_width);
756 break;
757 case REF_STATUS_EXPECTING_REPORT:
758 print_ref_status('!', "[remote failure]", ref,
759 ref->deletion ? NULL : ref->peer_ref,
760 "remote failed to report status",
761 report, porcelain, summary_width);
762 break;
763 case REF_STATUS_ATOMIC_PUSH_FAILED:
764 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
765 "atomic push failed",
766 report, porcelain, summary_width);
767 break;
768 case REF_STATUS_OK:
769 print_ok_ref_status(ref, report, porcelain, summary_width);
770 break;
773 return 1;
776 static int print_one_push_status(struct ref *ref, const char *dest, int count,
777 int porcelain, int summary_width)
779 struct ref_push_report *report;
780 int n = 0;
782 if (!ref->report)
783 return print_one_push_report(ref, dest, count,
784 NULL, porcelain, summary_width);
786 for (report = ref->report; report; report = report->next)
787 print_one_push_report(ref, dest, count + n++,
788 report, porcelain, summary_width);
789 return n;
792 static int measure_abbrev(const struct object_id *oid, int sofar)
794 char hex[GIT_MAX_HEXSZ + 1];
795 int w = repo_find_unique_abbrev_r(the_repository, hex, oid,
796 DEFAULT_ABBREV);
798 return (w < sofar) ? sofar : w;
801 int transport_summary_width(const struct ref *refs)
803 int maxw = -1;
805 for (; refs; refs = refs->next) {
806 maxw = measure_abbrev(&refs->old_oid, maxw);
807 maxw = measure_abbrev(&refs->new_oid, maxw);
809 if (maxw < 0)
810 maxw = FALLBACK_DEFAULT_ABBREV;
811 return (2 * maxw + 3);
814 void transport_print_push_status(const char *dest, struct ref *refs,
815 int verbose, int porcelain, unsigned int *reject_reasons)
817 struct ref *ref;
818 int n = 0;
819 char *head;
820 int summary_width = transport_summary_width(refs);
822 if (transport_color_config() < 0)
823 warning(_("could not parse transport.color.* config"));
825 head = refs_resolve_refdup(get_main_ref_store(the_repository), "HEAD",
826 RESOLVE_REF_READING, NULL, NULL);
828 if (verbose) {
829 for (ref = refs; ref; ref = ref->next)
830 if (ref->status == REF_STATUS_UPTODATE)
831 n += print_one_push_status(ref, dest, n,
832 porcelain, summary_width);
835 for (ref = refs; ref; ref = ref->next)
836 if (ref->status == REF_STATUS_OK)
837 n += print_one_push_status(ref, dest, n,
838 porcelain, summary_width);
840 *reject_reasons = 0;
841 for (ref = refs; ref; ref = ref->next) {
842 if (ref->status != REF_STATUS_NONE &&
843 ref->status != REF_STATUS_UPTODATE &&
844 ref->status != REF_STATUS_OK)
845 n += print_one_push_status(ref, dest, n,
846 porcelain, summary_width);
847 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
848 if (head != NULL && !strcmp(head, ref->name))
849 *reject_reasons |= REJECT_NON_FF_HEAD;
850 else
851 *reject_reasons |= REJECT_NON_FF_OTHER;
852 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
853 *reject_reasons |= REJECT_ALREADY_EXISTS;
854 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
855 *reject_reasons |= REJECT_FETCH_FIRST;
856 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
857 *reject_reasons |= REJECT_NEEDS_FORCE;
858 } else if (ref->status == REF_STATUS_REJECT_REMOTE_UPDATED) {
859 *reject_reasons |= REJECT_REF_NEEDS_UPDATE;
862 free(head);
865 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
867 struct git_transport_data *data = transport->data;
868 struct send_pack_args args;
869 int ret = 0;
871 if (transport_color_config() < 0)
872 return -1;
874 if (!data->finished_handshake)
875 get_refs_via_connect(transport, 1, NULL);
877 memset(&args, 0, sizeof(args));
878 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
879 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
880 args.use_thin_pack = data->options.thin;
881 args.verbose = (transport->verbose > 0);
882 args.quiet = (transport->verbose < 0);
883 args.progress = transport->progress;
884 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
885 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
886 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
887 args.push_options = transport->push_options;
888 args.url = transport->url;
890 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
891 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
892 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
893 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
894 else
895 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
897 switch (data->version) {
898 case protocol_v2:
899 die(_("support for protocol v2 not implemented yet"));
900 break;
901 case protocol_v1:
902 case protocol_v0:
903 ret = send_pack(&args, data->fd, data->conn, remote_refs,
904 &data->extra_have);
905 break;
906 case protocol_unknown_version:
907 BUG("unknown protocol version");
910 close(data->fd[1]);
911 close(data->fd[0]);
913 * Atomic push may abort the connection early and close the pipe,
914 * which may cause an error for `finish_connect()`. Ignore this error
915 * for atomic git-push.
917 if (ret || args.atomic)
918 finish_connect(data->conn);
919 else
920 ret = finish_connect(data->conn);
921 data->conn = NULL;
922 data->finished_handshake = 0;
924 return ret;
927 static int connect_git(struct transport *transport, const char *name,
928 const char *executable, int fd[2])
930 struct git_transport_data *data = transport->data;
931 data->conn = git_connect(data->fd, transport->url,
932 name, executable, 0);
933 fd[0] = data->fd[0];
934 fd[1] = data->fd[1];
935 return 0;
938 static int disconnect_git(struct transport *transport)
940 struct git_transport_data *data = transport->data;
941 if (data->conn) {
942 if (data->finished_handshake && !transport->stateless_rpc)
943 packet_flush(data->fd[1]);
944 close(data->fd[0]);
945 if (data->fd[1] >= 0)
946 close(data->fd[1]);
947 finish_connect(data->conn);
950 if (data->options.negotiation_tips) {
951 oid_array_clear(data->options.negotiation_tips);
952 free(data->options.negotiation_tips);
954 list_objects_filter_release(&data->options.filter_options);
955 oid_array_clear(&data->extra_have);
956 oid_array_clear(&data->shallow);
957 free(data);
958 return 0;
961 static struct transport_vtable taken_over_vtable = {
962 .get_refs_list = get_refs_via_connect,
963 .get_bundle_uri = get_bundle_uri,
964 .fetch_refs = fetch_refs_via_pack,
965 .push_refs = git_transport_push,
966 .disconnect = disconnect_git
969 void transport_take_over(struct transport *transport,
970 struct child_process *child)
972 struct git_transport_data *data;
974 if (!transport->smart_options)
975 BUG("taking over transport requires non-NULL "
976 "smart_options field.");
978 CALLOC_ARRAY(data, 1);
979 data->options = *transport->smart_options;
980 data->conn = child;
981 data->fd[0] = data->conn->out;
982 data->fd[1] = data->conn->in;
983 data->finished_handshake = 0;
984 transport->data = data;
986 transport->vtable = &taken_over_vtable;
987 transport->smart_options = &(data->options);
989 transport->cannot_reuse = 1;
992 static int is_file(const char *url)
994 struct stat buf;
995 if (stat(url, &buf))
996 return 0;
997 return S_ISREG(buf.st_mode);
1000 static int external_specification_len(const char *url)
1002 return strchr(url, ':') - url;
1005 static const struct string_list *protocol_allow_list(void)
1007 static int enabled = -1;
1008 static struct string_list allowed = STRING_LIST_INIT_DUP;
1010 if (enabled < 0) {
1011 const char *v = getenv("GIT_ALLOW_PROTOCOL");
1012 if (v) {
1013 string_list_split(&allowed, v, ':', -1);
1014 string_list_sort(&allowed);
1015 enabled = 1;
1016 } else {
1017 enabled = 0;
1021 return enabled ? &allowed : NULL;
1024 enum protocol_allow_config {
1025 PROTOCOL_ALLOW_NEVER = 0,
1026 PROTOCOL_ALLOW_USER_ONLY,
1027 PROTOCOL_ALLOW_ALWAYS
1030 static enum protocol_allow_config parse_protocol_config(const char *key,
1031 const char *value)
1033 if (!strcasecmp(value, "always"))
1034 return PROTOCOL_ALLOW_ALWAYS;
1035 else if (!strcasecmp(value, "never"))
1036 return PROTOCOL_ALLOW_NEVER;
1037 else if (!strcasecmp(value, "user"))
1038 return PROTOCOL_ALLOW_USER_ONLY;
1040 die(_("unknown value for config '%s': %s"), key, value);
1043 static enum protocol_allow_config get_protocol_config(const char *type)
1045 char *key = xstrfmt("protocol.%s.allow", type);
1046 char *value;
1048 /* first check the per-protocol config */
1049 if (!git_config_get_string(key, &value)) {
1050 enum protocol_allow_config ret =
1051 parse_protocol_config(key, value);
1052 free(key);
1053 free(value);
1054 return ret;
1056 free(key);
1058 /* if defined, fallback to user-defined default for unknown protocols */
1059 if (!git_config_get_string("protocol.allow", &value)) {
1060 enum protocol_allow_config ret =
1061 parse_protocol_config("protocol.allow", value);
1062 free(value);
1063 return ret;
1066 /* fallback to built-in defaults */
1067 /* known safe */
1068 if (!strcmp(type, "http") ||
1069 !strcmp(type, "https") ||
1070 !strcmp(type, "git") ||
1071 !strcmp(type, "ssh"))
1072 return PROTOCOL_ALLOW_ALWAYS;
1074 /* known scary; err on the side of caution */
1075 if (!strcmp(type, "ext"))
1076 return PROTOCOL_ALLOW_NEVER;
1078 /* unknown; by default let them be used only directly by the user */
1079 return PROTOCOL_ALLOW_USER_ONLY;
1082 int is_transport_allowed(const char *type, int from_user)
1084 const struct string_list *allow_list = protocol_allow_list();
1085 if (allow_list)
1086 return string_list_has_string(allow_list, type);
1088 switch (get_protocol_config(type)) {
1089 case PROTOCOL_ALLOW_ALWAYS:
1090 return 1;
1091 case PROTOCOL_ALLOW_NEVER:
1092 return 0;
1093 case PROTOCOL_ALLOW_USER_ONLY:
1094 if (from_user < 0)
1095 from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1096 return from_user;
1099 BUG("invalid protocol_allow_config type");
1102 void transport_check_allowed(const char *type)
1104 if (!is_transport_allowed(type, -1))
1105 die(_("transport '%s' not allowed"), type);
1108 static struct transport_vtable bundle_vtable = {
1109 .get_refs_list = get_refs_from_bundle,
1110 .fetch_refs = fetch_refs_from_bundle,
1111 .disconnect = close_bundle
1114 static struct transport_vtable builtin_smart_vtable = {
1115 .get_refs_list = get_refs_via_connect,
1116 .get_bundle_uri = get_bundle_uri,
1117 .fetch_refs = fetch_refs_via_pack,
1118 .push_refs = git_transport_push,
1119 .connect = connect_git,
1120 .disconnect = disconnect_git
1123 struct transport *transport_get(struct remote *remote, const char *url)
1125 const char *helper;
1126 char *helper_to_free = NULL;
1127 const char *p;
1128 struct transport *ret = xcalloc(1, sizeof(*ret));
1130 ret->progress = isatty(2);
1131 string_list_init_dup(&ret->pack_lockfiles);
1133 CALLOC_ARRAY(ret->bundles, 1);
1134 init_bundle_list(ret->bundles);
1136 if (!remote)
1137 BUG("No remote provided to transport_get()");
1139 ret->got_remote_refs = 0;
1140 ret->remote = remote;
1141 helper = remote->foreign_vcs;
1143 if (!url)
1144 url = remote->url.v[0];
1145 ret->url = url;
1147 p = url;
1148 while (is_urlschemechar(p == url, *p))
1149 p++;
1150 if (starts_with(p, "::"))
1151 helper = helper_to_free = xstrndup(url, p - url);
1153 if (helper) {
1154 transport_helper_init(ret, helper);
1155 free(helper_to_free);
1156 } else if (starts_with(url, "rsync:")) {
1157 die(_("git-over-rsync is no longer supported"));
1158 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
1159 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
1160 bundle_header_init(&data->header);
1161 transport_check_allowed("file");
1162 ret->data = data;
1163 ret->vtable = &bundle_vtable;
1164 ret->smart_options = NULL;
1165 } else if (!is_url(url)
1166 || starts_with(url, "file://")
1167 || starts_with(url, "git://")
1168 || starts_with(url, "ssh://")
1169 || starts_with(url, "git+ssh://") /* deprecated - do not use */
1170 || starts_with(url, "ssh+git://") /* deprecated - do not use */
1173 * These are builtin smart transports; "allowed" transports
1174 * will be checked individually in git_connect.
1176 struct git_transport_data *data = xcalloc(1, sizeof(*data));
1177 list_objects_filter_init(&data->options.filter_options);
1178 ret->data = data;
1179 ret->vtable = &builtin_smart_vtable;
1180 ret->smart_options = &(data->options);
1182 data->conn = NULL;
1183 data->finished_handshake = 0;
1184 } else {
1185 /* Unknown protocol in URL. Pass to external handler. */
1186 int len = external_specification_len(url);
1187 char *handler = xmemdupz(url, len);
1188 transport_helper_init(ret, handler);
1189 free(handler);
1192 if (ret->smart_options) {
1193 ret->smart_options->thin = 1;
1194 ret->smart_options->uploadpack = "git-upload-pack";
1195 if (remote->uploadpack)
1196 ret->smart_options->uploadpack = remote->uploadpack;
1197 ret->smart_options->receivepack = "git-receive-pack";
1198 if (remote->receivepack)
1199 ret->smart_options->receivepack = remote->receivepack;
1202 ret->hash_algo = &hash_algos[GIT_HASH_SHA1];
1204 return ret;
1207 const struct git_hash_algo *transport_get_hash_algo(struct transport *transport)
1209 return transport->hash_algo;
1212 int transport_set_option(struct transport *transport,
1213 const char *name, const char *value)
1215 int git_reports = 1, protocol_reports = 1;
1217 if (transport->smart_options)
1218 git_reports = set_git_option(transport->smart_options,
1219 name, value);
1221 if (transport->vtable->set_option)
1222 protocol_reports = transport->vtable->set_option(transport,
1223 name, value);
1225 /* If either report is 0, report 0 (success). */
1226 if (!git_reports || !protocol_reports)
1227 return 0;
1228 /* If either reports -1 (invalid value), report -1. */
1229 if ((git_reports == -1) || (protocol_reports == -1))
1230 return -1;
1231 /* Otherwise if both report unknown, report unknown. */
1232 return 1;
1235 void transport_set_verbosity(struct transport *transport, int verbosity,
1236 int force_progress)
1238 if (verbosity >= 1)
1239 transport->verbose = verbosity <= 3 ? verbosity : 3;
1240 if (verbosity < 0)
1241 transport->verbose = -1;
1244 * Rules used to determine whether to report progress (processing aborts
1245 * when a rule is satisfied):
1247 * . Report progress, if force_progress is 1 (ie. --progress).
1248 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1249 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1250 * . Report progress if isatty(2) is 1.
1252 if (force_progress >= 0)
1253 transport->progress = !!force_progress;
1254 else
1255 transport->progress = verbosity >= 0 && isatty(2);
1258 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
1260 int i;
1262 fprintf(stderr, _("The following submodule paths contain changes that can\n"
1263 "not be found on any remote:\n"));
1264 for (i = 0; i < needs_pushing->nr; i++)
1265 fprintf(stderr, " %s\n", needs_pushing->items[i].string);
1266 fprintf(stderr, _("\nPlease try\n\n"
1267 " git push --recurse-submodules=on-demand\n\n"
1268 "or cd to the path and use\n\n"
1269 " git push\n\n"
1270 "to push them to a remote.\n\n"));
1272 string_list_clear(needs_pushing, 0);
1274 die(_("Aborting."));
1277 static int run_pre_push_hook(struct transport *transport,
1278 struct ref *remote_refs)
1280 int ret = 0, x;
1281 struct ref *r;
1282 struct child_process proc = CHILD_PROCESS_INIT;
1283 struct strbuf buf;
1284 const char *hook_path = find_hook(the_repository, "pre-push");
1286 if (!hook_path)
1287 return 0;
1289 strvec_push(&proc.args, hook_path);
1290 strvec_push(&proc.args, transport->remote->name);
1291 strvec_push(&proc.args, transport->url);
1293 proc.in = -1;
1294 proc.trace2_hook_name = "pre-push";
1296 if (start_command(&proc)) {
1297 finish_command(&proc);
1298 return -1;
1301 sigchain_push(SIGPIPE, SIG_IGN);
1303 strbuf_init(&buf, 256);
1305 for (r = remote_refs; r; r = r->next) {
1306 if (!r->peer_ref) continue;
1307 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
1308 if (r->status == REF_STATUS_REJECT_STALE) continue;
1309 if (r->status == REF_STATUS_REJECT_REMOTE_UPDATED) continue;
1310 if (r->status == REF_STATUS_UPTODATE) continue;
1312 strbuf_reset(&buf);
1313 strbuf_addf( &buf, "%s %s %s %s\n",
1314 r->peer_ref->name, oid_to_hex(&r->new_oid),
1315 r->name, oid_to_hex(&r->old_oid));
1317 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
1318 /* We do not mind if a hook does not read all refs. */
1319 if (errno != EPIPE)
1320 ret = -1;
1321 break;
1325 strbuf_release(&buf);
1327 x = close(proc.in);
1328 if (!ret)
1329 ret = x;
1331 sigchain_pop(SIGPIPE);
1333 x = finish_command(&proc);
1334 if (!ret)
1335 ret = x;
1337 return ret;
1340 int transport_push(struct repository *r,
1341 struct transport *transport,
1342 struct refspec *rs, int flags,
1343 unsigned int *reject_reasons)
1345 struct ref *remote_refs = NULL;
1346 struct ref *local_refs = NULL;
1347 int match_flags = MATCH_REFS_NONE;
1348 int verbose = (transport->verbose > 0);
1349 int quiet = (transport->verbose < 0);
1350 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1351 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1352 int push_ret, err;
1353 int ret = -1;
1354 struct transport_ls_refs_options transport_options =
1355 TRANSPORT_LS_REFS_OPTIONS_INIT;
1357 *reject_reasons = 0;
1359 if (transport_color_config() < 0)
1360 goto done;
1362 if (!transport->vtable->push_refs)
1363 goto done;
1365 local_refs = get_local_heads();
1367 if (check_push_refs(local_refs, rs) < 0)
1368 goto done;
1370 refspec_ref_prefixes(rs, &transport_options.ref_prefixes);
1372 trace2_region_enter("transport_push", "get_refs_list", r);
1373 remote_refs = transport->vtable->get_refs_list(transport, 1,
1374 &transport_options);
1375 trace2_region_leave("transport_push", "get_refs_list", r);
1377 transport_ls_refs_options_release(&transport_options);
1379 if (flags & TRANSPORT_PUSH_ALL)
1380 match_flags |= MATCH_REFS_ALL;
1381 if (flags & TRANSPORT_PUSH_MIRROR)
1382 match_flags |= MATCH_REFS_MIRROR;
1383 if (flags & TRANSPORT_PUSH_PRUNE)
1384 match_flags |= MATCH_REFS_PRUNE;
1385 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1386 match_flags |= MATCH_REFS_FOLLOW_TAGS;
1388 if (match_push_refs(local_refs, &remote_refs, rs, match_flags))
1389 goto done;
1391 if (transport->smart_options &&
1392 transport->smart_options->cas &&
1393 !is_empty_cas(transport->smart_options->cas))
1394 apply_push_cas(transport->smart_options->cas,
1395 transport->remote, remote_refs);
1397 set_ref_status_for_push(remote_refs,
1398 flags & TRANSPORT_PUSH_MIRROR,
1399 flags & TRANSPORT_PUSH_FORCE);
1401 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1402 if (run_pre_push_hook(transport, remote_refs))
1403 goto done;
1405 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1406 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1407 !is_bare_repository()) {
1408 struct ref *ref = remote_refs;
1409 struct oid_array commits = OID_ARRAY_INIT;
1411 trace2_region_enter("transport_push", "push_submodules", r);
1412 for (; ref; ref = ref->next)
1413 if (!is_null_oid(&ref->new_oid))
1414 oid_array_append(&commits,
1415 &ref->new_oid);
1417 if (!push_unpushed_submodules(r,
1418 &commits,
1419 transport->remote,
1421 transport->push_options,
1422 pretend)) {
1423 oid_array_clear(&commits);
1424 trace2_region_leave("transport_push", "push_submodules", r);
1425 die(_("failed to push all needed submodules"));
1427 oid_array_clear(&commits);
1428 trace2_region_leave("transport_push", "push_submodules", r);
1431 if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1432 ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1433 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1434 !pretend)) && !is_bare_repository()) {
1435 struct ref *ref = remote_refs;
1436 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1437 struct oid_array commits = OID_ARRAY_INIT;
1439 trace2_region_enter("transport_push", "check_submodules", r);
1440 for (; ref; ref = ref->next)
1441 if (!is_null_oid(&ref->new_oid))
1442 oid_array_append(&commits,
1443 &ref->new_oid);
1445 if (find_unpushed_submodules(r,
1446 &commits,
1447 transport->remote->name,
1448 &needs_pushing)) {
1449 oid_array_clear(&commits);
1450 trace2_region_leave("transport_push", "check_submodules", r);
1451 die_with_unpushed_submodules(&needs_pushing);
1453 string_list_clear(&needs_pushing, 0);
1454 oid_array_clear(&commits);
1455 trace2_region_leave("transport_push", "check_submodules", r);
1458 if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY)) {
1459 trace2_region_enter("transport_push", "push_refs", r);
1460 push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1461 trace2_region_leave("transport_push", "push_refs", r);
1462 } else
1463 push_ret = 0;
1464 err = push_had_errors(remote_refs);
1465 ret = push_ret | err;
1467 if (!quiet || err)
1468 transport_print_push_status(transport->url, remote_refs,
1469 verbose | porcelain, porcelain,
1470 reject_reasons);
1472 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1473 set_upstreams(transport, remote_refs, pretend);
1475 if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1476 TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1477 struct ref *ref;
1478 for (ref = remote_refs; ref; ref = ref->next)
1479 transport_update_tracking_ref(transport->remote, ref, verbose);
1482 if (porcelain && !push_ret)
1483 puts("Done");
1484 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1485 /* stable plumbing output; do not modify or localize */
1486 fprintf(stderr, "Everything up-to-date\n");
1488 done:
1489 free_refs(local_refs);
1490 free_refs(remote_refs);
1491 return ret;
1494 const struct ref *transport_get_remote_refs(struct transport *transport,
1495 struct transport_ls_refs_options *transport_options)
1497 if (!transport->got_remote_refs) {
1498 transport->remote_refs =
1499 transport->vtable->get_refs_list(transport, 0,
1500 transport_options);
1501 transport->got_remote_refs = 1;
1504 return transport->remote_refs;
1507 void transport_ls_refs_options_release(struct transport_ls_refs_options *opts)
1509 strvec_clear(&opts->ref_prefixes);
1510 free((char *)opts->unborn_head_target);
1513 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1515 int rc;
1516 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1517 struct ref **heads = NULL;
1518 struct ref *rm;
1520 for (rm = refs; rm; rm = rm->next) {
1521 nr_refs++;
1522 if (rm->peer_ref &&
1523 !is_null_oid(&rm->old_oid) &&
1524 oideq(&rm->peer_ref->old_oid, &rm->old_oid))
1525 continue;
1526 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1527 heads[nr_heads++] = rm;
1530 if (!nr_heads) {
1532 * When deepening of a shallow repository is requested,
1533 * then local and remote refs are likely to still be equal.
1534 * Just feed them all to the fetch method in that case.
1535 * This condition shouldn't be met in a non-deepening fetch
1536 * (see builtin/fetch.c:quickfetch()).
1538 ALLOC_ARRAY(heads, nr_refs);
1539 for (rm = refs; rm; rm = rm->next)
1540 heads[nr_heads++] = rm;
1543 rc = transport->vtable->fetch_refs(transport, nr_heads, heads);
1545 free(heads);
1546 return rc;
1549 int transport_get_remote_bundle_uri(struct transport *transport)
1551 int value = 0;
1552 const struct transport_vtable *vtable = transport->vtable;
1554 /* Check config only once. */
1555 if (transport->got_remote_bundle_uri)
1556 return 0;
1557 transport->got_remote_bundle_uri = 1;
1560 * Don't request bundle-uri from the server unless configured to
1561 * do so by the transfer.bundleURI=true config option.
1563 if (git_config_get_bool("transfer.bundleuri", &value) || !value)
1564 return 0;
1566 if (!transport->bundles->baseURI)
1567 transport->bundles->baseURI = xstrdup(transport->url);
1569 if (!vtable->get_bundle_uri)
1570 return error(_("bundle-uri operation not supported by protocol"));
1572 if (vtable->get_bundle_uri(transport) < 0)
1573 return error(_("could not retrieve server-advertised bundle-uri list"));
1574 return 0;
1577 void transport_unlock_pack(struct transport *transport, unsigned int flags)
1579 int in_signal_handler = !!(flags & TRANSPORT_UNLOCK_PACK_IN_SIGNAL_HANDLER);
1580 int i;
1582 for (i = 0; i < transport->pack_lockfiles.nr; i++)
1583 if (in_signal_handler)
1584 unlink(transport->pack_lockfiles.items[i].string);
1585 else
1586 unlink_or_warn(transport->pack_lockfiles.items[i].string);
1587 if (!in_signal_handler)
1588 string_list_clear(&transport->pack_lockfiles, 0);
1591 int transport_connect(struct transport *transport, const char *name,
1592 const char *exec, int fd[2])
1594 if (transport->vtable->connect)
1595 return transport->vtable->connect(transport, name, exec, fd);
1596 else
1597 die(_("operation not supported by protocol"));
1600 int transport_disconnect(struct transport *transport)
1602 int ret = 0;
1603 if (transport->vtable->disconnect)
1604 ret = transport->vtable->disconnect(transport);
1605 if (transport->got_remote_refs)
1606 free_refs((void *)transport->remote_refs);
1607 clear_bundle_list(transport->bundles);
1608 free(transport->bundles);
1609 free(transport);
1610 return ret;
1614 * Strip username (and password) from a URL and return
1615 * it in a newly allocated string.
1617 char *transport_anonymize_url(const char *url)
1619 char *scheme_prefix, *anon_part;
1620 size_t anon_len, prefix_len = 0;
1622 anon_part = strchr(url, '@');
1623 if (url_is_local_not_ssh(url) || !anon_part)
1624 goto literal_copy;
1626 anon_len = strlen(++anon_part);
1627 scheme_prefix = strstr(url, "://");
1628 if (!scheme_prefix) {
1629 if (!strchr(anon_part, ':'))
1630 /* cannot be "me@there:/path/name" */
1631 goto literal_copy;
1632 } else {
1633 const char *cp;
1634 /* make sure scheme is reasonable */
1635 for (cp = url; cp < scheme_prefix; cp++) {
1636 switch (*cp) {
1637 /* RFC 1738 2.1 */
1638 case '+': case '.': case '-':
1639 break; /* ok */
1640 default:
1641 if (isalnum(*cp))
1642 break;
1643 /* it isn't */
1644 goto literal_copy;
1647 /* @ past the first slash does not count */
1648 cp = strchr(scheme_prefix + 3, '/');
1649 if (cp && cp < anon_part)
1650 goto literal_copy;
1651 prefix_len = scheme_prefix - url + 3;
1653 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1654 (int)anon_len, anon_part);
1655 literal_copy:
1656 return xstrdup(url);