Merge branch 'js/mingw-default-ident'
[git/debian.git] / transport.c
blobf4ffbd96cb65fdbac3c74d3d60c253bc3a8884f6
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 "sha1-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;
127 static struct ref *get_refs_from_bundle(struct transport *transport,
128 int for_push,
129 const struct argv_array *ref_prefixes)
131 struct bundle_transport_data *data = transport->data;
132 struct ref *result = NULL;
133 int i;
135 if (for_push)
136 return NULL;
138 if (data->fd > 0)
139 close(data->fd);
140 data->fd = read_bundle_header(transport->url, &data->header);
141 if (data->fd < 0)
142 die(_("could not read bundle '%s'"), transport->url);
143 for (i = 0; i < data->header.references.nr; i++) {
144 struct ref_list_entry *e = data->header.references.list + i;
145 struct ref *ref = alloc_ref(e->name);
146 oidcpy(&ref->old_oid, &e->oid);
147 ref->next = result;
148 result = ref;
150 return result;
153 static int fetch_refs_from_bundle(struct transport *transport,
154 int nr_heads, struct ref **to_fetch)
156 struct bundle_transport_data *data = transport->data;
157 return unbundle(&data->header, data->fd,
158 transport->progress ? BUNDLE_VERBOSE : 0);
161 static int close_bundle(struct transport *transport)
163 struct bundle_transport_data *data = transport->data;
164 if (data->fd > 0)
165 close(data->fd);
166 free(data);
167 return 0;
170 struct git_transport_data {
171 struct git_transport_options options;
172 struct child_process *conn;
173 int fd[2];
174 unsigned got_remote_heads : 1;
175 enum protocol_version version;
176 struct oid_array extra_have;
177 struct oid_array shallow;
180 static int set_git_option(struct git_transport_options *opts,
181 const char *name, const char *value)
183 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
184 opts->uploadpack = value;
185 return 0;
186 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
187 opts->receivepack = value;
188 return 0;
189 } else if (!strcmp(name, TRANS_OPT_THIN)) {
190 opts->thin = !!value;
191 return 0;
192 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
193 opts->followtags = !!value;
194 return 0;
195 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
196 opts->keep = !!value;
197 return 0;
198 } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
199 opts->update_shallow = !!value;
200 return 0;
201 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
202 if (!value)
203 opts->depth = 0;
204 else {
205 char *end;
206 opts->depth = strtol(value, &end, 0);
207 if (*end)
208 die(_("transport: invalid depth option '%s'"), value);
210 return 0;
211 } else if (!strcmp(name, TRANS_OPT_DEEPEN_SINCE)) {
212 opts->deepen_since = value;
213 return 0;
214 } else if (!strcmp(name, TRANS_OPT_DEEPEN_NOT)) {
215 opts->deepen_not = (const struct string_list *)value;
216 return 0;
217 } else if (!strcmp(name, TRANS_OPT_DEEPEN_RELATIVE)) {
218 opts->deepen_relative = !!value;
219 return 0;
220 } else if (!strcmp(name, TRANS_OPT_FROM_PROMISOR)) {
221 opts->from_promisor = !!value;
222 return 0;
223 } else if (!strcmp(name, TRANS_OPT_NO_DEPENDENTS)) {
224 opts->no_dependents = !!value;
225 return 0;
226 } else if (!strcmp(name, TRANS_OPT_LIST_OBJECTS_FILTER)) {
227 parse_list_objects_filter(&opts->filter_options, value);
228 return 0;
230 return 1;
233 static int connect_setup(struct transport *transport, int for_push)
235 struct git_transport_data *data = transport->data;
236 int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
238 if (data->conn)
239 return 0;
241 switch (transport->family) {
242 case TRANSPORT_FAMILY_ALL: break;
243 case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
244 case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
247 data->conn = git_connect(data->fd, transport->url,
248 for_push ? data->options.receivepack :
249 data->options.uploadpack,
250 flags);
252 return 0;
256 * Obtains the protocol version from the transport and writes it to
257 * transport->data->version, first connecting if not already connected.
259 * If the protocol version is one that allows skipping the listing of remote
260 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
261 * this function returns NULL. Otherwise, this function returns the list of
262 * remote refs.
264 static struct ref *handshake(struct transport *transport, int for_push,
265 const struct argv_array *ref_prefixes,
266 int must_list_refs)
268 struct git_transport_data *data = transport->data;
269 struct ref *refs = NULL;
270 struct packet_reader reader;
272 connect_setup(transport, for_push);
274 packet_reader_init(&reader, data->fd[0], NULL, 0,
275 PACKET_READ_CHOMP_NEWLINE |
276 PACKET_READ_GENTLE_ON_EOF);
278 data->version = discover_version(&reader);
279 switch (data->version) {
280 case protocol_v2:
281 if (must_list_refs)
282 get_remote_refs(data->fd[1], &reader, &refs, for_push,
283 ref_prefixes,
284 transport->server_options);
285 break;
286 case protocol_v1:
287 case protocol_v0:
288 get_remote_heads(&reader, &refs,
289 for_push ? REF_NORMAL : 0,
290 &data->extra_have,
291 &data->shallow);
292 break;
293 case protocol_unknown_version:
294 BUG("unknown protocol version");
296 data->got_remote_heads = 1;
298 if (reader.line_peeked)
299 BUG("buffer must be empty at the end of handshake()");
301 return refs;
304 static struct ref *get_refs_via_connect(struct transport *transport, int for_push,
305 const struct argv_array *ref_prefixes)
307 return handshake(transport, for_push, ref_prefixes, 1);
310 static int fetch_refs_via_pack(struct transport *transport,
311 int nr_heads, struct ref **to_fetch)
313 int ret = 0;
314 struct git_transport_data *data = transport->data;
315 struct ref *refs = NULL;
316 char *dest = xstrdup(transport->url);
317 struct fetch_pack_args args;
318 struct ref *refs_tmp = NULL;
320 memset(&args, 0, sizeof(args));
321 args.uploadpack = data->options.uploadpack;
322 args.keep_pack = data->options.keep;
323 args.lock_pack = 1;
324 args.use_thin_pack = data->options.thin;
325 args.include_tag = data->options.followtags;
326 args.verbose = (transport->verbose > 1);
327 args.quiet = (transport->verbose < 0);
328 args.no_progress = !transport->progress;
329 args.depth = data->options.depth;
330 args.deepen_since = data->options.deepen_since;
331 args.deepen_not = data->options.deepen_not;
332 args.deepen_relative = data->options.deepen_relative;
333 args.check_self_contained_and_connected =
334 data->options.check_self_contained_and_connected;
335 args.cloning = transport->cloning;
336 args.update_shallow = data->options.update_shallow;
337 args.from_promisor = data->options.from_promisor;
338 args.no_dependents = data->options.no_dependents;
339 args.filter_options = data->options.filter_options;
340 args.stateless_rpc = transport->stateless_rpc;
341 args.server_options = transport->server_options;
342 args.negotiation_tips = data->options.negotiation_tips;
344 if (!data->got_remote_heads) {
345 int i;
346 int must_list_refs = 0;
347 for (i = 0; i < nr_heads; i++) {
348 if (!to_fetch[i]->exact_oid) {
349 must_list_refs = 1;
350 break;
353 refs_tmp = handshake(transport, 0, NULL, must_list_refs);
356 switch (data->version) {
357 case protocol_v2:
358 refs = fetch_pack(&args, data->fd, data->conn,
359 refs_tmp ? refs_tmp : transport->remote_refs,
360 dest, to_fetch, nr_heads, &data->shallow,
361 &transport->pack_lockfile, data->version);
362 break;
363 case protocol_v1:
364 case protocol_v0:
365 refs = fetch_pack(&args, data->fd, data->conn,
366 refs_tmp ? refs_tmp : transport->remote_refs,
367 dest, to_fetch, nr_heads, &data->shallow,
368 &transport->pack_lockfile, data->version);
369 break;
370 case protocol_unknown_version:
371 BUG("unknown protocol version");
374 close(data->fd[0]);
375 close(data->fd[1]);
376 if (finish_connect(data->conn))
377 ret = -1;
378 data->conn = NULL;
379 data->got_remote_heads = 0;
380 data->options.self_contained_and_connected =
381 args.self_contained_and_connected;
382 data->options.connectivity_checked = args.connectivity_checked;
384 if (refs == NULL)
385 ret = -1;
386 if (report_unmatched_refs(to_fetch, nr_heads))
387 ret = -1;
389 free_refs(refs_tmp);
390 free_refs(refs);
391 free(dest);
392 return ret;
395 static int push_had_errors(struct ref *ref)
397 for (; ref; ref = ref->next) {
398 switch (ref->status) {
399 case REF_STATUS_NONE:
400 case REF_STATUS_UPTODATE:
401 case REF_STATUS_OK:
402 break;
403 default:
404 return 1;
407 return 0;
410 int transport_refs_pushed(struct ref *ref)
412 for (; ref; ref = ref->next) {
413 switch(ref->status) {
414 case REF_STATUS_NONE:
415 case REF_STATUS_UPTODATE:
416 break;
417 default:
418 return 1;
421 return 0;
424 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
426 struct refspec_item rs;
428 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
429 return;
431 rs.src = ref->name;
432 rs.dst = NULL;
434 if (!remote_find_tracking(remote, &rs)) {
435 if (verbose)
436 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
437 if (ref->deletion) {
438 delete_ref(NULL, rs.dst, NULL, 0);
439 } else
440 update_ref("update by push", rs.dst, &ref->new_oid,
441 NULL, 0, 0);
442 free(rs.dst);
446 static void print_ref_status(char flag, const char *summary,
447 struct ref *to, struct ref *from, const char *msg,
448 int porcelain, int summary_width)
450 if (porcelain) {
451 if (from)
452 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
453 else
454 fprintf(stdout, "%c\t:%s\t", flag, to->name);
455 if (msg)
456 fprintf(stdout, "%s (%s)\n", summary, msg);
457 else
458 fprintf(stdout, "%s\n", summary);
459 } else {
460 const char *red = "", *reset = "";
461 if (push_had_errors(to)) {
462 red = transport_get_color(TRANSPORT_COLOR_REJECTED);
463 reset = transport_get_color(TRANSPORT_COLOR_RESET);
465 fprintf(stderr, " %s%c %-*s%s ", red, flag, summary_width,
466 summary, reset);
467 if (from)
468 fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
469 else
470 fputs(prettify_refname(to->name), stderr);
471 if (msg) {
472 fputs(" (", stderr);
473 fputs(msg, stderr);
474 fputc(')', stderr);
476 fputc('\n', stderr);
480 static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_width)
482 if (ref->deletion)
483 print_ref_status('-', "[deleted]", ref, NULL, NULL,
484 porcelain, summary_width);
485 else if (is_null_oid(&ref->old_oid))
486 print_ref_status('*',
487 (starts_with(ref->name, "refs/tags/") ? "[new tag]" :
488 "[new branch]"),
489 ref, ref->peer_ref, NULL, porcelain, summary_width);
490 else {
491 struct strbuf quickref = STRBUF_INIT;
492 char type;
493 const char *msg;
495 strbuf_add_unique_abbrev(&quickref, &ref->old_oid,
496 DEFAULT_ABBREV);
497 if (ref->forced_update) {
498 strbuf_addstr(&quickref, "...");
499 type = '+';
500 msg = "forced update";
501 } else {
502 strbuf_addstr(&quickref, "..");
503 type = ' ';
504 msg = NULL;
506 strbuf_add_unique_abbrev(&quickref, &ref->new_oid,
507 DEFAULT_ABBREV);
509 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
510 porcelain, summary_width);
511 strbuf_release(&quickref);
515 static int print_one_push_status(struct ref *ref, const char *dest, int count,
516 int porcelain, int summary_width)
518 if (!count) {
519 char *url = transport_anonymize_url(dest);
520 fprintf(porcelain ? stdout : stderr, "To %s\n", url);
521 free(url);
524 switch(ref->status) {
525 case REF_STATUS_NONE:
526 print_ref_status('X', "[no match]", ref, NULL, NULL,
527 porcelain, summary_width);
528 break;
529 case REF_STATUS_REJECT_NODELETE:
530 print_ref_status('!', "[rejected]", ref, NULL,
531 "remote does not support deleting refs",
532 porcelain, summary_width);
533 break;
534 case REF_STATUS_UPTODATE:
535 print_ref_status('=', "[up to date]", ref,
536 ref->peer_ref, NULL, porcelain, summary_width);
537 break;
538 case REF_STATUS_REJECT_NONFASTFORWARD:
539 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
540 "non-fast-forward", porcelain, summary_width);
541 break;
542 case REF_STATUS_REJECT_ALREADY_EXISTS:
543 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
544 "already exists", porcelain, summary_width);
545 break;
546 case REF_STATUS_REJECT_FETCH_FIRST:
547 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
548 "fetch first", porcelain, summary_width);
549 break;
550 case REF_STATUS_REJECT_NEEDS_FORCE:
551 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
552 "needs force", porcelain, summary_width);
553 break;
554 case REF_STATUS_REJECT_STALE:
555 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
556 "stale info", porcelain, summary_width);
557 break;
558 case REF_STATUS_REJECT_SHALLOW:
559 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
560 "new shallow roots not allowed",
561 porcelain, summary_width);
562 break;
563 case REF_STATUS_REMOTE_REJECT:
564 print_ref_status('!', "[remote rejected]", ref,
565 ref->deletion ? NULL : ref->peer_ref,
566 ref->remote_status, porcelain, summary_width);
567 break;
568 case REF_STATUS_EXPECTING_REPORT:
569 print_ref_status('!', "[remote failure]", ref,
570 ref->deletion ? NULL : ref->peer_ref,
571 "remote failed to report status",
572 porcelain, summary_width);
573 break;
574 case REF_STATUS_ATOMIC_PUSH_FAILED:
575 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
576 "atomic push failed", porcelain, summary_width);
577 break;
578 case REF_STATUS_OK:
579 print_ok_ref_status(ref, porcelain, summary_width);
580 break;
583 return 1;
586 static int measure_abbrev(const struct object_id *oid, int sofar)
588 char hex[GIT_MAX_HEXSZ + 1];
589 int w = find_unique_abbrev_r(hex, oid, DEFAULT_ABBREV);
591 return (w < sofar) ? sofar : w;
594 int transport_summary_width(const struct ref *refs)
596 int maxw = -1;
598 for (; refs; refs = refs->next) {
599 maxw = measure_abbrev(&refs->old_oid, maxw);
600 maxw = measure_abbrev(&refs->new_oid, maxw);
602 if (maxw < 0)
603 maxw = FALLBACK_DEFAULT_ABBREV;
604 return (2 * maxw + 3);
607 void transport_print_push_status(const char *dest, struct ref *refs,
608 int verbose, int porcelain, unsigned int *reject_reasons)
610 struct ref *ref;
611 int n = 0;
612 char *head;
613 int summary_width = transport_summary_width(refs);
615 if (transport_color_config() < 0)
616 warning(_("could not parse transport.color.* config"));
618 head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
620 if (verbose) {
621 for (ref = refs; ref; ref = ref->next)
622 if (ref->status == REF_STATUS_UPTODATE)
623 n += print_one_push_status(ref, dest, n,
624 porcelain, summary_width);
627 for (ref = refs; ref; ref = ref->next)
628 if (ref->status == REF_STATUS_OK)
629 n += print_one_push_status(ref, dest, n,
630 porcelain, summary_width);
632 *reject_reasons = 0;
633 for (ref = refs; ref; ref = ref->next) {
634 if (ref->status != REF_STATUS_NONE &&
635 ref->status != REF_STATUS_UPTODATE &&
636 ref->status != REF_STATUS_OK)
637 n += print_one_push_status(ref, dest, n,
638 porcelain, summary_width);
639 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
640 if (head != NULL && !strcmp(head, ref->name))
641 *reject_reasons |= REJECT_NON_FF_HEAD;
642 else
643 *reject_reasons |= REJECT_NON_FF_OTHER;
644 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
645 *reject_reasons |= REJECT_ALREADY_EXISTS;
646 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
647 *reject_reasons |= REJECT_FETCH_FIRST;
648 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
649 *reject_reasons |= REJECT_NEEDS_FORCE;
652 free(head);
655 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
657 struct git_transport_data *data = transport->data;
658 struct send_pack_args args;
659 int ret = 0;
661 if (transport_color_config() < 0)
662 return -1;
664 if (!data->got_remote_heads)
665 get_refs_via_connect(transport, 1, NULL);
667 memset(&args, 0, sizeof(args));
668 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
669 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
670 args.use_thin_pack = data->options.thin;
671 args.verbose = (transport->verbose > 0);
672 args.quiet = (transport->verbose < 0);
673 args.progress = transport->progress;
674 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
675 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
676 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
677 args.push_options = transport->push_options;
678 args.url = transport->url;
680 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
681 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
682 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
683 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
684 else
685 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
687 switch (data->version) {
688 case protocol_v2:
689 die(_("support for protocol v2 not implemented yet"));
690 break;
691 case protocol_v1:
692 case protocol_v0:
693 ret = send_pack(&args, data->fd, data->conn, remote_refs,
694 &data->extra_have);
695 break;
696 case protocol_unknown_version:
697 BUG("unknown protocol version");
700 close(data->fd[1]);
701 close(data->fd[0]);
702 ret |= finish_connect(data->conn);
703 data->conn = NULL;
704 data->got_remote_heads = 0;
706 return ret;
709 static int connect_git(struct transport *transport, const char *name,
710 const char *executable, int fd[2])
712 struct git_transport_data *data = transport->data;
713 data->conn = git_connect(data->fd, transport->url,
714 executable, 0);
715 fd[0] = data->fd[0];
716 fd[1] = data->fd[1];
717 return 0;
720 static int disconnect_git(struct transport *transport)
722 struct git_transport_data *data = transport->data;
723 if (data->conn) {
724 if (data->got_remote_heads)
725 packet_flush(data->fd[1]);
726 close(data->fd[0]);
727 close(data->fd[1]);
728 finish_connect(data->conn);
731 free(data);
732 return 0;
735 static struct transport_vtable taken_over_vtable = {
737 NULL,
738 get_refs_via_connect,
739 fetch_refs_via_pack,
740 git_transport_push,
741 NULL,
742 disconnect_git
745 void transport_take_over(struct transport *transport,
746 struct child_process *child)
748 struct git_transport_data *data;
750 if (!transport->smart_options)
751 BUG("taking over transport requires non-NULL "
752 "smart_options field.");
754 data = xcalloc(1, sizeof(*data));
755 data->options = *transport->smart_options;
756 data->conn = child;
757 data->fd[0] = data->conn->out;
758 data->fd[1] = data->conn->in;
759 data->got_remote_heads = 0;
760 transport->data = data;
762 transport->vtable = &taken_over_vtable;
763 transport->smart_options = &(data->options);
765 transport->cannot_reuse = 1;
768 static int is_file(const char *url)
770 struct stat buf;
771 if (stat(url, &buf))
772 return 0;
773 return S_ISREG(buf.st_mode);
776 static int external_specification_len(const char *url)
778 return strchr(url, ':') - url;
781 static const struct string_list *protocol_whitelist(void)
783 static int enabled = -1;
784 static struct string_list allowed = STRING_LIST_INIT_DUP;
786 if (enabled < 0) {
787 const char *v = getenv("GIT_ALLOW_PROTOCOL");
788 if (v) {
789 string_list_split(&allowed, v, ':', -1);
790 string_list_sort(&allowed);
791 enabled = 1;
792 } else {
793 enabled = 0;
797 return enabled ? &allowed : NULL;
800 enum protocol_allow_config {
801 PROTOCOL_ALLOW_NEVER = 0,
802 PROTOCOL_ALLOW_USER_ONLY,
803 PROTOCOL_ALLOW_ALWAYS
806 static enum protocol_allow_config parse_protocol_config(const char *key,
807 const char *value)
809 if (!strcasecmp(value, "always"))
810 return PROTOCOL_ALLOW_ALWAYS;
811 else if (!strcasecmp(value, "never"))
812 return PROTOCOL_ALLOW_NEVER;
813 else if (!strcasecmp(value, "user"))
814 return PROTOCOL_ALLOW_USER_ONLY;
816 die(_("unknown value for config '%s': %s"), key, value);
819 static enum protocol_allow_config get_protocol_config(const char *type)
821 char *key = xstrfmt("protocol.%s.allow", type);
822 char *value;
824 /* first check the per-protocol config */
825 if (!git_config_get_string(key, &value)) {
826 enum protocol_allow_config ret =
827 parse_protocol_config(key, value);
828 free(key);
829 free(value);
830 return ret;
832 free(key);
834 /* if defined, fallback to user-defined default for unknown protocols */
835 if (!git_config_get_string("protocol.allow", &value)) {
836 enum protocol_allow_config ret =
837 parse_protocol_config("protocol.allow", value);
838 free(value);
839 return ret;
842 /* fallback to built-in defaults */
843 /* known safe */
844 if (!strcmp(type, "http") ||
845 !strcmp(type, "https") ||
846 !strcmp(type, "git") ||
847 !strcmp(type, "ssh") ||
848 !strcmp(type, "file"))
849 return PROTOCOL_ALLOW_ALWAYS;
851 /* known scary; err on the side of caution */
852 if (!strcmp(type, "ext"))
853 return PROTOCOL_ALLOW_NEVER;
855 /* unknown; by default let them be used only directly by the user */
856 return PROTOCOL_ALLOW_USER_ONLY;
859 int is_transport_allowed(const char *type, int from_user)
861 const struct string_list *whitelist = protocol_whitelist();
862 if (whitelist)
863 return string_list_has_string(whitelist, type);
865 switch (get_protocol_config(type)) {
866 case PROTOCOL_ALLOW_ALWAYS:
867 return 1;
868 case PROTOCOL_ALLOW_NEVER:
869 return 0;
870 case PROTOCOL_ALLOW_USER_ONLY:
871 if (from_user < 0)
872 from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
873 return from_user;
876 BUG("invalid protocol_allow_config type");
879 void transport_check_allowed(const char *type)
881 if (!is_transport_allowed(type, -1))
882 die(_("transport '%s' not allowed"), type);
885 static struct transport_vtable bundle_vtable = {
887 NULL,
888 get_refs_from_bundle,
889 fetch_refs_from_bundle,
890 NULL,
891 NULL,
892 close_bundle
895 static struct transport_vtable builtin_smart_vtable = {
897 NULL,
898 get_refs_via_connect,
899 fetch_refs_via_pack,
900 git_transport_push,
901 connect_git,
902 disconnect_git
905 struct transport *transport_get(struct remote *remote, const char *url)
907 const char *helper;
908 struct transport *ret = xcalloc(1, sizeof(*ret));
910 ret->progress = isatty(2);
912 if (!remote)
913 BUG("No remote provided to transport_get()");
915 ret->got_remote_refs = 0;
916 ret->remote = remote;
917 helper = remote->foreign_vcs;
919 if (!url && remote->url)
920 url = remote->url[0];
921 ret->url = url;
923 /* maybe it is a foreign URL? */
924 if (url) {
925 const char *p = url;
927 while (is_urlschemechar(p == url, *p))
928 p++;
929 if (starts_with(p, "::"))
930 helper = xstrndup(url, p - url);
933 if (helper) {
934 transport_helper_init(ret, helper);
935 } else if (starts_with(url, "rsync:")) {
936 die(_("git-over-rsync is no longer supported"));
937 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
938 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
939 transport_check_allowed("file");
940 ret->data = data;
941 ret->vtable = &bundle_vtable;
942 ret->smart_options = NULL;
943 } else if (!is_url(url)
944 || starts_with(url, "file://")
945 || starts_with(url, "git://")
946 || starts_with(url, "ssh://")
947 || starts_with(url, "git+ssh://") /* deprecated - do not use */
948 || starts_with(url, "ssh+git://") /* deprecated - do not use */
951 * These are builtin smart transports; "allowed" transports
952 * will be checked individually in git_connect.
954 struct git_transport_data *data = xcalloc(1, sizeof(*data));
955 ret->data = data;
956 ret->vtable = &builtin_smart_vtable;
957 ret->smart_options = &(data->options);
959 data->conn = NULL;
960 data->got_remote_heads = 0;
961 } else {
962 /* Unknown protocol in URL. Pass to external handler. */
963 int len = external_specification_len(url);
964 char *handler = xmemdupz(url, len);
965 transport_helper_init(ret, handler);
968 if (ret->smart_options) {
969 ret->smart_options->thin = 1;
970 ret->smart_options->uploadpack = "git-upload-pack";
971 if (remote->uploadpack)
972 ret->smart_options->uploadpack = remote->uploadpack;
973 ret->smart_options->receivepack = "git-receive-pack";
974 if (remote->receivepack)
975 ret->smart_options->receivepack = remote->receivepack;
978 return ret;
981 int transport_set_option(struct transport *transport,
982 const char *name, const char *value)
984 int git_reports = 1, protocol_reports = 1;
986 if (transport->smart_options)
987 git_reports = set_git_option(transport->smart_options,
988 name, value);
990 if (transport->vtable->set_option)
991 protocol_reports = transport->vtable->set_option(transport,
992 name, value);
994 /* If either report is 0, report 0 (success). */
995 if (!git_reports || !protocol_reports)
996 return 0;
997 /* If either reports -1 (invalid value), report -1. */
998 if ((git_reports == -1) || (protocol_reports == -1))
999 return -1;
1000 /* Otherwise if both report unknown, report unknown. */
1001 return 1;
1004 void transport_set_verbosity(struct transport *transport, int verbosity,
1005 int force_progress)
1007 if (verbosity >= 1)
1008 transport->verbose = verbosity <= 3 ? verbosity : 3;
1009 if (verbosity < 0)
1010 transport->verbose = -1;
1013 * Rules used to determine whether to report progress (processing aborts
1014 * when a rule is satisfied):
1016 * . Report progress, if force_progress is 1 (ie. --progress).
1017 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1018 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1019 * . Report progress if isatty(2) is 1.
1021 if (force_progress >= 0)
1022 transport->progress = !!force_progress;
1023 else
1024 transport->progress = verbosity >= 0 && isatty(2);
1027 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
1029 int i;
1031 fprintf(stderr, _("The following submodule paths contain changes that can\n"
1032 "not be found on any remote:\n"));
1033 for (i = 0; i < needs_pushing->nr; i++)
1034 fprintf(stderr, " %s\n", needs_pushing->items[i].string);
1035 fprintf(stderr, _("\nPlease try\n\n"
1036 " git push --recurse-submodules=on-demand\n\n"
1037 "or cd to the path and use\n\n"
1038 " git push\n\n"
1039 "to push them to a remote.\n\n"));
1041 string_list_clear(needs_pushing, 0);
1043 die(_("Aborting."));
1046 static int run_pre_push_hook(struct transport *transport,
1047 struct ref *remote_refs)
1049 int ret = 0, x;
1050 struct ref *r;
1051 struct child_process proc = CHILD_PROCESS_INIT;
1052 struct strbuf buf;
1053 const char *argv[4];
1055 if (!(argv[0] = find_hook("pre-push")))
1056 return 0;
1058 argv[1] = transport->remote->name;
1059 argv[2] = transport->url;
1060 argv[3] = NULL;
1062 proc.argv = argv;
1063 proc.in = -1;
1065 if (start_command(&proc)) {
1066 finish_command(&proc);
1067 return -1;
1070 sigchain_push(SIGPIPE, SIG_IGN);
1072 strbuf_init(&buf, 256);
1074 for (r = remote_refs; r; r = r->next) {
1075 if (!r->peer_ref) continue;
1076 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
1077 if (r->status == REF_STATUS_REJECT_STALE) continue;
1078 if (r->status == REF_STATUS_UPTODATE) continue;
1080 strbuf_reset(&buf);
1081 strbuf_addf( &buf, "%s %s %s %s\n",
1082 r->peer_ref->name, oid_to_hex(&r->new_oid),
1083 r->name, oid_to_hex(&r->old_oid));
1085 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
1086 /* We do not mind if a hook does not read all refs. */
1087 if (errno != EPIPE)
1088 ret = -1;
1089 break;
1093 strbuf_release(&buf);
1095 x = close(proc.in);
1096 if (!ret)
1097 ret = x;
1099 sigchain_pop(SIGPIPE);
1101 x = finish_command(&proc);
1102 if (!ret)
1103 ret = x;
1105 return ret;
1108 int transport_push(struct transport *transport,
1109 struct refspec *rs, int flags,
1110 unsigned int *reject_reasons)
1112 *reject_reasons = 0;
1114 if (transport_color_config() < 0)
1115 return -1;
1117 if (transport->vtable->push_refs) {
1118 struct ref *remote_refs;
1119 struct ref *local_refs = get_local_heads();
1120 int match_flags = MATCH_REFS_NONE;
1121 int verbose = (transport->verbose > 0);
1122 int quiet = (transport->verbose < 0);
1123 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1124 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1125 int push_ret, ret, err;
1126 struct argv_array ref_prefixes = ARGV_ARRAY_INIT;
1128 if (check_push_refs(local_refs, rs) < 0)
1129 return -1;
1131 refspec_ref_prefixes(rs, &ref_prefixes);
1133 remote_refs = transport->vtable->get_refs_list(transport, 1,
1134 &ref_prefixes);
1136 argv_array_clear(&ref_prefixes);
1138 if (flags & TRANSPORT_PUSH_ALL)
1139 match_flags |= MATCH_REFS_ALL;
1140 if (flags & TRANSPORT_PUSH_MIRROR)
1141 match_flags |= MATCH_REFS_MIRROR;
1142 if (flags & TRANSPORT_PUSH_PRUNE)
1143 match_flags |= MATCH_REFS_PRUNE;
1144 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1145 match_flags |= MATCH_REFS_FOLLOW_TAGS;
1147 if (match_push_refs(local_refs, &remote_refs, rs, match_flags))
1148 return -1;
1150 if (transport->smart_options &&
1151 transport->smart_options->cas &&
1152 !is_empty_cas(transport->smart_options->cas))
1153 apply_push_cas(transport->smart_options->cas,
1154 transport->remote, remote_refs);
1156 set_ref_status_for_push(remote_refs,
1157 flags & TRANSPORT_PUSH_MIRROR,
1158 flags & TRANSPORT_PUSH_FORCE);
1160 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1161 if (run_pre_push_hook(transport, remote_refs))
1162 return -1;
1164 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1165 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1166 !is_bare_repository()) {
1167 struct ref *ref = remote_refs;
1168 struct oid_array commits = OID_ARRAY_INIT;
1170 for (; ref; ref = ref->next)
1171 if (!is_null_oid(&ref->new_oid))
1172 oid_array_append(&commits,
1173 &ref->new_oid);
1175 if (!push_unpushed_submodules(&the_index,
1176 &commits,
1177 transport->remote,
1179 transport->push_options,
1180 pretend)) {
1181 oid_array_clear(&commits);
1182 die(_("failed to push all needed submodules"));
1184 oid_array_clear(&commits);
1187 if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1188 ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1189 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1190 !pretend)) && !is_bare_repository()) {
1191 struct ref *ref = remote_refs;
1192 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1193 struct oid_array commits = OID_ARRAY_INIT;
1195 for (; ref; ref = ref->next)
1196 if (!is_null_oid(&ref->new_oid))
1197 oid_array_append(&commits,
1198 &ref->new_oid);
1200 if (find_unpushed_submodules(&the_index,
1201 &commits,
1202 transport->remote->name,
1203 &needs_pushing)) {
1204 oid_array_clear(&commits);
1205 die_with_unpushed_submodules(&needs_pushing);
1207 string_list_clear(&needs_pushing, 0);
1208 oid_array_clear(&commits);
1211 if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY))
1212 push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1213 else
1214 push_ret = 0;
1215 err = push_had_errors(remote_refs);
1216 ret = push_ret | err;
1218 if (!quiet || err)
1219 transport_print_push_status(transport->url, remote_refs,
1220 verbose | porcelain, porcelain,
1221 reject_reasons);
1223 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1224 set_upstreams(transport, remote_refs, pretend);
1226 if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1227 TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1228 struct ref *ref;
1229 for (ref = remote_refs; ref; ref = ref->next)
1230 transport_update_tracking_ref(transport->remote, ref, verbose);
1233 if (porcelain && !push_ret)
1234 puts("Done");
1235 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1236 fprintf(stderr, "Everything up-to-date\n");
1238 return ret;
1240 return 1;
1243 const struct ref *transport_get_remote_refs(struct transport *transport,
1244 const struct argv_array *ref_prefixes)
1246 if (!transport->got_remote_refs) {
1247 transport->remote_refs =
1248 transport->vtable->get_refs_list(transport, 0,
1249 ref_prefixes);
1250 transport->got_remote_refs = 1;
1253 return transport->remote_refs;
1256 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1258 int rc;
1259 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1260 struct ref **heads = NULL;
1261 struct ref *rm;
1263 if (!transport->vtable->fetch_without_list)
1265 * Some transports (e.g. the built-in bundle transport and the
1266 * transport helper interface) do not work when fetching is
1267 * done immediately after transport creation. List the remote
1268 * refs anyway (if not already listed) as a workaround.
1270 transport_get_remote_refs(transport, NULL);
1272 for (rm = refs; rm; rm = rm->next) {
1273 nr_refs++;
1274 if (rm->peer_ref &&
1275 !is_null_oid(&rm->old_oid) &&
1276 oideq(&rm->peer_ref->old_oid, &rm->old_oid))
1277 continue;
1278 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1279 heads[nr_heads++] = rm;
1282 if (!nr_heads) {
1284 * When deepening of a shallow repository is requested,
1285 * then local and remote refs are likely to still be equal.
1286 * Just feed them all to the fetch method in that case.
1287 * This condition shouldn't be met in a non-deepening fetch
1288 * (see builtin/fetch.c:quickfetch()).
1290 ALLOC_ARRAY(heads, nr_refs);
1291 for (rm = refs; rm; rm = rm->next)
1292 heads[nr_heads++] = rm;
1295 rc = transport->vtable->fetch(transport, nr_heads, heads);
1297 free(heads);
1298 return rc;
1301 void transport_unlock_pack(struct transport *transport)
1303 if (transport->pack_lockfile) {
1304 unlink_or_warn(transport->pack_lockfile);
1305 FREE_AND_NULL(transport->pack_lockfile);
1309 int transport_connect(struct transport *transport, const char *name,
1310 const char *exec, int fd[2])
1312 if (transport->vtable->connect)
1313 return transport->vtable->connect(transport, name, exec, fd);
1314 else
1315 die(_("operation not supported by protocol"));
1318 int transport_disconnect(struct transport *transport)
1320 int ret = 0;
1321 if (transport->vtable->disconnect)
1322 ret = transport->vtable->disconnect(transport);
1323 free(transport);
1324 return ret;
1328 * Strip username (and password) from a URL and return
1329 * it in a newly allocated string.
1331 char *transport_anonymize_url(const char *url)
1333 char *scheme_prefix, *anon_part;
1334 size_t anon_len, prefix_len = 0;
1336 anon_part = strchr(url, '@');
1337 if (url_is_local_not_ssh(url) || !anon_part)
1338 goto literal_copy;
1340 anon_len = strlen(++anon_part);
1341 scheme_prefix = strstr(url, "://");
1342 if (!scheme_prefix) {
1343 if (!strchr(anon_part, ':'))
1344 /* cannot be "me@there:/path/name" */
1345 goto literal_copy;
1346 } else {
1347 const char *cp;
1348 /* make sure scheme is reasonable */
1349 for (cp = url; cp < scheme_prefix; cp++) {
1350 switch (*cp) {
1351 /* RFC 1738 2.1 */
1352 case '+': case '.': case '-':
1353 break; /* ok */
1354 default:
1355 if (isalnum(*cp))
1356 break;
1357 /* it isn't */
1358 goto literal_copy;
1361 /* @ past the first slash does not count */
1362 cp = strchr(scheme_prefix + 3, '/');
1363 if (cp && cp < anon_part)
1364 goto literal_copy;
1365 prefix_len = scheme_prefix - url + 3;
1367 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1368 (int)anon_len, anon_part);
1369 literal_copy:
1370 return xstrdup(url);
1373 static void fill_alternate_refs_command(struct child_process *cmd,
1374 const char *repo_path)
1376 const char *value;
1378 if (!git_config_get_value("core.alternateRefsCommand", &value)) {
1379 cmd->use_shell = 1;
1381 argv_array_push(&cmd->args, value);
1382 argv_array_push(&cmd->args, repo_path);
1383 } else {
1384 cmd->git_cmd = 1;
1386 argv_array_pushf(&cmd->args, "--git-dir=%s", repo_path);
1387 argv_array_push(&cmd->args, "for-each-ref");
1388 argv_array_push(&cmd->args, "--format=%(objectname)");
1390 if (!git_config_get_value("core.alternateRefsPrefixes", &value)) {
1391 argv_array_push(&cmd->args, "--");
1392 argv_array_split(&cmd->args, value);
1396 cmd->env = local_repo_env;
1397 cmd->out = -1;
1400 static void read_alternate_refs(const char *path,
1401 alternate_ref_fn *cb,
1402 void *data)
1404 struct child_process cmd = CHILD_PROCESS_INIT;
1405 struct strbuf line = STRBUF_INIT;
1406 FILE *fh;
1408 fill_alternate_refs_command(&cmd, path);
1410 if (start_command(&cmd))
1411 return;
1413 fh = xfdopen(cmd.out, "r");
1414 while (strbuf_getline_lf(&line, fh) != EOF) {
1415 struct object_id oid;
1417 if (get_oid_hex(line.buf, &oid) ||
1418 line.buf[GIT_SHA1_HEXSZ]) {
1419 warning(_("invalid line while parsing alternate refs: %s"),
1420 line.buf);
1421 break;
1424 cb(&oid, data);
1427 fclose(fh);
1428 finish_command(&cmd);
1431 struct alternate_refs_data {
1432 alternate_ref_fn *fn;
1433 void *data;
1436 static int refs_from_alternate_cb(struct alternate_object_database *e,
1437 void *data)
1439 struct strbuf path = STRBUF_INIT;
1440 size_t base_len;
1441 struct alternate_refs_data *cb = data;
1443 if (!strbuf_realpath(&path, e->path, 0))
1444 goto out;
1445 if (!strbuf_strip_suffix(&path, "/objects"))
1446 goto out;
1447 base_len = path.len;
1449 /* Is this a git repository with refs? */
1450 strbuf_addstr(&path, "/refs");
1451 if (!is_directory(path.buf))
1452 goto out;
1453 strbuf_setlen(&path, base_len);
1455 read_alternate_refs(path.buf, cb->fn, cb->data);
1457 out:
1458 strbuf_release(&path);
1459 return 0;
1462 void for_each_alternate_ref(alternate_ref_fn fn, void *data)
1464 struct alternate_refs_data cb;
1465 cb.fn = fn;
1466 cb.data = data;
1467 foreach_alt_odb(refs_from_alternate_cb, &cb);