Merge branch 'jc/t3206-test-when-finished-fix'
[git/gitster.git] / transport.c
blob7c4af9f56f2c10366acd398ed81df7090d35266f
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;
192 return ret;
195 static int close_bundle(struct transport *transport)
197 struct bundle_transport_data *data = transport->data;
198 if (data->fd > 0)
199 close(data->fd);
200 bundle_header_release(&data->header);
201 free(data);
202 return 0;
205 struct git_transport_data {
206 struct git_transport_options options;
207 struct child_process *conn;
208 int fd[2];
209 unsigned finished_handshake : 1;
210 enum protocol_version version;
211 struct oid_array extra_have;
212 struct oid_array shallow;
215 static int set_git_option(struct git_transport_options *opts,
216 const char *name, const char *value)
218 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
219 opts->uploadpack = value;
220 return 0;
221 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
222 opts->receivepack = value;
223 return 0;
224 } else if (!strcmp(name, TRANS_OPT_THIN)) {
225 opts->thin = !!value;
226 return 0;
227 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
228 opts->followtags = !!value;
229 return 0;
230 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
231 opts->keep = !!value;
232 return 0;
233 } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
234 opts->update_shallow = !!value;
235 return 0;
236 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
237 if (!value)
238 opts->depth = 0;
239 else {
240 char *end;
241 opts->depth = strtol(value, &end, 0);
242 if (*end)
243 die(_("transport: invalid depth option '%s'"), value);
245 return 0;
246 } else if (!strcmp(name, TRANS_OPT_DEEPEN_SINCE)) {
247 opts->deepen_since = value;
248 return 0;
249 } else if (!strcmp(name, TRANS_OPT_DEEPEN_NOT)) {
250 opts->deepen_not = (const struct string_list *)value;
251 return 0;
252 } else if (!strcmp(name, TRANS_OPT_DEEPEN_RELATIVE)) {
253 opts->deepen_relative = !!value;
254 return 0;
255 } else if (!strcmp(name, TRANS_OPT_FROM_PROMISOR)) {
256 opts->from_promisor = !!value;
257 return 0;
258 } else if (!strcmp(name, TRANS_OPT_LIST_OBJECTS_FILTER)) {
259 list_objects_filter_die_if_populated(&opts->filter_options);
260 parse_list_objects_filter(&opts->filter_options, value);
261 return 0;
262 } else if (!strcmp(name, TRANS_OPT_REFETCH)) {
263 opts->refetch = !!value;
264 return 0;
265 } else if (!strcmp(name, TRANS_OPT_REJECT_SHALLOW)) {
266 opts->reject_shallow = !!value;
267 return 0;
269 return 1;
272 static int connect_setup(struct transport *transport, int for_push)
274 struct git_transport_data *data = transport->data;
275 int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
277 if (data->conn)
278 return 0;
280 switch (transport->family) {
281 case TRANSPORT_FAMILY_ALL: break;
282 case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
283 case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
286 data->conn = git_connect(data->fd, transport->url,
287 for_push ?
288 "git-receive-pack" :
289 "git-upload-pack",
290 for_push ?
291 data->options.receivepack :
292 data->options.uploadpack,
293 flags);
295 return 0;
298 static void die_if_server_options(struct transport *transport)
300 if (!transport->server_options || !transport->server_options->nr)
301 return;
302 advise(_("see protocol.version in 'git help config' for more details"));
303 die(_("server options require protocol version 2 or later"));
307 * Obtains the protocol version from the transport and writes it to
308 * transport->data->version, first connecting if not already connected.
310 * If the protocol version is one that allows skipping the listing of remote
311 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
312 * this function returns NULL. Otherwise, this function returns the list of
313 * remote refs.
315 static struct ref *handshake(struct transport *transport, int for_push,
316 struct transport_ls_refs_options *options,
317 int must_list_refs)
319 struct git_transport_data *data = transport->data;
320 struct ref *refs = NULL;
321 struct packet_reader reader;
322 size_t sid_len;
323 const char *server_sid;
325 connect_setup(transport, for_push);
327 packet_reader_init(&reader, data->fd[0], NULL, 0,
328 PACKET_READ_CHOMP_NEWLINE |
329 PACKET_READ_GENTLE_ON_EOF |
330 PACKET_READ_DIE_ON_ERR_PACKET);
332 data->version = discover_version(&reader);
333 switch (data->version) {
334 case protocol_v2:
335 if (server_feature_v2("session-id", &server_sid))
336 trace2_data_string("transfer", NULL, "server-sid", server_sid);
337 if (must_list_refs)
338 get_remote_refs(data->fd[1], &reader, &refs, for_push,
339 options,
340 transport->server_options,
341 transport->stateless_rpc);
342 break;
343 case protocol_v1:
344 case protocol_v0:
345 die_if_server_options(transport);
346 get_remote_heads(&reader, &refs,
347 for_push ? REF_NORMAL : 0,
348 &data->extra_have,
349 &data->shallow);
350 server_sid = server_feature_value("session-id", &sid_len);
351 if (server_sid) {
352 char *sid = xstrndup(server_sid, sid_len);
353 trace2_data_string("transfer", NULL, "server-sid", sid);
354 free(sid);
356 break;
357 case protocol_unknown_version:
358 BUG("unknown protocol version");
360 data->finished_handshake = 1;
361 transport->hash_algo = reader.hash_algo;
363 if (reader.line_peeked)
364 BUG("buffer must be empty at the end of handshake()");
366 return refs;
369 static struct ref *get_refs_via_connect(struct transport *transport, int for_push,
370 struct transport_ls_refs_options *options)
372 return handshake(transport, for_push, options, 1);
375 static int get_bundle_uri(struct transport *transport)
377 struct git_transport_data *data = transport->data;
378 struct packet_reader reader;
379 int stateless_rpc = transport->stateless_rpc;
381 if (!transport->bundles) {
382 CALLOC_ARRAY(transport->bundles, 1);
383 init_bundle_list(transport->bundles);
386 if (!data->finished_handshake) {
387 struct ref *refs = handshake(transport, 0, NULL, 0);
389 if (refs)
390 free_refs(refs);
394 * "Support" protocol v0 and v2 without bundle-uri support by
395 * silently degrading to a NOOP.
397 if (!server_supports_v2("bundle-uri"))
398 return 0;
400 packet_reader_init(&reader, data->fd[0], NULL, 0,
401 PACKET_READ_CHOMP_NEWLINE |
402 PACKET_READ_GENTLE_ON_EOF);
404 return get_remote_bundle_uri(data->fd[1], &reader,
405 transport->bundles, stateless_rpc);
408 static int fetch_refs_via_pack(struct transport *transport,
409 int nr_heads, struct ref **to_fetch)
411 int ret = 0;
412 struct git_transport_data *data = transport->data;
413 struct ref *refs = NULL;
414 struct fetch_pack_args args;
415 struct ref *refs_tmp = NULL;
417 memset(&args, 0, sizeof(args));
418 args.uploadpack = data->options.uploadpack;
419 args.keep_pack = data->options.keep;
420 args.lock_pack = 1;
421 args.use_thin_pack = data->options.thin;
422 args.include_tag = data->options.followtags;
423 args.verbose = (transport->verbose > 1);
424 args.quiet = (transport->verbose < 0);
425 args.no_progress = !transport->progress;
426 args.depth = data->options.depth;
427 args.deepen_since = data->options.deepen_since;
428 args.deepen_not = data->options.deepen_not;
429 args.deepen_relative = data->options.deepen_relative;
430 args.check_self_contained_and_connected =
431 data->options.check_self_contained_and_connected;
432 args.cloning = transport->cloning;
433 args.update_shallow = data->options.update_shallow;
434 args.from_promisor = data->options.from_promisor;
435 list_objects_filter_copy(&args.filter_options,
436 &data->options.filter_options);
437 args.refetch = data->options.refetch;
438 args.stateless_rpc = transport->stateless_rpc;
439 args.server_options = transport->server_options;
440 args.negotiation_tips = data->options.negotiation_tips;
441 args.reject_shallow_remote = transport->smart_options->reject_shallow;
443 if (!data->finished_handshake) {
444 int i;
445 int must_list_refs = 0;
446 for (i = 0; i < nr_heads; i++) {
447 if (!to_fetch[i]->exact_oid) {
448 must_list_refs = 1;
449 break;
452 refs_tmp = handshake(transport, 0, NULL, must_list_refs);
455 if (data->version == protocol_unknown_version)
456 BUG("unknown protocol version");
457 else if (data->version <= protocol_v1)
458 die_if_server_options(transport);
460 if (data->options.acked_commits) {
461 if (data->version < protocol_v2) {
462 warning(_("--negotiate-only requires protocol v2"));
463 ret = -1;
464 } else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
465 warning(_("server does not support wait-for-done"));
466 ret = -1;
467 } else {
468 negotiate_using_fetch(data->options.negotiation_tips,
469 transport->server_options,
470 transport->stateless_rpc,
471 data->fd,
472 data->options.acked_commits);
473 ret = 0;
475 goto cleanup;
478 refs = fetch_pack(&args, data->fd,
479 refs_tmp ? refs_tmp : transport->remote_refs,
480 to_fetch, nr_heads, &data->shallow,
481 &transport->pack_lockfiles, data->version);
483 data->finished_handshake = 0;
484 data->options.self_contained_and_connected =
485 args.self_contained_and_connected;
486 data->options.connectivity_checked = args.connectivity_checked;
488 if (!refs)
489 ret = -1;
490 if (report_unmatched_refs(to_fetch, nr_heads))
491 ret = -1;
493 cleanup:
494 close(data->fd[0]);
495 if (data->fd[1] >= 0)
496 close(data->fd[1]);
497 if (finish_connect(data->conn))
498 ret = -1;
499 data->conn = NULL;
501 free_refs(refs_tmp);
502 free_refs(refs);
503 list_objects_filter_release(&args.filter_options);
504 return ret;
507 static int push_had_errors(struct ref *ref)
509 for (; ref; ref = ref->next) {
510 switch (ref->status) {
511 case REF_STATUS_NONE:
512 case REF_STATUS_UPTODATE:
513 case REF_STATUS_OK:
514 break;
515 default:
516 return 1;
519 return 0;
522 int transport_refs_pushed(struct ref *ref)
524 for (; ref; ref = ref->next) {
525 switch(ref->status) {
526 case REF_STATUS_NONE:
527 case REF_STATUS_UPTODATE:
528 break;
529 default:
530 return 1;
533 return 0;
536 static void update_one_tracking_ref(struct remote *remote, char *refname,
537 struct object_id *new_oid, int deletion,
538 int verbose)
540 struct refspec_item rs;
542 memset(&rs, 0, sizeof(rs));
543 rs.src = refname;
544 rs.dst = NULL;
546 if (!remote_find_tracking(remote, &rs)) {
547 if (verbose)
548 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
549 if (deletion)
550 refs_delete_ref(get_main_ref_store(the_repository),
551 NULL, rs.dst, NULL, 0);
552 else
553 refs_update_ref(get_main_ref_store(the_repository),
554 "update by push", rs.dst, new_oid,
555 NULL, 0, 0);
556 free(rs.dst);
560 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
562 char *refname;
563 struct object_id *new_oid;
564 struct ref_push_report *report;
566 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
567 return;
569 report = ref->report;
570 if (!report)
571 update_one_tracking_ref(remote, ref->name, &ref->new_oid,
572 ref->deletion, verbose);
573 else
574 for (; report; report = report->next) {
575 refname = report->ref_name ? (char *)report->ref_name : ref->name;
576 new_oid = report->new_oid ? report->new_oid : &ref->new_oid;
577 update_one_tracking_ref(remote, refname, new_oid,
578 is_null_oid(new_oid), verbose);
582 static void print_ref_status(char flag, const char *summary,
583 struct ref *to, struct ref *from, const char *msg,
584 struct ref_push_report *report,
585 int porcelain, int summary_width)
587 const char *to_name;
589 if (report && report->ref_name)
590 to_name = report->ref_name;
591 else
592 to_name = to->name;
594 if (porcelain) {
595 if (from)
596 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to_name);
597 else
598 fprintf(stdout, "%c\t:%s\t", flag, to_name);
599 if (msg)
600 fprintf(stdout, "%s (%s)\n", summary, msg);
601 else
602 fprintf(stdout, "%s\n", summary);
603 } else {
604 const char *red = "", *reset = "";
605 if (push_had_errors(to)) {
606 red = transport_get_color(TRANSPORT_COLOR_REJECTED);
607 reset = transport_get_color(TRANSPORT_COLOR_RESET);
609 fprintf(stderr, " %s%c %-*s%s ", red, flag, summary_width,
610 summary, reset);
611 if (from)
612 fprintf(stderr, "%s -> %s",
613 prettify_refname(from->name),
614 prettify_refname(to_name));
615 else
616 fputs(prettify_refname(to_name), stderr);
617 if (msg) {
618 fputs(" (", stderr);
619 fputs(msg, stderr);
620 fputc(')', stderr);
622 fputc('\n', stderr);
626 static void print_ok_ref_status(struct ref *ref,
627 struct ref_push_report *report,
628 int porcelain, int summary_width)
630 struct object_id *old_oid;
631 struct object_id *new_oid;
632 const char *ref_name;
633 int forced_update;
635 if (report && report->old_oid)
636 old_oid = report->old_oid;
637 else
638 old_oid = &ref->old_oid;
639 if (report && report->new_oid)
640 new_oid = report->new_oid;
641 else
642 new_oid = &ref->new_oid;
643 if (report && report->forced_update)
644 forced_update = report->forced_update;
645 else
646 forced_update = ref->forced_update;
647 if (report && report->ref_name)
648 ref_name = report->ref_name;
649 else
650 ref_name = ref->name;
652 if (ref->deletion)
653 print_ref_status('-', "[deleted]", ref, NULL, NULL,
654 report, porcelain, summary_width);
655 else if (is_null_oid(old_oid))
656 print_ref_status('*',
657 (starts_with(ref_name, "refs/tags/")
658 ? "[new tag]"
659 : (starts_with(ref_name, "refs/heads/")
660 ? "[new branch]"
661 : "[new reference]")),
662 ref, ref->peer_ref, NULL,
663 report, porcelain, summary_width);
664 else {
665 struct strbuf quickref = STRBUF_INIT;
666 char type;
667 const char *msg;
669 strbuf_add_unique_abbrev(&quickref, old_oid,
670 DEFAULT_ABBREV);
671 if (forced_update) {
672 strbuf_addstr(&quickref, "...");
673 type = '+';
674 msg = "forced update";
675 } else {
676 strbuf_addstr(&quickref, "..");
677 type = ' ';
678 msg = NULL;
680 strbuf_add_unique_abbrev(&quickref, new_oid,
681 DEFAULT_ABBREV);
683 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
684 report, porcelain, summary_width);
685 strbuf_release(&quickref);
689 static int print_one_push_report(struct ref *ref, const char *dest, int count,
690 struct ref_push_report *report,
691 int porcelain, int summary_width)
693 if (!count) {
694 char *url = transport_anonymize_url(dest);
695 fprintf(porcelain ? stdout : stderr, "To %s\n", url);
696 free(url);
699 switch(ref->status) {
700 case REF_STATUS_NONE:
701 print_ref_status('X', "[no match]", ref, NULL, NULL,
702 report, porcelain, summary_width);
703 break;
704 case REF_STATUS_REJECT_NODELETE:
705 print_ref_status('!', "[rejected]", ref, NULL,
706 "remote does not support deleting refs",
707 report, porcelain, summary_width);
708 break;
709 case REF_STATUS_UPTODATE:
710 print_ref_status('=', "[up to date]", ref,
711 ref->peer_ref, NULL,
712 report, porcelain, summary_width);
713 break;
714 case REF_STATUS_REJECT_NONFASTFORWARD:
715 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
716 "non-fast-forward",
717 report, porcelain, summary_width);
718 break;
719 case REF_STATUS_REJECT_ALREADY_EXISTS:
720 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
721 "already exists",
722 report, porcelain, summary_width);
723 break;
724 case REF_STATUS_REJECT_FETCH_FIRST:
725 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
726 "fetch first",
727 report, porcelain, summary_width);
728 break;
729 case REF_STATUS_REJECT_NEEDS_FORCE:
730 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
731 "needs force",
732 report, porcelain, summary_width);
733 break;
734 case REF_STATUS_REJECT_STALE:
735 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
736 "stale info",
737 report, porcelain, summary_width);
738 break;
739 case REF_STATUS_REJECT_REMOTE_UPDATED:
740 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
741 "remote ref updated since checkout",
742 report, porcelain, summary_width);
743 break;
744 case REF_STATUS_REJECT_SHALLOW:
745 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
746 "new shallow roots not allowed",
747 report, porcelain, summary_width);
748 break;
749 case REF_STATUS_REMOTE_REJECT:
750 print_ref_status('!', "[remote rejected]", ref,
751 ref->deletion ? NULL : ref->peer_ref,
752 ref->remote_status,
753 report, porcelain, summary_width);
754 break;
755 case REF_STATUS_EXPECTING_REPORT:
756 print_ref_status('!', "[remote failure]", ref,
757 ref->deletion ? NULL : ref->peer_ref,
758 "remote failed to report status",
759 report, porcelain, summary_width);
760 break;
761 case REF_STATUS_ATOMIC_PUSH_FAILED:
762 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
763 "atomic push failed",
764 report, porcelain, summary_width);
765 break;
766 case REF_STATUS_OK:
767 print_ok_ref_status(ref, report, porcelain, summary_width);
768 break;
771 return 1;
774 static int print_one_push_status(struct ref *ref, const char *dest, int count,
775 int porcelain, int summary_width)
777 struct ref_push_report *report;
778 int n = 0;
780 if (!ref->report)
781 return print_one_push_report(ref, dest, count,
782 NULL, porcelain, summary_width);
784 for (report = ref->report; report; report = report->next)
785 print_one_push_report(ref, dest, count + n++,
786 report, porcelain, summary_width);
787 return n;
790 static int measure_abbrev(const struct object_id *oid, int sofar)
792 char hex[GIT_MAX_HEXSZ + 1];
793 int w = repo_find_unique_abbrev_r(the_repository, hex, oid,
794 DEFAULT_ABBREV);
796 return (w < sofar) ? sofar : w;
799 int transport_summary_width(const struct ref *refs)
801 int maxw = -1;
803 for (; refs; refs = refs->next) {
804 maxw = measure_abbrev(&refs->old_oid, maxw);
805 maxw = measure_abbrev(&refs->new_oid, maxw);
807 if (maxw < 0)
808 maxw = FALLBACK_DEFAULT_ABBREV;
809 return (2 * maxw + 3);
812 void transport_print_push_status(const char *dest, struct ref *refs,
813 int verbose, int porcelain, unsigned int *reject_reasons)
815 struct ref *ref;
816 int n = 0;
817 char *head;
818 int summary_width = transport_summary_width(refs);
820 if (transport_color_config() < 0)
821 warning(_("could not parse transport.color.* config"));
823 head = refs_resolve_refdup(get_main_ref_store(the_repository), "HEAD",
824 RESOLVE_REF_READING, NULL, NULL);
826 if (verbose) {
827 for (ref = refs; ref; ref = ref->next)
828 if (ref->status == REF_STATUS_UPTODATE)
829 n += print_one_push_status(ref, dest, n,
830 porcelain, summary_width);
833 for (ref = refs; ref; ref = ref->next)
834 if (ref->status == REF_STATUS_OK)
835 n += print_one_push_status(ref, dest, n,
836 porcelain, summary_width);
838 *reject_reasons = 0;
839 for (ref = refs; ref; ref = ref->next) {
840 if (ref->status != REF_STATUS_NONE &&
841 ref->status != REF_STATUS_UPTODATE &&
842 ref->status != REF_STATUS_OK)
843 n += print_one_push_status(ref, dest, n,
844 porcelain, summary_width);
845 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
846 if (head != NULL && !strcmp(head, ref->name))
847 *reject_reasons |= REJECT_NON_FF_HEAD;
848 else
849 *reject_reasons |= REJECT_NON_FF_OTHER;
850 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
851 *reject_reasons |= REJECT_ALREADY_EXISTS;
852 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
853 *reject_reasons |= REJECT_FETCH_FIRST;
854 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
855 *reject_reasons |= REJECT_NEEDS_FORCE;
856 } else if (ref->status == REF_STATUS_REJECT_REMOTE_UPDATED) {
857 *reject_reasons |= REJECT_REF_NEEDS_UPDATE;
860 free(head);
863 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
865 struct git_transport_data *data = transport->data;
866 struct send_pack_args args;
867 int ret = 0;
869 if (transport_color_config() < 0)
870 return -1;
872 if (!data->finished_handshake)
873 get_refs_via_connect(transport, 1, NULL);
875 memset(&args, 0, sizeof(args));
876 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
877 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
878 args.use_thin_pack = data->options.thin;
879 args.verbose = (transport->verbose > 0);
880 args.quiet = (transport->verbose < 0);
881 args.progress = transport->progress;
882 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
883 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
884 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
885 args.push_options = transport->push_options;
886 args.url = transport->url;
888 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
889 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
890 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
891 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
892 else
893 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
895 switch (data->version) {
896 case protocol_v2:
897 die(_("support for protocol v2 not implemented yet"));
898 break;
899 case protocol_v1:
900 case protocol_v0:
901 ret = send_pack(&args, data->fd, data->conn, remote_refs,
902 &data->extra_have);
903 break;
904 case protocol_unknown_version:
905 BUG("unknown protocol version");
908 close(data->fd[1]);
909 close(data->fd[0]);
911 * Atomic push may abort the connection early and close the pipe,
912 * which may cause an error for `finish_connect()`. Ignore this error
913 * for atomic git-push.
915 if (ret || args.atomic)
916 finish_connect(data->conn);
917 else
918 ret = finish_connect(data->conn);
919 data->conn = NULL;
920 data->finished_handshake = 0;
922 return ret;
925 static int connect_git(struct transport *transport, const char *name,
926 const char *executable, int fd[2])
928 struct git_transport_data *data = transport->data;
929 data->conn = git_connect(data->fd, transport->url,
930 name, executable, 0);
931 fd[0] = data->fd[0];
932 fd[1] = data->fd[1];
933 return 0;
936 static int disconnect_git(struct transport *transport)
938 struct git_transport_data *data = transport->data;
939 if (data->conn) {
940 if (data->finished_handshake && !transport->stateless_rpc)
941 packet_flush(data->fd[1]);
942 close(data->fd[0]);
943 if (data->fd[1] >= 0)
944 close(data->fd[1]);
945 finish_connect(data->conn);
948 list_objects_filter_release(&data->options.filter_options);
949 free(data);
950 return 0;
953 static struct transport_vtable taken_over_vtable = {
954 .get_refs_list = get_refs_via_connect,
955 .get_bundle_uri = get_bundle_uri,
956 .fetch_refs = fetch_refs_via_pack,
957 .push_refs = git_transport_push,
958 .disconnect = disconnect_git
961 void transport_take_over(struct transport *transport,
962 struct child_process *child)
964 struct git_transport_data *data;
966 if (!transport->smart_options)
967 BUG("taking over transport requires non-NULL "
968 "smart_options field.");
970 CALLOC_ARRAY(data, 1);
971 data->options = *transport->smart_options;
972 data->conn = child;
973 data->fd[0] = data->conn->out;
974 data->fd[1] = data->conn->in;
975 data->finished_handshake = 0;
976 transport->data = data;
978 transport->vtable = &taken_over_vtable;
979 transport->smart_options = &(data->options);
981 transport->cannot_reuse = 1;
984 static int is_file(const char *url)
986 struct stat buf;
987 if (stat(url, &buf))
988 return 0;
989 return S_ISREG(buf.st_mode);
992 static int external_specification_len(const char *url)
994 return strchr(url, ':') - url;
997 static const struct string_list *protocol_allow_list(void)
999 static int enabled = -1;
1000 static struct string_list allowed = STRING_LIST_INIT_DUP;
1002 if (enabled < 0) {
1003 const char *v = getenv("GIT_ALLOW_PROTOCOL");
1004 if (v) {
1005 string_list_split(&allowed, v, ':', -1);
1006 string_list_sort(&allowed);
1007 enabled = 1;
1008 } else {
1009 enabled = 0;
1013 return enabled ? &allowed : NULL;
1016 enum protocol_allow_config {
1017 PROTOCOL_ALLOW_NEVER = 0,
1018 PROTOCOL_ALLOW_USER_ONLY,
1019 PROTOCOL_ALLOW_ALWAYS
1022 static enum protocol_allow_config parse_protocol_config(const char *key,
1023 const char *value)
1025 if (!strcasecmp(value, "always"))
1026 return PROTOCOL_ALLOW_ALWAYS;
1027 else if (!strcasecmp(value, "never"))
1028 return PROTOCOL_ALLOW_NEVER;
1029 else if (!strcasecmp(value, "user"))
1030 return PROTOCOL_ALLOW_USER_ONLY;
1032 die(_("unknown value for config '%s': %s"), key, value);
1035 static enum protocol_allow_config get_protocol_config(const char *type)
1037 char *key = xstrfmt("protocol.%s.allow", type);
1038 char *value;
1040 /* first check the per-protocol config */
1041 if (!git_config_get_string(key, &value)) {
1042 enum protocol_allow_config ret =
1043 parse_protocol_config(key, value);
1044 free(key);
1045 free(value);
1046 return ret;
1048 free(key);
1050 /* if defined, fallback to user-defined default for unknown protocols */
1051 if (!git_config_get_string("protocol.allow", &value)) {
1052 enum protocol_allow_config ret =
1053 parse_protocol_config("protocol.allow", value);
1054 free(value);
1055 return ret;
1058 /* fallback to built-in defaults */
1059 /* known safe */
1060 if (!strcmp(type, "http") ||
1061 !strcmp(type, "https") ||
1062 !strcmp(type, "git") ||
1063 !strcmp(type, "ssh"))
1064 return PROTOCOL_ALLOW_ALWAYS;
1066 /* known scary; err on the side of caution */
1067 if (!strcmp(type, "ext"))
1068 return PROTOCOL_ALLOW_NEVER;
1070 /* unknown; by default let them be used only directly by the user */
1071 return PROTOCOL_ALLOW_USER_ONLY;
1074 int is_transport_allowed(const char *type, int from_user)
1076 const struct string_list *allow_list = protocol_allow_list();
1077 if (allow_list)
1078 return string_list_has_string(allow_list, type);
1080 switch (get_protocol_config(type)) {
1081 case PROTOCOL_ALLOW_ALWAYS:
1082 return 1;
1083 case PROTOCOL_ALLOW_NEVER:
1084 return 0;
1085 case PROTOCOL_ALLOW_USER_ONLY:
1086 if (from_user < 0)
1087 from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1088 return from_user;
1091 BUG("invalid protocol_allow_config type");
1094 void transport_check_allowed(const char *type)
1096 if (!is_transport_allowed(type, -1))
1097 die(_("transport '%s' not allowed"), type);
1100 static struct transport_vtable bundle_vtable = {
1101 .get_refs_list = get_refs_from_bundle,
1102 .fetch_refs = fetch_refs_from_bundle,
1103 .disconnect = close_bundle
1106 static struct transport_vtable builtin_smart_vtable = {
1107 .get_refs_list = get_refs_via_connect,
1108 .get_bundle_uri = get_bundle_uri,
1109 .fetch_refs = fetch_refs_via_pack,
1110 .push_refs = git_transport_push,
1111 .connect = connect_git,
1112 .disconnect = disconnect_git
1115 struct transport *transport_get(struct remote *remote, const char *url)
1117 const char *helper;
1118 char *helper_to_free = NULL;
1119 const char *p;
1120 struct transport *ret = xcalloc(1, sizeof(*ret));
1122 ret->progress = isatty(2);
1123 string_list_init_dup(&ret->pack_lockfiles);
1125 CALLOC_ARRAY(ret->bundles, 1);
1126 init_bundle_list(ret->bundles);
1128 if (!remote)
1129 BUG("No remote provided to transport_get()");
1131 ret->got_remote_refs = 0;
1132 ret->remote = remote;
1133 helper = remote->foreign_vcs;
1135 if (!url)
1136 url = remote->url.v[0];
1137 ret->url = url;
1139 p = url;
1140 while (is_urlschemechar(p == url, *p))
1141 p++;
1142 if (starts_with(p, "::"))
1143 helper = helper_to_free = xstrndup(url, p - url);
1145 if (helper) {
1146 transport_helper_init(ret, helper);
1147 free(helper_to_free);
1148 } else if (starts_with(url, "rsync:")) {
1149 die(_("git-over-rsync is no longer supported"));
1150 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
1151 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
1152 bundle_header_init(&data->header);
1153 transport_check_allowed("file");
1154 ret->data = data;
1155 ret->vtable = &bundle_vtable;
1156 ret->smart_options = NULL;
1157 } else if (!is_url(url)
1158 || starts_with(url, "file://")
1159 || starts_with(url, "git://")
1160 || starts_with(url, "ssh://")
1161 || starts_with(url, "git+ssh://") /* deprecated - do not use */
1162 || starts_with(url, "ssh+git://") /* deprecated - do not use */
1165 * These are builtin smart transports; "allowed" transports
1166 * will be checked individually in git_connect.
1168 struct git_transport_data *data = xcalloc(1, sizeof(*data));
1169 list_objects_filter_init(&data->options.filter_options);
1170 ret->data = data;
1171 ret->vtable = &builtin_smart_vtable;
1172 ret->smart_options = &(data->options);
1174 data->conn = NULL;
1175 data->finished_handshake = 0;
1176 } else {
1177 /* Unknown protocol in URL. Pass to external handler. */
1178 int len = external_specification_len(url);
1179 char *handler = xmemdupz(url, len);
1180 transport_helper_init(ret, handler);
1181 free(handler);
1184 if (ret->smart_options) {
1185 ret->smart_options->thin = 1;
1186 ret->smart_options->uploadpack = "git-upload-pack";
1187 if (remote->uploadpack)
1188 ret->smart_options->uploadpack = remote->uploadpack;
1189 ret->smart_options->receivepack = "git-receive-pack";
1190 if (remote->receivepack)
1191 ret->smart_options->receivepack = remote->receivepack;
1194 ret->hash_algo = &hash_algos[GIT_HASH_SHA1];
1196 return ret;
1199 const struct git_hash_algo *transport_get_hash_algo(struct transport *transport)
1201 return transport->hash_algo;
1204 int transport_set_option(struct transport *transport,
1205 const char *name, const char *value)
1207 int git_reports = 1, protocol_reports = 1;
1209 if (transport->smart_options)
1210 git_reports = set_git_option(transport->smart_options,
1211 name, value);
1213 if (transport->vtable->set_option)
1214 protocol_reports = transport->vtable->set_option(transport,
1215 name, value);
1217 /* If either report is 0, report 0 (success). */
1218 if (!git_reports || !protocol_reports)
1219 return 0;
1220 /* If either reports -1 (invalid value), report -1. */
1221 if ((git_reports == -1) || (protocol_reports == -1))
1222 return -1;
1223 /* Otherwise if both report unknown, report unknown. */
1224 return 1;
1227 void transport_set_verbosity(struct transport *transport, int verbosity,
1228 int force_progress)
1230 if (verbosity >= 1)
1231 transport->verbose = verbosity <= 3 ? verbosity : 3;
1232 if (verbosity < 0)
1233 transport->verbose = -1;
1236 * Rules used to determine whether to report progress (processing aborts
1237 * when a rule is satisfied):
1239 * . Report progress, if force_progress is 1 (ie. --progress).
1240 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1241 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1242 * . Report progress if isatty(2) is 1.
1244 if (force_progress >= 0)
1245 transport->progress = !!force_progress;
1246 else
1247 transport->progress = verbosity >= 0 && isatty(2);
1250 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
1252 int i;
1254 fprintf(stderr, _("The following submodule paths contain changes that can\n"
1255 "not be found on any remote:\n"));
1256 for (i = 0; i < needs_pushing->nr; i++)
1257 fprintf(stderr, " %s\n", needs_pushing->items[i].string);
1258 fprintf(stderr, _("\nPlease try\n\n"
1259 " git push --recurse-submodules=on-demand\n\n"
1260 "or cd to the path and use\n\n"
1261 " git push\n\n"
1262 "to push them to a remote.\n\n"));
1264 string_list_clear(needs_pushing, 0);
1266 die(_("Aborting."));
1269 static int run_pre_push_hook(struct transport *transport,
1270 struct ref *remote_refs)
1272 int ret = 0, x;
1273 struct ref *r;
1274 struct child_process proc = CHILD_PROCESS_INIT;
1275 struct strbuf buf;
1276 const char *hook_path = find_hook("pre-push");
1278 if (!hook_path)
1279 return 0;
1281 strvec_push(&proc.args, hook_path);
1282 strvec_push(&proc.args, transport->remote->name);
1283 strvec_push(&proc.args, transport->url);
1285 proc.in = -1;
1286 proc.trace2_hook_name = "pre-push";
1288 if (start_command(&proc)) {
1289 finish_command(&proc);
1290 return -1;
1293 sigchain_push(SIGPIPE, SIG_IGN);
1295 strbuf_init(&buf, 256);
1297 for (r = remote_refs; r; r = r->next) {
1298 if (!r->peer_ref) continue;
1299 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
1300 if (r->status == REF_STATUS_REJECT_STALE) continue;
1301 if (r->status == REF_STATUS_REJECT_REMOTE_UPDATED) continue;
1302 if (r->status == REF_STATUS_UPTODATE) continue;
1304 strbuf_reset(&buf);
1305 strbuf_addf( &buf, "%s %s %s %s\n",
1306 r->peer_ref->name, oid_to_hex(&r->new_oid),
1307 r->name, oid_to_hex(&r->old_oid));
1309 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
1310 /* We do not mind if a hook does not read all refs. */
1311 if (errno != EPIPE)
1312 ret = -1;
1313 break;
1317 strbuf_release(&buf);
1319 x = close(proc.in);
1320 if (!ret)
1321 ret = x;
1323 sigchain_pop(SIGPIPE);
1325 x = finish_command(&proc);
1326 if (!ret)
1327 ret = x;
1329 return ret;
1332 int transport_push(struct repository *r,
1333 struct transport *transport,
1334 struct refspec *rs, int flags,
1335 unsigned int *reject_reasons)
1337 struct ref *remote_refs = NULL;
1338 struct ref *local_refs = NULL;
1339 int match_flags = MATCH_REFS_NONE;
1340 int verbose = (transport->verbose > 0);
1341 int quiet = (transport->verbose < 0);
1342 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1343 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1344 int push_ret, err;
1345 int ret = -1;
1346 struct transport_ls_refs_options transport_options =
1347 TRANSPORT_LS_REFS_OPTIONS_INIT;
1349 *reject_reasons = 0;
1351 if (transport_color_config() < 0)
1352 goto done;
1354 if (!transport->vtable->push_refs)
1355 goto done;
1357 local_refs = get_local_heads();
1359 if (check_push_refs(local_refs, rs) < 0)
1360 goto done;
1362 refspec_ref_prefixes(rs, &transport_options.ref_prefixes);
1364 trace2_region_enter("transport_push", "get_refs_list", r);
1365 remote_refs = transport->vtable->get_refs_list(transport, 1,
1366 &transport_options);
1367 trace2_region_leave("transport_push", "get_refs_list", r);
1369 transport_ls_refs_options_release(&transport_options);
1371 if (flags & TRANSPORT_PUSH_ALL)
1372 match_flags |= MATCH_REFS_ALL;
1373 if (flags & TRANSPORT_PUSH_MIRROR)
1374 match_flags |= MATCH_REFS_MIRROR;
1375 if (flags & TRANSPORT_PUSH_PRUNE)
1376 match_flags |= MATCH_REFS_PRUNE;
1377 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1378 match_flags |= MATCH_REFS_FOLLOW_TAGS;
1380 if (match_push_refs(local_refs, &remote_refs, rs, match_flags))
1381 goto done;
1383 if (transport->smart_options &&
1384 transport->smart_options->cas &&
1385 !is_empty_cas(transport->smart_options->cas))
1386 apply_push_cas(transport->smart_options->cas,
1387 transport->remote, remote_refs);
1389 set_ref_status_for_push(remote_refs,
1390 flags & TRANSPORT_PUSH_MIRROR,
1391 flags & TRANSPORT_PUSH_FORCE);
1393 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1394 if (run_pre_push_hook(transport, remote_refs))
1395 goto done;
1397 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1398 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1399 !is_bare_repository()) {
1400 struct ref *ref = remote_refs;
1401 struct oid_array commits = OID_ARRAY_INIT;
1403 trace2_region_enter("transport_push", "push_submodules", r);
1404 for (; ref; ref = ref->next)
1405 if (!is_null_oid(&ref->new_oid))
1406 oid_array_append(&commits,
1407 &ref->new_oid);
1409 if (!push_unpushed_submodules(r,
1410 &commits,
1411 transport->remote,
1413 transport->push_options,
1414 pretend)) {
1415 oid_array_clear(&commits);
1416 trace2_region_leave("transport_push", "push_submodules", r);
1417 die(_("failed to push all needed submodules"));
1419 oid_array_clear(&commits);
1420 trace2_region_leave("transport_push", "push_submodules", r);
1423 if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1424 ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1425 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1426 !pretend)) && !is_bare_repository()) {
1427 struct ref *ref = remote_refs;
1428 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1429 struct oid_array commits = OID_ARRAY_INIT;
1431 trace2_region_enter("transport_push", "check_submodules", r);
1432 for (; ref; ref = ref->next)
1433 if (!is_null_oid(&ref->new_oid))
1434 oid_array_append(&commits,
1435 &ref->new_oid);
1437 if (find_unpushed_submodules(r,
1438 &commits,
1439 transport->remote->name,
1440 &needs_pushing)) {
1441 oid_array_clear(&commits);
1442 trace2_region_leave("transport_push", "check_submodules", r);
1443 die_with_unpushed_submodules(&needs_pushing);
1445 string_list_clear(&needs_pushing, 0);
1446 oid_array_clear(&commits);
1447 trace2_region_leave("transport_push", "check_submodules", r);
1450 if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY)) {
1451 trace2_region_enter("transport_push", "push_refs", r);
1452 push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1453 trace2_region_leave("transport_push", "push_refs", r);
1454 } else
1455 push_ret = 0;
1456 err = push_had_errors(remote_refs);
1457 ret = push_ret | err;
1459 if (!quiet || err)
1460 transport_print_push_status(transport->url, remote_refs,
1461 verbose | porcelain, porcelain,
1462 reject_reasons);
1464 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1465 set_upstreams(transport, remote_refs, pretend);
1467 if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1468 TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1469 struct ref *ref;
1470 for (ref = remote_refs; ref; ref = ref->next)
1471 transport_update_tracking_ref(transport->remote, ref, verbose);
1474 if (porcelain && !push_ret)
1475 puts("Done");
1476 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1477 /* stable plumbing output; do not modify or localize */
1478 fprintf(stderr, "Everything up-to-date\n");
1480 done:
1481 free_refs(local_refs);
1482 free_refs(remote_refs);
1483 return ret;
1486 const struct ref *transport_get_remote_refs(struct transport *transport,
1487 struct transport_ls_refs_options *transport_options)
1489 if (!transport->got_remote_refs) {
1490 transport->remote_refs =
1491 transport->vtable->get_refs_list(transport, 0,
1492 transport_options);
1493 transport->got_remote_refs = 1;
1496 return transport->remote_refs;
1499 void transport_ls_refs_options_release(struct transport_ls_refs_options *opts)
1501 strvec_clear(&opts->ref_prefixes);
1502 free((char *)opts->unborn_head_target);
1505 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1507 int rc;
1508 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1509 struct ref **heads = NULL;
1510 struct ref *rm;
1512 for (rm = refs; rm; rm = rm->next) {
1513 nr_refs++;
1514 if (rm->peer_ref &&
1515 !is_null_oid(&rm->old_oid) &&
1516 oideq(&rm->peer_ref->old_oid, &rm->old_oid))
1517 continue;
1518 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1519 heads[nr_heads++] = rm;
1522 if (!nr_heads) {
1524 * When deepening of a shallow repository is requested,
1525 * then local and remote refs are likely to still be equal.
1526 * Just feed them all to the fetch method in that case.
1527 * This condition shouldn't be met in a non-deepening fetch
1528 * (see builtin/fetch.c:quickfetch()).
1530 ALLOC_ARRAY(heads, nr_refs);
1531 for (rm = refs; rm; rm = rm->next)
1532 heads[nr_heads++] = rm;
1535 rc = transport->vtable->fetch_refs(transport, nr_heads, heads);
1537 free(heads);
1538 return rc;
1541 int transport_get_remote_bundle_uri(struct transport *transport)
1543 int value = 0;
1544 const struct transport_vtable *vtable = transport->vtable;
1546 /* Check config only once. */
1547 if (transport->got_remote_bundle_uri)
1548 return 0;
1549 transport->got_remote_bundle_uri = 1;
1552 * Don't request bundle-uri from the server unless configured to
1553 * do so by the transfer.bundleURI=true config option.
1555 if (git_config_get_bool("transfer.bundleuri", &value) || !value)
1556 return 0;
1558 if (!transport->bundles->baseURI)
1559 transport->bundles->baseURI = xstrdup(transport->url);
1561 if (!vtable->get_bundle_uri)
1562 return error(_("bundle-uri operation not supported by protocol"));
1564 if (vtable->get_bundle_uri(transport) < 0)
1565 return error(_("could not retrieve server-advertised bundle-uri list"));
1566 return 0;
1569 void transport_unlock_pack(struct transport *transport, unsigned int flags)
1571 int in_signal_handler = !!(flags & TRANSPORT_UNLOCK_PACK_IN_SIGNAL_HANDLER);
1572 int i;
1574 for (i = 0; i < transport->pack_lockfiles.nr; i++)
1575 if (in_signal_handler)
1576 unlink(transport->pack_lockfiles.items[i].string);
1577 else
1578 unlink_or_warn(transport->pack_lockfiles.items[i].string);
1579 if (!in_signal_handler)
1580 string_list_clear(&transport->pack_lockfiles, 0);
1583 int transport_connect(struct transport *transport, const char *name,
1584 const char *exec, int fd[2])
1586 if (transport->vtable->connect)
1587 return transport->vtable->connect(transport, name, exec, fd);
1588 else
1589 die(_("operation not supported by protocol"));
1592 int transport_disconnect(struct transport *transport)
1594 int ret = 0;
1595 if (transport->vtable->disconnect)
1596 ret = transport->vtable->disconnect(transport);
1597 if (transport->got_remote_refs)
1598 free_refs((void *)transport->remote_refs);
1599 clear_bundle_list(transport->bundles);
1600 free(transport->bundles);
1601 free(transport);
1602 return ret;
1606 * Strip username (and password) from a URL and return
1607 * it in a newly allocated string.
1609 char *transport_anonymize_url(const char *url)
1611 char *scheme_prefix, *anon_part;
1612 size_t anon_len, prefix_len = 0;
1614 anon_part = strchr(url, '@');
1615 if (url_is_local_not_ssh(url) || !anon_part)
1616 goto literal_copy;
1618 anon_len = strlen(++anon_part);
1619 scheme_prefix = strstr(url, "://");
1620 if (!scheme_prefix) {
1621 if (!strchr(anon_part, ':'))
1622 /* cannot be "me@there:/path/name" */
1623 goto literal_copy;
1624 } else {
1625 const char *cp;
1626 /* make sure scheme is reasonable */
1627 for (cp = url; cp < scheme_prefix; cp++) {
1628 switch (*cp) {
1629 /* RFC 1738 2.1 */
1630 case '+': case '.': case '-':
1631 break; /* ok */
1632 default:
1633 if (isalnum(*cp))
1634 break;
1635 /* it isn't */
1636 goto literal_copy;
1639 /* @ past the first slash does not count */
1640 cp = strchr(scheme_prefix + 3, '/');
1641 if (cp && cp < anon_part)
1642 goto literal_copy;
1643 prefix_len = scheme_prefix - url + 3;
1645 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1646 (int)anon_len, anon_part);
1647 literal_copy:
1648 return xstrdup(url);