The twelfth batch
[alt-git.git] / transport.c
blobef66e73090a9886b044a6109084baf1e60dc2542
1 #include "cache.h"
2 #include "config.h"
3 #include "transport.h"
4 #include "run-command.h"
5 #include "pkt-line.h"
6 #include "fetch-pack.h"
7 #include "remote.h"
8 #include "connect.h"
9 #include "send-pack.h"
10 #include "walker.h"
11 #include "bundle.h"
12 #include "dir.h"
13 #include "refs.h"
14 #include "refspec.h"
15 #include "branch.h"
16 #include "url.h"
17 #include "submodule.h"
18 #include "string-list.h"
19 #include "oid-array.h"
20 #include "sigchain.h"
21 #include "transport-internal.h"
22 #include "protocol.h"
23 #include "object-store.h"
24 #include "color.h"
26 static int transport_use_color = -1;
27 static char transport_colors[][COLOR_MAXLEN] = {
28 GIT_COLOR_RESET,
29 GIT_COLOR_RED /* REJECTED */
32 enum color_transport {
33 TRANSPORT_COLOR_RESET = 0,
34 TRANSPORT_COLOR_REJECTED = 1
37 static int transport_color_config(void)
39 const char *keys[] = {
40 "color.transport.reset",
41 "color.transport.rejected"
42 }, *key = "color.transport";
43 char *value;
44 int i;
45 static int initialized;
47 if (initialized)
48 return 0;
49 initialized = 1;
51 if (!git_config_get_string(key, &value))
52 transport_use_color = git_config_colorbool(key, value);
54 if (!want_color_stderr(transport_use_color))
55 return 0;
57 for (i = 0; i < ARRAY_SIZE(keys); i++)
58 if (!git_config_get_string(keys[i], &value)) {
59 if (!value)
60 return config_error_nonbool(keys[i]);
61 if (color_parse(value, transport_colors[i]) < 0)
62 return -1;
65 return 0;
68 static const char *transport_get_color(enum color_transport ix)
70 if (want_color_stderr(transport_use_color))
71 return transport_colors[ix];
72 return "";
75 static void set_upstreams(struct transport *transport, struct ref *refs,
76 int pretend)
78 struct ref *ref;
79 for (ref = refs; ref; ref = ref->next) {
80 const char *localname;
81 const char *tmp;
82 const char *remotename;
83 int flag = 0;
85 * Check suitability for tracking. Must be successful /
86 * already up-to-date ref create/modify (not delete).
88 if (ref->status != REF_STATUS_OK &&
89 ref->status != REF_STATUS_UPTODATE)
90 continue;
91 if (!ref->peer_ref)
92 continue;
93 if (is_null_oid(&ref->new_oid))
94 continue;
96 /* Follow symbolic refs (mainly for HEAD). */
97 localname = ref->peer_ref->name;
98 remotename = ref->name;
99 tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING,
100 NULL, &flag);
101 if (tmp && flag & REF_ISSYMREF &&
102 starts_with(tmp, "refs/heads/"))
103 localname = tmp;
105 /* Both source and destination must be local branches. */
106 if (!localname || !starts_with(localname, "refs/heads/"))
107 continue;
108 if (!remotename || !starts_with(remotename, "refs/heads/"))
109 continue;
111 if (!pretend)
112 install_branch_config(BRANCH_CONFIG_VERBOSE,
113 localname + 11, transport->remote->name,
114 remotename);
115 else
116 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
117 localname + 11, remotename + 11,
118 transport->remote->name);
122 struct bundle_transport_data {
123 int fd;
124 struct bundle_header header;
125 unsigned get_refs_from_bundle_called : 1;
128 static struct ref *get_refs_from_bundle(struct transport *transport,
129 int for_push,
130 struct transport_ls_refs_options *transport_options)
132 struct bundle_transport_data *data = transport->data;
133 struct ref *result = NULL;
134 int i;
136 if (for_push)
137 return NULL;
139 data->get_refs_from_bundle_called = 1;
141 if (data->fd > 0)
142 close(data->fd);
143 data->fd = read_bundle_header(transport->url, &data->header);
144 if (data->fd < 0)
145 die(_("could not read bundle '%s'"), transport->url);
147 transport->hash_algo = data->header.hash_algo;
149 for (i = 0; i < data->header.references.nr; i++) {
150 struct ref_list_entry *e = data->header.references.list + i;
151 struct ref *ref = alloc_ref(e->name);
152 oidcpy(&ref->old_oid, &e->oid);
153 ref->next = result;
154 result = ref;
156 return result;
159 static int fetch_refs_from_bundle(struct transport *transport,
160 int nr_heads, struct ref **to_fetch)
162 struct bundle_transport_data *data = transport->data;
163 int ret;
165 if (!data->get_refs_from_bundle_called)
166 get_refs_from_bundle(transport, 0, NULL);
167 ret = unbundle(the_repository, &data->header, data->fd,
168 transport->progress ? BUNDLE_VERBOSE : 0);
169 transport->hash_algo = data->header.hash_algo;
170 return ret;
173 static int close_bundle(struct transport *transport)
175 struct bundle_transport_data *data = transport->data;
176 if (data->fd > 0)
177 close(data->fd);
178 free(data);
179 return 0;
182 struct git_transport_data {
183 struct git_transport_options options;
184 struct child_process *conn;
185 int fd[2];
186 unsigned got_remote_heads : 1;
187 enum protocol_version version;
188 struct oid_array extra_have;
189 struct oid_array shallow;
192 static int set_git_option(struct git_transport_options *opts,
193 const char *name, const char *value)
195 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
196 opts->uploadpack = value;
197 return 0;
198 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
199 opts->receivepack = value;
200 return 0;
201 } else if (!strcmp(name, TRANS_OPT_THIN)) {
202 opts->thin = !!value;
203 return 0;
204 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
205 opts->followtags = !!value;
206 return 0;
207 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
208 opts->keep = !!value;
209 return 0;
210 } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
211 opts->update_shallow = !!value;
212 return 0;
213 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
214 if (!value)
215 opts->depth = 0;
216 else {
217 char *end;
218 opts->depth = strtol(value, &end, 0);
219 if (*end)
220 die(_("transport: invalid depth option '%s'"), value);
222 return 0;
223 } else if (!strcmp(name, TRANS_OPT_DEEPEN_SINCE)) {
224 opts->deepen_since = value;
225 return 0;
226 } else if (!strcmp(name, TRANS_OPT_DEEPEN_NOT)) {
227 opts->deepen_not = (const struct string_list *)value;
228 return 0;
229 } else if (!strcmp(name, TRANS_OPT_DEEPEN_RELATIVE)) {
230 opts->deepen_relative = !!value;
231 return 0;
232 } else if (!strcmp(name, TRANS_OPT_FROM_PROMISOR)) {
233 opts->from_promisor = !!value;
234 return 0;
235 } else if (!strcmp(name, TRANS_OPT_LIST_OBJECTS_FILTER)) {
236 list_objects_filter_die_if_populated(&opts->filter_options);
237 parse_list_objects_filter(&opts->filter_options, value);
238 return 0;
239 } else if (!strcmp(name, TRANS_OPT_REJECT_SHALLOW)) {
240 opts->reject_shallow = !!value;
241 return 0;
243 return 1;
246 static int connect_setup(struct transport *transport, int for_push)
248 struct git_transport_data *data = transport->data;
249 int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
251 if (data->conn)
252 return 0;
254 switch (transport->family) {
255 case TRANSPORT_FAMILY_ALL: break;
256 case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
257 case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
260 data->conn = git_connect(data->fd, transport->url,
261 for_push ? data->options.receivepack :
262 data->options.uploadpack,
263 flags);
265 return 0;
268 static void die_if_server_options(struct transport *transport)
270 if (!transport->server_options || !transport->server_options->nr)
271 return;
272 advise(_("see protocol.version in 'git help config' for more details"));
273 die(_("server options require protocol version 2 or later"));
277 * Obtains the protocol version from the transport and writes it to
278 * transport->data->version, first connecting if not already connected.
280 * If the protocol version is one that allows skipping the listing of remote
281 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
282 * this function returns NULL. Otherwise, this function returns the list of
283 * remote refs.
285 static struct ref *handshake(struct transport *transport, int for_push,
286 struct transport_ls_refs_options *options,
287 int must_list_refs)
289 struct git_transport_data *data = transport->data;
290 struct ref *refs = NULL;
291 struct packet_reader reader;
292 int sid_len;
293 const char *server_sid;
295 connect_setup(transport, for_push);
297 packet_reader_init(&reader, data->fd[0], NULL, 0,
298 PACKET_READ_CHOMP_NEWLINE |
299 PACKET_READ_GENTLE_ON_EOF |
300 PACKET_READ_DIE_ON_ERR_PACKET);
302 data->version = discover_version(&reader);
303 switch (data->version) {
304 case protocol_v2:
305 if (server_feature_v2("session-id", &server_sid))
306 trace2_data_string("transfer", NULL, "server-sid", server_sid);
307 if (must_list_refs)
308 get_remote_refs(data->fd[1], &reader, &refs, for_push,
309 options,
310 transport->server_options,
311 transport->stateless_rpc);
312 break;
313 case protocol_v1:
314 case protocol_v0:
315 die_if_server_options(transport);
316 get_remote_heads(&reader, &refs,
317 for_push ? REF_NORMAL : 0,
318 &data->extra_have,
319 &data->shallow);
320 server_sid = server_feature_value("session-id", &sid_len);
321 if (server_sid) {
322 char *sid = xstrndup(server_sid, sid_len);
323 trace2_data_string("transfer", NULL, "server-sid", sid);
324 free(sid);
326 break;
327 case protocol_unknown_version:
328 BUG("unknown protocol version");
330 data->got_remote_heads = 1;
331 transport->hash_algo = reader.hash_algo;
333 if (reader.line_peeked)
334 BUG("buffer must be empty at the end of handshake()");
336 return refs;
339 static struct ref *get_refs_via_connect(struct transport *transport, int for_push,
340 struct transport_ls_refs_options *options)
342 return handshake(transport, for_push, options, 1);
345 static int fetch_refs_via_pack(struct transport *transport,
346 int nr_heads, struct ref **to_fetch)
348 int ret = 0;
349 struct git_transport_data *data = transport->data;
350 struct ref *refs = NULL;
351 struct fetch_pack_args args;
352 struct ref *refs_tmp = NULL;
354 memset(&args, 0, sizeof(args));
355 args.uploadpack = data->options.uploadpack;
356 args.keep_pack = data->options.keep;
357 args.lock_pack = 1;
358 args.use_thin_pack = data->options.thin;
359 args.include_tag = data->options.followtags;
360 args.verbose = (transport->verbose > 1);
361 args.quiet = (transport->verbose < 0);
362 args.no_progress = !transport->progress;
363 args.depth = data->options.depth;
364 args.deepen_since = data->options.deepen_since;
365 args.deepen_not = data->options.deepen_not;
366 args.deepen_relative = data->options.deepen_relative;
367 args.check_self_contained_and_connected =
368 data->options.check_self_contained_and_connected;
369 args.cloning = transport->cloning;
370 args.update_shallow = data->options.update_shallow;
371 args.from_promisor = data->options.from_promisor;
372 args.filter_options = data->options.filter_options;
373 args.stateless_rpc = transport->stateless_rpc;
374 args.server_options = transport->server_options;
375 args.negotiation_tips = data->options.negotiation_tips;
376 args.reject_shallow_remote = transport->smart_options->reject_shallow;
378 if (!data->got_remote_heads) {
379 int i;
380 int must_list_refs = 0;
381 for (i = 0; i < nr_heads; i++) {
382 if (!to_fetch[i]->exact_oid) {
383 must_list_refs = 1;
384 break;
387 refs_tmp = handshake(transport, 0, NULL, must_list_refs);
390 if (data->version == protocol_unknown_version)
391 BUG("unknown protocol version");
392 else if (data->version <= protocol_v1)
393 die_if_server_options(transport);
395 refs = fetch_pack(&args, data->fd,
396 refs_tmp ? refs_tmp : transport->remote_refs,
397 to_fetch, nr_heads, &data->shallow,
398 &transport->pack_lockfiles, data->version);
400 close(data->fd[0]);
401 close(data->fd[1]);
402 if (finish_connect(data->conn))
403 ret = -1;
404 data->conn = NULL;
405 data->got_remote_heads = 0;
406 data->options.self_contained_and_connected =
407 args.self_contained_and_connected;
408 data->options.connectivity_checked = args.connectivity_checked;
410 if (refs == NULL)
411 ret = -1;
412 if (report_unmatched_refs(to_fetch, nr_heads))
413 ret = -1;
415 free_refs(refs_tmp);
416 free_refs(refs);
417 return ret;
420 static int push_had_errors(struct ref *ref)
422 for (; ref; ref = ref->next) {
423 switch (ref->status) {
424 case REF_STATUS_NONE:
425 case REF_STATUS_UPTODATE:
426 case REF_STATUS_OK:
427 break;
428 default:
429 return 1;
432 return 0;
435 int transport_refs_pushed(struct ref *ref)
437 for (; ref; ref = ref->next) {
438 switch(ref->status) {
439 case REF_STATUS_NONE:
440 case REF_STATUS_UPTODATE:
441 break;
442 default:
443 return 1;
446 return 0;
449 static void update_one_tracking_ref(struct remote *remote, char *refname,
450 struct object_id *new_oid, int deletion,
451 int verbose)
453 struct refspec_item rs;
455 memset(&rs, 0, sizeof(rs));
456 rs.src = refname;
457 rs.dst = NULL;
459 if (!remote_find_tracking(remote, &rs)) {
460 if (verbose)
461 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
462 if (deletion)
463 delete_ref(NULL, rs.dst, NULL, 0);
464 else
465 update_ref("update by push", rs.dst, new_oid,
466 NULL, 0, 0);
467 free(rs.dst);
471 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
473 char *refname;
474 struct object_id *new_oid;
475 struct ref_push_report *report;
477 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
478 return;
480 report = ref->report;
481 if (!report)
482 update_one_tracking_ref(remote, ref->name, &ref->new_oid,
483 ref->deletion, verbose);
484 else
485 for (; report; report = report->next) {
486 refname = report->ref_name ? (char *)report->ref_name : ref->name;
487 new_oid = report->new_oid ? report->new_oid : &ref->new_oid;
488 update_one_tracking_ref(remote, refname, new_oid,
489 is_null_oid(new_oid), verbose);
493 static void print_ref_status(char flag, const char *summary,
494 struct ref *to, struct ref *from, const char *msg,
495 struct ref_push_report *report,
496 int porcelain, int summary_width)
498 const char *to_name;
500 if (report && report->ref_name)
501 to_name = report->ref_name;
502 else
503 to_name = to->name;
505 if (porcelain) {
506 if (from)
507 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to_name);
508 else
509 fprintf(stdout, "%c\t:%s\t", flag, to_name);
510 if (msg)
511 fprintf(stdout, "%s (%s)\n", summary, msg);
512 else
513 fprintf(stdout, "%s\n", summary);
514 } else {
515 const char *red = "", *reset = "";
516 if (push_had_errors(to)) {
517 red = transport_get_color(TRANSPORT_COLOR_REJECTED);
518 reset = transport_get_color(TRANSPORT_COLOR_RESET);
520 fprintf(stderr, " %s%c %-*s%s ", red, flag, summary_width,
521 summary, reset);
522 if (from)
523 fprintf(stderr, "%s -> %s",
524 prettify_refname(from->name),
525 prettify_refname(to_name));
526 else
527 fputs(prettify_refname(to_name), stderr);
528 if (msg) {
529 fputs(" (", stderr);
530 fputs(msg, stderr);
531 fputc(')', stderr);
533 fputc('\n', stderr);
537 static void print_ok_ref_status(struct ref *ref,
538 struct ref_push_report *report,
539 int porcelain, int summary_width)
541 struct object_id *old_oid;
542 struct object_id *new_oid;
543 const char *ref_name;
544 int forced_update;
546 if (report && report->old_oid)
547 old_oid = report->old_oid;
548 else
549 old_oid = &ref->old_oid;
550 if (report && report->new_oid)
551 new_oid = report->new_oid;
552 else
553 new_oid = &ref->new_oid;
554 if (report && report->forced_update)
555 forced_update = report->forced_update;
556 else
557 forced_update = ref->forced_update;
558 if (report && report->ref_name)
559 ref_name = report->ref_name;
560 else
561 ref_name = ref->name;
563 if (ref->deletion)
564 print_ref_status('-', "[deleted]", ref, NULL, NULL,
565 report, porcelain, summary_width);
566 else if (is_null_oid(old_oid))
567 print_ref_status('*',
568 (starts_with(ref_name, "refs/tags/")
569 ? "[new tag]"
570 : (starts_with(ref_name, "refs/heads/")
571 ? "[new branch]"
572 : "[new reference]")),
573 ref, ref->peer_ref, NULL,
574 report, porcelain, summary_width);
575 else {
576 struct strbuf quickref = STRBUF_INIT;
577 char type;
578 const char *msg;
580 strbuf_add_unique_abbrev(&quickref, old_oid,
581 DEFAULT_ABBREV);
582 if (forced_update) {
583 strbuf_addstr(&quickref, "...");
584 type = '+';
585 msg = "forced update";
586 } else {
587 strbuf_addstr(&quickref, "..");
588 type = ' ';
589 msg = NULL;
591 strbuf_add_unique_abbrev(&quickref, new_oid,
592 DEFAULT_ABBREV);
594 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
595 report, porcelain, summary_width);
596 strbuf_release(&quickref);
600 static int print_one_push_report(struct ref *ref, const char *dest, int count,
601 struct ref_push_report *report,
602 int porcelain, int summary_width)
604 if (!count) {
605 char *url = transport_anonymize_url(dest);
606 fprintf(porcelain ? stdout : stderr, "To %s\n", url);
607 free(url);
610 switch(ref->status) {
611 case REF_STATUS_NONE:
612 print_ref_status('X', "[no match]", ref, NULL, NULL,
613 report, porcelain, summary_width);
614 break;
615 case REF_STATUS_REJECT_NODELETE:
616 print_ref_status('!', "[rejected]", ref, NULL,
617 "remote does not support deleting refs",
618 report, porcelain, summary_width);
619 break;
620 case REF_STATUS_UPTODATE:
621 print_ref_status('=', "[up to date]", ref,
622 ref->peer_ref, NULL,
623 report, porcelain, summary_width);
624 break;
625 case REF_STATUS_REJECT_NONFASTFORWARD:
626 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
627 "non-fast-forward",
628 report, porcelain, summary_width);
629 break;
630 case REF_STATUS_REJECT_ALREADY_EXISTS:
631 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
632 "already exists",
633 report, porcelain, summary_width);
634 break;
635 case REF_STATUS_REJECT_FETCH_FIRST:
636 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
637 "fetch first",
638 report, porcelain, summary_width);
639 break;
640 case REF_STATUS_REJECT_NEEDS_FORCE:
641 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
642 "needs force",
643 report, porcelain, summary_width);
644 break;
645 case REF_STATUS_REJECT_STALE:
646 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
647 "stale info",
648 report, porcelain, summary_width);
649 break;
650 case REF_STATUS_REJECT_REMOTE_UPDATED:
651 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
652 "remote ref updated since checkout",
653 report, porcelain, summary_width);
654 break;
655 case REF_STATUS_REJECT_SHALLOW:
656 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
657 "new shallow roots not allowed",
658 report, porcelain, summary_width);
659 break;
660 case REF_STATUS_REMOTE_REJECT:
661 print_ref_status('!', "[remote rejected]", ref,
662 ref->deletion ? NULL : ref->peer_ref,
663 ref->remote_status,
664 report, porcelain, summary_width);
665 break;
666 case REF_STATUS_EXPECTING_REPORT:
667 print_ref_status('!', "[remote failure]", ref,
668 ref->deletion ? NULL : ref->peer_ref,
669 "remote failed to report status",
670 report, porcelain, summary_width);
671 break;
672 case REF_STATUS_ATOMIC_PUSH_FAILED:
673 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
674 "atomic push failed",
675 report, porcelain, summary_width);
676 break;
677 case REF_STATUS_OK:
678 print_ok_ref_status(ref, report, porcelain, summary_width);
679 break;
682 return 1;
685 static int print_one_push_status(struct ref *ref, const char *dest, int count,
686 int porcelain, int summary_width)
688 struct ref_push_report *report;
689 int n = 0;
691 if (!ref->report)
692 return print_one_push_report(ref, dest, count,
693 NULL, porcelain, summary_width);
695 for (report = ref->report; report; report = report->next)
696 print_one_push_report(ref, dest, count + n++,
697 report, porcelain, summary_width);
698 return n;
701 static int measure_abbrev(const struct object_id *oid, int sofar)
703 char hex[GIT_MAX_HEXSZ + 1];
704 int w = find_unique_abbrev_r(hex, oid, DEFAULT_ABBREV);
706 return (w < sofar) ? sofar : w;
709 int transport_summary_width(const struct ref *refs)
711 int maxw = -1;
713 for (; refs; refs = refs->next) {
714 maxw = measure_abbrev(&refs->old_oid, maxw);
715 maxw = measure_abbrev(&refs->new_oid, maxw);
717 if (maxw < 0)
718 maxw = FALLBACK_DEFAULT_ABBREV;
719 return (2 * maxw + 3);
722 void transport_print_push_status(const char *dest, struct ref *refs,
723 int verbose, int porcelain, unsigned int *reject_reasons)
725 struct ref *ref;
726 int n = 0;
727 char *head;
728 int summary_width = transport_summary_width(refs);
730 if (transport_color_config() < 0)
731 warning(_("could not parse transport.color.* config"));
733 head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
735 if (verbose) {
736 for (ref = refs; ref; ref = ref->next)
737 if (ref->status == REF_STATUS_UPTODATE)
738 n += print_one_push_status(ref, dest, n,
739 porcelain, summary_width);
742 for (ref = refs; ref; ref = ref->next)
743 if (ref->status == REF_STATUS_OK)
744 n += print_one_push_status(ref, dest, n,
745 porcelain, summary_width);
747 *reject_reasons = 0;
748 for (ref = refs; ref; ref = ref->next) {
749 if (ref->status != REF_STATUS_NONE &&
750 ref->status != REF_STATUS_UPTODATE &&
751 ref->status != REF_STATUS_OK)
752 n += print_one_push_status(ref, dest, n,
753 porcelain, summary_width);
754 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
755 if (head != NULL && !strcmp(head, ref->name))
756 *reject_reasons |= REJECT_NON_FF_HEAD;
757 else
758 *reject_reasons |= REJECT_NON_FF_OTHER;
759 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
760 *reject_reasons |= REJECT_ALREADY_EXISTS;
761 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
762 *reject_reasons |= REJECT_FETCH_FIRST;
763 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
764 *reject_reasons |= REJECT_NEEDS_FORCE;
765 } else if (ref->status == REF_STATUS_REJECT_REMOTE_UPDATED) {
766 *reject_reasons |= REJECT_REF_NEEDS_UPDATE;
769 free(head);
772 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
774 struct git_transport_data *data = transport->data;
775 struct send_pack_args args;
776 int ret = 0;
778 if (transport_color_config() < 0)
779 return -1;
781 if (!data->got_remote_heads)
782 get_refs_via_connect(transport, 1, NULL);
784 memset(&args, 0, sizeof(args));
785 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
786 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
787 args.use_thin_pack = data->options.thin;
788 args.verbose = (transport->verbose > 0);
789 args.quiet = (transport->verbose < 0);
790 args.progress = transport->progress;
791 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
792 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
793 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
794 args.push_options = transport->push_options;
795 args.url = transport->url;
797 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
798 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
799 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
800 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
801 else
802 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
804 switch (data->version) {
805 case protocol_v2:
806 die(_("support for protocol v2 not implemented yet"));
807 break;
808 case protocol_v1:
809 case protocol_v0:
810 ret = send_pack(&args, data->fd, data->conn, remote_refs,
811 &data->extra_have);
812 break;
813 case protocol_unknown_version:
814 BUG("unknown protocol version");
817 close(data->fd[1]);
818 close(data->fd[0]);
820 * Atomic push may abort the connection early and close the pipe,
821 * which may cause an error for `finish_connect()`. Ignore this error
822 * for atomic git-push.
824 if (ret || args.atomic)
825 finish_connect(data->conn);
826 else
827 ret = finish_connect(data->conn);
828 data->conn = NULL;
829 data->got_remote_heads = 0;
831 return ret;
834 static int connect_git(struct transport *transport, const char *name,
835 const char *executable, int fd[2])
837 struct git_transport_data *data = transport->data;
838 data->conn = git_connect(data->fd, transport->url,
839 executable, 0);
840 fd[0] = data->fd[0];
841 fd[1] = data->fd[1];
842 return 0;
845 static int disconnect_git(struct transport *transport)
847 struct git_transport_data *data = transport->data;
848 if (data->conn) {
849 if (data->got_remote_heads && !transport->stateless_rpc)
850 packet_flush(data->fd[1]);
851 close(data->fd[0]);
852 close(data->fd[1]);
853 finish_connect(data->conn);
856 free(data);
857 return 0;
860 static struct transport_vtable taken_over_vtable = {
861 NULL,
862 get_refs_via_connect,
863 fetch_refs_via_pack,
864 git_transport_push,
865 NULL,
866 disconnect_git
869 void transport_take_over(struct transport *transport,
870 struct child_process *child)
872 struct git_transport_data *data;
874 if (!transport->smart_options)
875 BUG("taking over transport requires non-NULL "
876 "smart_options field.");
878 CALLOC_ARRAY(data, 1);
879 data->options = *transport->smart_options;
880 data->conn = child;
881 data->fd[0] = data->conn->out;
882 data->fd[1] = data->conn->in;
883 data->got_remote_heads = 0;
884 transport->data = data;
886 transport->vtable = &taken_over_vtable;
887 transport->smart_options = &(data->options);
889 transport->cannot_reuse = 1;
892 static int is_file(const char *url)
894 struct stat buf;
895 if (stat(url, &buf))
896 return 0;
897 return S_ISREG(buf.st_mode);
900 static int external_specification_len(const char *url)
902 return strchr(url, ':') - url;
905 static const struct string_list *protocol_whitelist(void)
907 static int enabled = -1;
908 static struct string_list allowed = STRING_LIST_INIT_DUP;
910 if (enabled < 0) {
911 const char *v = getenv("GIT_ALLOW_PROTOCOL");
912 if (v) {
913 string_list_split(&allowed, v, ':', -1);
914 string_list_sort(&allowed);
915 enabled = 1;
916 } else {
917 enabled = 0;
921 return enabled ? &allowed : NULL;
924 enum protocol_allow_config {
925 PROTOCOL_ALLOW_NEVER = 0,
926 PROTOCOL_ALLOW_USER_ONLY,
927 PROTOCOL_ALLOW_ALWAYS
930 static enum protocol_allow_config parse_protocol_config(const char *key,
931 const char *value)
933 if (!strcasecmp(value, "always"))
934 return PROTOCOL_ALLOW_ALWAYS;
935 else if (!strcasecmp(value, "never"))
936 return PROTOCOL_ALLOW_NEVER;
937 else if (!strcasecmp(value, "user"))
938 return PROTOCOL_ALLOW_USER_ONLY;
940 die(_("unknown value for config '%s': %s"), key, value);
943 static enum protocol_allow_config get_protocol_config(const char *type)
945 char *key = xstrfmt("protocol.%s.allow", type);
946 char *value;
948 /* first check the per-protocol config */
949 if (!git_config_get_string(key, &value)) {
950 enum protocol_allow_config ret =
951 parse_protocol_config(key, value);
952 free(key);
953 free(value);
954 return ret;
956 free(key);
958 /* if defined, fallback to user-defined default for unknown protocols */
959 if (!git_config_get_string("protocol.allow", &value)) {
960 enum protocol_allow_config ret =
961 parse_protocol_config("protocol.allow", value);
962 free(value);
963 return ret;
966 /* fallback to built-in defaults */
967 /* known safe */
968 if (!strcmp(type, "http") ||
969 !strcmp(type, "https") ||
970 !strcmp(type, "git") ||
971 !strcmp(type, "ssh") ||
972 !strcmp(type, "file"))
973 return PROTOCOL_ALLOW_ALWAYS;
975 /* known scary; err on the side of caution */
976 if (!strcmp(type, "ext"))
977 return PROTOCOL_ALLOW_NEVER;
979 /* unknown; by default let them be used only directly by the user */
980 return PROTOCOL_ALLOW_USER_ONLY;
983 int is_transport_allowed(const char *type, int from_user)
985 const struct string_list *whitelist = protocol_whitelist();
986 if (whitelist)
987 return string_list_has_string(whitelist, type);
989 switch (get_protocol_config(type)) {
990 case PROTOCOL_ALLOW_ALWAYS:
991 return 1;
992 case PROTOCOL_ALLOW_NEVER:
993 return 0;
994 case PROTOCOL_ALLOW_USER_ONLY:
995 if (from_user < 0)
996 from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
997 return from_user;
1000 BUG("invalid protocol_allow_config type");
1003 void transport_check_allowed(const char *type)
1005 if (!is_transport_allowed(type, -1))
1006 die(_("transport '%s' not allowed"), type);
1009 static struct transport_vtable bundle_vtable = {
1010 NULL,
1011 get_refs_from_bundle,
1012 fetch_refs_from_bundle,
1013 NULL,
1014 NULL,
1015 close_bundle
1018 static struct transport_vtable builtin_smart_vtable = {
1019 NULL,
1020 get_refs_via_connect,
1021 fetch_refs_via_pack,
1022 git_transport_push,
1023 connect_git,
1024 disconnect_git
1027 struct transport *transport_get(struct remote *remote, const char *url)
1029 const char *helper;
1030 struct transport *ret = xcalloc(1, sizeof(*ret));
1032 ret->progress = isatty(2);
1033 string_list_init(&ret->pack_lockfiles, 1);
1035 if (!remote)
1036 BUG("No remote provided to transport_get()");
1038 ret->got_remote_refs = 0;
1039 ret->remote = remote;
1040 helper = remote->foreign_vcs;
1042 if (!url && remote->url)
1043 url = remote->url[0];
1044 ret->url = url;
1046 /* maybe it is a foreign URL? */
1047 if (url) {
1048 const char *p = url;
1050 while (is_urlschemechar(p == url, *p))
1051 p++;
1052 if (starts_with(p, "::"))
1053 helper = xstrndup(url, p - url);
1056 if (helper) {
1057 transport_helper_init(ret, helper);
1058 } else if (starts_with(url, "rsync:")) {
1059 die(_("git-over-rsync is no longer supported"));
1060 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
1061 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
1062 transport_check_allowed("file");
1063 ret->data = data;
1064 ret->vtable = &bundle_vtable;
1065 ret->smart_options = NULL;
1066 } else if (!is_url(url)
1067 || starts_with(url, "file://")
1068 || starts_with(url, "git://")
1069 || starts_with(url, "ssh://")
1070 || starts_with(url, "git+ssh://") /* deprecated - do not use */
1071 || starts_with(url, "ssh+git://") /* deprecated - do not use */
1074 * These are builtin smart transports; "allowed" transports
1075 * will be checked individually in git_connect.
1077 struct git_transport_data *data = xcalloc(1, sizeof(*data));
1078 ret->data = data;
1079 ret->vtable = &builtin_smart_vtable;
1080 ret->smart_options = &(data->options);
1082 data->conn = NULL;
1083 data->got_remote_heads = 0;
1084 } else {
1085 /* Unknown protocol in URL. Pass to external handler. */
1086 int len = external_specification_len(url);
1087 char *handler = xmemdupz(url, len);
1088 transport_helper_init(ret, handler);
1091 if (ret->smart_options) {
1092 ret->smart_options->thin = 1;
1093 ret->smart_options->uploadpack = "git-upload-pack";
1094 if (remote->uploadpack)
1095 ret->smart_options->uploadpack = remote->uploadpack;
1096 ret->smart_options->receivepack = "git-receive-pack";
1097 if (remote->receivepack)
1098 ret->smart_options->receivepack = remote->receivepack;
1101 ret->hash_algo = &hash_algos[GIT_HASH_SHA1];
1103 return ret;
1106 const struct git_hash_algo *transport_get_hash_algo(struct transport *transport)
1108 return transport->hash_algo;
1111 int transport_set_option(struct transport *transport,
1112 const char *name, const char *value)
1114 int git_reports = 1, protocol_reports = 1;
1116 if (transport->smart_options)
1117 git_reports = set_git_option(transport->smart_options,
1118 name, value);
1120 if (transport->vtable->set_option)
1121 protocol_reports = transport->vtable->set_option(transport,
1122 name, value);
1124 /* If either report is 0, report 0 (success). */
1125 if (!git_reports || !protocol_reports)
1126 return 0;
1127 /* If either reports -1 (invalid value), report -1. */
1128 if ((git_reports == -1) || (protocol_reports == -1))
1129 return -1;
1130 /* Otherwise if both report unknown, report unknown. */
1131 return 1;
1134 void transport_set_verbosity(struct transport *transport, int verbosity,
1135 int force_progress)
1137 if (verbosity >= 1)
1138 transport->verbose = verbosity <= 3 ? verbosity : 3;
1139 if (verbosity < 0)
1140 transport->verbose = -1;
1143 * Rules used to determine whether to report progress (processing aborts
1144 * when a rule is satisfied):
1146 * . Report progress, if force_progress is 1 (ie. --progress).
1147 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1148 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1149 * . Report progress if isatty(2) is 1.
1151 if (force_progress >= 0)
1152 transport->progress = !!force_progress;
1153 else
1154 transport->progress = verbosity >= 0 && isatty(2);
1157 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
1159 int i;
1161 fprintf(stderr, _("The following submodule paths contain changes that can\n"
1162 "not be found on any remote:\n"));
1163 for (i = 0; i < needs_pushing->nr; i++)
1164 fprintf(stderr, " %s\n", needs_pushing->items[i].string);
1165 fprintf(stderr, _("\nPlease try\n\n"
1166 " git push --recurse-submodules=on-demand\n\n"
1167 "or cd to the path and use\n\n"
1168 " git push\n\n"
1169 "to push them to a remote.\n\n"));
1171 string_list_clear(needs_pushing, 0);
1173 die(_("Aborting."));
1176 static int run_pre_push_hook(struct transport *transport,
1177 struct ref *remote_refs)
1179 int ret = 0, x;
1180 struct ref *r;
1181 struct child_process proc = CHILD_PROCESS_INIT;
1182 struct strbuf buf;
1183 const char *argv[4];
1185 if (!(argv[0] = find_hook("pre-push")))
1186 return 0;
1188 argv[1] = transport->remote->name;
1189 argv[2] = transport->url;
1190 argv[3] = NULL;
1192 proc.argv = argv;
1193 proc.in = -1;
1194 proc.trace2_hook_name = "pre-push";
1196 if (start_command(&proc)) {
1197 finish_command(&proc);
1198 return -1;
1201 sigchain_push(SIGPIPE, SIG_IGN);
1203 strbuf_init(&buf, 256);
1205 for (r = remote_refs; r; r = r->next) {
1206 if (!r->peer_ref) continue;
1207 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
1208 if (r->status == REF_STATUS_REJECT_STALE) continue;
1209 if (r->status == REF_STATUS_REJECT_REMOTE_UPDATED) continue;
1210 if (r->status == REF_STATUS_UPTODATE) continue;
1212 strbuf_reset(&buf);
1213 strbuf_addf( &buf, "%s %s %s %s\n",
1214 r->peer_ref->name, oid_to_hex(&r->new_oid),
1215 r->name, oid_to_hex(&r->old_oid));
1217 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
1218 /* We do not mind if a hook does not read all refs. */
1219 if (errno != EPIPE)
1220 ret = -1;
1221 break;
1225 strbuf_release(&buf);
1227 x = close(proc.in);
1228 if (!ret)
1229 ret = x;
1231 sigchain_pop(SIGPIPE);
1233 x = finish_command(&proc);
1234 if (!ret)
1235 ret = x;
1237 return ret;
1240 int transport_push(struct repository *r,
1241 struct transport *transport,
1242 struct refspec *rs, int flags,
1243 unsigned int *reject_reasons)
1245 *reject_reasons = 0;
1247 if (transport_color_config() < 0)
1248 return -1;
1250 if (transport->vtable->push_refs) {
1251 struct ref *remote_refs;
1252 struct ref *local_refs = get_local_heads();
1253 int match_flags = MATCH_REFS_NONE;
1254 int verbose = (transport->verbose > 0);
1255 int quiet = (transport->verbose < 0);
1256 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1257 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1258 int push_ret, ret, err;
1259 struct transport_ls_refs_options transport_options =
1260 TRANSPORT_LS_REFS_OPTIONS_INIT;
1262 if (check_push_refs(local_refs, rs) < 0)
1263 return -1;
1265 refspec_ref_prefixes(rs, &transport_options.ref_prefixes);
1267 trace2_region_enter("transport_push", "get_refs_list", r);
1268 remote_refs = transport->vtable->get_refs_list(transport, 1,
1269 &transport_options);
1270 trace2_region_leave("transport_push", "get_refs_list", r);
1272 strvec_clear(&transport_options.ref_prefixes);
1274 if (flags & TRANSPORT_PUSH_ALL)
1275 match_flags |= MATCH_REFS_ALL;
1276 if (flags & TRANSPORT_PUSH_MIRROR)
1277 match_flags |= MATCH_REFS_MIRROR;
1278 if (flags & TRANSPORT_PUSH_PRUNE)
1279 match_flags |= MATCH_REFS_PRUNE;
1280 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1281 match_flags |= MATCH_REFS_FOLLOW_TAGS;
1283 if (match_push_refs(local_refs, &remote_refs, rs, match_flags))
1284 return -1;
1286 if (transport->smart_options &&
1287 transport->smart_options->cas &&
1288 !is_empty_cas(transport->smart_options->cas))
1289 apply_push_cas(transport->smart_options->cas,
1290 transport->remote, remote_refs);
1292 set_ref_status_for_push(remote_refs,
1293 flags & TRANSPORT_PUSH_MIRROR,
1294 flags & TRANSPORT_PUSH_FORCE);
1296 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1297 if (run_pre_push_hook(transport, remote_refs))
1298 return -1;
1300 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1301 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1302 !is_bare_repository()) {
1303 struct ref *ref = remote_refs;
1304 struct oid_array commits = OID_ARRAY_INIT;
1306 trace2_region_enter("transport_push", "push_submodules", r);
1307 for (; ref; ref = ref->next)
1308 if (!is_null_oid(&ref->new_oid))
1309 oid_array_append(&commits,
1310 &ref->new_oid);
1312 if (!push_unpushed_submodules(r,
1313 &commits,
1314 transport->remote,
1316 transport->push_options,
1317 pretend)) {
1318 oid_array_clear(&commits);
1319 trace2_region_leave("transport_push", "push_submodules", r);
1320 die(_("failed to push all needed submodules"));
1322 oid_array_clear(&commits);
1323 trace2_region_leave("transport_push", "push_submodules", r);
1326 if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1327 ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1328 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1329 !pretend)) && !is_bare_repository()) {
1330 struct ref *ref = remote_refs;
1331 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1332 struct oid_array commits = OID_ARRAY_INIT;
1334 trace2_region_enter("transport_push", "check_submodules", r);
1335 for (; ref; ref = ref->next)
1336 if (!is_null_oid(&ref->new_oid))
1337 oid_array_append(&commits,
1338 &ref->new_oid);
1340 if (find_unpushed_submodules(r,
1341 &commits,
1342 transport->remote->name,
1343 &needs_pushing)) {
1344 oid_array_clear(&commits);
1345 trace2_region_leave("transport_push", "check_submodules", r);
1346 die_with_unpushed_submodules(&needs_pushing);
1348 string_list_clear(&needs_pushing, 0);
1349 oid_array_clear(&commits);
1350 trace2_region_leave("transport_push", "check_submodules", r);
1353 if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY)) {
1354 trace2_region_enter("transport_push", "push_refs", r);
1355 push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1356 trace2_region_leave("transport_push", "push_refs", r);
1357 } else
1358 push_ret = 0;
1359 err = push_had_errors(remote_refs);
1360 ret = push_ret | err;
1362 if (!quiet || err)
1363 transport_print_push_status(transport->url, remote_refs,
1364 verbose | porcelain, porcelain,
1365 reject_reasons);
1367 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1368 set_upstreams(transport, remote_refs, pretend);
1370 if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1371 TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1372 struct ref *ref;
1373 for (ref = remote_refs; ref; ref = ref->next)
1374 transport_update_tracking_ref(transport->remote, ref, verbose);
1377 if (porcelain && !push_ret)
1378 puts("Done");
1379 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1380 fprintf(stderr, "Everything up-to-date\n");
1382 return ret;
1384 return 1;
1387 const struct ref *transport_get_remote_refs(struct transport *transport,
1388 struct transport_ls_refs_options *transport_options)
1390 if (!transport->got_remote_refs) {
1391 transport->remote_refs =
1392 transport->vtable->get_refs_list(transport, 0,
1393 transport_options);
1394 transport->got_remote_refs = 1;
1397 return transport->remote_refs;
1400 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1402 int rc;
1403 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1404 struct ref **heads = NULL;
1405 struct ref *rm;
1407 for (rm = refs; rm; rm = rm->next) {
1408 nr_refs++;
1409 if (rm->peer_ref &&
1410 !is_null_oid(&rm->old_oid) &&
1411 oideq(&rm->peer_ref->old_oid, &rm->old_oid))
1412 continue;
1413 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1414 heads[nr_heads++] = rm;
1417 if (!nr_heads) {
1419 * When deepening of a shallow repository is requested,
1420 * then local and remote refs are likely to still be equal.
1421 * Just feed them all to the fetch method in that case.
1422 * This condition shouldn't be met in a non-deepening fetch
1423 * (see builtin/fetch.c:quickfetch()).
1425 ALLOC_ARRAY(heads, nr_refs);
1426 for (rm = refs; rm; rm = rm->next)
1427 heads[nr_heads++] = rm;
1430 rc = transport->vtable->fetch(transport, nr_heads, heads);
1432 free(heads);
1433 return rc;
1436 void transport_unlock_pack(struct transport *transport)
1438 int i;
1440 for (i = 0; i < transport->pack_lockfiles.nr; i++)
1441 unlink_or_warn(transport->pack_lockfiles.items[i].string);
1442 string_list_clear(&transport->pack_lockfiles, 0);
1445 int transport_connect(struct transport *transport, const char *name,
1446 const char *exec, int fd[2])
1448 if (transport->vtable->connect)
1449 return transport->vtable->connect(transport, name, exec, fd);
1450 else
1451 die(_("operation not supported by protocol"));
1454 int transport_disconnect(struct transport *transport)
1456 int ret = 0;
1457 if (transport->vtable->disconnect)
1458 ret = transport->vtable->disconnect(transport);
1459 if (transport->got_remote_refs)
1460 free_refs((void *)transport->remote_refs);
1461 free(transport);
1462 return ret;
1466 * Strip username (and password) from a URL and return
1467 * it in a newly allocated string.
1469 char *transport_anonymize_url(const char *url)
1471 char *scheme_prefix, *anon_part;
1472 size_t anon_len, prefix_len = 0;
1474 anon_part = strchr(url, '@');
1475 if (url_is_local_not_ssh(url) || !anon_part)
1476 goto literal_copy;
1478 anon_len = strlen(++anon_part);
1479 scheme_prefix = strstr(url, "://");
1480 if (!scheme_prefix) {
1481 if (!strchr(anon_part, ':'))
1482 /* cannot be "me@there:/path/name" */
1483 goto literal_copy;
1484 } else {
1485 const char *cp;
1486 /* make sure scheme is reasonable */
1487 for (cp = url; cp < scheme_prefix; cp++) {
1488 switch (*cp) {
1489 /* RFC 1738 2.1 */
1490 case '+': case '.': case '-':
1491 break; /* ok */
1492 default:
1493 if (isalnum(*cp))
1494 break;
1495 /* it isn't */
1496 goto literal_copy;
1499 /* @ past the first slash does not count */
1500 cp = strchr(scheme_prefix + 3, '/');
1501 if (cp && cp < anon_part)
1502 goto literal_copy;
1503 prefix_len = scheme_prefix - url + 3;
1505 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1506 (int)anon_len, anon_part);
1507 literal_copy:
1508 return xstrdup(url);