http: allow providing extra headers for http requests
[git/debian.git] / transport.c
blob342db492cac5dfaec33de407bfca16aa9951781f
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 "protocol.h"
23 static void set_upstreams(struct transport *transport, struct ref *refs,
24 int pretend)
26 struct ref *ref;
27 for (ref = refs; ref; ref = ref->next) {
28 const char *localname;
29 const char *tmp;
30 const char *remotename;
31 int flag = 0;
33 * Check suitability for tracking. Must be successful /
34 * already up-to-date ref create/modify (not delete).
36 if (ref->status != REF_STATUS_OK &&
37 ref->status != REF_STATUS_UPTODATE)
38 continue;
39 if (!ref->peer_ref)
40 continue;
41 if (is_null_oid(&ref->new_oid))
42 continue;
44 /* Follow symbolic refs (mainly for HEAD). */
45 localname = ref->peer_ref->name;
46 remotename = ref->name;
47 tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING,
48 NULL, &flag);
49 if (tmp && flag & REF_ISSYMREF &&
50 starts_with(tmp, "refs/heads/"))
51 localname = tmp;
53 /* Both source and destination must be local branches. */
54 if (!localname || !starts_with(localname, "refs/heads/"))
55 continue;
56 if (!remotename || !starts_with(remotename, "refs/heads/"))
57 continue;
59 if (!pretend)
60 install_branch_config(BRANCH_CONFIG_VERBOSE,
61 localname + 11, transport->remote->name,
62 remotename);
63 else
64 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
65 localname + 11, remotename + 11,
66 transport->remote->name);
70 struct bundle_transport_data {
71 int fd;
72 struct bundle_header header;
75 static struct ref *get_refs_from_bundle(struct transport *transport,
76 int for_push,
77 const struct argv_array *ref_prefixes)
79 struct bundle_transport_data *data = transport->data;
80 struct ref *result = NULL;
81 int i;
83 if (for_push)
84 return NULL;
86 if (data->fd > 0)
87 close(data->fd);
88 data->fd = read_bundle_header(transport->url, &data->header);
89 if (data->fd < 0)
90 die ("Could not read bundle '%s'.", transport->url);
91 for (i = 0; i < data->header.references.nr; i++) {
92 struct ref_list_entry *e = data->header.references.list + i;
93 struct ref *ref = alloc_ref(e->name);
94 oidcpy(&ref->old_oid, &e->oid);
95 ref->next = result;
96 result = ref;
98 return result;
101 static int fetch_refs_from_bundle(struct transport *transport,
102 int nr_heads, struct ref **to_fetch)
104 struct bundle_transport_data *data = transport->data;
105 return unbundle(&data->header, data->fd,
106 transport->progress ? BUNDLE_VERBOSE : 0);
109 static int close_bundle(struct transport *transport)
111 struct bundle_transport_data *data = transport->data;
112 if (data->fd > 0)
113 close(data->fd);
114 free(data);
115 return 0;
118 struct git_transport_data {
119 struct git_transport_options options;
120 struct child_process *conn;
121 int fd[2];
122 unsigned got_remote_heads : 1;
123 enum protocol_version version;
124 struct oid_array extra_have;
125 struct oid_array shallow;
128 static int set_git_option(struct git_transport_options *opts,
129 const char *name, const char *value)
131 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
132 opts->uploadpack = value;
133 return 0;
134 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
135 opts->receivepack = value;
136 return 0;
137 } else if (!strcmp(name, TRANS_OPT_THIN)) {
138 opts->thin = !!value;
139 return 0;
140 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
141 opts->followtags = !!value;
142 return 0;
143 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
144 opts->keep = !!value;
145 return 0;
146 } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
147 opts->update_shallow = !!value;
148 return 0;
149 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
150 if (!value)
151 opts->depth = 0;
152 else {
153 char *end;
154 opts->depth = strtol(value, &end, 0);
155 if (*end)
156 die(_("transport: invalid depth option '%s'"), value);
158 return 0;
159 } else if (!strcmp(name, TRANS_OPT_DEEPEN_SINCE)) {
160 opts->deepen_since = value;
161 return 0;
162 } else if (!strcmp(name, TRANS_OPT_DEEPEN_NOT)) {
163 opts->deepen_not = (const struct string_list *)value;
164 return 0;
165 } else if (!strcmp(name, TRANS_OPT_DEEPEN_RELATIVE)) {
166 opts->deepen_relative = !!value;
167 return 0;
169 return 1;
172 static int connect_setup(struct transport *transport, int for_push)
174 struct git_transport_data *data = transport->data;
175 int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
177 if (data->conn)
178 return 0;
180 switch (transport->family) {
181 case TRANSPORT_FAMILY_ALL: break;
182 case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
183 case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
186 data->conn = git_connect(data->fd, transport->url,
187 for_push ? data->options.receivepack :
188 data->options.uploadpack,
189 flags);
191 return 0;
194 static struct ref *get_refs_via_connect(struct transport *transport, int for_push,
195 const struct argv_array *ref_prefixes)
197 struct git_transport_data *data = transport->data;
198 struct ref *refs = NULL;
199 struct packet_reader reader;
201 connect_setup(transport, for_push);
203 packet_reader_init(&reader, data->fd[0], NULL, 0,
204 PACKET_READ_CHOMP_NEWLINE |
205 PACKET_READ_GENTLE_ON_EOF);
207 data->version = discover_version(&reader);
208 switch (data->version) {
209 case protocol_v2:
210 get_remote_refs(data->fd[1], &reader, &refs, for_push,
211 ref_prefixes);
212 break;
213 case protocol_v1:
214 case protocol_v0:
215 get_remote_heads(&reader, &refs,
216 for_push ? REF_NORMAL : 0,
217 &data->extra_have,
218 &data->shallow);
219 break;
220 case protocol_unknown_version:
221 BUG("unknown protocol version");
223 data->got_remote_heads = 1;
225 return refs;
228 static int fetch_refs_via_pack(struct transport *transport,
229 int nr_heads, struct ref **to_fetch)
231 int ret = 0;
232 struct git_transport_data *data = transport->data;
233 struct ref *refs = NULL;
234 char *dest = xstrdup(transport->url);
235 struct fetch_pack_args args;
236 struct ref *refs_tmp = NULL;
238 memset(&args, 0, sizeof(args));
239 args.uploadpack = data->options.uploadpack;
240 args.keep_pack = data->options.keep;
241 args.lock_pack = 1;
242 args.use_thin_pack = data->options.thin;
243 args.include_tag = data->options.followtags;
244 args.verbose = (transport->verbose > 1);
245 args.quiet = (transport->verbose < 0);
246 args.no_progress = !transport->progress;
247 args.depth = data->options.depth;
248 args.deepen_since = data->options.deepen_since;
249 args.deepen_not = data->options.deepen_not;
250 args.deepen_relative = data->options.deepen_relative;
251 args.check_self_contained_and_connected =
252 data->options.check_self_contained_and_connected;
253 args.cloning = transport->cloning;
254 args.update_shallow = data->options.update_shallow;
255 args.stateless_rpc = transport->stateless_rpc;
257 if (!data->got_remote_heads)
258 refs_tmp = get_refs_via_connect(transport, 0, NULL);
260 switch (data->version) {
261 case protocol_v2:
262 refs = fetch_pack(&args, data->fd, data->conn,
263 refs_tmp ? refs_tmp : transport->remote_refs,
264 dest, to_fetch, nr_heads, &data->shallow,
265 &transport->pack_lockfile, data->version);
266 break;
267 case protocol_v1:
268 case protocol_v0:
269 refs = fetch_pack(&args, data->fd, data->conn,
270 refs_tmp ? refs_tmp : transport->remote_refs,
271 dest, to_fetch, nr_heads, &data->shallow,
272 &transport->pack_lockfile, data->version);
273 break;
274 case protocol_unknown_version:
275 BUG("unknown protocol version");
278 close(data->fd[0]);
279 close(data->fd[1]);
280 if (finish_connect(data->conn))
281 ret = -1;
282 data->conn = NULL;
283 data->got_remote_heads = 0;
284 data->options.self_contained_and_connected =
285 args.self_contained_and_connected;
287 if (refs == NULL)
288 ret = -1;
289 if (report_unmatched_refs(to_fetch, nr_heads))
290 ret = -1;
292 free_refs(refs_tmp);
293 free_refs(refs);
294 free(dest);
295 return ret;
298 static int push_had_errors(struct ref *ref)
300 for (; ref; ref = ref->next) {
301 switch (ref->status) {
302 case REF_STATUS_NONE:
303 case REF_STATUS_UPTODATE:
304 case REF_STATUS_OK:
305 break;
306 default:
307 return 1;
310 return 0;
313 int transport_refs_pushed(struct ref *ref)
315 for (; ref; ref = ref->next) {
316 switch(ref->status) {
317 case REF_STATUS_NONE:
318 case REF_STATUS_UPTODATE:
319 break;
320 default:
321 return 1;
324 return 0;
327 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
329 struct refspec rs;
331 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
332 return;
334 rs.src = ref->name;
335 rs.dst = NULL;
337 if (!remote_find_tracking(remote, &rs)) {
338 if (verbose)
339 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
340 if (ref->deletion) {
341 delete_ref(NULL, rs.dst, NULL, 0);
342 } else
343 update_ref("update by push", rs.dst, &ref->new_oid,
344 NULL, 0, 0);
345 free(rs.dst);
349 static void print_ref_status(char flag, const char *summary,
350 struct ref *to, struct ref *from, const char *msg,
351 int porcelain, int summary_width)
353 if (porcelain) {
354 if (from)
355 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
356 else
357 fprintf(stdout, "%c\t:%s\t", flag, to->name);
358 if (msg)
359 fprintf(stdout, "%s (%s)\n", summary, msg);
360 else
361 fprintf(stdout, "%s\n", summary);
362 } else {
363 fprintf(stderr, " %c %-*s ", flag, summary_width, summary);
364 if (from)
365 fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
366 else
367 fputs(prettify_refname(to->name), stderr);
368 if (msg) {
369 fputs(" (", stderr);
370 fputs(msg, stderr);
371 fputc(')', stderr);
373 fputc('\n', stderr);
377 static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_width)
379 if (ref->deletion)
380 print_ref_status('-', "[deleted]", ref, NULL, NULL,
381 porcelain, summary_width);
382 else if (is_null_oid(&ref->old_oid))
383 print_ref_status('*',
384 (starts_with(ref->name, "refs/tags/") ? "[new tag]" :
385 "[new branch]"),
386 ref, ref->peer_ref, NULL, porcelain, summary_width);
387 else {
388 struct strbuf quickref = STRBUF_INIT;
389 char type;
390 const char *msg;
392 strbuf_add_unique_abbrev(&quickref, ref->old_oid.hash,
393 DEFAULT_ABBREV);
394 if (ref->forced_update) {
395 strbuf_addstr(&quickref, "...");
396 type = '+';
397 msg = "forced update";
398 } else {
399 strbuf_addstr(&quickref, "..");
400 type = ' ';
401 msg = NULL;
403 strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash,
404 DEFAULT_ABBREV);
406 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
407 porcelain, summary_width);
408 strbuf_release(&quickref);
412 static int print_one_push_status(struct ref *ref, const char *dest, int count,
413 int porcelain, int summary_width)
415 if (!count) {
416 char *url = transport_anonymize_url(dest);
417 fprintf(porcelain ? stdout : stderr, "To %s\n", url);
418 free(url);
421 switch(ref->status) {
422 case REF_STATUS_NONE:
423 print_ref_status('X', "[no match]", ref, NULL, NULL,
424 porcelain, summary_width);
425 break;
426 case REF_STATUS_REJECT_NODELETE:
427 print_ref_status('!', "[rejected]", ref, NULL,
428 "remote does not support deleting refs",
429 porcelain, summary_width);
430 break;
431 case REF_STATUS_UPTODATE:
432 print_ref_status('=', "[up to date]", ref,
433 ref->peer_ref, NULL, porcelain, summary_width);
434 break;
435 case REF_STATUS_REJECT_NONFASTFORWARD:
436 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
437 "non-fast-forward", porcelain, summary_width);
438 break;
439 case REF_STATUS_REJECT_ALREADY_EXISTS:
440 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
441 "already exists", porcelain, summary_width);
442 break;
443 case REF_STATUS_REJECT_FETCH_FIRST:
444 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
445 "fetch first", porcelain, summary_width);
446 break;
447 case REF_STATUS_REJECT_NEEDS_FORCE:
448 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
449 "needs force", porcelain, summary_width);
450 break;
451 case REF_STATUS_REJECT_STALE:
452 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
453 "stale info", porcelain, summary_width);
454 break;
455 case REF_STATUS_REJECT_SHALLOW:
456 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
457 "new shallow roots not allowed",
458 porcelain, summary_width);
459 break;
460 case REF_STATUS_REMOTE_REJECT:
461 print_ref_status('!', "[remote rejected]", ref,
462 ref->deletion ? NULL : ref->peer_ref,
463 ref->remote_status, porcelain, summary_width);
464 break;
465 case REF_STATUS_EXPECTING_REPORT:
466 print_ref_status('!', "[remote failure]", ref,
467 ref->deletion ? NULL : ref->peer_ref,
468 "remote failed to report status",
469 porcelain, summary_width);
470 break;
471 case REF_STATUS_ATOMIC_PUSH_FAILED:
472 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
473 "atomic push failed", porcelain, summary_width);
474 break;
475 case REF_STATUS_OK:
476 print_ok_ref_status(ref, porcelain, summary_width);
477 break;
480 return 1;
483 static int measure_abbrev(const struct object_id *oid, int sofar)
485 char hex[GIT_MAX_HEXSZ + 1];
486 int w = find_unique_abbrev_r(hex, oid->hash, DEFAULT_ABBREV);
488 return (w < sofar) ? sofar : w;
491 int transport_summary_width(const struct ref *refs)
493 int maxw = -1;
495 for (; refs; refs = refs->next) {
496 maxw = measure_abbrev(&refs->old_oid, maxw);
497 maxw = measure_abbrev(&refs->new_oid, maxw);
499 if (maxw < 0)
500 maxw = FALLBACK_DEFAULT_ABBREV;
501 return (2 * maxw + 3);
504 void transport_print_push_status(const char *dest, struct ref *refs,
505 int verbose, int porcelain, unsigned int *reject_reasons)
507 struct ref *ref;
508 int n = 0;
509 char *head;
510 int summary_width = transport_summary_width(refs);
512 head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
514 if (verbose) {
515 for (ref = refs; ref; ref = ref->next)
516 if (ref->status == REF_STATUS_UPTODATE)
517 n += print_one_push_status(ref, dest, n,
518 porcelain, summary_width);
521 for (ref = refs; ref; ref = ref->next)
522 if (ref->status == REF_STATUS_OK)
523 n += print_one_push_status(ref, dest, n,
524 porcelain, summary_width);
526 *reject_reasons = 0;
527 for (ref = refs; ref; ref = ref->next) {
528 if (ref->status != REF_STATUS_NONE &&
529 ref->status != REF_STATUS_UPTODATE &&
530 ref->status != REF_STATUS_OK)
531 n += print_one_push_status(ref, dest, n,
532 porcelain, summary_width);
533 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
534 if (head != NULL && !strcmp(head, ref->name))
535 *reject_reasons |= REJECT_NON_FF_HEAD;
536 else
537 *reject_reasons |= REJECT_NON_FF_OTHER;
538 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
539 *reject_reasons |= REJECT_ALREADY_EXISTS;
540 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
541 *reject_reasons |= REJECT_FETCH_FIRST;
542 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
543 *reject_reasons |= REJECT_NEEDS_FORCE;
546 free(head);
549 void transport_verify_remote_names(int nr_heads, const char **heads)
551 int i;
553 for (i = 0; i < nr_heads; i++) {
554 const char *local = heads[i];
555 const char *remote = strrchr(heads[i], ':');
557 if (*local == '+')
558 local++;
560 /* A matching refspec is okay. */
561 if (remote == local && remote[1] == '\0')
562 continue;
564 remote = remote ? (remote + 1) : local;
565 if (check_refname_format(remote,
566 REFNAME_ALLOW_ONELEVEL|REFNAME_REFSPEC_PATTERN))
567 die("remote part of refspec is not a valid name in %s",
568 heads[i]);
572 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
574 struct git_transport_data *data = transport->data;
575 struct send_pack_args args;
576 int ret = 0;
578 if (!data->got_remote_heads)
579 get_refs_via_connect(transport, 1, NULL);
581 memset(&args, 0, sizeof(args));
582 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
583 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
584 args.use_thin_pack = data->options.thin;
585 args.verbose = (transport->verbose > 0);
586 args.quiet = (transport->verbose < 0);
587 args.progress = transport->progress;
588 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
589 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
590 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
591 args.push_options = transport->push_options;
592 args.url = transport->url;
594 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
595 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
596 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
597 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
598 else
599 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
601 switch (data->version) {
602 case protocol_v2:
603 die("support for protocol v2 not implemented yet");
604 break;
605 case protocol_v1:
606 case protocol_v0:
607 ret = send_pack(&args, data->fd, data->conn, remote_refs,
608 &data->extra_have);
609 break;
610 case protocol_unknown_version:
611 BUG("unknown protocol version");
614 close(data->fd[1]);
615 close(data->fd[0]);
616 ret |= finish_connect(data->conn);
617 data->conn = NULL;
618 data->got_remote_heads = 0;
620 return ret;
623 static int connect_git(struct transport *transport, const char *name,
624 const char *executable, int fd[2])
626 struct git_transport_data *data = transport->data;
627 data->conn = git_connect(data->fd, transport->url,
628 executable, 0);
629 fd[0] = data->fd[0];
630 fd[1] = data->fd[1];
631 return 0;
634 static int disconnect_git(struct transport *transport)
636 struct git_transport_data *data = transport->data;
637 if (data->conn) {
638 if (data->got_remote_heads)
639 packet_flush(data->fd[1]);
640 close(data->fd[0]);
641 close(data->fd[1]);
642 finish_connect(data->conn);
645 free(data);
646 return 0;
649 static struct transport_vtable taken_over_vtable = {
650 NULL,
651 get_refs_via_connect,
652 fetch_refs_via_pack,
653 git_transport_push,
654 NULL,
655 disconnect_git
658 void transport_take_over(struct transport *transport,
659 struct child_process *child)
661 struct git_transport_data *data;
663 if (!transport->smart_options)
664 die("BUG: taking over transport requires non-NULL "
665 "smart_options field.");
667 data = xcalloc(1, sizeof(*data));
668 data->options = *transport->smart_options;
669 data->conn = child;
670 data->fd[0] = data->conn->out;
671 data->fd[1] = data->conn->in;
672 data->got_remote_heads = 0;
673 transport->data = data;
675 transport->vtable = &taken_over_vtable;
676 transport->smart_options = &(data->options);
678 transport->cannot_reuse = 1;
681 static int is_file(const char *url)
683 struct stat buf;
684 if (stat(url, &buf))
685 return 0;
686 return S_ISREG(buf.st_mode);
689 static int external_specification_len(const char *url)
691 return strchr(url, ':') - url;
694 static const struct string_list *protocol_whitelist(void)
696 static int enabled = -1;
697 static struct string_list allowed = STRING_LIST_INIT_DUP;
699 if (enabled < 0) {
700 const char *v = getenv("GIT_ALLOW_PROTOCOL");
701 if (v) {
702 string_list_split(&allowed, v, ':', -1);
703 string_list_sort(&allowed);
704 enabled = 1;
705 } else {
706 enabled = 0;
710 return enabled ? &allowed : NULL;
713 enum protocol_allow_config {
714 PROTOCOL_ALLOW_NEVER = 0,
715 PROTOCOL_ALLOW_USER_ONLY,
716 PROTOCOL_ALLOW_ALWAYS
719 static enum protocol_allow_config parse_protocol_config(const char *key,
720 const char *value)
722 if (!strcasecmp(value, "always"))
723 return PROTOCOL_ALLOW_ALWAYS;
724 else if (!strcasecmp(value, "never"))
725 return PROTOCOL_ALLOW_NEVER;
726 else if (!strcasecmp(value, "user"))
727 return PROTOCOL_ALLOW_USER_ONLY;
729 die("unknown value for config '%s': %s", key, value);
732 static enum protocol_allow_config get_protocol_config(const char *type)
734 char *key = xstrfmt("protocol.%s.allow", type);
735 char *value;
737 /* first check the per-protocol config */
738 if (!git_config_get_string(key, &value)) {
739 enum protocol_allow_config ret =
740 parse_protocol_config(key, value);
741 free(key);
742 free(value);
743 return ret;
745 free(key);
747 /* if defined, fallback to user-defined default for unknown protocols */
748 if (!git_config_get_string("protocol.allow", &value)) {
749 enum protocol_allow_config ret =
750 parse_protocol_config("protocol.allow", value);
751 free(value);
752 return ret;
755 /* fallback to built-in defaults */
756 /* known safe */
757 if (!strcmp(type, "http") ||
758 !strcmp(type, "https") ||
759 !strcmp(type, "git") ||
760 !strcmp(type, "ssh") ||
761 !strcmp(type, "file"))
762 return PROTOCOL_ALLOW_ALWAYS;
764 /* known scary; err on the side of caution */
765 if (!strcmp(type, "ext"))
766 return PROTOCOL_ALLOW_NEVER;
768 /* unknown; by default let them be used only directly by the user */
769 return PROTOCOL_ALLOW_USER_ONLY;
772 int is_transport_allowed(const char *type, int from_user)
774 const struct string_list *whitelist = protocol_whitelist();
775 if (whitelist)
776 return string_list_has_string(whitelist, type);
778 switch (get_protocol_config(type)) {
779 case PROTOCOL_ALLOW_ALWAYS:
780 return 1;
781 case PROTOCOL_ALLOW_NEVER:
782 return 0;
783 case PROTOCOL_ALLOW_USER_ONLY:
784 if (from_user < 0)
785 from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
786 return from_user;
789 die("BUG: invalid protocol_allow_config type");
792 void transport_check_allowed(const char *type)
794 if (!is_transport_allowed(type, -1))
795 die("transport '%s' not allowed", type);
798 static struct transport_vtable bundle_vtable = {
799 NULL,
800 get_refs_from_bundle,
801 fetch_refs_from_bundle,
802 NULL,
803 NULL,
804 close_bundle
807 static struct transport_vtable builtin_smart_vtable = {
808 NULL,
809 get_refs_via_connect,
810 fetch_refs_via_pack,
811 git_transport_push,
812 connect_git,
813 disconnect_git
816 struct transport *transport_get(struct remote *remote, const char *url)
818 const char *helper;
819 struct transport *ret = xcalloc(1, sizeof(*ret));
821 ret->progress = isatty(2);
823 if (!remote)
824 die("No remote provided to transport_get()");
826 ret->got_remote_refs = 0;
827 ret->remote = remote;
828 helper = remote->foreign_vcs;
830 if (!url && remote->url)
831 url = remote->url[0];
832 ret->url = url;
834 /* maybe it is a foreign URL? */
835 if (url) {
836 const char *p = url;
838 while (is_urlschemechar(p == url, *p))
839 p++;
840 if (starts_with(p, "::"))
841 helper = xstrndup(url, p - url);
844 if (helper) {
845 transport_helper_init(ret, helper);
846 } else if (starts_with(url, "rsync:")) {
847 die("git-over-rsync is no longer supported");
848 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
849 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
850 transport_check_allowed("file");
851 ret->data = data;
852 ret->vtable = &bundle_vtable;
853 ret->smart_options = NULL;
854 } else if (!is_url(url)
855 || starts_with(url, "file://")
856 || starts_with(url, "git://")
857 || starts_with(url, "ssh://")
858 || starts_with(url, "git+ssh://") /* deprecated - do not use */
859 || starts_with(url, "ssh+git://") /* deprecated - do not use */
862 * These are builtin smart transports; "allowed" transports
863 * will be checked individually in git_connect.
865 struct git_transport_data *data = xcalloc(1, sizeof(*data));
866 ret->data = data;
867 ret->vtable = &builtin_smart_vtable;
868 ret->smart_options = &(data->options);
870 data->conn = NULL;
871 data->got_remote_heads = 0;
872 } else {
873 /* Unknown protocol in URL. Pass to external handler. */
874 int len = external_specification_len(url);
875 char *handler = xmemdupz(url, len);
876 transport_helper_init(ret, handler);
879 if (ret->smart_options) {
880 ret->smart_options->thin = 1;
881 ret->smart_options->uploadpack = "git-upload-pack";
882 if (remote->uploadpack)
883 ret->smart_options->uploadpack = remote->uploadpack;
884 ret->smart_options->receivepack = "git-receive-pack";
885 if (remote->receivepack)
886 ret->smart_options->receivepack = remote->receivepack;
889 return ret;
892 int transport_set_option(struct transport *transport,
893 const char *name, const char *value)
895 int git_reports = 1, protocol_reports = 1;
897 if (transport->smart_options)
898 git_reports = set_git_option(transport->smart_options,
899 name, value);
901 if (transport->vtable->set_option)
902 protocol_reports = transport->vtable->set_option(transport,
903 name, value);
905 /* If either report is 0, report 0 (success). */
906 if (!git_reports || !protocol_reports)
907 return 0;
908 /* If either reports -1 (invalid value), report -1. */
909 if ((git_reports == -1) || (protocol_reports == -1))
910 return -1;
911 /* Otherwise if both report unknown, report unknown. */
912 return 1;
915 void transport_set_verbosity(struct transport *transport, int verbosity,
916 int force_progress)
918 if (verbosity >= 1)
919 transport->verbose = verbosity <= 3 ? verbosity : 3;
920 if (verbosity < 0)
921 transport->verbose = -1;
924 * Rules used to determine whether to report progress (processing aborts
925 * when a rule is satisfied):
927 * . Report progress, if force_progress is 1 (ie. --progress).
928 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
929 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
930 * . Report progress if isatty(2) is 1.
932 if (force_progress >= 0)
933 transport->progress = !!force_progress;
934 else
935 transport->progress = verbosity >= 0 && isatty(2);
938 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
940 int i;
942 fprintf(stderr, _("The following submodule paths contain changes that can\n"
943 "not be found on any remote:\n"));
944 for (i = 0; i < needs_pushing->nr; i++)
945 fprintf(stderr, " %s\n", needs_pushing->items[i].string);
946 fprintf(stderr, _("\nPlease try\n\n"
947 " git push --recurse-submodules=on-demand\n\n"
948 "or cd to the path and use\n\n"
949 " git push\n\n"
950 "to push them to a remote.\n\n"));
952 string_list_clear(needs_pushing, 0);
954 die(_("Aborting."));
957 static int run_pre_push_hook(struct transport *transport,
958 struct ref *remote_refs)
960 int ret = 0, x;
961 struct ref *r;
962 struct child_process proc = CHILD_PROCESS_INIT;
963 struct strbuf buf;
964 const char *argv[4];
966 if (!(argv[0] = find_hook("pre-push")))
967 return 0;
969 argv[1] = transport->remote->name;
970 argv[2] = transport->url;
971 argv[3] = NULL;
973 proc.argv = argv;
974 proc.in = -1;
976 if (start_command(&proc)) {
977 finish_command(&proc);
978 return -1;
981 sigchain_push(SIGPIPE, SIG_IGN);
983 strbuf_init(&buf, 256);
985 for (r = remote_refs; r; r = r->next) {
986 if (!r->peer_ref) continue;
987 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
988 if (r->status == REF_STATUS_REJECT_STALE) continue;
989 if (r->status == REF_STATUS_UPTODATE) continue;
991 strbuf_reset(&buf);
992 strbuf_addf( &buf, "%s %s %s %s\n",
993 r->peer_ref->name, oid_to_hex(&r->new_oid),
994 r->name, oid_to_hex(&r->old_oid));
996 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
997 /* We do not mind if a hook does not read all refs. */
998 if (errno != EPIPE)
999 ret = -1;
1000 break;
1004 strbuf_release(&buf);
1006 x = close(proc.in);
1007 if (!ret)
1008 ret = x;
1010 sigchain_pop(SIGPIPE);
1012 x = finish_command(&proc);
1013 if (!ret)
1014 ret = x;
1016 return ret;
1019 int transport_push(struct transport *transport,
1020 int refspec_nr, const char **refspec, int flags,
1021 unsigned int *reject_reasons)
1023 *reject_reasons = 0;
1024 transport_verify_remote_names(refspec_nr, refspec);
1026 if (transport->vtable->push_refs) {
1027 struct ref *remote_refs;
1028 struct ref *local_refs = get_local_heads();
1029 int match_flags = MATCH_REFS_NONE;
1030 int verbose = (transport->verbose > 0);
1031 int quiet = (transport->verbose < 0);
1032 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1033 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1034 int push_ret, ret, err;
1035 struct refspec *tmp_rs;
1036 struct argv_array ref_prefixes = ARGV_ARRAY_INIT;
1037 int i;
1039 if (check_push_refs(local_refs, refspec_nr, refspec) < 0)
1040 return -1;
1042 tmp_rs = parse_push_refspec(refspec_nr, refspec);
1043 for (i = 0; i < refspec_nr; i++) {
1044 const char *prefix = NULL;
1046 if (tmp_rs[i].dst)
1047 prefix = tmp_rs[i].dst;
1048 else if (tmp_rs[i].src && !tmp_rs[i].exact_sha1)
1049 prefix = tmp_rs[i].src;
1051 if (prefix) {
1052 const char *glob = strchr(prefix, '*');
1053 if (glob)
1054 argv_array_pushf(&ref_prefixes, "%.*s",
1055 (int)(glob - prefix),
1056 prefix);
1057 else
1058 expand_ref_prefix(&ref_prefixes, prefix);
1062 remote_refs = transport->vtable->get_refs_list(transport, 1,
1063 &ref_prefixes);
1065 argv_array_clear(&ref_prefixes);
1066 free_refspec(refspec_nr, tmp_rs);
1068 if (flags & TRANSPORT_PUSH_ALL)
1069 match_flags |= MATCH_REFS_ALL;
1070 if (flags & TRANSPORT_PUSH_MIRROR)
1071 match_flags |= MATCH_REFS_MIRROR;
1072 if (flags & TRANSPORT_PUSH_PRUNE)
1073 match_flags |= MATCH_REFS_PRUNE;
1074 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1075 match_flags |= MATCH_REFS_FOLLOW_TAGS;
1077 if (match_push_refs(local_refs, &remote_refs,
1078 refspec_nr, refspec, match_flags)) {
1079 return -1;
1082 if (transport->smart_options &&
1083 transport->smart_options->cas &&
1084 !is_empty_cas(transport->smart_options->cas))
1085 apply_push_cas(transport->smart_options->cas,
1086 transport->remote, remote_refs);
1088 set_ref_status_for_push(remote_refs,
1089 flags & TRANSPORT_PUSH_MIRROR,
1090 flags & TRANSPORT_PUSH_FORCE);
1092 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1093 if (run_pre_push_hook(transport, remote_refs))
1094 return -1;
1096 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1097 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1098 !is_bare_repository()) {
1099 struct ref *ref = remote_refs;
1100 struct oid_array commits = OID_ARRAY_INIT;
1102 for (; ref; ref = ref->next)
1103 if (!is_null_oid(&ref->new_oid))
1104 oid_array_append(&commits,
1105 &ref->new_oid);
1107 if (!push_unpushed_submodules(&commits,
1108 transport->remote,
1109 refspec, refspec_nr,
1110 transport->push_options,
1111 pretend)) {
1112 oid_array_clear(&commits);
1113 die("Failed to push all needed submodules!");
1115 oid_array_clear(&commits);
1118 if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1119 ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1120 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1121 !pretend)) && !is_bare_repository()) {
1122 struct ref *ref = remote_refs;
1123 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1124 struct oid_array commits = OID_ARRAY_INIT;
1126 for (; ref; ref = ref->next)
1127 if (!is_null_oid(&ref->new_oid))
1128 oid_array_append(&commits,
1129 &ref->new_oid);
1131 if (find_unpushed_submodules(&commits, transport->remote->name,
1132 &needs_pushing)) {
1133 oid_array_clear(&commits);
1134 die_with_unpushed_submodules(&needs_pushing);
1136 string_list_clear(&needs_pushing, 0);
1137 oid_array_clear(&commits);
1140 if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY))
1141 push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1142 else
1143 push_ret = 0;
1144 err = push_had_errors(remote_refs);
1145 ret = push_ret | err;
1147 if (!quiet || err)
1148 transport_print_push_status(transport->url, remote_refs,
1149 verbose | porcelain, porcelain,
1150 reject_reasons);
1152 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1153 set_upstreams(transport, remote_refs, pretend);
1155 if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1156 TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1157 struct ref *ref;
1158 for (ref = remote_refs; ref; ref = ref->next)
1159 transport_update_tracking_ref(transport->remote, ref, verbose);
1162 if (porcelain && !push_ret)
1163 puts("Done");
1164 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1165 fprintf(stderr, "Everything up-to-date\n");
1167 return ret;
1169 return 1;
1172 const struct ref *transport_get_remote_refs(struct transport *transport,
1173 const struct argv_array *ref_prefixes)
1175 if (!transport->got_remote_refs) {
1176 transport->remote_refs =
1177 transport->vtable->get_refs_list(transport, 0,
1178 ref_prefixes);
1179 transport->got_remote_refs = 1;
1182 return transport->remote_refs;
1185 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1187 int rc;
1188 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1189 struct ref **heads = NULL;
1190 struct ref *rm;
1192 for (rm = refs; rm; rm = rm->next) {
1193 nr_refs++;
1194 if (rm->peer_ref &&
1195 !is_null_oid(&rm->old_oid) &&
1196 !oidcmp(&rm->peer_ref->old_oid, &rm->old_oid))
1197 continue;
1198 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1199 heads[nr_heads++] = rm;
1202 if (!nr_heads) {
1204 * When deepening of a shallow repository is requested,
1205 * then local and remote refs are likely to still be equal.
1206 * Just feed them all to the fetch method in that case.
1207 * This condition shouldn't be met in a non-deepening fetch
1208 * (see builtin/fetch.c:quickfetch()).
1210 ALLOC_ARRAY(heads, nr_refs);
1211 for (rm = refs; rm; rm = rm->next)
1212 heads[nr_heads++] = rm;
1215 rc = transport->vtable->fetch(transport, nr_heads, heads);
1217 free(heads);
1218 return rc;
1221 void transport_unlock_pack(struct transport *transport)
1223 if (transport->pack_lockfile) {
1224 unlink_or_warn(transport->pack_lockfile);
1225 FREE_AND_NULL(transport->pack_lockfile);
1229 int transport_connect(struct transport *transport, const char *name,
1230 const char *exec, int fd[2])
1232 if (transport->vtable->connect)
1233 return transport->vtable->connect(transport, name, exec, fd);
1234 else
1235 die("Operation not supported by protocol");
1238 int transport_disconnect(struct transport *transport)
1240 int ret = 0;
1241 if (transport->vtable->disconnect)
1242 ret = transport->vtable->disconnect(transport);
1243 free(transport);
1244 return ret;
1248 * Strip username (and password) from a URL and return
1249 * it in a newly allocated string.
1251 char *transport_anonymize_url(const char *url)
1253 char *scheme_prefix, *anon_part;
1254 size_t anon_len, prefix_len = 0;
1256 anon_part = strchr(url, '@');
1257 if (url_is_local_not_ssh(url) || !anon_part)
1258 goto literal_copy;
1260 anon_len = strlen(++anon_part);
1261 scheme_prefix = strstr(url, "://");
1262 if (!scheme_prefix) {
1263 if (!strchr(anon_part, ':'))
1264 /* cannot be "me@there:/path/name" */
1265 goto literal_copy;
1266 } else {
1267 const char *cp;
1268 /* make sure scheme is reasonable */
1269 for (cp = url; cp < scheme_prefix; cp++) {
1270 switch (*cp) {
1271 /* RFC 1738 2.1 */
1272 case '+': case '.': case '-':
1273 break; /* ok */
1274 default:
1275 if (isalnum(*cp))
1276 break;
1277 /* it isn't */
1278 goto literal_copy;
1281 /* @ past the first slash does not count */
1282 cp = strchr(scheme_prefix + 3, '/');
1283 if (cp && cp < anon_part)
1284 goto literal_copy;
1285 prefix_len = scheme_prefix - url + 3;
1287 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1288 (int)anon_len, anon_part);
1289 literal_copy:
1290 return xstrdup(url);
1293 static void read_alternate_refs(const char *path,
1294 alternate_ref_fn *cb,
1295 void *data)
1297 struct child_process cmd = CHILD_PROCESS_INIT;
1298 struct strbuf line = STRBUF_INIT;
1299 FILE *fh;
1301 cmd.git_cmd = 1;
1302 argv_array_pushf(&cmd.args, "--git-dir=%s", path);
1303 argv_array_push(&cmd.args, "for-each-ref");
1304 argv_array_push(&cmd.args, "--format=%(objectname) %(refname)");
1305 cmd.env = local_repo_env;
1306 cmd.out = -1;
1308 if (start_command(&cmd))
1309 return;
1311 fh = xfdopen(cmd.out, "r");
1312 while (strbuf_getline_lf(&line, fh) != EOF) {
1313 struct object_id oid;
1315 if (get_oid_hex(line.buf, &oid) ||
1316 line.buf[GIT_SHA1_HEXSZ] != ' ') {
1317 warning("invalid line while parsing alternate refs: %s",
1318 line.buf);
1319 break;
1322 cb(line.buf + GIT_SHA1_HEXSZ + 1, &oid, data);
1325 fclose(fh);
1326 finish_command(&cmd);
1329 struct alternate_refs_data {
1330 alternate_ref_fn *fn;
1331 void *data;
1334 static int refs_from_alternate_cb(struct alternate_object_database *e,
1335 void *data)
1337 struct strbuf path = STRBUF_INIT;
1338 size_t base_len;
1339 struct alternate_refs_data *cb = data;
1341 if (!strbuf_realpath(&path, e->path, 0))
1342 goto out;
1343 if (!strbuf_strip_suffix(&path, "/objects"))
1344 goto out;
1345 base_len = path.len;
1347 /* Is this a git repository with refs? */
1348 strbuf_addstr(&path, "/refs");
1349 if (!is_directory(path.buf))
1350 goto out;
1351 strbuf_setlen(&path, base_len);
1353 read_alternate_refs(path.buf, cb->fn, cb->data);
1355 out:
1356 strbuf_release(&path);
1357 return 0;
1360 void for_each_alternate_ref(alternate_ref_fn fn, void *data)
1362 struct alternate_refs_data cb;
1363 cb.fn = fn;
1364 cb.data = data;
1365 foreach_alt_odb(refs_from_alternate_cb, &cb);