path: expose `do_git_common_path()` as `repo_common_pathv()`
[git/gitster.git] / transport.c
blob12cc5b4d9676a5b3be166f1d33e5b6616073da93
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 const char *p;
1119 struct transport *ret = xcalloc(1, sizeof(*ret));
1121 ret->progress = isatty(2);
1122 string_list_init_dup(&ret->pack_lockfiles);
1124 CALLOC_ARRAY(ret->bundles, 1);
1125 init_bundle_list(ret->bundles);
1127 if (!remote)
1128 BUG("No remote provided to transport_get()");
1130 ret->got_remote_refs = 0;
1131 ret->remote = remote;
1132 helper = remote->foreign_vcs;
1134 if (!url)
1135 url = remote->url.v[0];
1136 ret->url = url;
1138 p = url;
1139 while (is_urlschemechar(p == url, *p))
1140 p++;
1141 if (starts_with(p, "::"))
1142 helper = xstrndup(url, p - url);
1144 if (helper) {
1145 transport_helper_init(ret, helper);
1146 } else if (starts_with(url, "rsync:")) {
1147 die(_("git-over-rsync is no longer supported"));
1148 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
1149 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
1150 bundle_header_init(&data->header);
1151 transport_check_allowed("file");
1152 ret->data = data;
1153 ret->vtable = &bundle_vtable;
1154 ret->smart_options = NULL;
1155 } else if (!is_url(url)
1156 || starts_with(url, "file://")
1157 || starts_with(url, "git://")
1158 || starts_with(url, "ssh://")
1159 || starts_with(url, "git+ssh://") /* deprecated - do not use */
1160 || starts_with(url, "ssh+git://") /* deprecated - do not use */
1163 * These are builtin smart transports; "allowed" transports
1164 * will be checked individually in git_connect.
1166 struct git_transport_data *data = xcalloc(1, sizeof(*data));
1167 list_objects_filter_init(&data->options.filter_options);
1168 ret->data = data;
1169 ret->vtable = &builtin_smart_vtable;
1170 ret->smart_options = &(data->options);
1172 data->conn = NULL;
1173 data->finished_handshake = 0;
1174 } else {
1175 /* Unknown protocol in URL. Pass to external handler. */
1176 int len = external_specification_len(url);
1177 char *handler = xmemdupz(url, len);
1178 transport_helper_init(ret, handler);
1179 free(handler);
1182 if (ret->smart_options) {
1183 ret->smart_options->thin = 1;
1184 ret->smart_options->uploadpack = "git-upload-pack";
1185 if (remote->uploadpack)
1186 ret->smart_options->uploadpack = remote->uploadpack;
1187 ret->smart_options->receivepack = "git-receive-pack";
1188 if (remote->receivepack)
1189 ret->smart_options->receivepack = remote->receivepack;
1192 ret->hash_algo = &hash_algos[GIT_HASH_SHA1];
1194 return ret;
1197 const struct git_hash_algo *transport_get_hash_algo(struct transport *transport)
1199 return transport->hash_algo;
1202 int transport_set_option(struct transport *transport,
1203 const char *name, const char *value)
1205 int git_reports = 1, protocol_reports = 1;
1207 if (transport->smart_options)
1208 git_reports = set_git_option(transport->smart_options,
1209 name, value);
1211 if (transport->vtable->set_option)
1212 protocol_reports = transport->vtable->set_option(transport,
1213 name, value);
1215 /* If either report is 0, report 0 (success). */
1216 if (!git_reports || !protocol_reports)
1217 return 0;
1218 /* If either reports -1 (invalid value), report -1. */
1219 if ((git_reports == -1) || (protocol_reports == -1))
1220 return -1;
1221 /* Otherwise if both report unknown, report unknown. */
1222 return 1;
1225 void transport_set_verbosity(struct transport *transport, int verbosity,
1226 int force_progress)
1228 if (verbosity >= 1)
1229 transport->verbose = verbosity <= 3 ? verbosity : 3;
1230 if (verbosity < 0)
1231 transport->verbose = -1;
1234 * Rules used to determine whether to report progress (processing aborts
1235 * when a rule is satisfied):
1237 * . Report progress, if force_progress is 1 (ie. --progress).
1238 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1239 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1240 * . Report progress if isatty(2) is 1.
1242 if (force_progress >= 0)
1243 transport->progress = !!force_progress;
1244 else
1245 transport->progress = verbosity >= 0 && isatty(2);
1248 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
1250 int i;
1252 fprintf(stderr, _("The following submodule paths contain changes that can\n"
1253 "not be found on any remote:\n"));
1254 for (i = 0; i < needs_pushing->nr; i++)
1255 fprintf(stderr, " %s\n", needs_pushing->items[i].string);
1256 fprintf(stderr, _("\nPlease try\n\n"
1257 " git push --recurse-submodules=on-demand\n\n"
1258 "or cd to the path and use\n\n"
1259 " git push\n\n"
1260 "to push them to a remote.\n\n"));
1262 string_list_clear(needs_pushing, 0);
1264 die(_("Aborting."));
1267 static int run_pre_push_hook(struct transport *transport,
1268 struct ref *remote_refs)
1270 int ret = 0, x;
1271 struct ref *r;
1272 struct child_process proc = CHILD_PROCESS_INIT;
1273 struct strbuf buf;
1274 const char *hook_path = find_hook("pre-push");
1276 if (!hook_path)
1277 return 0;
1279 strvec_push(&proc.args, hook_path);
1280 strvec_push(&proc.args, transport->remote->name);
1281 strvec_push(&proc.args, transport->url);
1283 proc.in = -1;
1284 proc.trace2_hook_name = "pre-push";
1286 if (start_command(&proc)) {
1287 finish_command(&proc);
1288 return -1;
1291 sigchain_push(SIGPIPE, SIG_IGN);
1293 strbuf_init(&buf, 256);
1295 for (r = remote_refs; r; r = r->next) {
1296 if (!r->peer_ref) continue;
1297 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
1298 if (r->status == REF_STATUS_REJECT_STALE) continue;
1299 if (r->status == REF_STATUS_REJECT_REMOTE_UPDATED) continue;
1300 if (r->status == REF_STATUS_UPTODATE) continue;
1302 strbuf_reset(&buf);
1303 strbuf_addf( &buf, "%s %s %s %s\n",
1304 r->peer_ref->name, oid_to_hex(&r->new_oid),
1305 r->name, oid_to_hex(&r->old_oid));
1307 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
1308 /* We do not mind if a hook does not read all refs. */
1309 if (errno != EPIPE)
1310 ret = -1;
1311 break;
1315 strbuf_release(&buf);
1317 x = close(proc.in);
1318 if (!ret)
1319 ret = x;
1321 sigchain_pop(SIGPIPE);
1323 x = finish_command(&proc);
1324 if (!ret)
1325 ret = x;
1327 return ret;
1330 int transport_push(struct repository *r,
1331 struct transport *transport,
1332 struct refspec *rs, int flags,
1333 unsigned int *reject_reasons)
1335 struct ref *remote_refs = NULL;
1336 struct ref *local_refs = NULL;
1337 int match_flags = MATCH_REFS_NONE;
1338 int verbose = (transport->verbose > 0);
1339 int quiet = (transport->verbose < 0);
1340 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1341 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1342 int push_ret, err;
1343 int ret = -1;
1344 struct transport_ls_refs_options transport_options =
1345 TRANSPORT_LS_REFS_OPTIONS_INIT;
1347 *reject_reasons = 0;
1349 if (transport_color_config() < 0)
1350 goto done;
1352 if (!transport->vtable->push_refs)
1353 goto done;
1355 local_refs = get_local_heads();
1357 if (check_push_refs(local_refs, rs) < 0)
1358 goto done;
1360 refspec_ref_prefixes(rs, &transport_options.ref_prefixes);
1362 trace2_region_enter("transport_push", "get_refs_list", r);
1363 remote_refs = transport->vtable->get_refs_list(transport, 1,
1364 &transport_options);
1365 trace2_region_leave("transport_push", "get_refs_list", r);
1367 transport_ls_refs_options_release(&transport_options);
1369 if (flags & TRANSPORT_PUSH_ALL)
1370 match_flags |= MATCH_REFS_ALL;
1371 if (flags & TRANSPORT_PUSH_MIRROR)
1372 match_flags |= MATCH_REFS_MIRROR;
1373 if (flags & TRANSPORT_PUSH_PRUNE)
1374 match_flags |= MATCH_REFS_PRUNE;
1375 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1376 match_flags |= MATCH_REFS_FOLLOW_TAGS;
1378 if (match_push_refs(local_refs, &remote_refs, rs, match_flags))
1379 goto done;
1381 if (transport->smart_options &&
1382 transport->smart_options->cas &&
1383 !is_empty_cas(transport->smart_options->cas))
1384 apply_push_cas(transport->smart_options->cas,
1385 transport->remote, remote_refs);
1387 set_ref_status_for_push(remote_refs,
1388 flags & TRANSPORT_PUSH_MIRROR,
1389 flags & TRANSPORT_PUSH_FORCE);
1391 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1392 if (run_pre_push_hook(transport, remote_refs))
1393 goto done;
1395 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1396 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1397 !is_bare_repository()) {
1398 struct ref *ref = remote_refs;
1399 struct oid_array commits = OID_ARRAY_INIT;
1401 trace2_region_enter("transport_push", "push_submodules", r);
1402 for (; ref; ref = ref->next)
1403 if (!is_null_oid(&ref->new_oid))
1404 oid_array_append(&commits,
1405 &ref->new_oid);
1407 if (!push_unpushed_submodules(r,
1408 &commits,
1409 transport->remote,
1411 transport->push_options,
1412 pretend)) {
1413 oid_array_clear(&commits);
1414 trace2_region_leave("transport_push", "push_submodules", r);
1415 die(_("failed to push all needed submodules"));
1417 oid_array_clear(&commits);
1418 trace2_region_leave("transport_push", "push_submodules", r);
1421 if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1422 ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1423 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1424 !pretend)) && !is_bare_repository()) {
1425 struct ref *ref = remote_refs;
1426 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1427 struct oid_array commits = OID_ARRAY_INIT;
1429 trace2_region_enter("transport_push", "check_submodules", r);
1430 for (; ref; ref = ref->next)
1431 if (!is_null_oid(&ref->new_oid))
1432 oid_array_append(&commits,
1433 &ref->new_oid);
1435 if (find_unpushed_submodules(r,
1436 &commits,
1437 transport->remote->name,
1438 &needs_pushing)) {
1439 oid_array_clear(&commits);
1440 trace2_region_leave("transport_push", "check_submodules", r);
1441 die_with_unpushed_submodules(&needs_pushing);
1443 string_list_clear(&needs_pushing, 0);
1444 oid_array_clear(&commits);
1445 trace2_region_leave("transport_push", "check_submodules", r);
1448 if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY)) {
1449 trace2_region_enter("transport_push", "push_refs", r);
1450 push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1451 trace2_region_leave("transport_push", "push_refs", r);
1452 } else
1453 push_ret = 0;
1454 err = push_had_errors(remote_refs);
1455 ret = push_ret | err;
1457 if (!quiet || err)
1458 transport_print_push_status(transport->url, remote_refs,
1459 verbose | porcelain, porcelain,
1460 reject_reasons);
1462 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1463 set_upstreams(transport, remote_refs, pretend);
1465 if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1466 TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1467 struct ref *ref;
1468 for (ref = remote_refs; ref; ref = ref->next)
1469 transport_update_tracking_ref(transport->remote, ref, verbose);
1472 if (porcelain && !push_ret)
1473 puts("Done");
1474 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1475 /* stable plumbing output; do not modify or localize */
1476 fprintf(stderr, "Everything up-to-date\n");
1478 done:
1479 free_refs(local_refs);
1480 free_refs(remote_refs);
1481 return ret;
1484 const struct ref *transport_get_remote_refs(struct transport *transport,
1485 struct transport_ls_refs_options *transport_options)
1487 if (!transport->got_remote_refs) {
1488 transport->remote_refs =
1489 transport->vtable->get_refs_list(transport, 0,
1490 transport_options);
1491 transport->got_remote_refs = 1;
1494 return transport->remote_refs;
1497 void transport_ls_refs_options_release(struct transport_ls_refs_options *opts)
1499 strvec_clear(&opts->ref_prefixes);
1500 free((char *)opts->unborn_head_target);
1503 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1505 int rc;
1506 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1507 struct ref **heads = NULL;
1508 struct ref *rm;
1510 for (rm = refs; rm; rm = rm->next) {
1511 nr_refs++;
1512 if (rm->peer_ref &&
1513 !is_null_oid(&rm->old_oid) &&
1514 oideq(&rm->peer_ref->old_oid, &rm->old_oid))
1515 continue;
1516 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1517 heads[nr_heads++] = rm;
1520 if (!nr_heads) {
1522 * When deepening of a shallow repository is requested,
1523 * then local and remote refs are likely to still be equal.
1524 * Just feed them all to the fetch method in that case.
1525 * This condition shouldn't be met in a non-deepening fetch
1526 * (see builtin/fetch.c:quickfetch()).
1528 ALLOC_ARRAY(heads, nr_refs);
1529 for (rm = refs; rm; rm = rm->next)
1530 heads[nr_heads++] = rm;
1533 rc = transport->vtable->fetch_refs(transport, nr_heads, heads);
1535 free(heads);
1536 return rc;
1539 int transport_get_remote_bundle_uri(struct transport *transport)
1541 int value = 0;
1542 const struct transport_vtable *vtable = transport->vtable;
1544 /* Check config only once. */
1545 if (transport->got_remote_bundle_uri)
1546 return 0;
1547 transport->got_remote_bundle_uri = 1;
1550 * Don't request bundle-uri from the server unless configured to
1551 * do so by the transfer.bundleURI=true config option.
1553 if (git_config_get_bool("transfer.bundleuri", &value) || !value)
1554 return 0;
1556 if (!transport->bundles->baseURI)
1557 transport->bundles->baseURI = xstrdup(transport->url);
1559 if (!vtable->get_bundle_uri)
1560 return error(_("bundle-uri operation not supported by protocol"));
1562 if (vtable->get_bundle_uri(transport) < 0)
1563 return error(_("could not retrieve server-advertised bundle-uri list"));
1564 return 0;
1567 void transport_unlock_pack(struct transport *transport, unsigned int flags)
1569 int in_signal_handler = !!(flags & TRANSPORT_UNLOCK_PACK_IN_SIGNAL_HANDLER);
1570 int i;
1572 for (i = 0; i < transport->pack_lockfiles.nr; i++)
1573 if (in_signal_handler)
1574 unlink(transport->pack_lockfiles.items[i].string);
1575 else
1576 unlink_or_warn(transport->pack_lockfiles.items[i].string);
1577 if (!in_signal_handler)
1578 string_list_clear(&transport->pack_lockfiles, 0);
1581 int transport_connect(struct transport *transport, const char *name,
1582 const char *exec, int fd[2])
1584 if (transport->vtable->connect)
1585 return transport->vtable->connect(transport, name, exec, fd);
1586 else
1587 die(_("operation not supported by protocol"));
1590 int transport_disconnect(struct transport *transport)
1592 int ret = 0;
1593 if (transport->vtable->disconnect)
1594 ret = transport->vtable->disconnect(transport);
1595 if (transport->got_remote_refs)
1596 free_refs((void *)transport->remote_refs);
1597 clear_bundle_list(transport->bundles);
1598 free(transport->bundles);
1599 free(transport);
1600 return ret;
1604 * Strip username (and password) from a URL and return
1605 * it in a newly allocated string.
1607 char *transport_anonymize_url(const char *url)
1609 char *scheme_prefix, *anon_part;
1610 size_t anon_len, prefix_len = 0;
1612 anon_part = strchr(url, '@');
1613 if (url_is_local_not_ssh(url) || !anon_part)
1614 goto literal_copy;
1616 anon_len = strlen(++anon_part);
1617 scheme_prefix = strstr(url, "://");
1618 if (!scheme_prefix) {
1619 if (!strchr(anon_part, ':'))
1620 /* cannot be "me@there:/path/name" */
1621 goto literal_copy;
1622 } else {
1623 const char *cp;
1624 /* make sure scheme is reasonable */
1625 for (cp = url; cp < scheme_prefix; cp++) {
1626 switch (*cp) {
1627 /* RFC 1738 2.1 */
1628 case '+': case '.': case '-':
1629 break; /* ok */
1630 default:
1631 if (isalnum(*cp))
1632 break;
1633 /* it isn't */
1634 goto literal_copy;
1637 /* @ past the first slash does not count */
1638 cp = strchr(scheme_prefix + 3, '/');
1639 if (cp && cp < anon_part)
1640 goto literal_copy;
1641 prefix_len = scheme_prefix - url + 3;
1643 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1644 (int)anon_len, anon_part);
1645 literal_copy:
1646 return xstrdup(url);