config: document the settings to colorize push errors/hints
[git.git] / transport.c
blobf72081a5a582b099aecbefd419072b63614c8b18
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 "branch.h"
15 #include "url.h"
16 #include "submodule.h"
17 #include "string-list.h"
18 #include "sha1-array.h"
19 #include "sigchain.h"
20 #include "transport-internal.h"
21 #include "object-store.h"
22 #include "color.h"
24 static int transport_use_color = -1;
25 static char transport_colors[][COLOR_MAXLEN] = {
26 GIT_COLOR_RESET,
27 GIT_COLOR_RED /* REJECTED */
30 enum color_transport {
31 TRANSPORT_COLOR_RESET = 0,
32 TRANSPORT_COLOR_REJECTED = 1
35 static int transport_color_config(void)
37 const char *keys[] = {
38 "color.transport.reset",
39 "color.transport.rejected"
40 }, *key = "color.transport";
41 char *value;
42 int i;
43 static int initialized;
45 if (initialized)
46 return 0;
47 initialized = 1;
49 if (!git_config_get_string(key, &value))
50 transport_use_color = git_config_colorbool(key, value);
52 if (!want_color_stderr(transport_use_color))
53 return 0;
55 for (i = 0; i < ARRAY_SIZE(keys); i++)
56 if (!git_config_get_string(keys[i], &value)) {
57 if (!value)
58 return config_error_nonbool(keys[i]);
59 if (color_parse(value, transport_colors[i]) < 0)
60 return -1;
63 return 0;
66 static const char *transport_get_color(enum color_transport ix)
68 if (want_color_stderr(transport_use_color))
69 return transport_colors[ix];
70 return "";
73 static void set_upstreams(struct transport *transport, struct ref *refs,
74 int pretend)
76 struct ref *ref;
77 for (ref = refs; ref; ref = ref->next) {
78 const char *localname;
79 const char *tmp;
80 const char *remotename;
81 int flag = 0;
83 * Check suitability for tracking. Must be successful /
84 * already up-to-date ref create/modify (not delete).
86 if (ref->status != REF_STATUS_OK &&
87 ref->status != REF_STATUS_UPTODATE)
88 continue;
89 if (!ref->peer_ref)
90 continue;
91 if (is_null_oid(&ref->new_oid))
92 continue;
94 /* Follow symbolic refs (mainly for HEAD). */
95 localname = ref->peer_ref->name;
96 remotename = ref->name;
97 tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING,
98 NULL, &flag);
99 if (tmp && flag & REF_ISSYMREF &&
100 starts_with(tmp, "refs/heads/"))
101 localname = tmp;
103 /* Both source and destination must be local branches. */
104 if (!localname || !starts_with(localname, "refs/heads/"))
105 continue;
106 if (!remotename || !starts_with(remotename, "refs/heads/"))
107 continue;
109 if (!pretend)
110 install_branch_config(BRANCH_CONFIG_VERBOSE,
111 localname + 11, transport->remote->name,
112 remotename);
113 else
114 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
115 localname + 11, remotename + 11,
116 transport->remote->name);
120 struct bundle_transport_data {
121 int fd;
122 struct bundle_header header;
125 static struct ref *get_refs_from_bundle(struct transport *transport, int for_push)
127 struct bundle_transport_data *data = transport->data;
128 struct ref *result = NULL;
129 int i;
131 if (for_push)
132 return NULL;
134 if (data->fd > 0)
135 close(data->fd);
136 data->fd = read_bundle_header(transport->url, &data->header);
137 if (data->fd < 0)
138 die ("Could not read bundle '%s'.", transport->url);
139 for (i = 0; i < data->header.references.nr; i++) {
140 struct ref_list_entry *e = data->header.references.list + i;
141 struct ref *ref = alloc_ref(e->name);
142 oidcpy(&ref->old_oid, &e->oid);
143 ref->next = result;
144 result = ref;
146 return result;
149 static int fetch_refs_from_bundle(struct transport *transport,
150 int nr_heads, struct ref **to_fetch)
152 struct bundle_transport_data *data = transport->data;
153 return unbundle(&data->header, data->fd,
154 transport->progress ? BUNDLE_VERBOSE : 0);
157 static int close_bundle(struct transport *transport)
159 struct bundle_transport_data *data = transport->data;
160 if (data->fd > 0)
161 close(data->fd);
162 free(data);
163 return 0;
166 struct git_transport_data {
167 struct git_transport_options options;
168 struct child_process *conn;
169 int fd[2];
170 unsigned got_remote_heads : 1;
171 struct oid_array extra_have;
172 struct oid_array shallow;
175 static int set_git_option(struct git_transport_options *opts,
176 const char *name, const char *value)
178 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
179 opts->uploadpack = value;
180 return 0;
181 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
182 opts->receivepack = value;
183 return 0;
184 } else if (!strcmp(name, TRANS_OPT_THIN)) {
185 opts->thin = !!value;
186 return 0;
187 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
188 opts->followtags = !!value;
189 return 0;
190 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
191 opts->keep = !!value;
192 return 0;
193 } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
194 opts->update_shallow = !!value;
195 return 0;
196 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
197 if (!value)
198 opts->depth = 0;
199 else {
200 char *end;
201 opts->depth = strtol(value, &end, 0);
202 if (*end)
203 die(_("transport: invalid depth option '%s'"), value);
205 return 0;
206 } else if (!strcmp(name, TRANS_OPT_DEEPEN_SINCE)) {
207 opts->deepen_since = value;
208 return 0;
209 } else if (!strcmp(name, TRANS_OPT_DEEPEN_NOT)) {
210 opts->deepen_not = (const struct string_list *)value;
211 return 0;
212 } else if (!strcmp(name, TRANS_OPT_DEEPEN_RELATIVE)) {
213 opts->deepen_relative = !!value;
214 return 0;
215 } else if (!strcmp(name, TRANS_OPT_FROM_PROMISOR)) {
216 opts->from_promisor = !!value;
217 return 0;
218 } else if (!strcmp(name, TRANS_OPT_NO_DEPENDENTS)) {
219 opts->no_dependents = !!value;
220 return 0;
221 } else if (!strcmp(name, TRANS_OPT_LIST_OBJECTS_FILTER)) {
222 parse_list_objects_filter(&opts->filter_options, value);
223 return 0;
225 return 1;
228 static int connect_setup(struct transport *transport, int for_push)
230 struct git_transport_data *data = transport->data;
231 int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
233 if (data->conn)
234 return 0;
236 switch (transport->family) {
237 case TRANSPORT_FAMILY_ALL: break;
238 case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
239 case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
242 data->conn = git_connect(data->fd, transport->url,
243 for_push ? data->options.receivepack :
244 data->options.uploadpack,
245 flags);
247 return 0;
250 static struct ref *get_refs_via_connect(struct transport *transport, int for_push)
252 struct git_transport_data *data = transport->data;
253 struct ref *refs;
255 connect_setup(transport, for_push);
256 get_remote_heads(data->fd[0], NULL, 0, &refs,
257 for_push ? REF_NORMAL : 0,
258 &data->extra_have,
259 &data->shallow);
260 data->got_remote_heads = 1;
262 return refs;
265 static int fetch_refs_via_pack(struct transport *transport,
266 int nr_heads, struct ref **to_fetch)
268 int ret = 0;
269 struct git_transport_data *data = transport->data;
270 struct ref *refs;
271 char *dest = xstrdup(transport->url);
272 struct fetch_pack_args args;
273 struct ref *refs_tmp = NULL;
275 memset(&args, 0, sizeof(args));
276 args.uploadpack = data->options.uploadpack;
277 args.keep_pack = data->options.keep;
278 args.lock_pack = 1;
279 args.use_thin_pack = data->options.thin;
280 args.include_tag = data->options.followtags;
281 args.verbose = (transport->verbose > 1);
282 args.quiet = (transport->verbose < 0);
283 args.no_progress = !transport->progress;
284 args.depth = data->options.depth;
285 args.deepen_since = data->options.deepen_since;
286 args.deepen_not = data->options.deepen_not;
287 args.deepen_relative = data->options.deepen_relative;
288 args.check_self_contained_and_connected =
289 data->options.check_self_contained_and_connected;
290 args.cloning = transport->cloning;
291 args.update_shallow = data->options.update_shallow;
292 args.from_promisor = data->options.from_promisor;
293 args.no_dependents = data->options.no_dependents;
294 args.filter_options = data->options.filter_options;
296 if (!data->got_remote_heads) {
297 connect_setup(transport, 0);
298 get_remote_heads(data->fd[0], NULL, 0, &refs_tmp, 0,
299 NULL, &data->shallow);
300 data->got_remote_heads = 1;
303 refs = fetch_pack(&args, data->fd, data->conn,
304 refs_tmp ? refs_tmp : transport->remote_refs,
305 dest, to_fetch, nr_heads, &data->shallow,
306 &transport->pack_lockfile);
307 close(data->fd[0]);
308 close(data->fd[1]);
309 if (finish_connect(data->conn))
310 ret = -1;
311 data->conn = NULL;
312 data->got_remote_heads = 0;
313 data->options.self_contained_and_connected =
314 args.self_contained_and_connected;
316 if (refs == NULL)
317 ret = -1;
318 if (report_unmatched_refs(to_fetch, nr_heads))
319 ret = -1;
321 free_refs(refs_tmp);
322 free_refs(refs);
323 free(dest);
324 return ret;
327 static int push_had_errors(struct ref *ref)
329 for (; ref; ref = ref->next) {
330 switch (ref->status) {
331 case REF_STATUS_NONE:
332 case REF_STATUS_UPTODATE:
333 case REF_STATUS_OK:
334 break;
335 default:
336 return 1;
339 return 0;
342 int transport_refs_pushed(struct ref *ref)
344 for (; ref; ref = ref->next) {
345 switch(ref->status) {
346 case REF_STATUS_NONE:
347 case REF_STATUS_UPTODATE:
348 break;
349 default:
350 return 1;
353 return 0;
356 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
358 struct refspec rs;
360 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
361 return;
363 rs.src = ref->name;
364 rs.dst = NULL;
366 if (!remote_find_tracking(remote, &rs)) {
367 if (verbose)
368 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
369 if (ref->deletion) {
370 delete_ref(NULL, rs.dst, NULL, 0);
371 } else
372 update_ref("update by push", rs.dst, &ref->new_oid,
373 NULL, 0, 0);
374 free(rs.dst);
378 static void print_ref_status(char flag, const char *summary,
379 struct ref *to, struct ref *from, const char *msg,
380 int porcelain, int summary_width)
382 if (porcelain) {
383 if (from)
384 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
385 else
386 fprintf(stdout, "%c\t:%s\t", flag, to->name);
387 if (msg)
388 fprintf(stdout, "%s (%s)\n", summary, msg);
389 else
390 fprintf(stdout, "%s\n", summary);
391 } else {
392 const char *red = "", *reset = "";
393 if (push_had_errors(to)) {
394 red = transport_get_color(TRANSPORT_COLOR_REJECTED);
395 reset = transport_get_color(TRANSPORT_COLOR_RESET);
397 fprintf(stderr, " %s%c %-*s%s ", red, flag, summary_width,
398 summary, reset);
399 if (from)
400 fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
401 else
402 fputs(prettify_refname(to->name), stderr);
403 if (msg) {
404 fputs(" (", stderr);
405 fputs(msg, stderr);
406 fputc(')', stderr);
408 fputc('\n', stderr);
412 static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_width)
414 if (ref->deletion)
415 print_ref_status('-', "[deleted]", ref, NULL, NULL,
416 porcelain, summary_width);
417 else if (is_null_oid(&ref->old_oid))
418 print_ref_status('*',
419 (starts_with(ref->name, "refs/tags/") ? "[new tag]" :
420 "[new branch]"),
421 ref, ref->peer_ref, NULL, porcelain, summary_width);
422 else {
423 struct strbuf quickref = STRBUF_INIT;
424 char type;
425 const char *msg;
427 strbuf_add_unique_abbrev(&quickref, &ref->old_oid,
428 DEFAULT_ABBREV);
429 if (ref->forced_update) {
430 strbuf_addstr(&quickref, "...");
431 type = '+';
432 msg = "forced update";
433 } else {
434 strbuf_addstr(&quickref, "..");
435 type = ' ';
436 msg = NULL;
438 strbuf_add_unique_abbrev(&quickref, &ref->new_oid,
439 DEFAULT_ABBREV);
441 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
442 porcelain, summary_width);
443 strbuf_release(&quickref);
447 static int print_one_push_status(struct ref *ref, const char *dest, int count,
448 int porcelain, int summary_width)
450 if (!count) {
451 char *url = transport_anonymize_url(dest);
452 fprintf(porcelain ? stdout : stderr, "To %s\n", url);
453 free(url);
456 switch(ref->status) {
457 case REF_STATUS_NONE:
458 print_ref_status('X', "[no match]", ref, NULL, NULL,
459 porcelain, summary_width);
460 break;
461 case REF_STATUS_REJECT_NODELETE:
462 print_ref_status('!', "[rejected]", ref, NULL,
463 "remote does not support deleting refs",
464 porcelain, summary_width);
465 break;
466 case REF_STATUS_UPTODATE:
467 print_ref_status('=', "[up to date]", ref,
468 ref->peer_ref, NULL, porcelain, summary_width);
469 break;
470 case REF_STATUS_REJECT_NONFASTFORWARD:
471 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
472 "non-fast-forward", porcelain, summary_width);
473 break;
474 case REF_STATUS_REJECT_ALREADY_EXISTS:
475 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
476 "already exists", porcelain, summary_width);
477 break;
478 case REF_STATUS_REJECT_FETCH_FIRST:
479 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
480 "fetch first", porcelain, summary_width);
481 break;
482 case REF_STATUS_REJECT_NEEDS_FORCE:
483 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
484 "needs force", porcelain, summary_width);
485 break;
486 case REF_STATUS_REJECT_STALE:
487 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
488 "stale info", porcelain, summary_width);
489 break;
490 case REF_STATUS_REJECT_SHALLOW:
491 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
492 "new shallow roots not allowed",
493 porcelain, summary_width);
494 break;
495 case REF_STATUS_REMOTE_REJECT:
496 print_ref_status('!', "[remote rejected]", ref,
497 ref->deletion ? NULL : ref->peer_ref,
498 ref->remote_status, porcelain, summary_width);
499 break;
500 case REF_STATUS_EXPECTING_REPORT:
501 print_ref_status('!', "[remote failure]", ref,
502 ref->deletion ? NULL : ref->peer_ref,
503 "remote failed to report status",
504 porcelain, summary_width);
505 break;
506 case REF_STATUS_ATOMIC_PUSH_FAILED:
507 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
508 "atomic push failed", porcelain, summary_width);
509 break;
510 case REF_STATUS_OK:
511 print_ok_ref_status(ref, porcelain, summary_width);
512 break;
515 return 1;
518 static int measure_abbrev(const struct object_id *oid, int sofar)
520 char hex[GIT_MAX_HEXSZ + 1];
521 int w = find_unique_abbrev_r(hex, oid, DEFAULT_ABBREV);
523 return (w < sofar) ? sofar : w;
526 int transport_summary_width(const struct ref *refs)
528 int maxw = -1;
530 for (; refs; refs = refs->next) {
531 maxw = measure_abbrev(&refs->old_oid, maxw);
532 maxw = measure_abbrev(&refs->new_oid, maxw);
534 if (maxw < 0)
535 maxw = FALLBACK_DEFAULT_ABBREV;
536 return (2 * maxw + 3);
539 void transport_print_push_status(const char *dest, struct ref *refs,
540 int verbose, int porcelain, unsigned int *reject_reasons)
542 struct ref *ref;
543 int n = 0;
544 char *head;
545 int summary_width = transport_summary_width(refs);
547 if (transport_color_config() < 0)
548 warning(_("could not parse transport.color.* config"));
550 head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
552 if (verbose) {
553 for (ref = refs; ref; ref = ref->next)
554 if (ref->status == REF_STATUS_UPTODATE)
555 n += print_one_push_status(ref, dest, n,
556 porcelain, summary_width);
559 for (ref = refs; ref; ref = ref->next)
560 if (ref->status == REF_STATUS_OK)
561 n += print_one_push_status(ref, dest, n,
562 porcelain, summary_width);
564 *reject_reasons = 0;
565 for (ref = refs; ref; ref = ref->next) {
566 if (ref->status != REF_STATUS_NONE &&
567 ref->status != REF_STATUS_UPTODATE &&
568 ref->status != REF_STATUS_OK)
569 n += print_one_push_status(ref, dest, n,
570 porcelain, summary_width);
571 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
572 if (head != NULL && !strcmp(head, ref->name))
573 *reject_reasons |= REJECT_NON_FF_HEAD;
574 else
575 *reject_reasons |= REJECT_NON_FF_OTHER;
576 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
577 *reject_reasons |= REJECT_ALREADY_EXISTS;
578 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
579 *reject_reasons |= REJECT_FETCH_FIRST;
580 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
581 *reject_reasons |= REJECT_NEEDS_FORCE;
584 free(head);
587 void transport_verify_remote_names(int nr_heads, const char **heads)
589 int i;
591 for (i = 0; i < nr_heads; i++) {
592 const char *local = heads[i];
593 const char *remote = strrchr(heads[i], ':');
595 if (*local == '+')
596 local++;
598 /* A matching refspec is okay. */
599 if (remote == local && remote[1] == '\0')
600 continue;
602 remote = remote ? (remote + 1) : local;
603 if (check_refname_format(remote,
604 REFNAME_ALLOW_ONELEVEL|REFNAME_REFSPEC_PATTERN))
605 die("remote part of refspec is not a valid name in %s",
606 heads[i]);
610 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
612 struct git_transport_data *data = transport->data;
613 struct send_pack_args args;
614 int ret;
616 if (transport_color_config() < 0)
617 return -1;
619 if (!data->got_remote_heads) {
620 struct ref *tmp_refs;
621 connect_setup(transport, 1);
623 get_remote_heads(data->fd[0], NULL, 0, &tmp_refs, REF_NORMAL,
624 NULL, &data->shallow);
625 data->got_remote_heads = 1;
628 memset(&args, 0, sizeof(args));
629 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
630 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
631 args.use_thin_pack = data->options.thin;
632 args.verbose = (transport->verbose > 0);
633 args.quiet = (transport->verbose < 0);
634 args.progress = transport->progress;
635 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
636 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
637 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
638 args.push_options = transport->push_options;
639 args.url = transport->url;
641 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
642 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
643 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
644 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
645 else
646 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
648 ret = send_pack(&args, data->fd, data->conn, remote_refs,
649 &data->extra_have);
651 close(data->fd[1]);
652 close(data->fd[0]);
653 ret |= finish_connect(data->conn);
654 data->conn = NULL;
655 data->got_remote_heads = 0;
657 return ret;
660 static int connect_git(struct transport *transport, const char *name,
661 const char *executable, int fd[2])
663 struct git_transport_data *data = transport->data;
664 data->conn = git_connect(data->fd, transport->url,
665 executable, 0);
666 fd[0] = data->fd[0];
667 fd[1] = data->fd[1];
668 return 0;
671 static int disconnect_git(struct transport *transport)
673 struct git_transport_data *data = transport->data;
674 if (data->conn) {
675 if (data->got_remote_heads)
676 packet_flush(data->fd[1]);
677 close(data->fd[0]);
678 close(data->fd[1]);
679 finish_connect(data->conn);
682 free(data);
683 return 0;
686 static struct transport_vtable taken_over_vtable = {
687 NULL,
688 get_refs_via_connect,
689 fetch_refs_via_pack,
690 git_transport_push,
691 NULL,
692 disconnect_git
695 void transport_take_over(struct transport *transport,
696 struct child_process *child)
698 struct git_transport_data *data;
700 if (!transport->smart_options)
701 die("BUG: taking over transport requires non-NULL "
702 "smart_options field.");
704 data = xcalloc(1, sizeof(*data));
705 data->options = *transport->smart_options;
706 data->conn = child;
707 data->fd[0] = data->conn->out;
708 data->fd[1] = data->conn->in;
709 data->got_remote_heads = 0;
710 transport->data = data;
712 transport->vtable = &taken_over_vtable;
713 transport->smart_options = &(data->options);
715 transport->cannot_reuse = 1;
718 static int is_file(const char *url)
720 struct stat buf;
721 if (stat(url, &buf))
722 return 0;
723 return S_ISREG(buf.st_mode);
726 static int external_specification_len(const char *url)
728 return strchr(url, ':') - url;
731 static const struct string_list *protocol_whitelist(void)
733 static int enabled = -1;
734 static struct string_list allowed = STRING_LIST_INIT_DUP;
736 if (enabled < 0) {
737 const char *v = getenv("GIT_ALLOW_PROTOCOL");
738 if (v) {
739 string_list_split(&allowed, v, ':', -1);
740 string_list_sort(&allowed);
741 enabled = 1;
742 } else {
743 enabled = 0;
747 return enabled ? &allowed : NULL;
750 enum protocol_allow_config {
751 PROTOCOL_ALLOW_NEVER = 0,
752 PROTOCOL_ALLOW_USER_ONLY,
753 PROTOCOL_ALLOW_ALWAYS
756 static enum protocol_allow_config parse_protocol_config(const char *key,
757 const char *value)
759 if (!strcasecmp(value, "always"))
760 return PROTOCOL_ALLOW_ALWAYS;
761 else if (!strcasecmp(value, "never"))
762 return PROTOCOL_ALLOW_NEVER;
763 else if (!strcasecmp(value, "user"))
764 return PROTOCOL_ALLOW_USER_ONLY;
766 die("unknown value for config '%s': %s", key, value);
769 static enum protocol_allow_config get_protocol_config(const char *type)
771 char *key = xstrfmt("protocol.%s.allow", type);
772 char *value;
774 /* first check the per-protocol config */
775 if (!git_config_get_string(key, &value)) {
776 enum protocol_allow_config ret =
777 parse_protocol_config(key, value);
778 free(key);
779 free(value);
780 return ret;
782 free(key);
784 /* if defined, fallback to user-defined default for unknown protocols */
785 if (!git_config_get_string("protocol.allow", &value)) {
786 enum protocol_allow_config ret =
787 parse_protocol_config("protocol.allow", value);
788 free(value);
789 return ret;
792 /* fallback to built-in defaults */
793 /* known safe */
794 if (!strcmp(type, "http") ||
795 !strcmp(type, "https") ||
796 !strcmp(type, "git") ||
797 !strcmp(type, "ssh") ||
798 !strcmp(type, "file"))
799 return PROTOCOL_ALLOW_ALWAYS;
801 /* known scary; err on the side of caution */
802 if (!strcmp(type, "ext"))
803 return PROTOCOL_ALLOW_NEVER;
805 /* unknown; by default let them be used only directly by the user */
806 return PROTOCOL_ALLOW_USER_ONLY;
809 int is_transport_allowed(const char *type, int from_user)
811 const struct string_list *whitelist = protocol_whitelist();
812 if (whitelist)
813 return string_list_has_string(whitelist, type);
815 switch (get_protocol_config(type)) {
816 case PROTOCOL_ALLOW_ALWAYS:
817 return 1;
818 case PROTOCOL_ALLOW_NEVER:
819 return 0;
820 case PROTOCOL_ALLOW_USER_ONLY:
821 if (from_user < 0)
822 from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
823 return from_user;
826 die("BUG: invalid protocol_allow_config type");
829 void transport_check_allowed(const char *type)
831 if (!is_transport_allowed(type, -1))
832 die("transport '%s' not allowed", type);
835 static struct transport_vtable bundle_vtable = {
836 NULL,
837 get_refs_from_bundle,
838 fetch_refs_from_bundle,
839 NULL,
840 NULL,
841 close_bundle
844 static struct transport_vtable builtin_smart_vtable = {
845 NULL,
846 get_refs_via_connect,
847 fetch_refs_via_pack,
848 git_transport_push,
849 connect_git,
850 disconnect_git
853 struct transport *transport_get(struct remote *remote, const char *url)
855 const char *helper;
856 struct transport *ret = xcalloc(1, sizeof(*ret));
858 ret->progress = isatty(2);
860 if (!remote)
861 die("No remote provided to transport_get()");
863 ret->got_remote_refs = 0;
864 ret->remote = remote;
865 helper = remote->foreign_vcs;
867 if (!url && remote->url)
868 url = remote->url[0];
869 ret->url = url;
871 /* maybe it is a foreign URL? */
872 if (url) {
873 const char *p = url;
875 while (is_urlschemechar(p == url, *p))
876 p++;
877 if (starts_with(p, "::"))
878 helper = xstrndup(url, p - url);
881 if (helper) {
882 transport_helper_init(ret, helper);
883 } else if (starts_with(url, "rsync:")) {
884 die("git-over-rsync is no longer supported");
885 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
886 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
887 transport_check_allowed("file");
888 ret->data = data;
889 ret->vtable = &bundle_vtable;
890 ret->smart_options = NULL;
891 } else if (!is_url(url)
892 || starts_with(url, "file://")
893 || starts_with(url, "git://")
894 || starts_with(url, "ssh://")
895 || starts_with(url, "git+ssh://") /* deprecated - do not use */
896 || starts_with(url, "ssh+git://") /* deprecated - do not use */
899 * These are builtin smart transports; "allowed" transports
900 * will be checked individually in git_connect.
902 struct git_transport_data *data = xcalloc(1, sizeof(*data));
903 ret->data = data;
904 ret->vtable = &builtin_smart_vtable;
905 ret->smart_options = &(data->options);
907 data->conn = NULL;
908 data->got_remote_heads = 0;
909 } else {
910 /* Unknown protocol in URL. Pass to external handler. */
911 int len = external_specification_len(url);
912 char *handler = xmemdupz(url, len);
913 transport_helper_init(ret, handler);
916 if (ret->smart_options) {
917 ret->smart_options->thin = 1;
918 ret->smart_options->uploadpack = "git-upload-pack";
919 if (remote->uploadpack)
920 ret->smart_options->uploadpack = remote->uploadpack;
921 ret->smart_options->receivepack = "git-receive-pack";
922 if (remote->receivepack)
923 ret->smart_options->receivepack = remote->receivepack;
926 return ret;
929 int transport_set_option(struct transport *transport,
930 const char *name, const char *value)
932 int git_reports = 1, protocol_reports = 1;
934 if (transport->smart_options)
935 git_reports = set_git_option(transport->smart_options,
936 name, value);
938 if (transport->vtable->set_option)
939 protocol_reports = transport->vtable->set_option(transport,
940 name, value);
942 /* If either report is 0, report 0 (success). */
943 if (!git_reports || !protocol_reports)
944 return 0;
945 /* If either reports -1 (invalid value), report -1. */
946 if ((git_reports == -1) || (protocol_reports == -1))
947 return -1;
948 /* Otherwise if both report unknown, report unknown. */
949 return 1;
952 void transport_set_verbosity(struct transport *transport, int verbosity,
953 int force_progress)
955 if (verbosity >= 1)
956 transport->verbose = verbosity <= 3 ? verbosity : 3;
957 if (verbosity < 0)
958 transport->verbose = -1;
961 * Rules used to determine whether to report progress (processing aborts
962 * when a rule is satisfied):
964 * . Report progress, if force_progress is 1 (ie. --progress).
965 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
966 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
967 * . Report progress if isatty(2) is 1.
969 if (force_progress >= 0)
970 transport->progress = !!force_progress;
971 else
972 transport->progress = verbosity >= 0 && isatty(2);
975 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
977 int i;
979 fprintf(stderr, _("The following submodule paths contain changes that can\n"
980 "not be found on any remote:\n"));
981 for (i = 0; i < needs_pushing->nr; i++)
982 fprintf(stderr, " %s\n", needs_pushing->items[i].string);
983 fprintf(stderr, _("\nPlease try\n\n"
984 " git push --recurse-submodules=on-demand\n\n"
985 "or cd to the path and use\n\n"
986 " git push\n\n"
987 "to push them to a remote.\n\n"));
989 string_list_clear(needs_pushing, 0);
991 die(_("Aborting."));
994 static int run_pre_push_hook(struct transport *transport,
995 struct ref *remote_refs)
997 int ret = 0, x;
998 struct ref *r;
999 struct child_process proc = CHILD_PROCESS_INIT;
1000 struct strbuf buf;
1001 const char *argv[4];
1003 if (!(argv[0] = find_hook("pre-push")))
1004 return 0;
1006 argv[1] = transport->remote->name;
1007 argv[2] = transport->url;
1008 argv[3] = NULL;
1010 proc.argv = argv;
1011 proc.in = -1;
1013 if (start_command(&proc)) {
1014 finish_command(&proc);
1015 return -1;
1018 sigchain_push(SIGPIPE, SIG_IGN);
1020 strbuf_init(&buf, 256);
1022 for (r = remote_refs; r; r = r->next) {
1023 if (!r->peer_ref) continue;
1024 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
1025 if (r->status == REF_STATUS_REJECT_STALE) continue;
1026 if (r->status == REF_STATUS_UPTODATE) continue;
1028 strbuf_reset(&buf);
1029 strbuf_addf( &buf, "%s %s %s %s\n",
1030 r->peer_ref->name, oid_to_hex(&r->new_oid),
1031 r->name, oid_to_hex(&r->old_oid));
1033 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
1034 /* We do not mind if a hook does not read all refs. */
1035 if (errno != EPIPE)
1036 ret = -1;
1037 break;
1041 strbuf_release(&buf);
1043 x = close(proc.in);
1044 if (!ret)
1045 ret = x;
1047 sigchain_pop(SIGPIPE);
1049 x = finish_command(&proc);
1050 if (!ret)
1051 ret = x;
1053 return ret;
1056 int transport_push(struct transport *transport,
1057 int refspec_nr, const char **refspec, int flags,
1058 unsigned int *reject_reasons)
1060 *reject_reasons = 0;
1061 transport_verify_remote_names(refspec_nr, refspec);
1063 if (transport_color_config() < 0)
1064 return -1;
1066 if (transport->vtable->push_refs) {
1067 struct ref *remote_refs;
1068 struct ref *local_refs = get_local_heads();
1069 int match_flags = MATCH_REFS_NONE;
1070 int verbose = (transport->verbose > 0);
1071 int quiet = (transport->verbose < 0);
1072 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1073 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1074 int push_ret, ret, err;
1076 if (check_push_refs(local_refs, refspec_nr, refspec) < 0)
1077 return -1;
1079 remote_refs = transport->vtable->get_refs_list(transport, 1);
1081 if (flags & TRANSPORT_PUSH_ALL)
1082 match_flags |= MATCH_REFS_ALL;
1083 if (flags & TRANSPORT_PUSH_MIRROR)
1084 match_flags |= MATCH_REFS_MIRROR;
1085 if (flags & TRANSPORT_PUSH_PRUNE)
1086 match_flags |= MATCH_REFS_PRUNE;
1087 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1088 match_flags |= MATCH_REFS_FOLLOW_TAGS;
1090 if (match_push_refs(local_refs, &remote_refs,
1091 refspec_nr, refspec, match_flags)) {
1092 return -1;
1095 if (transport->smart_options &&
1096 transport->smart_options->cas &&
1097 !is_empty_cas(transport->smart_options->cas))
1098 apply_push_cas(transport->smart_options->cas,
1099 transport->remote, remote_refs);
1101 set_ref_status_for_push(remote_refs,
1102 flags & TRANSPORT_PUSH_MIRROR,
1103 flags & TRANSPORT_PUSH_FORCE);
1105 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1106 if (run_pre_push_hook(transport, remote_refs))
1107 return -1;
1109 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1110 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1111 !is_bare_repository()) {
1112 struct ref *ref = remote_refs;
1113 struct oid_array commits = OID_ARRAY_INIT;
1115 for (; ref; ref = ref->next)
1116 if (!is_null_oid(&ref->new_oid))
1117 oid_array_append(&commits,
1118 &ref->new_oid);
1120 if (!push_unpushed_submodules(&commits,
1121 transport->remote,
1122 refspec, refspec_nr,
1123 transport->push_options,
1124 pretend)) {
1125 oid_array_clear(&commits);
1126 die("Failed to push all needed submodules!");
1128 oid_array_clear(&commits);
1131 if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1132 ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1133 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1134 !pretend)) && !is_bare_repository()) {
1135 struct ref *ref = remote_refs;
1136 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1137 struct oid_array commits = OID_ARRAY_INIT;
1139 for (; ref; ref = ref->next)
1140 if (!is_null_oid(&ref->new_oid))
1141 oid_array_append(&commits,
1142 &ref->new_oid);
1144 if (find_unpushed_submodules(&commits, transport->remote->name,
1145 &needs_pushing)) {
1146 oid_array_clear(&commits);
1147 die_with_unpushed_submodules(&needs_pushing);
1149 string_list_clear(&needs_pushing, 0);
1150 oid_array_clear(&commits);
1153 if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY))
1154 push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1155 else
1156 push_ret = 0;
1157 err = push_had_errors(remote_refs);
1158 ret = push_ret | err;
1160 if (!quiet || err)
1161 transport_print_push_status(transport->url, remote_refs,
1162 verbose | porcelain, porcelain,
1163 reject_reasons);
1165 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1166 set_upstreams(transport, remote_refs, pretend);
1168 if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1169 TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1170 struct ref *ref;
1171 for (ref = remote_refs; ref; ref = ref->next)
1172 transport_update_tracking_ref(transport->remote, ref, verbose);
1175 if (porcelain && !push_ret)
1176 puts("Done");
1177 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1178 fprintf(stderr, "Everything up-to-date\n");
1180 return ret;
1182 return 1;
1185 const struct ref *transport_get_remote_refs(struct transport *transport)
1187 if (!transport->got_remote_refs) {
1188 transport->remote_refs = transport->vtable->get_refs_list(transport, 0);
1189 transport->got_remote_refs = 1;
1192 return transport->remote_refs;
1195 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1197 int rc;
1198 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1199 struct ref **heads = NULL;
1200 struct ref *rm;
1202 for (rm = refs; rm; rm = rm->next) {
1203 nr_refs++;
1204 if (rm->peer_ref &&
1205 !is_null_oid(&rm->old_oid) &&
1206 !oidcmp(&rm->peer_ref->old_oid, &rm->old_oid))
1207 continue;
1208 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1209 heads[nr_heads++] = rm;
1212 if (!nr_heads) {
1214 * When deepening of a shallow repository is requested,
1215 * then local and remote refs are likely to still be equal.
1216 * Just feed them all to the fetch method in that case.
1217 * This condition shouldn't be met in a non-deepening fetch
1218 * (see builtin/fetch.c:quickfetch()).
1220 ALLOC_ARRAY(heads, nr_refs);
1221 for (rm = refs; rm; rm = rm->next)
1222 heads[nr_heads++] = rm;
1225 rc = transport->vtable->fetch(transport, nr_heads, heads);
1227 free(heads);
1228 return rc;
1231 void transport_unlock_pack(struct transport *transport)
1233 if (transport->pack_lockfile) {
1234 unlink_or_warn(transport->pack_lockfile);
1235 FREE_AND_NULL(transport->pack_lockfile);
1239 int transport_connect(struct transport *transport, const char *name,
1240 const char *exec, int fd[2])
1242 if (transport->vtable->connect)
1243 return transport->vtable->connect(transport, name, exec, fd);
1244 else
1245 die("Operation not supported by protocol");
1248 int transport_disconnect(struct transport *transport)
1250 int ret = 0;
1251 if (transport->vtable->disconnect)
1252 ret = transport->vtable->disconnect(transport);
1253 free(transport);
1254 return ret;
1258 * Strip username (and password) from a URL and return
1259 * it in a newly allocated string.
1261 char *transport_anonymize_url(const char *url)
1263 char *scheme_prefix, *anon_part;
1264 size_t anon_len, prefix_len = 0;
1266 anon_part = strchr(url, '@');
1267 if (url_is_local_not_ssh(url) || !anon_part)
1268 goto literal_copy;
1270 anon_len = strlen(++anon_part);
1271 scheme_prefix = strstr(url, "://");
1272 if (!scheme_prefix) {
1273 if (!strchr(anon_part, ':'))
1274 /* cannot be "me@there:/path/name" */
1275 goto literal_copy;
1276 } else {
1277 const char *cp;
1278 /* make sure scheme is reasonable */
1279 for (cp = url; cp < scheme_prefix; cp++) {
1280 switch (*cp) {
1281 /* RFC 1738 2.1 */
1282 case '+': case '.': case '-':
1283 break; /* ok */
1284 default:
1285 if (isalnum(*cp))
1286 break;
1287 /* it isn't */
1288 goto literal_copy;
1291 /* @ past the first slash does not count */
1292 cp = strchr(scheme_prefix + 3, '/');
1293 if (cp && cp < anon_part)
1294 goto literal_copy;
1295 prefix_len = scheme_prefix - url + 3;
1297 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1298 (int)anon_len, anon_part);
1299 literal_copy:
1300 return xstrdup(url);
1303 static void read_alternate_refs(const char *path,
1304 alternate_ref_fn *cb,
1305 void *data)
1307 struct child_process cmd = CHILD_PROCESS_INIT;
1308 struct strbuf line = STRBUF_INIT;
1309 FILE *fh;
1311 cmd.git_cmd = 1;
1312 argv_array_pushf(&cmd.args, "--git-dir=%s", path);
1313 argv_array_push(&cmd.args, "for-each-ref");
1314 argv_array_push(&cmd.args, "--format=%(objectname) %(refname)");
1315 cmd.env = local_repo_env;
1316 cmd.out = -1;
1318 if (start_command(&cmd))
1319 return;
1321 fh = xfdopen(cmd.out, "r");
1322 while (strbuf_getline_lf(&line, fh) != EOF) {
1323 struct object_id oid;
1325 if (get_oid_hex(line.buf, &oid) ||
1326 line.buf[GIT_SHA1_HEXSZ] != ' ') {
1327 warning("invalid line while parsing alternate refs: %s",
1328 line.buf);
1329 break;
1332 cb(line.buf + GIT_SHA1_HEXSZ + 1, &oid, data);
1335 fclose(fh);
1336 finish_command(&cmd);
1339 struct alternate_refs_data {
1340 alternate_ref_fn *fn;
1341 void *data;
1344 static int refs_from_alternate_cb(struct alternate_object_database *e,
1345 void *data)
1347 struct strbuf path = STRBUF_INIT;
1348 size_t base_len;
1349 struct alternate_refs_data *cb = data;
1351 if (!strbuf_realpath(&path, e->path, 0))
1352 goto out;
1353 if (!strbuf_strip_suffix(&path, "/objects"))
1354 goto out;
1355 base_len = path.len;
1357 /* Is this a git repository with refs? */
1358 strbuf_addstr(&path, "/refs");
1359 if (!is_directory(path.buf))
1360 goto out;
1361 strbuf_setlen(&path, base_len);
1363 read_alternate_refs(path.buf, cb->fn, cb->data);
1365 out:
1366 strbuf_release(&path);
1367 return 0;
1370 void for_each_alternate_ref(alternate_ref_fn fn, void *data)
1372 struct alternate_refs_data cb;
1373 cb.fn = fn;
1374 cb.data = data;
1375 foreach_alt_odb(refs_from_alternate_cb, &cb);