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