Merge branch 'jh/fsck-promisors'
[git.git] / transport.c
blobe82db773fd2383a998dff45b2b06eed668b5bebc
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;
171 return 1;
174 static int connect_setup(struct transport *transport, int for_push)
176 struct git_transport_data *data = transport->data;
177 int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
179 if (data->conn)
180 return 0;
182 switch (transport->family) {
183 case TRANSPORT_FAMILY_ALL: break;
184 case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
185 case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
188 data->conn = git_connect(data->fd, transport->url,
189 for_push ? data->options.receivepack :
190 data->options.uploadpack,
191 flags);
193 return 0;
196 static struct ref *get_refs_via_connect(struct transport *transport, int for_push)
198 struct git_transport_data *data = transport->data;
199 struct ref *refs;
201 connect_setup(transport, for_push);
202 get_remote_heads(data->fd[0], NULL, 0, &refs,
203 for_push ? REF_NORMAL : 0,
204 &data->extra_have,
205 &data->shallow);
206 data->got_remote_heads = 1;
208 return refs;
211 static int fetch_refs_via_pack(struct transport *transport,
212 int nr_heads, struct ref **to_fetch)
214 int ret = 0;
215 struct git_transport_data *data = transport->data;
216 struct ref *refs;
217 char *dest = xstrdup(transport->url);
218 struct fetch_pack_args args;
219 struct ref *refs_tmp = NULL;
221 memset(&args, 0, sizeof(args));
222 args.uploadpack = data->options.uploadpack;
223 args.keep_pack = data->options.keep;
224 args.lock_pack = 1;
225 args.use_thin_pack = data->options.thin;
226 args.include_tag = data->options.followtags;
227 args.verbose = (transport->verbose > 1);
228 args.quiet = (transport->verbose < 0);
229 args.no_progress = !transport->progress;
230 args.depth = data->options.depth;
231 args.deepen_since = data->options.deepen_since;
232 args.deepen_not = data->options.deepen_not;
233 args.deepen_relative = data->options.deepen_relative;
234 args.check_self_contained_and_connected =
235 data->options.check_self_contained_and_connected;
236 args.cloning = transport->cloning;
237 args.update_shallow = data->options.update_shallow;
238 args.from_promisor = data->options.from_promisor;
239 args.no_dependents = data->options.no_dependents;
241 if (!data->got_remote_heads) {
242 connect_setup(transport, 0);
243 get_remote_heads(data->fd[0], NULL, 0, &refs_tmp, 0,
244 NULL, &data->shallow);
245 data->got_remote_heads = 1;
248 refs = fetch_pack(&args, data->fd, data->conn,
249 refs_tmp ? refs_tmp : transport->remote_refs,
250 dest, to_fetch, nr_heads, &data->shallow,
251 &transport->pack_lockfile);
252 close(data->fd[0]);
253 close(data->fd[1]);
254 if (finish_connect(data->conn))
255 ret = -1;
256 data->conn = NULL;
257 data->got_remote_heads = 0;
258 data->options.self_contained_and_connected =
259 args.self_contained_and_connected;
261 if (refs == NULL)
262 ret = -1;
263 if (report_unmatched_refs(to_fetch, nr_heads))
264 ret = -1;
266 free_refs(refs_tmp);
267 free_refs(refs);
268 free(dest);
269 return ret;
272 static int push_had_errors(struct ref *ref)
274 for (; ref; ref = ref->next) {
275 switch (ref->status) {
276 case REF_STATUS_NONE:
277 case REF_STATUS_UPTODATE:
278 case REF_STATUS_OK:
279 break;
280 default:
281 return 1;
284 return 0;
287 int transport_refs_pushed(struct ref *ref)
289 for (; ref; ref = ref->next) {
290 switch(ref->status) {
291 case REF_STATUS_NONE:
292 case REF_STATUS_UPTODATE:
293 break;
294 default:
295 return 1;
298 return 0;
301 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
303 struct refspec rs;
305 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
306 return;
308 rs.src = ref->name;
309 rs.dst = NULL;
311 if (!remote_find_tracking(remote, &rs)) {
312 if (verbose)
313 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
314 if (ref->deletion) {
315 delete_ref(NULL, rs.dst, NULL, 0);
316 } else
317 update_ref("update by push", rs.dst, &ref->new_oid,
318 NULL, 0, 0);
319 free(rs.dst);
323 static void print_ref_status(char flag, const char *summary,
324 struct ref *to, struct ref *from, const char *msg,
325 int porcelain, int summary_width)
327 if (porcelain) {
328 if (from)
329 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
330 else
331 fprintf(stdout, "%c\t:%s\t", flag, to->name);
332 if (msg)
333 fprintf(stdout, "%s (%s)\n", summary, msg);
334 else
335 fprintf(stdout, "%s\n", summary);
336 } else {
337 fprintf(stderr, " %c %-*s ", flag, summary_width, summary);
338 if (from)
339 fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
340 else
341 fputs(prettify_refname(to->name), stderr);
342 if (msg) {
343 fputs(" (", stderr);
344 fputs(msg, stderr);
345 fputc(')', stderr);
347 fputc('\n', stderr);
351 static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_width)
353 if (ref->deletion)
354 print_ref_status('-', "[deleted]", ref, NULL, NULL,
355 porcelain, summary_width);
356 else if (is_null_oid(&ref->old_oid))
357 print_ref_status('*',
358 (starts_with(ref->name, "refs/tags/") ? "[new tag]" :
359 "[new branch]"),
360 ref, ref->peer_ref, NULL, porcelain, summary_width);
361 else {
362 struct strbuf quickref = STRBUF_INIT;
363 char type;
364 const char *msg;
366 strbuf_add_unique_abbrev(&quickref, ref->old_oid.hash,
367 DEFAULT_ABBREV);
368 if (ref->forced_update) {
369 strbuf_addstr(&quickref, "...");
370 type = '+';
371 msg = "forced update";
372 } else {
373 strbuf_addstr(&quickref, "..");
374 type = ' ';
375 msg = NULL;
377 strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash,
378 DEFAULT_ABBREV);
380 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
381 porcelain, summary_width);
382 strbuf_release(&quickref);
386 static int print_one_push_status(struct ref *ref, const char *dest, int count,
387 int porcelain, int summary_width)
389 if (!count) {
390 char *url = transport_anonymize_url(dest);
391 fprintf(porcelain ? stdout : stderr, "To %s\n", url);
392 free(url);
395 switch(ref->status) {
396 case REF_STATUS_NONE:
397 print_ref_status('X', "[no match]", ref, NULL, NULL,
398 porcelain, summary_width);
399 break;
400 case REF_STATUS_REJECT_NODELETE:
401 print_ref_status('!', "[rejected]", ref, NULL,
402 "remote does not support deleting refs",
403 porcelain, summary_width);
404 break;
405 case REF_STATUS_UPTODATE:
406 print_ref_status('=', "[up to date]", ref,
407 ref->peer_ref, NULL, porcelain, summary_width);
408 break;
409 case REF_STATUS_REJECT_NONFASTFORWARD:
410 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
411 "non-fast-forward", porcelain, summary_width);
412 break;
413 case REF_STATUS_REJECT_ALREADY_EXISTS:
414 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
415 "already exists", porcelain, summary_width);
416 break;
417 case REF_STATUS_REJECT_FETCH_FIRST:
418 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
419 "fetch first", porcelain, summary_width);
420 break;
421 case REF_STATUS_REJECT_NEEDS_FORCE:
422 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
423 "needs force", porcelain, summary_width);
424 break;
425 case REF_STATUS_REJECT_STALE:
426 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
427 "stale info", porcelain, summary_width);
428 break;
429 case REF_STATUS_REJECT_SHALLOW:
430 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
431 "new shallow roots not allowed",
432 porcelain, summary_width);
433 break;
434 case REF_STATUS_REMOTE_REJECT:
435 print_ref_status('!', "[remote rejected]", ref,
436 ref->deletion ? NULL : ref->peer_ref,
437 ref->remote_status, porcelain, summary_width);
438 break;
439 case REF_STATUS_EXPECTING_REPORT:
440 print_ref_status('!', "[remote failure]", ref,
441 ref->deletion ? NULL : ref->peer_ref,
442 "remote failed to report status",
443 porcelain, summary_width);
444 break;
445 case REF_STATUS_ATOMIC_PUSH_FAILED:
446 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
447 "atomic push failed", porcelain, summary_width);
448 break;
449 case REF_STATUS_OK:
450 print_ok_ref_status(ref, porcelain, summary_width);
451 break;
454 return 1;
457 static int measure_abbrev(const struct object_id *oid, int sofar)
459 char hex[GIT_MAX_HEXSZ + 1];
460 int w = find_unique_abbrev_r(hex, oid->hash, DEFAULT_ABBREV);
462 return (w < sofar) ? sofar : w;
465 int transport_summary_width(const struct ref *refs)
467 int maxw = -1;
469 for (; refs; refs = refs->next) {
470 maxw = measure_abbrev(&refs->old_oid, maxw);
471 maxw = measure_abbrev(&refs->new_oid, maxw);
473 if (maxw < 0)
474 maxw = FALLBACK_DEFAULT_ABBREV;
475 return (2 * maxw + 3);
478 void transport_print_push_status(const char *dest, struct ref *refs,
479 int verbose, int porcelain, unsigned int *reject_reasons)
481 struct ref *ref;
482 int n = 0;
483 char *head;
484 int summary_width = transport_summary_width(refs);
486 head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
488 if (verbose) {
489 for (ref = refs; ref; ref = ref->next)
490 if (ref->status == REF_STATUS_UPTODATE)
491 n += print_one_push_status(ref, dest, n,
492 porcelain, summary_width);
495 for (ref = refs; ref; ref = ref->next)
496 if (ref->status == REF_STATUS_OK)
497 n += print_one_push_status(ref, dest, n,
498 porcelain, summary_width);
500 *reject_reasons = 0;
501 for (ref = refs; ref; ref = ref->next) {
502 if (ref->status != REF_STATUS_NONE &&
503 ref->status != REF_STATUS_UPTODATE &&
504 ref->status != REF_STATUS_OK)
505 n += print_one_push_status(ref, dest, n,
506 porcelain, summary_width);
507 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
508 if (head != NULL && !strcmp(head, ref->name))
509 *reject_reasons |= REJECT_NON_FF_HEAD;
510 else
511 *reject_reasons |= REJECT_NON_FF_OTHER;
512 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
513 *reject_reasons |= REJECT_ALREADY_EXISTS;
514 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
515 *reject_reasons |= REJECT_FETCH_FIRST;
516 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
517 *reject_reasons |= REJECT_NEEDS_FORCE;
520 free(head);
523 void transport_verify_remote_names(int nr_heads, const char **heads)
525 int i;
527 for (i = 0; i < nr_heads; i++) {
528 const char *local = heads[i];
529 const char *remote = strrchr(heads[i], ':');
531 if (*local == '+')
532 local++;
534 /* A matching refspec is okay. */
535 if (remote == local && remote[1] == '\0')
536 continue;
538 remote = remote ? (remote + 1) : local;
539 if (check_refname_format(remote,
540 REFNAME_ALLOW_ONELEVEL|REFNAME_REFSPEC_PATTERN))
541 die("remote part of refspec is not a valid name in %s",
542 heads[i]);
546 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
548 struct git_transport_data *data = transport->data;
549 struct send_pack_args args;
550 int ret;
552 if (!data->got_remote_heads) {
553 struct ref *tmp_refs;
554 connect_setup(transport, 1);
556 get_remote_heads(data->fd[0], NULL, 0, &tmp_refs, REF_NORMAL,
557 NULL, &data->shallow);
558 data->got_remote_heads = 1;
561 memset(&args, 0, sizeof(args));
562 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
563 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
564 args.use_thin_pack = data->options.thin;
565 args.verbose = (transport->verbose > 0);
566 args.quiet = (transport->verbose < 0);
567 args.progress = transport->progress;
568 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
569 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
570 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
571 args.push_options = transport->push_options;
572 args.url = transport->url;
574 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
575 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
576 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
577 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
578 else
579 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
581 ret = send_pack(&args, data->fd, data->conn, remote_refs,
582 &data->extra_have);
584 close(data->fd[1]);
585 close(data->fd[0]);
586 ret |= finish_connect(data->conn);
587 data->conn = NULL;
588 data->got_remote_heads = 0;
590 return ret;
593 static int connect_git(struct transport *transport, const char *name,
594 const char *executable, int fd[2])
596 struct git_transport_data *data = transport->data;
597 data->conn = git_connect(data->fd, transport->url,
598 executable, 0);
599 fd[0] = data->fd[0];
600 fd[1] = data->fd[1];
601 return 0;
604 static int disconnect_git(struct transport *transport)
606 struct git_transport_data *data = transport->data;
607 if (data->conn) {
608 if (data->got_remote_heads)
609 packet_flush(data->fd[1]);
610 close(data->fd[0]);
611 close(data->fd[1]);
612 finish_connect(data->conn);
615 free(data);
616 return 0;
619 static struct transport_vtable taken_over_vtable = {
620 NULL,
621 get_refs_via_connect,
622 fetch_refs_via_pack,
623 git_transport_push,
624 NULL,
625 disconnect_git
628 void transport_take_over(struct transport *transport,
629 struct child_process *child)
631 struct git_transport_data *data;
633 if (!transport->smart_options)
634 die("BUG: taking over transport requires non-NULL "
635 "smart_options field.");
637 data = xcalloc(1, sizeof(*data));
638 data->options = *transport->smart_options;
639 data->conn = child;
640 data->fd[0] = data->conn->out;
641 data->fd[1] = data->conn->in;
642 data->got_remote_heads = 0;
643 transport->data = data;
645 transport->vtable = &taken_over_vtable;
646 transport->smart_options = &(data->options);
648 transport->cannot_reuse = 1;
651 static int is_file(const char *url)
653 struct stat buf;
654 if (stat(url, &buf))
655 return 0;
656 return S_ISREG(buf.st_mode);
659 static int external_specification_len(const char *url)
661 return strchr(url, ':') - url;
664 static const struct string_list *protocol_whitelist(void)
666 static int enabled = -1;
667 static struct string_list allowed = STRING_LIST_INIT_DUP;
669 if (enabled < 0) {
670 const char *v = getenv("GIT_ALLOW_PROTOCOL");
671 if (v) {
672 string_list_split(&allowed, v, ':', -1);
673 string_list_sort(&allowed);
674 enabled = 1;
675 } else {
676 enabled = 0;
680 return enabled ? &allowed : NULL;
683 enum protocol_allow_config {
684 PROTOCOL_ALLOW_NEVER = 0,
685 PROTOCOL_ALLOW_USER_ONLY,
686 PROTOCOL_ALLOW_ALWAYS
689 static enum protocol_allow_config parse_protocol_config(const char *key,
690 const char *value)
692 if (!strcasecmp(value, "always"))
693 return PROTOCOL_ALLOW_ALWAYS;
694 else if (!strcasecmp(value, "never"))
695 return PROTOCOL_ALLOW_NEVER;
696 else if (!strcasecmp(value, "user"))
697 return PROTOCOL_ALLOW_USER_ONLY;
699 die("unknown value for config '%s': %s", key, value);
702 static enum protocol_allow_config get_protocol_config(const char *type)
704 char *key = xstrfmt("protocol.%s.allow", type);
705 char *value;
707 /* first check the per-protocol config */
708 if (!git_config_get_string(key, &value)) {
709 enum protocol_allow_config ret =
710 parse_protocol_config(key, value);
711 free(key);
712 free(value);
713 return ret;
715 free(key);
717 /* if defined, fallback to user-defined default for unknown protocols */
718 if (!git_config_get_string("protocol.allow", &value)) {
719 enum protocol_allow_config ret =
720 parse_protocol_config("protocol.allow", value);
721 free(value);
722 return ret;
725 /* fallback to built-in defaults */
726 /* known safe */
727 if (!strcmp(type, "http") ||
728 !strcmp(type, "https") ||
729 !strcmp(type, "git") ||
730 !strcmp(type, "ssh") ||
731 !strcmp(type, "file"))
732 return PROTOCOL_ALLOW_ALWAYS;
734 /* known scary; err on the side of caution */
735 if (!strcmp(type, "ext"))
736 return PROTOCOL_ALLOW_NEVER;
738 /* unknown; by default let them be used only directly by the user */
739 return PROTOCOL_ALLOW_USER_ONLY;
742 int is_transport_allowed(const char *type, int from_user)
744 const struct string_list *whitelist = protocol_whitelist();
745 if (whitelist)
746 return string_list_has_string(whitelist, type);
748 switch (get_protocol_config(type)) {
749 case PROTOCOL_ALLOW_ALWAYS:
750 return 1;
751 case PROTOCOL_ALLOW_NEVER:
752 return 0;
753 case PROTOCOL_ALLOW_USER_ONLY:
754 if (from_user < 0)
755 from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
756 return from_user;
759 die("BUG: invalid protocol_allow_config type");
762 void transport_check_allowed(const char *type)
764 if (!is_transport_allowed(type, -1))
765 die("transport '%s' not allowed", type);
768 static struct transport_vtable bundle_vtable = {
769 NULL,
770 get_refs_from_bundle,
771 fetch_refs_from_bundle,
772 NULL,
773 NULL,
774 close_bundle
777 static struct transport_vtable builtin_smart_vtable = {
778 NULL,
779 get_refs_via_connect,
780 fetch_refs_via_pack,
781 git_transport_push,
782 connect_git,
783 disconnect_git
786 struct transport *transport_get(struct remote *remote, const char *url)
788 const char *helper;
789 struct transport *ret = xcalloc(1, sizeof(*ret));
791 ret->progress = isatty(2);
793 if (!remote)
794 die("No remote provided to transport_get()");
796 ret->got_remote_refs = 0;
797 ret->remote = remote;
798 helper = remote->foreign_vcs;
800 if (!url && remote->url)
801 url = remote->url[0];
802 ret->url = url;
804 /* maybe it is a foreign URL? */
805 if (url) {
806 const char *p = url;
808 while (is_urlschemechar(p == url, *p))
809 p++;
810 if (starts_with(p, "::"))
811 helper = xstrndup(url, p - url);
814 if (helper) {
815 transport_helper_init(ret, helper);
816 } else if (starts_with(url, "rsync:")) {
817 die("git-over-rsync is no longer supported");
818 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
819 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
820 transport_check_allowed("file");
821 ret->data = data;
822 ret->vtable = &bundle_vtable;
823 ret->smart_options = NULL;
824 } else if (!is_url(url)
825 || starts_with(url, "file://")
826 || starts_with(url, "git://")
827 || starts_with(url, "ssh://")
828 || starts_with(url, "git+ssh://") /* deprecated - do not use */
829 || starts_with(url, "ssh+git://") /* deprecated - do not use */
832 * These are builtin smart transports; "allowed" transports
833 * will be checked individually in git_connect.
835 struct git_transport_data *data = xcalloc(1, sizeof(*data));
836 ret->data = data;
837 ret->vtable = &builtin_smart_vtable;
838 ret->smart_options = &(data->options);
840 data->conn = NULL;
841 data->got_remote_heads = 0;
842 } else {
843 /* Unknown protocol in URL. Pass to external handler. */
844 int len = external_specification_len(url);
845 char *handler = xmemdupz(url, len);
846 transport_helper_init(ret, handler);
849 if (ret->smart_options) {
850 ret->smart_options->thin = 1;
851 ret->smart_options->uploadpack = "git-upload-pack";
852 if (remote->uploadpack)
853 ret->smart_options->uploadpack = remote->uploadpack;
854 ret->smart_options->receivepack = "git-receive-pack";
855 if (remote->receivepack)
856 ret->smart_options->receivepack = remote->receivepack;
859 return ret;
862 int transport_set_option(struct transport *transport,
863 const char *name, const char *value)
865 int git_reports = 1, protocol_reports = 1;
867 if (transport->smart_options)
868 git_reports = set_git_option(transport->smart_options,
869 name, value);
871 if (transport->vtable->set_option)
872 protocol_reports = transport->vtable->set_option(transport,
873 name, value);
875 /* If either report is 0, report 0 (success). */
876 if (!git_reports || !protocol_reports)
877 return 0;
878 /* If either reports -1 (invalid value), report -1. */
879 if ((git_reports == -1) || (protocol_reports == -1))
880 return -1;
881 /* Otherwise if both report unknown, report unknown. */
882 return 1;
885 void transport_set_verbosity(struct transport *transport, int verbosity,
886 int force_progress)
888 if (verbosity >= 1)
889 transport->verbose = verbosity <= 3 ? verbosity : 3;
890 if (verbosity < 0)
891 transport->verbose = -1;
894 * Rules used to determine whether to report progress (processing aborts
895 * when a rule is satisfied):
897 * . Report progress, if force_progress is 1 (ie. --progress).
898 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
899 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
900 * . Report progress if isatty(2) is 1.
902 if (force_progress >= 0)
903 transport->progress = !!force_progress;
904 else
905 transport->progress = verbosity >= 0 && isatty(2);
908 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
910 int i;
912 fprintf(stderr, _("The following submodule paths contain changes that can\n"
913 "not be found on any remote:\n"));
914 for (i = 0; i < needs_pushing->nr; i++)
915 fprintf(stderr, " %s\n", needs_pushing->items[i].string);
916 fprintf(stderr, _("\nPlease try\n\n"
917 " git push --recurse-submodules=on-demand\n\n"
918 "or cd to the path and use\n\n"
919 " git push\n\n"
920 "to push them to a remote.\n\n"));
922 string_list_clear(needs_pushing, 0);
924 die(_("Aborting."));
927 static int run_pre_push_hook(struct transport *transport,
928 struct ref *remote_refs)
930 int ret = 0, x;
931 struct ref *r;
932 struct child_process proc = CHILD_PROCESS_INIT;
933 struct strbuf buf;
934 const char *argv[4];
936 if (!(argv[0] = find_hook("pre-push")))
937 return 0;
939 argv[1] = transport->remote->name;
940 argv[2] = transport->url;
941 argv[3] = NULL;
943 proc.argv = argv;
944 proc.in = -1;
946 if (start_command(&proc)) {
947 finish_command(&proc);
948 return -1;
951 sigchain_push(SIGPIPE, SIG_IGN);
953 strbuf_init(&buf, 256);
955 for (r = remote_refs; r; r = r->next) {
956 if (!r->peer_ref) continue;
957 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
958 if (r->status == REF_STATUS_REJECT_STALE) continue;
959 if (r->status == REF_STATUS_UPTODATE) continue;
961 strbuf_reset(&buf);
962 strbuf_addf( &buf, "%s %s %s %s\n",
963 r->peer_ref->name, oid_to_hex(&r->new_oid),
964 r->name, oid_to_hex(&r->old_oid));
966 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
967 /* We do not mind if a hook does not read all refs. */
968 if (errno != EPIPE)
969 ret = -1;
970 break;
974 strbuf_release(&buf);
976 x = close(proc.in);
977 if (!ret)
978 ret = x;
980 sigchain_pop(SIGPIPE);
982 x = finish_command(&proc);
983 if (!ret)
984 ret = x;
986 return ret;
989 int transport_push(struct transport *transport,
990 int refspec_nr, const char **refspec, int flags,
991 unsigned int *reject_reasons)
993 *reject_reasons = 0;
994 transport_verify_remote_names(refspec_nr, refspec);
996 if (transport->vtable->push_refs) {
997 struct ref *remote_refs;
998 struct ref *local_refs = get_local_heads();
999 int match_flags = MATCH_REFS_NONE;
1000 int verbose = (transport->verbose > 0);
1001 int quiet = (transport->verbose < 0);
1002 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1003 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1004 int push_ret, ret, err;
1006 if (check_push_refs(local_refs, refspec_nr, refspec) < 0)
1007 return -1;
1009 remote_refs = transport->vtable->get_refs_list(transport, 1);
1011 if (flags & TRANSPORT_PUSH_ALL)
1012 match_flags |= MATCH_REFS_ALL;
1013 if (flags & TRANSPORT_PUSH_MIRROR)
1014 match_flags |= MATCH_REFS_MIRROR;
1015 if (flags & TRANSPORT_PUSH_PRUNE)
1016 match_flags |= MATCH_REFS_PRUNE;
1017 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1018 match_flags |= MATCH_REFS_FOLLOW_TAGS;
1020 if (match_push_refs(local_refs, &remote_refs,
1021 refspec_nr, refspec, match_flags)) {
1022 return -1;
1025 if (transport->smart_options &&
1026 transport->smart_options->cas &&
1027 !is_empty_cas(transport->smart_options->cas))
1028 apply_push_cas(transport->smart_options->cas,
1029 transport->remote, remote_refs);
1031 set_ref_status_for_push(remote_refs,
1032 flags & TRANSPORT_PUSH_MIRROR,
1033 flags & TRANSPORT_PUSH_FORCE);
1035 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1036 if (run_pre_push_hook(transport, remote_refs))
1037 return -1;
1039 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1040 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1041 !is_bare_repository()) {
1042 struct ref *ref = remote_refs;
1043 struct oid_array commits = OID_ARRAY_INIT;
1045 for (; ref; ref = ref->next)
1046 if (!is_null_oid(&ref->new_oid))
1047 oid_array_append(&commits,
1048 &ref->new_oid);
1050 if (!push_unpushed_submodules(&commits,
1051 transport->remote,
1052 refspec, refspec_nr,
1053 transport->push_options,
1054 pretend)) {
1055 oid_array_clear(&commits);
1056 die("Failed to push all needed submodules!");
1058 oid_array_clear(&commits);
1061 if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1062 ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1063 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1064 !pretend)) && !is_bare_repository()) {
1065 struct ref *ref = remote_refs;
1066 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1067 struct oid_array commits = OID_ARRAY_INIT;
1069 for (; ref; ref = ref->next)
1070 if (!is_null_oid(&ref->new_oid))
1071 oid_array_append(&commits,
1072 &ref->new_oid);
1074 if (find_unpushed_submodules(&commits, transport->remote->name,
1075 &needs_pushing)) {
1076 oid_array_clear(&commits);
1077 die_with_unpushed_submodules(&needs_pushing);
1079 string_list_clear(&needs_pushing, 0);
1080 oid_array_clear(&commits);
1083 if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY))
1084 push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1085 else
1086 push_ret = 0;
1087 err = push_had_errors(remote_refs);
1088 ret = push_ret | err;
1090 if (!quiet || err)
1091 transport_print_push_status(transport->url, remote_refs,
1092 verbose | porcelain, porcelain,
1093 reject_reasons);
1095 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1096 set_upstreams(transport, remote_refs, pretend);
1098 if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1099 TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1100 struct ref *ref;
1101 for (ref = remote_refs; ref; ref = ref->next)
1102 transport_update_tracking_ref(transport->remote, ref, verbose);
1105 if (porcelain && !push_ret)
1106 puts("Done");
1107 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1108 fprintf(stderr, "Everything up-to-date\n");
1110 return ret;
1112 return 1;
1115 const struct ref *transport_get_remote_refs(struct transport *transport)
1117 if (!transport->got_remote_refs) {
1118 transport->remote_refs = transport->vtable->get_refs_list(transport, 0);
1119 transport->got_remote_refs = 1;
1122 return transport->remote_refs;
1125 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1127 int rc;
1128 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1129 struct ref **heads = NULL;
1130 struct ref *rm;
1132 for (rm = refs; rm; rm = rm->next) {
1133 nr_refs++;
1134 if (rm->peer_ref &&
1135 !is_null_oid(&rm->old_oid) &&
1136 !oidcmp(&rm->peer_ref->old_oid, &rm->old_oid))
1137 continue;
1138 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1139 heads[nr_heads++] = rm;
1142 if (!nr_heads) {
1144 * When deepening of a shallow repository is requested,
1145 * then local and remote refs are likely to still be equal.
1146 * Just feed them all to the fetch method in that case.
1147 * This condition shouldn't be met in a non-deepening fetch
1148 * (see builtin/fetch.c:quickfetch()).
1150 ALLOC_ARRAY(heads, nr_refs);
1151 for (rm = refs; rm; rm = rm->next)
1152 heads[nr_heads++] = rm;
1155 rc = transport->vtable->fetch(transport, nr_heads, heads);
1157 free(heads);
1158 return rc;
1161 void transport_unlock_pack(struct transport *transport)
1163 if (transport->pack_lockfile) {
1164 unlink_or_warn(transport->pack_lockfile);
1165 FREE_AND_NULL(transport->pack_lockfile);
1169 int transport_connect(struct transport *transport, const char *name,
1170 const char *exec, int fd[2])
1172 if (transport->vtable->connect)
1173 return transport->vtable->connect(transport, name, exec, fd);
1174 else
1175 die("Operation not supported by protocol");
1178 int transport_disconnect(struct transport *transport)
1180 int ret = 0;
1181 if (transport->vtable->disconnect)
1182 ret = transport->vtable->disconnect(transport);
1183 free(transport);
1184 return ret;
1188 * Strip username (and password) from a URL and return
1189 * it in a newly allocated string.
1191 char *transport_anonymize_url(const char *url)
1193 char *scheme_prefix, *anon_part;
1194 size_t anon_len, prefix_len = 0;
1196 anon_part = strchr(url, '@');
1197 if (url_is_local_not_ssh(url) || !anon_part)
1198 goto literal_copy;
1200 anon_len = strlen(++anon_part);
1201 scheme_prefix = strstr(url, "://");
1202 if (!scheme_prefix) {
1203 if (!strchr(anon_part, ':'))
1204 /* cannot be "me@there:/path/name" */
1205 goto literal_copy;
1206 } else {
1207 const char *cp;
1208 /* make sure scheme is reasonable */
1209 for (cp = url; cp < scheme_prefix; cp++) {
1210 switch (*cp) {
1211 /* RFC 1738 2.1 */
1212 case '+': case '.': case '-':
1213 break; /* ok */
1214 default:
1215 if (isalnum(*cp))
1216 break;
1217 /* it isn't */
1218 goto literal_copy;
1221 /* @ past the first slash does not count */
1222 cp = strchr(scheme_prefix + 3, '/');
1223 if (cp && cp < anon_part)
1224 goto literal_copy;
1225 prefix_len = scheme_prefix - url + 3;
1227 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1228 (int)anon_len, anon_part);
1229 literal_copy:
1230 return xstrdup(url);
1233 static void read_alternate_refs(const char *path,
1234 alternate_ref_fn *cb,
1235 void *data)
1237 struct child_process cmd = CHILD_PROCESS_INIT;
1238 struct strbuf line = STRBUF_INIT;
1239 FILE *fh;
1241 cmd.git_cmd = 1;
1242 argv_array_pushf(&cmd.args, "--git-dir=%s", path);
1243 argv_array_push(&cmd.args, "for-each-ref");
1244 argv_array_push(&cmd.args, "--format=%(objectname) %(refname)");
1245 cmd.env = local_repo_env;
1246 cmd.out = -1;
1248 if (start_command(&cmd))
1249 return;
1251 fh = xfdopen(cmd.out, "r");
1252 while (strbuf_getline_lf(&line, fh) != EOF) {
1253 struct object_id oid;
1255 if (get_oid_hex(line.buf, &oid) ||
1256 line.buf[GIT_SHA1_HEXSZ] != ' ') {
1257 warning("invalid line while parsing alternate refs: %s",
1258 line.buf);
1259 break;
1262 cb(line.buf + GIT_SHA1_HEXSZ + 1, &oid, data);
1265 fclose(fh);
1266 finish_command(&cmd);
1269 struct alternate_refs_data {
1270 alternate_ref_fn *fn;
1271 void *data;
1274 static int refs_from_alternate_cb(struct alternate_object_database *e,
1275 void *data)
1277 struct strbuf path = STRBUF_INIT;
1278 size_t base_len;
1279 struct alternate_refs_data *cb = data;
1281 if (!strbuf_realpath(&path, e->path, 0))
1282 goto out;
1283 if (!strbuf_strip_suffix(&path, "/objects"))
1284 goto out;
1285 base_len = path.len;
1287 /* Is this a git repository with refs? */
1288 strbuf_addstr(&path, "/refs");
1289 if (!is_directory(path.buf))
1290 goto out;
1291 strbuf_setlen(&path, base_len);
1293 read_alternate_refs(path.buf, cb->fn, cb->data);
1295 out:
1296 strbuf_release(&path);
1297 return 0;
1300 void for_each_alternate_ref(alternate_ref_fn fn, void *data)
1302 struct alternate_refs_data cb;
1303 cb.fn = fn;
1304 cb.data = data;
1305 foreach_alt_odb(refs_from_alternate_cb, &cb);