treewide: be explicit about dependence on strbuf.h
[alt-git.git] / transport.c
blob8d3ad8022a405766b92e57152e6a3282fe6b2ae2
1 #include "git-compat-util.h"
2 #include "advice.h"
3 #include "alloc.h"
4 #include "config.h"
5 #include "environment.h"
6 #include "hex.h"
7 #include "transport.h"
8 #include "hook.h"
9 #include "pkt-line.h"
10 #include "fetch-pack.h"
11 #include "remote.h"
12 #include "connect.h"
13 #include "send-pack.h"
14 #include "walker.h"
15 #include "bundle.h"
16 #include "dir.h"
17 #include "gettext.h"
18 #include "refs.h"
19 #include "refspec.h"
20 #include "branch.h"
21 #include "url.h"
22 #include "submodule.h"
23 #include "string-list.h"
24 #include "oid-array.h"
25 #include "sigchain.h"
26 #include "trace2.h"
27 #include "transport-internal.h"
28 #include "protocol.h"
29 #include "object-name.h"
30 #include "object-store.h"
31 #include "color.h"
32 #include "bundle-uri.h"
33 #include "wrapper.h"
35 static int transport_use_color = -1;
36 static char transport_colors[][COLOR_MAXLEN] = {
37 GIT_COLOR_RESET,
38 GIT_COLOR_RED /* REJECTED */
41 enum color_transport {
42 TRANSPORT_COLOR_RESET = 0,
43 TRANSPORT_COLOR_REJECTED = 1
46 static int transport_color_config(void)
48 const char *keys[] = {
49 "color.transport.reset",
50 "color.transport.rejected"
51 }, *key = "color.transport";
52 char *value;
53 int i;
54 static int initialized;
56 if (initialized)
57 return 0;
58 initialized = 1;
60 if (!git_config_get_string(key, &value))
61 transport_use_color = git_config_colorbool(key, value);
63 if (!want_color_stderr(transport_use_color))
64 return 0;
66 for (i = 0; i < ARRAY_SIZE(keys); i++)
67 if (!git_config_get_string(keys[i], &value)) {
68 if (!value)
69 return config_error_nonbool(keys[i]);
70 if (color_parse(value, transport_colors[i]) < 0)
71 return -1;
74 return 0;
77 static const char *transport_get_color(enum color_transport ix)
79 if (want_color_stderr(transport_use_color))
80 return transport_colors[ix];
81 return "";
84 static void set_upstreams(struct transport *transport, struct ref *refs,
85 int pretend)
87 struct ref *ref;
88 for (ref = refs; ref; ref = ref->next) {
89 const char *localname;
90 const char *tmp;
91 const char *remotename;
92 int flag = 0;
94 * Check suitability for tracking. Must be successful /
95 * already up-to-date ref create/modify (not delete).
97 if (ref->status != REF_STATUS_OK &&
98 ref->status != REF_STATUS_UPTODATE)
99 continue;
100 if (!ref->peer_ref)
101 continue;
102 if (is_null_oid(&ref->new_oid))
103 continue;
105 /* Follow symbolic refs (mainly for HEAD). */
106 localname = ref->peer_ref->name;
107 remotename = ref->name;
108 tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING,
109 NULL, &flag);
110 if (tmp && flag & REF_ISSYMREF &&
111 starts_with(tmp, "refs/heads/"))
112 localname = tmp;
114 /* Both source and destination must be local branches. */
115 if (!localname || !starts_with(localname, "refs/heads/"))
116 continue;
117 if (!remotename || !starts_with(remotename, "refs/heads/"))
118 continue;
120 if (!pretend) {
121 int flag = transport->verbose < 0 ? 0 : BRANCH_CONFIG_VERBOSE;
122 install_branch_config(flag, localname + 11,
123 transport->remote->name, remotename);
124 } else if (transport->verbose >= 0)
125 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
126 localname + 11, remotename + 11,
127 transport->remote->name);
131 struct bundle_transport_data {
132 int fd;
133 struct bundle_header header;
134 unsigned get_refs_from_bundle_called : 1;
137 static void get_refs_from_bundle_inner(struct transport *transport)
139 struct bundle_transport_data *data = transport->data;
141 data->get_refs_from_bundle_called = 1;
143 if (data->fd > 0)
144 close(data->fd);
145 data->fd = read_bundle_header(transport->url, &data->header);
146 if (data->fd < 0)
147 die(_("could not read bundle '%s'"), transport->url);
149 transport->hash_algo = data->header.hash_algo;
152 static struct ref *get_refs_from_bundle(struct transport *transport,
153 int for_push,
154 struct transport_ls_refs_options *transport_options UNUSED)
156 struct bundle_transport_data *data = transport->data;
157 struct ref *result = NULL;
158 int i;
160 if (for_push)
161 return NULL;
163 get_refs_from_bundle_inner(transport);
165 for (i = 0; i < data->header.references.nr; i++) {
166 struct string_list_item *e = data->header.references.items + i;
167 const char *name = e->string;
168 struct ref *ref = alloc_ref(name);
169 struct object_id *oid = e->util;
170 oidcpy(&ref->old_oid, oid);
171 ref->next = result;
172 result = ref;
174 return result;
177 static int fetch_refs_from_bundle(struct transport *transport,
178 int nr_heads, struct ref **to_fetch)
180 struct bundle_transport_data *data = transport->data;
181 struct strvec extra_index_pack_args = STRVEC_INIT;
182 int ret;
184 if (transport->progress)
185 strvec_push(&extra_index_pack_args, "-v");
187 if (!data->get_refs_from_bundle_called)
188 get_refs_from_bundle_inner(transport);
189 ret = unbundle(the_repository, &data->header, data->fd,
190 &extra_index_pack_args, 0);
191 transport->hash_algo = data->header.hash_algo;
192 return ret;
195 static int close_bundle(struct transport *transport)
197 struct bundle_transport_data *data = transport->data;
198 if (data->fd > 0)
199 close(data->fd);
200 bundle_header_release(&data->header);
201 free(data);
202 return 0;
205 struct git_transport_data {
206 struct git_transport_options options;
207 struct child_process *conn;
208 int fd[2];
209 unsigned finished_handshake : 1;
210 enum protocol_version version;
211 struct oid_array extra_have;
212 struct oid_array shallow;
215 static int set_git_option(struct git_transport_options *opts,
216 const char *name, const char *value)
218 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
219 opts->uploadpack = value;
220 return 0;
221 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
222 opts->receivepack = value;
223 return 0;
224 } else if (!strcmp(name, TRANS_OPT_THIN)) {
225 opts->thin = !!value;
226 return 0;
227 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
228 opts->followtags = !!value;
229 return 0;
230 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
231 opts->keep = !!value;
232 return 0;
233 } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
234 opts->update_shallow = !!value;
235 return 0;
236 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
237 if (!value)
238 opts->depth = 0;
239 else {
240 char *end;
241 opts->depth = strtol(value, &end, 0);
242 if (*end)
243 die(_("transport: invalid depth option '%s'"), value);
245 return 0;
246 } else if (!strcmp(name, TRANS_OPT_DEEPEN_SINCE)) {
247 opts->deepen_since = value;
248 return 0;
249 } else if (!strcmp(name, TRANS_OPT_DEEPEN_NOT)) {
250 opts->deepen_not = (const struct string_list *)value;
251 return 0;
252 } else if (!strcmp(name, TRANS_OPT_DEEPEN_RELATIVE)) {
253 opts->deepen_relative = !!value;
254 return 0;
255 } else if (!strcmp(name, TRANS_OPT_FROM_PROMISOR)) {
256 opts->from_promisor = !!value;
257 return 0;
258 } else if (!strcmp(name, TRANS_OPT_LIST_OBJECTS_FILTER)) {
259 list_objects_filter_die_if_populated(&opts->filter_options);
260 parse_list_objects_filter(&opts->filter_options, value);
261 return 0;
262 } else if (!strcmp(name, TRANS_OPT_REFETCH)) {
263 opts->refetch = !!value;
264 return 0;
265 } else if (!strcmp(name, TRANS_OPT_REJECT_SHALLOW)) {
266 opts->reject_shallow = !!value;
267 return 0;
269 return 1;
272 static int connect_setup(struct transport *transport, int for_push)
274 struct git_transport_data *data = transport->data;
275 int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
277 if (data->conn)
278 return 0;
280 switch (transport->family) {
281 case TRANSPORT_FAMILY_ALL: break;
282 case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
283 case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
286 data->conn = git_connect(data->fd, transport->url,
287 for_push ? data->options.receivepack :
288 data->options.uploadpack,
289 flags);
291 return 0;
294 static void die_if_server_options(struct transport *transport)
296 if (!transport->server_options || !transport->server_options->nr)
297 return;
298 advise(_("see protocol.version in 'git help config' for more details"));
299 die(_("server options require protocol version 2 or later"));
303 * Obtains the protocol version from the transport and writes it to
304 * transport->data->version, first connecting if not already connected.
306 * If the protocol version is one that allows skipping the listing of remote
307 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
308 * this function returns NULL. Otherwise, this function returns the list of
309 * remote refs.
311 static struct ref *handshake(struct transport *transport, int for_push,
312 struct transport_ls_refs_options *options,
313 int must_list_refs)
315 struct git_transport_data *data = transport->data;
316 struct ref *refs = NULL;
317 struct packet_reader reader;
318 int sid_len;
319 const char *server_sid;
321 connect_setup(transport, for_push);
323 packet_reader_init(&reader, data->fd[0], NULL, 0,
324 PACKET_READ_CHOMP_NEWLINE |
325 PACKET_READ_GENTLE_ON_EOF |
326 PACKET_READ_DIE_ON_ERR_PACKET);
328 data->version = discover_version(&reader);
329 switch (data->version) {
330 case protocol_v2:
331 if (server_feature_v2("session-id", &server_sid))
332 trace2_data_string("transfer", NULL, "server-sid", server_sid);
333 if (must_list_refs)
334 get_remote_refs(data->fd[1], &reader, &refs, for_push,
335 options,
336 transport->server_options,
337 transport->stateless_rpc);
338 break;
339 case protocol_v1:
340 case protocol_v0:
341 die_if_server_options(transport);
342 get_remote_heads(&reader, &refs,
343 for_push ? REF_NORMAL : 0,
344 &data->extra_have,
345 &data->shallow);
346 server_sid = server_feature_value("session-id", &sid_len);
347 if (server_sid) {
348 char *sid = xstrndup(server_sid, sid_len);
349 trace2_data_string("transfer", NULL, "server-sid", sid);
350 free(sid);
352 break;
353 case protocol_unknown_version:
354 BUG("unknown protocol version");
356 data->finished_handshake = 1;
357 transport->hash_algo = reader.hash_algo;
359 if (reader.line_peeked)
360 BUG("buffer must be empty at the end of handshake()");
362 return refs;
365 static struct ref *get_refs_via_connect(struct transport *transport, int for_push,
366 struct transport_ls_refs_options *options)
368 return handshake(transport, for_push, options, 1);
371 static int get_bundle_uri(struct transport *transport)
373 struct git_transport_data *data = transport->data;
374 struct packet_reader reader;
375 int stateless_rpc = transport->stateless_rpc;
377 if (!transport->bundles) {
378 CALLOC_ARRAY(transport->bundles, 1);
379 init_bundle_list(transport->bundles);
382 if (!data->finished_handshake) {
383 struct ref *refs = handshake(transport, 0, NULL, 0);
385 if (refs)
386 free_refs(refs);
390 * "Support" protocol v0 and v2 without bundle-uri support by
391 * silently degrading to a NOOP.
393 if (!server_supports_v2("bundle-uri"))
394 return 0;
396 packet_reader_init(&reader, data->fd[0], NULL, 0,
397 PACKET_READ_CHOMP_NEWLINE |
398 PACKET_READ_GENTLE_ON_EOF);
400 return get_remote_bundle_uri(data->fd[1], &reader,
401 transport->bundles, stateless_rpc);
404 static int fetch_refs_via_pack(struct transport *transport,
405 int nr_heads, struct ref **to_fetch)
407 int ret = 0;
408 struct git_transport_data *data = transport->data;
409 struct ref *refs = NULL;
410 struct fetch_pack_args args;
411 struct ref *refs_tmp = NULL;
413 memset(&args, 0, sizeof(args));
414 args.uploadpack = data->options.uploadpack;
415 args.keep_pack = data->options.keep;
416 args.lock_pack = 1;
417 args.use_thin_pack = data->options.thin;
418 args.include_tag = data->options.followtags;
419 args.verbose = (transport->verbose > 1);
420 args.quiet = (transport->verbose < 0);
421 args.no_progress = !transport->progress;
422 args.depth = data->options.depth;
423 args.deepen_since = data->options.deepen_since;
424 args.deepen_not = data->options.deepen_not;
425 args.deepen_relative = data->options.deepen_relative;
426 args.check_self_contained_and_connected =
427 data->options.check_self_contained_and_connected;
428 args.cloning = transport->cloning;
429 args.update_shallow = data->options.update_shallow;
430 args.from_promisor = data->options.from_promisor;
431 list_objects_filter_copy(&args.filter_options,
432 &data->options.filter_options);
433 args.refetch = data->options.refetch;
434 args.stateless_rpc = transport->stateless_rpc;
435 args.server_options = transport->server_options;
436 args.negotiation_tips = data->options.negotiation_tips;
437 args.reject_shallow_remote = transport->smart_options->reject_shallow;
439 if (!data->finished_handshake) {
440 int i;
441 int must_list_refs = 0;
442 for (i = 0; i < nr_heads; i++) {
443 if (!to_fetch[i]->exact_oid) {
444 must_list_refs = 1;
445 break;
448 refs_tmp = handshake(transport, 0, NULL, must_list_refs);
451 if (data->version == protocol_unknown_version)
452 BUG("unknown protocol version");
453 else if (data->version <= protocol_v1)
454 die_if_server_options(transport);
456 if (data->options.acked_commits) {
457 if (data->version < protocol_v2) {
458 warning(_("--negotiate-only requires protocol v2"));
459 ret = -1;
460 } else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
461 warning(_("server does not support wait-for-done"));
462 ret = -1;
463 } else {
464 negotiate_using_fetch(data->options.negotiation_tips,
465 transport->server_options,
466 transport->stateless_rpc,
467 data->fd,
468 data->options.acked_commits);
469 ret = 0;
471 goto cleanup;
474 refs = fetch_pack(&args, data->fd,
475 refs_tmp ? refs_tmp : transport->remote_refs,
476 to_fetch, nr_heads, &data->shallow,
477 &transport->pack_lockfiles, data->version);
479 data->finished_handshake = 0;
480 data->options.self_contained_and_connected =
481 args.self_contained_and_connected;
482 data->options.connectivity_checked = args.connectivity_checked;
484 if (!refs)
485 ret = -1;
486 if (report_unmatched_refs(to_fetch, nr_heads))
487 ret = -1;
489 cleanup:
490 close(data->fd[0]);
491 if (data->fd[1] >= 0)
492 close(data->fd[1]);
493 if (finish_connect(data->conn))
494 ret = -1;
495 data->conn = NULL;
497 free_refs(refs_tmp);
498 free_refs(refs);
499 list_objects_filter_release(&args.filter_options);
500 return ret;
503 static int push_had_errors(struct ref *ref)
505 for (; ref; ref = ref->next) {
506 switch (ref->status) {
507 case REF_STATUS_NONE:
508 case REF_STATUS_UPTODATE:
509 case REF_STATUS_OK:
510 break;
511 default:
512 return 1;
515 return 0;
518 int transport_refs_pushed(struct ref *ref)
520 for (; ref; ref = ref->next) {
521 switch(ref->status) {
522 case REF_STATUS_NONE:
523 case REF_STATUS_UPTODATE:
524 break;
525 default:
526 return 1;
529 return 0;
532 static void update_one_tracking_ref(struct remote *remote, char *refname,
533 struct object_id *new_oid, int deletion,
534 int verbose)
536 struct refspec_item rs;
538 memset(&rs, 0, sizeof(rs));
539 rs.src = refname;
540 rs.dst = NULL;
542 if (!remote_find_tracking(remote, &rs)) {
543 if (verbose)
544 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
545 if (deletion)
546 delete_ref(NULL, rs.dst, NULL, 0);
547 else
548 update_ref("update by push", rs.dst, new_oid,
549 NULL, 0, 0);
550 free(rs.dst);
554 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
556 char *refname;
557 struct object_id *new_oid;
558 struct ref_push_report *report;
560 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
561 return;
563 report = ref->report;
564 if (!report)
565 update_one_tracking_ref(remote, ref->name, &ref->new_oid,
566 ref->deletion, verbose);
567 else
568 for (; report; report = report->next) {
569 refname = report->ref_name ? (char *)report->ref_name : ref->name;
570 new_oid = report->new_oid ? report->new_oid : &ref->new_oid;
571 update_one_tracking_ref(remote, refname, new_oid,
572 is_null_oid(new_oid), verbose);
576 static void print_ref_status(char flag, const char *summary,
577 struct ref *to, struct ref *from, const char *msg,
578 struct ref_push_report *report,
579 int porcelain, int summary_width)
581 const char *to_name;
583 if (report && report->ref_name)
584 to_name = report->ref_name;
585 else
586 to_name = to->name;
588 if (porcelain) {
589 if (from)
590 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to_name);
591 else
592 fprintf(stdout, "%c\t:%s\t", flag, to_name);
593 if (msg)
594 fprintf(stdout, "%s (%s)\n", summary, msg);
595 else
596 fprintf(stdout, "%s\n", summary);
597 } else {
598 const char *red = "", *reset = "";
599 if (push_had_errors(to)) {
600 red = transport_get_color(TRANSPORT_COLOR_REJECTED);
601 reset = transport_get_color(TRANSPORT_COLOR_RESET);
603 fprintf(stderr, " %s%c %-*s%s ", red, flag, summary_width,
604 summary, reset);
605 if (from)
606 fprintf(stderr, "%s -> %s",
607 prettify_refname(from->name),
608 prettify_refname(to_name));
609 else
610 fputs(prettify_refname(to_name), stderr);
611 if (msg) {
612 fputs(" (", stderr);
613 fputs(msg, stderr);
614 fputc(')', stderr);
616 fputc('\n', stderr);
620 static void print_ok_ref_status(struct ref *ref,
621 struct ref_push_report *report,
622 int porcelain, int summary_width)
624 struct object_id *old_oid;
625 struct object_id *new_oid;
626 const char *ref_name;
627 int forced_update;
629 if (report && report->old_oid)
630 old_oid = report->old_oid;
631 else
632 old_oid = &ref->old_oid;
633 if (report && report->new_oid)
634 new_oid = report->new_oid;
635 else
636 new_oid = &ref->new_oid;
637 if (report && report->forced_update)
638 forced_update = report->forced_update;
639 else
640 forced_update = ref->forced_update;
641 if (report && report->ref_name)
642 ref_name = report->ref_name;
643 else
644 ref_name = ref->name;
646 if (ref->deletion)
647 print_ref_status('-', "[deleted]", ref, NULL, NULL,
648 report, porcelain, summary_width);
649 else if (is_null_oid(old_oid))
650 print_ref_status('*',
651 (starts_with(ref_name, "refs/tags/")
652 ? "[new tag]"
653 : (starts_with(ref_name, "refs/heads/")
654 ? "[new branch]"
655 : "[new reference]")),
656 ref, ref->peer_ref, NULL,
657 report, porcelain, summary_width);
658 else {
659 struct strbuf quickref = STRBUF_INIT;
660 char type;
661 const char *msg;
663 strbuf_add_unique_abbrev(&quickref, old_oid,
664 DEFAULT_ABBREV);
665 if (forced_update) {
666 strbuf_addstr(&quickref, "...");
667 type = '+';
668 msg = "forced update";
669 } else {
670 strbuf_addstr(&quickref, "..");
671 type = ' ';
672 msg = NULL;
674 strbuf_add_unique_abbrev(&quickref, new_oid,
675 DEFAULT_ABBREV);
677 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
678 report, porcelain, summary_width);
679 strbuf_release(&quickref);
683 static int print_one_push_report(struct ref *ref, const char *dest, int count,
684 struct ref_push_report *report,
685 int porcelain, int summary_width)
687 if (!count) {
688 char *url = transport_anonymize_url(dest);
689 fprintf(porcelain ? stdout : stderr, "To %s\n", url);
690 free(url);
693 switch(ref->status) {
694 case REF_STATUS_NONE:
695 print_ref_status('X', "[no match]", ref, NULL, NULL,
696 report, porcelain, summary_width);
697 break;
698 case REF_STATUS_REJECT_NODELETE:
699 print_ref_status('!', "[rejected]", ref, NULL,
700 "remote does not support deleting refs",
701 report, porcelain, summary_width);
702 break;
703 case REF_STATUS_UPTODATE:
704 print_ref_status('=', "[up to date]", ref,
705 ref->peer_ref, NULL,
706 report, porcelain, summary_width);
707 break;
708 case REF_STATUS_REJECT_NONFASTFORWARD:
709 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
710 "non-fast-forward",
711 report, porcelain, summary_width);
712 break;
713 case REF_STATUS_REJECT_ALREADY_EXISTS:
714 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
715 "already exists",
716 report, porcelain, summary_width);
717 break;
718 case REF_STATUS_REJECT_FETCH_FIRST:
719 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
720 "fetch first",
721 report, porcelain, summary_width);
722 break;
723 case REF_STATUS_REJECT_NEEDS_FORCE:
724 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
725 "needs force",
726 report, porcelain, summary_width);
727 break;
728 case REF_STATUS_REJECT_STALE:
729 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
730 "stale info",
731 report, porcelain, summary_width);
732 break;
733 case REF_STATUS_REJECT_REMOTE_UPDATED:
734 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
735 "remote ref updated since checkout",
736 report, porcelain, summary_width);
737 break;
738 case REF_STATUS_REJECT_SHALLOW:
739 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
740 "new shallow roots not allowed",
741 report, porcelain, summary_width);
742 break;
743 case REF_STATUS_REMOTE_REJECT:
744 print_ref_status('!', "[remote rejected]", ref,
745 ref->deletion ? NULL : ref->peer_ref,
746 ref->remote_status,
747 report, porcelain, summary_width);
748 break;
749 case REF_STATUS_EXPECTING_REPORT:
750 print_ref_status('!', "[remote failure]", ref,
751 ref->deletion ? NULL : ref->peer_ref,
752 "remote failed to report status",
753 report, porcelain, summary_width);
754 break;
755 case REF_STATUS_ATOMIC_PUSH_FAILED:
756 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
757 "atomic push failed",
758 report, porcelain, summary_width);
759 break;
760 case REF_STATUS_OK:
761 print_ok_ref_status(ref, report, porcelain, summary_width);
762 break;
765 return 1;
768 static int print_one_push_status(struct ref *ref, const char *dest, int count,
769 int porcelain, int summary_width)
771 struct ref_push_report *report;
772 int n = 0;
774 if (!ref->report)
775 return print_one_push_report(ref, dest, count,
776 NULL, porcelain, summary_width);
778 for (report = ref->report; report; report = report->next)
779 print_one_push_report(ref, dest, count + n++,
780 report, porcelain, summary_width);
781 return n;
784 static int measure_abbrev(const struct object_id *oid, int sofar)
786 char hex[GIT_MAX_HEXSZ + 1];
787 int w = repo_find_unique_abbrev_r(the_repository, hex, oid,
788 DEFAULT_ABBREV);
790 return (w < sofar) ? sofar : w;
793 int transport_summary_width(const struct ref *refs)
795 int maxw = -1;
797 for (; refs; refs = refs->next) {
798 maxw = measure_abbrev(&refs->old_oid, maxw);
799 maxw = measure_abbrev(&refs->new_oid, maxw);
801 if (maxw < 0)
802 maxw = FALLBACK_DEFAULT_ABBREV;
803 return (2 * maxw + 3);
806 void transport_print_push_status(const char *dest, struct ref *refs,
807 int verbose, int porcelain, unsigned int *reject_reasons)
809 struct ref *ref;
810 int n = 0;
811 char *head;
812 int summary_width = transport_summary_width(refs);
814 if (transport_color_config() < 0)
815 warning(_("could not parse transport.color.* config"));
817 head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
819 if (verbose) {
820 for (ref = refs; ref; ref = ref->next)
821 if (ref->status == REF_STATUS_UPTODATE)
822 n += print_one_push_status(ref, dest, n,
823 porcelain, summary_width);
826 for (ref = refs; ref; ref = ref->next)
827 if (ref->status == REF_STATUS_OK)
828 n += print_one_push_status(ref, dest, n,
829 porcelain, summary_width);
831 *reject_reasons = 0;
832 for (ref = refs; ref; ref = ref->next) {
833 if (ref->status != REF_STATUS_NONE &&
834 ref->status != REF_STATUS_UPTODATE &&
835 ref->status != REF_STATUS_OK)
836 n += print_one_push_status(ref, dest, n,
837 porcelain, summary_width);
838 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
839 if (head != NULL && !strcmp(head, ref->name))
840 *reject_reasons |= REJECT_NON_FF_HEAD;
841 else
842 *reject_reasons |= REJECT_NON_FF_OTHER;
843 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
844 *reject_reasons |= REJECT_ALREADY_EXISTS;
845 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
846 *reject_reasons |= REJECT_FETCH_FIRST;
847 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
848 *reject_reasons |= REJECT_NEEDS_FORCE;
849 } else if (ref->status == REF_STATUS_REJECT_REMOTE_UPDATED) {
850 *reject_reasons |= REJECT_REF_NEEDS_UPDATE;
853 free(head);
856 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
858 struct git_transport_data *data = transport->data;
859 struct send_pack_args args;
860 int ret = 0;
862 if (transport_color_config() < 0)
863 return -1;
865 if (!data->finished_handshake)
866 get_refs_via_connect(transport, 1, NULL);
868 memset(&args, 0, sizeof(args));
869 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
870 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
871 args.use_thin_pack = data->options.thin;
872 args.verbose = (transport->verbose > 0);
873 args.quiet = (transport->verbose < 0);
874 args.progress = transport->progress;
875 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
876 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
877 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
878 args.push_options = transport->push_options;
879 args.url = transport->url;
881 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
882 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
883 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
884 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
885 else
886 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
888 switch (data->version) {
889 case protocol_v2:
890 die(_("support for protocol v2 not implemented yet"));
891 break;
892 case protocol_v1:
893 case protocol_v0:
894 ret = send_pack(&args, data->fd, data->conn, remote_refs,
895 &data->extra_have);
896 break;
897 case protocol_unknown_version:
898 BUG("unknown protocol version");
901 close(data->fd[1]);
902 close(data->fd[0]);
904 * Atomic push may abort the connection early and close the pipe,
905 * which may cause an error for `finish_connect()`. Ignore this error
906 * for atomic git-push.
908 if (ret || args.atomic)
909 finish_connect(data->conn);
910 else
911 ret = finish_connect(data->conn);
912 data->conn = NULL;
913 data->finished_handshake = 0;
915 return ret;
918 static int connect_git(struct transport *transport, const char *name,
919 const char *executable, int fd[2])
921 struct git_transport_data *data = transport->data;
922 data->conn = git_connect(data->fd, transport->url,
923 executable, 0);
924 fd[0] = data->fd[0];
925 fd[1] = data->fd[1];
926 return 0;
929 static int disconnect_git(struct transport *transport)
931 struct git_transport_data *data = transport->data;
932 if (data->conn) {
933 if (data->finished_handshake && !transport->stateless_rpc)
934 packet_flush(data->fd[1]);
935 close(data->fd[0]);
936 if (data->fd[1] >= 0)
937 close(data->fd[1]);
938 finish_connect(data->conn);
941 list_objects_filter_release(&data->options.filter_options);
942 free(data);
943 return 0;
946 static struct transport_vtable taken_over_vtable = {
947 .get_refs_list = get_refs_via_connect,
948 .get_bundle_uri = get_bundle_uri,
949 .fetch_refs = fetch_refs_via_pack,
950 .push_refs = git_transport_push,
951 .disconnect = disconnect_git
954 void transport_take_over(struct transport *transport,
955 struct child_process *child)
957 struct git_transport_data *data;
959 if (!transport->smart_options)
960 BUG("taking over transport requires non-NULL "
961 "smart_options field.");
963 CALLOC_ARRAY(data, 1);
964 data->options = *transport->smart_options;
965 data->conn = child;
966 data->fd[0] = data->conn->out;
967 data->fd[1] = data->conn->in;
968 data->finished_handshake = 0;
969 transport->data = data;
971 transport->vtable = &taken_over_vtable;
972 transport->smart_options = &(data->options);
974 transport->cannot_reuse = 1;
977 static int is_file(const char *url)
979 struct stat buf;
980 if (stat(url, &buf))
981 return 0;
982 return S_ISREG(buf.st_mode);
985 static int external_specification_len(const char *url)
987 return strchr(url, ':') - url;
990 static const struct string_list *protocol_allow_list(void)
992 static int enabled = -1;
993 static struct string_list allowed = STRING_LIST_INIT_DUP;
995 if (enabled < 0) {
996 const char *v = getenv("GIT_ALLOW_PROTOCOL");
997 if (v) {
998 string_list_split(&allowed, v, ':', -1);
999 string_list_sort(&allowed);
1000 enabled = 1;
1001 } else {
1002 enabled = 0;
1006 return enabled ? &allowed : NULL;
1009 enum protocol_allow_config {
1010 PROTOCOL_ALLOW_NEVER = 0,
1011 PROTOCOL_ALLOW_USER_ONLY,
1012 PROTOCOL_ALLOW_ALWAYS
1015 static enum protocol_allow_config parse_protocol_config(const char *key,
1016 const char *value)
1018 if (!strcasecmp(value, "always"))
1019 return PROTOCOL_ALLOW_ALWAYS;
1020 else if (!strcasecmp(value, "never"))
1021 return PROTOCOL_ALLOW_NEVER;
1022 else if (!strcasecmp(value, "user"))
1023 return PROTOCOL_ALLOW_USER_ONLY;
1025 die(_("unknown value for config '%s': %s"), key, value);
1028 static enum protocol_allow_config get_protocol_config(const char *type)
1030 char *key = xstrfmt("protocol.%s.allow", type);
1031 char *value;
1033 /* first check the per-protocol config */
1034 if (!git_config_get_string(key, &value)) {
1035 enum protocol_allow_config ret =
1036 parse_protocol_config(key, value);
1037 free(key);
1038 free(value);
1039 return ret;
1041 free(key);
1043 /* if defined, fallback to user-defined default for unknown protocols */
1044 if (!git_config_get_string("protocol.allow", &value)) {
1045 enum protocol_allow_config ret =
1046 parse_protocol_config("protocol.allow", value);
1047 free(value);
1048 return ret;
1051 /* fallback to built-in defaults */
1052 /* known safe */
1053 if (!strcmp(type, "http") ||
1054 !strcmp(type, "https") ||
1055 !strcmp(type, "git") ||
1056 !strcmp(type, "ssh"))
1057 return PROTOCOL_ALLOW_ALWAYS;
1059 /* known scary; err on the side of caution */
1060 if (!strcmp(type, "ext"))
1061 return PROTOCOL_ALLOW_NEVER;
1063 /* unknown; by default let them be used only directly by the user */
1064 return PROTOCOL_ALLOW_USER_ONLY;
1067 int is_transport_allowed(const char *type, int from_user)
1069 const struct string_list *allow_list = protocol_allow_list();
1070 if (allow_list)
1071 return string_list_has_string(allow_list, type);
1073 switch (get_protocol_config(type)) {
1074 case PROTOCOL_ALLOW_ALWAYS:
1075 return 1;
1076 case PROTOCOL_ALLOW_NEVER:
1077 return 0;
1078 case PROTOCOL_ALLOW_USER_ONLY:
1079 if (from_user < 0)
1080 from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1081 return from_user;
1084 BUG("invalid protocol_allow_config type");
1087 void transport_check_allowed(const char *type)
1089 if (!is_transport_allowed(type, -1))
1090 die(_("transport '%s' not allowed"), type);
1093 static struct transport_vtable bundle_vtable = {
1094 .get_refs_list = get_refs_from_bundle,
1095 .fetch_refs = fetch_refs_from_bundle,
1096 .disconnect = close_bundle
1099 static struct transport_vtable builtin_smart_vtable = {
1100 .get_refs_list = get_refs_via_connect,
1101 .get_bundle_uri = get_bundle_uri,
1102 .fetch_refs = fetch_refs_via_pack,
1103 .push_refs = git_transport_push,
1104 .connect = connect_git,
1105 .disconnect = disconnect_git
1108 struct transport *transport_get(struct remote *remote, const char *url)
1110 const char *helper;
1111 struct transport *ret = xcalloc(1, sizeof(*ret));
1113 ret->progress = isatty(2);
1114 string_list_init_dup(&ret->pack_lockfiles);
1116 CALLOC_ARRAY(ret->bundles, 1);
1117 init_bundle_list(ret->bundles);
1119 if (!remote)
1120 BUG("No remote provided to transport_get()");
1122 ret->got_remote_refs = 0;
1123 ret->remote = remote;
1124 helper = remote->foreign_vcs;
1126 if (!url && remote->url)
1127 url = remote->url[0];
1128 ret->url = url;
1130 /* maybe it is a foreign URL? */
1131 if (url) {
1132 const char *p = url;
1134 while (is_urlschemechar(p == url, *p))
1135 p++;
1136 if (starts_with(p, "::"))
1137 helper = xstrndup(url, p - url);
1140 if (helper) {
1141 transport_helper_init(ret, helper);
1142 } else if (starts_with(url, "rsync:")) {
1143 die(_("git-over-rsync is no longer supported"));
1144 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
1145 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
1146 bundle_header_init(&data->header);
1147 transport_check_allowed("file");
1148 ret->data = data;
1149 ret->vtable = &bundle_vtable;
1150 ret->smart_options = NULL;
1151 } else if (!is_url(url)
1152 || starts_with(url, "file://")
1153 || starts_with(url, "git://")
1154 || starts_with(url, "ssh://")
1155 || starts_with(url, "git+ssh://") /* deprecated - do not use */
1156 || starts_with(url, "ssh+git://") /* deprecated - do not use */
1159 * These are builtin smart transports; "allowed" transports
1160 * will be checked individually in git_connect.
1162 struct git_transport_data *data = xcalloc(1, sizeof(*data));
1163 list_objects_filter_init(&data->options.filter_options);
1164 ret->data = data;
1165 ret->vtable = &builtin_smart_vtable;
1166 ret->smart_options = &(data->options);
1168 data->conn = NULL;
1169 data->finished_handshake = 0;
1170 } else {
1171 /* Unknown protocol in URL. Pass to external handler. */
1172 int len = external_specification_len(url);
1173 char *handler = xmemdupz(url, len);
1174 transport_helper_init(ret, handler);
1177 if (ret->smart_options) {
1178 ret->smart_options->thin = 1;
1179 ret->smart_options->uploadpack = "git-upload-pack";
1180 if (remote->uploadpack)
1181 ret->smart_options->uploadpack = remote->uploadpack;
1182 ret->smart_options->receivepack = "git-receive-pack";
1183 if (remote->receivepack)
1184 ret->smart_options->receivepack = remote->receivepack;
1187 ret->hash_algo = &hash_algos[GIT_HASH_SHA1];
1189 return ret;
1192 const struct git_hash_algo *transport_get_hash_algo(struct transport *transport)
1194 return transport->hash_algo;
1197 int transport_set_option(struct transport *transport,
1198 const char *name, const char *value)
1200 int git_reports = 1, protocol_reports = 1;
1202 if (transport->smart_options)
1203 git_reports = set_git_option(transport->smart_options,
1204 name, value);
1206 if (transport->vtable->set_option)
1207 protocol_reports = transport->vtable->set_option(transport,
1208 name, value);
1210 /* If either report is 0, report 0 (success). */
1211 if (!git_reports || !protocol_reports)
1212 return 0;
1213 /* If either reports -1 (invalid value), report -1. */
1214 if ((git_reports == -1) || (protocol_reports == -1))
1215 return -1;
1216 /* Otherwise if both report unknown, report unknown. */
1217 return 1;
1220 void transport_set_verbosity(struct transport *transport, int verbosity,
1221 int force_progress)
1223 if (verbosity >= 1)
1224 transport->verbose = verbosity <= 3 ? verbosity : 3;
1225 if (verbosity < 0)
1226 transport->verbose = -1;
1229 * Rules used to determine whether to report progress (processing aborts
1230 * when a rule is satisfied):
1232 * . Report progress, if force_progress is 1 (ie. --progress).
1233 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1234 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1235 * . Report progress if isatty(2) is 1.
1237 if (force_progress >= 0)
1238 transport->progress = !!force_progress;
1239 else
1240 transport->progress = verbosity >= 0 && isatty(2);
1243 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
1245 int i;
1247 fprintf(stderr, _("The following submodule paths contain changes that can\n"
1248 "not be found on any remote:\n"));
1249 for (i = 0; i < needs_pushing->nr; i++)
1250 fprintf(stderr, " %s\n", needs_pushing->items[i].string);
1251 fprintf(stderr, _("\nPlease try\n\n"
1252 " git push --recurse-submodules=on-demand\n\n"
1253 "or cd to the path and use\n\n"
1254 " git push\n\n"
1255 "to push them to a remote.\n\n"));
1257 string_list_clear(needs_pushing, 0);
1259 die(_("Aborting."));
1262 static int run_pre_push_hook(struct transport *transport,
1263 struct ref *remote_refs)
1265 int ret = 0, x;
1266 struct ref *r;
1267 struct child_process proc = CHILD_PROCESS_INIT;
1268 struct strbuf buf;
1269 const char *hook_path = find_hook("pre-push");
1271 if (!hook_path)
1272 return 0;
1274 strvec_push(&proc.args, hook_path);
1275 strvec_push(&proc.args, transport->remote->name);
1276 strvec_push(&proc.args, transport->url);
1278 proc.in = -1;
1279 proc.trace2_hook_name = "pre-push";
1281 if (start_command(&proc)) {
1282 finish_command(&proc);
1283 return -1;
1286 sigchain_push(SIGPIPE, SIG_IGN);
1288 strbuf_init(&buf, 256);
1290 for (r = remote_refs; r; r = r->next) {
1291 if (!r->peer_ref) continue;
1292 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
1293 if (r->status == REF_STATUS_REJECT_STALE) continue;
1294 if (r->status == REF_STATUS_REJECT_REMOTE_UPDATED) continue;
1295 if (r->status == REF_STATUS_UPTODATE) continue;
1297 strbuf_reset(&buf);
1298 strbuf_addf( &buf, "%s %s %s %s\n",
1299 r->peer_ref->name, oid_to_hex(&r->new_oid),
1300 r->name, oid_to_hex(&r->old_oid));
1302 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
1303 /* We do not mind if a hook does not read all refs. */
1304 if (errno != EPIPE)
1305 ret = -1;
1306 break;
1310 strbuf_release(&buf);
1312 x = close(proc.in);
1313 if (!ret)
1314 ret = x;
1316 sigchain_pop(SIGPIPE);
1318 x = finish_command(&proc);
1319 if (!ret)
1320 ret = x;
1322 return ret;
1325 int transport_push(struct repository *r,
1326 struct transport *transport,
1327 struct refspec *rs, int flags,
1328 unsigned int *reject_reasons)
1330 struct ref *remote_refs = NULL;
1331 struct ref *local_refs = NULL;
1332 int match_flags = MATCH_REFS_NONE;
1333 int verbose = (transport->verbose > 0);
1334 int quiet = (transport->verbose < 0);
1335 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1336 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1337 int push_ret, err;
1338 int ret = -1;
1339 struct transport_ls_refs_options transport_options =
1340 TRANSPORT_LS_REFS_OPTIONS_INIT;
1342 *reject_reasons = 0;
1344 if (transport_color_config() < 0)
1345 goto done;
1347 if (!transport->vtable->push_refs)
1348 goto done;
1350 local_refs = get_local_heads();
1352 if (check_push_refs(local_refs, rs) < 0)
1353 goto done;
1355 refspec_ref_prefixes(rs, &transport_options.ref_prefixes);
1357 trace2_region_enter("transport_push", "get_refs_list", r);
1358 remote_refs = transport->vtable->get_refs_list(transport, 1,
1359 &transport_options);
1360 trace2_region_leave("transport_push", "get_refs_list", r);
1362 transport_ls_refs_options_release(&transport_options);
1364 if (flags & TRANSPORT_PUSH_ALL)
1365 match_flags |= MATCH_REFS_ALL;
1366 if (flags & TRANSPORT_PUSH_MIRROR)
1367 match_flags |= MATCH_REFS_MIRROR;
1368 if (flags & TRANSPORT_PUSH_PRUNE)
1369 match_flags |= MATCH_REFS_PRUNE;
1370 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1371 match_flags |= MATCH_REFS_FOLLOW_TAGS;
1373 if (match_push_refs(local_refs, &remote_refs, rs, match_flags))
1374 goto done;
1376 if (transport->smart_options &&
1377 transport->smart_options->cas &&
1378 !is_empty_cas(transport->smart_options->cas))
1379 apply_push_cas(transport->smart_options->cas,
1380 transport->remote, remote_refs);
1382 set_ref_status_for_push(remote_refs,
1383 flags & TRANSPORT_PUSH_MIRROR,
1384 flags & TRANSPORT_PUSH_FORCE);
1386 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1387 if (run_pre_push_hook(transport, remote_refs))
1388 goto done;
1390 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1391 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1392 !is_bare_repository()) {
1393 struct ref *ref = remote_refs;
1394 struct oid_array commits = OID_ARRAY_INIT;
1396 trace2_region_enter("transport_push", "push_submodules", r);
1397 for (; ref; ref = ref->next)
1398 if (!is_null_oid(&ref->new_oid))
1399 oid_array_append(&commits,
1400 &ref->new_oid);
1402 if (!push_unpushed_submodules(r,
1403 &commits,
1404 transport->remote,
1406 transport->push_options,
1407 pretend)) {
1408 oid_array_clear(&commits);
1409 trace2_region_leave("transport_push", "push_submodules", r);
1410 die(_("failed to push all needed submodules"));
1412 oid_array_clear(&commits);
1413 trace2_region_leave("transport_push", "push_submodules", r);
1416 if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1417 ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1418 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1419 !pretend)) && !is_bare_repository()) {
1420 struct ref *ref = remote_refs;
1421 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1422 struct oid_array commits = OID_ARRAY_INIT;
1424 trace2_region_enter("transport_push", "check_submodules", r);
1425 for (; ref; ref = ref->next)
1426 if (!is_null_oid(&ref->new_oid))
1427 oid_array_append(&commits,
1428 &ref->new_oid);
1430 if (find_unpushed_submodules(r,
1431 &commits,
1432 transport->remote->name,
1433 &needs_pushing)) {
1434 oid_array_clear(&commits);
1435 trace2_region_leave("transport_push", "check_submodules", r);
1436 die_with_unpushed_submodules(&needs_pushing);
1438 string_list_clear(&needs_pushing, 0);
1439 oid_array_clear(&commits);
1440 trace2_region_leave("transport_push", "check_submodules", r);
1443 if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY)) {
1444 trace2_region_enter("transport_push", "push_refs", r);
1445 push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1446 trace2_region_leave("transport_push", "push_refs", r);
1447 } else
1448 push_ret = 0;
1449 err = push_had_errors(remote_refs);
1450 ret = push_ret | err;
1452 if (!quiet || err)
1453 transport_print_push_status(transport->url, remote_refs,
1454 verbose | porcelain, porcelain,
1455 reject_reasons);
1457 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1458 set_upstreams(transport, remote_refs, pretend);
1460 if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1461 TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1462 struct ref *ref;
1463 for (ref = remote_refs; ref; ref = ref->next)
1464 transport_update_tracking_ref(transport->remote, ref, verbose);
1467 if (porcelain && !push_ret)
1468 puts("Done");
1469 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1470 fprintf(stderr, "Everything up-to-date\n");
1472 done:
1473 free_refs(local_refs);
1474 free_refs(remote_refs);
1475 return ret;
1478 const struct ref *transport_get_remote_refs(struct transport *transport,
1479 struct transport_ls_refs_options *transport_options)
1481 if (!transport->got_remote_refs) {
1482 transport->remote_refs =
1483 transport->vtable->get_refs_list(transport, 0,
1484 transport_options);
1485 transport->got_remote_refs = 1;
1488 return transport->remote_refs;
1491 void transport_ls_refs_options_release(struct transport_ls_refs_options *opts)
1493 strvec_clear(&opts->ref_prefixes);
1494 free((char *)opts->unborn_head_target);
1497 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1499 int rc;
1500 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1501 struct ref **heads = NULL;
1502 struct ref *rm;
1504 for (rm = refs; rm; rm = rm->next) {
1505 nr_refs++;
1506 if (rm->peer_ref &&
1507 !is_null_oid(&rm->old_oid) &&
1508 oideq(&rm->peer_ref->old_oid, &rm->old_oid))
1509 continue;
1510 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1511 heads[nr_heads++] = rm;
1514 if (!nr_heads) {
1516 * When deepening of a shallow repository is requested,
1517 * then local and remote refs are likely to still be equal.
1518 * Just feed them all to the fetch method in that case.
1519 * This condition shouldn't be met in a non-deepening fetch
1520 * (see builtin/fetch.c:quickfetch()).
1522 ALLOC_ARRAY(heads, nr_refs);
1523 for (rm = refs; rm; rm = rm->next)
1524 heads[nr_heads++] = rm;
1527 rc = transport->vtable->fetch_refs(transport, nr_heads, heads);
1529 free(heads);
1530 return rc;
1533 int transport_get_remote_bundle_uri(struct transport *transport)
1535 int value = 0;
1536 const struct transport_vtable *vtable = transport->vtable;
1538 /* Check config only once. */
1539 if (transport->got_remote_bundle_uri)
1540 return 0;
1541 transport->got_remote_bundle_uri = 1;
1544 * Don't request bundle-uri from the server unless configured to
1545 * do so by the transfer.bundleURI=true config option.
1547 if (git_config_get_bool("transfer.bundleuri", &value) || !value)
1548 return 0;
1550 if (!transport->bundles->baseURI)
1551 transport->bundles->baseURI = xstrdup(transport->url);
1553 if (!vtable->get_bundle_uri)
1554 return error(_("bundle-uri operation not supported by protocol"));
1556 if (vtable->get_bundle_uri(transport) < 0)
1557 return error(_("could not retrieve server-advertised bundle-uri list"));
1558 return 0;
1561 void transport_unlock_pack(struct transport *transport, unsigned int flags)
1563 int in_signal_handler = !!(flags & TRANSPORT_UNLOCK_PACK_IN_SIGNAL_HANDLER);
1564 int i;
1566 for (i = 0; i < transport->pack_lockfiles.nr; i++)
1567 if (in_signal_handler)
1568 unlink(transport->pack_lockfiles.items[i].string);
1569 else
1570 unlink_or_warn(transport->pack_lockfiles.items[i].string);
1571 if (!in_signal_handler)
1572 string_list_clear(&transport->pack_lockfiles, 0);
1575 int transport_connect(struct transport *transport, const char *name,
1576 const char *exec, int fd[2])
1578 if (transport->vtable->connect)
1579 return transport->vtable->connect(transport, name, exec, fd);
1580 else
1581 die(_("operation not supported by protocol"));
1584 int transport_disconnect(struct transport *transport)
1586 int ret = 0;
1587 if (transport->vtable->disconnect)
1588 ret = transport->vtable->disconnect(transport);
1589 if (transport->got_remote_refs)
1590 free_refs((void *)transport->remote_refs);
1591 clear_bundle_list(transport->bundles);
1592 free(transport->bundles);
1593 free(transport);
1594 return ret;
1598 * Strip username (and password) from a URL and return
1599 * it in a newly allocated string.
1601 char *transport_anonymize_url(const char *url)
1603 char *scheme_prefix, *anon_part;
1604 size_t anon_len, prefix_len = 0;
1606 anon_part = strchr(url, '@');
1607 if (url_is_local_not_ssh(url) || !anon_part)
1608 goto literal_copy;
1610 anon_len = strlen(++anon_part);
1611 scheme_prefix = strstr(url, "://");
1612 if (!scheme_prefix) {
1613 if (!strchr(anon_part, ':'))
1614 /* cannot be "me@there:/path/name" */
1615 goto literal_copy;
1616 } else {
1617 const char *cp;
1618 /* make sure scheme is reasonable */
1619 for (cp = url; cp < scheme_prefix; cp++) {
1620 switch (*cp) {
1621 /* RFC 1738 2.1 */
1622 case '+': case '.': case '-':
1623 break; /* ok */
1624 default:
1625 if (isalnum(*cp))
1626 break;
1627 /* it isn't */
1628 goto literal_copy;
1631 /* @ past the first slash does not count */
1632 cp = strchr(scheme_prefix + 3, '/');
1633 if (cp && cp < anon_part)
1634 goto literal_copy;
1635 prefix_len = scheme_prefix - url + 3;
1637 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1638 (int)anon_len, anon_part);
1639 literal_copy:
1640 return xstrdup(url);