Merge branch 'ob/t3501-retitle'
[alt-git.git] / transport.c
blob67afdae57c1c1d0da0d88b1e9823ed6b4fefe4ca
1 #include "git-compat-util.h"
2 #include "advice.h"
3 #include "alloc.h"
4 #include "config.h"
5 #include "environment.h"
6 #include "hex.h"
7 #include "transport.h"
8 #include "hook.h"
9 #include "pkt-line.h"
10 #include "fetch-pack.h"
11 #include "remote.h"
12 #include "connect.h"
13 #include "send-pack.h"
14 #include "walker.h"
15 #include "bundle.h"
16 #include "dir.h"
17 #include "gettext.h"
18 #include "refs.h"
19 #include "refspec.h"
20 #include "branch.h"
21 #include "url.h"
22 #include "submodule.h"
23 #include "string-list.h"
24 #include "oid-array.h"
25 #include "sigchain.h"
26 #include "trace2.h"
27 #include "transport-internal.h"
28 #include "protocol.h"
29 #include "object-name.h"
30 #include "object-store.h"
31 #include "color.h"
32 #include "bundle-uri.h"
33 #include "wrapper.h"
35 static int transport_use_color = -1;
36 static char transport_colors[][COLOR_MAXLEN] = {
37 GIT_COLOR_RESET,
38 GIT_COLOR_RED /* REJECTED */
41 enum color_transport {
42 TRANSPORT_COLOR_RESET = 0,
43 TRANSPORT_COLOR_REJECTED = 1
46 static int transport_color_config(void)
48 const char *keys[] = {
49 "color.transport.reset",
50 "color.transport.rejected"
51 }, *key = "color.transport";
52 char *value;
53 int i;
54 static int initialized;
56 if (initialized)
57 return 0;
58 initialized = 1;
60 if (!git_config_get_string(key, &value))
61 transport_use_color = git_config_colorbool(key, value);
63 if (!want_color_stderr(transport_use_color))
64 return 0;
66 for (i = 0; i < ARRAY_SIZE(keys); i++)
67 if (!git_config_get_string(keys[i], &value)) {
68 if (!value)
69 return config_error_nonbool(keys[i]);
70 if (color_parse(value, transport_colors[i]) < 0)
71 return -1;
74 return 0;
77 static const char *transport_get_color(enum color_transport ix)
79 if (want_color_stderr(transport_use_color))
80 return transport_colors[ix];
81 return "";
84 static void set_upstreams(struct transport *transport, struct ref *refs,
85 int pretend)
87 struct ref *ref;
88 for (ref = refs; ref; ref = ref->next) {
89 const char *localname;
90 const char *tmp;
91 const char *remotename;
92 int flag = 0;
94 * Check suitability for tracking. Must be successful /
95 * already up-to-date ref create/modify (not delete).
97 if (ref->status != REF_STATUS_OK &&
98 ref->status != REF_STATUS_UPTODATE)
99 continue;
100 if (!ref->peer_ref)
101 continue;
102 if (is_null_oid(&ref->new_oid))
103 continue;
105 /* Follow symbolic refs (mainly for HEAD). */
106 localname = ref->peer_ref->name;
107 remotename = ref->name;
108 tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING,
109 NULL, &flag);
110 if (tmp && flag & REF_ISSYMREF &&
111 starts_with(tmp, "refs/heads/"))
112 localname = tmp;
114 /* Both source and destination must be local branches. */
115 if (!localname || !starts_with(localname, "refs/heads/"))
116 continue;
117 if (!remotename || !starts_with(remotename, "refs/heads/"))
118 continue;
120 if (!pretend) {
121 int flag = transport->verbose < 0 ? 0 : BRANCH_CONFIG_VERBOSE;
122 install_branch_config(flag, localname + 11,
123 transport->remote->name, remotename);
124 } else if (transport->verbose >= 0)
125 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
126 localname + 11, remotename + 11,
127 transport->remote->name);
131 struct bundle_transport_data {
132 int fd;
133 struct bundle_header header;
134 unsigned get_refs_from_bundle_called : 1;
137 static void get_refs_from_bundle_inner(struct transport *transport)
139 struct bundle_transport_data *data = transport->data;
141 data->get_refs_from_bundle_called = 1;
143 if (data->fd > 0)
144 close(data->fd);
145 data->fd = read_bundle_header(transport->url, &data->header);
146 if (data->fd < 0)
147 die(_("could not read bundle '%s'"), transport->url);
149 transport->hash_algo = data->header.hash_algo;
152 static struct ref *get_refs_from_bundle(struct transport *transport,
153 int for_push,
154 struct transport_ls_refs_options *transport_options UNUSED)
156 struct bundle_transport_data *data = transport->data;
157 struct ref *result = NULL;
158 int i;
160 if (for_push)
161 return NULL;
163 get_refs_from_bundle_inner(transport);
165 for (i = 0; i < data->header.references.nr; i++) {
166 struct string_list_item *e = data->header.references.items + i;
167 const char *name = e->string;
168 struct ref *ref = alloc_ref(name);
169 struct object_id *oid = e->util;
170 oidcpy(&ref->old_oid, oid);
171 ref->next = result;
172 result = ref;
174 return result;
177 static int fetch_refs_from_bundle(struct transport *transport,
178 int nr_heads UNUSED,
179 struct ref **to_fetch UNUSED)
181 struct bundle_transport_data *data = transport->data;
182 struct strvec extra_index_pack_args = STRVEC_INIT;
183 int ret;
185 if (transport->progress)
186 strvec_push(&extra_index_pack_args, "-v");
188 if (!data->get_refs_from_bundle_called)
189 get_refs_from_bundle_inner(transport);
190 ret = unbundle(the_repository, &data->header, data->fd,
191 &extra_index_pack_args, 0);
192 transport->hash_algo = data->header.hash_algo;
193 return ret;
196 static int close_bundle(struct transport *transport)
198 struct bundle_transport_data *data = transport->data;
199 if (data->fd > 0)
200 close(data->fd);
201 bundle_header_release(&data->header);
202 free(data);
203 return 0;
206 struct git_transport_data {
207 struct git_transport_options options;
208 struct child_process *conn;
209 int fd[2];
210 unsigned finished_handshake : 1;
211 enum protocol_version version;
212 struct oid_array extra_have;
213 struct oid_array shallow;
216 static int set_git_option(struct git_transport_options *opts,
217 const char *name, const char *value)
219 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
220 opts->uploadpack = value;
221 return 0;
222 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
223 opts->receivepack = value;
224 return 0;
225 } else if (!strcmp(name, TRANS_OPT_THIN)) {
226 opts->thin = !!value;
227 return 0;
228 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
229 opts->followtags = !!value;
230 return 0;
231 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
232 opts->keep = !!value;
233 return 0;
234 } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
235 opts->update_shallow = !!value;
236 return 0;
237 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
238 if (!value)
239 opts->depth = 0;
240 else {
241 char *end;
242 opts->depth = strtol(value, &end, 0);
243 if (*end)
244 die(_("transport: invalid depth option '%s'"), value);
246 return 0;
247 } else if (!strcmp(name, TRANS_OPT_DEEPEN_SINCE)) {
248 opts->deepen_since = value;
249 return 0;
250 } else if (!strcmp(name, TRANS_OPT_DEEPEN_NOT)) {
251 opts->deepen_not = (const struct string_list *)value;
252 return 0;
253 } else if (!strcmp(name, TRANS_OPT_DEEPEN_RELATIVE)) {
254 opts->deepen_relative = !!value;
255 return 0;
256 } else if (!strcmp(name, TRANS_OPT_FROM_PROMISOR)) {
257 opts->from_promisor = !!value;
258 return 0;
259 } else if (!strcmp(name, TRANS_OPT_LIST_OBJECTS_FILTER)) {
260 list_objects_filter_die_if_populated(&opts->filter_options);
261 parse_list_objects_filter(&opts->filter_options, value);
262 return 0;
263 } else if (!strcmp(name, TRANS_OPT_REFETCH)) {
264 opts->refetch = !!value;
265 return 0;
266 } else if (!strcmp(name, TRANS_OPT_REJECT_SHALLOW)) {
267 opts->reject_shallow = !!value;
268 return 0;
270 return 1;
273 static int connect_setup(struct transport *transport, int for_push)
275 struct git_transport_data *data = transport->data;
276 int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
278 if (data->conn)
279 return 0;
281 switch (transport->family) {
282 case TRANSPORT_FAMILY_ALL: break;
283 case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
284 case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
287 data->conn = git_connect(data->fd, transport->url,
288 for_push ?
289 "git-receive-pack" :
290 "git-upload-pack",
291 for_push ?
292 data->options.receivepack :
293 data->options.uploadpack,
294 flags);
296 return 0;
299 static void die_if_server_options(struct transport *transport)
301 if (!transport->server_options || !transport->server_options->nr)
302 return;
303 advise(_("see protocol.version in 'git help config' for more details"));
304 die(_("server options require protocol version 2 or later"));
308 * Obtains the protocol version from the transport and writes it to
309 * transport->data->version, first connecting if not already connected.
311 * If the protocol version is one that allows skipping the listing of remote
312 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
313 * this function returns NULL. Otherwise, this function returns the list of
314 * remote refs.
316 static struct ref *handshake(struct transport *transport, int for_push,
317 struct transport_ls_refs_options *options,
318 int must_list_refs)
320 struct git_transport_data *data = transport->data;
321 struct ref *refs = NULL;
322 struct packet_reader reader;
323 size_t sid_len;
324 const char *server_sid;
326 connect_setup(transport, for_push);
328 packet_reader_init(&reader, data->fd[0], NULL, 0,
329 PACKET_READ_CHOMP_NEWLINE |
330 PACKET_READ_GENTLE_ON_EOF |
331 PACKET_READ_DIE_ON_ERR_PACKET);
333 data->version = discover_version(&reader);
334 switch (data->version) {
335 case protocol_v2:
336 if (server_feature_v2("session-id", &server_sid))
337 trace2_data_string("transfer", NULL, "server-sid", server_sid);
338 if (must_list_refs)
339 get_remote_refs(data->fd[1], &reader, &refs, for_push,
340 options,
341 transport->server_options,
342 transport->stateless_rpc);
343 break;
344 case protocol_v1:
345 case protocol_v0:
346 die_if_server_options(transport);
347 get_remote_heads(&reader, &refs,
348 for_push ? REF_NORMAL : 0,
349 &data->extra_have,
350 &data->shallow);
351 server_sid = server_feature_value("session-id", &sid_len);
352 if (server_sid) {
353 char *sid = xstrndup(server_sid, sid_len);
354 trace2_data_string("transfer", NULL, "server-sid", sid);
355 free(sid);
357 break;
358 case protocol_unknown_version:
359 BUG("unknown protocol version");
361 data->finished_handshake = 1;
362 transport->hash_algo = reader.hash_algo;
364 if (reader.line_peeked)
365 BUG("buffer must be empty at the end of handshake()");
367 return refs;
370 static struct ref *get_refs_via_connect(struct transport *transport, int for_push,
371 struct transport_ls_refs_options *options)
373 return handshake(transport, for_push, options, 1);
376 static int get_bundle_uri(struct transport *transport)
378 struct git_transport_data *data = transport->data;
379 struct packet_reader reader;
380 int stateless_rpc = transport->stateless_rpc;
382 if (!transport->bundles) {
383 CALLOC_ARRAY(transport->bundles, 1);
384 init_bundle_list(transport->bundles);
387 if (!data->finished_handshake) {
388 struct ref *refs = handshake(transport, 0, NULL, 0);
390 if (refs)
391 free_refs(refs);
395 * "Support" protocol v0 and v2 without bundle-uri support by
396 * silently degrading to a NOOP.
398 if (!server_supports_v2("bundle-uri"))
399 return 0;
401 packet_reader_init(&reader, data->fd[0], NULL, 0,
402 PACKET_READ_CHOMP_NEWLINE |
403 PACKET_READ_GENTLE_ON_EOF);
405 return get_remote_bundle_uri(data->fd[1], &reader,
406 transport->bundles, stateless_rpc);
409 static int fetch_refs_via_pack(struct transport *transport,
410 int nr_heads, struct ref **to_fetch)
412 int ret = 0;
413 struct git_transport_data *data = transport->data;
414 struct ref *refs = NULL;
415 struct fetch_pack_args args;
416 struct ref *refs_tmp = NULL;
418 memset(&args, 0, sizeof(args));
419 args.uploadpack = data->options.uploadpack;
420 args.keep_pack = data->options.keep;
421 args.lock_pack = 1;
422 args.use_thin_pack = data->options.thin;
423 args.include_tag = data->options.followtags;
424 args.verbose = (transport->verbose > 1);
425 args.quiet = (transport->verbose < 0);
426 args.no_progress = !transport->progress;
427 args.depth = data->options.depth;
428 args.deepen_since = data->options.deepen_since;
429 args.deepen_not = data->options.deepen_not;
430 args.deepen_relative = data->options.deepen_relative;
431 args.check_self_contained_and_connected =
432 data->options.check_self_contained_and_connected;
433 args.cloning = transport->cloning;
434 args.update_shallow = data->options.update_shallow;
435 args.from_promisor = data->options.from_promisor;
436 list_objects_filter_copy(&args.filter_options,
437 &data->options.filter_options);
438 args.refetch = data->options.refetch;
439 args.stateless_rpc = transport->stateless_rpc;
440 args.server_options = transport->server_options;
441 args.negotiation_tips = data->options.negotiation_tips;
442 args.reject_shallow_remote = transport->smart_options->reject_shallow;
444 if (!data->finished_handshake) {
445 int i;
446 int must_list_refs = 0;
447 for (i = 0; i < nr_heads; i++) {
448 if (!to_fetch[i]->exact_oid) {
449 must_list_refs = 1;
450 break;
453 refs_tmp = handshake(transport, 0, NULL, must_list_refs);
456 if (data->version == protocol_unknown_version)
457 BUG("unknown protocol version");
458 else if (data->version <= protocol_v1)
459 die_if_server_options(transport);
461 if (data->options.acked_commits) {
462 if (data->version < protocol_v2) {
463 warning(_("--negotiate-only requires protocol v2"));
464 ret = -1;
465 } else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
466 warning(_("server does not support wait-for-done"));
467 ret = -1;
468 } else {
469 negotiate_using_fetch(data->options.negotiation_tips,
470 transport->server_options,
471 transport->stateless_rpc,
472 data->fd,
473 data->options.acked_commits);
474 ret = 0;
476 goto cleanup;
479 refs = fetch_pack(&args, data->fd,
480 refs_tmp ? refs_tmp : transport->remote_refs,
481 to_fetch, nr_heads, &data->shallow,
482 &transport->pack_lockfiles, data->version);
484 data->finished_handshake = 0;
485 data->options.self_contained_and_connected =
486 args.self_contained_and_connected;
487 data->options.connectivity_checked = args.connectivity_checked;
489 if (!refs)
490 ret = -1;
491 if (report_unmatched_refs(to_fetch, nr_heads))
492 ret = -1;
494 cleanup:
495 close(data->fd[0]);
496 if (data->fd[1] >= 0)
497 close(data->fd[1]);
498 if (finish_connect(data->conn))
499 ret = -1;
500 data->conn = NULL;
502 free_refs(refs_tmp);
503 free_refs(refs);
504 list_objects_filter_release(&args.filter_options);
505 return ret;
508 static int push_had_errors(struct ref *ref)
510 for (; ref; ref = ref->next) {
511 switch (ref->status) {
512 case REF_STATUS_NONE:
513 case REF_STATUS_UPTODATE:
514 case REF_STATUS_OK:
515 break;
516 default:
517 return 1;
520 return 0;
523 int transport_refs_pushed(struct ref *ref)
525 for (; ref; ref = ref->next) {
526 switch(ref->status) {
527 case REF_STATUS_NONE:
528 case REF_STATUS_UPTODATE:
529 break;
530 default:
531 return 1;
534 return 0;
537 static void update_one_tracking_ref(struct remote *remote, char *refname,
538 struct object_id *new_oid, int deletion,
539 int verbose)
541 struct refspec_item rs;
543 memset(&rs, 0, sizeof(rs));
544 rs.src = refname;
545 rs.dst = NULL;
547 if (!remote_find_tracking(remote, &rs)) {
548 if (verbose)
549 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
550 if (deletion)
551 delete_ref(NULL, rs.dst, NULL, 0);
552 else
553 update_ref("update by push", rs.dst, new_oid,
554 NULL, 0, 0);
555 free(rs.dst);
559 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
561 char *refname;
562 struct object_id *new_oid;
563 struct ref_push_report *report;
565 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
566 return;
568 report = ref->report;
569 if (!report)
570 update_one_tracking_ref(remote, ref->name, &ref->new_oid,
571 ref->deletion, verbose);
572 else
573 for (; report; report = report->next) {
574 refname = report->ref_name ? (char *)report->ref_name : ref->name;
575 new_oid = report->new_oid ? report->new_oid : &ref->new_oid;
576 update_one_tracking_ref(remote, refname, new_oid,
577 is_null_oid(new_oid), verbose);
581 static void print_ref_status(char flag, const char *summary,
582 struct ref *to, struct ref *from, const char *msg,
583 struct ref_push_report *report,
584 int porcelain, int summary_width)
586 const char *to_name;
588 if (report && report->ref_name)
589 to_name = report->ref_name;
590 else
591 to_name = to->name;
593 if (porcelain) {
594 if (from)
595 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to_name);
596 else
597 fprintf(stdout, "%c\t:%s\t", flag, to_name);
598 if (msg)
599 fprintf(stdout, "%s (%s)\n", summary, msg);
600 else
601 fprintf(stdout, "%s\n", summary);
602 } else {
603 const char *red = "", *reset = "";
604 if (push_had_errors(to)) {
605 red = transport_get_color(TRANSPORT_COLOR_REJECTED);
606 reset = transport_get_color(TRANSPORT_COLOR_RESET);
608 fprintf(stderr, " %s%c %-*s%s ", red, flag, summary_width,
609 summary, reset);
610 if (from)
611 fprintf(stderr, "%s -> %s",
612 prettify_refname(from->name),
613 prettify_refname(to_name));
614 else
615 fputs(prettify_refname(to_name), stderr);
616 if (msg) {
617 fputs(" (", stderr);
618 fputs(msg, stderr);
619 fputc(')', stderr);
621 fputc('\n', stderr);
625 static void print_ok_ref_status(struct ref *ref,
626 struct ref_push_report *report,
627 int porcelain, int summary_width)
629 struct object_id *old_oid;
630 struct object_id *new_oid;
631 const char *ref_name;
632 int forced_update;
634 if (report && report->old_oid)
635 old_oid = report->old_oid;
636 else
637 old_oid = &ref->old_oid;
638 if (report && report->new_oid)
639 new_oid = report->new_oid;
640 else
641 new_oid = &ref->new_oid;
642 if (report && report->forced_update)
643 forced_update = report->forced_update;
644 else
645 forced_update = ref->forced_update;
646 if (report && report->ref_name)
647 ref_name = report->ref_name;
648 else
649 ref_name = ref->name;
651 if (ref->deletion)
652 print_ref_status('-', "[deleted]", ref, NULL, NULL,
653 report, porcelain, summary_width);
654 else if (is_null_oid(old_oid))
655 print_ref_status('*',
656 (starts_with(ref_name, "refs/tags/")
657 ? "[new tag]"
658 : (starts_with(ref_name, "refs/heads/")
659 ? "[new branch]"
660 : "[new reference]")),
661 ref, ref->peer_ref, NULL,
662 report, porcelain, summary_width);
663 else {
664 struct strbuf quickref = STRBUF_INIT;
665 char type;
666 const char *msg;
668 strbuf_add_unique_abbrev(&quickref, old_oid,
669 DEFAULT_ABBREV);
670 if (forced_update) {
671 strbuf_addstr(&quickref, "...");
672 type = '+';
673 msg = "forced update";
674 } else {
675 strbuf_addstr(&quickref, "..");
676 type = ' ';
677 msg = NULL;
679 strbuf_add_unique_abbrev(&quickref, new_oid,
680 DEFAULT_ABBREV);
682 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
683 report, porcelain, summary_width);
684 strbuf_release(&quickref);
688 static int print_one_push_report(struct ref *ref, const char *dest, int count,
689 struct ref_push_report *report,
690 int porcelain, int summary_width)
692 if (!count) {
693 char *url = transport_anonymize_url(dest);
694 fprintf(porcelain ? stdout : stderr, "To %s\n", url);
695 free(url);
698 switch(ref->status) {
699 case REF_STATUS_NONE:
700 print_ref_status('X', "[no match]", ref, NULL, NULL,
701 report, porcelain, summary_width);
702 break;
703 case REF_STATUS_REJECT_NODELETE:
704 print_ref_status('!', "[rejected]", ref, NULL,
705 "remote does not support deleting refs",
706 report, porcelain, summary_width);
707 break;
708 case REF_STATUS_UPTODATE:
709 print_ref_status('=', "[up to date]", ref,
710 ref->peer_ref, NULL,
711 report, porcelain, summary_width);
712 break;
713 case REF_STATUS_REJECT_NONFASTFORWARD:
714 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
715 "non-fast-forward",
716 report, porcelain, summary_width);
717 break;
718 case REF_STATUS_REJECT_ALREADY_EXISTS:
719 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
720 "already exists",
721 report, porcelain, summary_width);
722 break;
723 case REF_STATUS_REJECT_FETCH_FIRST:
724 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
725 "fetch first",
726 report, porcelain, summary_width);
727 break;
728 case REF_STATUS_REJECT_NEEDS_FORCE:
729 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
730 "needs force",
731 report, porcelain, summary_width);
732 break;
733 case REF_STATUS_REJECT_STALE:
734 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
735 "stale info",
736 report, porcelain, summary_width);
737 break;
738 case REF_STATUS_REJECT_REMOTE_UPDATED:
739 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
740 "remote ref updated since checkout",
741 report, porcelain, summary_width);
742 break;
743 case REF_STATUS_REJECT_SHALLOW:
744 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
745 "new shallow roots not allowed",
746 report, porcelain, summary_width);
747 break;
748 case REF_STATUS_REMOTE_REJECT:
749 print_ref_status('!', "[remote rejected]", ref,
750 ref->deletion ? NULL : ref->peer_ref,
751 ref->remote_status,
752 report, porcelain, summary_width);
753 break;
754 case REF_STATUS_EXPECTING_REPORT:
755 print_ref_status('!', "[remote failure]", ref,
756 ref->deletion ? NULL : ref->peer_ref,
757 "remote failed to report status",
758 report, porcelain, summary_width);
759 break;
760 case REF_STATUS_ATOMIC_PUSH_FAILED:
761 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
762 "atomic push failed",
763 report, porcelain, summary_width);
764 break;
765 case REF_STATUS_OK:
766 print_ok_ref_status(ref, report, porcelain, summary_width);
767 break;
770 return 1;
773 static int print_one_push_status(struct ref *ref, const char *dest, int count,
774 int porcelain, int summary_width)
776 struct ref_push_report *report;
777 int n = 0;
779 if (!ref->report)
780 return print_one_push_report(ref, dest, count,
781 NULL, porcelain, summary_width);
783 for (report = ref->report; report; report = report->next)
784 print_one_push_report(ref, dest, count + n++,
785 report, porcelain, summary_width);
786 return n;
789 static int measure_abbrev(const struct object_id *oid, int sofar)
791 char hex[GIT_MAX_HEXSZ + 1];
792 int w = repo_find_unique_abbrev_r(the_repository, hex, oid,
793 DEFAULT_ABBREV);
795 return (w < sofar) ? sofar : w;
798 int transport_summary_width(const struct ref *refs)
800 int maxw = -1;
802 for (; refs; refs = refs->next) {
803 maxw = measure_abbrev(&refs->old_oid, maxw);
804 maxw = measure_abbrev(&refs->new_oid, maxw);
806 if (maxw < 0)
807 maxw = FALLBACK_DEFAULT_ABBREV;
808 return (2 * maxw + 3);
811 void transport_print_push_status(const char *dest, struct ref *refs,
812 int verbose, int porcelain, unsigned int *reject_reasons)
814 struct ref *ref;
815 int n = 0;
816 char *head;
817 int summary_width = transport_summary_width(refs);
819 if (transport_color_config() < 0)
820 warning(_("could not parse transport.color.* config"));
822 head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
824 if (verbose) {
825 for (ref = refs; ref; ref = ref->next)
826 if (ref->status == REF_STATUS_UPTODATE)
827 n += print_one_push_status(ref, dest, n,
828 porcelain, summary_width);
831 for (ref = refs; ref; ref = ref->next)
832 if (ref->status == REF_STATUS_OK)
833 n += print_one_push_status(ref, dest, n,
834 porcelain, summary_width);
836 *reject_reasons = 0;
837 for (ref = refs; ref; ref = ref->next) {
838 if (ref->status != REF_STATUS_NONE &&
839 ref->status != REF_STATUS_UPTODATE &&
840 ref->status != REF_STATUS_OK)
841 n += print_one_push_status(ref, dest, n,
842 porcelain, summary_width);
843 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
844 if (head != NULL && !strcmp(head, ref->name))
845 *reject_reasons |= REJECT_NON_FF_HEAD;
846 else
847 *reject_reasons |= REJECT_NON_FF_OTHER;
848 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
849 *reject_reasons |= REJECT_ALREADY_EXISTS;
850 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
851 *reject_reasons |= REJECT_FETCH_FIRST;
852 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
853 *reject_reasons |= REJECT_NEEDS_FORCE;
854 } else if (ref->status == REF_STATUS_REJECT_REMOTE_UPDATED) {
855 *reject_reasons |= REJECT_REF_NEEDS_UPDATE;
858 free(head);
861 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
863 struct git_transport_data *data = transport->data;
864 struct send_pack_args args;
865 int ret = 0;
867 if (transport_color_config() < 0)
868 return -1;
870 if (!data->finished_handshake)
871 get_refs_via_connect(transport, 1, NULL);
873 memset(&args, 0, sizeof(args));
874 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
875 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
876 args.use_thin_pack = data->options.thin;
877 args.verbose = (transport->verbose > 0);
878 args.quiet = (transport->verbose < 0);
879 args.progress = transport->progress;
880 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
881 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
882 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
883 args.push_options = transport->push_options;
884 args.url = transport->url;
886 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
887 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
888 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
889 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
890 else
891 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
893 switch (data->version) {
894 case protocol_v2:
895 die(_("support for protocol v2 not implemented yet"));
896 break;
897 case protocol_v1:
898 case protocol_v0:
899 ret = send_pack(&args, data->fd, data->conn, remote_refs,
900 &data->extra_have);
901 break;
902 case protocol_unknown_version:
903 BUG("unknown protocol version");
906 close(data->fd[1]);
907 close(data->fd[0]);
909 * Atomic push may abort the connection early and close the pipe,
910 * which may cause an error for `finish_connect()`. Ignore this error
911 * for atomic git-push.
913 if (ret || args.atomic)
914 finish_connect(data->conn);
915 else
916 ret = finish_connect(data->conn);
917 data->conn = NULL;
918 data->finished_handshake = 0;
920 return ret;
923 static int connect_git(struct transport *transport, const char *name,
924 const char *executable, int fd[2])
926 struct git_transport_data *data = transport->data;
927 data->conn = git_connect(data->fd, transport->url,
928 name, executable, 0);
929 fd[0] = data->fd[0];
930 fd[1] = data->fd[1];
931 return 0;
934 static int disconnect_git(struct transport *transport)
936 struct git_transport_data *data = transport->data;
937 if (data->conn) {
938 if (data->finished_handshake && !transport->stateless_rpc)
939 packet_flush(data->fd[1]);
940 close(data->fd[0]);
941 if (data->fd[1] >= 0)
942 close(data->fd[1]);
943 finish_connect(data->conn);
946 list_objects_filter_release(&data->options.filter_options);
947 free(data);
948 return 0;
951 static struct transport_vtable taken_over_vtable = {
952 .get_refs_list = get_refs_via_connect,
953 .get_bundle_uri = get_bundle_uri,
954 .fetch_refs = fetch_refs_via_pack,
955 .push_refs = git_transport_push,
956 .disconnect = disconnect_git
959 void transport_take_over(struct transport *transport,
960 struct child_process *child)
962 struct git_transport_data *data;
964 if (!transport->smart_options)
965 BUG("taking over transport requires non-NULL "
966 "smart_options field.");
968 CALLOC_ARRAY(data, 1);
969 data->options = *transport->smart_options;
970 data->conn = child;
971 data->fd[0] = data->conn->out;
972 data->fd[1] = data->conn->in;
973 data->finished_handshake = 0;
974 transport->data = data;
976 transport->vtable = &taken_over_vtable;
977 transport->smart_options = &(data->options);
979 transport->cannot_reuse = 1;
982 static int is_file(const char *url)
984 struct stat buf;
985 if (stat(url, &buf))
986 return 0;
987 return S_ISREG(buf.st_mode);
990 static int external_specification_len(const char *url)
992 return strchr(url, ':') - url;
995 static const struct string_list *protocol_allow_list(void)
997 static int enabled = -1;
998 static struct string_list allowed = STRING_LIST_INIT_DUP;
1000 if (enabled < 0) {
1001 const char *v = getenv("GIT_ALLOW_PROTOCOL");
1002 if (v) {
1003 string_list_split(&allowed, v, ':', -1);
1004 string_list_sort(&allowed);
1005 enabled = 1;
1006 } else {
1007 enabled = 0;
1011 return enabled ? &allowed : NULL;
1014 enum protocol_allow_config {
1015 PROTOCOL_ALLOW_NEVER = 0,
1016 PROTOCOL_ALLOW_USER_ONLY,
1017 PROTOCOL_ALLOW_ALWAYS
1020 static enum protocol_allow_config parse_protocol_config(const char *key,
1021 const char *value)
1023 if (!strcasecmp(value, "always"))
1024 return PROTOCOL_ALLOW_ALWAYS;
1025 else if (!strcasecmp(value, "never"))
1026 return PROTOCOL_ALLOW_NEVER;
1027 else if (!strcasecmp(value, "user"))
1028 return PROTOCOL_ALLOW_USER_ONLY;
1030 die(_("unknown value for config '%s': %s"), key, value);
1033 static enum protocol_allow_config get_protocol_config(const char *type)
1035 char *key = xstrfmt("protocol.%s.allow", type);
1036 char *value;
1038 /* first check the per-protocol config */
1039 if (!git_config_get_string(key, &value)) {
1040 enum protocol_allow_config ret =
1041 parse_protocol_config(key, value);
1042 free(key);
1043 free(value);
1044 return ret;
1046 free(key);
1048 /* if defined, fallback to user-defined default for unknown protocols */
1049 if (!git_config_get_string("protocol.allow", &value)) {
1050 enum protocol_allow_config ret =
1051 parse_protocol_config("protocol.allow", value);
1052 free(value);
1053 return ret;
1056 /* fallback to built-in defaults */
1057 /* known safe */
1058 if (!strcmp(type, "http") ||
1059 !strcmp(type, "https") ||
1060 !strcmp(type, "git") ||
1061 !strcmp(type, "ssh"))
1062 return PROTOCOL_ALLOW_ALWAYS;
1064 /* known scary; err on the side of caution */
1065 if (!strcmp(type, "ext"))
1066 return PROTOCOL_ALLOW_NEVER;
1068 /* unknown; by default let them be used only directly by the user */
1069 return PROTOCOL_ALLOW_USER_ONLY;
1072 int is_transport_allowed(const char *type, int from_user)
1074 const struct string_list *allow_list = protocol_allow_list();
1075 if (allow_list)
1076 return string_list_has_string(allow_list, type);
1078 switch (get_protocol_config(type)) {
1079 case PROTOCOL_ALLOW_ALWAYS:
1080 return 1;
1081 case PROTOCOL_ALLOW_NEVER:
1082 return 0;
1083 case PROTOCOL_ALLOW_USER_ONLY:
1084 if (from_user < 0)
1085 from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1086 return from_user;
1089 BUG("invalid protocol_allow_config type");
1092 void transport_check_allowed(const char *type)
1094 if (!is_transport_allowed(type, -1))
1095 die(_("transport '%s' not allowed"), type);
1098 static struct transport_vtable bundle_vtable = {
1099 .get_refs_list = get_refs_from_bundle,
1100 .fetch_refs = fetch_refs_from_bundle,
1101 .disconnect = close_bundle
1104 static struct transport_vtable builtin_smart_vtable = {
1105 .get_refs_list = get_refs_via_connect,
1106 .get_bundle_uri = get_bundle_uri,
1107 .fetch_refs = fetch_refs_via_pack,
1108 .push_refs = git_transport_push,
1109 .connect = connect_git,
1110 .disconnect = disconnect_git
1113 struct transport *transport_get(struct remote *remote, const char *url)
1115 const char *helper;
1116 struct transport *ret = xcalloc(1, sizeof(*ret));
1118 ret->progress = isatty(2);
1119 string_list_init_dup(&ret->pack_lockfiles);
1121 CALLOC_ARRAY(ret->bundles, 1);
1122 init_bundle_list(ret->bundles);
1124 if (!remote)
1125 BUG("No remote provided to transport_get()");
1127 ret->got_remote_refs = 0;
1128 ret->remote = remote;
1129 helper = remote->foreign_vcs;
1131 if (!url && remote->url)
1132 url = remote->url[0];
1133 ret->url = url;
1135 /* maybe it is a foreign URL? */
1136 if (url) {
1137 const char *p = url;
1139 while (is_urlschemechar(p == url, *p))
1140 p++;
1141 if (starts_with(p, "::"))
1142 helper = xstrndup(url, p - url);
1145 if (helper) {
1146 transport_helper_init(ret, helper);
1147 } else if (starts_with(url, "rsync:")) {
1148 die(_("git-over-rsync is no longer supported"));
1149 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
1150 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
1151 bundle_header_init(&data->header);
1152 transport_check_allowed("file");
1153 ret->data = data;
1154 ret->vtable = &bundle_vtable;
1155 ret->smart_options = NULL;
1156 } else if (!is_url(url)
1157 || starts_with(url, "file://")
1158 || starts_with(url, "git://")
1159 || starts_with(url, "ssh://")
1160 || starts_with(url, "git+ssh://") /* deprecated - do not use */
1161 || starts_with(url, "ssh+git://") /* deprecated - do not use */
1164 * These are builtin smart transports; "allowed" transports
1165 * will be checked individually in git_connect.
1167 struct git_transport_data *data = xcalloc(1, sizeof(*data));
1168 list_objects_filter_init(&data->options.filter_options);
1169 ret->data = data;
1170 ret->vtable = &builtin_smart_vtable;
1171 ret->smart_options = &(data->options);
1173 data->conn = NULL;
1174 data->finished_handshake = 0;
1175 } else {
1176 /* Unknown protocol in URL. Pass to external handler. */
1177 int len = external_specification_len(url);
1178 char *handler = xmemdupz(url, len);
1179 transport_helper_init(ret, 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 fprintf(stderr, "Everything up-to-date\n");
1477 done:
1478 free_refs(local_refs);
1479 free_refs(remote_refs);
1480 return ret;
1483 const struct ref *transport_get_remote_refs(struct transport *transport,
1484 struct transport_ls_refs_options *transport_options)
1486 if (!transport->got_remote_refs) {
1487 transport->remote_refs =
1488 transport->vtable->get_refs_list(transport, 0,
1489 transport_options);
1490 transport->got_remote_refs = 1;
1493 return transport->remote_refs;
1496 void transport_ls_refs_options_release(struct transport_ls_refs_options *opts)
1498 strvec_clear(&opts->ref_prefixes);
1499 free((char *)opts->unborn_head_target);
1502 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1504 int rc;
1505 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1506 struct ref **heads = NULL;
1507 struct ref *rm;
1509 for (rm = refs; rm; rm = rm->next) {
1510 nr_refs++;
1511 if (rm->peer_ref &&
1512 !is_null_oid(&rm->old_oid) &&
1513 oideq(&rm->peer_ref->old_oid, &rm->old_oid))
1514 continue;
1515 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1516 heads[nr_heads++] = rm;
1519 if (!nr_heads) {
1521 * When deepening of a shallow repository is requested,
1522 * then local and remote refs are likely to still be equal.
1523 * Just feed them all to the fetch method in that case.
1524 * This condition shouldn't be met in a non-deepening fetch
1525 * (see builtin/fetch.c:quickfetch()).
1527 ALLOC_ARRAY(heads, nr_refs);
1528 for (rm = refs; rm; rm = rm->next)
1529 heads[nr_heads++] = rm;
1532 rc = transport->vtable->fetch_refs(transport, nr_heads, heads);
1534 free(heads);
1535 return rc;
1538 int transport_get_remote_bundle_uri(struct transport *transport)
1540 int value = 0;
1541 const struct transport_vtable *vtable = transport->vtable;
1543 /* Check config only once. */
1544 if (transport->got_remote_bundle_uri)
1545 return 0;
1546 transport->got_remote_bundle_uri = 1;
1549 * Don't request bundle-uri from the server unless configured to
1550 * do so by the transfer.bundleURI=true config option.
1552 if (git_config_get_bool("transfer.bundleuri", &value) || !value)
1553 return 0;
1555 if (!transport->bundles->baseURI)
1556 transport->bundles->baseURI = xstrdup(transport->url);
1558 if (!vtable->get_bundle_uri)
1559 return error(_("bundle-uri operation not supported by protocol"));
1561 if (vtable->get_bundle_uri(transport) < 0)
1562 return error(_("could not retrieve server-advertised bundle-uri list"));
1563 return 0;
1566 void transport_unlock_pack(struct transport *transport, unsigned int flags)
1568 int in_signal_handler = !!(flags & TRANSPORT_UNLOCK_PACK_IN_SIGNAL_HANDLER);
1569 int i;
1571 for (i = 0; i < transport->pack_lockfiles.nr; i++)
1572 if (in_signal_handler)
1573 unlink(transport->pack_lockfiles.items[i].string);
1574 else
1575 unlink_or_warn(transport->pack_lockfiles.items[i].string);
1576 if (!in_signal_handler)
1577 string_list_clear(&transport->pack_lockfiles, 0);
1580 int transport_connect(struct transport *transport, const char *name,
1581 const char *exec, int fd[2])
1583 if (transport->vtable->connect)
1584 return transport->vtable->connect(transport, name, exec, fd);
1585 else
1586 die(_("operation not supported by protocol"));
1589 int transport_disconnect(struct transport *transport)
1591 int ret = 0;
1592 if (transport->vtable->disconnect)
1593 ret = transport->vtable->disconnect(transport);
1594 if (transport->got_remote_refs)
1595 free_refs((void *)transport->remote_refs);
1596 clear_bundle_list(transport->bundles);
1597 free(transport->bundles);
1598 free(transport);
1599 return ret;
1603 * Strip username (and password) from a URL and return
1604 * it in a newly allocated string.
1606 char *transport_anonymize_url(const char *url)
1608 char *scheme_prefix, *anon_part;
1609 size_t anon_len, prefix_len = 0;
1611 anon_part = strchr(url, '@');
1612 if (url_is_local_not_ssh(url) || !anon_part)
1613 goto literal_copy;
1615 anon_len = strlen(++anon_part);
1616 scheme_prefix = strstr(url, "://");
1617 if (!scheme_prefix) {
1618 if (!strchr(anon_part, ':'))
1619 /* cannot be "me@there:/path/name" */
1620 goto literal_copy;
1621 } else {
1622 const char *cp;
1623 /* make sure scheme is reasonable */
1624 for (cp = url; cp < scheme_prefix; cp++) {
1625 switch (*cp) {
1626 /* RFC 1738 2.1 */
1627 case '+': case '.': case '-':
1628 break; /* ok */
1629 default:
1630 if (isalnum(*cp))
1631 break;
1632 /* it isn't */
1633 goto literal_copy;
1636 /* @ past the first slash does not count */
1637 cp = strchr(scheme_prefix + 3, '/');
1638 if (cp && cp < anon_part)
1639 goto literal_copy;
1640 prefix_len = scheme_prefix - url + 3;
1642 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1643 (int)anon_len, anon_part);
1644 literal_copy:
1645 return xstrdup(url);