t5332-multi-pack-reuse.sh: demonstrate duplicate packing failure
[git/gitster.git] / transport.c
blob1098bbd60e4f6594d5889ea28dc0fe0cffa79a19
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 (server_feature_v2("session-id", &server_sid))
338 trace2_data_string("transfer", NULL, "server-sid", server_sid);
339 if (must_list_refs)
340 get_remote_refs(data->fd[1], &reader, &refs, for_push,
341 options,
342 transport->server_options,
343 transport->stateless_rpc);
344 break;
345 case protocol_v1:
346 case protocol_v0:
347 die_if_server_options(transport);
348 get_remote_heads(&reader, &refs,
349 for_push ? REF_NORMAL : 0,
350 &data->extra_have,
351 &data->shallow);
352 server_sid = server_feature_value("session-id", &sid_len);
353 if (server_sid) {
354 char *sid = xstrndup(server_sid, sid_len);
355 trace2_data_string("transfer", NULL, "server-sid", sid);
356 free(sid);
358 break;
359 case protocol_unknown_version:
360 BUG("unknown protocol version");
362 data->finished_handshake = 1;
363 transport->hash_algo = reader.hash_algo;
365 if (reader.line_peeked)
366 BUG("buffer must be empty at the end of handshake()");
368 return refs;
371 static struct ref *get_refs_via_connect(struct transport *transport, int for_push,
372 struct transport_ls_refs_options *options)
374 return handshake(transport, for_push, options, 1);
377 static int get_bundle_uri(struct transport *transport)
379 struct git_transport_data *data = transport->data;
380 struct packet_reader reader;
381 int stateless_rpc = transport->stateless_rpc;
383 if (!transport->bundles) {
384 CALLOC_ARRAY(transport->bundles, 1);
385 init_bundle_list(transport->bundles);
388 if (!data->finished_handshake) {
389 struct ref *refs = handshake(transport, 0, NULL, 0);
391 if (refs)
392 free_refs(refs);
396 * "Support" protocol v0 and v2 without bundle-uri support by
397 * silently degrading to a NOOP.
399 if (!server_supports_v2("bundle-uri"))
400 return 0;
402 packet_reader_init(&reader, data->fd[0], NULL, 0,
403 PACKET_READ_CHOMP_NEWLINE |
404 PACKET_READ_GENTLE_ON_EOF);
406 return get_remote_bundle_uri(data->fd[1], &reader,
407 transport->bundles, stateless_rpc);
410 static int fetch_refs_via_pack(struct transport *transport,
411 int nr_heads, struct ref **to_fetch)
413 int ret = 0;
414 struct git_transport_data *data = transport->data;
415 struct ref *refs = NULL;
416 struct fetch_pack_args args;
417 struct ref *refs_tmp = NULL, **to_fetch_dup = NULL;
419 memset(&args, 0, sizeof(args));
420 args.uploadpack = data->options.uploadpack;
421 args.keep_pack = data->options.keep;
422 args.lock_pack = 1;
423 args.use_thin_pack = data->options.thin;
424 args.include_tag = data->options.followtags;
425 args.verbose = (transport->verbose > 1);
426 args.quiet = (transport->verbose < 0);
427 args.no_progress = !transport->progress;
428 args.depth = data->options.depth;
429 args.deepen_since = data->options.deepen_since;
430 args.deepen_not = data->options.deepen_not;
431 args.deepen_relative = data->options.deepen_relative;
432 args.check_self_contained_and_connected =
433 data->options.check_self_contained_and_connected;
434 args.cloning = transport->cloning;
435 args.update_shallow = data->options.update_shallow;
436 args.from_promisor = data->options.from_promisor;
437 list_objects_filter_copy(&args.filter_options,
438 &data->options.filter_options);
439 args.refetch = data->options.refetch;
440 args.stateless_rpc = transport->stateless_rpc;
441 args.server_options = transport->server_options;
442 args.negotiation_tips = data->options.negotiation_tips;
443 args.reject_shallow_remote = transport->smart_options->reject_shallow;
445 if (!data->finished_handshake) {
446 int i;
447 int must_list_refs = 0;
448 for (i = 0; i < nr_heads; i++) {
449 if (!to_fetch[i]->exact_oid) {
450 must_list_refs = 1;
451 break;
454 refs_tmp = handshake(transport, 0, NULL, must_list_refs);
457 if (data->version == protocol_unknown_version)
458 BUG("unknown protocol version");
459 else if (data->version <= protocol_v1)
460 die_if_server_options(transport);
462 if (data->options.acked_commits) {
463 if (data->version < protocol_v2) {
464 warning(_("--negotiate-only requires protocol v2"));
465 ret = -1;
466 } else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
467 warning(_("server does not support wait-for-done"));
468 ret = -1;
469 } else {
470 negotiate_using_fetch(data->options.negotiation_tips,
471 transport->server_options,
472 transport->stateless_rpc,
473 data->fd,
474 data->options.acked_commits);
475 ret = 0;
477 goto cleanup;
481 * Create a shallow copy of `sought` so that we can free all of its entries.
482 * This is because `fetch_pack()` will modify the array to evict some
483 * entries, but won't free those.
485 DUP_ARRAY(to_fetch_dup, to_fetch, nr_heads);
486 to_fetch = to_fetch_dup;
488 refs = fetch_pack(&args, data->fd,
489 refs_tmp ? refs_tmp : transport->remote_refs,
490 to_fetch, nr_heads, &data->shallow,
491 &transport->pack_lockfiles, data->version);
493 data->finished_handshake = 0;
494 data->options.self_contained_and_connected =
495 args.self_contained_and_connected;
496 data->options.connectivity_checked = args.connectivity_checked;
498 if (!refs)
499 ret = -1;
500 if (report_unmatched_refs(to_fetch, nr_heads))
501 ret = -1;
503 cleanup:
504 close(data->fd[0]);
505 if (data->fd[1] >= 0)
506 close(data->fd[1]);
507 if (finish_connect(data->conn))
508 ret = -1;
509 data->conn = NULL;
511 free(to_fetch_dup);
512 free_refs(refs_tmp);
513 free_refs(refs);
514 list_objects_filter_release(&args.filter_options);
515 return ret;
518 static int push_had_errors(struct ref *ref)
520 for (; ref; ref = ref->next) {
521 switch (ref->status) {
522 case REF_STATUS_NONE:
523 case REF_STATUS_UPTODATE:
524 case REF_STATUS_OK:
525 break;
526 default:
527 return 1;
530 return 0;
533 int transport_refs_pushed(struct ref *ref)
535 for (; ref; ref = ref->next) {
536 switch(ref->status) {
537 case REF_STATUS_NONE:
538 case REF_STATUS_UPTODATE:
539 break;
540 default:
541 return 1;
544 return 0;
547 static void update_one_tracking_ref(struct remote *remote, char *refname,
548 struct object_id *new_oid, int deletion,
549 int verbose)
551 struct refspec_item rs;
553 memset(&rs, 0, sizeof(rs));
554 rs.src = refname;
555 rs.dst = NULL;
557 if (!remote_find_tracking(remote, &rs)) {
558 if (verbose)
559 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
560 if (deletion)
561 refs_delete_ref(get_main_ref_store(the_repository),
562 NULL, rs.dst, NULL, 0);
563 else
564 refs_update_ref(get_main_ref_store(the_repository),
565 "update by push", rs.dst, new_oid,
566 NULL, 0, 0);
567 free(rs.dst);
571 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
573 char *refname;
574 struct object_id *new_oid;
575 struct ref_push_report *report;
577 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
578 return;
580 report = ref->report;
581 if (!report)
582 update_one_tracking_ref(remote, ref->name, &ref->new_oid,
583 ref->deletion, verbose);
584 else
585 for (; report; report = report->next) {
586 refname = report->ref_name ? (char *)report->ref_name : ref->name;
587 new_oid = report->new_oid ? report->new_oid : &ref->new_oid;
588 update_one_tracking_ref(remote, refname, new_oid,
589 is_null_oid(new_oid), verbose);
593 static void print_ref_status(char flag, const char *summary,
594 struct ref *to, struct ref *from, const char *msg,
595 struct ref_push_report *report,
596 int porcelain, int summary_width)
598 const char *to_name;
600 if (report && report->ref_name)
601 to_name = report->ref_name;
602 else
603 to_name = to->name;
605 if (porcelain) {
606 if (from)
607 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to_name);
608 else
609 fprintf(stdout, "%c\t:%s\t", flag, to_name);
610 if (msg)
611 fprintf(stdout, "%s (%s)\n", summary, msg);
612 else
613 fprintf(stdout, "%s\n", summary);
614 } else {
615 const char *red = "", *reset = "";
616 if (push_had_errors(to)) {
617 red = transport_get_color(TRANSPORT_COLOR_REJECTED);
618 reset = transport_get_color(TRANSPORT_COLOR_RESET);
620 fprintf(stderr, " %s%c %-*s%s ", red, flag, summary_width,
621 summary, reset);
622 if (from)
623 fprintf(stderr, "%s -> %s",
624 prettify_refname(from->name),
625 prettify_refname(to_name));
626 else
627 fputs(prettify_refname(to_name), stderr);
628 if (msg) {
629 fputs(" (", stderr);
630 fputs(msg, stderr);
631 fputc(')', stderr);
633 fputc('\n', stderr);
637 static void print_ok_ref_status(struct ref *ref,
638 struct ref_push_report *report,
639 int porcelain, int summary_width)
641 struct object_id *old_oid;
642 struct object_id *new_oid;
643 const char *ref_name;
644 int forced_update;
646 if (report && report->old_oid)
647 old_oid = report->old_oid;
648 else
649 old_oid = &ref->old_oid;
650 if (report && report->new_oid)
651 new_oid = report->new_oid;
652 else
653 new_oid = &ref->new_oid;
654 if (report && report->forced_update)
655 forced_update = report->forced_update;
656 else
657 forced_update = ref->forced_update;
658 if (report && report->ref_name)
659 ref_name = report->ref_name;
660 else
661 ref_name = ref->name;
663 if (ref->deletion)
664 print_ref_status('-', "[deleted]", ref, NULL, NULL,
665 report, porcelain, summary_width);
666 else if (is_null_oid(old_oid))
667 print_ref_status('*',
668 (starts_with(ref_name, "refs/tags/")
669 ? "[new tag]"
670 : (starts_with(ref_name, "refs/heads/")
671 ? "[new branch]"
672 : "[new reference]")),
673 ref, ref->peer_ref, NULL,
674 report, porcelain, summary_width);
675 else {
676 struct strbuf quickref = STRBUF_INIT;
677 char type;
678 const char *msg;
680 strbuf_add_unique_abbrev(&quickref, old_oid,
681 DEFAULT_ABBREV);
682 if (forced_update) {
683 strbuf_addstr(&quickref, "...");
684 type = '+';
685 msg = "forced update";
686 } else {
687 strbuf_addstr(&quickref, "..");
688 type = ' ';
689 msg = NULL;
691 strbuf_add_unique_abbrev(&quickref, new_oid,
692 DEFAULT_ABBREV);
694 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
695 report, porcelain, summary_width);
696 strbuf_release(&quickref);
700 static int print_one_push_report(struct ref *ref, const char *dest, int count,
701 struct ref_push_report *report,
702 int porcelain, int summary_width)
704 if (!count) {
705 char *url = transport_anonymize_url(dest);
706 fprintf(porcelain ? stdout : stderr, "To %s\n", url);
707 free(url);
710 switch(ref->status) {
711 case REF_STATUS_NONE:
712 print_ref_status('X', "[no match]", ref, NULL, NULL,
713 report, porcelain, summary_width);
714 break;
715 case REF_STATUS_REJECT_NODELETE:
716 print_ref_status('!', "[rejected]", ref, NULL,
717 "remote does not support deleting refs",
718 report, porcelain, summary_width);
719 break;
720 case REF_STATUS_UPTODATE:
721 print_ref_status('=', "[up to date]", ref,
722 ref->peer_ref, NULL,
723 report, porcelain, summary_width);
724 break;
725 case REF_STATUS_REJECT_NONFASTFORWARD:
726 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
727 "non-fast-forward",
728 report, porcelain, summary_width);
729 break;
730 case REF_STATUS_REJECT_ALREADY_EXISTS:
731 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
732 "already exists",
733 report, porcelain, summary_width);
734 break;
735 case REF_STATUS_REJECT_FETCH_FIRST:
736 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
737 "fetch first",
738 report, porcelain, summary_width);
739 break;
740 case REF_STATUS_REJECT_NEEDS_FORCE:
741 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
742 "needs force",
743 report, porcelain, summary_width);
744 break;
745 case REF_STATUS_REJECT_STALE:
746 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
747 "stale info",
748 report, porcelain, summary_width);
749 break;
750 case REF_STATUS_REJECT_REMOTE_UPDATED:
751 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
752 "remote ref updated since checkout",
753 report, porcelain, summary_width);
754 break;
755 case REF_STATUS_REJECT_SHALLOW:
756 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
757 "new shallow roots not allowed",
758 report, porcelain, summary_width);
759 break;
760 case REF_STATUS_REMOTE_REJECT:
761 print_ref_status('!', "[remote rejected]", ref,
762 ref->deletion ? NULL : ref->peer_ref,
763 ref->remote_status,
764 report, porcelain, summary_width);
765 break;
766 case REF_STATUS_EXPECTING_REPORT:
767 print_ref_status('!', "[remote failure]", ref,
768 ref->deletion ? NULL : ref->peer_ref,
769 "remote failed to report status",
770 report, porcelain, summary_width);
771 break;
772 case REF_STATUS_ATOMIC_PUSH_FAILED:
773 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
774 "atomic push failed",
775 report, porcelain, summary_width);
776 break;
777 case REF_STATUS_OK:
778 print_ok_ref_status(ref, report, porcelain, summary_width);
779 break;
782 return 1;
785 static int print_one_push_status(struct ref *ref, const char *dest, int count,
786 int porcelain, int summary_width)
788 struct ref_push_report *report;
789 int n = 0;
791 if (!ref->report)
792 return print_one_push_report(ref, dest, count,
793 NULL, porcelain, summary_width);
795 for (report = ref->report; report; report = report->next)
796 print_one_push_report(ref, dest, count + n++,
797 report, porcelain, summary_width);
798 return n;
801 static int measure_abbrev(const struct object_id *oid, int sofar)
803 char hex[GIT_MAX_HEXSZ + 1];
804 int w = repo_find_unique_abbrev_r(the_repository, hex, oid,
805 DEFAULT_ABBREV);
807 return (w < sofar) ? sofar : w;
810 int transport_summary_width(const struct ref *refs)
812 int maxw = -1;
814 for (; refs; refs = refs->next) {
815 maxw = measure_abbrev(&refs->old_oid, maxw);
816 maxw = measure_abbrev(&refs->new_oid, maxw);
818 if (maxw < 0)
819 maxw = FALLBACK_DEFAULT_ABBREV;
820 return (2 * maxw + 3);
823 void transport_print_push_status(const char *dest, struct ref *refs,
824 int verbose, int porcelain, unsigned int *reject_reasons)
826 struct ref *ref;
827 int n = 0;
828 char *head;
829 int summary_width = transport_summary_width(refs);
831 if (transport_color_config() < 0)
832 warning(_("could not parse transport.color.* config"));
834 head = refs_resolve_refdup(get_main_ref_store(the_repository), "HEAD",
835 RESOLVE_REF_READING, NULL, NULL);
837 if (verbose) {
838 for (ref = refs; ref; ref = ref->next)
839 if (ref->status == REF_STATUS_UPTODATE)
840 n += print_one_push_status(ref, dest, n,
841 porcelain, summary_width);
844 for (ref = refs; ref; ref = ref->next)
845 if (ref->status == REF_STATUS_OK)
846 n += print_one_push_status(ref, dest, n,
847 porcelain, summary_width);
849 *reject_reasons = 0;
850 for (ref = refs; ref; ref = ref->next) {
851 if (ref->status != REF_STATUS_NONE &&
852 ref->status != REF_STATUS_UPTODATE &&
853 ref->status != REF_STATUS_OK)
854 n += print_one_push_status(ref, dest, n,
855 porcelain, summary_width);
856 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
857 if (head != NULL && !strcmp(head, ref->name))
858 *reject_reasons |= REJECT_NON_FF_HEAD;
859 else
860 *reject_reasons |= REJECT_NON_FF_OTHER;
861 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
862 *reject_reasons |= REJECT_ALREADY_EXISTS;
863 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
864 *reject_reasons |= REJECT_FETCH_FIRST;
865 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
866 *reject_reasons |= REJECT_NEEDS_FORCE;
867 } else if (ref->status == REF_STATUS_REJECT_REMOTE_UPDATED) {
868 *reject_reasons |= REJECT_REF_NEEDS_UPDATE;
871 free(head);
874 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
876 struct git_transport_data *data = transport->data;
877 struct send_pack_args args;
878 int ret = 0;
880 if (transport_color_config() < 0)
881 return -1;
883 if (!data->finished_handshake)
884 get_refs_via_connect(transport, 1, NULL);
886 memset(&args, 0, sizeof(args));
887 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
888 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
889 args.use_thin_pack = data->options.thin;
890 args.verbose = (transport->verbose > 0);
891 args.quiet = (transport->verbose < 0);
892 args.progress = transport->progress;
893 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
894 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
895 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
896 args.push_options = transport->push_options;
897 args.url = transport->url;
899 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
900 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
901 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
902 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
903 else
904 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
906 switch (data->version) {
907 case protocol_v2:
908 die(_("support for protocol v2 not implemented yet"));
909 break;
910 case protocol_v1:
911 case protocol_v0:
912 ret = send_pack(&args, data->fd, data->conn, remote_refs,
913 &data->extra_have);
914 break;
915 case protocol_unknown_version:
916 BUG("unknown protocol version");
919 close(data->fd[1]);
920 close(data->fd[0]);
922 * Atomic push may abort the connection early and close the pipe,
923 * which may cause an error for `finish_connect()`. Ignore this error
924 * for atomic git-push.
926 if (ret || args.atomic)
927 finish_connect(data->conn);
928 else
929 ret = finish_connect(data->conn);
930 data->conn = NULL;
931 data->finished_handshake = 0;
933 return ret;
936 static int connect_git(struct transport *transport, const char *name,
937 const char *executable, int fd[2])
939 struct git_transport_data *data = transport->data;
940 data->conn = git_connect(data->fd, transport->url,
941 name, executable, 0);
942 fd[0] = data->fd[0];
943 fd[1] = data->fd[1];
944 return 0;
947 static int disconnect_git(struct transport *transport)
949 struct git_transport_data *data = transport->data;
950 if (data->conn) {
951 if (data->finished_handshake && !transport->stateless_rpc)
952 packet_flush(data->fd[1]);
953 close(data->fd[0]);
954 if (data->fd[1] >= 0)
955 close(data->fd[1]);
956 finish_connect(data->conn);
959 if (data->options.negotiation_tips) {
960 oid_array_clear(data->options.negotiation_tips);
961 free(data->options.negotiation_tips);
963 list_objects_filter_release(&data->options.filter_options);
964 oid_array_clear(&data->extra_have);
965 oid_array_clear(&data->shallow);
966 free(data);
967 return 0;
970 static struct transport_vtable taken_over_vtable = {
971 .get_refs_list = get_refs_via_connect,
972 .get_bundle_uri = get_bundle_uri,
973 .fetch_refs = fetch_refs_via_pack,
974 .push_refs = git_transport_push,
975 .disconnect = disconnect_git
978 void transport_take_over(struct transport *transport,
979 struct child_process *child)
981 struct git_transport_data *data;
983 if (!transport->smart_options)
984 BUG("taking over transport requires non-NULL "
985 "smart_options field.");
987 CALLOC_ARRAY(data, 1);
988 data->options = *transport->smart_options;
989 data->conn = child;
990 data->fd[0] = data->conn->out;
991 data->fd[1] = data->conn->in;
992 data->finished_handshake = 0;
993 transport->data = data;
995 transport->vtable = &taken_over_vtable;
996 transport->smart_options = &(data->options);
998 transport->cannot_reuse = 1;
1001 static int is_file(const char *url)
1003 struct stat buf;
1004 if (stat(url, &buf))
1005 return 0;
1006 return S_ISREG(buf.st_mode);
1009 static int external_specification_len(const char *url)
1011 return strchr(url, ':') - url;
1014 static const struct string_list *protocol_allow_list(void)
1016 static int enabled = -1;
1017 static struct string_list allowed = STRING_LIST_INIT_DUP;
1019 if (enabled < 0) {
1020 const char *v = getenv("GIT_ALLOW_PROTOCOL");
1021 if (v) {
1022 string_list_split(&allowed, v, ':', -1);
1023 string_list_sort(&allowed);
1024 enabled = 1;
1025 } else {
1026 enabled = 0;
1030 return enabled ? &allowed : NULL;
1033 enum protocol_allow_config {
1034 PROTOCOL_ALLOW_NEVER = 0,
1035 PROTOCOL_ALLOW_USER_ONLY,
1036 PROTOCOL_ALLOW_ALWAYS
1039 static enum protocol_allow_config parse_protocol_config(const char *key,
1040 const char *value)
1042 if (!strcasecmp(value, "always"))
1043 return PROTOCOL_ALLOW_ALWAYS;
1044 else if (!strcasecmp(value, "never"))
1045 return PROTOCOL_ALLOW_NEVER;
1046 else if (!strcasecmp(value, "user"))
1047 return PROTOCOL_ALLOW_USER_ONLY;
1049 die(_("unknown value for config '%s': %s"), key, value);
1052 static enum protocol_allow_config get_protocol_config(const char *type)
1054 char *key = xstrfmt("protocol.%s.allow", type);
1055 char *value;
1057 /* first check the per-protocol config */
1058 if (!git_config_get_string(key, &value)) {
1059 enum protocol_allow_config ret =
1060 parse_protocol_config(key, value);
1061 free(key);
1062 free(value);
1063 return ret;
1065 free(key);
1067 /* if defined, fallback to user-defined default for unknown protocols */
1068 if (!git_config_get_string("protocol.allow", &value)) {
1069 enum protocol_allow_config ret =
1070 parse_protocol_config("protocol.allow", value);
1071 free(value);
1072 return ret;
1075 /* fallback to built-in defaults */
1076 /* known safe */
1077 if (!strcmp(type, "http") ||
1078 !strcmp(type, "https") ||
1079 !strcmp(type, "git") ||
1080 !strcmp(type, "ssh"))
1081 return PROTOCOL_ALLOW_ALWAYS;
1083 /* known scary; err on the side of caution */
1084 if (!strcmp(type, "ext"))
1085 return PROTOCOL_ALLOW_NEVER;
1087 /* unknown; by default let them be used only directly by the user */
1088 return PROTOCOL_ALLOW_USER_ONLY;
1091 int is_transport_allowed(const char *type, int from_user)
1093 const struct string_list *allow_list = protocol_allow_list();
1094 if (allow_list)
1095 return string_list_has_string(allow_list, type);
1097 switch (get_protocol_config(type)) {
1098 case PROTOCOL_ALLOW_ALWAYS:
1099 return 1;
1100 case PROTOCOL_ALLOW_NEVER:
1101 return 0;
1102 case PROTOCOL_ALLOW_USER_ONLY:
1103 if (from_user < 0)
1104 from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1105 return from_user;
1108 BUG("invalid protocol_allow_config type");
1111 void transport_check_allowed(const char *type)
1113 if (!is_transport_allowed(type, -1))
1114 die(_("transport '%s' not allowed"), type);
1117 static struct transport_vtable bundle_vtable = {
1118 .get_refs_list = get_refs_from_bundle,
1119 .fetch_refs = fetch_refs_from_bundle,
1120 .disconnect = close_bundle
1123 static struct transport_vtable builtin_smart_vtable = {
1124 .get_refs_list = get_refs_via_connect,
1125 .get_bundle_uri = get_bundle_uri,
1126 .fetch_refs = fetch_refs_via_pack,
1127 .push_refs = git_transport_push,
1128 .connect = connect_git,
1129 .disconnect = disconnect_git
1132 struct transport *transport_get(struct remote *remote, const char *url)
1134 const char *helper;
1135 char *helper_to_free = NULL;
1136 const char *p;
1137 struct transport *ret = xcalloc(1, sizeof(*ret));
1139 ret->progress = isatty(2);
1140 string_list_init_dup(&ret->pack_lockfiles);
1142 CALLOC_ARRAY(ret->bundles, 1);
1143 init_bundle_list(ret->bundles);
1145 if (!remote)
1146 BUG("No remote provided to transport_get()");
1148 ret->got_remote_refs = 0;
1149 ret->remote = remote;
1150 helper = remote->foreign_vcs;
1152 if (!url)
1153 url = remote->url.v[0];
1154 ret->url = url;
1156 p = url;
1157 while (is_urlschemechar(p == url, *p))
1158 p++;
1159 if (starts_with(p, "::"))
1160 helper = helper_to_free = xstrndup(url, p - url);
1162 if (helper) {
1163 transport_helper_init(ret, helper);
1164 free(helper_to_free);
1165 } else if (starts_with(url, "rsync:")) {
1166 die(_("git-over-rsync is no longer supported"));
1167 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
1168 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
1169 bundle_header_init(&data->header);
1170 transport_check_allowed("file");
1171 ret->data = data;
1172 ret->vtable = &bundle_vtable;
1173 ret->smart_options = NULL;
1174 } else if (!is_url(url)
1175 || starts_with(url, "file://")
1176 || starts_with(url, "git://")
1177 || starts_with(url, "ssh://")
1178 || starts_with(url, "git+ssh://") /* deprecated - do not use */
1179 || starts_with(url, "ssh+git://") /* deprecated - do not use */
1182 * These are builtin smart transports; "allowed" transports
1183 * will be checked individually in git_connect.
1185 struct git_transport_data *data = xcalloc(1, sizeof(*data));
1186 list_objects_filter_init(&data->options.filter_options);
1187 ret->data = data;
1188 ret->vtable = &builtin_smart_vtable;
1189 ret->smart_options = &(data->options);
1191 data->conn = NULL;
1192 data->finished_handshake = 0;
1193 } else {
1194 /* Unknown protocol in URL. Pass to external handler. */
1195 int len = external_specification_len(url);
1196 char *handler = xmemdupz(url, len);
1197 transport_helper_init(ret, handler);
1198 free(handler);
1201 if (ret->smart_options) {
1202 ret->smart_options->thin = 1;
1203 ret->smart_options->uploadpack = "git-upload-pack";
1204 if (remote->uploadpack)
1205 ret->smart_options->uploadpack = remote->uploadpack;
1206 ret->smart_options->receivepack = "git-receive-pack";
1207 if (remote->receivepack)
1208 ret->smart_options->receivepack = remote->receivepack;
1211 ret->hash_algo = &hash_algos[GIT_HASH_SHA1];
1213 return ret;
1216 const struct git_hash_algo *transport_get_hash_algo(struct transport *transport)
1218 return transport->hash_algo;
1221 int transport_set_option(struct transport *transport,
1222 const char *name, const char *value)
1224 int git_reports = 1, protocol_reports = 1;
1226 if (transport->smart_options)
1227 git_reports = set_git_option(transport->smart_options,
1228 name, value);
1230 if (transport->vtable->set_option)
1231 protocol_reports = transport->vtable->set_option(transport,
1232 name, value);
1234 /* If either report is 0, report 0 (success). */
1235 if (!git_reports || !protocol_reports)
1236 return 0;
1237 /* If either reports -1 (invalid value), report -1. */
1238 if ((git_reports == -1) || (protocol_reports == -1))
1239 return -1;
1240 /* Otherwise if both report unknown, report unknown. */
1241 return 1;
1244 void transport_set_verbosity(struct transport *transport, int verbosity,
1245 int force_progress)
1247 if (verbosity >= 1)
1248 transport->verbose = verbosity <= 3 ? verbosity : 3;
1249 if (verbosity < 0)
1250 transport->verbose = -1;
1253 * Rules used to determine whether to report progress (processing aborts
1254 * when a rule is satisfied):
1256 * . Report progress, if force_progress is 1 (ie. --progress).
1257 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1258 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1259 * . Report progress if isatty(2) is 1.
1261 if (force_progress >= 0)
1262 transport->progress = !!force_progress;
1263 else
1264 transport->progress = verbosity >= 0 && isatty(2);
1267 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
1269 int i;
1271 fprintf(stderr, _("The following submodule paths contain changes that can\n"
1272 "not be found on any remote:\n"));
1273 for (i = 0; i < needs_pushing->nr; i++)
1274 fprintf(stderr, " %s\n", needs_pushing->items[i].string);
1275 fprintf(stderr, _("\nPlease try\n\n"
1276 " git push --recurse-submodules=on-demand\n\n"
1277 "or cd to the path and use\n\n"
1278 " git push\n\n"
1279 "to push them to a remote.\n\n"));
1281 string_list_clear(needs_pushing, 0);
1283 die(_("Aborting."));
1286 static int run_pre_push_hook(struct transport *transport,
1287 struct ref *remote_refs)
1289 int ret = 0, x;
1290 struct ref *r;
1291 struct child_process proc = CHILD_PROCESS_INIT;
1292 struct strbuf buf;
1293 const char *hook_path = find_hook(the_repository, "pre-push");
1295 if (!hook_path)
1296 return 0;
1298 strvec_push(&proc.args, hook_path);
1299 strvec_push(&proc.args, transport->remote->name);
1300 strvec_push(&proc.args, transport->url);
1302 proc.in = -1;
1303 proc.trace2_hook_name = "pre-push";
1305 if (start_command(&proc)) {
1306 finish_command(&proc);
1307 return -1;
1310 sigchain_push(SIGPIPE, SIG_IGN);
1312 strbuf_init(&buf, 256);
1314 for (r = remote_refs; r; r = r->next) {
1315 if (!r->peer_ref) continue;
1316 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
1317 if (r->status == REF_STATUS_REJECT_STALE) continue;
1318 if (r->status == REF_STATUS_REJECT_REMOTE_UPDATED) continue;
1319 if (r->status == REF_STATUS_UPTODATE) continue;
1321 strbuf_reset(&buf);
1322 strbuf_addf( &buf, "%s %s %s %s\n",
1323 r->peer_ref->name, oid_to_hex(&r->new_oid),
1324 r->name, oid_to_hex(&r->old_oid));
1326 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
1327 /* We do not mind if a hook does not read all refs. */
1328 if (errno != EPIPE)
1329 ret = -1;
1330 break;
1334 strbuf_release(&buf);
1336 x = close(proc.in);
1337 if (!ret)
1338 ret = x;
1340 sigchain_pop(SIGPIPE);
1342 x = finish_command(&proc);
1343 if (!ret)
1344 ret = x;
1346 return ret;
1349 int transport_push(struct repository *r,
1350 struct transport *transport,
1351 struct refspec *rs, int flags,
1352 unsigned int *reject_reasons)
1354 struct ref *remote_refs = NULL;
1355 struct ref *local_refs = NULL;
1356 int match_flags = MATCH_REFS_NONE;
1357 int verbose = (transport->verbose > 0);
1358 int quiet = (transport->verbose < 0);
1359 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1360 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1361 int push_ret, err;
1362 int ret = -1;
1363 struct transport_ls_refs_options transport_options =
1364 TRANSPORT_LS_REFS_OPTIONS_INIT;
1366 *reject_reasons = 0;
1368 if (transport_color_config() < 0)
1369 goto done;
1371 if (!transport->vtable->push_refs)
1372 goto done;
1374 local_refs = get_local_heads();
1376 if (check_push_refs(local_refs, rs) < 0)
1377 goto done;
1379 refspec_ref_prefixes(rs, &transport_options.ref_prefixes);
1381 trace2_region_enter("transport_push", "get_refs_list", r);
1382 remote_refs = transport->vtable->get_refs_list(transport, 1,
1383 &transport_options);
1384 trace2_region_leave("transport_push", "get_refs_list", r);
1386 transport_ls_refs_options_release(&transport_options);
1388 if (flags & TRANSPORT_PUSH_ALL)
1389 match_flags |= MATCH_REFS_ALL;
1390 if (flags & TRANSPORT_PUSH_MIRROR)
1391 match_flags |= MATCH_REFS_MIRROR;
1392 if (flags & TRANSPORT_PUSH_PRUNE)
1393 match_flags |= MATCH_REFS_PRUNE;
1394 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1395 match_flags |= MATCH_REFS_FOLLOW_TAGS;
1397 if (match_push_refs(local_refs, &remote_refs, rs, match_flags))
1398 goto done;
1400 if (transport->smart_options &&
1401 transport->smart_options->cas &&
1402 !is_empty_cas(transport->smart_options->cas))
1403 apply_push_cas(transport->smart_options->cas,
1404 transport->remote, remote_refs);
1406 set_ref_status_for_push(remote_refs,
1407 flags & TRANSPORT_PUSH_MIRROR,
1408 flags & TRANSPORT_PUSH_FORCE);
1410 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1411 if (run_pre_push_hook(transport, remote_refs))
1412 goto done;
1414 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1415 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1416 !is_bare_repository()) {
1417 struct ref *ref = remote_refs;
1418 struct oid_array commits = OID_ARRAY_INIT;
1420 trace2_region_enter("transport_push", "push_submodules", r);
1421 for (; ref; ref = ref->next)
1422 if (!is_null_oid(&ref->new_oid))
1423 oid_array_append(&commits,
1424 &ref->new_oid);
1426 if (!push_unpushed_submodules(r,
1427 &commits,
1428 transport->remote,
1430 transport->push_options,
1431 pretend)) {
1432 oid_array_clear(&commits);
1433 trace2_region_leave("transport_push", "push_submodules", r);
1434 die(_("failed to push all needed submodules"));
1436 oid_array_clear(&commits);
1437 trace2_region_leave("transport_push", "push_submodules", r);
1440 if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1441 ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1442 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1443 !pretend)) && !is_bare_repository()) {
1444 struct ref *ref = remote_refs;
1445 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1446 struct oid_array commits = OID_ARRAY_INIT;
1448 trace2_region_enter("transport_push", "check_submodules", r);
1449 for (; ref; ref = ref->next)
1450 if (!is_null_oid(&ref->new_oid))
1451 oid_array_append(&commits,
1452 &ref->new_oid);
1454 if (find_unpushed_submodules(r,
1455 &commits,
1456 transport->remote->name,
1457 &needs_pushing)) {
1458 oid_array_clear(&commits);
1459 trace2_region_leave("transport_push", "check_submodules", r);
1460 die_with_unpushed_submodules(&needs_pushing);
1462 string_list_clear(&needs_pushing, 0);
1463 oid_array_clear(&commits);
1464 trace2_region_leave("transport_push", "check_submodules", r);
1467 if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY)) {
1468 trace2_region_enter("transport_push", "push_refs", r);
1469 push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1470 trace2_region_leave("transport_push", "push_refs", r);
1471 } else
1472 push_ret = 0;
1473 err = push_had_errors(remote_refs);
1474 ret = push_ret | err;
1476 if (!quiet || err)
1477 transport_print_push_status(transport->url, remote_refs,
1478 verbose | porcelain, porcelain,
1479 reject_reasons);
1481 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1482 set_upstreams(transport, remote_refs, pretend);
1484 if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1485 TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1486 struct ref *ref;
1487 for (ref = remote_refs; ref; ref = ref->next)
1488 transport_update_tracking_ref(transport->remote, ref, verbose);
1491 if (porcelain && !push_ret)
1492 puts("Done");
1493 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1494 /* stable plumbing output; do not modify or localize */
1495 fprintf(stderr, "Everything up-to-date\n");
1497 done:
1498 free_refs(local_refs);
1499 free_refs(remote_refs);
1500 return ret;
1503 const struct ref *transport_get_remote_refs(struct transport *transport,
1504 struct transport_ls_refs_options *transport_options)
1506 if (!transport->got_remote_refs) {
1507 transport->remote_refs =
1508 transport->vtable->get_refs_list(transport, 0,
1509 transport_options);
1510 transport->got_remote_refs = 1;
1513 return transport->remote_refs;
1516 void transport_ls_refs_options_release(struct transport_ls_refs_options *opts)
1518 strvec_clear(&opts->ref_prefixes);
1519 free((char *)opts->unborn_head_target);
1522 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1524 int rc;
1525 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1526 struct ref **heads = NULL;
1527 struct ref *rm;
1529 for (rm = refs; rm; rm = rm->next) {
1530 nr_refs++;
1531 if (rm->peer_ref &&
1532 !is_null_oid(&rm->old_oid) &&
1533 oideq(&rm->peer_ref->old_oid, &rm->old_oid))
1534 continue;
1535 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1536 heads[nr_heads++] = rm;
1539 if (!nr_heads) {
1541 * When deepening of a shallow repository is requested,
1542 * then local and remote refs are likely to still be equal.
1543 * Just feed them all to the fetch method in that case.
1544 * This condition shouldn't be met in a non-deepening fetch
1545 * (see builtin/fetch.c:quickfetch()).
1547 ALLOC_ARRAY(heads, nr_refs);
1548 for (rm = refs; rm; rm = rm->next)
1549 heads[nr_heads++] = rm;
1552 rc = transport->vtable->fetch_refs(transport, nr_heads, heads);
1554 free(heads);
1555 return rc;
1558 int transport_get_remote_bundle_uri(struct transport *transport)
1560 int value = 0;
1561 const struct transport_vtable *vtable = transport->vtable;
1563 /* Check config only once. */
1564 if (transport->got_remote_bundle_uri)
1565 return 0;
1566 transport->got_remote_bundle_uri = 1;
1569 * Don't request bundle-uri from the server unless configured to
1570 * do so by the transfer.bundleURI=true config option.
1572 if (git_config_get_bool("transfer.bundleuri", &value) || !value)
1573 return 0;
1575 if (!transport->bundles->baseURI)
1576 transport->bundles->baseURI = xstrdup(transport->url);
1578 if (!vtable->get_bundle_uri)
1579 return error(_("bundle-uri operation not supported by protocol"));
1581 if (vtable->get_bundle_uri(transport) < 0)
1582 return error(_("could not retrieve server-advertised bundle-uri list"));
1583 return 0;
1586 void transport_unlock_pack(struct transport *transport, unsigned int flags)
1588 int in_signal_handler = !!(flags & TRANSPORT_UNLOCK_PACK_IN_SIGNAL_HANDLER);
1589 int i;
1591 for (i = 0; i < transport->pack_lockfiles.nr; i++)
1592 if (in_signal_handler)
1593 unlink(transport->pack_lockfiles.items[i].string);
1594 else
1595 unlink_or_warn(transport->pack_lockfiles.items[i].string);
1596 if (!in_signal_handler)
1597 string_list_clear(&transport->pack_lockfiles, 0);
1600 int transport_connect(struct transport *transport, const char *name,
1601 const char *exec, int fd[2])
1603 if (transport->vtable->connect)
1604 return transport->vtable->connect(transport, name, exec, fd);
1605 else
1606 die(_("operation not supported by protocol"));
1609 int transport_disconnect(struct transport *transport)
1611 int ret = 0;
1612 if (transport->vtable->disconnect)
1613 ret = transport->vtable->disconnect(transport);
1614 if (transport->got_remote_refs)
1615 free_refs((void *)transport->remote_refs);
1616 clear_bundle_list(transport->bundles);
1617 free(transport->bundles);
1618 free(transport);
1619 return ret;
1623 * Strip username (and password) from a URL and return
1624 * it in a newly allocated string.
1626 char *transport_anonymize_url(const char *url)
1628 char *scheme_prefix, *anon_part;
1629 size_t anon_len, prefix_len = 0;
1631 anon_part = strchr(url, '@');
1632 if (url_is_local_not_ssh(url) || !anon_part)
1633 goto literal_copy;
1635 anon_len = strlen(++anon_part);
1636 scheme_prefix = strstr(url, "://");
1637 if (!scheme_prefix) {
1638 if (!strchr(anon_part, ':'))
1639 /* cannot be "me@there:/path/name" */
1640 goto literal_copy;
1641 } else {
1642 const char *cp;
1643 /* make sure scheme is reasonable */
1644 for (cp = url; cp < scheme_prefix; cp++) {
1645 switch (*cp) {
1646 /* RFC 1738 2.1 */
1647 case '+': case '.': case '-':
1648 break; /* ok */
1649 default:
1650 if (isalnum(*cp))
1651 break;
1652 /* it isn't */
1653 goto literal_copy;
1656 /* @ past the first slash does not count */
1657 cp = strchr(scheme_prefix + 3, '/');
1658 if (cp && cp < anon_part)
1659 goto literal_copy;
1660 prefix_len = scheme_prefix - url + 3;
1662 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1663 (int)anon_len, anon_part);
1664 literal_copy:
1665 return xstrdup(url);