compat/win32: fix const-correctness with string constants
[git.git] / transport.c
blob83ddea8fbc03875b6a9683b37f1fbc27d3d2984b
1 #include "git-compat-util.h"
2 #include "advice.h"
3 #include "config.h"
4 #include "environment.h"
5 #include "hex.h"
6 #include "transport.h"
7 #include "hook.h"
8 #include "pkt-line.h"
9 #include "fetch-pack.h"
10 #include "remote.h"
11 #include "connect.h"
12 #include "send-pack.h"
13 #include "bundle.h"
14 #include "gettext.h"
15 #include "refs.h"
16 #include "refspec.h"
17 #include "branch.h"
18 #include "url.h"
19 #include "submodule.h"
20 #include "string-list.h"
21 #include "oid-array.h"
22 #include "sigchain.h"
23 #include "trace2.h"
24 #include "transport-internal.h"
25 #include "protocol.h"
26 #include "object-name.h"
27 #include "color.h"
28 #include "bundle-uri.h"
30 static int transport_use_color = -1;
31 static char transport_colors[][COLOR_MAXLEN] = {
32 GIT_COLOR_RESET,
33 GIT_COLOR_RED /* REJECTED */
36 enum color_transport {
37 TRANSPORT_COLOR_RESET = 0,
38 TRANSPORT_COLOR_REJECTED = 1
41 static int transport_color_config(void)
43 const char *keys[] = {
44 "color.transport.reset",
45 "color.transport.rejected"
46 }, *key = "color.transport";
47 char *value;
48 int i;
49 static int initialized;
51 if (initialized)
52 return 0;
53 initialized = 1;
55 if (!git_config_get_string(key, &value))
56 transport_use_color = git_config_colorbool(key, value);
58 if (!want_color_stderr(transport_use_color))
59 return 0;
61 for (i = 0; i < ARRAY_SIZE(keys); i++)
62 if (!git_config_get_string(keys[i], &value)) {
63 if (!value)
64 return config_error_nonbool(keys[i]);
65 if (color_parse(value, transport_colors[i]) < 0)
66 return -1;
69 return 0;
72 static const char *transport_get_color(enum color_transport ix)
74 if (want_color_stderr(transport_use_color))
75 return transport_colors[ix];
76 return "";
79 static void set_upstreams(struct transport *transport, struct ref *refs,
80 int pretend)
82 struct ref *ref;
83 for (ref = refs; ref; ref = ref->next) {
84 const char *localname;
85 const char *tmp;
86 const char *remotename;
87 int flag = 0;
89 * Check suitability for tracking. Must be successful /
90 * already up-to-date ref create/modify (not delete).
92 if (ref->status != REF_STATUS_OK &&
93 ref->status != REF_STATUS_UPTODATE)
94 continue;
95 if (!ref->peer_ref)
96 continue;
97 if (is_null_oid(&ref->new_oid))
98 continue;
100 /* Follow symbolic refs (mainly for HEAD). */
101 localname = ref->peer_ref->name;
102 remotename = ref->name;
103 tmp = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
104 localname, RESOLVE_REF_READING,
105 NULL, &flag);
106 if (tmp && flag & REF_ISSYMREF &&
107 starts_with(tmp, "refs/heads/"))
108 localname = tmp;
110 /* Both source and destination must be local branches. */
111 if (!localname || !starts_with(localname, "refs/heads/"))
112 continue;
113 if (!remotename || !starts_with(remotename, "refs/heads/"))
114 continue;
116 if (!pretend) {
117 int flag = transport->verbose < 0 ? 0 : BRANCH_CONFIG_VERBOSE;
118 install_branch_config(flag, localname + 11,
119 transport->remote->name, remotename);
120 } else if (transport->verbose >= 0)
121 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
122 localname + 11, remotename + 11,
123 transport->remote->name);
127 struct bundle_transport_data {
128 int fd;
129 struct bundle_header header;
130 unsigned get_refs_from_bundle_called : 1;
133 static void get_refs_from_bundle_inner(struct transport *transport)
135 struct bundle_transport_data *data = transport->data;
137 data->get_refs_from_bundle_called = 1;
139 if (data->fd > 0)
140 close(data->fd);
141 data->fd = read_bundle_header(transport->url, &data->header);
142 if (data->fd < 0)
143 die(_("could not read bundle '%s'"), transport->url);
145 transport->hash_algo = data->header.hash_algo;
148 static struct ref *get_refs_from_bundle(struct transport *transport,
149 int for_push,
150 struct transport_ls_refs_options *transport_options UNUSED)
152 struct bundle_transport_data *data = transport->data;
153 struct ref *result = NULL;
154 int i;
156 if (for_push)
157 return NULL;
159 get_refs_from_bundle_inner(transport);
161 for (i = 0; i < data->header.references.nr; i++) {
162 struct string_list_item *e = data->header.references.items + i;
163 const char *name = e->string;
164 struct ref *ref = alloc_ref(name);
165 struct object_id *oid = e->util;
166 oidcpy(&ref->old_oid, oid);
167 ref->next = result;
168 result = ref;
170 return result;
173 static int fetch_refs_from_bundle(struct transport *transport,
174 int nr_heads UNUSED,
175 struct ref **to_fetch UNUSED)
177 struct bundle_transport_data *data = transport->data;
178 struct strvec extra_index_pack_args = STRVEC_INIT;
179 int ret;
181 if (transport->progress)
182 strvec_push(&extra_index_pack_args, "-v");
184 if (!data->get_refs_from_bundle_called)
185 get_refs_from_bundle_inner(transport);
186 ret = unbundle(the_repository, &data->header, data->fd,
187 &extra_index_pack_args, 0);
188 transport->hash_algo = data->header.hash_algo;
189 return ret;
192 static int close_bundle(struct transport *transport)
194 struct bundle_transport_data *data = transport->data;
195 if (data->fd > 0)
196 close(data->fd);
197 bundle_header_release(&data->header);
198 free(data);
199 return 0;
202 struct git_transport_data {
203 struct git_transport_options options;
204 struct child_process *conn;
205 int fd[2];
206 unsigned finished_handshake : 1;
207 enum protocol_version version;
208 struct oid_array extra_have;
209 struct oid_array shallow;
212 static int set_git_option(struct git_transport_options *opts,
213 const char *name, const char *value)
215 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
216 opts->uploadpack = value;
217 return 0;
218 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
219 opts->receivepack = value;
220 return 0;
221 } else if (!strcmp(name, TRANS_OPT_THIN)) {
222 opts->thin = !!value;
223 return 0;
224 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
225 opts->followtags = !!value;
226 return 0;
227 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
228 opts->keep = !!value;
229 return 0;
230 } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
231 opts->update_shallow = !!value;
232 return 0;
233 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
234 if (!value)
235 opts->depth = 0;
236 else {
237 char *end;
238 opts->depth = strtol(value, &end, 0);
239 if (*end)
240 die(_("transport: invalid depth option '%s'"), value);
242 return 0;
243 } else if (!strcmp(name, TRANS_OPT_DEEPEN_SINCE)) {
244 opts->deepen_since = value;
245 return 0;
246 } else if (!strcmp(name, TRANS_OPT_DEEPEN_NOT)) {
247 opts->deepen_not = (const struct string_list *)value;
248 return 0;
249 } else if (!strcmp(name, TRANS_OPT_DEEPEN_RELATIVE)) {
250 opts->deepen_relative = !!value;
251 return 0;
252 } else if (!strcmp(name, TRANS_OPT_FROM_PROMISOR)) {
253 opts->from_promisor = !!value;
254 return 0;
255 } else if (!strcmp(name, TRANS_OPT_LIST_OBJECTS_FILTER)) {
256 list_objects_filter_die_if_populated(&opts->filter_options);
257 parse_list_objects_filter(&opts->filter_options, value);
258 return 0;
259 } else if (!strcmp(name, TRANS_OPT_REFETCH)) {
260 opts->refetch = !!value;
261 return 0;
262 } else if (!strcmp(name, TRANS_OPT_REJECT_SHALLOW)) {
263 opts->reject_shallow = !!value;
264 return 0;
266 return 1;
269 static int connect_setup(struct transport *transport, int for_push)
271 struct git_transport_data *data = transport->data;
272 int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
274 if (data->conn)
275 return 0;
277 switch (transport->family) {
278 case TRANSPORT_FAMILY_ALL: break;
279 case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
280 case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
283 data->conn = git_connect(data->fd, transport->url,
284 for_push ?
285 "git-receive-pack" :
286 "git-upload-pack",
287 for_push ?
288 data->options.receivepack :
289 data->options.uploadpack,
290 flags);
292 return 0;
295 static void die_if_server_options(struct transport *transport)
297 if (!transport->server_options || !transport->server_options->nr)
298 return;
299 advise(_("see protocol.version in 'git help config' for more details"));
300 die(_("server options require protocol version 2 or later"));
304 * Obtains the protocol version from the transport and writes it to
305 * transport->data->version, first connecting if not already connected.
307 * If the protocol version is one that allows skipping the listing of remote
308 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
309 * this function returns NULL. Otherwise, this function returns the list of
310 * remote refs.
312 static struct ref *handshake(struct transport *transport, int for_push,
313 struct transport_ls_refs_options *options,
314 int must_list_refs)
316 struct git_transport_data *data = transport->data;
317 struct ref *refs = NULL;
318 struct packet_reader reader;
319 size_t sid_len;
320 const char *server_sid;
322 connect_setup(transport, for_push);
324 packet_reader_init(&reader, data->fd[0], NULL, 0,
325 PACKET_READ_CHOMP_NEWLINE |
326 PACKET_READ_GENTLE_ON_EOF |
327 PACKET_READ_DIE_ON_ERR_PACKET);
329 data->version = discover_version(&reader);
330 switch (data->version) {
331 case protocol_v2:
332 if (server_feature_v2("session-id", &server_sid))
333 trace2_data_string("transfer", NULL, "server-sid", server_sid);
334 if (must_list_refs)
335 get_remote_refs(data->fd[1], &reader, &refs, for_push,
336 options,
337 transport->server_options,
338 transport->stateless_rpc);
339 break;
340 case protocol_v1:
341 case protocol_v0:
342 die_if_server_options(transport);
343 get_remote_heads(&reader, &refs,
344 for_push ? REF_NORMAL : 0,
345 &data->extra_have,
346 &data->shallow);
347 server_sid = server_feature_value("session-id", &sid_len);
348 if (server_sid) {
349 char *sid = xstrndup(server_sid, sid_len);
350 trace2_data_string("transfer", NULL, "server-sid", sid);
351 free(sid);
353 break;
354 case protocol_unknown_version:
355 BUG("unknown protocol version");
357 data->finished_handshake = 1;
358 transport->hash_algo = reader.hash_algo;
360 if (reader.line_peeked)
361 BUG("buffer must be empty at the end of handshake()");
363 return refs;
366 static struct ref *get_refs_via_connect(struct transport *transport, int for_push,
367 struct transport_ls_refs_options *options)
369 return handshake(transport, for_push, options, 1);
372 static int get_bundle_uri(struct transport *transport)
374 struct git_transport_data *data = transport->data;
375 struct packet_reader reader;
376 int stateless_rpc = transport->stateless_rpc;
378 if (!transport->bundles) {
379 CALLOC_ARRAY(transport->bundles, 1);
380 init_bundle_list(transport->bundles);
383 if (!data->finished_handshake) {
384 struct ref *refs = handshake(transport, 0, NULL, 0);
386 if (refs)
387 free_refs(refs);
391 * "Support" protocol v0 and v2 without bundle-uri support by
392 * silently degrading to a NOOP.
394 if (!server_supports_v2("bundle-uri"))
395 return 0;
397 packet_reader_init(&reader, data->fd[0], NULL, 0,
398 PACKET_READ_CHOMP_NEWLINE |
399 PACKET_READ_GENTLE_ON_EOF);
401 return get_remote_bundle_uri(data->fd[1], &reader,
402 transport->bundles, stateless_rpc);
405 static int fetch_refs_via_pack(struct transport *transport,
406 int nr_heads, struct ref **to_fetch)
408 int ret = 0;
409 struct git_transport_data *data = transport->data;
410 struct ref *refs = NULL;
411 struct fetch_pack_args args;
412 struct ref *refs_tmp = NULL;
414 memset(&args, 0, sizeof(args));
415 args.uploadpack = data->options.uploadpack;
416 args.keep_pack = data->options.keep;
417 args.lock_pack = 1;
418 args.use_thin_pack = data->options.thin;
419 args.include_tag = data->options.followtags;
420 args.verbose = (transport->verbose > 1);
421 args.quiet = (transport->verbose < 0);
422 args.no_progress = !transport->progress;
423 args.depth = data->options.depth;
424 args.deepen_since = data->options.deepen_since;
425 args.deepen_not = data->options.deepen_not;
426 args.deepen_relative = data->options.deepen_relative;
427 args.check_self_contained_and_connected =
428 data->options.check_self_contained_and_connected;
429 args.cloning = transport->cloning;
430 args.update_shallow = data->options.update_shallow;
431 args.from_promisor = data->options.from_promisor;
432 list_objects_filter_copy(&args.filter_options,
433 &data->options.filter_options);
434 args.refetch = data->options.refetch;
435 args.stateless_rpc = transport->stateless_rpc;
436 args.server_options = transport->server_options;
437 args.negotiation_tips = data->options.negotiation_tips;
438 args.reject_shallow_remote = transport->smart_options->reject_shallow;
440 if (!data->finished_handshake) {
441 int i;
442 int must_list_refs = 0;
443 for (i = 0; i < nr_heads; i++) {
444 if (!to_fetch[i]->exact_oid) {
445 must_list_refs = 1;
446 break;
449 refs_tmp = handshake(transport, 0, NULL, must_list_refs);
452 if (data->version == protocol_unknown_version)
453 BUG("unknown protocol version");
454 else if (data->version <= protocol_v1)
455 die_if_server_options(transport);
457 if (data->options.acked_commits) {
458 if (data->version < protocol_v2) {
459 warning(_("--negotiate-only requires protocol v2"));
460 ret = -1;
461 } else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
462 warning(_("server does not support wait-for-done"));
463 ret = -1;
464 } else {
465 negotiate_using_fetch(data->options.negotiation_tips,
466 transport->server_options,
467 transport->stateless_rpc,
468 data->fd,
469 data->options.acked_commits);
470 ret = 0;
472 goto cleanup;
475 refs = fetch_pack(&args, data->fd,
476 refs_tmp ? refs_tmp : transport->remote_refs,
477 to_fetch, nr_heads, &data->shallow,
478 &transport->pack_lockfiles, data->version);
480 data->finished_handshake = 0;
481 data->options.self_contained_and_connected =
482 args.self_contained_and_connected;
483 data->options.connectivity_checked = args.connectivity_checked;
485 if (!refs)
486 ret = -1;
487 if (report_unmatched_refs(to_fetch, nr_heads))
488 ret = -1;
490 cleanup:
491 close(data->fd[0]);
492 if (data->fd[1] >= 0)
493 close(data->fd[1]);
494 if (finish_connect(data->conn))
495 ret = -1;
496 data->conn = NULL;
498 free_refs(refs_tmp);
499 free_refs(refs);
500 list_objects_filter_release(&args.filter_options);
501 return ret;
504 static int push_had_errors(struct ref *ref)
506 for (; ref; ref = ref->next) {
507 switch (ref->status) {
508 case REF_STATUS_NONE:
509 case REF_STATUS_UPTODATE:
510 case REF_STATUS_OK:
511 break;
512 default:
513 return 1;
516 return 0;
519 int transport_refs_pushed(struct ref *ref)
521 for (; ref; ref = ref->next) {
522 switch(ref->status) {
523 case REF_STATUS_NONE:
524 case REF_STATUS_UPTODATE:
525 break;
526 default:
527 return 1;
530 return 0;
533 static void update_one_tracking_ref(struct remote *remote, char *refname,
534 struct object_id *new_oid, int deletion,
535 int verbose)
537 struct refspec_item rs;
539 memset(&rs, 0, sizeof(rs));
540 rs.src = refname;
541 rs.dst = NULL;
543 if (!remote_find_tracking(remote, &rs)) {
544 if (verbose)
545 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
546 if (deletion)
547 refs_delete_ref(get_main_ref_store(the_repository),
548 NULL, rs.dst, NULL, 0);
549 else
550 refs_update_ref(get_main_ref_store(the_repository),
551 "update by push", rs.dst, new_oid,
552 NULL, 0, 0);
553 free(rs.dst);
557 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
559 char *refname;
560 struct object_id *new_oid;
561 struct ref_push_report *report;
563 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
564 return;
566 report = ref->report;
567 if (!report)
568 update_one_tracking_ref(remote, ref->name, &ref->new_oid,
569 ref->deletion, verbose);
570 else
571 for (; report; report = report->next) {
572 refname = report->ref_name ? (char *)report->ref_name : ref->name;
573 new_oid = report->new_oid ? report->new_oid : &ref->new_oid;
574 update_one_tracking_ref(remote, refname, new_oid,
575 is_null_oid(new_oid), verbose);
579 static void print_ref_status(char flag, const char *summary,
580 struct ref *to, struct ref *from, const char *msg,
581 struct ref_push_report *report,
582 int porcelain, int summary_width)
584 const char *to_name;
586 if (report && report->ref_name)
587 to_name = report->ref_name;
588 else
589 to_name = to->name;
591 if (porcelain) {
592 if (from)
593 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to_name);
594 else
595 fprintf(stdout, "%c\t:%s\t", flag, to_name);
596 if (msg)
597 fprintf(stdout, "%s (%s)\n", summary, msg);
598 else
599 fprintf(stdout, "%s\n", summary);
600 } else {
601 const char *red = "", *reset = "";
602 if (push_had_errors(to)) {
603 red = transport_get_color(TRANSPORT_COLOR_REJECTED);
604 reset = transport_get_color(TRANSPORT_COLOR_RESET);
606 fprintf(stderr, " %s%c %-*s%s ", red, flag, summary_width,
607 summary, reset);
608 if (from)
609 fprintf(stderr, "%s -> %s",
610 prettify_refname(from->name),
611 prettify_refname(to_name));
612 else
613 fputs(prettify_refname(to_name), stderr);
614 if (msg) {
615 fputs(" (", stderr);
616 fputs(msg, stderr);
617 fputc(')', stderr);
619 fputc('\n', stderr);
623 static void print_ok_ref_status(struct ref *ref,
624 struct ref_push_report *report,
625 int porcelain, int summary_width)
627 struct object_id *old_oid;
628 struct object_id *new_oid;
629 const char *ref_name;
630 int forced_update;
632 if (report && report->old_oid)
633 old_oid = report->old_oid;
634 else
635 old_oid = &ref->old_oid;
636 if (report && report->new_oid)
637 new_oid = report->new_oid;
638 else
639 new_oid = &ref->new_oid;
640 if (report && report->forced_update)
641 forced_update = report->forced_update;
642 else
643 forced_update = ref->forced_update;
644 if (report && report->ref_name)
645 ref_name = report->ref_name;
646 else
647 ref_name = ref->name;
649 if (ref->deletion)
650 print_ref_status('-', "[deleted]", ref, NULL, NULL,
651 report, porcelain, summary_width);
652 else if (is_null_oid(old_oid))
653 print_ref_status('*',
654 (starts_with(ref_name, "refs/tags/")
655 ? "[new tag]"
656 : (starts_with(ref_name, "refs/heads/")
657 ? "[new branch]"
658 : "[new reference]")),
659 ref, ref->peer_ref, NULL,
660 report, porcelain, summary_width);
661 else {
662 struct strbuf quickref = STRBUF_INIT;
663 char type;
664 const char *msg;
666 strbuf_add_unique_abbrev(&quickref, old_oid,
667 DEFAULT_ABBREV);
668 if (forced_update) {
669 strbuf_addstr(&quickref, "...");
670 type = '+';
671 msg = "forced update";
672 } else {
673 strbuf_addstr(&quickref, "..");
674 type = ' ';
675 msg = NULL;
677 strbuf_add_unique_abbrev(&quickref, new_oid,
678 DEFAULT_ABBREV);
680 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
681 report, porcelain, summary_width);
682 strbuf_release(&quickref);
686 static int print_one_push_report(struct ref *ref, const char *dest, int count,
687 struct ref_push_report *report,
688 int porcelain, int summary_width)
690 if (!count) {
691 char *url = transport_anonymize_url(dest);
692 fprintf(porcelain ? stdout : stderr, "To %s\n", url);
693 free(url);
696 switch(ref->status) {
697 case REF_STATUS_NONE:
698 print_ref_status('X', "[no match]", ref, NULL, NULL,
699 report, porcelain, summary_width);
700 break;
701 case REF_STATUS_REJECT_NODELETE:
702 print_ref_status('!', "[rejected]", ref, NULL,
703 "remote does not support deleting refs",
704 report, porcelain, summary_width);
705 break;
706 case REF_STATUS_UPTODATE:
707 print_ref_status('=', "[up to date]", ref,
708 ref->peer_ref, NULL,
709 report, porcelain, summary_width);
710 break;
711 case REF_STATUS_REJECT_NONFASTFORWARD:
712 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
713 "non-fast-forward",
714 report, porcelain, summary_width);
715 break;
716 case REF_STATUS_REJECT_ALREADY_EXISTS:
717 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
718 "already exists",
719 report, porcelain, summary_width);
720 break;
721 case REF_STATUS_REJECT_FETCH_FIRST:
722 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
723 "fetch first",
724 report, porcelain, summary_width);
725 break;
726 case REF_STATUS_REJECT_NEEDS_FORCE:
727 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
728 "needs force",
729 report, porcelain, summary_width);
730 break;
731 case REF_STATUS_REJECT_STALE:
732 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
733 "stale info",
734 report, porcelain, summary_width);
735 break;
736 case REF_STATUS_REJECT_REMOTE_UPDATED:
737 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
738 "remote ref updated since checkout",
739 report, porcelain, summary_width);
740 break;
741 case REF_STATUS_REJECT_SHALLOW:
742 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
743 "new shallow roots not allowed",
744 report, porcelain, summary_width);
745 break;
746 case REF_STATUS_REMOTE_REJECT:
747 print_ref_status('!', "[remote rejected]", ref,
748 ref->deletion ? NULL : ref->peer_ref,
749 ref->remote_status,
750 report, porcelain, summary_width);
751 break;
752 case REF_STATUS_EXPECTING_REPORT:
753 print_ref_status('!', "[remote failure]", ref,
754 ref->deletion ? NULL : ref->peer_ref,
755 "remote failed to report status",
756 report, porcelain, summary_width);
757 break;
758 case REF_STATUS_ATOMIC_PUSH_FAILED:
759 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
760 "atomic push failed",
761 report, porcelain, summary_width);
762 break;
763 case REF_STATUS_OK:
764 print_ok_ref_status(ref, report, porcelain, summary_width);
765 break;
768 return 1;
771 static int print_one_push_status(struct ref *ref, const char *dest, int count,
772 int porcelain, int summary_width)
774 struct ref_push_report *report;
775 int n = 0;
777 if (!ref->report)
778 return print_one_push_report(ref, dest, count,
779 NULL, porcelain, summary_width);
781 for (report = ref->report; report; report = report->next)
782 print_one_push_report(ref, dest, count + n++,
783 report, porcelain, summary_width);
784 return n;
787 static int measure_abbrev(const struct object_id *oid, int sofar)
789 char hex[GIT_MAX_HEXSZ + 1];
790 int w = repo_find_unique_abbrev_r(the_repository, hex, oid,
791 DEFAULT_ABBREV);
793 return (w < sofar) ? sofar : w;
796 int transport_summary_width(const struct ref *refs)
798 int maxw = -1;
800 for (; refs; refs = refs->next) {
801 maxw = measure_abbrev(&refs->old_oid, maxw);
802 maxw = measure_abbrev(&refs->new_oid, maxw);
804 if (maxw < 0)
805 maxw = FALLBACK_DEFAULT_ABBREV;
806 return (2 * maxw + 3);
809 void transport_print_push_status(const char *dest, struct ref *refs,
810 int verbose, int porcelain, unsigned int *reject_reasons)
812 struct ref *ref;
813 int n = 0;
814 char *head;
815 int summary_width = transport_summary_width(refs);
817 if (transport_color_config() < 0)
818 warning(_("could not parse transport.color.* config"));
820 head = refs_resolve_refdup(get_main_ref_store(the_repository), "HEAD",
821 RESOLVE_REF_READING, NULL, NULL);
823 if (verbose) {
824 for (ref = refs; ref; ref = ref->next)
825 if (ref->status == REF_STATUS_UPTODATE)
826 n += print_one_push_status(ref, dest, n,
827 porcelain, summary_width);
830 for (ref = refs; ref; ref = ref->next)
831 if (ref->status == REF_STATUS_OK)
832 n += print_one_push_status(ref, dest, n,
833 porcelain, summary_width);
835 *reject_reasons = 0;
836 for (ref = refs; ref; ref = ref->next) {
837 if (ref->status != REF_STATUS_NONE &&
838 ref->status != REF_STATUS_UPTODATE &&
839 ref->status != REF_STATUS_OK)
840 n += print_one_push_status(ref, dest, n,
841 porcelain, summary_width);
842 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
843 if (head != NULL && !strcmp(head, ref->name))
844 *reject_reasons |= REJECT_NON_FF_HEAD;
845 else
846 *reject_reasons |= REJECT_NON_FF_OTHER;
847 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
848 *reject_reasons |= REJECT_ALREADY_EXISTS;
849 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
850 *reject_reasons |= REJECT_FETCH_FIRST;
851 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
852 *reject_reasons |= REJECT_NEEDS_FORCE;
853 } else if (ref->status == REF_STATUS_REJECT_REMOTE_UPDATED) {
854 *reject_reasons |= REJECT_REF_NEEDS_UPDATE;
857 free(head);
860 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
862 struct git_transport_data *data = transport->data;
863 struct send_pack_args args;
864 int ret = 0;
866 if (transport_color_config() < 0)
867 return -1;
869 if (!data->finished_handshake)
870 get_refs_via_connect(transport, 1, NULL);
872 memset(&args, 0, sizeof(args));
873 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
874 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
875 args.use_thin_pack = data->options.thin;
876 args.verbose = (transport->verbose > 0);
877 args.quiet = (transport->verbose < 0);
878 args.progress = transport->progress;
879 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
880 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
881 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
882 args.push_options = transport->push_options;
883 args.url = transport->url;
885 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
886 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
887 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
888 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
889 else
890 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
892 switch (data->version) {
893 case protocol_v2:
894 die(_("support for protocol v2 not implemented yet"));
895 break;
896 case protocol_v1:
897 case protocol_v0:
898 ret = send_pack(&args, data->fd, data->conn, remote_refs,
899 &data->extra_have);
900 break;
901 case protocol_unknown_version:
902 BUG("unknown protocol version");
905 close(data->fd[1]);
906 close(data->fd[0]);
908 * Atomic push may abort the connection early and close the pipe,
909 * which may cause an error for `finish_connect()`. Ignore this error
910 * for atomic git-push.
912 if (ret || args.atomic)
913 finish_connect(data->conn);
914 else
915 ret = finish_connect(data->conn);
916 data->conn = NULL;
917 data->finished_handshake = 0;
919 return ret;
922 static int connect_git(struct transport *transport, const char *name,
923 const char *executable, int fd[2])
925 struct git_transport_data *data = transport->data;
926 data->conn = git_connect(data->fd, transport->url,
927 name, executable, 0);
928 fd[0] = data->fd[0];
929 fd[1] = data->fd[1];
930 return 0;
933 static int disconnect_git(struct transport *transport)
935 struct git_transport_data *data = transport->data;
936 if (data->conn) {
937 if (data->finished_handshake && !transport->stateless_rpc)
938 packet_flush(data->fd[1]);
939 close(data->fd[0]);
940 if (data->fd[1] >= 0)
941 close(data->fd[1]);
942 finish_connect(data->conn);
945 list_objects_filter_release(&data->options.filter_options);
946 free(data);
947 return 0;
950 static struct transport_vtable taken_over_vtable = {
951 .get_refs_list = get_refs_via_connect,
952 .get_bundle_uri = get_bundle_uri,
953 .fetch_refs = fetch_refs_via_pack,
954 .push_refs = git_transport_push,
955 .disconnect = disconnect_git
958 void transport_take_over(struct transport *transport,
959 struct child_process *child)
961 struct git_transport_data *data;
963 if (!transport->smart_options)
964 BUG("taking over transport requires non-NULL "
965 "smart_options field.");
967 CALLOC_ARRAY(data, 1);
968 data->options = *transport->smart_options;
969 data->conn = child;
970 data->fd[0] = data->conn->out;
971 data->fd[1] = data->conn->in;
972 data->finished_handshake = 0;
973 transport->data = data;
975 transport->vtable = &taken_over_vtable;
976 transport->smart_options = &(data->options);
978 transport->cannot_reuse = 1;
981 static int is_file(const char *url)
983 struct stat buf;
984 if (stat(url, &buf))
985 return 0;
986 return S_ISREG(buf.st_mode);
989 static int external_specification_len(const char *url)
991 return strchr(url, ':') - url;
994 static const struct string_list *protocol_allow_list(void)
996 static int enabled = -1;
997 static struct string_list allowed = STRING_LIST_INIT_DUP;
999 if (enabled < 0) {
1000 const char *v = getenv("GIT_ALLOW_PROTOCOL");
1001 if (v) {
1002 string_list_split(&allowed, v, ':', -1);
1003 string_list_sort(&allowed);
1004 enabled = 1;
1005 } else {
1006 enabled = 0;
1010 return enabled ? &allowed : NULL;
1013 enum protocol_allow_config {
1014 PROTOCOL_ALLOW_NEVER = 0,
1015 PROTOCOL_ALLOW_USER_ONLY,
1016 PROTOCOL_ALLOW_ALWAYS
1019 static enum protocol_allow_config parse_protocol_config(const char *key,
1020 const char *value)
1022 if (!strcasecmp(value, "always"))
1023 return PROTOCOL_ALLOW_ALWAYS;
1024 else if (!strcasecmp(value, "never"))
1025 return PROTOCOL_ALLOW_NEVER;
1026 else if (!strcasecmp(value, "user"))
1027 return PROTOCOL_ALLOW_USER_ONLY;
1029 die(_("unknown value for config '%s': %s"), key, value);
1032 static enum protocol_allow_config get_protocol_config(const char *type)
1034 char *key = xstrfmt("protocol.%s.allow", type);
1035 char *value;
1037 /* first check the per-protocol config */
1038 if (!git_config_get_string(key, &value)) {
1039 enum protocol_allow_config ret =
1040 parse_protocol_config(key, value);
1041 free(key);
1042 free(value);
1043 return ret;
1045 free(key);
1047 /* if defined, fallback to user-defined default for unknown protocols */
1048 if (!git_config_get_string("protocol.allow", &value)) {
1049 enum protocol_allow_config ret =
1050 parse_protocol_config("protocol.allow", value);
1051 free(value);
1052 return ret;
1055 /* fallback to built-in defaults */
1056 /* known safe */
1057 if (!strcmp(type, "http") ||
1058 !strcmp(type, "https") ||
1059 !strcmp(type, "git") ||
1060 !strcmp(type, "ssh"))
1061 return PROTOCOL_ALLOW_ALWAYS;
1063 /* known scary; err on the side of caution */
1064 if (!strcmp(type, "ext"))
1065 return PROTOCOL_ALLOW_NEVER;
1067 /* unknown; by default let them be used only directly by the user */
1068 return PROTOCOL_ALLOW_USER_ONLY;
1071 int is_transport_allowed(const char *type, int from_user)
1073 const struct string_list *allow_list = protocol_allow_list();
1074 if (allow_list)
1075 return string_list_has_string(allow_list, type);
1077 switch (get_protocol_config(type)) {
1078 case PROTOCOL_ALLOW_ALWAYS:
1079 return 1;
1080 case PROTOCOL_ALLOW_NEVER:
1081 return 0;
1082 case PROTOCOL_ALLOW_USER_ONLY:
1083 if (from_user < 0)
1084 from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1085 return from_user;
1088 BUG("invalid protocol_allow_config type");
1091 void transport_check_allowed(const char *type)
1093 if (!is_transport_allowed(type, -1))
1094 die(_("transport '%s' not allowed"), type);
1097 static struct transport_vtable bundle_vtable = {
1098 .get_refs_list = get_refs_from_bundle,
1099 .fetch_refs = fetch_refs_from_bundle,
1100 .disconnect = close_bundle
1103 static struct transport_vtable builtin_smart_vtable = {
1104 .get_refs_list = get_refs_via_connect,
1105 .get_bundle_uri = get_bundle_uri,
1106 .fetch_refs = fetch_refs_via_pack,
1107 .push_refs = git_transport_push,
1108 .connect = connect_git,
1109 .disconnect = disconnect_git
1112 struct transport *transport_get(struct remote *remote, const char *url)
1114 const char *helper;
1115 struct transport *ret = xcalloc(1, sizeof(*ret));
1117 ret->progress = isatty(2);
1118 string_list_init_dup(&ret->pack_lockfiles);
1120 CALLOC_ARRAY(ret->bundles, 1);
1121 init_bundle_list(ret->bundles);
1123 if (!remote)
1124 BUG("No remote provided to transport_get()");
1126 ret->got_remote_refs = 0;
1127 ret->remote = remote;
1128 helper = remote->foreign_vcs;
1130 if (!url && remote->url)
1131 url = remote->url[0];
1132 ret->url = url;
1134 /* maybe it is a foreign URL? */
1135 if (url) {
1136 const char *p = url;
1138 while (is_urlschemechar(p == url, *p))
1139 p++;
1140 if (starts_with(p, "::"))
1141 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);