l10n: fr v2.28.0 round 1
[git/debian.git] / transport.c
blobb41386eccb4f6432dcdcd8300ee49f8031bfad56
1 #include "cache.h"
2 #include "config.h"
3 #include "transport.h"
4 #include "run-command.h"
5 #include "pkt-line.h"
6 #include "fetch-pack.h"
7 #include "remote.h"
8 #include "connect.h"
9 #include "send-pack.h"
10 #include "walker.h"
11 #include "bundle.h"
12 #include "dir.h"
13 #include "refs.h"
14 #include "refspec.h"
15 #include "branch.h"
16 #include "url.h"
17 #include "submodule.h"
18 #include "string-list.h"
19 #include "oid-array.h"
20 #include "sigchain.h"
21 #include "transport-internal.h"
22 #include "protocol.h"
23 #include "object-store.h"
24 #include "color.h"
26 static int transport_use_color = -1;
27 static char transport_colors[][COLOR_MAXLEN] = {
28 GIT_COLOR_RESET,
29 GIT_COLOR_RED /* REJECTED */
32 enum color_transport {
33 TRANSPORT_COLOR_RESET = 0,
34 TRANSPORT_COLOR_REJECTED = 1
37 static int transport_color_config(void)
39 const char *keys[] = {
40 "color.transport.reset",
41 "color.transport.rejected"
42 }, *key = "color.transport";
43 char *value;
44 int i;
45 static int initialized;
47 if (initialized)
48 return 0;
49 initialized = 1;
51 if (!git_config_get_string(key, &value))
52 transport_use_color = git_config_colorbool(key, value);
54 if (!want_color_stderr(transport_use_color))
55 return 0;
57 for (i = 0; i < ARRAY_SIZE(keys); i++)
58 if (!git_config_get_string(keys[i], &value)) {
59 if (!value)
60 return config_error_nonbool(keys[i]);
61 if (color_parse(value, transport_colors[i]) < 0)
62 return -1;
65 return 0;
68 static const char *transport_get_color(enum color_transport ix)
70 if (want_color_stderr(transport_use_color))
71 return transport_colors[ix];
72 return "";
75 static void set_upstreams(struct transport *transport, struct ref *refs,
76 int pretend)
78 struct ref *ref;
79 for (ref = refs; ref; ref = ref->next) {
80 const char *localname;
81 const char *tmp;
82 const char *remotename;
83 int flag = 0;
85 * Check suitability for tracking. Must be successful /
86 * already up-to-date ref create/modify (not delete).
88 if (ref->status != REF_STATUS_OK &&
89 ref->status != REF_STATUS_UPTODATE)
90 continue;
91 if (!ref->peer_ref)
92 continue;
93 if (is_null_oid(&ref->new_oid))
94 continue;
96 /* Follow symbolic refs (mainly for HEAD). */
97 localname = ref->peer_ref->name;
98 remotename = ref->name;
99 tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING,
100 NULL, &flag);
101 if (tmp && flag & REF_ISSYMREF &&
102 starts_with(tmp, "refs/heads/"))
103 localname = tmp;
105 /* Both source and destination must be local branches. */
106 if (!localname || !starts_with(localname, "refs/heads/"))
107 continue;
108 if (!remotename || !starts_with(remotename, "refs/heads/"))
109 continue;
111 if (!pretend)
112 install_branch_config(BRANCH_CONFIG_VERBOSE,
113 localname + 11, transport->remote->name,
114 remotename);
115 else
116 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
117 localname + 11, remotename + 11,
118 transport->remote->name);
122 struct bundle_transport_data {
123 int fd;
124 struct bundle_header header;
125 unsigned get_refs_from_bundle_called : 1;
128 static struct ref *get_refs_from_bundle(struct transport *transport,
129 int for_push,
130 const struct argv_array *ref_prefixes)
132 struct bundle_transport_data *data = transport->data;
133 struct ref *result = NULL;
134 int i;
136 if (for_push)
137 return NULL;
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;
149 for (i = 0; i < data->header.references.nr; i++) {
150 struct ref_list_entry *e = data->header.references.list + i;
151 struct ref *ref = alloc_ref(e->name);
152 oidcpy(&ref->old_oid, &e->oid);
153 ref->next = result;
154 result = ref;
156 return result;
159 static int fetch_refs_from_bundle(struct transport *transport,
160 int nr_heads, struct ref **to_fetch)
162 struct bundle_transport_data *data = transport->data;
163 int ret;
165 if (!data->get_refs_from_bundle_called)
166 get_refs_from_bundle(transport, 0, NULL);
167 ret = unbundle(the_repository, &data->header, data->fd,
168 transport->progress ? BUNDLE_VERBOSE : 0);
169 transport->hash_algo = data->header.hash_algo;
170 return ret;
173 static int close_bundle(struct transport *transport)
175 struct bundle_transport_data *data = transport->data;
176 if (data->fd > 0)
177 close(data->fd);
178 free(data);
179 return 0;
182 struct git_transport_data {
183 struct git_transport_options options;
184 struct child_process *conn;
185 int fd[2];
186 unsigned got_remote_heads : 1;
187 enum protocol_version version;
188 struct oid_array extra_have;
189 struct oid_array shallow;
192 static int set_git_option(struct git_transport_options *opts,
193 const char *name, const char *value)
195 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
196 opts->uploadpack = value;
197 return 0;
198 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
199 opts->receivepack = value;
200 return 0;
201 } else if (!strcmp(name, TRANS_OPT_THIN)) {
202 opts->thin = !!value;
203 return 0;
204 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
205 opts->followtags = !!value;
206 return 0;
207 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
208 opts->keep = !!value;
209 return 0;
210 } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
211 opts->update_shallow = !!value;
212 return 0;
213 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
214 if (!value)
215 opts->depth = 0;
216 else {
217 char *end;
218 opts->depth = strtol(value, &end, 0);
219 if (*end)
220 die(_("transport: invalid depth option '%s'"), value);
222 return 0;
223 } else if (!strcmp(name, TRANS_OPT_DEEPEN_SINCE)) {
224 opts->deepen_since = value;
225 return 0;
226 } else if (!strcmp(name, TRANS_OPT_DEEPEN_NOT)) {
227 opts->deepen_not = (const struct string_list *)value;
228 return 0;
229 } else if (!strcmp(name, TRANS_OPT_DEEPEN_RELATIVE)) {
230 opts->deepen_relative = !!value;
231 return 0;
232 } else if (!strcmp(name, TRANS_OPT_FROM_PROMISOR)) {
233 opts->from_promisor = !!value;
234 return 0;
235 } else if (!strcmp(name, TRANS_OPT_NO_DEPENDENTS)) {
236 opts->no_dependents = !!value;
237 return 0;
238 } else if (!strcmp(name, TRANS_OPT_LIST_OBJECTS_FILTER)) {
239 list_objects_filter_die_if_populated(&opts->filter_options);
240 parse_list_objects_filter(&opts->filter_options, value);
241 return 0;
243 return 1;
246 static int connect_setup(struct transport *transport, int for_push)
248 struct git_transport_data *data = transport->data;
249 int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
251 if (data->conn)
252 return 0;
254 switch (transport->family) {
255 case TRANSPORT_FAMILY_ALL: break;
256 case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
257 case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
260 data->conn = git_connect(data->fd, transport->url,
261 for_push ? data->options.receivepack :
262 data->options.uploadpack,
263 flags);
265 return 0;
268 static void die_if_server_options(struct transport *transport)
270 if (!transport->server_options || !transport->server_options->nr)
271 return;
272 advise(_("see protocol.version in 'git help config' for more details"));
273 die(_("server options require protocol version 2 or later"));
277 * Obtains the protocol version from the transport and writes it to
278 * transport->data->version, first connecting if not already connected.
280 * If the protocol version is one that allows skipping the listing of remote
281 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
282 * this function returns NULL. Otherwise, this function returns the list of
283 * remote refs.
285 static struct ref *handshake(struct transport *transport, int for_push,
286 const struct argv_array *ref_prefixes,
287 int must_list_refs)
289 struct git_transport_data *data = transport->data;
290 struct ref *refs = NULL;
291 struct packet_reader reader;
293 connect_setup(transport, for_push);
295 packet_reader_init(&reader, data->fd[0], NULL, 0,
296 PACKET_READ_CHOMP_NEWLINE |
297 PACKET_READ_GENTLE_ON_EOF |
298 PACKET_READ_DIE_ON_ERR_PACKET);
300 data->version = discover_version(&reader);
301 switch (data->version) {
302 case protocol_v2:
303 if (must_list_refs)
304 get_remote_refs(data->fd[1], &reader, &refs, for_push,
305 ref_prefixes,
306 transport->server_options,
307 transport->stateless_rpc);
308 break;
309 case protocol_v1:
310 case protocol_v0:
311 die_if_server_options(transport);
312 get_remote_heads(&reader, &refs,
313 for_push ? REF_NORMAL : 0,
314 &data->extra_have,
315 &data->shallow);
316 break;
317 case protocol_unknown_version:
318 BUG("unknown protocol version");
320 data->got_remote_heads = 1;
321 transport->hash_algo = reader.hash_algo;
323 if (reader.line_peeked)
324 BUG("buffer must be empty at the end of handshake()");
326 return refs;
329 static struct ref *get_refs_via_connect(struct transport *transport, int for_push,
330 const struct argv_array *ref_prefixes)
332 return handshake(transport, for_push, ref_prefixes, 1);
335 static int fetch_refs_via_pack(struct transport *transport,
336 int nr_heads, struct ref **to_fetch)
338 int ret = 0;
339 struct git_transport_data *data = transport->data;
340 struct ref *refs = NULL;
341 struct fetch_pack_args args;
342 struct ref *refs_tmp = NULL;
344 memset(&args, 0, sizeof(args));
345 args.uploadpack = data->options.uploadpack;
346 args.keep_pack = data->options.keep;
347 args.lock_pack = 1;
348 args.use_thin_pack = data->options.thin;
349 args.include_tag = data->options.followtags;
350 args.verbose = (transport->verbose > 1);
351 args.quiet = (transport->verbose < 0);
352 args.no_progress = !transport->progress;
353 args.depth = data->options.depth;
354 args.deepen_since = data->options.deepen_since;
355 args.deepen_not = data->options.deepen_not;
356 args.deepen_relative = data->options.deepen_relative;
357 args.check_self_contained_and_connected =
358 data->options.check_self_contained_and_connected;
359 args.cloning = transport->cloning;
360 args.update_shallow = data->options.update_shallow;
361 args.from_promisor = data->options.from_promisor;
362 args.no_dependents = data->options.no_dependents;
363 args.filter_options = data->options.filter_options;
364 args.stateless_rpc = transport->stateless_rpc;
365 args.server_options = transport->server_options;
366 args.negotiation_tips = data->options.negotiation_tips;
368 if (!data->got_remote_heads) {
369 int i;
370 int must_list_refs = 0;
371 for (i = 0; i < nr_heads; i++) {
372 if (!to_fetch[i]->exact_oid) {
373 must_list_refs = 1;
374 break;
377 refs_tmp = handshake(transport, 0, NULL, must_list_refs);
380 if (data->version == protocol_unknown_version)
381 BUG("unknown protocol version");
382 else if (data->version <= protocol_v1)
383 die_if_server_options(transport);
385 refs = fetch_pack(&args, data->fd,
386 refs_tmp ? refs_tmp : transport->remote_refs,
387 to_fetch, nr_heads, &data->shallow,
388 &transport->pack_lockfiles, data->version);
390 close(data->fd[0]);
391 close(data->fd[1]);
392 if (finish_connect(data->conn))
393 ret = -1;
394 data->conn = NULL;
395 data->got_remote_heads = 0;
396 data->options.self_contained_and_connected =
397 args.self_contained_and_connected;
398 data->options.connectivity_checked = args.connectivity_checked;
400 if (refs == NULL)
401 ret = -1;
402 if (report_unmatched_refs(to_fetch, nr_heads))
403 ret = -1;
405 free_refs(refs_tmp);
406 free_refs(refs);
407 return ret;
410 static int push_had_errors(struct ref *ref)
412 for (; ref; ref = ref->next) {
413 switch (ref->status) {
414 case REF_STATUS_NONE:
415 case REF_STATUS_UPTODATE:
416 case REF_STATUS_OK:
417 break;
418 default:
419 return 1;
422 return 0;
425 int transport_refs_pushed(struct ref *ref)
427 for (; ref; ref = ref->next) {
428 switch(ref->status) {
429 case REF_STATUS_NONE:
430 case REF_STATUS_UPTODATE:
431 break;
432 default:
433 return 1;
436 return 0;
439 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
441 struct refspec_item rs;
443 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
444 return;
446 rs.src = ref->name;
447 rs.dst = NULL;
449 if (!remote_find_tracking(remote, &rs)) {
450 if (verbose)
451 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
452 if (ref->deletion) {
453 delete_ref(NULL, rs.dst, NULL, 0);
454 } else
455 update_ref("update by push", rs.dst, &ref->new_oid,
456 NULL, 0, 0);
457 free(rs.dst);
461 static void print_ref_status(char flag, const char *summary,
462 struct ref *to, struct ref *from, const char *msg,
463 int porcelain, int summary_width)
465 if (porcelain) {
466 if (from)
467 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
468 else
469 fprintf(stdout, "%c\t:%s\t", flag, to->name);
470 if (msg)
471 fprintf(stdout, "%s (%s)\n", summary, msg);
472 else
473 fprintf(stdout, "%s\n", summary);
474 } else {
475 const char *red = "", *reset = "";
476 if (push_had_errors(to)) {
477 red = transport_get_color(TRANSPORT_COLOR_REJECTED);
478 reset = transport_get_color(TRANSPORT_COLOR_RESET);
480 fprintf(stderr, " %s%c %-*s%s ", red, flag, summary_width,
481 summary, reset);
482 if (from)
483 fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
484 else
485 fputs(prettify_refname(to->name), stderr);
486 if (msg) {
487 fputs(" (", stderr);
488 fputs(msg, stderr);
489 fputc(')', stderr);
491 fputc('\n', stderr);
495 static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_width)
497 if (ref->deletion)
498 print_ref_status('-', "[deleted]", ref, NULL, NULL,
499 porcelain, summary_width);
500 else if (is_null_oid(&ref->old_oid))
501 print_ref_status('*',
502 (starts_with(ref->name, "refs/tags/") ? "[new tag]" :
503 "[new branch]"),
504 ref, ref->peer_ref, NULL, porcelain, summary_width);
505 else {
506 struct strbuf quickref = STRBUF_INIT;
507 char type;
508 const char *msg;
510 strbuf_add_unique_abbrev(&quickref, &ref->old_oid,
511 DEFAULT_ABBREV);
512 if (ref->forced_update) {
513 strbuf_addstr(&quickref, "...");
514 type = '+';
515 msg = "forced update";
516 } else {
517 strbuf_addstr(&quickref, "..");
518 type = ' ';
519 msg = NULL;
521 strbuf_add_unique_abbrev(&quickref, &ref->new_oid,
522 DEFAULT_ABBREV);
524 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
525 porcelain, summary_width);
526 strbuf_release(&quickref);
530 static int print_one_push_status(struct ref *ref, const char *dest, int count,
531 int porcelain, int summary_width)
533 if (!count) {
534 char *url = transport_anonymize_url(dest);
535 fprintf(porcelain ? stdout : stderr, "To %s\n", url);
536 free(url);
539 switch(ref->status) {
540 case REF_STATUS_NONE:
541 print_ref_status('X', "[no match]", ref, NULL, NULL,
542 porcelain, summary_width);
543 break;
544 case REF_STATUS_REJECT_NODELETE:
545 print_ref_status('!', "[rejected]", ref, NULL,
546 "remote does not support deleting refs",
547 porcelain, summary_width);
548 break;
549 case REF_STATUS_UPTODATE:
550 print_ref_status('=', "[up to date]", ref,
551 ref->peer_ref, NULL, porcelain, summary_width);
552 break;
553 case REF_STATUS_REJECT_NONFASTFORWARD:
554 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
555 "non-fast-forward", porcelain, summary_width);
556 break;
557 case REF_STATUS_REJECT_ALREADY_EXISTS:
558 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
559 "already exists", porcelain, summary_width);
560 break;
561 case REF_STATUS_REJECT_FETCH_FIRST:
562 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
563 "fetch first", porcelain, summary_width);
564 break;
565 case REF_STATUS_REJECT_NEEDS_FORCE:
566 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
567 "needs force", porcelain, summary_width);
568 break;
569 case REF_STATUS_REJECT_STALE:
570 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
571 "stale info", porcelain, summary_width);
572 break;
573 case REF_STATUS_REJECT_SHALLOW:
574 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
575 "new shallow roots not allowed",
576 porcelain, summary_width);
577 break;
578 case REF_STATUS_REMOTE_REJECT:
579 print_ref_status('!', "[remote rejected]", ref,
580 ref->deletion ? NULL : ref->peer_ref,
581 ref->remote_status, porcelain, summary_width);
582 break;
583 case REF_STATUS_EXPECTING_REPORT:
584 print_ref_status('!', "[remote failure]", ref,
585 ref->deletion ? NULL : ref->peer_ref,
586 "remote failed to report status",
587 porcelain, summary_width);
588 break;
589 case REF_STATUS_ATOMIC_PUSH_FAILED:
590 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
591 "atomic push failed", porcelain, summary_width);
592 break;
593 case REF_STATUS_OK:
594 print_ok_ref_status(ref, porcelain, summary_width);
595 break;
598 return 1;
601 static int measure_abbrev(const struct object_id *oid, int sofar)
603 char hex[GIT_MAX_HEXSZ + 1];
604 int w = find_unique_abbrev_r(hex, oid, DEFAULT_ABBREV);
606 return (w < sofar) ? sofar : w;
609 int transport_summary_width(const struct ref *refs)
611 int maxw = -1;
613 for (; refs; refs = refs->next) {
614 maxw = measure_abbrev(&refs->old_oid, maxw);
615 maxw = measure_abbrev(&refs->new_oid, maxw);
617 if (maxw < 0)
618 maxw = FALLBACK_DEFAULT_ABBREV;
619 return (2 * maxw + 3);
622 void transport_print_push_status(const char *dest, struct ref *refs,
623 int verbose, int porcelain, unsigned int *reject_reasons)
625 struct ref *ref;
626 int n = 0;
627 char *head;
628 int summary_width = transport_summary_width(refs);
630 if (transport_color_config() < 0)
631 warning(_("could not parse transport.color.* config"));
633 head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
635 if (verbose) {
636 for (ref = refs; ref; ref = ref->next)
637 if (ref->status == REF_STATUS_UPTODATE)
638 n += print_one_push_status(ref, dest, n,
639 porcelain, summary_width);
642 for (ref = refs; ref; ref = ref->next)
643 if (ref->status == REF_STATUS_OK)
644 n += print_one_push_status(ref, dest, n,
645 porcelain, summary_width);
647 *reject_reasons = 0;
648 for (ref = refs; ref; ref = ref->next) {
649 if (ref->status != REF_STATUS_NONE &&
650 ref->status != REF_STATUS_UPTODATE &&
651 ref->status != REF_STATUS_OK)
652 n += print_one_push_status(ref, dest, n,
653 porcelain, summary_width);
654 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
655 if (head != NULL && !strcmp(head, ref->name))
656 *reject_reasons |= REJECT_NON_FF_HEAD;
657 else
658 *reject_reasons |= REJECT_NON_FF_OTHER;
659 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
660 *reject_reasons |= REJECT_ALREADY_EXISTS;
661 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
662 *reject_reasons |= REJECT_FETCH_FIRST;
663 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
664 *reject_reasons |= REJECT_NEEDS_FORCE;
667 free(head);
670 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
672 struct git_transport_data *data = transport->data;
673 struct send_pack_args args;
674 int ret = 0;
676 if (transport_color_config() < 0)
677 return -1;
679 if (!data->got_remote_heads)
680 get_refs_via_connect(transport, 1, NULL);
682 memset(&args, 0, sizeof(args));
683 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
684 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
685 args.use_thin_pack = data->options.thin;
686 args.verbose = (transport->verbose > 0);
687 args.quiet = (transport->verbose < 0);
688 args.progress = transport->progress;
689 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
690 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
691 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
692 args.push_options = transport->push_options;
693 args.url = transport->url;
695 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
696 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
697 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
698 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
699 else
700 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
702 switch (data->version) {
703 case protocol_v2:
704 die(_("support for protocol v2 not implemented yet"));
705 break;
706 case protocol_v1:
707 case protocol_v0:
708 ret = send_pack(&args, data->fd, data->conn, remote_refs,
709 &data->extra_have);
710 break;
711 case protocol_unknown_version:
712 BUG("unknown protocol version");
715 close(data->fd[1]);
716 close(data->fd[0]);
718 * Atomic push may abort the connection early and close the pipe,
719 * which may cause an error for `finish_connect()`. Ignore this error
720 * for atomic git-push.
722 if (ret || args.atomic)
723 finish_connect(data->conn);
724 else
725 ret = finish_connect(data->conn);
726 data->conn = NULL;
727 data->got_remote_heads = 0;
729 return ret;
732 static int connect_git(struct transport *transport, const char *name,
733 const char *executable, int fd[2])
735 struct git_transport_data *data = transport->data;
736 data->conn = git_connect(data->fd, transport->url,
737 executable, 0);
738 fd[0] = data->fd[0];
739 fd[1] = data->fd[1];
740 return 0;
743 static int disconnect_git(struct transport *transport)
745 struct git_transport_data *data = transport->data;
746 if (data->conn) {
747 if (data->got_remote_heads && !transport->stateless_rpc)
748 packet_flush(data->fd[1]);
749 close(data->fd[0]);
750 close(data->fd[1]);
751 finish_connect(data->conn);
754 free(data);
755 return 0;
758 static struct transport_vtable taken_over_vtable = {
759 NULL,
760 get_refs_via_connect,
761 fetch_refs_via_pack,
762 git_transport_push,
763 NULL,
764 disconnect_git
767 void transport_take_over(struct transport *transport,
768 struct child_process *child)
770 struct git_transport_data *data;
772 if (!transport->smart_options)
773 BUG("taking over transport requires non-NULL "
774 "smart_options field.");
776 data = xcalloc(1, sizeof(*data));
777 data->options = *transport->smart_options;
778 data->conn = child;
779 data->fd[0] = data->conn->out;
780 data->fd[1] = data->conn->in;
781 data->got_remote_heads = 0;
782 transport->data = data;
784 transport->vtable = &taken_over_vtable;
785 transport->smart_options = &(data->options);
787 transport->cannot_reuse = 1;
790 static int is_file(const char *url)
792 struct stat buf;
793 if (stat(url, &buf))
794 return 0;
795 return S_ISREG(buf.st_mode);
798 static int external_specification_len(const char *url)
800 return strchr(url, ':') - url;
803 static const struct string_list *protocol_whitelist(void)
805 static int enabled = -1;
806 static struct string_list allowed = STRING_LIST_INIT_DUP;
808 if (enabled < 0) {
809 const char *v = getenv("GIT_ALLOW_PROTOCOL");
810 if (v) {
811 string_list_split(&allowed, v, ':', -1);
812 string_list_sort(&allowed);
813 enabled = 1;
814 } else {
815 enabled = 0;
819 return enabled ? &allowed : NULL;
822 enum protocol_allow_config {
823 PROTOCOL_ALLOW_NEVER = 0,
824 PROTOCOL_ALLOW_USER_ONLY,
825 PROTOCOL_ALLOW_ALWAYS
828 static enum protocol_allow_config parse_protocol_config(const char *key,
829 const char *value)
831 if (!strcasecmp(value, "always"))
832 return PROTOCOL_ALLOW_ALWAYS;
833 else if (!strcasecmp(value, "never"))
834 return PROTOCOL_ALLOW_NEVER;
835 else if (!strcasecmp(value, "user"))
836 return PROTOCOL_ALLOW_USER_ONLY;
838 die(_("unknown value for config '%s': %s"), key, value);
841 static enum protocol_allow_config get_protocol_config(const char *type)
843 char *key = xstrfmt("protocol.%s.allow", type);
844 char *value;
846 /* first check the per-protocol config */
847 if (!git_config_get_string(key, &value)) {
848 enum protocol_allow_config ret =
849 parse_protocol_config(key, value);
850 free(key);
851 free(value);
852 return ret;
854 free(key);
856 /* if defined, fallback to user-defined default for unknown protocols */
857 if (!git_config_get_string("protocol.allow", &value)) {
858 enum protocol_allow_config ret =
859 parse_protocol_config("protocol.allow", value);
860 free(value);
861 return ret;
864 /* fallback to built-in defaults */
865 /* known safe */
866 if (!strcmp(type, "http") ||
867 !strcmp(type, "https") ||
868 !strcmp(type, "git") ||
869 !strcmp(type, "ssh") ||
870 !strcmp(type, "file"))
871 return PROTOCOL_ALLOW_ALWAYS;
873 /* known scary; err on the side of caution */
874 if (!strcmp(type, "ext"))
875 return PROTOCOL_ALLOW_NEVER;
877 /* unknown; by default let them be used only directly by the user */
878 return PROTOCOL_ALLOW_USER_ONLY;
881 int is_transport_allowed(const char *type, int from_user)
883 const struct string_list *whitelist = protocol_whitelist();
884 if (whitelist)
885 return string_list_has_string(whitelist, type);
887 switch (get_protocol_config(type)) {
888 case PROTOCOL_ALLOW_ALWAYS:
889 return 1;
890 case PROTOCOL_ALLOW_NEVER:
891 return 0;
892 case PROTOCOL_ALLOW_USER_ONLY:
893 if (from_user < 0)
894 from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
895 return from_user;
898 BUG("invalid protocol_allow_config type");
901 void transport_check_allowed(const char *type)
903 if (!is_transport_allowed(type, -1))
904 die(_("transport '%s' not allowed"), type);
907 static struct transport_vtable bundle_vtable = {
908 NULL,
909 get_refs_from_bundle,
910 fetch_refs_from_bundle,
911 NULL,
912 NULL,
913 close_bundle
916 static struct transport_vtable builtin_smart_vtable = {
917 NULL,
918 get_refs_via_connect,
919 fetch_refs_via_pack,
920 git_transport_push,
921 connect_git,
922 disconnect_git
925 struct transport *transport_get(struct remote *remote, const char *url)
927 const char *helper;
928 struct transport *ret = xcalloc(1, sizeof(*ret));
930 ret->progress = isatty(2);
931 string_list_init(&ret->pack_lockfiles, 1);
933 if (!remote)
934 BUG("No remote provided to transport_get()");
936 ret->got_remote_refs = 0;
937 ret->remote = remote;
938 helper = remote->foreign_vcs;
940 if (!url && remote->url)
941 url = remote->url[0];
942 ret->url = url;
944 /* maybe it is a foreign URL? */
945 if (url) {
946 const char *p = url;
948 while (is_urlschemechar(p == url, *p))
949 p++;
950 if (starts_with(p, "::"))
951 helper = xstrndup(url, p - url);
954 if (helper) {
955 transport_helper_init(ret, helper);
956 } else if (starts_with(url, "rsync:")) {
957 die(_("git-over-rsync is no longer supported"));
958 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
959 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
960 transport_check_allowed("file");
961 ret->data = data;
962 ret->vtable = &bundle_vtable;
963 ret->smart_options = NULL;
964 } else if (!is_url(url)
965 || starts_with(url, "file://")
966 || starts_with(url, "git://")
967 || starts_with(url, "ssh://")
968 || starts_with(url, "git+ssh://") /* deprecated - do not use */
969 || starts_with(url, "ssh+git://") /* deprecated - do not use */
972 * These are builtin smart transports; "allowed" transports
973 * will be checked individually in git_connect.
975 struct git_transport_data *data = xcalloc(1, sizeof(*data));
976 ret->data = data;
977 ret->vtable = &builtin_smart_vtable;
978 ret->smart_options = &(data->options);
980 data->conn = NULL;
981 data->got_remote_heads = 0;
982 } else {
983 /* Unknown protocol in URL. Pass to external handler. */
984 int len = external_specification_len(url);
985 char *handler = xmemdupz(url, len);
986 transport_helper_init(ret, handler);
989 if (ret->smart_options) {
990 ret->smart_options->thin = 1;
991 ret->smart_options->uploadpack = "git-upload-pack";
992 if (remote->uploadpack)
993 ret->smart_options->uploadpack = remote->uploadpack;
994 ret->smart_options->receivepack = "git-receive-pack";
995 if (remote->receivepack)
996 ret->smart_options->receivepack = remote->receivepack;
999 ret->hash_algo = &hash_algos[GIT_HASH_SHA1];
1001 return ret;
1004 const struct git_hash_algo *transport_get_hash_algo(struct transport *transport)
1006 return transport->hash_algo;
1009 int transport_set_option(struct transport *transport,
1010 const char *name, const char *value)
1012 int git_reports = 1, protocol_reports = 1;
1014 if (transport->smart_options)
1015 git_reports = set_git_option(transport->smart_options,
1016 name, value);
1018 if (transport->vtable->set_option)
1019 protocol_reports = transport->vtable->set_option(transport,
1020 name, value);
1022 /* If either report is 0, report 0 (success). */
1023 if (!git_reports || !protocol_reports)
1024 return 0;
1025 /* If either reports -1 (invalid value), report -1. */
1026 if ((git_reports == -1) || (protocol_reports == -1))
1027 return -1;
1028 /* Otherwise if both report unknown, report unknown. */
1029 return 1;
1032 void transport_set_verbosity(struct transport *transport, int verbosity,
1033 int force_progress)
1035 if (verbosity >= 1)
1036 transport->verbose = verbosity <= 3 ? verbosity : 3;
1037 if (verbosity < 0)
1038 transport->verbose = -1;
1041 * Rules used to determine whether to report progress (processing aborts
1042 * when a rule is satisfied):
1044 * . Report progress, if force_progress is 1 (ie. --progress).
1045 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1046 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1047 * . Report progress if isatty(2) is 1.
1049 if (force_progress >= 0)
1050 transport->progress = !!force_progress;
1051 else
1052 transport->progress = verbosity >= 0 && isatty(2);
1055 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
1057 int i;
1059 fprintf(stderr, _("The following submodule paths contain changes that can\n"
1060 "not be found on any remote:\n"));
1061 for (i = 0; i < needs_pushing->nr; i++)
1062 fprintf(stderr, " %s\n", needs_pushing->items[i].string);
1063 fprintf(stderr, _("\nPlease try\n\n"
1064 " git push --recurse-submodules=on-demand\n\n"
1065 "or cd to the path and use\n\n"
1066 " git push\n\n"
1067 "to push them to a remote.\n\n"));
1069 string_list_clear(needs_pushing, 0);
1071 die(_("Aborting."));
1074 static int run_pre_push_hook(struct transport *transport,
1075 struct ref *remote_refs)
1077 int ret = 0, x;
1078 struct ref *r;
1079 struct child_process proc = CHILD_PROCESS_INIT;
1080 struct strbuf buf;
1081 const char *argv[4];
1083 if (!(argv[0] = find_hook("pre-push")))
1084 return 0;
1086 argv[1] = transport->remote->name;
1087 argv[2] = transport->url;
1088 argv[3] = NULL;
1090 proc.argv = argv;
1091 proc.in = -1;
1092 proc.trace2_hook_name = "pre-push";
1094 if (start_command(&proc)) {
1095 finish_command(&proc);
1096 return -1;
1099 sigchain_push(SIGPIPE, SIG_IGN);
1101 strbuf_init(&buf, 256);
1103 for (r = remote_refs; r; r = r->next) {
1104 if (!r->peer_ref) continue;
1105 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
1106 if (r->status == REF_STATUS_REJECT_STALE) continue;
1107 if (r->status == REF_STATUS_UPTODATE) continue;
1109 strbuf_reset(&buf);
1110 strbuf_addf( &buf, "%s %s %s %s\n",
1111 r->peer_ref->name, oid_to_hex(&r->new_oid),
1112 r->name, oid_to_hex(&r->old_oid));
1114 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
1115 /* We do not mind if a hook does not read all refs. */
1116 if (errno != EPIPE)
1117 ret = -1;
1118 break;
1122 strbuf_release(&buf);
1124 x = close(proc.in);
1125 if (!ret)
1126 ret = x;
1128 sigchain_pop(SIGPIPE);
1130 x = finish_command(&proc);
1131 if (!ret)
1132 ret = x;
1134 return ret;
1137 int transport_push(struct repository *r,
1138 struct transport *transport,
1139 struct refspec *rs, int flags,
1140 unsigned int *reject_reasons)
1142 *reject_reasons = 0;
1144 if (transport_color_config() < 0)
1145 return -1;
1147 if (transport->vtable->push_refs) {
1148 struct ref *remote_refs;
1149 struct ref *local_refs = get_local_heads();
1150 int match_flags = MATCH_REFS_NONE;
1151 int verbose = (transport->verbose > 0);
1152 int quiet = (transport->verbose < 0);
1153 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1154 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1155 int push_ret, ret, err;
1156 struct argv_array ref_prefixes = ARGV_ARRAY_INIT;
1158 if (check_push_refs(local_refs, rs) < 0)
1159 return -1;
1161 refspec_ref_prefixes(rs, &ref_prefixes);
1163 trace2_region_enter("transport_push", "get_refs_list", r);
1164 remote_refs = transport->vtable->get_refs_list(transport, 1,
1165 &ref_prefixes);
1166 trace2_region_leave("transport_push", "get_refs_list", r);
1168 argv_array_clear(&ref_prefixes);
1170 if (flags & TRANSPORT_PUSH_ALL)
1171 match_flags |= MATCH_REFS_ALL;
1172 if (flags & TRANSPORT_PUSH_MIRROR)
1173 match_flags |= MATCH_REFS_MIRROR;
1174 if (flags & TRANSPORT_PUSH_PRUNE)
1175 match_flags |= MATCH_REFS_PRUNE;
1176 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1177 match_flags |= MATCH_REFS_FOLLOW_TAGS;
1179 if (match_push_refs(local_refs, &remote_refs, rs, match_flags))
1180 return -1;
1182 if (transport->smart_options &&
1183 transport->smart_options->cas &&
1184 !is_empty_cas(transport->smart_options->cas))
1185 apply_push_cas(transport->smart_options->cas,
1186 transport->remote, remote_refs);
1188 set_ref_status_for_push(remote_refs,
1189 flags & TRANSPORT_PUSH_MIRROR,
1190 flags & TRANSPORT_PUSH_FORCE);
1192 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1193 if (run_pre_push_hook(transport, remote_refs))
1194 return -1;
1196 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1197 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1198 !is_bare_repository()) {
1199 struct ref *ref = remote_refs;
1200 struct oid_array commits = OID_ARRAY_INIT;
1202 trace2_region_enter("transport_push", "push_submodules", r);
1203 for (; ref; ref = ref->next)
1204 if (!is_null_oid(&ref->new_oid))
1205 oid_array_append(&commits,
1206 &ref->new_oid);
1208 if (!push_unpushed_submodules(r,
1209 &commits,
1210 transport->remote,
1212 transport->push_options,
1213 pretend)) {
1214 oid_array_clear(&commits);
1215 trace2_region_leave("transport_push", "push_submodules", r);
1216 die(_("failed to push all needed submodules"));
1218 oid_array_clear(&commits);
1219 trace2_region_leave("transport_push", "push_submodules", r);
1222 if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1223 ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1224 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1225 !pretend)) && !is_bare_repository()) {
1226 struct ref *ref = remote_refs;
1227 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1228 struct oid_array commits = OID_ARRAY_INIT;
1230 trace2_region_enter("transport_push", "check_submodules", r);
1231 for (; ref; ref = ref->next)
1232 if (!is_null_oid(&ref->new_oid))
1233 oid_array_append(&commits,
1234 &ref->new_oid);
1236 if (find_unpushed_submodules(r,
1237 &commits,
1238 transport->remote->name,
1239 &needs_pushing)) {
1240 oid_array_clear(&commits);
1241 trace2_region_leave("transport_push", "check_submodules", r);
1242 die_with_unpushed_submodules(&needs_pushing);
1244 string_list_clear(&needs_pushing, 0);
1245 oid_array_clear(&commits);
1246 trace2_region_leave("transport_push", "check_submodules", r);
1249 if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY)) {
1250 trace2_region_enter("transport_push", "push_refs", r);
1251 push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1252 trace2_region_leave("transport_push", "push_refs", r);
1253 } else
1254 push_ret = 0;
1255 err = push_had_errors(remote_refs);
1256 ret = push_ret | err;
1258 if (!quiet || err)
1259 transport_print_push_status(transport->url, remote_refs,
1260 verbose | porcelain, porcelain,
1261 reject_reasons);
1263 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1264 set_upstreams(transport, remote_refs, pretend);
1266 if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1267 TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1268 struct ref *ref;
1269 for (ref = remote_refs; ref; ref = ref->next)
1270 transport_update_tracking_ref(transport->remote, ref, verbose);
1273 if (porcelain && !push_ret)
1274 puts("Done");
1275 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1276 fprintf(stderr, "Everything up-to-date\n");
1278 return ret;
1280 return 1;
1283 const struct ref *transport_get_remote_refs(struct transport *transport,
1284 const struct argv_array *ref_prefixes)
1286 if (!transport->got_remote_refs) {
1287 transport->remote_refs =
1288 transport->vtable->get_refs_list(transport, 0,
1289 ref_prefixes);
1290 transport->got_remote_refs = 1;
1293 return transport->remote_refs;
1296 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1298 int rc;
1299 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1300 struct ref **heads = NULL;
1301 struct ref *rm;
1303 for (rm = refs; rm; rm = rm->next) {
1304 nr_refs++;
1305 if (rm->peer_ref &&
1306 !is_null_oid(&rm->old_oid) &&
1307 oideq(&rm->peer_ref->old_oid, &rm->old_oid))
1308 continue;
1309 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1310 heads[nr_heads++] = rm;
1313 if (!nr_heads) {
1315 * When deepening of a shallow repository is requested,
1316 * then local and remote refs are likely to still be equal.
1317 * Just feed them all to the fetch method in that case.
1318 * This condition shouldn't be met in a non-deepening fetch
1319 * (see builtin/fetch.c:quickfetch()).
1321 ALLOC_ARRAY(heads, nr_refs);
1322 for (rm = refs; rm; rm = rm->next)
1323 heads[nr_heads++] = rm;
1326 rc = transport->vtable->fetch(transport, nr_heads, heads);
1328 free(heads);
1329 return rc;
1332 void transport_unlock_pack(struct transport *transport)
1334 int i;
1336 for (i = 0; i < transport->pack_lockfiles.nr; i++)
1337 unlink_or_warn(transport->pack_lockfiles.items[i].string);
1338 string_list_clear(&transport->pack_lockfiles, 0);
1341 int transport_connect(struct transport *transport, const char *name,
1342 const char *exec, int fd[2])
1344 if (transport->vtable->connect)
1345 return transport->vtable->connect(transport, name, exec, fd);
1346 else
1347 die(_("operation not supported by protocol"));
1350 int transport_disconnect(struct transport *transport)
1352 int ret = 0;
1353 if (transport->vtable->disconnect)
1354 ret = transport->vtable->disconnect(transport);
1355 free(transport);
1356 return ret;
1360 * Strip username (and password) from a URL and return
1361 * it in a newly allocated string.
1363 char *transport_anonymize_url(const char *url)
1365 char *scheme_prefix, *anon_part;
1366 size_t anon_len, prefix_len = 0;
1368 anon_part = strchr(url, '@');
1369 if (url_is_local_not_ssh(url) || !anon_part)
1370 goto literal_copy;
1372 anon_len = strlen(++anon_part);
1373 scheme_prefix = strstr(url, "://");
1374 if (!scheme_prefix) {
1375 if (!strchr(anon_part, ':'))
1376 /* cannot be "me@there:/path/name" */
1377 goto literal_copy;
1378 } else {
1379 const char *cp;
1380 /* make sure scheme is reasonable */
1381 for (cp = url; cp < scheme_prefix; cp++) {
1382 switch (*cp) {
1383 /* RFC 1738 2.1 */
1384 case '+': case '.': case '-':
1385 break; /* ok */
1386 default:
1387 if (isalnum(*cp))
1388 break;
1389 /* it isn't */
1390 goto literal_copy;
1393 /* @ past the first slash does not count */
1394 cp = strchr(scheme_prefix + 3, '/');
1395 if (cp && cp < anon_part)
1396 goto literal_copy;
1397 prefix_len = scheme_prefix - url + 3;
1399 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1400 (int)anon_len, anon_part);
1401 literal_copy:
1402 return xstrdup(url);