treewide: be explicit about dependence on oid-array.h
[alt-git.git] / transport.c
blob82bf2496ba73374338ec8a0630545782fed81190
1 #include "cache.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-store.h"
30 #include "color.h"
31 #include "bundle-uri.h"
32 #include "wrapper.h"
34 static int transport_use_color = -1;
35 static char transport_colors[][COLOR_MAXLEN] = {
36 GIT_COLOR_RESET,
37 GIT_COLOR_RED /* REJECTED */
40 enum color_transport {
41 TRANSPORT_COLOR_RESET = 0,
42 TRANSPORT_COLOR_REJECTED = 1
45 static int transport_color_config(void)
47 const char *keys[] = {
48 "color.transport.reset",
49 "color.transport.rejected"
50 }, *key = "color.transport";
51 char *value;
52 int i;
53 static int initialized;
55 if (initialized)
56 return 0;
57 initialized = 1;
59 if (!git_config_get_string(key, &value))
60 transport_use_color = git_config_colorbool(key, value);
62 if (!want_color_stderr(transport_use_color))
63 return 0;
65 for (i = 0; i < ARRAY_SIZE(keys); i++)
66 if (!git_config_get_string(keys[i], &value)) {
67 if (!value)
68 return config_error_nonbool(keys[i]);
69 if (color_parse(value, transport_colors[i]) < 0)
70 return -1;
73 return 0;
76 static const char *transport_get_color(enum color_transport ix)
78 if (want_color_stderr(transport_use_color))
79 return transport_colors[ix];
80 return "";
83 static void set_upstreams(struct transport *transport, struct ref *refs,
84 int pretend)
86 struct ref *ref;
87 for (ref = refs; ref; ref = ref->next) {
88 const char *localname;
89 const char *tmp;
90 const char *remotename;
91 int flag = 0;
93 * Check suitability for tracking. Must be successful /
94 * already up-to-date ref create/modify (not delete).
96 if (ref->status != REF_STATUS_OK &&
97 ref->status != REF_STATUS_UPTODATE)
98 continue;
99 if (!ref->peer_ref)
100 continue;
101 if (is_null_oid(&ref->new_oid))
102 continue;
104 /* Follow symbolic refs (mainly for HEAD). */
105 localname = ref->peer_ref->name;
106 remotename = ref->name;
107 tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING,
108 NULL, &flag);
109 if (tmp && flag & REF_ISSYMREF &&
110 starts_with(tmp, "refs/heads/"))
111 localname = tmp;
113 /* Both source and destination must be local branches. */
114 if (!localname || !starts_with(localname, "refs/heads/"))
115 continue;
116 if (!remotename || !starts_with(remotename, "refs/heads/"))
117 continue;
119 if (!pretend) {
120 int flag = transport->verbose < 0 ? 0 : BRANCH_CONFIG_VERBOSE;
121 install_branch_config(flag, localname + 11,
122 transport->remote->name, remotename);
123 } else if (transport->verbose >= 0)
124 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
125 localname + 11, remotename + 11,
126 transport->remote->name);
130 struct bundle_transport_data {
131 int fd;
132 struct bundle_header header;
133 unsigned get_refs_from_bundle_called : 1;
136 static void get_refs_from_bundle_inner(struct transport *transport)
138 struct bundle_transport_data *data = transport->data;
140 data->get_refs_from_bundle_called = 1;
142 if (data->fd > 0)
143 close(data->fd);
144 data->fd = read_bundle_header(transport->url, &data->header);
145 if (data->fd < 0)
146 die(_("could not read bundle '%s'"), transport->url);
148 transport->hash_algo = data->header.hash_algo;
151 static struct ref *get_refs_from_bundle(struct transport *transport,
152 int for_push,
153 struct transport_ls_refs_options *transport_options UNUSED)
155 struct bundle_transport_data *data = transport->data;
156 struct ref *result = NULL;
157 int i;
159 if (for_push)
160 return NULL;
162 get_refs_from_bundle_inner(transport);
164 for (i = 0; i < data->header.references.nr; i++) {
165 struct string_list_item *e = data->header.references.items + i;
166 const char *name = e->string;
167 struct ref *ref = alloc_ref(name);
168 struct object_id *oid = e->util;
169 oidcpy(&ref->old_oid, oid);
170 ref->next = result;
171 result = ref;
173 return result;
176 static int fetch_refs_from_bundle(struct transport *transport,
177 int nr_heads, struct ref **to_fetch)
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 ? data->options.receivepack :
287 data->options.uploadpack,
288 flags);
290 return 0;
293 static void die_if_server_options(struct transport *transport)
295 if (!transport->server_options || !transport->server_options->nr)
296 return;
297 advise(_("see protocol.version in 'git help config' for more details"));
298 die(_("server options require protocol version 2 or later"));
302 * Obtains the protocol version from the transport and writes it to
303 * transport->data->version, first connecting if not already connected.
305 * If the protocol version is one that allows skipping the listing of remote
306 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
307 * this function returns NULL. Otherwise, this function returns the list of
308 * remote refs.
310 static struct ref *handshake(struct transport *transport, int for_push,
311 struct transport_ls_refs_options *options,
312 int must_list_refs)
314 struct git_transport_data *data = transport->data;
315 struct ref *refs = NULL;
316 struct packet_reader reader;
317 int sid_len;
318 const char *server_sid;
320 connect_setup(transport, for_push);
322 packet_reader_init(&reader, data->fd[0], NULL, 0,
323 PACKET_READ_CHOMP_NEWLINE |
324 PACKET_READ_GENTLE_ON_EOF |
325 PACKET_READ_DIE_ON_ERR_PACKET);
327 data->version = discover_version(&reader);
328 switch (data->version) {
329 case protocol_v2:
330 if (server_feature_v2("session-id", &server_sid))
331 trace2_data_string("transfer", NULL, "server-sid", server_sid);
332 if (must_list_refs)
333 get_remote_refs(data->fd[1], &reader, &refs, for_push,
334 options,
335 transport->server_options,
336 transport->stateless_rpc);
337 break;
338 case protocol_v1:
339 case protocol_v0:
340 die_if_server_options(transport);
341 get_remote_heads(&reader, &refs,
342 for_push ? REF_NORMAL : 0,
343 &data->extra_have,
344 &data->shallow);
345 server_sid = server_feature_value("session-id", &sid_len);
346 if (server_sid) {
347 char *sid = xstrndup(server_sid, sid_len);
348 trace2_data_string("transfer", NULL, "server-sid", sid);
349 free(sid);
351 break;
352 case protocol_unknown_version:
353 BUG("unknown protocol version");
355 data->finished_handshake = 1;
356 transport->hash_algo = reader.hash_algo;
358 if (reader.line_peeked)
359 BUG("buffer must be empty at the end of handshake()");
361 return refs;
364 static struct ref *get_refs_via_connect(struct transport *transport, int for_push,
365 struct transport_ls_refs_options *options)
367 return handshake(transport, for_push, options, 1);
370 static int get_bundle_uri(struct transport *transport)
372 struct git_transport_data *data = transport->data;
373 struct packet_reader reader;
374 int stateless_rpc = transport->stateless_rpc;
376 if (!transport->bundles) {
377 CALLOC_ARRAY(transport->bundles, 1);
378 init_bundle_list(transport->bundles);
381 if (!data->finished_handshake) {
382 struct ref *refs = handshake(transport, 0, NULL, 0);
384 if (refs)
385 free_refs(refs);
389 * "Support" protocol v0 and v2 without bundle-uri support by
390 * silently degrading to a NOOP.
392 if (!server_supports_v2("bundle-uri"))
393 return 0;
395 packet_reader_init(&reader, data->fd[0], NULL, 0,
396 PACKET_READ_CHOMP_NEWLINE |
397 PACKET_READ_GENTLE_ON_EOF);
399 return get_remote_bundle_uri(data->fd[1], &reader,
400 transport->bundles, stateless_rpc);
403 static int fetch_refs_via_pack(struct transport *transport,
404 int nr_heads, struct ref **to_fetch)
406 int ret = 0;
407 struct git_transport_data *data = transport->data;
408 struct ref *refs = NULL;
409 struct fetch_pack_args args;
410 struct ref *refs_tmp = NULL;
412 memset(&args, 0, sizeof(args));
413 args.uploadpack = data->options.uploadpack;
414 args.keep_pack = data->options.keep;
415 args.lock_pack = 1;
416 args.use_thin_pack = data->options.thin;
417 args.include_tag = data->options.followtags;
418 args.verbose = (transport->verbose > 1);
419 args.quiet = (transport->verbose < 0);
420 args.no_progress = !transport->progress;
421 args.depth = data->options.depth;
422 args.deepen_since = data->options.deepen_since;
423 args.deepen_not = data->options.deepen_not;
424 args.deepen_relative = data->options.deepen_relative;
425 args.check_self_contained_and_connected =
426 data->options.check_self_contained_and_connected;
427 args.cloning = transport->cloning;
428 args.update_shallow = data->options.update_shallow;
429 args.from_promisor = data->options.from_promisor;
430 list_objects_filter_copy(&args.filter_options,
431 &data->options.filter_options);
432 args.refetch = data->options.refetch;
433 args.stateless_rpc = transport->stateless_rpc;
434 args.server_options = transport->server_options;
435 args.negotiation_tips = data->options.negotiation_tips;
436 args.reject_shallow_remote = transport->smart_options->reject_shallow;
438 if (!data->finished_handshake) {
439 int i;
440 int must_list_refs = 0;
441 for (i = 0; i < nr_heads; i++) {
442 if (!to_fetch[i]->exact_oid) {
443 must_list_refs = 1;
444 break;
447 refs_tmp = handshake(transport, 0, NULL, must_list_refs);
450 if (data->version == protocol_unknown_version)
451 BUG("unknown protocol version");
452 else if (data->version <= protocol_v1)
453 die_if_server_options(transport);
455 if (data->options.acked_commits) {
456 if (data->version < protocol_v2) {
457 warning(_("--negotiate-only requires protocol v2"));
458 ret = -1;
459 } else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
460 warning(_("server does not support wait-for-done"));
461 ret = -1;
462 } else {
463 negotiate_using_fetch(data->options.negotiation_tips,
464 transport->server_options,
465 transport->stateless_rpc,
466 data->fd,
467 data->options.acked_commits);
468 ret = 0;
470 goto cleanup;
473 refs = fetch_pack(&args, data->fd,
474 refs_tmp ? refs_tmp : transport->remote_refs,
475 to_fetch, nr_heads, &data->shallow,
476 &transport->pack_lockfiles, data->version);
478 data->finished_handshake = 0;
479 data->options.self_contained_and_connected =
480 args.self_contained_and_connected;
481 data->options.connectivity_checked = args.connectivity_checked;
483 if (!refs)
484 ret = -1;
485 if (report_unmatched_refs(to_fetch, nr_heads))
486 ret = -1;
488 cleanup:
489 close(data->fd[0]);
490 if (data->fd[1] >= 0)
491 close(data->fd[1]);
492 if (finish_connect(data->conn))
493 ret = -1;
494 data->conn = NULL;
496 free_refs(refs_tmp);
497 free_refs(refs);
498 list_objects_filter_release(&args.filter_options);
499 return ret;
502 static int push_had_errors(struct ref *ref)
504 for (; ref; ref = ref->next) {
505 switch (ref->status) {
506 case REF_STATUS_NONE:
507 case REF_STATUS_UPTODATE:
508 case REF_STATUS_OK:
509 break;
510 default:
511 return 1;
514 return 0;
517 int transport_refs_pushed(struct ref *ref)
519 for (; ref; ref = ref->next) {
520 switch(ref->status) {
521 case REF_STATUS_NONE:
522 case REF_STATUS_UPTODATE:
523 break;
524 default:
525 return 1;
528 return 0;
531 static void update_one_tracking_ref(struct remote *remote, char *refname,
532 struct object_id *new_oid, int deletion,
533 int verbose)
535 struct refspec_item rs;
537 memset(&rs, 0, sizeof(rs));
538 rs.src = refname;
539 rs.dst = NULL;
541 if (!remote_find_tracking(remote, &rs)) {
542 if (verbose)
543 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
544 if (deletion)
545 delete_ref(NULL, rs.dst, NULL, 0);
546 else
547 update_ref("update by push", rs.dst, new_oid,
548 NULL, 0, 0);
549 free(rs.dst);
553 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
555 char *refname;
556 struct object_id *new_oid;
557 struct ref_push_report *report;
559 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
560 return;
562 report = ref->report;
563 if (!report)
564 update_one_tracking_ref(remote, ref->name, &ref->new_oid,
565 ref->deletion, verbose);
566 else
567 for (; report; report = report->next) {
568 refname = report->ref_name ? (char *)report->ref_name : ref->name;
569 new_oid = report->new_oid ? report->new_oid : &ref->new_oid;
570 update_one_tracking_ref(remote, refname, new_oid,
571 is_null_oid(new_oid), verbose);
575 static void print_ref_status(char flag, const char *summary,
576 struct ref *to, struct ref *from, const char *msg,
577 struct ref_push_report *report,
578 int porcelain, int summary_width)
580 const char *to_name;
582 if (report && report->ref_name)
583 to_name = report->ref_name;
584 else
585 to_name = to->name;
587 if (porcelain) {
588 if (from)
589 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to_name);
590 else
591 fprintf(stdout, "%c\t:%s\t", flag, to_name);
592 if (msg)
593 fprintf(stdout, "%s (%s)\n", summary, msg);
594 else
595 fprintf(stdout, "%s\n", summary);
596 } else {
597 const char *red = "", *reset = "";
598 if (push_had_errors(to)) {
599 red = transport_get_color(TRANSPORT_COLOR_REJECTED);
600 reset = transport_get_color(TRANSPORT_COLOR_RESET);
602 fprintf(stderr, " %s%c %-*s%s ", red, flag, summary_width,
603 summary, reset);
604 if (from)
605 fprintf(stderr, "%s -> %s",
606 prettify_refname(from->name),
607 prettify_refname(to_name));
608 else
609 fputs(prettify_refname(to_name), stderr);
610 if (msg) {
611 fputs(" (", stderr);
612 fputs(msg, stderr);
613 fputc(')', stderr);
615 fputc('\n', stderr);
619 static void print_ok_ref_status(struct ref *ref,
620 struct ref_push_report *report,
621 int porcelain, int summary_width)
623 struct object_id *old_oid;
624 struct object_id *new_oid;
625 const char *ref_name;
626 int forced_update;
628 if (report && report->old_oid)
629 old_oid = report->old_oid;
630 else
631 old_oid = &ref->old_oid;
632 if (report && report->new_oid)
633 new_oid = report->new_oid;
634 else
635 new_oid = &ref->new_oid;
636 if (report && report->forced_update)
637 forced_update = report->forced_update;
638 else
639 forced_update = ref->forced_update;
640 if (report && report->ref_name)
641 ref_name = report->ref_name;
642 else
643 ref_name = ref->name;
645 if (ref->deletion)
646 print_ref_status('-', "[deleted]", ref, NULL, NULL,
647 report, porcelain, summary_width);
648 else if (is_null_oid(old_oid))
649 print_ref_status('*',
650 (starts_with(ref_name, "refs/tags/")
651 ? "[new tag]"
652 : (starts_with(ref_name, "refs/heads/")
653 ? "[new branch]"
654 : "[new reference]")),
655 ref, ref->peer_ref, NULL,
656 report, porcelain, summary_width);
657 else {
658 struct strbuf quickref = STRBUF_INIT;
659 char type;
660 const char *msg;
662 strbuf_add_unique_abbrev(&quickref, old_oid,
663 DEFAULT_ABBREV);
664 if (forced_update) {
665 strbuf_addstr(&quickref, "...");
666 type = '+';
667 msg = "forced update";
668 } else {
669 strbuf_addstr(&quickref, "..");
670 type = ' ';
671 msg = NULL;
673 strbuf_add_unique_abbrev(&quickref, new_oid,
674 DEFAULT_ABBREV);
676 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
677 report, porcelain, summary_width);
678 strbuf_release(&quickref);
682 static int print_one_push_report(struct ref *ref, const char *dest, int count,
683 struct ref_push_report *report,
684 int porcelain, int summary_width)
686 if (!count) {
687 char *url = transport_anonymize_url(dest);
688 fprintf(porcelain ? stdout : stderr, "To %s\n", url);
689 free(url);
692 switch(ref->status) {
693 case REF_STATUS_NONE:
694 print_ref_status('X', "[no match]", ref, NULL, NULL,
695 report, porcelain, summary_width);
696 break;
697 case REF_STATUS_REJECT_NODELETE:
698 print_ref_status('!', "[rejected]", ref, NULL,
699 "remote does not support deleting refs",
700 report, porcelain, summary_width);
701 break;
702 case REF_STATUS_UPTODATE:
703 print_ref_status('=', "[up to date]", ref,
704 ref->peer_ref, NULL,
705 report, porcelain, summary_width);
706 break;
707 case REF_STATUS_REJECT_NONFASTFORWARD:
708 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
709 "non-fast-forward",
710 report, porcelain, summary_width);
711 break;
712 case REF_STATUS_REJECT_ALREADY_EXISTS:
713 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
714 "already exists",
715 report, porcelain, summary_width);
716 break;
717 case REF_STATUS_REJECT_FETCH_FIRST:
718 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
719 "fetch first",
720 report, porcelain, summary_width);
721 break;
722 case REF_STATUS_REJECT_NEEDS_FORCE:
723 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
724 "needs force",
725 report, porcelain, summary_width);
726 break;
727 case REF_STATUS_REJECT_STALE:
728 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
729 "stale info",
730 report, porcelain, summary_width);
731 break;
732 case REF_STATUS_REJECT_REMOTE_UPDATED:
733 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
734 "remote ref updated since checkout",
735 report, porcelain, summary_width);
736 break;
737 case REF_STATUS_REJECT_SHALLOW:
738 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
739 "new shallow roots not allowed",
740 report, porcelain, summary_width);
741 break;
742 case REF_STATUS_REMOTE_REJECT:
743 print_ref_status('!', "[remote rejected]", ref,
744 ref->deletion ? NULL : ref->peer_ref,
745 ref->remote_status,
746 report, porcelain, summary_width);
747 break;
748 case REF_STATUS_EXPECTING_REPORT:
749 print_ref_status('!', "[remote failure]", ref,
750 ref->deletion ? NULL : ref->peer_ref,
751 "remote failed to report status",
752 report, porcelain, summary_width);
753 break;
754 case REF_STATUS_ATOMIC_PUSH_FAILED:
755 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
756 "atomic push failed",
757 report, porcelain, summary_width);
758 break;
759 case REF_STATUS_OK:
760 print_ok_ref_status(ref, report, porcelain, summary_width);
761 break;
764 return 1;
767 static int print_one_push_status(struct ref *ref, const char *dest, int count,
768 int porcelain, int summary_width)
770 struct ref_push_report *report;
771 int n = 0;
773 if (!ref->report)
774 return print_one_push_report(ref, dest, count,
775 NULL, porcelain, summary_width);
777 for (report = ref->report; report; report = report->next)
778 print_one_push_report(ref, dest, count + n++,
779 report, porcelain, summary_width);
780 return n;
783 static int measure_abbrev(const struct object_id *oid, int sofar)
785 char hex[GIT_MAX_HEXSZ + 1];
786 int w = repo_find_unique_abbrev_r(the_repository, hex, oid,
787 DEFAULT_ABBREV);
789 return (w < sofar) ? sofar : w;
792 int transport_summary_width(const struct ref *refs)
794 int maxw = -1;
796 for (; refs; refs = refs->next) {
797 maxw = measure_abbrev(&refs->old_oid, maxw);
798 maxw = measure_abbrev(&refs->new_oid, maxw);
800 if (maxw < 0)
801 maxw = FALLBACK_DEFAULT_ABBREV;
802 return (2 * maxw + 3);
805 void transport_print_push_status(const char *dest, struct ref *refs,
806 int verbose, int porcelain, unsigned int *reject_reasons)
808 struct ref *ref;
809 int n = 0;
810 char *head;
811 int summary_width = transport_summary_width(refs);
813 if (transport_color_config() < 0)
814 warning(_("could not parse transport.color.* config"));
816 head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
818 if (verbose) {
819 for (ref = refs; ref; ref = ref->next)
820 if (ref->status == REF_STATUS_UPTODATE)
821 n += print_one_push_status(ref, dest, n,
822 porcelain, summary_width);
825 for (ref = refs; ref; ref = ref->next)
826 if (ref->status == REF_STATUS_OK)
827 n += print_one_push_status(ref, dest, n,
828 porcelain, summary_width);
830 *reject_reasons = 0;
831 for (ref = refs; ref; ref = ref->next) {
832 if (ref->status != REF_STATUS_NONE &&
833 ref->status != REF_STATUS_UPTODATE &&
834 ref->status != REF_STATUS_OK)
835 n += print_one_push_status(ref, dest, n,
836 porcelain, summary_width);
837 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
838 if (head != NULL && !strcmp(head, ref->name))
839 *reject_reasons |= REJECT_NON_FF_HEAD;
840 else
841 *reject_reasons |= REJECT_NON_FF_OTHER;
842 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
843 *reject_reasons |= REJECT_ALREADY_EXISTS;
844 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
845 *reject_reasons |= REJECT_FETCH_FIRST;
846 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
847 *reject_reasons |= REJECT_NEEDS_FORCE;
848 } else if (ref->status == REF_STATUS_REJECT_REMOTE_UPDATED) {
849 *reject_reasons |= REJECT_REF_NEEDS_UPDATE;
852 free(head);
855 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
857 struct git_transport_data *data = transport->data;
858 struct send_pack_args args;
859 int ret = 0;
861 if (transport_color_config() < 0)
862 return -1;
864 if (!data->finished_handshake)
865 get_refs_via_connect(transport, 1, NULL);
867 memset(&args, 0, sizeof(args));
868 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
869 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
870 args.use_thin_pack = data->options.thin;
871 args.verbose = (transport->verbose > 0);
872 args.quiet = (transport->verbose < 0);
873 args.progress = transport->progress;
874 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
875 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
876 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
877 args.push_options = transport->push_options;
878 args.url = transport->url;
880 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
881 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
882 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
883 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
884 else
885 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
887 switch (data->version) {
888 case protocol_v2:
889 die(_("support for protocol v2 not implemented yet"));
890 break;
891 case protocol_v1:
892 case protocol_v0:
893 ret = send_pack(&args, data->fd, data->conn, remote_refs,
894 &data->extra_have);
895 break;
896 case protocol_unknown_version:
897 BUG("unknown protocol version");
900 close(data->fd[1]);
901 close(data->fd[0]);
903 * Atomic push may abort the connection early and close the pipe,
904 * which may cause an error for `finish_connect()`. Ignore this error
905 * for atomic git-push.
907 if (ret || args.atomic)
908 finish_connect(data->conn);
909 else
910 ret = finish_connect(data->conn);
911 data->conn = NULL;
912 data->finished_handshake = 0;
914 return ret;
917 static int connect_git(struct transport *transport, const char *name,
918 const char *executable, int fd[2])
920 struct git_transport_data *data = transport->data;
921 data->conn = git_connect(data->fd, transport->url,
922 executable, 0);
923 fd[0] = data->fd[0];
924 fd[1] = data->fd[1];
925 return 0;
928 static int disconnect_git(struct transport *transport)
930 struct git_transport_data *data = transport->data;
931 if (data->conn) {
932 if (data->finished_handshake && !transport->stateless_rpc)
933 packet_flush(data->fd[1]);
934 close(data->fd[0]);
935 if (data->fd[1] >= 0)
936 close(data->fd[1]);
937 finish_connect(data->conn);
940 list_objects_filter_release(&data->options.filter_options);
941 free(data);
942 return 0;
945 static struct transport_vtable taken_over_vtable = {
946 .get_refs_list = get_refs_via_connect,
947 .get_bundle_uri = get_bundle_uri,
948 .fetch_refs = fetch_refs_via_pack,
949 .push_refs = git_transport_push,
950 .disconnect = disconnect_git
953 void transport_take_over(struct transport *transport,
954 struct child_process *child)
956 struct git_transport_data *data;
958 if (!transport->smart_options)
959 BUG("taking over transport requires non-NULL "
960 "smart_options field.");
962 CALLOC_ARRAY(data, 1);
963 data->options = *transport->smart_options;
964 data->conn = child;
965 data->fd[0] = data->conn->out;
966 data->fd[1] = data->conn->in;
967 data->finished_handshake = 0;
968 transport->data = data;
970 transport->vtable = &taken_over_vtable;
971 transport->smart_options = &(data->options);
973 transport->cannot_reuse = 1;
976 static int is_file(const char *url)
978 struct stat buf;
979 if (stat(url, &buf))
980 return 0;
981 return S_ISREG(buf.st_mode);
984 static int external_specification_len(const char *url)
986 return strchr(url, ':') - url;
989 static const struct string_list *protocol_allow_list(void)
991 static int enabled = -1;
992 static struct string_list allowed = STRING_LIST_INIT_DUP;
994 if (enabled < 0) {
995 const char *v = getenv("GIT_ALLOW_PROTOCOL");
996 if (v) {
997 string_list_split(&allowed, v, ':', -1);
998 string_list_sort(&allowed);
999 enabled = 1;
1000 } else {
1001 enabled = 0;
1005 return enabled ? &allowed : NULL;
1008 enum protocol_allow_config {
1009 PROTOCOL_ALLOW_NEVER = 0,
1010 PROTOCOL_ALLOW_USER_ONLY,
1011 PROTOCOL_ALLOW_ALWAYS
1014 static enum protocol_allow_config parse_protocol_config(const char *key,
1015 const char *value)
1017 if (!strcasecmp(value, "always"))
1018 return PROTOCOL_ALLOW_ALWAYS;
1019 else if (!strcasecmp(value, "never"))
1020 return PROTOCOL_ALLOW_NEVER;
1021 else if (!strcasecmp(value, "user"))
1022 return PROTOCOL_ALLOW_USER_ONLY;
1024 die(_("unknown value for config '%s': %s"), key, value);
1027 static enum protocol_allow_config get_protocol_config(const char *type)
1029 char *key = xstrfmt("protocol.%s.allow", type);
1030 char *value;
1032 /* first check the per-protocol config */
1033 if (!git_config_get_string(key, &value)) {
1034 enum protocol_allow_config ret =
1035 parse_protocol_config(key, value);
1036 free(key);
1037 free(value);
1038 return ret;
1040 free(key);
1042 /* if defined, fallback to user-defined default for unknown protocols */
1043 if (!git_config_get_string("protocol.allow", &value)) {
1044 enum protocol_allow_config ret =
1045 parse_protocol_config("protocol.allow", value);
1046 free(value);
1047 return ret;
1050 /* fallback to built-in defaults */
1051 /* known safe */
1052 if (!strcmp(type, "http") ||
1053 !strcmp(type, "https") ||
1054 !strcmp(type, "git") ||
1055 !strcmp(type, "ssh"))
1056 return PROTOCOL_ALLOW_ALWAYS;
1058 /* known scary; err on the side of caution */
1059 if (!strcmp(type, "ext"))
1060 return PROTOCOL_ALLOW_NEVER;
1062 /* unknown; by default let them be used only directly by the user */
1063 return PROTOCOL_ALLOW_USER_ONLY;
1066 int is_transport_allowed(const char *type, int from_user)
1068 const struct string_list *allow_list = protocol_allow_list();
1069 if (allow_list)
1070 return string_list_has_string(allow_list, type);
1072 switch (get_protocol_config(type)) {
1073 case PROTOCOL_ALLOW_ALWAYS:
1074 return 1;
1075 case PROTOCOL_ALLOW_NEVER:
1076 return 0;
1077 case PROTOCOL_ALLOW_USER_ONLY:
1078 if (from_user < 0)
1079 from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1080 return from_user;
1083 BUG("invalid protocol_allow_config type");
1086 void transport_check_allowed(const char *type)
1088 if (!is_transport_allowed(type, -1))
1089 die(_("transport '%s' not allowed"), type);
1092 static struct transport_vtable bundle_vtable = {
1093 .get_refs_list = get_refs_from_bundle,
1094 .fetch_refs = fetch_refs_from_bundle,
1095 .disconnect = close_bundle
1098 static struct transport_vtable builtin_smart_vtable = {
1099 .get_refs_list = get_refs_via_connect,
1100 .get_bundle_uri = get_bundle_uri,
1101 .fetch_refs = fetch_refs_via_pack,
1102 .push_refs = git_transport_push,
1103 .connect = connect_git,
1104 .disconnect = disconnect_git
1107 struct transport *transport_get(struct remote *remote, const char *url)
1109 const char *helper;
1110 struct transport *ret = xcalloc(1, sizeof(*ret));
1112 ret->progress = isatty(2);
1113 string_list_init_dup(&ret->pack_lockfiles);
1115 CALLOC_ARRAY(ret->bundles, 1);
1116 init_bundle_list(ret->bundles);
1118 if (!remote)
1119 BUG("No remote provided to transport_get()");
1121 ret->got_remote_refs = 0;
1122 ret->remote = remote;
1123 helper = remote->foreign_vcs;
1125 if (!url && remote->url)
1126 url = remote->url[0];
1127 ret->url = url;
1129 /* maybe it is a foreign URL? */
1130 if (url) {
1131 const char *p = url;
1133 while (is_urlschemechar(p == url, *p))
1134 p++;
1135 if (starts_with(p, "::"))
1136 helper = xstrndup(url, p - url);
1139 if (helper) {
1140 transport_helper_init(ret, helper);
1141 } else if (starts_with(url, "rsync:")) {
1142 die(_("git-over-rsync is no longer supported"));
1143 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
1144 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
1145 bundle_header_init(&data->header);
1146 transport_check_allowed("file");
1147 ret->data = data;
1148 ret->vtable = &bundle_vtable;
1149 ret->smart_options = NULL;
1150 } else if (!is_url(url)
1151 || starts_with(url, "file://")
1152 || starts_with(url, "git://")
1153 || starts_with(url, "ssh://")
1154 || starts_with(url, "git+ssh://") /* deprecated - do not use */
1155 || starts_with(url, "ssh+git://") /* deprecated - do not use */
1158 * These are builtin smart transports; "allowed" transports
1159 * will be checked individually in git_connect.
1161 struct git_transport_data *data = xcalloc(1, sizeof(*data));
1162 list_objects_filter_init(&data->options.filter_options);
1163 ret->data = data;
1164 ret->vtable = &builtin_smart_vtable;
1165 ret->smart_options = &(data->options);
1167 data->conn = NULL;
1168 data->finished_handshake = 0;
1169 } else {
1170 /* Unknown protocol in URL. Pass to external handler. */
1171 int len = external_specification_len(url);
1172 char *handler = xmemdupz(url, len);
1173 transport_helper_init(ret, handler);
1176 if (ret->smart_options) {
1177 ret->smart_options->thin = 1;
1178 ret->smart_options->uploadpack = "git-upload-pack";
1179 if (remote->uploadpack)
1180 ret->smart_options->uploadpack = remote->uploadpack;
1181 ret->smart_options->receivepack = "git-receive-pack";
1182 if (remote->receivepack)
1183 ret->smart_options->receivepack = remote->receivepack;
1186 ret->hash_algo = &hash_algos[GIT_HASH_SHA1];
1188 return ret;
1191 const struct git_hash_algo *transport_get_hash_algo(struct transport *transport)
1193 return transport->hash_algo;
1196 int transport_set_option(struct transport *transport,
1197 const char *name, const char *value)
1199 int git_reports = 1, protocol_reports = 1;
1201 if (transport->smart_options)
1202 git_reports = set_git_option(transport->smart_options,
1203 name, value);
1205 if (transport->vtable->set_option)
1206 protocol_reports = transport->vtable->set_option(transport,
1207 name, value);
1209 /* If either report is 0, report 0 (success). */
1210 if (!git_reports || !protocol_reports)
1211 return 0;
1212 /* If either reports -1 (invalid value), report -1. */
1213 if ((git_reports == -1) || (protocol_reports == -1))
1214 return -1;
1215 /* Otherwise if both report unknown, report unknown. */
1216 return 1;
1219 void transport_set_verbosity(struct transport *transport, int verbosity,
1220 int force_progress)
1222 if (verbosity >= 1)
1223 transport->verbose = verbosity <= 3 ? verbosity : 3;
1224 if (verbosity < 0)
1225 transport->verbose = -1;
1228 * Rules used to determine whether to report progress (processing aborts
1229 * when a rule is satisfied):
1231 * . Report progress, if force_progress is 1 (ie. --progress).
1232 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1233 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1234 * . Report progress if isatty(2) is 1.
1236 if (force_progress >= 0)
1237 transport->progress = !!force_progress;
1238 else
1239 transport->progress = verbosity >= 0 && isatty(2);
1242 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
1244 int i;
1246 fprintf(stderr, _("The following submodule paths contain changes that can\n"
1247 "not be found on any remote:\n"));
1248 for (i = 0; i < needs_pushing->nr; i++)
1249 fprintf(stderr, " %s\n", needs_pushing->items[i].string);
1250 fprintf(stderr, _("\nPlease try\n\n"
1251 " git push --recurse-submodules=on-demand\n\n"
1252 "or cd to the path and use\n\n"
1253 " git push\n\n"
1254 "to push them to a remote.\n\n"));
1256 string_list_clear(needs_pushing, 0);
1258 die(_("Aborting."));
1261 static int run_pre_push_hook(struct transport *transport,
1262 struct ref *remote_refs)
1264 int ret = 0, x;
1265 struct ref *r;
1266 struct child_process proc = CHILD_PROCESS_INIT;
1267 struct strbuf buf;
1268 const char *hook_path = find_hook("pre-push");
1270 if (!hook_path)
1271 return 0;
1273 strvec_push(&proc.args, hook_path);
1274 strvec_push(&proc.args, transport->remote->name);
1275 strvec_push(&proc.args, transport->url);
1277 proc.in = -1;
1278 proc.trace2_hook_name = "pre-push";
1280 if (start_command(&proc)) {
1281 finish_command(&proc);
1282 return -1;
1285 sigchain_push(SIGPIPE, SIG_IGN);
1287 strbuf_init(&buf, 256);
1289 for (r = remote_refs; r; r = r->next) {
1290 if (!r->peer_ref) continue;
1291 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
1292 if (r->status == REF_STATUS_REJECT_STALE) continue;
1293 if (r->status == REF_STATUS_REJECT_REMOTE_UPDATED) continue;
1294 if (r->status == REF_STATUS_UPTODATE) continue;
1296 strbuf_reset(&buf);
1297 strbuf_addf( &buf, "%s %s %s %s\n",
1298 r->peer_ref->name, oid_to_hex(&r->new_oid),
1299 r->name, oid_to_hex(&r->old_oid));
1301 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
1302 /* We do not mind if a hook does not read all refs. */
1303 if (errno != EPIPE)
1304 ret = -1;
1305 break;
1309 strbuf_release(&buf);
1311 x = close(proc.in);
1312 if (!ret)
1313 ret = x;
1315 sigchain_pop(SIGPIPE);
1317 x = finish_command(&proc);
1318 if (!ret)
1319 ret = x;
1321 return ret;
1324 int transport_push(struct repository *r,
1325 struct transport *transport,
1326 struct refspec *rs, int flags,
1327 unsigned int *reject_reasons)
1329 struct ref *remote_refs = NULL;
1330 struct ref *local_refs = NULL;
1331 int match_flags = MATCH_REFS_NONE;
1332 int verbose = (transport->verbose > 0);
1333 int quiet = (transport->verbose < 0);
1334 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1335 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1336 int push_ret, err;
1337 int ret = -1;
1338 struct transport_ls_refs_options transport_options =
1339 TRANSPORT_LS_REFS_OPTIONS_INIT;
1341 *reject_reasons = 0;
1343 if (transport_color_config() < 0)
1344 goto done;
1346 if (!transport->vtable->push_refs)
1347 goto done;
1349 local_refs = get_local_heads();
1351 if (check_push_refs(local_refs, rs) < 0)
1352 goto done;
1354 refspec_ref_prefixes(rs, &transport_options.ref_prefixes);
1356 trace2_region_enter("transport_push", "get_refs_list", r);
1357 remote_refs = transport->vtable->get_refs_list(transport, 1,
1358 &transport_options);
1359 trace2_region_leave("transport_push", "get_refs_list", r);
1361 transport_ls_refs_options_release(&transport_options);
1363 if (flags & TRANSPORT_PUSH_ALL)
1364 match_flags |= MATCH_REFS_ALL;
1365 if (flags & TRANSPORT_PUSH_MIRROR)
1366 match_flags |= MATCH_REFS_MIRROR;
1367 if (flags & TRANSPORT_PUSH_PRUNE)
1368 match_flags |= MATCH_REFS_PRUNE;
1369 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1370 match_flags |= MATCH_REFS_FOLLOW_TAGS;
1372 if (match_push_refs(local_refs, &remote_refs, rs, match_flags))
1373 goto done;
1375 if (transport->smart_options &&
1376 transport->smart_options->cas &&
1377 !is_empty_cas(transport->smart_options->cas))
1378 apply_push_cas(transport->smart_options->cas,
1379 transport->remote, remote_refs);
1381 set_ref_status_for_push(remote_refs,
1382 flags & TRANSPORT_PUSH_MIRROR,
1383 flags & TRANSPORT_PUSH_FORCE);
1385 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1386 if (run_pre_push_hook(transport, remote_refs))
1387 goto done;
1389 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1390 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1391 !is_bare_repository()) {
1392 struct ref *ref = remote_refs;
1393 struct oid_array commits = OID_ARRAY_INIT;
1395 trace2_region_enter("transport_push", "push_submodules", r);
1396 for (; ref; ref = ref->next)
1397 if (!is_null_oid(&ref->new_oid))
1398 oid_array_append(&commits,
1399 &ref->new_oid);
1401 if (!push_unpushed_submodules(r,
1402 &commits,
1403 transport->remote,
1405 transport->push_options,
1406 pretend)) {
1407 oid_array_clear(&commits);
1408 trace2_region_leave("transport_push", "push_submodules", r);
1409 die(_("failed to push all needed submodules"));
1411 oid_array_clear(&commits);
1412 trace2_region_leave("transport_push", "push_submodules", r);
1415 if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1416 ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1417 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1418 !pretend)) && !is_bare_repository()) {
1419 struct ref *ref = remote_refs;
1420 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1421 struct oid_array commits = OID_ARRAY_INIT;
1423 trace2_region_enter("transport_push", "check_submodules", r);
1424 for (; ref; ref = ref->next)
1425 if (!is_null_oid(&ref->new_oid))
1426 oid_array_append(&commits,
1427 &ref->new_oid);
1429 if (find_unpushed_submodules(r,
1430 &commits,
1431 transport->remote->name,
1432 &needs_pushing)) {
1433 oid_array_clear(&commits);
1434 trace2_region_leave("transport_push", "check_submodules", r);
1435 die_with_unpushed_submodules(&needs_pushing);
1437 string_list_clear(&needs_pushing, 0);
1438 oid_array_clear(&commits);
1439 trace2_region_leave("transport_push", "check_submodules", r);
1442 if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY)) {
1443 trace2_region_enter("transport_push", "push_refs", r);
1444 push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1445 trace2_region_leave("transport_push", "push_refs", r);
1446 } else
1447 push_ret = 0;
1448 err = push_had_errors(remote_refs);
1449 ret = push_ret | err;
1451 if (!quiet || err)
1452 transport_print_push_status(transport->url, remote_refs,
1453 verbose | porcelain, porcelain,
1454 reject_reasons);
1456 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1457 set_upstreams(transport, remote_refs, pretend);
1459 if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1460 TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1461 struct ref *ref;
1462 for (ref = remote_refs; ref; ref = ref->next)
1463 transport_update_tracking_ref(transport->remote, ref, verbose);
1466 if (porcelain && !push_ret)
1467 puts("Done");
1468 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1469 fprintf(stderr, "Everything up-to-date\n");
1471 done:
1472 free_refs(local_refs);
1473 free_refs(remote_refs);
1474 return ret;
1477 const struct ref *transport_get_remote_refs(struct transport *transport,
1478 struct transport_ls_refs_options *transport_options)
1480 if (!transport->got_remote_refs) {
1481 transport->remote_refs =
1482 transport->vtable->get_refs_list(transport, 0,
1483 transport_options);
1484 transport->got_remote_refs = 1;
1487 return transport->remote_refs;
1490 void transport_ls_refs_options_release(struct transport_ls_refs_options *opts)
1492 strvec_clear(&opts->ref_prefixes);
1493 free((char *)opts->unborn_head_target);
1496 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1498 int rc;
1499 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1500 struct ref **heads = NULL;
1501 struct ref *rm;
1503 for (rm = refs; rm; rm = rm->next) {
1504 nr_refs++;
1505 if (rm->peer_ref &&
1506 !is_null_oid(&rm->old_oid) &&
1507 oideq(&rm->peer_ref->old_oid, &rm->old_oid))
1508 continue;
1509 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1510 heads[nr_heads++] = rm;
1513 if (!nr_heads) {
1515 * When deepening of a shallow repository is requested,
1516 * then local and remote refs are likely to still be equal.
1517 * Just feed them all to the fetch method in that case.
1518 * This condition shouldn't be met in a non-deepening fetch
1519 * (see builtin/fetch.c:quickfetch()).
1521 ALLOC_ARRAY(heads, nr_refs);
1522 for (rm = refs; rm; rm = rm->next)
1523 heads[nr_heads++] = rm;
1526 rc = transport->vtable->fetch_refs(transport, nr_heads, heads);
1528 free(heads);
1529 return rc;
1532 int transport_get_remote_bundle_uri(struct transport *transport)
1534 int value = 0;
1535 const struct transport_vtable *vtable = transport->vtable;
1537 /* Check config only once. */
1538 if (transport->got_remote_bundle_uri)
1539 return 0;
1540 transport->got_remote_bundle_uri = 1;
1543 * Don't request bundle-uri from the server unless configured to
1544 * do so by the transfer.bundleURI=true config option.
1546 if (git_config_get_bool("transfer.bundleuri", &value) || !value)
1547 return 0;
1549 if (!transport->bundles->baseURI)
1550 transport->bundles->baseURI = xstrdup(transport->url);
1552 if (!vtable->get_bundle_uri)
1553 return error(_("bundle-uri operation not supported by protocol"));
1555 if (vtable->get_bundle_uri(transport) < 0)
1556 return error(_("could not retrieve server-advertised bundle-uri list"));
1557 return 0;
1560 void transport_unlock_pack(struct transport *transport, unsigned int flags)
1562 int in_signal_handler = !!(flags & TRANSPORT_UNLOCK_PACK_IN_SIGNAL_HANDLER);
1563 int i;
1565 for (i = 0; i < transport->pack_lockfiles.nr; i++)
1566 if (in_signal_handler)
1567 unlink(transport->pack_lockfiles.items[i].string);
1568 else
1569 unlink_or_warn(transport->pack_lockfiles.items[i].string);
1570 if (!in_signal_handler)
1571 string_list_clear(&transport->pack_lockfiles, 0);
1574 int transport_connect(struct transport *transport, const char *name,
1575 const char *exec, int fd[2])
1577 if (transport->vtable->connect)
1578 return transport->vtable->connect(transport, name, exec, fd);
1579 else
1580 die(_("operation not supported by protocol"));
1583 int transport_disconnect(struct transport *transport)
1585 int ret = 0;
1586 if (transport->vtable->disconnect)
1587 ret = transport->vtable->disconnect(transport);
1588 if (transport->got_remote_refs)
1589 free_refs((void *)transport->remote_refs);
1590 clear_bundle_list(transport->bundles);
1591 free(transport->bundles);
1592 free(transport);
1593 return ret;
1597 * Strip username (and password) from a URL and return
1598 * it in a newly allocated string.
1600 char *transport_anonymize_url(const char *url)
1602 char *scheme_prefix, *anon_part;
1603 size_t anon_len, prefix_len = 0;
1605 anon_part = strchr(url, '@');
1606 if (url_is_local_not_ssh(url) || !anon_part)
1607 goto literal_copy;
1609 anon_len = strlen(++anon_part);
1610 scheme_prefix = strstr(url, "://");
1611 if (!scheme_prefix) {
1612 if (!strchr(anon_part, ':'))
1613 /* cannot be "me@there:/path/name" */
1614 goto literal_copy;
1615 } else {
1616 const char *cp;
1617 /* make sure scheme is reasonable */
1618 for (cp = url; cp < scheme_prefix; cp++) {
1619 switch (*cp) {
1620 /* RFC 1738 2.1 */
1621 case '+': case '.': case '-':
1622 break; /* ok */
1623 default:
1624 if (isalnum(*cp))
1625 break;
1626 /* it isn't */
1627 goto literal_copy;
1630 /* @ past the first slash does not count */
1631 cp = strchr(scheme_prefix + 3, '/');
1632 if (cp && cp < anon_part)
1633 goto literal_copy;
1634 prefix_len = scheme_prefix - url + 3;
1636 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1637 (int)anon_len, anon_part);
1638 literal_copy:
1639 return xstrdup(url);