test-lib: check Bash version for '-x' without using shell arrays
[git.git] / transport.c
blob00d48b5b565b0edfa826a8c426e76cfc60a2f0a1
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"
22 static void set_upstreams(struct transport *transport, struct ref *refs,
23 int pretend)
25 struct ref *ref;
26 for (ref = refs; ref; ref = ref->next) {
27 const char *localname;
28 const char *tmp;
29 const char *remotename;
30 int flag = 0;
32 * Check suitability for tracking. Must be successful /
33 * already up-to-date ref create/modify (not delete).
35 if (ref->status != REF_STATUS_OK &&
36 ref->status != REF_STATUS_UPTODATE)
37 continue;
38 if (!ref->peer_ref)
39 continue;
40 if (is_null_oid(&ref->new_oid))
41 continue;
43 /* Follow symbolic refs (mainly for HEAD). */
44 localname = ref->peer_ref->name;
45 remotename = ref->name;
46 tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING,
47 NULL, &flag);
48 if (tmp && flag & REF_ISSYMREF &&
49 starts_with(tmp, "refs/heads/"))
50 localname = tmp;
52 /* Both source and destination must be local branches. */
53 if (!localname || !starts_with(localname, "refs/heads/"))
54 continue;
55 if (!remotename || !starts_with(remotename, "refs/heads/"))
56 continue;
58 if (!pretend)
59 install_branch_config(BRANCH_CONFIG_VERBOSE,
60 localname + 11, transport->remote->name,
61 remotename);
62 else
63 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
64 localname + 11, remotename + 11,
65 transport->remote->name);
69 struct bundle_transport_data {
70 int fd;
71 struct bundle_header header;
74 static struct ref *get_refs_from_bundle(struct transport *transport, int for_push)
76 struct bundle_transport_data *data = transport->data;
77 struct ref *result = NULL;
78 int i;
80 if (for_push)
81 return NULL;
83 if (data->fd > 0)
84 close(data->fd);
85 data->fd = read_bundle_header(transport->url, &data->header);
86 if (data->fd < 0)
87 die ("Could not read bundle '%s'.", transport->url);
88 for (i = 0; i < data->header.references.nr; i++) {
89 struct ref_list_entry *e = data->header.references.list + i;
90 struct ref *ref = alloc_ref(e->name);
91 oidcpy(&ref->old_oid, &e->oid);
92 ref->next = result;
93 result = ref;
95 return result;
98 static int fetch_refs_from_bundle(struct transport *transport,
99 int nr_heads, struct ref **to_fetch)
101 struct bundle_transport_data *data = transport->data;
102 return unbundle(&data->header, data->fd,
103 transport->progress ? BUNDLE_VERBOSE : 0);
106 static int close_bundle(struct transport *transport)
108 struct bundle_transport_data *data = transport->data;
109 if (data->fd > 0)
110 close(data->fd);
111 free(data);
112 return 0;
115 struct git_transport_data {
116 struct git_transport_options options;
117 struct child_process *conn;
118 int fd[2];
119 unsigned got_remote_heads : 1;
120 struct oid_array extra_have;
121 struct oid_array shallow;
124 static int set_git_option(struct git_transport_options *opts,
125 const char *name, const char *value)
127 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
128 opts->uploadpack = value;
129 return 0;
130 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
131 opts->receivepack = value;
132 return 0;
133 } else if (!strcmp(name, TRANS_OPT_THIN)) {
134 opts->thin = !!value;
135 return 0;
136 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
137 opts->followtags = !!value;
138 return 0;
139 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
140 opts->keep = !!value;
141 return 0;
142 } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
143 opts->update_shallow = !!value;
144 return 0;
145 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
146 if (!value)
147 opts->depth = 0;
148 else {
149 char *end;
150 opts->depth = strtol(value, &end, 0);
151 if (*end)
152 die(_("transport: invalid depth option '%s'"), value);
154 return 0;
155 } else if (!strcmp(name, TRANS_OPT_DEEPEN_SINCE)) {
156 opts->deepen_since = value;
157 return 0;
158 } else if (!strcmp(name, TRANS_OPT_DEEPEN_NOT)) {
159 opts->deepen_not = (const struct string_list *)value;
160 return 0;
161 } else if (!strcmp(name, TRANS_OPT_DEEPEN_RELATIVE)) {
162 opts->deepen_relative = !!value;
163 return 0;
164 } else if (!strcmp(name, TRANS_OPT_FROM_PROMISOR)) {
165 opts->from_promisor = !!value;
166 return 0;
167 } else if (!strcmp(name, TRANS_OPT_NO_DEPENDENTS)) {
168 opts->no_dependents = !!value;
169 return 0;
170 } else if (!strcmp(name, TRANS_OPT_LIST_OBJECTS_FILTER)) {
171 parse_list_objects_filter(&opts->filter_options, value);
172 return 0;
174 return 1;
177 static int connect_setup(struct transport *transport, int for_push)
179 struct git_transport_data *data = transport->data;
180 int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
182 if (data->conn)
183 return 0;
185 switch (transport->family) {
186 case TRANSPORT_FAMILY_ALL: break;
187 case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
188 case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
191 data->conn = git_connect(data->fd, transport->url,
192 for_push ? data->options.receivepack :
193 data->options.uploadpack,
194 flags);
196 return 0;
199 static struct ref *get_refs_via_connect(struct transport *transport, int for_push)
201 struct git_transport_data *data = transport->data;
202 struct ref *refs;
204 connect_setup(transport, for_push);
205 get_remote_heads(data->fd[0], NULL, 0, &refs,
206 for_push ? REF_NORMAL : 0,
207 &data->extra_have,
208 &data->shallow);
209 data->got_remote_heads = 1;
211 return refs;
214 static int fetch_refs_via_pack(struct transport *transport,
215 int nr_heads, struct ref **to_fetch)
217 int ret = 0;
218 struct git_transport_data *data = transport->data;
219 struct ref *refs;
220 char *dest = xstrdup(transport->url);
221 struct fetch_pack_args args;
222 struct ref *refs_tmp = NULL;
224 memset(&args, 0, sizeof(args));
225 args.uploadpack = data->options.uploadpack;
226 args.keep_pack = data->options.keep;
227 args.lock_pack = 1;
228 args.use_thin_pack = data->options.thin;
229 args.include_tag = data->options.followtags;
230 args.verbose = (transport->verbose > 1);
231 args.quiet = (transport->verbose < 0);
232 args.no_progress = !transport->progress;
233 args.depth = data->options.depth;
234 args.deepen_since = data->options.deepen_since;
235 args.deepen_not = data->options.deepen_not;
236 args.deepen_relative = data->options.deepen_relative;
237 args.check_self_contained_and_connected =
238 data->options.check_self_contained_and_connected;
239 args.cloning = transport->cloning;
240 args.update_shallow = data->options.update_shallow;
241 args.from_promisor = data->options.from_promisor;
242 args.no_dependents = data->options.no_dependents;
243 args.filter_options = data->options.filter_options;
245 if (!data->got_remote_heads) {
246 connect_setup(transport, 0);
247 get_remote_heads(data->fd[0], NULL, 0, &refs_tmp, 0,
248 NULL, &data->shallow);
249 data->got_remote_heads = 1;
252 refs = fetch_pack(&args, data->fd, data->conn,
253 refs_tmp ? refs_tmp : transport->remote_refs,
254 dest, to_fetch, nr_heads, &data->shallow,
255 &transport->pack_lockfile);
256 close(data->fd[0]);
257 close(data->fd[1]);
258 if (finish_connect(data->conn))
259 ret = -1;
260 data->conn = NULL;
261 data->got_remote_heads = 0;
262 data->options.self_contained_and_connected =
263 args.self_contained_and_connected;
265 if (refs == NULL)
266 ret = -1;
267 if (report_unmatched_refs(to_fetch, nr_heads))
268 ret = -1;
270 free_refs(refs_tmp);
271 free_refs(refs);
272 free(dest);
273 return ret;
276 static int push_had_errors(struct ref *ref)
278 for (; ref; ref = ref->next) {
279 switch (ref->status) {
280 case REF_STATUS_NONE:
281 case REF_STATUS_UPTODATE:
282 case REF_STATUS_OK:
283 break;
284 default:
285 return 1;
288 return 0;
291 int transport_refs_pushed(struct ref *ref)
293 for (; ref; ref = ref->next) {
294 switch(ref->status) {
295 case REF_STATUS_NONE:
296 case REF_STATUS_UPTODATE:
297 break;
298 default:
299 return 1;
302 return 0;
305 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
307 struct refspec rs;
309 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
310 return;
312 rs.src = ref->name;
313 rs.dst = NULL;
315 if (!remote_find_tracking(remote, &rs)) {
316 if (verbose)
317 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
318 if (ref->deletion) {
319 delete_ref(NULL, rs.dst, NULL, 0);
320 } else
321 update_ref("update by push", rs.dst, &ref->new_oid,
322 NULL, 0, 0);
323 free(rs.dst);
327 static void print_ref_status(char flag, const char *summary,
328 struct ref *to, struct ref *from, const char *msg,
329 int porcelain, int summary_width)
331 if (porcelain) {
332 if (from)
333 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
334 else
335 fprintf(stdout, "%c\t:%s\t", flag, to->name);
336 if (msg)
337 fprintf(stdout, "%s (%s)\n", summary, msg);
338 else
339 fprintf(stdout, "%s\n", summary);
340 } else {
341 fprintf(stderr, " %c %-*s ", flag, summary_width, summary);
342 if (from)
343 fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
344 else
345 fputs(prettify_refname(to->name), stderr);
346 if (msg) {
347 fputs(" (", stderr);
348 fputs(msg, stderr);
349 fputc(')', stderr);
351 fputc('\n', stderr);
355 static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_width)
357 if (ref->deletion)
358 print_ref_status('-', "[deleted]", ref, NULL, NULL,
359 porcelain, summary_width);
360 else if (is_null_oid(&ref->old_oid))
361 print_ref_status('*',
362 (starts_with(ref->name, "refs/tags/") ? "[new tag]" :
363 "[new branch]"),
364 ref, ref->peer_ref, NULL, porcelain, summary_width);
365 else {
366 struct strbuf quickref = STRBUF_INIT;
367 char type;
368 const char *msg;
370 strbuf_add_unique_abbrev(&quickref, ref->old_oid.hash,
371 DEFAULT_ABBREV);
372 if (ref->forced_update) {
373 strbuf_addstr(&quickref, "...");
374 type = '+';
375 msg = "forced update";
376 } else {
377 strbuf_addstr(&quickref, "..");
378 type = ' ';
379 msg = NULL;
381 strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash,
382 DEFAULT_ABBREV);
384 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
385 porcelain, summary_width);
386 strbuf_release(&quickref);
390 static int print_one_push_status(struct ref *ref, const char *dest, int count,
391 int porcelain, int summary_width)
393 if (!count) {
394 char *url = transport_anonymize_url(dest);
395 fprintf(porcelain ? stdout : stderr, "To %s\n", url);
396 free(url);
399 switch(ref->status) {
400 case REF_STATUS_NONE:
401 print_ref_status('X', "[no match]", ref, NULL, NULL,
402 porcelain, summary_width);
403 break;
404 case REF_STATUS_REJECT_NODELETE:
405 print_ref_status('!', "[rejected]", ref, NULL,
406 "remote does not support deleting refs",
407 porcelain, summary_width);
408 break;
409 case REF_STATUS_UPTODATE:
410 print_ref_status('=', "[up to date]", ref,
411 ref->peer_ref, NULL, porcelain, summary_width);
412 break;
413 case REF_STATUS_REJECT_NONFASTFORWARD:
414 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
415 "non-fast-forward", porcelain, summary_width);
416 break;
417 case REF_STATUS_REJECT_ALREADY_EXISTS:
418 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
419 "already exists", porcelain, summary_width);
420 break;
421 case REF_STATUS_REJECT_FETCH_FIRST:
422 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
423 "fetch first", porcelain, summary_width);
424 break;
425 case REF_STATUS_REJECT_NEEDS_FORCE:
426 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
427 "needs force", porcelain, summary_width);
428 break;
429 case REF_STATUS_REJECT_STALE:
430 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
431 "stale info", porcelain, summary_width);
432 break;
433 case REF_STATUS_REJECT_SHALLOW:
434 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
435 "new shallow roots not allowed",
436 porcelain, summary_width);
437 break;
438 case REF_STATUS_REMOTE_REJECT:
439 print_ref_status('!', "[remote rejected]", ref,
440 ref->deletion ? NULL : ref->peer_ref,
441 ref->remote_status, porcelain, summary_width);
442 break;
443 case REF_STATUS_EXPECTING_REPORT:
444 print_ref_status('!', "[remote failure]", ref,
445 ref->deletion ? NULL : ref->peer_ref,
446 "remote failed to report status",
447 porcelain, summary_width);
448 break;
449 case REF_STATUS_ATOMIC_PUSH_FAILED:
450 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
451 "atomic push failed", porcelain, summary_width);
452 break;
453 case REF_STATUS_OK:
454 print_ok_ref_status(ref, porcelain, summary_width);
455 break;
458 return 1;
461 static int measure_abbrev(const struct object_id *oid, int sofar)
463 char hex[GIT_MAX_HEXSZ + 1];
464 int w = find_unique_abbrev_r(hex, oid->hash, DEFAULT_ABBREV);
466 return (w < sofar) ? sofar : w;
469 int transport_summary_width(const struct ref *refs)
471 int maxw = -1;
473 for (; refs; refs = refs->next) {
474 maxw = measure_abbrev(&refs->old_oid, maxw);
475 maxw = measure_abbrev(&refs->new_oid, maxw);
477 if (maxw < 0)
478 maxw = FALLBACK_DEFAULT_ABBREV;
479 return (2 * maxw + 3);
482 void transport_print_push_status(const char *dest, struct ref *refs,
483 int verbose, int porcelain, unsigned int *reject_reasons)
485 struct ref *ref;
486 int n = 0;
487 char *head;
488 int summary_width = transport_summary_width(refs);
490 head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
492 if (verbose) {
493 for (ref = refs; ref; ref = ref->next)
494 if (ref->status == REF_STATUS_UPTODATE)
495 n += print_one_push_status(ref, dest, n,
496 porcelain, summary_width);
499 for (ref = refs; ref; ref = ref->next)
500 if (ref->status == REF_STATUS_OK)
501 n += print_one_push_status(ref, dest, n,
502 porcelain, summary_width);
504 *reject_reasons = 0;
505 for (ref = refs; ref; ref = ref->next) {
506 if (ref->status != REF_STATUS_NONE &&
507 ref->status != REF_STATUS_UPTODATE &&
508 ref->status != REF_STATUS_OK)
509 n += print_one_push_status(ref, dest, n,
510 porcelain, summary_width);
511 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
512 if (head != NULL && !strcmp(head, ref->name))
513 *reject_reasons |= REJECT_NON_FF_HEAD;
514 else
515 *reject_reasons |= REJECT_NON_FF_OTHER;
516 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
517 *reject_reasons |= REJECT_ALREADY_EXISTS;
518 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
519 *reject_reasons |= REJECT_FETCH_FIRST;
520 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
521 *reject_reasons |= REJECT_NEEDS_FORCE;
524 free(head);
527 void transport_verify_remote_names(int nr_heads, const char **heads)
529 int i;
531 for (i = 0; i < nr_heads; i++) {
532 const char *local = heads[i];
533 const char *remote = strrchr(heads[i], ':');
535 if (*local == '+')
536 local++;
538 /* A matching refspec is okay. */
539 if (remote == local && remote[1] == '\0')
540 continue;
542 remote = remote ? (remote + 1) : local;
543 if (check_refname_format(remote,
544 REFNAME_ALLOW_ONELEVEL|REFNAME_REFSPEC_PATTERN))
545 die("remote part of refspec is not a valid name in %s",
546 heads[i]);
550 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
552 struct git_transport_data *data = transport->data;
553 struct send_pack_args args;
554 int ret;
556 if (!data->got_remote_heads) {
557 struct ref *tmp_refs;
558 connect_setup(transport, 1);
560 get_remote_heads(data->fd[0], NULL, 0, &tmp_refs, REF_NORMAL,
561 NULL, &data->shallow);
562 data->got_remote_heads = 1;
565 memset(&args, 0, sizeof(args));
566 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
567 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
568 args.use_thin_pack = data->options.thin;
569 args.verbose = (transport->verbose > 0);
570 args.quiet = (transport->verbose < 0);
571 args.progress = transport->progress;
572 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
573 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
574 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
575 args.push_options = transport->push_options;
576 args.url = transport->url;
578 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
579 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
580 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
581 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
582 else
583 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
585 ret = send_pack(&args, data->fd, data->conn, remote_refs,
586 &data->extra_have);
588 close(data->fd[1]);
589 close(data->fd[0]);
590 ret |= finish_connect(data->conn);
591 data->conn = NULL;
592 data->got_remote_heads = 0;
594 return ret;
597 static int connect_git(struct transport *transport, const char *name,
598 const char *executable, int fd[2])
600 struct git_transport_data *data = transport->data;
601 data->conn = git_connect(data->fd, transport->url,
602 executable, 0);
603 fd[0] = data->fd[0];
604 fd[1] = data->fd[1];
605 return 0;
608 static int disconnect_git(struct transport *transport)
610 struct git_transport_data *data = transport->data;
611 if (data->conn) {
612 if (data->got_remote_heads)
613 packet_flush(data->fd[1]);
614 close(data->fd[0]);
615 close(data->fd[1]);
616 finish_connect(data->conn);
619 free(data);
620 return 0;
623 static struct transport_vtable taken_over_vtable = {
624 NULL,
625 get_refs_via_connect,
626 fetch_refs_via_pack,
627 git_transport_push,
628 NULL,
629 disconnect_git
632 void transport_take_over(struct transport *transport,
633 struct child_process *child)
635 struct git_transport_data *data;
637 if (!transport->smart_options)
638 die("BUG: taking over transport requires non-NULL "
639 "smart_options field.");
641 data = xcalloc(1, sizeof(*data));
642 data->options = *transport->smart_options;
643 data->conn = child;
644 data->fd[0] = data->conn->out;
645 data->fd[1] = data->conn->in;
646 data->got_remote_heads = 0;
647 transport->data = data;
649 transport->vtable = &taken_over_vtable;
650 transport->smart_options = &(data->options);
652 transport->cannot_reuse = 1;
655 static int is_file(const char *url)
657 struct stat buf;
658 if (stat(url, &buf))
659 return 0;
660 return S_ISREG(buf.st_mode);
663 static int external_specification_len(const char *url)
665 return strchr(url, ':') - url;
668 static const struct string_list *protocol_whitelist(void)
670 static int enabled = -1;
671 static struct string_list allowed = STRING_LIST_INIT_DUP;
673 if (enabled < 0) {
674 const char *v = getenv("GIT_ALLOW_PROTOCOL");
675 if (v) {
676 string_list_split(&allowed, v, ':', -1);
677 string_list_sort(&allowed);
678 enabled = 1;
679 } else {
680 enabled = 0;
684 return enabled ? &allowed : NULL;
687 enum protocol_allow_config {
688 PROTOCOL_ALLOW_NEVER = 0,
689 PROTOCOL_ALLOW_USER_ONLY,
690 PROTOCOL_ALLOW_ALWAYS
693 static enum protocol_allow_config parse_protocol_config(const char *key,
694 const char *value)
696 if (!strcasecmp(value, "always"))
697 return PROTOCOL_ALLOW_ALWAYS;
698 else if (!strcasecmp(value, "never"))
699 return PROTOCOL_ALLOW_NEVER;
700 else if (!strcasecmp(value, "user"))
701 return PROTOCOL_ALLOW_USER_ONLY;
703 die("unknown value for config '%s': %s", key, value);
706 static enum protocol_allow_config get_protocol_config(const char *type)
708 char *key = xstrfmt("protocol.%s.allow", type);
709 char *value;
711 /* first check the per-protocol config */
712 if (!git_config_get_string(key, &value)) {
713 enum protocol_allow_config ret =
714 parse_protocol_config(key, value);
715 free(key);
716 free(value);
717 return ret;
719 free(key);
721 /* if defined, fallback to user-defined default for unknown protocols */
722 if (!git_config_get_string("protocol.allow", &value)) {
723 enum protocol_allow_config ret =
724 parse_protocol_config("protocol.allow", value);
725 free(value);
726 return ret;
729 /* fallback to built-in defaults */
730 /* known safe */
731 if (!strcmp(type, "http") ||
732 !strcmp(type, "https") ||
733 !strcmp(type, "git") ||
734 !strcmp(type, "ssh") ||
735 !strcmp(type, "file"))
736 return PROTOCOL_ALLOW_ALWAYS;
738 /* known scary; err on the side of caution */
739 if (!strcmp(type, "ext"))
740 return PROTOCOL_ALLOW_NEVER;
742 /* unknown; by default let them be used only directly by the user */
743 return PROTOCOL_ALLOW_USER_ONLY;
746 int is_transport_allowed(const char *type, int from_user)
748 const struct string_list *whitelist = protocol_whitelist();
749 if (whitelist)
750 return string_list_has_string(whitelist, type);
752 switch (get_protocol_config(type)) {
753 case PROTOCOL_ALLOW_ALWAYS:
754 return 1;
755 case PROTOCOL_ALLOW_NEVER:
756 return 0;
757 case PROTOCOL_ALLOW_USER_ONLY:
758 if (from_user < 0)
759 from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
760 return from_user;
763 die("BUG: invalid protocol_allow_config type");
766 void transport_check_allowed(const char *type)
768 if (!is_transport_allowed(type, -1))
769 die("transport '%s' not allowed", type);
772 static struct transport_vtable bundle_vtable = {
773 NULL,
774 get_refs_from_bundle,
775 fetch_refs_from_bundle,
776 NULL,
777 NULL,
778 close_bundle
781 static struct transport_vtable builtin_smart_vtable = {
782 NULL,
783 get_refs_via_connect,
784 fetch_refs_via_pack,
785 git_transport_push,
786 connect_git,
787 disconnect_git
790 struct transport *transport_get(struct remote *remote, const char *url)
792 const char *helper;
793 struct transport *ret = xcalloc(1, sizeof(*ret));
795 ret->progress = isatty(2);
797 if (!remote)
798 die("No remote provided to transport_get()");
800 ret->got_remote_refs = 0;
801 ret->remote = remote;
802 helper = remote->foreign_vcs;
804 if (!url && remote->url)
805 url = remote->url[0];
806 ret->url = url;
808 /* maybe it is a foreign URL? */
809 if (url) {
810 const char *p = url;
812 while (is_urlschemechar(p == url, *p))
813 p++;
814 if (starts_with(p, "::"))
815 helper = xstrndup(url, p - url);
818 if (helper) {
819 transport_helper_init(ret, helper);
820 } else if (starts_with(url, "rsync:")) {
821 die("git-over-rsync is no longer supported");
822 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
823 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
824 transport_check_allowed("file");
825 ret->data = data;
826 ret->vtable = &bundle_vtable;
827 ret->smart_options = NULL;
828 } else if (!is_url(url)
829 || starts_with(url, "file://")
830 || starts_with(url, "git://")
831 || starts_with(url, "ssh://")
832 || starts_with(url, "git+ssh://") /* deprecated - do not use */
833 || starts_with(url, "ssh+git://") /* deprecated - do not use */
836 * These are builtin smart transports; "allowed" transports
837 * will be checked individually in git_connect.
839 struct git_transport_data *data = xcalloc(1, sizeof(*data));
840 ret->data = data;
841 ret->vtable = &builtin_smart_vtable;
842 ret->smart_options = &(data->options);
844 data->conn = NULL;
845 data->got_remote_heads = 0;
846 } else {
847 /* Unknown protocol in URL. Pass to external handler. */
848 int len = external_specification_len(url);
849 char *handler = xmemdupz(url, len);
850 transport_helper_init(ret, handler);
853 if (ret->smart_options) {
854 ret->smart_options->thin = 1;
855 ret->smart_options->uploadpack = "git-upload-pack";
856 if (remote->uploadpack)
857 ret->smart_options->uploadpack = remote->uploadpack;
858 ret->smart_options->receivepack = "git-receive-pack";
859 if (remote->receivepack)
860 ret->smart_options->receivepack = remote->receivepack;
863 return ret;
866 int transport_set_option(struct transport *transport,
867 const char *name, const char *value)
869 int git_reports = 1, protocol_reports = 1;
871 if (transport->smart_options)
872 git_reports = set_git_option(transport->smart_options,
873 name, value);
875 if (transport->vtable->set_option)
876 protocol_reports = transport->vtable->set_option(transport,
877 name, value);
879 /* If either report is 0, report 0 (success). */
880 if (!git_reports || !protocol_reports)
881 return 0;
882 /* If either reports -1 (invalid value), report -1. */
883 if ((git_reports == -1) || (protocol_reports == -1))
884 return -1;
885 /* Otherwise if both report unknown, report unknown. */
886 return 1;
889 void transport_set_verbosity(struct transport *transport, int verbosity,
890 int force_progress)
892 if (verbosity >= 1)
893 transport->verbose = verbosity <= 3 ? verbosity : 3;
894 if (verbosity < 0)
895 transport->verbose = -1;
898 * Rules used to determine whether to report progress (processing aborts
899 * when a rule is satisfied):
901 * . Report progress, if force_progress is 1 (ie. --progress).
902 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
903 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
904 * . Report progress if isatty(2) is 1.
906 if (force_progress >= 0)
907 transport->progress = !!force_progress;
908 else
909 transport->progress = verbosity >= 0 && isatty(2);
912 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
914 int i;
916 fprintf(stderr, _("The following submodule paths contain changes that can\n"
917 "not be found on any remote:\n"));
918 for (i = 0; i < needs_pushing->nr; i++)
919 fprintf(stderr, " %s\n", needs_pushing->items[i].string);
920 fprintf(stderr, _("\nPlease try\n\n"
921 " git push --recurse-submodules=on-demand\n\n"
922 "or cd to the path and use\n\n"
923 " git push\n\n"
924 "to push them to a remote.\n\n"));
926 string_list_clear(needs_pushing, 0);
928 die(_("Aborting."));
931 static int run_pre_push_hook(struct transport *transport,
932 struct ref *remote_refs)
934 int ret = 0, x;
935 struct ref *r;
936 struct child_process proc = CHILD_PROCESS_INIT;
937 struct strbuf buf;
938 const char *argv[4];
940 if (!(argv[0] = find_hook("pre-push")))
941 return 0;
943 argv[1] = transport->remote->name;
944 argv[2] = transport->url;
945 argv[3] = NULL;
947 proc.argv = argv;
948 proc.in = -1;
950 if (start_command(&proc)) {
951 finish_command(&proc);
952 return -1;
955 sigchain_push(SIGPIPE, SIG_IGN);
957 strbuf_init(&buf, 256);
959 for (r = remote_refs; r; r = r->next) {
960 if (!r->peer_ref) continue;
961 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
962 if (r->status == REF_STATUS_REJECT_STALE) continue;
963 if (r->status == REF_STATUS_UPTODATE) continue;
965 strbuf_reset(&buf);
966 strbuf_addf( &buf, "%s %s %s %s\n",
967 r->peer_ref->name, oid_to_hex(&r->new_oid),
968 r->name, oid_to_hex(&r->old_oid));
970 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
971 /* We do not mind if a hook does not read all refs. */
972 if (errno != EPIPE)
973 ret = -1;
974 break;
978 strbuf_release(&buf);
980 x = close(proc.in);
981 if (!ret)
982 ret = x;
984 sigchain_pop(SIGPIPE);
986 x = finish_command(&proc);
987 if (!ret)
988 ret = x;
990 return ret;
993 int transport_push(struct transport *transport,
994 int refspec_nr, const char **refspec, int flags,
995 unsigned int *reject_reasons)
997 *reject_reasons = 0;
998 transport_verify_remote_names(refspec_nr, refspec);
1000 if (transport->vtable->push_refs) {
1001 struct ref *remote_refs;
1002 struct ref *local_refs = get_local_heads();
1003 int match_flags = MATCH_REFS_NONE;
1004 int verbose = (transport->verbose > 0);
1005 int quiet = (transport->verbose < 0);
1006 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1007 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1008 int push_ret, ret, err;
1010 if (check_push_refs(local_refs, refspec_nr, refspec) < 0)
1011 return -1;
1013 remote_refs = transport->vtable->get_refs_list(transport, 1);
1015 if (flags & TRANSPORT_PUSH_ALL)
1016 match_flags |= MATCH_REFS_ALL;
1017 if (flags & TRANSPORT_PUSH_MIRROR)
1018 match_flags |= MATCH_REFS_MIRROR;
1019 if (flags & TRANSPORT_PUSH_PRUNE)
1020 match_flags |= MATCH_REFS_PRUNE;
1021 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1022 match_flags |= MATCH_REFS_FOLLOW_TAGS;
1024 if (match_push_refs(local_refs, &remote_refs,
1025 refspec_nr, refspec, match_flags)) {
1026 return -1;
1029 if (transport->smart_options &&
1030 transport->smart_options->cas &&
1031 !is_empty_cas(transport->smart_options->cas))
1032 apply_push_cas(transport->smart_options->cas,
1033 transport->remote, remote_refs);
1035 set_ref_status_for_push(remote_refs,
1036 flags & TRANSPORT_PUSH_MIRROR,
1037 flags & TRANSPORT_PUSH_FORCE);
1039 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1040 if (run_pre_push_hook(transport, remote_refs))
1041 return -1;
1043 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1044 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1045 !is_bare_repository()) {
1046 struct ref *ref = remote_refs;
1047 struct oid_array commits = OID_ARRAY_INIT;
1049 for (; ref; ref = ref->next)
1050 if (!is_null_oid(&ref->new_oid))
1051 oid_array_append(&commits,
1052 &ref->new_oid);
1054 if (!push_unpushed_submodules(&commits,
1055 transport->remote,
1056 refspec, refspec_nr,
1057 transport->push_options,
1058 pretend)) {
1059 oid_array_clear(&commits);
1060 die("Failed to push all needed submodules!");
1062 oid_array_clear(&commits);
1065 if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1066 ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1067 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1068 !pretend)) && !is_bare_repository()) {
1069 struct ref *ref = remote_refs;
1070 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1071 struct oid_array commits = OID_ARRAY_INIT;
1073 for (; ref; ref = ref->next)
1074 if (!is_null_oid(&ref->new_oid))
1075 oid_array_append(&commits,
1076 &ref->new_oid);
1078 if (find_unpushed_submodules(&commits, transport->remote->name,
1079 &needs_pushing)) {
1080 oid_array_clear(&commits);
1081 die_with_unpushed_submodules(&needs_pushing);
1083 string_list_clear(&needs_pushing, 0);
1084 oid_array_clear(&commits);
1087 if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY))
1088 push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1089 else
1090 push_ret = 0;
1091 err = push_had_errors(remote_refs);
1092 ret = push_ret | err;
1094 if (!quiet || err)
1095 transport_print_push_status(transport->url, remote_refs,
1096 verbose | porcelain, porcelain,
1097 reject_reasons);
1099 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1100 set_upstreams(transport, remote_refs, pretend);
1102 if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1103 TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1104 struct ref *ref;
1105 for (ref = remote_refs; ref; ref = ref->next)
1106 transport_update_tracking_ref(transport->remote, ref, verbose);
1109 if (porcelain && !push_ret)
1110 puts("Done");
1111 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1112 fprintf(stderr, "Everything up-to-date\n");
1114 return ret;
1116 return 1;
1119 const struct ref *transport_get_remote_refs(struct transport *transport)
1121 if (!transport->got_remote_refs) {
1122 transport->remote_refs = transport->vtable->get_refs_list(transport, 0);
1123 transport->got_remote_refs = 1;
1126 return transport->remote_refs;
1129 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1131 int rc;
1132 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1133 struct ref **heads = NULL;
1134 struct ref *rm;
1136 for (rm = refs; rm; rm = rm->next) {
1137 nr_refs++;
1138 if (rm->peer_ref &&
1139 !is_null_oid(&rm->old_oid) &&
1140 !oidcmp(&rm->peer_ref->old_oid, &rm->old_oid))
1141 continue;
1142 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1143 heads[nr_heads++] = rm;
1146 if (!nr_heads) {
1148 * When deepening of a shallow repository is requested,
1149 * then local and remote refs are likely to still be equal.
1150 * Just feed them all to the fetch method in that case.
1151 * This condition shouldn't be met in a non-deepening fetch
1152 * (see builtin/fetch.c:quickfetch()).
1154 ALLOC_ARRAY(heads, nr_refs);
1155 for (rm = refs; rm; rm = rm->next)
1156 heads[nr_heads++] = rm;
1159 rc = transport->vtable->fetch(transport, nr_heads, heads);
1161 free(heads);
1162 return rc;
1165 void transport_unlock_pack(struct transport *transport)
1167 if (transport->pack_lockfile) {
1168 unlink_or_warn(transport->pack_lockfile);
1169 FREE_AND_NULL(transport->pack_lockfile);
1173 int transport_connect(struct transport *transport, const char *name,
1174 const char *exec, int fd[2])
1176 if (transport->vtable->connect)
1177 return transport->vtable->connect(transport, name, exec, fd);
1178 else
1179 die("Operation not supported by protocol");
1182 int transport_disconnect(struct transport *transport)
1184 int ret = 0;
1185 if (transport->vtable->disconnect)
1186 ret = transport->vtable->disconnect(transport);
1187 free(transport);
1188 return ret;
1192 * Strip username (and password) from a URL and return
1193 * it in a newly allocated string.
1195 char *transport_anonymize_url(const char *url)
1197 char *scheme_prefix, *anon_part;
1198 size_t anon_len, prefix_len = 0;
1200 anon_part = strchr(url, '@');
1201 if (url_is_local_not_ssh(url) || !anon_part)
1202 goto literal_copy;
1204 anon_len = strlen(++anon_part);
1205 scheme_prefix = strstr(url, "://");
1206 if (!scheme_prefix) {
1207 if (!strchr(anon_part, ':'))
1208 /* cannot be "me@there:/path/name" */
1209 goto literal_copy;
1210 } else {
1211 const char *cp;
1212 /* make sure scheme is reasonable */
1213 for (cp = url; cp < scheme_prefix; cp++) {
1214 switch (*cp) {
1215 /* RFC 1738 2.1 */
1216 case '+': case '.': case '-':
1217 break; /* ok */
1218 default:
1219 if (isalnum(*cp))
1220 break;
1221 /* it isn't */
1222 goto literal_copy;
1225 /* @ past the first slash does not count */
1226 cp = strchr(scheme_prefix + 3, '/');
1227 if (cp && cp < anon_part)
1228 goto literal_copy;
1229 prefix_len = scheme_prefix - url + 3;
1231 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1232 (int)anon_len, anon_part);
1233 literal_copy:
1234 return xstrdup(url);
1237 static void read_alternate_refs(const char *path,
1238 alternate_ref_fn *cb,
1239 void *data)
1241 struct child_process cmd = CHILD_PROCESS_INIT;
1242 struct strbuf line = STRBUF_INIT;
1243 FILE *fh;
1245 cmd.git_cmd = 1;
1246 argv_array_pushf(&cmd.args, "--git-dir=%s", path);
1247 argv_array_push(&cmd.args, "for-each-ref");
1248 argv_array_push(&cmd.args, "--format=%(objectname) %(refname)");
1249 cmd.env = local_repo_env;
1250 cmd.out = -1;
1252 if (start_command(&cmd))
1253 return;
1255 fh = xfdopen(cmd.out, "r");
1256 while (strbuf_getline_lf(&line, fh) != EOF) {
1257 struct object_id oid;
1259 if (get_oid_hex(line.buf, &oid) ||
1260 line.buf[GIT_SHA1_HEXSZ] != ' ') {
1261 warning("invalid line while parsing alternate refs: %s",
1262 line.buf);
1263 break;
1266 cb(line.buf + GIT_SHA1_HEXSZ + 1, &oid, data);
1269 fclose(fh);
1270 finish_command(&cmd);
1273 struct alternate_refs_data {
1274 alternate_ref_fn *fn;
1275 void *data;
1278 static int refs_from_alternate_cb(struct alternate_object_database *e,
1279 void *data)
1281 struct strbuf path = STRBUF_INIT;
1282 size_t base_len;
1283 struct alternate_refs_data *cb = data;
1285 if (!strbuf_realpath(&path, e->path, 0))
1286 goto out;
1287 if (!strbuf_strip_suffix(&path, "/objects"))
1288 goto out;
1289 base_len = path.len;
1291 /* Is this a git repository with refs? */
1292 strbuf_addstr(&path, "/refs");
1293 if (!is_directory(path.buf))
1294 goto out;
1295 strbuf_setlen(&path, base_len);
1297 read_alternate_refs(path.buf, cb->fn, cb->data);
1299 out:
1300 strbuf_release(&path);
1301 return 0;
1304 void for_each_alternate_ref(alternate_ref_fn fn, void *data)
1306 struct alternate_refs_data cb;
1307 cb.fn = fn;
1308 cb.data = data;
1309 foreach_alt_odb(refs_from_alternate_cb, &cb);