Merge branch 'rs/parse-options-help-text-is-optional'
[alt-git.git] / transport.c
blob219af8fd50ed57c0fc0f954965e481b51e7aae72
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 "walker.h"
14 #include "bundle.h"
15 #include "dir.h"
16 #include "gettext.h"
17 #include "refs.h"
18 #include "refspec.h"
19 #include "branch.h"
20 #include "url.h"
21 #include "submodule.h"
22 #include "string-list.h"
23 #include "oid-array.h"
24 #include "sigchain.h"
25 #include "trace2.h"
26 #include "transport-internal.h"
27 #include "protocol.h"
28 #include "object-name.h"
29 #include "object-store-ll.h"
30 #include "color.h"
31 #include "bundle-uri.h"
33 static int transport_use_color = -1;
34 static char transport_colors[][COLOR_MAXLEN] = {
35 GIT_COLOR_RESET,
36 GIT_COLOR_RED /* REJECTED */
39 enum color_transport {
40 TRANSPORT_COLOR_RESET = 0,
41 TRANSPORT_COLOR_REJECTED = 1
44 static int transport_color_config(void)
46 const char *keys[] = {
47 "color.transport.reset",
48 "color.transport.rejected"
49 }, *key = "color.transport";
50 char *value;
51 int i;
52 static int initialized;
54 if (initialized)
55 return 0;
56 initialized = 1;
58 if (!git_config_get_string(key, &value))
59 transport_use_color = git_config_colorbool(key, value);
61 if (!want_color_stderr(transport_use_color))
62 return 0;
64 for (i = 0; i < ARRAY_SIZE(keys); i++)
65 if (!git_config_get_string(keys[i], &value)) {
66 if (!value)
67 return config_error_nonbool(keys[i]);
68 if (color_parse(value, transport_colors[i]) < 0)
69 return -1;
72 return 0;
75 static const char *transport_get_color(enum color_transport ix)
77 if (want_color_stderr(transport_use_color))
78 return transport_colors[ix];
79 return "";
82 static void set_upstreams(struct transport *transport, struct ref *refs,
83 int pretend)
85 struct ref *ref;
86 for (ref = refs; ref; ref = ref->next) {
87 const char *localname;
88 const char *tmp;
89 const char *remotename;
90 int flag = 0;
92 * Check suitability for tracking. Must be successful /
93 * already up-to-date ref create/modify (not delete).
95 if (ref->status != REF_STATUS_OK &&
96 ref->status != REF_STATUS_UPTODATE)
97 continue;
98 if (!ref->peer_ref)
99 continue;
100 if (is_null_oid(&ref->new_oid))
101 continue;
103 /* Follow symbolic refs (mainly for HEAD). */
104 localname = ref->peer_ref->name;
105 remotename = ref->name;
106 tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING,
107 NULL, &flag);
108 if (tmp && flag & REF_ISSYMREF &&
109 starts_with(tmp, "refs/heads/"))
110 localname = tmp;
112 /* Both source and destination must be local branches. */
113 if (!localname || !starts_with(localname, "refs/heads/"))
114 continue;
115 if (!remotename || !starts_with(remotename, "refs/heads/"))
116 continue;
118 if (!pretend) {
119 int flag = transport->verbose < 0 ? 0 : BRANCH_CONFIG_VERBOSE;
120 install_branch_config(flag, localname + 11,
121 transport->remote->name, remotename);
122 } else if (transport->verbose >= 0)
123 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
124 localname + 11, remotename + 11,
125 transport->remote->name);
129 struct bundle_transport_data {
130 int fd;
131 struct bundle_header header;
132 unsigned get_refs_from_bundle_called : 1;
135 static void get_refs_from_bundle_inner(struct transport *transport)
137 struct bundle_transport_data *data = transport->data;
139 data->get_refs_from_bundle_called = 1;
141 if (data->fd > 0)
142 close(data->fd);
143 data->fd = read_bundle_header(transport->url, &data->header);
144 if (data->fd < 0)
145 die(_("could not read bundle '%s'"), transport->url);
147 transport->hash_algo = data->header.hash_algo;
150 static struct ref *get_refs_from_bundle(struct transport *transport,
151 int for_push,
152 struct transport_ls_refs_options *transport_options UNUSED)
154 struct bundle_transport_data *data = transport->data;
155 struct ref *result = NULL;
156 int i;
158 if (for_push)
159 return NULL;
161 get_refs_from_bundle_inner(transport);
163 for (i = 0; i < data->header.references.nr; i++) {
164 struct string_list_item *e = data->header.references.items + i;
165 const char *name = e->string;
166 struct ref *ref = alloc_ref(name);
167 struct object_id *oid = e->util;
168 oidcpy(&ref->old_oid, oid);
169 ref->next = result;
170 result = ref;
172 return result;
175 static int fetch_refs_from_bundle(struct transport *transport,
176 int nr_heads UNUSED,
177 struct ref **to_fetch UNUSED)
179 struct bundle_transport_data *data = transport->data;
180 struct strvec extra_index_pack_args = STRVEC_INIT;
181 int ret;
183 if (transport->progress)
184 strvec_push(&extra_index_pack_args, "-v");
186 if (!data->get_refs_from_bundle_called)
187 get_refs_from_bundle_inner(transport);
188 ret = unbundle(the_repository, &data->header, data->fd,
189 &extra_index_pack_args, 0);
190 transport->hash_algo = data->header.hash_algo;
191 return ret;
194 static int close_bundle(struct transport *transport)
196 struct bundle_transport_data *data = transport->data;
197 if (data->fd > 0)
198 close(data->fd);
199 bundle_header_release(&data->header);
200 free(data);
201 return 0;
204 struct git_transport_data {
205 struct git_transport_options options;
206 struct child_process *conn;
207 int fd[2];
208 unsigned finished_handshake : 1;
209 enum protocol_version version;
210 struct oid_array extra_have;
211 struct oid_array shallow;
214 static int set_git_option(struct git_transport_options *opts,
215 const char *name, const char *value)
217 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
218 opts->uploadpack = value;
219 return 0;
220 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
221 opts->receivepack = value;
222 return 0;
223 } else if (!strcmp(name, TRANS_OPT_THIN)) {
224 opts->thin = !!value;
225 return 0;
226 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
227 opts->followtags = !!value;
228 return 0;
229 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
230 opts->keep = !!value;
231 return 0;
232 } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
233 opts->update_shallow = !!value;
234 return 0;
235 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
236 if (!value)
237 opts->depth = 0;
238 else {
239 char *end;
240 opts->depth = strtol(value, &end, 0);
241 if (*end)
242 die(_("transport: invalid depth option '%s'"), value);
244 return 0;
245 } else if (!strcmp(name, TRANS_OPT_DEEPEN_SINCE)) {
246 opts->deepen_since = value;
247 return 0;
248 } else if (!strcmp(name, TRANS_OPT_DEEPEN_NOT)) {
249 opts->deepen_not = (const struct string_list *)value;
250 return 0;
251 } else if (!strcmp(name, TRANS_OPT_DEEPEN_RELATIVE)) {
252 opts->deepen_relative = !!value;
253 return 0;
254 } else if (!strcmp(name, TRANS_OPT_FROM_PROMISOR)) {
255 opts->from_promisor = !!value;
256 return 0;
257 } else if (!strcmp(name, TRANS_OPT_LIST_OBJECTS_FILTER)) {
258 list_objects_filter_die_if_populated(&opts->filter_options);
259 parse_list_objects_filter(&opts->filter_options, value);
260 return 0;
261 } else if (!strcmp(name, TRANS_OPT_REFETCH)) {
262 opts->refetch = !!value;
263 return 0;
264 } else if (!strcmp(name, TRANS_OPT_REJECT_SHALLOW)) {
265 opts->reject_shallow = !!value;
266 return 0;
268 return 1;
271 static int connect_setup(struct transport *transport, int for_push)
273 struct git_transport_data *data = transport->data;
274 int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
276 if (data->conn)
277 return 0;
279 switch (transport->family) {
280 case TRANSPORT_FAMILY_ALL: break;
281 case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
282 case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
285 data->conn = git_connect(data->fd, transport->url,
286 for_push ?
287 "git-receive-pack" :
288 "git-upload-pack",
289 for_push ?
290 data->options.receivepack :
291 data->options.uploadpack,
292 flags);
294 return 0;
297 static void die_if_server_options(struct transport *transport)
299 if (!transport->server_options || !transport->server_options->nr)
300 return;
301 advise(_("see protocol.version in 'git help config' for more details"));
302 die(_("server options require protocol version 2 or later"));
306 * Obtains the protocol version from the transport and writes it to
307 * transport->data->version, first connecting if not already connected.
309 * If the protocol version is one that allows skipping the listing of remote
310 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
311 * this function returns NULL. Otherwise, this function returns the list of
312 * remote refs.
314 static struct ref *handshake(struct transport *transport, int for_push,
315 struct transport_ls_refs_options *options,
316 int must_list_refs)
318 struct git_transport_data *data = transport->data;
319 struct ref *refs = NULL;
320 struct packet_reader reader;
321 size_t sid_len;
322 const char *server_sid;
324 connect_setup(transport, for_push);
326 packet_reader_init(&reader, data->fd[0], NULL, 0,
327 PACKET_READ_CHOMP_NEWLINE |
328 PACKET_READ_GENTLE_ON_EOF |
329 PACKET_READ_DIE_ON_ERR_PACKET);
331 data->version = discover_version(&reader);
332 switch (data->version) {
333 case protocol_v2:
334 if (server_feature_v2("session-id", &server_sid))
335 trace2_data_string("transfer", NULL, "server-sid", server_sid);
336 if (must_list_refs)
337 get_remote_refs(data->fd[1], &reader, &refs, for_push,
338 options,
339 transport->server_options,
340 transport->stateless_rpc);
341 break;
342 case protocol_v1:
343 case protocol_v0:
344 die_if_server_options(transport);
345 get_remote_heads(&reader, &refs,
346 for_push ? REF_NORMAL : 0,
347 &data->extra_have,
348 &data->shallow);
349 server_sid = server_feature_value("session-id", &sid_len);
350 if (server_sid) {
351 char *sid = xstrndup(server_sid, sid_len);
352 trace2_data_string("transfer", NULL, "server-sid", sid);
353 free(sid);
355 break;
356 case protocol_unknown_version:
357 BUG("unknown protocol version");
359 data->finished_handshake = 1;
360 transport->hash_algo = reader.hash_algo;
362 if (reader.line_peeked)
363 BUG("buffer must be empty at the end of handshake()");
365 return refs;
368 static struct ref *get_refs_via_connect(struct transport *transport, int for_push,
369 struct transport_ls_refs_options *options)
371 return handshake(transport, for_push, options, 1);
374 static int get_bundle_uri(struct transport *transport)
376 struct git_transport_data *data = transport->data;
377 struct packet_reader reader;
378 int stateless_rpc = transport->stateless_rpc;
380 if (!transport->bundles) {
381 CALLOC_ARRAY(transport->bundles, 1);
382 init_bundle_list(transport->bundles);
385 if (!data->finished_handshake) {
386 struct ref *refs = handshake(transport, 0, NULL, 0);
388 if (refs)
389 free_refs(refs);
393 * "Support" protocol v0 and v2 without bundle-uri support by
394 * silently degrading to a NOOP.
396 if (!server_supports_v2("bundle-uri"))
397 return 0;
399 packet_reader_init(&reader, data->fd[0], NULL, 0,
400 PACKET_READ_CHOMP_NEWLINE |
401 PACKET_READ_GENTLE_ON_EOF);
403 return get_remote_bundle_uri(data->fd[1], &reader,
404 transport->bundles, stateless_rpc);
407 static int fetch_refs_via_pack(struct transport *transport,
408 int nr_heads, struct ref **to_fetch)
410 int ret = 0;
411 struct git_transport_data *data = transport->data;
412 struct ref *refs = NULL;
413 struct fetch_pack_args args;
414 struct ref *refs_tmp = NULL;
416 memset(&args, 0, sizeof(args));
417 args.uploadpack = data->options.uploadpack;
418 args.keep_pack = data->options.keep;
419 args.lock_pack = 1;
420 args.use_thin_pack = data->options.thin;
421 args.include_tag = data->options.followtags;
422 args.verbose = (transport->verbose > 1);
423 args.quiet = (transport->verbose < 0);
424 args.no_progress = !transport->progress;
425 args.depth = data->options.depth;
426 args.deepen_since = data->options.deepen_since;
427 args.deepen_not = data->options.deepen_not;
428 args.deepen_relative = data->options.deepen_relative;
429 args.check_self_contained_and_connected =
430 data->options.check_self_contained_and_connected;
431 args.cloning = transport->cloning;
432 args.update_shallow = data->options.update_shallow;
433 args.from_promisor = data->options.from_promisor;
434 list_objects_filter_copy(&args.filter_options,
435 &data->options.filter_options);
436 args.refetch = data->options.refetch;
437 args.stateless_rpc = transport->stateless_rpc;
438 args.server_options = transport->server_options;
439 args.negotiation_tips = data->options.negotiation_tips;
440 args.reject_shallow_remote = transport->smart_options->reject_shallow;
442 if (!data->finished_handshake) {
443 int i;
444 int must_list_refs = 0;
445 for (i = 0; i < nr_heads; i++) {
446 if (!to_fetch[i]->exact_oid) {
447 must_list_refs = 1;
448 break;
451 refs_tmp = handshake(transport, 0, NULL, must_list_refs);
454 if (data->version == protocol_unknown_version)
455 BUG("unknown protocol version");
456 else if (data->version <= protocol_v1)
457 die_if_server_options(transport);
459 if (data->options.acked_commits) {
460 if (data->version < protocol_v2) {
461 warning(_("--negotiate-only requires protocol v2"));
462 ret = -1;
463 } else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
464 warning(_("server does not support wait-for-done"));
465 ret = -1;
466 } else {
467 negotiate_using_fetch(data->options.negotiation_tips,
468 transport->server_options,
469 transport->stateless_rpc,
470 data->fd,
471 data->options.acked_commits);
472 ret = 0;
474 goto cleanup;
477 refs = fetch_pack(&args, data->fd,
478 refs_tmp ? refs_tmp : transport->remote_refs,
479 to_fetch, nr_heads, &data->shallow,
480 &transport->pack_lockfiles, data->version);
482 data->finished_handshake = 0;
483 data->options.self_contained_and_connected =
484 args.self_contained_and_connected;
485 data->options.connectivity_checked = args.connectivity_checked;
487 if (!refs)
488 ret = -1;
489 if (report_unmatched_refs(to_fetch, nr_heads))
490 ret = -1;
492 cleanup:
493 close(data->fd[0]);
494 if (data->fd[1] >= 0)
495 close(data->fd[1]);
496 if (finish_connect(data->conn))
497 ret = -1;
498 data->conn = NULL;
500 free_refs(refs_tmp);
501 free_refs(refs);
502 list_objects_filter_release(&args.filter_options);
503 return ret;
506 static int push_had_errors(struct ref *ref)
508 for (; ref; ref = ref->next) {
509 switch (ref->status) {
510 case REF_STATUS_NONE:
511 case REF_STATUS_UPTODATE:
512 case REF_STATUS_OK:
513 break;
514 default:
515 return 1;
518 return 0;
521 int transport_refs_pushed(struct ref *ref)
523 for (; ref; ref = ref->next) {
524 switch(ref->status) {
525 case REF_STATUS_NONE:
526 case REF_STATUS_UPTODATE:
527 break;
528 default:
529 return 1;
532 return 0;
535 static void update_one_tracking_ref(struct remote *remote, char *refname,
536 struct object_id *new_oid, int deletion,
537 int verbose)
539 struct refspec_item rs;
541 memset(&rs, 0, sizeof(rs));
542 rs.src = refname;
543 rs.dst = NULL;
545 if (!remote_find_tracking(remote, &rs)) {
546 if (verbose)
547 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
548 if (deletion)
549 delete_ref(NULL, rs.dst, NULL, 0);
550 else
551 update_ref("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 = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
822 if (verbose) {
823 for (ref = refs; ref; ref = ref->next)
824 if (ref->status == REF_STATUS_UPTODATE)
825 n += print_one_push_status(ref, dest, n,
826 porcelain, summary_width);
829 for (ref = refs; ref; ref = ref->next)
830 if (ref->status == REF_STATUS_OK)
831 n += print_one_push_status(ref, dest, n,
832 porcelain, summary_width);
834 *reject_reasons = 0;
835 for (ref = refs; ref; ref = ref->next) {
836 if (ref->status != REF_STATUS_NONE &&
837 ref->status != REF_STATUS_UPTODATE &&
838 ref->status != REF_STATUS_OK)
839 n += print_one_push_status(ref, dest, n,
840 porcelain, summary_width);
841 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
842 if (head != NULL && !strcmp(head, ref->name))
843 *reject_reasons |= REJECT_NON_FF_HEAD;
844 else
845 *reject_reasons |= REJECT_NON_FF_OTHER;
846 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
847 *reject_reasons |= REJECT_ALREADY_EXISTS;
848 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
849 *reject_reasons |= REJECT_FETCH_FIRST;
850 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
851 *reject_reasons |= REJECT_NEEDS_FORCE;
852 } else if (ref->status == REF_STATUS_REJECT_REMOTE_UPDATED) {
853 *reject_reasons |= REJECT_REF_NEEDS_UPDATE;
856 free(head);
859 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
861 struct git_transport_data *data = transport->data;
862 struct send_pack_args args;
863 int ret = 0;
865 if (transport_color_config() < 0)
866 return -1;
868 if (!data->finished_handshake)
869 get_refs_via_connect(transport, 1, NULL);
871 memset(&args, 0, sizeof(args));
872 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
873 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
874 args.use_thin_pack = data->options.thin;
875 args.verbose = (transport->verbose > 0);
876 args.quiet = (transport->verbose < 0);
877 args.progress = transport->progress;
878 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
879 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
880 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
881 args.push_options = transport->push_options;
882 args.url = transport->url;
884 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
885 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
886 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
887 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
888 else
889 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
891 switch (data->version) {
892 case protocol_v2:
893 die(_("support for protocol v2 not implemented yet"));
894 break;
895 case protocol_v1:
896 case protocol_v0:
897 ret = send_pack(&args, data->fd, data->conn, remote_refs,
898 &data->extra_have);
899 break;
900 case protocol_unknown_version:
901 BUG("unknown protocol version");
904 close(data->fd[1]);
905 close(data->fd[0]);
907 * Atomic push may abort the connection early and close the pipe,
908 * which may cause an error for `finish_connect()`. Ignore this error
909 * for atomic git-push.
911 if (ret || args.atomic)
912 finish_connect(data->conn);
913 else
914 ret = finish_connect(data->conn);
915 data->conn = NULL;
916 data->finished_handshake = 0;
918 return ret;
921 static int connect_git(struct transport *transport, const char *name,
922 const char *executable, int fd[2])
924 struct git_transport_data *data = transport->data;
925 data->conn = git_connect(data->fd, transport->url,
926 name, executable, 0);
927 fd[0] = data->fd[0];
928 fd[1] = data->fd[1];
929 return 0;
932 static int disconnect_git(struct transport *transport)
934 struct git_transport_data *data = transport->data;
935 if (data->conn) {
936 if (data->finished_handshake && !transport->stateless_rpc)
937 packet_flush(data->fd[1]);
938 close(data->fd[0]);
939 if (data->fd[1] >= 0)
940 close(data->fd[1]);
941 finish_connect(data->conn);
944 list_objects_filter_release(&data->options.filter_options);
945 free(data);
946 return 0;
949 static struct transport_vtable taken_over_vtable = {
950 .get_refs_list = get_refs_via_connect,
951 .get_bundle_uri = get_bundle_uri,
952 .fetch_refs = fetch_refs_via_pack,
953 .push_refs = git_transport_push,
954 .disconnect = disconnect_git
957 void transport_take_over(struct transport *transport,
958 struct child_process *child)
960 struct git_transport_data *data;
962 if (!transport->smart_options)
963 BUG("taking over transport requires non-NULL "
964 "smart_options field.");
966 CALLOC_ARRAY(data, 1);
967 data->options = *transport->smart_options;
968 data->conn = child;
969 data->fd[0] = data->conn->out;
970 data->fd[1] = data->conn->in;
971 data->finished_handshake = 0;
972 transport->data = data;
974 transport->vtable = &taken_over_vtable;
975 transport->smart_options = &(data->options);
977 transport->cannot_reuse = 1;
980 static int is_file(const char *url)
982 struct stat buf;
983 if (stat(url, &buf))
984 return 0;
985 return S_ISREG(buf.st_mode);
988 static int external_specification_len(const char *url)
990 return strchr(url, ':') - url;
993 static const struct string_list *protocol_allow_list(void)
995 static int enabled = -1;
996 static struct string_list allowed = STRING_LIST_INIT_DUP;
998 if (enabled < 0) {
999 const char *v = getenv("GIT_ALLOW_PROTOCOL");
1000 if (v) {
1001 string_list_split(&allowed, v, ':', -1);
1002 string_list_sort(&allowed);
1003 enabled = 1;
1004 } else {
1005 enabled = 0;
1009 return enabled ? &allowed : NULL;
1012 enum protocol_allow_config {
1013 PROTOCOL_ALLOW_NEVER = 0,
1014 PROTOCOL_ALLOW_USER_ONLY,
1015 PROTOCOL_ALLOW_ALWAYS
1018 static enum protocol_allow_config parse_protocol_config(const char *key,
1019 const char *value)
1021 if (!strcasecmp(value, "always"))
1022 return PROTOCOL_ALLOW_ALWAYS;
1023 else if (!strcasecmp(value, "never"))
1024 return PROTOCOL_ALLOW_NEVER;
1025 else if (!strcasecmp(value, "user"))
1026 return PROTOCOL_ALLOW_USER_ONLY;
1028 die(_("unknown value for config '%s': %s"), key, value);
1031 static enum protocol_allow_config get_protocol_config(const char *type)
1033 char *key = xstrfmt("protocol.%s.allow", type);
1034 char *value;
1036 /* first check the per-protocol config */
1037 if (!git_config_get_string(key, &value)) {
1038 enum protocol_allow_config ret =
1039 parse_protocol_config(key, value);
1040 free(key);
1041 free(value);
1042 return ret;
1044 free(key);
1046 /* if defined, fallback to user-defined default for unknown protocols */
1047 if (!git_config_get_string("protocol.allow", &value)) {
1048 enum protocol_allow_config ret =
1049 parse_protocol_config("protocol.allow", value);
1050 free(value);
1051 return ret;
1054 /* fallback to built-in defaults */
1055 /* known safe */
1056 if (!strcmp(type, "http") ||
1057 !strcmp(type, "https") ||
1058 !strcmp(type, "git") ||
1059 !strcmp(type, "ssh"))
1060 return PROTOCOL_ALLOW_ALWAYS;
1062 /* known scary; err on the side of caution */
1063 if (!strcmp(type, "ext"))
1064 return PROTOCOL_ALLOW_NEVER;
1066 /* unknown; by default let them be used only directly by the user */
1067 return PROTOCOL_ALLOW_USER_ONLY;
1070 int is_transport_allowed(const char *type, int from_user)
1072 const struct string_list *allow_list = protocol_allow_list();
1073 if (allow_list)
1074 return string_list_has_string(allow_list, type);
1076 switch (get_protocol_config(type)) {
1077 case PROTOCOL_ALLOW_ALWAYS:
1078 return 1;
1079 case PROTOCOL_ALLOW_NEVER:
1080 return 0;
1081 case PROTOCOL_ALLOW_USER_ONLY:
1082 if (from_user < 0)
1083 from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1084 return from_user;
1087 BUG("invalid protocol_allow_config type");
1090 void transport_check_allowed(const char *type)
1092 if (!is_transport_allowed(type, -1))
1093 die(_("transport '%s' not allowed"), type);
1096 static struct transport_vtable bundle_vtable = {
1097 .get_refs_list = get_refs_from_bundle,
1098 .fetch_refs = fetch_refs_from_bundle,
1099 .disconnect = close_bundle
1102 static struct transport_vtable builtin_smart_vtable = {
1103 .get_refs_list = get_refs_via_connect,
1104 .get_bundle_uri = get_bundle_uri,
1105 .fetch_refs = fetch_refs_via_pack,
1106 .push_refs = git_transport_push,
1107 .connect = connect_git,
1108 .disconnect = disconnect_git
1111 struct transport *transport_get(struct remote *remote, const char *url)
1113 const char *helper;
1114 struct transport *ret = xcalloc(1, sizeof(*ret));
1116 ret->progress = isatty(2);
1117 string_list_init_dup(&ret->pack_lockfiles);
1119 CALLOC_ARRAY(ret->bundles, 1);
1120 init_bundle_list(ret->bundles);
1122 if (!remote)
1123 BUG("No remote provided to transport_get()");
1125 ret->got_remote_refs = 0;
1126 ret->remote = remote;
1127 helper = remote->foreign_vcs;
1129 if (!url && remote->url)
1130 url = remote->url[0];
1131 ret->url = url;
1133 /* maybe it is a foreign URL? */
1134 if (url) {
1135 const char *p = url;
1137 while (is_urlschemechar(p == url, *p))
1138 p++;
1139 if (starts_with(p, "::"))
1140 helper = xstrndup(url, p - url);
1143 if (helper) {
1144 transport_helper_init(ret, helper);
1145 } else if (starts_with(url, "rsync:")) {
1146 die(_("git-over-rsync is no longer supported"));
1147 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
1148 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
1149 bundle_header_init(&data->header);
1150 transport_check_allowed("file");
1151 ret->data = data;
1152 ret->vtable = &bundle_vtable;
1153 ret->smart_options = NULL;
1154 } else if (!is_url(url)
1155 || starts_with(url, "file://")
1156 || starts_with(url, "git://")
1157 || starts_with(url, "ssh://")
1158 || starts_with(url, "git+ssh://") /* deprecated - do not use */
1159 || starts_with(url, "ssh+git://") /* deprecated - do not use */
1162 * These are builtin smart transports; "allowed" transports
1163 * will be checked individually in git_connect.
1165 struct git_transport_data *data = xcalloc(1, sizeof(*data));
1166 list_objects_filter_init(&data->options.filter_options);
1167 ret->data = data;
1168 ret->vtable = &builtin_smart_vtable;
1169 ret->smart_options = &(data->options);
1171 data->conn = NULL;
1172 data->finished_handshake = 0;
1173 } else {
1174 /* Unknown protocol in URL. Pass to external handler. */
1175 int len = external_specification_len(url);
1176 char *handler = xmemdupz(url, len);
1177 transport_helper_init(ret, handler);
1180 if (ret->smart_options) {
1181 ret->smart_options->thin = 1;
1182 ret->smart_options->uploadpack = "git-upload-pack";
1183 if (remote->uploadpack)
1184 ret->smart_options->uploadpack = remote->uploadpack;
1185 ret->smart_options->receivepack = "git-receive-pack";
1186 if (remote->receivepack)
1187 ret->smart_options->receivepack = remote->receivepack;
1190 ret->hash_algo = &hash_algos[GIT_HASH_SHA1];
1192 return ret;
1195 const struct git_hash_algo *transport_get_hash_algo(struct transport *transport)
1197 return transport->hash_algo;
1200 int transport_set_option(struct transport *transport,
1201 const char *name, const char *value)
1203 int git_reports = 1, protocol_reports = 1;
1205 if (transport->smart_options)
1206 git_reports = set_git_option(transport->smart_options,
1207 name, value);
1209 if (transport->vtable->set_option)
1210 protocol_reports = transport->vtable->set_option(transport,
1211 name, value);
1213 /* If either report is 0, report 0 (success). */
1214 if (!git_reports || !protocol_reports)
1215 return 0;
1216 /* If either reports -1 (invalid value), report -1. */
1217 if ((git_reports == -1) || (protocol_reports == -1))
1218 return -1;
1219 /* Otherwise if both report unknown, report unknown. */
1220 return 1;
1223 void transport_set_verbosity(struct transport *transport, int verbosity,
1224 int force_progress)
1226 if (verbosity >= 1)
1227 transport->verbose = verbosity <= 3 ? verbosity : 3;
1228 if (verbosity < 0)
1229 transport->verbose = -1;
1232 * Rules used to determine whether to report progress (processing aborts
1233 * when a rule is satisfied):
1235 * . Report progress, if force_progress is 1 (ie. --progress).
1236 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1237 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1238 * . Report progress if isatty(2) is 1.
1240 if (force_progress >= 0)
1241 transport->progress = !!force_progress;
1242 else
1243 transport->progress = verbosity >= 0 && isatty(2);
1246 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
1248 int i;
1250 fprintf(stderr, _("The following submodule paths contain changes that can\n"
1251 "not be found on any remote:\n"));
1252 for (i = 0; i < needs_pushing->nr; i++)
1253 fprintf(stderr, " %s\n", needs_pushing->items[i].string);
1254 fprintf(stderr, _("\nPlease try\n\n"
1255 " git push --recurse-submodules=on-demand\n\n"
1256 "or cd to the path and use\n\n"
1257 " git push\n\n"
1258 "to push them to a remote.\n\n"));
1260 string_list_clear(needs_pushing, 0);
1262 die(_("Aborting."));
1265 static int run_pre_push_hook(struct transport *transport,
1266 struct ref *remote_refs)
1268 int ret = 0, x;
1269 struct ref *r;
1270 struct child_process proc = CHILD_PROCESS_INIT;
1271 struct strbuf buf;
1272 const char *hook_path = find_hook("pre-push");
1274 if (!hook_path)
1275 return 0;
1277 strvec_push(&proc.args, hook_path);
1278 strvec_push(&proc.args, transport->remote->name);
1279 strvec_push(&proc.args, transport->url);
1281 proc.in = -1;
1282 proc.trace2_hook_name = "pre-push";
1284 if (start_command(&proc)) {
1285 finish_command(&proc);
1286 return -1;
1289 sigchain_push(SIGPIPE, SIG_IGN);
1291 strbuf_init(&buf, 256);
1293 for (r = remote_refs; r; r = r->next) {
1294 if (!r->peer_ref) continue;
1295 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
1296 if (r->status == REF_STATUS_REJECT_STALE) continue;
1297 if (r->status == REF_STATUS_REJECT_REMOTE_UPDATED) continue;
1298 if (r->status == REF_STATUS_UPTODATE) continue;
1300 strbuf_reset(&buf);
1301 strbuf_addf( &buf, "%s %s %s %s\n",
1302 r->peer_ref->name, oid_to_hex(&r->new_oid),
1303 r->name, oid_to_hex(&r->old_oid));
1305 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
1306 /* We do not mind if a hook does not read all refs. */
1307 if (errno != EPIPE)
1308 ret = -1;
1309 break;
1313 strbuf_release(&buf);
1315 x = close(proc.in);
1316 if (!ret)
1317 ret = x;
1319 sigchain_pop(SIGPIPE);
1321 x = finish_command(&proc);
1322 if (!ret)
1323 ret = x;
1325 return ret;
1328 int transport_push(struct repository *r,
1329 struct transport *transport,
1330 struct refspec *rs, int flags,
1331 unsigned int *reject_reasons)
1333 struct ref *remote_refs = NULL;
1334 struct ref *local_refs = NULL;
1335 int match_flags = MATCH_REFS_NONE;
1336 int verbose = (transport->verbose > 0);
1337 int quiet = (transport->verbose < 0);
1338 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1339 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1340 int push_ret, err;
1341 int ret = -1;
1342 struct transport_ls_refs_options transport_options =
1343 TRANSPORT_LS_REFS_OPTIONS_INIT;
1345 *reject_reasons = 0;
1347 if (transport_color_config() < 0)
1348 goto done;
1350 if (!transport->vtable->push_refs)
1351 goto done;
1353 local_refs = get_local_heads();
1355 if (check_push_refs(local_refs, rs) < 0)
1356 goto done;
1358 refspec_ref_prefixes(rs, &transport_options.ref_prefixes);
1360 trace2_region_enter("transport_push", "get_refs_list", r);
1361 remote_refs = transport->vtable->get_refs_list(transport, 1,
1362 &transport_options);
1363 trace2_region_leave("transport_push", "get_refs_list", r);
1365 transport_ls_refs_options_release(&transport_options);
1367 if (flags & TRANSPORT_PUSH_ALL)
1368 match_flags |= MATCH_REFS_ALL;
1369 if (flags & TRANSPORT_PUSH_MIRROR)
1370 match_flags |= MATCH_REFS_MIRROR;
1371 if (flags & TRANSPORT_PUSH_PRUNE)
1372 match_flags |= MATCH_REFS_PRUNE;
1373 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1374 match_flags |= MATCH_REFS_FOLLOW_TAGS;
1376 if (match_push_refs(local_refs, &remote_refs, rs, match_flags))
1377 goto done;
1379 if (transport->smart_options &&
1380 transport->smart_options->cas &&
1381 !is_empty_cas(transport->smart_options->cas))
1382 apply_push_cas(transport->smart_options->cas,
1383 transport->remote, remote_refs);
1385 set_ref_status_for_push(remote_refs,
1386 flags & TRANSPORT_PUSH_MIRROR,
1387 flags & TRANSPORT_PUSH_FORCE);
1389 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1390 if (run_pre_push_hook(transport, remote_refs))
1391 goto done;
1393 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1394 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1395 !is_bare_repository()) {
1396 struct ref *ref = remote_refs;
1397 struct oid_array commits = OID_ARRAY_INIT;
1399 trace2_region_enter("transport_push", "push_submodules", r);
1400 for (; ref; ref = ref->next)
1401 if (!is_null_oid(&ref->new_oid))
1402 oid_array_append(&commits,
1403 &ref->new_oid);
1405 if (!push_unpushed_submodules(r,
1406 &commits,
1407 transport->remote,
1409 transport->push_options,
1410 pretend)) {
1411 oid_array_clear(&commits);
1412 trace2_region_leave("transport_push", "push_submodules", r);
1413 die(_("failed to push all needed submodules"));
1415 oid_array_clear(&commits);
1416 trace2_region_leave("transport_push", "push_submodules", r);
1419 if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1420 ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1421 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1422 !pretend)) && !is_bare_repository()) {
1423 struct ref *ref = remote_refs;
1424 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1425 struct oid_array commits = OID_ARRAY_INIT;
1427 trace2_region_enter("transport_push", "check_submodules", r);
1428 for (; ref; ref = ref->next)
1429 if (!is_null_oid(&ref->new_oid))
1430 oid_array_append(&commits,
1431 &ref->new_oid);
1433 if (find_unpushed_submodules(r,
1434 &commits,
1435 transport->remote->name,
1436 &needs_pushing)) {
1437 oid_array_clear(&commits);
1438 trace2_region_leave("transport_push", "check_submodules", r);
1439 die_with_unpushed_submodules(&needs_pushing);
1441 string_list_clear(&needs_pushing, 0);
1442 oid_array_clear(&commits);
1443 trace2_region_leave("transport_push", "check_submodules", r);
1446 if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY)) {
1447 trace2_region_enter("transport_push", "push_refs", r);
1448 push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1449 trace2_region_leave("transport_push", "push_refs", r);
1450 } else
1451 push_ret = 0;
1452 err = push_had_errors(remote_refs);
1453 ret = push_ret | err;
1455 if (!quiet || err)
1456 transport_print_push_status(transport->url, remote_refs,
1457 verbose | porcelain, porcelain,
1458 reject_reasons);
1460 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1461 set_upstreams(transport, remote_refs, pretend);
1463 if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1464 TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1465 struct ref *ref;
1466 for (ref = remote_refs; ref; ref = ref->next)
1467 transport_update_tracking_ref(transport->remote, ref, verbose);
1470 if (porcelain && !push_ret)
1471 puts("Done");
1472 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1473 fprintf(stderr, "Everything up-to-date\n");
1475 done:
1476 free_refs(local_refs);
1477 free_refs(remote_refs);
1478 return ret;
1481 const struct ref *transport_get_remote_refs(struct transport *transport,
1482 struct transport_ls_refs_options *transport_options)
1484 if (!transport->got_remote_refs) {
1485 transport->remote_refs =
1486 transport->vtable->get_refs_list(transport, 0,
1487 transport_options);
1488 transport->got_remote_refs = 1;
1491 return transport->remote_refs;
1494 void transport_ls_refs_options_release(struct transport_ls_refs_options *opts)
1496 strvec_clear(&opts->ref_prefixes);
1497 free((char *)opts->unborn_head_target);
1500 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1502 int rc;
1503 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1504 struct ref **heads = NULL;
1505 struct ref *rm;
1507 for (rm = refs; rm; rm = rm->next) {
1508 nr_refs++;
1509 if (rm->peer_ref &&
1510 !is_null_oid(&rm->old_oid) &&
1511 oideq(&rm->peer_ref->old_oid, &rm->old_oid))
1512 continue;
1513 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1514 heads[nr_heads++] = rm;
1517 if (!nr_heads) {
1519 * When deepening of a shallow repository is requested,
1520 * then local and remote refs are likely to still be equal.
1521 * Just feed them all to the fetch method in that case.
1522 * This condition shouldn't be met in a non-deepening fetch
1523 * (see builtin/fetch.c:quickfetch()).
1525 ALLOC_ARRAY(heads, nr_refs);
1526 for (rm = refs; rm; rm = rm->next)
1527 heads[nr_heads++] = rm;
1530 rc = transport->vtable->fetch_refs(transport, nr_heads, heads);
1532 free(heads);
1533 return rc;
1536 int transport_get_remote_bundle_uri(struct transport *transport)
1538 int value = 0;
1539 const struct transport_vtable *vtable = transport->vtable;
1541 /* Check config only once. */
1542 if (transport->got_remote_bundle_uri)
1543 return 0;
1544 transport->got_remote_bundle_uri = 1;
1547 * Don't request bundle-uri from the server unless configured to
1548 * do so by the transfer.bundleURI=true config option.
1550 if (git_config_get_bool("transfer.bundleuri", &value) || !value)
1551 return 0;
1553 if (!transport->bundles->baseURI)
1554 transport->bundles->baseURI = xstrdup(transport->url);
1556 if (!vtable->get_bundle_uri)
1557 return error(_("bundle-uri operation not supported by protocol"));
1559 if (vtable->get_bundle_uri(transport) < 0)
1560 return error(_("could not retrieve server-advertised bundle-uri list"));
1561 return 0;
1564 void transport_unlock_pack(struct transport *transport, unsigned int flags)
1566 int in_signal_handler = !!(flags & TRANSPORT_UNLOCK_PACK_IN_SIGNAL_HANDLER);
1567 int i;
1569 for (i = 0; i < transport->pack_lockfiles.nr; i++)
1570 if (in_signal_handler)
1571 unlink(transport->pack_lockfiles.items[i].string);
1572 else
1573 unlink_or_warn(transport->pack_lockfiles.items[i].string);
1574 if (!in_signal_handler)
1575 string_list_clear(&transport->pack_lockfiles, 0);
1578 int transport_connect(struct transport *transport, const char *name,
1579 const char *exec, int fd[2])
1581 if (transport->vtable->connect)
1582 return transport->vtable->connect(transport, name, exec, fd);
1583 else
1584 die(_("operation not supported by protocol"));
1587 int transport_disconnect(struct transport *transport)
1589 int ret = 0;
1590 if (transport->vtable->disconnect)
1591 ret = transport->vtable->disconnect(transport);
1592 if (transport->got_remote_refs)
1593 free_refs((void *)transport->remote_refs);
1594 clear_bundle_list(transport->bundles);
1595 free(transport->bundles);
1596 free(transport);
1597 return ret;
1601 * Strip username (and password) from a URL and return
1602 * it in a newly allocated string.
1604 char *transport_anonymize_url(const char *url)
1606 char *scheme_prefix, *anon_part;
1607 size_t anon_len, prefix_len = 0;
1609 anon_part = strchr(url, '@');
1610 if (url_is_local_not_ssh(url) || !anon_part)
1611 goto literal_copy;
1613 anon_len = strlen(++anon_part);
1614 scheme_prefix = strstr(url, "://");
1615 if (!scheme_prefix) {
1616 if (!strchr(anon_part, ':'))
1617 /* cannot be "me@there:/path/name" */
1618 goto literal_copy;
1619 } else {
1620 const char *cp;
1621 /* make sure scheme is reasonable */
1622 for (cp = url; cp < scheme_prefix; cp++) {
1623 switch (*cp) {
1624 /* RFC 1738 2.1 */
1625 case '+': case '.': case '-':
1626 break; /* ok */
1627 default:
1628 if (isalnum(*cp))
1629 break;
1630 /* it isn't */
1631 goto literal_copy;
1634 /* @ past the first slash does not count */
1635 cp = strchr(scheme_prefix + 3, '/');
1636 if (cp && cp < anon_part)
1637 goto literal_copy;
1638 prefix_len = scheme_prefix - url + 3;
1640 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1641 (int)anon_len, anon_part);
1642 literal_copy:
1643 return xstrdup(url);