rebase: stop reading and writing unnecessary strategy state
[git.git] / transport.c
blobfa9bc3be0850648b28c4ede889d956326fc9fe45
1 #include "git-compat-util.h"
2 #include "alloc.h"
3 #include "config.h"
4 #include "hex.h"
5 #include "transport.h"
6 #include "hook.h"
7 #include "pkt-line.h"
8 #include "fetch-pack.h"
9 #include "remote.h"
10 #include "connect.h"
11 #include "send-pack.h"
12 #include "walker.h"
13 #include "bundle.h"
14 #include "dir.h"
15 #include "gettext.h"
16 #include "refs.h"
17 #include "refspec.h"
18 #include "branch.h"
19 #include "url.h"
20 #include "submodule.h"
21 #include "string-list.h"
22 #include "oid-array.h"
23 #include "sigchain.h"
24 #include "transport-internal.h"
25 #include "protocol.h"
26 #include "object-store.h"
27 #include "color.h"
28 #include "bundle-uri.h"
30 static int transport_use_color = -1;
31 static char transport_colors[][COLOR_MAXLEN] = {
32 GIT_COLOR_RESET,
33 GIT_COLOR_RED /* REJECTED */
36 enum color_transport {
37 TRANSPORT_COLOR_RESET = 0,
38 TRANSPORT_COLOR_REJECTED = 1
41 static int transport_color_config(void)
43 const char *keys[] = {
44 "color.transport.reset",
45 "color.transport.rejected"
46 }, *key = "color.transport";
47 char *value;
48 int i;
49 static int initialized;
51 if (initialized)
52 return 0;
53 initialized = 1;
55 if (!git_config_get_string(key, &value))
56 transport_use_color = git_config_colorbool(key, value);
58 if (!want_color_stderr(transport_use_color))
59 return 0;
61 for (i = 0; i < ARRAY_SIZE(keys); i++)
62 if (!git_config_get_string(keys[i], &value)) {
63 if (!value)
64 return config_error_nonbool(keys[i]);
65 if (color_parse(value, transport_colors[i]) < 0)
66 return -1;
69 return 0;
72 static const char *transport_get_color(enum color_transport ix)
74 if (want_color_stderr(transport_use_color))
75 return transport_colors[ix];
76 return "";
79 static void set_upstreams(struct transport *transport, struct ref *refs,
80 int pretend)
82 struct ref *ref;
83 for (ref = refs; ref; ref = ref->next) {
84 const char *localname;
85 const char *tmp;
86 const char *remotename;
87 int flag = 0;
89 * Check suitability for tracking. Must be successful /
90 * already up-to-date ref create/modify (not delete).
92 if (ref->status != REF_STATUS_OK &&
93 ref->status != REF_STATUS_UPTODATE)
94 continue;
95 if (!ref->peer_ref)
96 continue;
97 if (is_null_oid(&ref->new_oid))
98 continue;
100 /* Follow symbolic refs (mainly for HEAD). */
101 localname = ref->peer_ref->name;
102 remotename = ref->name;
103 tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING,
104 NULL, &flag);
105 if (tmp && flag & REF_ISSYMREF &&
106 starts_with(tmp, "refs/heads/"))
107 localname = tmp;
109 /* Both source and destination must be local branches. */
110 if (!localname || !starts_with(localname, "refs/heads/"))
111 continue;
112 if (!remotename || !starts_with(remotename, "refs/heads/"))
113 continue;
115 if (!pretend) {
116 int flag = transport->verbose < 0 ? 0 : BRANCH_CONFIG_VERBOSE;
117 install_branch_config(flag, localname + 11,
118 transport->remote->name, remotename);
119 } else if (transport->verbose >= 0)
120 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
121 localname + 11, remotename + 11,
122 transport->remote->name);
126 struct bundle_transport_data {
127 int fd;
128 struct bundle_header header;
129 unsigned get_refs_from_bundle_called : 1;
132 static void get_refs_from_bundle_inner(struct transport *transport)
134 struct bundle_transport_data *data = transport->data;
136 data->get_refs_from_bundle_called = 1;
138 if (data->fd > 0)
139 close(data->fd);
140 data->fd = read_bundle_header(transport->url, &data->header);
141 if (data->fd < 0)
142 die(_("could not read bundle '%s'"), transport->url);
144 transport->hash_algo = data->header.hash_algo;
147 static struct ref *get_refs_from_bundle(struct transport *transport,
148 int for_push,
149 struct transport_ls_refs_options *transport_options UNUSED)
151 struct bundle_transport_data *data = transport->data;
152 struct ref *result = NULL;
153 int i;
155 if (for_push)
156 return NULL;
158 get_refs_from_bundle_inner(transport);
160 for (i = 0; i < data->header.references.nr; i++) {
161 struct string_list_item *e = data->header.references.items + i;
162 const char *name = e->string;
163 struct ref *ref = alloc_ref(name);
164 struct object_id *oid = e->util;
165 oidcpy(&ref->old_oid, oid);
166 ref->next = result;
167 result = ref;
169 return result;
172 static int fetch_refs_from_bundle(struct transport *transport,
173 int nr_heads, struct ref **to_fetch)
175 struct bundle_transport_data *data = transport->data;
176 struct strvec extra_index_pack_args = STRVEC_INIT;
177 int ret;
179 if (transport->progress)
180 strvec_push(&extra_index_pack_args, "-v");
182 if (!data->get_refs_from_bundle_called)
183 get_refs_from_bundle_inner(transport);
184 ret = unbundle(the_repository, &data->header, data->fd,
185 &extra_index_pack_args, 0);
186 transport->hash_algo = data->header.hash_algo;
187 return ret;
190 static int close_bundle(struct transport *transport)
192 struct bundle_transport_data *data = transport->data;
193 if (data->fd > 0)
194 close(data->fd);
195 bundle_header_release(&data->header);
196 free(data);
197 return 0;
200 struct git_transport_data {
201 struct git_transport_options options;
202 struct child_process *conn;
203 int fd[2];
204 unsigned finished_handshake : 1;
205 enum protocol_version version;
206 struct oid_array extra_have;
207 struct oid_array shallow;
210 static int set_git_option(struct git_transport_options *opts,
211 const char *name, const char *value)
213 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
214 opts->uploadpack = value;
215 return 0;
216 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
217 opts->receivepack = value;
218 return 0;
219 } else if (!strcmp(name, TRANS_OPT_THIN)) {
220 opts->thin = !!value;
221 return 0;
222 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
223 opts->followtags = !!value;
224 return 0;
225 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
226 opts->keep = !!value;
227 return 0;
228 } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
229 opts->update_shallow = !!value;
230 return 0;
231 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
232 if (!value)
233 opts->depth = 0;
234 else {
235 char *end;
236 opts->depth = strtol(value, &end, 0);
237 if (*end)
238 die(_("transport: invalid depth option '%s'"), value);
240 return 0;
241 } else if (!strcmp(name, TRANS_OPT_DEEPEN_SINCE)) {
242 opts->deepen_since = value;
243 return 0;
244 } else if (!strcmp(name, TRANS_OPT_DEEPEN_NOT)) {
245 opts->deepen_not = (const struct string_list *)value;
246 return 0;
247 } else if (!strcmp(name, TRANS_OPT_DEEPEN_RELATIVE)) {
248 opts->deepen_relative = !!value;
249 return 0;
250 } else if (!strcmp(name, TRANS_OPT_FROM_PROMISOR)) {
251 opts->from_promisor = !!value;
252 return 0;
253 } else if (!strcmp(name, TRANS_OPT_LIST_OBJECTS_FILTER)) {
254 list_objects_filter_die_if_populated(&opts->filter_options);
255 parse_list_objects_filter(&opts->filter_options, value);
256 return 0;
257 } else if (!strcmp(name, TRANS_OPT_REFETCH)) {
258 opts->refetch = !!value;
259 return 0;
260 } else if (!strcmp(name, TRANS_OPT_REJECT_SHALLOW)) {
261 opts->reject_shallow = !!value;
262 return 0;
264 return 1;
267 static int connect_setup(struct transport *transport, int for_push)
269 struct git_transport_data *data = transport->data;
270 int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
272 if (data->conn)
273 return 0;
275 switch (transport->family) {
276 case TRANSPORT_FAMILY_ALL: break;
277 case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
278 case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
281 data->conn = git_connect(data->fd, transport->url,
282 for_push ?
283 "git-receive-pack" :
284 "git-upload-pack",
285 for_push ?
286 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 = find_unique_abbrev_r(hex, oid, DEFAULT_ABBREV);
788 return (w < sofar) ? sofar : w;
791 int transport_summary_width(const struct ref *refs)
793 int maxw = -1;
795 for (; refs; refs = refs->next) {
796 maxw = measure_abbrev(&refs->old_oid, maxw);
797 maxw = measure_abbrev(&refs->new_oid, maxw);
799 if (maxw < 0)
800 maxw = FALLBACK_DEFAULT_ABBREV;
801 return (2 * maxw + 3);
804 void transport_print_push_status(const char *dest, struct ref *refs,
805 int verbose, int porcelain, unsigned int *reject_reasons)
807 struct ref *ref;
808 int n = 0;
809 char *head;
810 int summary_width = transport_summary_width(refs);
812 if (transport_color_config() < 0)
813 warning(_("could not parse transport.color.* config"));
815 head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
817 if (verbose) {
818 for (ref = refs; ref; ref = ref->next)
819 if (ref->status == REF_STATUS_UPTODATE)
820 n += print_one_push_status(ref, dest, n,
821 porcelain, summary_width);
824 for (ref = refs; ref; ref = ref->next)
825 if (ref->status == REF_STATUS_OK)
826 n += print_one_push_status(ref, dest, n,
827 porcelain, summary_width);
829 *reject_reasons = 0;
830 for (ref = refs; ref; ref = ref->next) {
831 if (ref->status != REF_STATUS_NONE &&
832 ref->status != REF_STATUS_UPTODATE &&
833 ref->status != REF_STATUS_OK)
834 n += print_one_push_status(ref, dest, n,
835 porcelain, summary_width);
836 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
837 if (head != NULL && !strcmp(head, ref->name))
838 *reject_reasons |= REJECT_NON_FF_HEAD;
839 else
840 *reject_reasons |= REJECT_NON_FF_OTHER;
841 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
842 *reject_reasons |= REJECT_ALREADY_EXISTS;
843 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
844 *reject_reasons |= REJECT_FETCH_FIRST;
845 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
846 *reject_reasons |= REJECT_NEEDS_FORCE;
847 } else if (ref->status == REF_STATUS_REJECT_REMOTE_UPDATED) {
848 *reject_reasons |= REJECT_REF_NEEDS_UPDATE;
851 free(head);
854 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
856 struct git_transport_data *data = transport->data;
857 struct send_pack_args args;
858 int ret = 0;
860 if (transport_color_config() < 0)
861 return -1;
863 if (!data->finished_handshake)
864 get_refs_via_connect(transport, 1, NULL);
866 memset(&args, 0, sizeof(args));
867 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
868 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
869 args.use_thin_pack = data->options.thin;
870 args.verbose = (transport->verbose > 0);
871 args.quiet = (transport->verbose < 0);
872 args.progress = transport->progress;
873 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
874 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
875 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
876 args.push_options = transport->push_options;
877 args.url = transport->url;
879 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
880 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
881 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
882 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
883 else
884 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
886 switch (data->version) {
887 case protocol_v2:
888 die(_("support for protocol v2 not implemented yet"));
889 break;
890 case protocol_v1:
891 case protocol_v0:
892 ret = send_pack(&args, data->fd, data->conn, remote_refs,
893 &data->extra_have);
894 break;
895 case protocol_unknown_version:
896 BUG("unknown protocol version");
899 close(data->fd[1]);
900 close(data->fd[0]);
902 * Atomic push may abort the connection early and close the pipe,
903 * which may cause an error for `finish_connect()`. Ignore this error
904 * for atomic git-push.
906 if (ret || args.atomic)
907 finish_connect(data->conn);
908 else
909 ret = finish_connect(data->conn);
910 data->conn = NULL;
911 data->finished_handshake = 0;
913 return ret;
916 static int connect_git(struct transport *transport, const char *name,
917 const char *executable, int fd[2])
919 struct git_transport_data *data = transport->data;
920 data->conn = git_connect(data->fd, transport->url,
921 name, executable, 0);
922 fd[0] = data->fd[0];
923 fd[1] = data->fd[1];
924 return 0;
927 static int disconnect_git(struct transport *transport)
929 struct git_transport_data *data = transport->data;
930 if (data->conn) {
931 if (data->finished_handshake && !transport->stateless_rpc)
932 packet_flush(data->fd[1]);
933 close(data->fd[0]);
934 if (data->fd[1] >= 0)
935 close(data->fd[1]);
936 finish_connect(data->conn);
939 list_objects_filter_release(&data->options.filter_options);
940 free(data);
941 return 0;
944 static struct transport_vtable taken_over_vtable = {
945 .get_refs_list = get_refs_via_connect,
946 .get_bundle_uri = get_bundle_uri,
947 .fetch_refs = fetch_refs_via_pack,
948 .push_refs = git_transport_push,
949 .disconnect = disconnect_git
952 void transport_take_over(struct transport *transport,
953 struct child_process *child)
955 struct git_transport_data *data;
957 if (!transport->smart_options)
958 BUG("taking over transport requires non-NULL "
959 "smart_options field.");
961 CALLOC_ARRAY(data, 1);
962 data->options = *transport->smart_options;
963 data->conn = child;
964 data->fd[0] = data->conn->out;
965 data->fd[1] = data->conn->in;
966 data->finished_handshake = 0;
967 transport->data = data;
969 transport->vtable = &taken_over_vtable;
970 transport->smart_options = &(data->options);
972 transport->cannot_reuse = 1;
975 static int is_file(const char *url)
977 struct stat buf;
978 if (stat(url, &buf))
979 return 0;
980 return S_ISREG(buf.st_mode);
983 static int external_specification_len(const char *url)
985 return strchr(url, ':') - url;
988 static const struct string_list *protocol_allow_list(void)
990 static int enabled = -1;
991 static struct string_list allowed = STRING_LIST_INIT_DUP;
993 if (enabled < 0) {
994 const char *v = getenv("GIT_ALLOW_PROTOCOL");
995 if (v) {
996 string_list_split(&allowed, v, ':', -1);
997 string_list_sort(&allowed);
998 enabled = 1;
999 } else {
1000 enabled = 0;
1004 return enabled ? &allowed : NULL;
1007 enum protocol_allow_config {
1008 PROTOCOL_ALLOW_NEVER = 0,
1009 PROTOCOL_ALLOW_USER_ONLY,
1010 PROTOCOL_ALLOW_ALWAYS
1013 static enum protocol_allow_config parse_protocol_config(const char *key,
1014 const char *value)
1016 if (!strcasecmp(value, "always"))
1017 return PROTOCOL_ALLOW_ALWAYS;
1018 else if (!strcasecmp(value, "never"))
1019 return PROTOCOL_ALLOW_NEVER;
1020 else if (!strcasecmp(value, "user"))
1021 return PROTOCOL_ALLOW_USER_ONLY;
1023 die(_("unknown value for config '%s': %s"), key, value);
1026 static enum protocol_allow_config get_protocol_config(const char *type)
1028 char *key = xstrfmt("protocol.%s.allow", type);
1029 char *value;
1031 /* first check the per-protocol config */
1032 if (!git_config_get_string(key, &value)) {
1033 enum protocol_allow_config ret =
1034 parse_protocol_config(key, value);
1035 free(key);
1036 free(value);
1037 return ret;
1039 free(key);
1041 /* if defined, fallback to user-defined default for unknown protocols */
1042 if (!git_config_get_string("protocol.allow", &value)) {
1043 enum protocol_allow_config ret =
1044 parse_protocol_config("protocol.allow", value);
1045 free(value);
1046 return ret;
1049 /* fallback to built-in defaults */
1050 /* known safe */
1051 if (!strcmp(type, "http") ||
1052 !strcmp(type, "https") ||
1053 !strcmp(type, "git") ||
1054 !strcmp(type, "ssh"))
1055 return PROTOCOL_ALLOW_ALWAYS;
1057 /* known scary; err on the side of caution */
1058 if (!strcmp(type, "ext"))
1059 return PROTOCOL_ALLOW_NEVER;
1061 /* unknown; by default let them be used only directly by the user */
1062 return PROTOCOL_ALLOW_USER_ONLY;
1065 int is_transport_allowed(const char *type, int from_user)
1067 const struct string_list *allow_list = protocol_allow_list();
1068 if (allow_list)
1069 return string_list_has_string(allow_list, type);
1071 switch (get_protocol_config(type)) {
1072 case PROTOCOL_ALLOW_ALWAYS:
1073 return 1;
1074 case PROTOCOL_ALLOW_NEVER:
1075 return 0;
1076 case PROTOCOL_ALLOW_USER_ONLY:
1077 if (from_user < 0)
1078 from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1079 return from_user;
1082 BUG("invalid protocol_allow_config type");
1085 void transport_check_allowed(const char *type)
1087 if (!is_transport_allowed(type, -1))
1088 die(_("transport '%s' not allowed"), type);
1091 static struct transport_vtable bundle_vtable = {
1092 .get_refs_list = get_refs_from_bundle,
1093 .fetch_refs = fetch_refs_from_bundle,
1094 .disconnect = close_bundle
1097 static struct transport_vtable builtin_smart_vtable = {
1098 .get_refs_list = get_refs_via_connect,
1099 .get_bundle_uri = get_bundle_uri,
1100 .fetch_refs = fetch_refs_via_pack,
1101 .push_refs = git_transport_push,
1102 .connect = connect_git,
1103 .disconnect = disconnect_git
1106 struct transport *transport_get(struct remote *remote, const char *url)
1108 const char *helper;
1109 struct transport *ret = xcalloc(1, sizeof(*ret));
1111 ret->progress = isatty(2);
1112 string_list_init_dup(&ret->pack_lockfiles);
1114 CALLOC_ARRAY(ret->bundles, 1);
1115 init_bundle_list(ret->bundles);
1117 if (!remote)
1118 BUG("No remote provided to transport_get()");
1120 ret->got_remote_refs = 0;
1121 ret->remote = remote;
1122 helper = remote->foreign_vcs;
1124 if (!url && remote->url)
1125 url = remote->url[0];
1126 ret->url = url;
1128 /* maybe it is a foreign URL? */
1129 if (url) {
1130 const char *p = url;
1132 while (is_urlschemechar(p == url, *p))
1133 p++;
1134 if (starts_with(p, "::"))
1135 helper = xstrndup(url, p - url);
1138 if (helper) {
1139 transport_helper_init(ret, helper);
1140 } else if (starts_with(url, "rsync:")) {
1141 die(_("git-over-rsync is no longer supported"));
1142 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
1143 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
1144 bundle_header_init(&data->header);
1145 transport_check_allowed("file");
1146 ret->data = data;
1147 ret->vtable = &bundle_vtable;
1148 ret->smart_options = NULL;
1149 } else if (!is_url(url)
1150 || starts_with(url, "file://")
1151 || starts_with(url, "git://")
1152 || starts_with(url, "ssh://")
1153 || starts_with(url, "git+ssh://") /* deprecated - do not use */
1154 || starts_with(url, "ssh+git://") /* deprecated - do not use */
1157 * These are builtin smart transports; "allowed" transports
1158 * will be checked individually in git_connect.
1160 struct git_transport_data *data = xcalloc(1, sizeof(*data));
1161 list_objects_filter_init(&data->options.filter_options);
1162 ret->data = data;
1163 ret->vtable = &builtin_smart_vtable;
1164 ret->smart_options = &(data->options);
1166 data->conn = NULL;
1167 data->finished_handshake = 0;
1168 } else {
1169 /* Unknown protocol in URL. Pass to external handler. */
1170 int len = external_specification_len(url);
1171 char *handler = xmemdupz(url, len);
1172 transport_helper_init(ret, handler);
1175 if (ret->smart_options) {
1176 ret->smart_options->thin = 1;
1177 ret->smart_options->uploadpack = "git-upload-pack";
1178 if (remote->uploadpack)
1179 ret->smart_options->uploadpack = remote->uploadpack;
1180 ret->smart_options->receivepack = "git-receive-pack";
1181 if (remote->receivepack)
1182 ret->smart_options->receivepack = remote->receivepack;
1185 ret->hash_algo = &hash_algos[GIT_HASH_SHA1];
1187 return ret;
1190 const struct git_hash_algo *transport_get_hash_algo(struct transport *transport)
1192 return transport->hash_algo;
1195 int transport_set_option(struct transport *transport,
1196 const char *name, const char *value)
1198 int git_reports = 1, protocol_reports = 1;
1200 if (transport->smart_options)
1201 git_reports = set_git_option(transport->smart_options,
1202 name, value);
1204 if (transport->vtable->set_option)
1205 protocol_reports = transport->vtable->set_option(transport,
1206 name, value);
1208 /* If either report is 0, report 0 (success). */
1209 if (!git_reports || !protocol_reports)
1210 return 0;
1211 /* If either reports -1 (invalid value), report -1. */
1212 if ((git_reports == -1) || (protocol_reports == -1))
1213 return -1;
1214 /* Otherwise if both report unknown, report unknown. */
1215 return 1;
1218 void transport_set_verbosity(struct transport *transport, int verbosity,
1219 int force_progress)
1221 if (verbosity >= 1)
1222 transport->verbose = verbosity <= 3 ? verbosity : 3;
1223 if (verbosity < 0)
1224 transport->verbose = -1;
1227 * Rules used to determine whether to report progress (processing aborts
1228 * when a rule is satisfied):
1230 * . Report progress, if force_progress is 1 (ie. --progress).
1231 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1232 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1233 * . Report progress if isatty(2) is 1.
1235 if (force_progress >= 0)
1236 transport->progress = !!force_progress;
1237 else
1238 transport->progress = verbosity >= 0 && isatty(2);
1241 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
1243 int i;
1245 fprintf(stderr, _("The following submodule paths contain changes that can\n"
1246 "not be found on any remote:\n"));
1247 for (i = 0; i < needs_pushing->nr; i++)
1248 fprintf(stderr, " %s\n", needs_pushing->items[i].string);
1249 fprintf(stderr, _("\nPlease try\n\n"
1250 " git push --recurse-submodules=on-demand\n\n"
1251 "or cd to the path and use\n\n"
1252 " git push\n\n"
1253 "to push them to a remote.\n\n"));
1255 string_list_clear(needs_pushing, 0);
1257 die(_("Aborting."));
1260 static int run_pre_push_hook(struct transport *transport,
1261 struct ref *remote_refs)
1263 int ret = 0, x;
1264 struct ref *r;
1265 struct child_process proc = CHILD_PROCESS_INIT;
1266 struct strbuf buf;
1267 const char *hook_path = find_hook("pre-push");
1269 if (!hook_path)
1270 return 0;
1272 strvec_push(&proc.args, hook_path);
1273 strvec_push(&proc.args, transport->remote->name);
1274 strvec_push(&proc.args, transport->url);
1276 proc.in = -1;
1277 proc.trace2_hook_name = "pre-push";
1279 if (start_command(&proc)) {
1280 finish_command(&proc);
1281 return -1;
1284 sigchain_push(SIGPIPE, SIG_IGN);
1286 strbuf_init(&buf, 256);
1288 for (r = remote_refs; r; r = r->next) {
1289 if (!r->peer_ref) continue;
1290 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
1291 if (r->status == REF_STATUS_REJECT_STALE) continue;
1292 if (r->status == REF_STATUS_REJECT_REMOTE_UPDATED) continue;
1293 if (r->status == REF_STATUS_UPTODATE) continue;
1295 strbuf_reset(&buf);
1296 strbuf_addf( &buf, "%s %s %s %s\n",
1297 r->peer_ref->name, oid_to_hex(&r->new_oid),
1298 r->name, oid_to_hex(&r->old_oid));
1300 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
1301 /* We do not mind if a hook does not read all refs. */
1302 if (errno != EPIPE)
1303 ret = -1;
1304 break;
1308 strbuf_release(&buf);
1310 x = close(proc.in);
1311 if (!ret)
1312 ret = x;
1314 sigchain_pop(SIGPIPE);
1316 x = finish_command(&proc);
1317 if (!ret)
1318 ret = x;
1320 return ret;
1323 int transport_push(struct repository *r,
1324 struct transport *transport,
1325 struct refspec *rs, int flags,
1326 unsigned int *reject_reasons)
1328 struct ref *remote_refs = NULL;
1329 struct ref *local_refs = NULL;
1330 int match_flags = MATCH_REFS_NONE;
1331 int verbose = (transport->verbose > 0);
1332 int quiet = (transport->verbose < 0);
1333 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1334 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1335 int push_ret, err;
1336 int ret = -1;
1337 struct transport_ls_refs_options transport_options =
1338 TRANSPORT_LS_REFS_OPTIONS_INIT;
1340 *reject_reasons = 0;
1342 if (transport_color_config() < 0)
1343 goto done;
1345 if (!transport->vtable->push_refs)
1346 goto done;
1348 local_refs = get_local_heads();
1350 if (check_push_refs(local_refs, rs) < 0)
1351 goto done;
1353 refspec_ref_prefixes(rs, &transport_options.ref_prefixes);
1355 trace2_region_enter("transport_push", "get_refs_list", r);
1356 remote_refs = transport->vtable->get_refs_list(transport, 1,
1357 &transport_options);
1358 trace2_region_leave("transport_push", "get_refs_list", r);
1360 transport_ls_refs_options_release(&transport_options);
1362 if (flags & TRANSPORT_PUSH_ALL)
1363 match_flags |= MATCH_REFS_ALL;
1364 if (flags & TRANSPORT_PUSH_MIRROR)
1365 match_flags |= MATCH_REFS_MIRROR;
1366 if (flags & TRANSPORT_PUSH_PRUNE)
1367 match_flags |= MATCH_REFS_PRUNE;
1368 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1369 match_flags |= MATCH_REFS_FOLLOW_TAGS;
1371 if (match_push_refs(local_refs, &remote_refs, rs, match_flags))
1372 goto done;
1374 if (transport->smart_options &&
1375 transport->smart_options->cas &&
1376 !is_empty_cas(transport->smart_options->cas))
1377 apply_push_cas(transport->smart_options->cas,
1378 transport->remote, remote_refs);
1380 set_ref_status_for_push(remote_refs,
1381 flags & TRANSPORT_PUSH_MIRROR,
1382 flags & TRANSPORT_PUSH_FORCE);
1384 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1385 if (run_pre_push_hook(transport, remote_refs))
1386 goto done;
1388 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1389 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1390 !is_bare_repository()) {
1391 struct ref *ref = remote_refs;
1392 struct oid_array commits = OID_ARRAY_INIT;
1394 trace2_region_enter("transport_push", "push_submodules", r);
1395 for (; ref; ref = ref->next)
1396 if (!is_null_oid(&ref->new_oid))
1397 oid_array_append(&commits,
1398 &ref->new_oid);
1400 if (!push_unpushed_submodules(r,
1401 &commits,
1402 transport->remote,
1404 transport->push_options,
1405 pretend)) {
1406 oid_array_clear(&commits);
1407 trace2_region_leave("transport_push", "push_submodules", r);
1408 die(_("failed to push all needed submodules"));
1410 oid_array_clear(&commits);
1411 trace2_region_leave("transport_push", "push_submodules", r);
1414 if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1415 ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1416 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1417 !pretend)) && !is_bare_repository()) {
1418 struct ref *ref = remote_refs;
1419 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1420 struct oid_array commits = OID_ARRAY_INIT;
1422 trace2_region_enter("transport_push", "check_submodules", r);
1423 for (; ref; ref = ref->next)
1424 if (!is_null_oid(&ref->new_oid))
1425 oid_array_append(&commits,
1426 &ref->new_oid);
1428 if (find_unpushed_submodules(r,
1429 &commits,
1430 transport->remote->name,
1431 &needs_pushing)) {
1432 oid_array_clear(&commits);
1433 trace2_region_leave("transport_push", "check_submodules", r);
1434 die_with_unpushed_submodules(&needs_pushing);
1436 string_list_clear(&needs_pushing, 0);
1437 oid_array_clear(&commits);
1438 trace2_region_leave("transport_push", "check_submodules", r);
1441 if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY)) {
1442 trace2_region_enter("transport_push", "push_refs", r);
1443 push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1444 trace2_region_leave("transport_push", "push_refs", r);
1445 } else
1446 push_ret = 0;
1447 err = push_had_errors(remote_refs);
1448 ret = push_ret | err;
1450 if (!quiet || err)
1451 transport_print_push_status(transport->url, remote_refs,
1452 verbose | porcelain, porcelain,
1453 reject_reasons);
1455 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1456 set_upstreams(transport, remote_refs, pretend);
1458 if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1459 TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1460 struct ref *ref;
1461 for (ref = remote_refs; ref; ref = ref->next)
1462 transport_update_tracking_ref(transport->remote, ref, verbose);
1465 if (porcelain && !push_ret)
1466 puts("Done");
1467 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1468 fprintf(stderr, "Everything up-to-date\n");
1470 done:
1471 free_refs(local_refs);
1472 free_refs(remote_refs);
1473 return ret;
1476 const struct ref *transport_get_remote_refs(struct transport *transport,
1477 struct transport_ls_refs_options *transport_options)
1479 if (!transport->got_remote_refs) {
1480 transport->remote_refs =
1481 transport->vtable->get_refs_list(transport, 0,
1482 transport_options);
1483 transport->got_remote_refs = 1;
1486 return transport->remote_refs;
1489 void transport_ls_refs_options_release(struct transport_ls_refs_options *opts)
1491 strvec_clear(&opts->ref_prefixes);
1492 free((char *)opts->unborn_head_target);
1495 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1497 int rc;
1498 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1499 struct ref **heads = NULL;
1500 struct ref *rm;
1502 for (rm = refs; rm; rm = rm->next) {
1503 nr_refs++;
1504 if (rm->peer_ref &&
1505 !is_null_oid(&rm->old_oid) &&
1506 oideq(&rm->peer_ref->old_oid, &rm->old_oid))
1507 continue;
1508 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1509 heads[nr_heads++] = rm;
1512 if (!nr_heads) {
1514 * When deepening of a shallow repository is requested,
1515 * then local and remote refs are likely to still be equal.
1516 * Just feed them all to the fetch method in that case.
1517 * This condition shouldn't be met in a non-deepening fetch
1518 * (see builtin/fetch.c:quickfetch()).
1520 ALLOC_ARRAY(heads, nr_refs);
1521 for (rm = refs; rm; rm = rm->next)
1522 heads[nr_heads++] = rm;
1525 rc = transport->vtable->fetch_refs(transport, nr_heads, heads);
1527 free(heads);
1528 return rc;
1531 int transport_get_remote_bundle_uri(struct transport *transport)
1533 int value = 0;
1534 const struct transport_vtable *vtable = transport->vtable;
1536 /* Check config only once. */
1537 if (transport->got_remote_bundle_uri)
1538 return 0;
1539 transport->got_remote_bundle_uri = 1;
1542 * Don't request bundle-uri from the server unless configured to
1543 * do so by the transfer.bundleURI=true config option.
1545 if (git_config_get_bool("transfer.bundleuri", &value) || !value)
1546 return 0;
1548 if (!transport->bundles->baseURI)
1549 transport->bundles->baseURI = xstrdup(transport->url);
1551 if (!vtable->get_bundle_uri)
1552 return error(_("bundle-uri operation not supported by protocol"));
1554 if (vtable->get_bundle_uri(transport) < 0)
1555 return error(_("could not retrieve server-advertised bundle-uri list"));
1556 return 0;
1559 void transport_unlock_pack(struct transport *transport, unsigned int flags)
1561 int in_signal_handler = !!(flags & TRANSPORT_UNLOCK_PACK_IN_SIGNAL_HANDLER);
1562 int i;
1564 for (i = 0; i < transport->pack_lockfiles.nr; i++)
1565 if (in_signal_handler)
1566 unlink(transport->pack_lockfiles.items[i].string);
1567 else
1568 unlink_or_warn(transport->pack_lockfiles.items[i].string);
1569 if (!in_signal_handler)
1570 string_list_clear(&transport->pack_lockfiles, 0);
1573 int transport_connect(struct transport *transport, const char *name,
1574 const char *exec, int fd[2])
1576 if (transport->vtable->connect)
1577 return transport->vtable->connect(transport, name, exec, fd);
1578 else
1579 die(_("operation not supported by protocol"));
1582 int transport_disconnect(struct transport *transport)
1584 int ret = 0;
1585 if (transport->vtable->disconnect)
1586 ret = transport->vtable->disconnect(transport);
1587 if (transport->got_remote_refs)
1588 free_refs((void *)transport->remote_refs);
1589 clear_bundle_list(transport->bundles);
1590 free(transport->bundles);
1591 free(transport);
1592 return ret;
1596 * Strip username (and password) from a URL and return
1597 * it in a newly allocated string.
1599 char *transport_anonymize_url(const char *url)
1601 char *scheme_prefix, *anon_part;
1602 size_t anon_len, prefix_len = 0;
1604 anon_part = strchr(url, '@');
1605 if (url_is_local_not_ssh(url) || !anon_part)
1606 goto literal_copy;
1608 anon_len = strlen(++anon_part);
1609 scheme_prefix = strstr(url, "://");
1610 if (!scheme_prefix) {
1611 if (!strchr(anon_part, ':'))
1612 /* cannot be "me@there:/path/name" */
1613 goto literal_copy;
1614 } else {
1615 const char *cp;
1616 /* make sure scheme is reasonable */
1617 for (cp = url; cp < scheme_prefix; cp++) {
1618 switch (*cp) {
1619 /* RFC 1738 2.1 */
1620 case '+': case '.': case '-':
1621 break; /* ok */
1622 default:
1623 if (isalnum(*cp))
1624 break;
1625 /* it isn't */
1626 goto literal_copy;
1629 /* @ past the first slash does not count */
1630 cp = strchr(scheme_prefix + 3, '/');
1631 if (cp && cp < anon_part)
1632 goto literal_copy;
1633 prefix_len = scheme_prefix - url + 3;
1635 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1636 (int)anon_len, anon_part);
1637 literal_copy:
1638 return xstrdup(url);