The eleventh batch
[git/gitster.git] / transport.c
blob47fda6a7732f4b8cdcb6e750f36b896a988ffd0b
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "advice.h"
5 #include "config.h"
6 #include "environment.h"
7 #include "hex.h"
8 #include "transport.h"
9 #include "hook.h"
10 #include "pkt-line.h"
11 #include "fetch-pack.h"
12 #include "remote.h"
13 #include "connect.h"
14 #include "send-pack.h"
15 #include "bundle.h"
16 #include "gettext.h"
17 #include "refs.h"
18 #include "refspec.h"
19 #include "branch.h"
20 #include "url.h"
21 #include "submodule.h"
22 #include "string-list.h"
23 #include "oid-array.h"
24 #include "sigchain.h"
25 #include "trace2.h"
26 #include "transport-internal.h"
27 #include "protocol.h"
28 #include "object-name.h"
29 #include "color.h"
30 #include "bundle-uri.h"
32 static int transport_use_color = -1;
33 static char transport_colors[][COLOR_MAXLEN] = {
34 GIT_COLOR_RESET,
35 GIT_COLOR_RED /* REJECTED */
38 enum color_transport {
39 TRANSPORT_COLOR_RESET = 0,
40 TRANSPORT_COLOR_REJECTED = 1
43 static int transport_color_config(void)
45 const char *keys[] = {
46 "color.transport.reset",
47 "color.transport.rejected"
48 }, *key = "color.transport";
49 char *value;
50 int i;
51 static int initialized;
53 if (initialized)
54 return 0;
55 initialized = 1;
57 if (!git_config_get_string(key, &value))
58 transport_use_color = git_config_colorbool(key, value);
60 if (!want_color_stderr(transport_use_color))
61 return 0;
63 for (i = 0; i < ARRAY_SIZE(keys); i++)
64 if (!git_config_get_string(keys[i], &value)) {
65 if (!value)
66 return config_error_nonbool(keys[i]);
67 if (color_parse(value, transport_colors[i]) < 0)
68 return -1;
71 return 0;
74 static const char *transport_get_color(enum color_transport ix)
76 if (want_color_stderr(transport_use_color))
77 return transport_colors[ix];
78 return "";
81 static void set_upstreams(struct transport *transport, struct ref *refs,
82 int pretend)
84 struct ref *ref;
85 for (ref = refs; ref; ref = ref->next) {
86 const char *localname;
87 const char *tmp;
88 const char *remotename;
89 int flag = 0;
91 * Check suitability for tracking. Must be successful /
92 * already up-to-date ref create/modify (not delete).
94 if (ref->status != REF_STATUS_OK &&
95 ref->status != REF_STATUS_UPTODATE)
96 continue;
97 if (!ref->peer_ref)
98 continue;
99 if (is_null_oid(&ref->new_oid))
100 continue;
102 /* Follow symbolic refs (mainly for HEAD). */
103 localname = ref->peer_ref->name;
104 remotename = ref->name;
105 tmp = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
106 localname, RESOLVE_REF_READING,
107 NULL, &flag);
108 if (tmp && flag & REF_ISSYMREF &&
109 starts_with(tmp, "refs/heads/"))
110 localname = tmp;
112 /* Both source and destination must be local branches. */
113 if (!localname || !starts_with(localname, "refs/heads/"))
114 continue;
115 if (!remotename || !starts_with(remotename, "refs/heads/"))
116 continue;
118 if (!pretend) {
119 int flag = transport->verbose < 0 ? 0 : BRANCH_CONFIG_VERBOSE;
120 install_branch_config(flag, localname + 11,
121 transport->remote->name, remotename);
122 } else if (transport->verbose >= 0)
123 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
124 localname + 11, remotename + 11,
125 transport->remote->name);
129 struct bundle_transport_data {
130 int fd;
131 struct bundle_header header;
132 unsigned get_refs_from_bundle_called : 1;
135 static void get_refs_from_bundle_inner(struct transport *transport)
137 struct bundle_transport_data *data = transport->data;
139 data->get_refs_from_bundle_called = 1;
141 if (data->fd > 0)
142 close(data->fd);
143 data->fd = read_bundle_header(transport->url, &data->header);
144 if (data->fd < 0)
145 die(_("could not read bundle '%s'"), transport->url);
147 transport->hash_algo = data->header.hash_algo;
150 static struct ref *get_refs_from_bundle(struct transport *transport,
151 int for_push,
152 struct transport_ls_refs_options *transport_options UNUSED)
154 struct bundle_transport_data *data = transport->data;
155 struct ref *result = NULL;
156 int i;
158 if (for_push)
159 return NULL;
161 get_refs_from_bundle_inner(transport);
163 for (i = 0; i < data->header.references.nr; i++) {
164 struct string_list_item *e = data->header.references.items + i;
165 const char *name = e->string;
166 struct ref *ref = alloc_ref(name);
167 struct object_id *oid = e->util;
168 oidcpy(&ref->old_oid, oid);
169 ref->next = result;
170 result = ref;
172 return result;
175 static int fetch_refs_from_bundle(struct transport *transport,
176 int nr_heads UNUSED,
177 struct ref **to_fetch UNUSED)
179 struct bundle_transport_data *data = transport->data;
180 struct strvec extra_index_pack_args = STRVEC_INIT;
181 int ret;
183 if (transport->progress)
184 strvec_push(&extra_index_pack_args, "-v");
186 if (!data->get_refs_from_bundle_called)
187 get_refs_from_bundle_inner(transport);
188 ret = unbundle(the_repository, &data->header, data->fd,
189 &extra_index_pack_args,
190 fetch_pack_fsck_objects() ? VERIFY_BUNDLE_FSCK : 0);
191 transport->hash_algo = data->header.hash_algo;
193 strvec_clear(&extra_index_pack_args);
194 return ret;
197 static int close_bundle(struct transport *transport)
199 struct bundle_transport_data *data = transport->data;
200 if (data->fd > 0)
201 close(data->fd);
202 bundle_header_release(&data->header);
203 free(data);
204 return 0;
207 struct git_transport_data {
208 struct git_transport_options options;
209 struct child_process *conn;
210 int fd[2];
211 unsigned finished_handshake : 1;
212 enum protocol_version version;
213 struct oid_array extra_have;
214 struct oid_array shallow;
217 static int set_git_option(struct git_transport_options *opts,
218 const char *name, const char *value)
220 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
221 opts->uploadpack = value;
222 return 0;
223 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
224 opts->receivepack = value;
225 return 0;
226 } else if (!strcmp(name, TRANS_OPT_THIN)) {
227 opts->thin = !!value;
228 return 0;
229 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
230 opts->followtags = !!value;
231 return 0;
232 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
233 opts->keep = !!value;
234 return 0;
235 } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
236 opts->update_shallow = !!value;
237 return 0;
238 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
239 if (!value)
240 opts->depth = 0;
241 else {
242 char *end;
243 opts->depth = strtol(value, &end, 0);
244 if (*end)
245 die(_("transport: invalid depth option '%s'"), value);
247 return 0;
248 } else if (!strcmp(name, TRANS_OPT_DEEPEN_SINCE)) {
249 opts->deepen_since = value;
250 return 0;
251 } else if (!strcmp(name, TRANS_OPT_DEEPEN_NOT)) {
252 opts->deepen_not = (const struct string_list *)value;
253 return 0;
254 } else if (!strcmp(name, TRANS_OPT_DEEPEN_RELATIVE)) {
255 opts->deepen_relative = !!value;
256 return 0;
257 } else if (!strcmp(name, TRANS_OPT_FROM_PROMISOR)) {
258 opts->from_promisor = !!value;
259 return 0;
260 } else if (!strcmp(name, TRANS_OPT_LIST_OBJECTS_FILTER)) {
261 list_objects_filter_die_if_populated(&opts->filter_options);
262 parse_list_objects_filter(&opts->filter_options, value);
263 return 0;
264 } else if (!strcmp(name, TRANS_OPT_REFETCH)) {
265 opts->refetch = !!value;
266 return 0;
267 } else if (!strcmp(name, TRANS_OPT_REJECT_SHALLOW)) {
268 opts->reject_shallow = !!value;
269 return 0;
271 return 1;
274 static int connect_setup(struct transport *transport, int for_push)
276 struct git_transport_data *data = transport->data;
277 int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
279 if (data->conn)
280 return 0;
282 switch (transport->family) {
283 case TRANSPORT_FAMILY_ALL: break;
284 case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
285 case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
288 data->conn = git_connect(data->fd, transport->url,
289 for_push ?
290 "git-receive-pack" :
291 "git-upload-pack",
292 for_push ?
293 data->options.receivepack :
294 data->options.uploadpack,
295 flags);
297 return 0;
300 static void die_if_server_options(struct transport *transport)
302 if (!transport->server_options || !transport->server_options->nr)
303 return;
304 advise(_("see protocol.version in 'git help config' for more details"));
305 die(_("server options require protocol version 2 or later"));
309 * Obtains the protocol version from the transport and writes it to
310 * transport->data->version, first connecting if not already connected.
312 * If the protocol version is one that allows skipping the listing of remote
313 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
314 * this function returns NULL. Otherwise, this function returns the list of
315 * remote refs.
317 static struct ref *handshake(struct transport *transport, int for_push,
318 struct transport_ls_refs_options *options,
319 int must_list_refs)
321 struct git_transport_data *data = transport->data;
322 struct ref *refs = NULL;
323 struct packet_reader reader;
324 size_t sid_len;
325 const char *server_sid;
327 connect_setup(transport, for_push);
329 packet_reader_init(&reader, data->fd[0], NULL, 0,
330 PACKET_READ_CHOMP_NEWLINE |
331 PACKET_READ_GENTLE_ON_EOF |
332 PACKET_READ_DIE_ON_ERR_PACKET);
334 data->version = discover_version(&reader);
335 switch (data->version) {
336 case protocol_v2:
337 if ((!transport->server_options || !transport->server_options->nr) &&
338 transport->remote->server_options.nr)
339 transport->server_options = &transport->remote->server_options;
340 if (server_feature_v2("session-id", &server_sid))
341 trace2_data_string("transfer", NULL, "server-sid", server_sid);
342 if (must_list_refs)
343 get_remote_refs(data->fd[1], &reader, &refs, for_push,
344 options,
345 transport->server_options,
346 transport->stateless_rpc);
347 break;
348 case protocol_v1:
349 case protocol_v0:
350 die_if_server_options(transport);
351 get_remote_heads(&reader, &refs,
352 for_push ? REF_NORMAL : 0,
353 &data->extra_have,
354 &data->shallow);
355 server_sid = server_feature_value("session-id", &sid_len);
356 if (server_sid) {
357 char *sid = xstrndup(server_sid, sid_len);
358 trace2_data_string("transfer", NULL, "server-sid", sid);
359 free(sid);
361 break;
362 case protocol_unknown_version:
363 BUG("unknown protocol version");
365 data->finished_handshake = 1;
366 transport->hash_algo = reader.hash_algo;
368 if (reader.line_peeked)
369 BUG("buffer must be empty at the end of handshake()");
371 return refs;
374 static struct ref *get_refs_via_connect(struct transport *transport, int for_push,
375 struct transport_ls_refs_options *options)
377 return handshake(transport, for_push, options, 1);
380 static int get_bundle_uri(struct transport *transport)
382 struct git_transport_data *data = transport->data;
383 struct packet_reader reader;
384 int stateless_rpc = transport->stateless_rpc;
386 if (!transport->bundles) {
387 CALLOC_ARRAY(transport->bundles, 1);
388 init_bundle_list(transport->bundles);
391 if (!data->finished_handshake) {
392 struct ref *refs = handshake(transport, 0, NULL, 0);
394 if (refs)
395 free_refs(refs);
399 * "Support" protocol v0 and v2 without bundle-uri support by
400 * silently degrading to a NOOP.
402 if (!server_supports_v2("bundle-uri"))
403 return 0;
405 packet_reader_init(&reader, data->fd[0], NULL, 0,
406 PACKET_READ_CHOMP_NEWLINE |
407 PACKET_READ_GENTLE_ON_EOF);
409 return get_remote_bundle_uri(data->fd[1], &reader,
410 transport->bundles, stateless_rpc);
413 static int fetch_refs_via_pack(struct transport *transport,
414 int nr_heads, struct ref **to_fetch)
416 int ret = 0;
417 struct git_transport_data *data = transport->data;
418 struct ref *refs = NULL;
419 struct fetch_pack_args args;
420 struct ref *refs_tmp = NULL, **to_fetch_dup = NULL;
422 memset(&args, 0, sizeof(args));
423 args.uploadpack = data->options.uploadpack;
424 args.keep_pack = data->options.keep;
425 args.lock_pack = 1;
426 args.use_thin_pack = data->options.thin;
427 args.include_tag = data->options.followtags;
428 args.verbose = (transport->verbose > 1);
429 args.quiet = (transport->verbose < 0);
430 args.no_progress = !transport->progress;
431 args.depth = data->options.depth;
432 args.deepen_since = data->options.deepen_since;
433 args.deepen_not = data->options.deepen_not;
434 args.deepen_relative = data->options.deepen_relative;
435 args.check_self_contained_and_connected =
436 data->options.check_self_contained_and_connected;
437 args.cloning = transport->cloning;
438 args.update_shallow = data->options.update_shallow;
439 args.from_promisor = data->options.from_promisor;
440 list_objects_filter_copy(&args.filter_options,
441 &data->options.filter_options);
442 args.refetch = data->options.refetch;
443 args.stateless_rpc = transport->stateless_rpc;
444 args.server_options = transport->server_options;
445 args.negotiation_tips = data->options.negotiation_tips;
446 args.reject_shallow_remote = transport->smart_options->reject_shallow;
448 if (!data->finished_handshake) {
449 int i;
450 int must_list_refs = 0;
451 for (i = 0; i < nr_heads; i++) {
452 if (!to_fetch[i]->exact_oid) {
453 must_list_refs = 1;
454 break;
457 refs_tmp = handshake(transport, 0, NULL, must_list_refs);
460 if (data->version == protocol_unknown_version)
461 BUG("unknown protocol version");
462 else if (data->version <= protocol_v1)
463 die_if_server_options(transport);
465 if (data->options.acked_commits) {
466 if (data->version < protocol_v2) {
467 warning(_("--negotiate-only requires protocol v2"));
468 ret = -1;
469 } else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
470 warning(_("server does not support wait-for-done"));
471 ret = -1;
472 } else {
473 negotiate_using_fetch(data->options.negotiation_tips,
474 transport->server_options,
475 transport->stateless_rpc,
476 data->fd,
477 data->options.acked_commits);
478 ret = 0;
480 goto cleanup;
484 * Create a shallow copy of `sought` so that we can free all of its entries.
485 * This is because `fetch_pack()` will modify the array to evict some
486 * entries, but won't free those.
488 DUP_ARRAY(to_fetch_dup, to_fetch, nr_heads);
489 to_fetch = to_fetch_dup;
491 refs = fetch_pack(&args, data->fd,
492 refs_tmp ? refs_tmp : transport->remote_refs,
493 to_fetch, nr_heads, &data->shallow,
494 &transport->pack_lockfiles, data->version);
496 data->finished_handshake = 0;
497 data->options.self_contained_and_connected =
498 args.self_contained_and_connected;
499 data->options.connectivity_checked = args.connectivity_checked;
501 if (!refs)
502 ret = -1;
503 if (report_unmatched_refs(to_fetch, nr_heads))
504 ret = -1;
506 cleanup:
507 close(data->fd[0]);
508 if (data->fd[1] >= 0)
509 close(data->fd[1]);
510 if (finish_connect(data->conn))
511 ret = -1;
512 data->conn = NULL;
514 free(to_fetch_dup);
515 free_refs(refs_tmp);
516 free_refs(refs);
517 list_objects_filter_release(&args.filter_options);
518 return ret;
521 static int push_had_errors(struct ref *ref)
523 for (; ref; ref = ref->next) {
524 switch (ref->status) {
525 case REF_STATUS_NONE:
526 case REF_STATUS_UPTODATE:
527 case REF_STATUS_OK:
528 break;
529 default:
530 return 1;
533 return 0;
536 int transport_refs_pushed(struct ref *ref)
538 for (; ref; ref = ref->next) {
539 switch(ref->status) {
540 case REF_STATUS_NONE:
541 case REF_STATUS_UPTODATE:
542 break;
543 default:
544 return 1;
547 return 0;
550 static void update_one_tracking_ref(struct remote *remote, char *refname,
551 struct object_id *new_oid, int deletion,
552 int verbose)
554 struct refspec_item rs;
556 memset(&rs, 0, sizeof(rs));
557 rs.src = refname;
558 rs.dst = NULL;
560 if (!remote_find_tracking(remote, &rs)) {
561 if (verbose)
562 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
563 if (deletion)
564 refs_delete_ref(get_main_ref_store(the_repository),
565 NULL, rs.dst, NULL, 0);
566 else
567 refs_update_ref(get_main_ref_store(the_repository),
568 "update by push", rs.dst, new_oid,
569 NULL, 0, 0);
570 free(rs.dst);
574 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
576 char *refname;
577 struct object_id *new_oid;
578 struct ref_push_report *report;
580 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
581 return;
583 report = ref->report;
584 if (!report)
585 update_one_tracking_ref(remote, ref->name, &ref->new_oid,
586 ref->deletion, verbose);
587 else
588 for (; report; report = report->next) {
589 refname = report->ref_name ? (char *)report->ref_name : ref->name;
590 new_oid = report->new_oid ? report->new_oid : &ref->new_oid;
591 update_one_tracking_ref(remote, refname, new_oid,
592 is_null_oid(new_oid), verbose);
596 static void print_ref_status(char flag, const char *summary,
597 struct ref *to, struct ref *from, const char *msg,
598 struct ref_push_report *report,
599 int porcelain, int summary_width)
601 const char *to_name;
603 if (report && report->ref_name)
604 to_name = report->ref_name;
605 else
606 to_name = to->name;
608 if (porcelain) {
609 if (from)
610 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to_name);
611 else
612 fprintf(stdout, "%c\t:%s\t", flag, to_name);
613 if (msg)
614 fprintf(stdout, "%s (%s)\n", summary, msg);
615 else
616 fprintf(stdout, "%s\n", summary);
617 } else {
618 const char *red = "", *reset = "";
619 if (push_had_errors(to)) {
620 red = transport_get_color(TRANSPORT_COLOR_REJECTED);
621 reset = transport_get_color(TRANSPORT_COLOR_RESET);
623 fprintf(stderr, " %s%c %-*s%s ", red, flag, summary_width,
624 summary, reset);
625 if (from)
626 fprintf(stderr, "%s -> %s",
627 prettify_refname(from->name),
628 prettify_refname(to_name));
629 else
630 fputs(prettify_refname(to_name), stderr);
631 if (msg) {
632 fputs(" (", stderr);
633 fputs(msg, stderr);
634 fputc(')', stderr);
636 fputc('\n', stderr);
640 static void print_ok_ref_status(struct ref *ref,
641 struct ref_push_report *report,
642 int porcelain, int summary_width)
644 struct object_id *old_oid;
645 struct object_id *new_oid;
646 const char *ref_name;
647 int forced_update;
649 if (report && report->old_oid)
650 old_oid = report->old_oid;
651 else
652 old_oid = &ref->old_oid;
653 if (report && report->new_oid)
654 new_oid = report->new_oid;
655 else
656 new_oid = &ref->new_oid;
657 if (report && report->forced_update)
658 forced_update = report->forced_update;
659 else
660 forced_update = ref->forced_update;
661 if (report && report->ref_name)
662 ref_name = report->ref_name;
663 else
664 ref_name = ref->name;
666 if (ref->deletion)
667 print_ref_status('-', "[deleted]", ref, NULL, NULL,
668 report, porcelain, summary_width);
669 else if (is_null_oid(old_oid))
670 print_ref_status('*',
671 (starts_with(ref_name, "refs/tags/")
672 ? "[new tag]"
673 : (starts_with(ref_name, "refs/heads/")
674 ? "[new branch]"
675 : "[new reference]")),
676 ref, ref->peer_ref, NULL,
677 report, porcelain, summary_width);
678 else {
679 struct strbuf quickref = STRBUF_INIT;
680 char type;
681 const char *msg;
683 strbuf_add_unique_abbrev(&quickref, old_oid,
684 DEFAULT_ABBREV);
685 if (forced_update) {
686 strbuf_addstr(&quickref, "...");
687 type = '+';
688 msg = "forced update";
689 } else {
690 strbuf_addstr(&quickref, "..");
691 type = ' ';
692 msg = NULL;
694 strbuf_add_unique_abbrev(&quickref, new_oid,
695 DEFAULT_ABBREV);
697 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
698 report, porcelain, summary_width);
699 strbuf_release(&quickref);
703 static int print_one_push_report(struct ref *ref, const char *dest, int count,
704 struct ref_push_report *report,
705 int porcelain, int summary_width)
707 if (!count) {
708 char *url = transport_anonymize_url(dest);
709 fprintf(porcelain ? stdout : stderr, "To %s\n", url);
710 free(url);
713 switch(ref->status) {
714 case REF_STATUS_NONE:
715 print_ref_status('X', "[no match]", ref, NULL, NULL,
716 report, porcelain, summary_width);
717 break;
718 case REF_STATUS_REJECT_NODELETE:
719 print_ref_status('!', "[rejected]", ref, NULL,
720 "remote does not support deleting refs",
721 report, porcelain, summary_width);
722 break;
723 case REF_STATUS_UPTODATE:
724 print_ref_status('=', "[up to date]", ref,
725 ref->peer_ref, NULL,
726 report, porcelain, summary_width);
727 break;
728 case REF_STATUS_REJECT_NONFASTFORWARD:
729 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
730 "non-fast-forward",
731 report, porcelain, summary_width);
732 break;
733 case REF_STATUS_REJECT_ALREADY_EXISTS:
734 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
735 "already exists",
736 report, porcelain, summary_width);
737 break;
738 case REF_STATUS_REJECT_FETCH_FIRST:
739 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
740 "fetch first",
741 report, porcelain, summary_width);
742 break;
743 case REF_STATUS_REJECT_NEEDS_FORCE:
744 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
745 "needs force",
746 report, porcelain, summary_width);
747 break;
748 case REF_STATUS_REJECT_STALE:
749 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
750 "stale info",
751 report, porcelain, summary_width);
752 break;
753 case REF_STATUS_REJECT_REMOTE_UPDATED:
754 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
755 "remote ref updated since checkout",
756 report, porcelain, summary_width);
757 break;
758 case REF_STATUS_REJECT_SHALLOW:
759 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
760 "new shallow roots not allowed",
761 report, porcelain, summary_width);
762 break;
763 case REF_STATUS_REMOTE_REJECT:
764 print_ref_status('!', "[remote rejected]", ref,
765 ref->deletion ? NULL : ref->peer_ref,
766 ref->remote_status,
767 report, porcelain, summary_width);
768 break;
769 case REF_STATUS_EXPECTING_REPORT:
770 print_ref_status('!', "[remote failure]", ref,
771 ref->deletion ? NULL : ref->peer_ref,
772 "remote failed to report status",
773 report, porcelain, summary_width);
774 break;
775 case REF_STATUS_ATOMIC_PUSH_FAILED:
776 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
777 "atomic push failed",
778 report, porcelain, summary_width);
779 break;
780 case REF_STATUS_OK:
781 print_ok_ref_status(ref, report, porcelain, summary_width);
782 break;
785 return 1;
788 static int print_one_push_status(struct ref *ref, const char *dest, int count,
789 int porcelain, int summary_width)
791 struct ref_push_report *report;
792 int n = 0;
794 if (!ref->report)
795 return print_one_push_report(ref, dest, count,
796 NULL, porcelain, summary_width);
798 for (report = ref->report; report; report = report->next)
799 print_one_push_report(ref, dest, count + n++,
800 report, porcelain, summary_width);
801 return n;
804 static int measure_abbrev(const struct object_id *oid, int sofar)
806 char hex[GIT_MAX_HEXSZ + 1];
807 int w = repo_find_unique_abbrev_r(the_repository, hex, oid,
808 DEFAULT_ABBREV);
810 return (w < sofar) ? sofar : w;
813 int transport_summary_width(const struct ref *refs)
815 int maxw = -1;
817 for (; refs; refs = refs->next) {
818 maxw = measure_abbrev(&refs->old_oid, maxw);
819 maxw = measure_abbrev(&refs->new_oid, maxw);
821 if (maxw < 0)
822 maxw = FALLBACK_DEFAULT_ABBREV;
823 return (2 * maxw + 3);
826 void transport_print_push_status(const char *dest, struct ref *refs,
827 int verbose, int porcelain, unsigned int *reject_reasons)
829 struct ref *ref;
830 int n = 0;
831 char *head;
832 int summary_width = transport_summary_width(refs);
834 if (transport_color_config() < 0)
835 warning(_("could not parse transport.color.* config"));
837 head = refs_resolve_refdup(get_main_ref_store(the_repository), "HEAD",
838 RESOLVE_REF_READING, NULL, NULL);
840 if (verbose) {
841 for (ref = refs; ref; ref = ref->next)
842 if (ref->status == REF_STATUS_UPTODATE)
843 n += print_one_push_status(ref, dest, n,
844 porcelain, summary_width);
847 for (ref = refs; ref; ref = ref->next)
848 if (ref->status == REF_STATUS_OK)
849 n += print_one_push_status(ref, dest, n,
850 porcelain, summary_width);
852 *reject_reasons = 0;
853 for (ref = refs; ref; ref = ref->next) {
854 if (ref->status != REF_STATUS_NONE &&
855 ref->status != REF_STATUS_UPTODATE &&
856 ref->status != REF_STATUS_OK)
857 n += print_one_push_status(ref, dest, n,
858 porcelain, summary_width);
859 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
860 if (head != NULL && !strcmp(head, ref->name))
861 *reject_reasons |= REJECT_NON_FF_HEAD;
862 else
863 *reject_reasons |= REJECT_NON_FF_OTHER;
864 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
865 *reject_reasons |= REJECT_ALREADY_EXISTS;
866 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
867 *reject_reasons |= REJECT_FETCH_FIRST;
868 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
869 *reject_reasons |= REJECT_NEEDS_FORCE;
870 } else if (ref->status == REF_STATUS_REJECT_REMOTE_UPDATED) {
871 *reject_reasons |= REJECT_REF_NEEDS_UPDATE;
874 free(head);
877 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
879 struct git_transport_data *data = transport->data;
880 struct send_pack_args args;
881 int ret = 0;
883 if (transport_color_config() < 0)
884 return -1;
886 if (!data->finished_handshake)
887 get_refs_via_connect(transport, 1, NULL);
889 memset(&args, 0, sizeof(args));
890 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
891 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
892 args.use_thin_pack = data->options.thin;
893 args.verbose = (transport->verbose > 0);
894 args.quiet = (transport->verbose < 0);
895 args.progress = transport->progress;
896 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
897 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
898 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
899 args.push_options = transport->push_options;
900 args.url = transport->url;
902 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
903 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
904 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
905 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
906 else
907 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
909 switch (data->version) {
910 case protocol_v2:
911 die(_("support for protocol v2 not implemented yet"));
912 break;
913 case protocol_v1:
914 case protocol_v0:
915 ret = send_pack(&args, data->fd, data->conn, remote_refs,
916 &data->extra_have);
917 break;
918 case protocol_unknown_version:
919 BUG("unknown protocol version");
922 close(data->fd[1]);
923 close(data->fd[0]);
925 * Atomic push may abort the connection early and close the pipe,
926 * which may cause an error for `finish_connect()`. Ignore this error
927 * for atomic git-push.
929 if (ret || args.atomic)
930 finish_connect(data->conn);
931 else
932 ret = finish_connect(data->conn);
933 data->conn = NULL;
934 data->finished_handshake = 0;
936 return ret;
939 static int connect_git(struct transport *transport, const char *name,
940 const char *executable, int fd[2])
942 struct git_transport_data *data = transport->data;
943 data->conn = git_connect(data->fd, transport->url,
944 name, executable, 0);
945 fd[0] = data->fd[0];
946 fd[1] = data->fd[1];
947 return 0;
950 static int disconnect_git(struct transport *transport)
952 struct git_transport_data *data = transport->data;
953 if (data->conn) {
954 if (data->finished_handshake && !transport->stateless_rpc)
955 packet_flush(data->fd[1]);
956 close(data->fd[0]);
957 if (data->fd[1] >= 0)
958 close(data->fd[1]);
959 finish_connect(data->conn);
962 if (data->options.negotiation_tips) {
963 oid_array_clear(data->options.negotiation_tips);
964 free(data->options.negotiation_tips);
966 list_objects_filter_release(&data->options.filter_options);
967 oid_array_clear(&data->extra_have);
968 oid_array_clear(&data->shallow);
969 free(data);
970 return 0;
973 static struct transport_vtable taken_over_vtable = {
974 .get_refs_list = get_refs_via_connect,
975 .get_bundle_uri = get_bundle_uri,
976 .fetch_refs = fetch_refs_via_pack,
977 .push_refs = git_transport_push,
978 .disconnect = disconnect_git
981 void transport_take_over(struct transport *transport,
982 struct child_process *child)
984 struct git_transport_data *data;
986 if (!transport->smart_options)
987 BUG("taking over transport requires non-NULL "
988 "smart_options field.");
990 CALLOC_ARRAY(data, 1);
991 data->options = *transport->smart_options;
992 data->conn = child;
993 data->fd[0] = data->conn->out;
994 data->fd[1] = data->conn->in;
995 data->finished_handshake = 0;
996 transport->data = data;
998 transport->vtable = &taken_over_vtable;
999 transport->smart_options = &(data->options);
1001 transport->cannot_reuse = 1;
1004 static int is_file(const char *url)
1006 struct stat buf;
1007 if (stat(url, &buf))
1008 return 0;
1009 return S_ISREG(buf.st_mode);
1012 static int external_specification_len(const char *url)
1014 return strchr(url, ':') - url;
1017 static const struct string_list *protocol_allow_list(void)
1019 static int enabled = -1;
1020 static struct string_list allowed = STRING_LIST_INIT_DUP;
1022 if (enabled < 0) {
1023 const char *v = getenv("GIT_ALLOW_PROTOCOL");
1024 if (v) {
1025 string_list_split(&allowed, v, ':', -1);
1026 string_list_sort(&allowed);
1027 enabled = 1;
1028 } else {
1029 enabled = 0;
1033 return enabled ? &allowed : NULL;
1036 enum protocol_allow_config {
1037 PROTOCOL_ALLOW_NEVER = 0,
1038 PROTOCOL_ALLOW_USER_ONLY,
1039 PROTOCOL_ALLOW_ALWAYS
1042 static enum protocol_allow_config parse_protocol_config(const char *key,
1043 const char *value)
1045 if (!strcasecmp(value, "always"))
1046 return PROTOCOL_ALLOW_ALWAYS;
1047 else if (!strcasecmp(value, "never"))
1048 return PROTOCOL_ALLOW_NEVER;
1049 else if (!strcasecmp(value, "user"))
1050 return PROTOCOL_ALLOW_USER_ONLY;
1052 die(_("unknown value for config '%s': %s"), key, value);
1055 static enum protocol_allow_config get_protocol_config(const char *type)
1057 char *key = xstrfmt("protocol.%s.allow", type);
1058 char *value;
1060 /* first check the per-protocol config */
1061 if (!git_config_get_string(key, &value)) {
1062 enum protocol_allow_config ret =
1063 parse_protocol_config(key, value);
1064 free(key);
1065 free(value);
1066 return ret;
1068 free(key);
1070 /* if defined, fallback to user-defined default for unknown protocols */
1071 if (!git_config_get_string("protocol.allow", &value)) {
1072 enum protocol_allow_config ret =
1073 parse_protocol_config("protocol.allow", value);
1074 free(value);
1075 return ret;
1078 /* fallback to built-in defaults */
1079 /* known safe */
1080 if (!strcmp(type, "http") ||
1081 !strcmp(type, "https") ||
1082 !strcmp(type, "git") ||
1083 !strcmp(type, "ssh"))
1084 return PROTOCOL_ALLOW_ALWAYS;
1086 /* known scary; err on the side of caution */
1087 if (!strcmp(type, "ext"))
1088 return PROTOCOL_ALLOW_NEVER;
1090 /* unknown; by default let them be used only directly by the user */
1091 return PROTOCOL_ALLOW_USER_ONLY;
1094 int is_transport_allowed(const char *type, int from_user)
1096 const struct string_list *allow_list = protocol_allow_list();
1097 if (allow_list)
1098 return string_list_has_string(allow_list, type);
1100 switch (get_protocol_config(type)) {
1101 case PROTOCOL_ALLOW_ALWAYS:
1102 return 1;
1103 case PROTOCOL_ALLOW_NEVER:
1104 return 0;
1105 case PROTOCOL_ALLOW_USER_ONLY:
1106 if (from_user < 0)
1107 from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1108 return from_user;
1111 BUG("invalid protocol_allow_config type");
1114 int parse_transport_option(const char *var, const char *value,
1115 struct string_list *transport_options)
1117 if (!value)
1118 return config_error_nonbool(var);
1119 if (!*value)
1120 string_list_clear(transport_options, 0);
1121 else
1122 string_list_append(transport_options, value);
1123 return 0;
1126 void transport_check_allowed(const char *type)
1128 if (!is_transport_allowed(type, -1))
1129 die(_("transport '%s' not allowed"), type);
1132 static struct transport_vtable bundle_vtable = {
1133 .get_refs_list = get_refs_from_bundle,
1134 .fetch_refs = fetch_refs_from_bundle,
1135 .disconnect = close_bundle
1138 static struct transport_vtable builtin_smart_vtable = {
1139 .get_refs_list = get_refs_via_connect,
1140 .get_bundle_uri = get_bundle_uri,
1141 .fetch_refs = fetch_refs_via_pack,
1142 .push_refs = git_transport_push,
1143 .connect = connect_git,
1144 .disconnect = disconnect_git
1147 struct transport *transport_get(struct remote *remote, const char *url)
1149 const char *helper;
1150 char *helper_to_free = NULL;
1151 const char *p;
1152 struct transport *ret = xcalloc(1, sizeof(*ret));
1154 ret->progress = isatty(2);
1155 string_list_init_dup(&ret->pack_lockfiles);
1157 CALLOC_ARRAY(ret->bundles, 1);
1158 init_bundle_list(ret->bundles);
1160 if (!remote)
1161 BUG("No remote provided to transport_get()");
1163 ret->got_remote_refs = 0;
1164 ret->remote = remote;
1165 helper = remote->foreign_vcs;
1167 if (!url)
1168 url = remote->url.v[0];
1169 ret->url = url;
1171 p = url;
1172 while (is_urlschemechar(p == url, *p))
1173 p++;
1174 if (starts_with(p, "::"))
1175 helper = helper_to_free = xstrndup(url, p - url);
1177 if (helper) {
1178 transport_helper_init(ret, helper);
1179 free(helper_to_free);
1180 } else if (starts_with(url, "rsync:")) {
1181 die(_("git-over-rsync is no longer supported"));
1182 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
1183 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
1184 bundle_header_init(&data->header);
1185 transport_check_allowed("file");
1186 ret->data = data;
1187 ret->vtable = &bundle_vtable;
1188 ret->smart_options = NULL;
1189 } else if (!is_url(url)
1190 || starts_with(url, "file://")
1191 || starts_with(url, "git://")
1192 || starts_with(url, "ssh://")
1193 || starts_with(url, "git+ssh://") /* deprecated - do not use */
1194 || starts_with(url, "ssh+git://") /* deprecated - do not use */
1197 * These are builtin smart transports; "allowed" transports
1198 * will be checked individually in git_connect.
1200 struct git_transport_data *data = xcalloc(1, sizeof(*data));
1201 list_objects_filter_init(&data->options.filter_options);
1202 ret->data = data;
1203 ret->vtable = &builtin_smart_vtable;
1204 ret->smart_options = &(data->options);
1206 data->conn = NULL;
1207 data->finished_handshake = 0;
1208 } else {
1209 /* Unknown protocol in URL. Pass to external handler. */
1210 int len = external_specification_len(url);
1211 char *handler = xmemdupz(url, len);
1212 transport_helper_init(ret, handler);
1213 free(handler);
1216 if (ret->smart_options) {
1217 ret->smart_options->thin = 1;
1218 ret->smart_options->uploadpack = "git-upload-pack";
1219 if (remote->uploadpack)
1220 ret->smart_options->uploadpack = remote->uploadpack;
1221 ret->smart_options->receivepack = "git-receive-pack";
1222 if (remote->receivepack)
1223 ret->smart_options->receivepack = remote->receivepack;
1226 ret->hash_algo = &hash_algos[GIT_HASH_SHA1];
1228 return ret;
1231 const struct git_hash_algo *transport_get_hash_algo(struct transport *transport)
1233 return transport->hash_algo;
1236 int transport_set_option(struct transport *transport,
1237 const char *name, const char *value)
1239 int git_reports = 1, protocol_reports = 1;
1241 if (transport->smart_options)
1242 git_reports = set_git_option(transport->smart_options,
1243 name, value);
1245 if (transport->vtable->set_option)
1246 protocol_reports = transport->vtable->set_option(transport,
1247 name, value);
1249 /* If either report is 0, report 0 (success). */
1250 if (!git_reports || !protocol_reports)
1251 return 0;
1252 /* If either reports -1 (invalid value), report -1. */
1253 if ((git_reports == -1) || (protocol_reports == -1))
1254 return -1;
1255 /* Otherwise if both report unknown, report unknown. */
1256 return 1;
1259 void transport_set_verbosity(struct transport *transport, int verbosity,
1260 int force_progress)
1262 if (verbosity >= 1)
1263 transport->verbose = verbosity <= 3 ? verbosity : 3;
1264 if (verbosity < 0)
1265 transport->verbose = -1;
1268 * Rules used to determine whether to report progress (processing aborts
1269 * when a rule is satisfied):
1271 * . Report progress, if force_progress is 1 (ie. --progress).
1272 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1273 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1274 * . Report progress if isatty(2) is 1.
1276 if (force_progress >= 0)
1277 transport->progress = !!force_progress;
1278 else
1279 transport->progress = verbosity >= 0 && isatty(2);
1282 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
1284 int i;
1286 fprintf(stderr, _("The following submodule paths contain changes that can\n"
1287 "not be found on any remote:\n"));
1288 for (i = 0; i < needs_pushing->nr; i++)
1289 fprintf(stderr, " %s\n", needs_pushing->items[i].string);
1290 fprintf(stderr, _("\nPlease try\n\n"
1291 " git push --recurse-submodules=on-demand\n\n"
1292 "or cd to the path and use\n\n"
1293 " git push\n\n"
1294 "to push them to a remote.\n\n"));
1296 string_list_clear(needs_pushing, 0);
1298 die(_("Aborting."));
1301 static int run_pre_push_hook(struct transport *transport,
1302 struct ref *remote_refs)
1304 int ret = 0, x;
1305 struct ref *r;
1306 struct child_process proc = CHILD_PROCESS_INIT;
1307 struct strbuf buf;
1308 const char *hook_path = find_hook(the_repository, "pre-push");
1310 if (!hook_path)
1311 return 0;
1313 strvec_push(&proc.args, hook_path);
1314 strvec_push(&proc.args, transport->remote->name);
1315 strvec_push(&proc.args, transport->url);
1317 proc.in = -1;
1318 proc.trace2_hook_name = "pre-push";
1320 if (start_command(&proc)) {
1321 finish_command(&proc);
1322 return -1;
1325 sigchain_push(SIGPIPE, SIG_IGN);
1327 strbuf_init(&buf, 256);
1329 for (r = remote_refs; r; r = r->next) {
1330 if (!r->peer_ref) continue;
1331 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
1332 if (r->status == REF_STATUS_REJECT_STALE) continue;
1333 if (r->status == REF_STATUS_REJECT_REMOTE_UPDATED) continue;
1334 if (r->status == REF_STATUS_UPTODATE) continue;
1336 strbuf_reset(&buf);
1337 strbuf_addf( &buf, "%s %s %s %s\n",
1338 r->peer_ref->name, oid_to_hex(&r->new_oid),
1339 r->name, oid_to_hex(&r->old_oid));
1341 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
1342 /* We do not mind if a hook does not read all refs. */
1343 if (errno != EPIPE)
1344 ret = -1;
1345 break;
1349 strbuf_release(&buf);
1351 x = close(proc.in);
1352 if (!ret)
1353 ret = x;
1355 sigchain_pop(SIGPIPE);
1357 x = finish_command(&proc);
1358 if (!ret)
1359 ret = x;
1361 return ret;
1364 int transport_push(struct repository *r,
1365 struct transport *transport,
1366 struct refspec *rs, int flags,
1367 unsigned int *reject_reasons)
1369 struct ref *remote_refs = NULL;
1370 struct ref *local_refs = NULL;
1371 int match_flags = MATCH_REFS_NONE;
1372 int verbose = (transport->verbose > 0);
1373 int quiet = (transport->verbose < 0);
1374 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1375 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1376 int push_ret, err;
1377 int ret = -1;
1378 struct transport_ls_refs_options transport_options =
1379 TRANSPORT_LS_REFS_OPTIONS_INIT;
1381 *reject_reasons = 0;
1383 if (transport_color_config() < 0)
1384 goto done;
1386 if (!transport->vtable->push_refs)
1387 goto done;
1389 local_refs = get_local_heads();
1391 if (check_push_refs(local_refs, rs) < 0)
1392 goto done;
1394 refspec_ref_prefixes(rs, &transport_options.ref_prefixes);
1396 trace2_region_enter("transport_push", "get_refs_list", r);
1397 remote_refs = transport->vtable->get_refs_list(transport, 1,
1398 &transport_options);
1399 trace2_region_leave("transport_push", "get_refs_list", r);
1401 transport_ls_refs_options_release(&transport_options);
1403 if (flags & TRANSPORT_PUSH_ALL)
1404 match_flags |= MATCH_REFS_ALL;
1405 if (flags & TRANSPORT_PUSH_MIRROR)
1406 match_flags |= MATCH_REFS_MIRROR;
1407 if (flags & TRANSPORT_PUSH_PRUNE)
1408 match_flags |= MATCH_REFS_PRUNE;
1409 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1410 match_flags |= MATCH_REFS_FOLLOW_TAGS;
1412 if (match_push_refs(local_refs, &remote_refs, rs, match_flags))
1413 goto done;
1415 if (transport->smart_options &&
1416 transport->smart_options->cas &&
1417 !is_empty_cas(transport->smart_options->cas))
1418 apply_push_cas(transport->smart_options->cas,
1419 transport->remote, remote_refs);
1421 set_ref_status_for_push(remote_refs,
1422 flags & TRANSPORT_PUSH_MIRROR,
1423 flags & TRANSPORT_PUSH_FORCE);
1425 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1426 if (run_pre_push_hook(transport, remote_refs))
1427 goto done;
1429 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1430 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1431 !is_bare_repository()) {
1432 struct ref *ref = remote_refs;
1433 struct oid_array commits = OID_ARRAY_INIT;
1435 trace2_region_enter("transport_push", "push_submodules", r);
1436 for (; ref; ref = ref->next)
1437 if (!is_null_oid(&ref->new_oid))
1438 oid_array_append(&commits,
1439 &ref->new_oid);
1441 if (!push_unpushed_submodules(r,
1442 &commits,
1443 transport->remote,
1445 transport->push_options,
1446 pretend)) {
1447 oid_array_clear(&commits);
1448 trace2_region_leave("transport_push", "push_submodules", r);
1449 die(_("failed to push all needed submodules"));
1451 oid_array_clear(&commits);
1452 trace2_region_leave("transport_push", "push_submodules", r);
1455 if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1456 ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1457 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1458 !pretend)) && !is_bare_repository()) {
1459 struct ref *ref = remote_refs;
1460 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1461 struct oid_array commits = OID_ARRAY_INIT;
1463 trace2_region_enter("transport_push", "check_submodules", r);
1464 for (; ref; ref = ref->next)
1465 if (!is_null_oid(&ref->new_oid))
1466 oid_array_append(&commits,
1467 &ref->new_oid);
1469 if (find_unpushed_submodules(r,
1470 &commits,
1471 transport->remote->name,
1472 &needs_pushing)) {
1473 oid_array_clear(&commits);
1474 trace2_region_leave("transport_push", "check_submodules", r);
1475 die_with_unpushed_submodules(&needs_pushing);
1477 string_list_clear(&needs_pushing, 0);
1478 oid_array_clear(&commits);
1479 trace2_region_leave("transport_push", "check_submodules", r);
1482 if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY)) {
1483 trace2_region_enter("transport_push", "push_refs", r);
1484 push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1485 trace2_region_leave("transport_push", "push_refs", r);
1486 } else
1487 push_ret = 0;
1488 err = push_had_errors(remote_refs);
1489 ret = push_ret | err;
1491 if (!quiet || err)
1492 transport_print_push_status(transport->url, remote_refs,
1493 verbose | porcelain, porcelain,
1494 reject_reasons);
1496 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1497 set_upstreams(transport, remote_refs, pretend);
1499 if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1500 TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1501 struct ref *ref;
1502 for (ref = remote_refs; ref; ref = ref->next)
1503 transport_update_tracking_ref(transport->remote, ref, verbose);
1506 if (porcelain && !push_ret)
1507 puts("Done");
1508 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1509 /* stable plumbing output; do not modify or localize */
1510 fprintf(stderr, "Everything up-to-date\n");
1512 done:
1513 free_refs(local_refs);
1514 free_refs(remote_refs);
1515 return ret;
1518 const struct ref *transport_get_remote_refs(struct transport *transport,
1519 struct transport_ls_refs_options *transport_options)
1521 if (!transport->got_remote_refs) {
1522 transport->remote_refs =
1523 transport->vtable->get_refs_list(transport, 0,
1524 transport_options);
1525 transport->got_remote_refs = 1;
1528 return transport->remote_refs;
1531 void transport_ls_refs_options_release(struct transport_ls_refs_options *opts)
1533 strvec_clear(&opts->ref_prefixes);
1534 free((char *)opts->unborn_head_target);
1537 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1539 int rc;
1540 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1541 struct ref **heads = NULL;
1542 struct ref *rm;
1544 for (rm = refs; rm; rm = rm->next) {
1545 nr_refs++;
1546 if (rm->peer_ref &&
1547 !is_null_oid(&rm->old_oid) &&
1548 oideq(&rm->peer_ref->old_oid, &rm->old_oid))
1549 continue;
1550 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1551 heads[nr_heads++] = rm;
1554 if (!nr_heads) {
1556 * When deepening of a shallow repository is requested,
1557 * then local and remote refs are likely to still be equal.
1558 * Just feed them all to the fetch method in that case.
1559 * This condition shouldn't be met in a non-deepening fetch
1560 * (see builtin/fetch.c:quickfetch()).
1562 ALLOC_ARRAY(heads, nr_refs);
1563 for (rm = refs; rm; rm = rm->next)
1564 heads[nr_heads++] = rm;
1567 rc = transport->vtable->fetch_refs(transport, nr_heads, heads);
1569 free(heads);
1570 return rc;
1573 int transport_get_remote_bundle_uri(struct transport *transport)
1575 int value = 0;
1576 const struct transport_vtable *vtable = transport->vtable;
1578 /* Check config only once. */
1579 if (transport->got_remote_bundle_uri)
1580 return 0;
1581 transport->got_remote_bundle_uri = 1;
1584 * Don't request bundle-uri from the server unless configured to
1585 * do so by the transfer.bundleURI=true config option.
1587 if (git_config_get_bool("transfer.bundleuri", &value) || !value)
1588 return 0;
1590 if (!transport->bundles->baseURI)
1591 transport->bundles->baseURI = xstrdup(transport->url);
1593 if (!vtable->get_bundle_uri)
1594 return error(_("bundle-uri operation not supported by protocol"));
1596 if (vtable->get_bundle_uri(transport) < 0)
1597 return error(_("could not retrieve server-advertised bundle-uri list"));
1598 return 0;
1601 void transport_unlock_pack(struct transport *transport, unsigned int flags)
1603 int in_signal_handler = !!(flags & TRANSPORT_UNLOCK_PACK_IN_SIGNAL_HANDLER);
1604 int i;
1606 for (i = 0; i < transport->pack_lockfiles.nr; i++)
1607 if (in_signal_handler)
1608 unlink(transport->pack_lockfiles.items[i].string);
1609 else
1610 unlink_or_warn(transport->pack_lockfiles.items[i].string);
1611 if (!in_signal_handler)
1612 string_list_clear(&transport->pack_lockfiles, 0);
1615 int transport_connect(struct transport *transport, const char *name,
1616 const char *exec, int fd[2])
1618 if (transport->vtable->connect)
1619 return transport->vtable->connect(transport, name, exec, fd);
1620 else
1621 die(_("operation not supported by protocol"));
1624 int transport_disconnect(struct transport *transport)
1626 int ret = 0;
1627 if (transport->vtable->disconnect)
1628 ret = transport->vtable->disconnect(transport);
1629 if (transport->got_remote_refs)
1630 free_refs((void *)transport->remote_refs);
1631 clear_bundle_list(transport->bundles);
1632 free(transport->bundles);
1633 free(transport);
1634 return ret;
1638 * Strip username (and password) from a URL and return
1639 * it in a newly allocated string.
1641 char *transport_anonymize_url(const char *url)
1643 char *scheme_prefix, *anon_part;
1644 size_t anon_len, prefix_len = 0;
1646 anon_part = strchr(url, '@');
1647 if (url_is_local_not_ssh(url) || !anon_part)
1648 goto literal_copy;
1650 anon_len = strlen(++anon_part);
1651 scheme_prefix = strstr(url, "://");
1652 if (!scheme_prefix) {
1653 if (!strchr(anon_part, ':'))
1654 /* cannot be "me@there:/path/name" */
1655 goto literal_copy;
1656 } else {
1657 const char *cp;
1658 /* make sure scheme is reasonable */
1659 for (cp = url; cp < scheme_prefix; cp++) {
1660 switch (*cp) {
1661 /* RFC 1738 2.1 */
1662 case '+': case '.': case '-':
1663 break; /* ok */
1664 default:
1665 if (isalnum(*cp))
1666 break;
1667 /* it isn't */
1668 goto literal_copy;
1671 /* @ past the first slash does not count */
1672 cp = strchr(scheme_prefix + 3, '/');
1673 if (cp && cp < anon_part)
1674 goto literal_copy;
1675 prefix_len = scheme_prefix - url + 3;
1677 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1678 (int)anon_len, anon_part);
1679 literal_copy:
1680 return xstrdup(url);