Merge branch 'cw/pack-config-doc'
[alt-git.git] / transport.c
blobb13fab5dc3b1b90933b77f15fe87ca422cc70784
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;
240 return 1;
243 static int connect_setup(struct transport *transport, int for_push)
245 struct git_transport_data *data = transport->data;
246 int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
248 if (data->conn)
249 return 0;
251 switch (transport->family) {
252 case TRANSPORT_FAMILY_ALL: break;
253 case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
254 case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
257 data->conn = git_connect(data->fd, transport->url,
258 for_push ? data->options.receivepack :
259 data->options.uploadpack,
260 flags);
262 return 0;
265 static void die_if_server_options(struct transport *transport)
267 if (!transport->server_options || !transport->server_options->nr)
268 return;
269 advise(_("see protocol.version in 'git help config' for more details"));
270 die(_("server options require protocol version 2 or later"));
274 * Obtains the protocol version from the transport and writes it to
275 * transport->data->version, first connecting if not already connected.
277 * If the protocol version is one that allows skipping the listing of remote
278 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
279 * this function returns NULL. Otherwise, this function returns the list of
280 * remote refs.
282 static struct ref *handshake(struct transport *transport, int for_push,
283 struct transport_ls_refs_options *options,
284 int must_list_refs)
286 struct git_transport_data *data = transport->data;
287 struct ref *refs = NULL;
288 struct packet_reader reader;
289 int sid_len;
290 const char *server_sid;
292 connect_setup(transport, for_push);
294 packet_reader_init(&reader, data->fd[0], NULL, 0,
295 PACKET_READ_CHOMP_NEWLINE |
296 PACKET_READ_GENTLE_ON_EOF |
297 PACKET_READ_DIE_ON_ERR_PACKET);
299 data->version = discover_version(&reader);
300 switch (data->version) {
301 case protocol_v2:
302 if (server_feature_v2("session-id", &server_sid))
303 trace2_data_string("transfer", NULL, "server-sid", server_sid);
304 if (must_list_refs)
305 get_remote_refs(data->fd[1], &reader, &refs, for_push,
306 options,
307 transport->server_options,
308 transport->stateless_rpc);
309 break;
310 case protocol_v1:
311 case protocol_v0:
312 die_if_server_options(transport);
313 get_remote_heads(&reader, &refs,
314 for_push ? REF_NORMAL : 0,
315 &data->extra_have,
316 &data->shallow);
317 server_sid = server_feature_value("session-id", &sid_len);
318 if (server_sid) {
319 char *sid = xstrndup(server_sid, sid_len);
320 trace2_data_string("transfer", NULL, "server-sid", sid);
321 free(sid);
323 break;
324 case protocol_unknown_version:
325 BUG("unknown protocol version");
327 data->got_remote_heads = 1;
328 transport->hash_algo = reader.hash_algo;
330 if (reader.line_peeked)
331 BUG("buffer must be empty at the end of handshake()");
333 return refs;
336 static struct ref *get_refs_via_connect(struct transport *transport, int for_push,
337 struct transport_ls_refs_options *options)
339 return handshake(transport, for_push, options, 1);
342 static int fetch_refs_via_pack(struct transport *transport,
343 int nr_heads, struct ref **to_fetch)
345 int ret = 0;
346 struct git_transport_data *data = transport->data;
347 struct ref *refs = NULL;
348 struct fetch_pack_args args;
349 struct ref *refs_tmp = NULL;
351 memset(&args, 0, sizeof(args));
352 args.uploadpack = data->options.uploadpack;
353 args.keep_pack = data->options.keep;
354 args.lock_pack = 1;
355 args.use_thin_pack = data->options.thin;
356 args.include_tag = data->options.followtags;
357 args.verbose = (transport->verbose > 1);
358 args.quiet = (transport->verbose < 0);
359 args.no_progress = !transport->progress;
360 args.depth = data->options.depth;
361 args.deepen_since = data->options.deepen_since;
362 args.deepen_not = data->options.deepen_not;
363 args.deepen_relative = data->options.deepen_relative;
364 args.check_self_contained_and_connected =
365 data->options.check_self_contained_and_connected;
366 args.cloning = transport->cloning;
367 args.update_shallow = data->options.update_shallow;
368 args.from_promisor = data->options.from_promisor;
369 args.filter_options = data->options.filter_options;
370 args.stateless_rpc = transport->stateless_rpc;
371 args.server_options = transport->server_options;
372 args.negotiation_tips = data->options.negotiation_tips;
374 if (!data->got_remote_heads) {
375 int i;
376 int must_list_refs = 0;
377 for (i = 0; i < nr_heads; i++) {
378 if (!to_fetch[i]->exact_oid) {
379 must_list_refs = 1;
380 break;
383 refs_tmp = handshake(transport, 0, NULL, must_list_refs);
386 if (data->version == protocol_unknown_version)
387 BUG("unknown protocol version");
388 else if (data->version <= protocol_v1)
389 die_if_server_options(transport);
391 refs = fetch_pack(&args, data->fd,
392 refs_tmp ? refs_tmp : transport->remote_refs,
393 to_fetch, nr_heads, &data->shallow,
394 &transport->pack_lockfiles, data->version);
396 close(data->fd[0]);
397 close(data->fd[1]);
398 if (finish_connect(data->conn))
399 ret = -1;
400 data->conn = NULL;
401 data->got_remote_heads = 0;
402 data->options.self_contained_and_connected =
403 args.self_contained_and_connected;
404 data->options.connectivity_checked = args.connectivity_checked;
406 if (refs == NULL)
407 ret = -1;
408 if (report_unmatched_refs(to_fetch, nr_heads))
409 ret = -1;
411 free_refs(refs_tmp);
412 free_refs(refs);
413 return ret;
416 static int push_had_errors(struct ref *ref)
418 for (; ref; ref = ref->next) {
419 switch (ref->status) {
420 case REF_STATUS_NONE:
421 case REF_STATUS_UPTODATE:
422 case REF_STATUS_OK:
423 break;
424 default:
425 return 1;
428 return 0;
431 int transport_refs_pushed(struct ref *ref)
433 for (; ref; ref = ref->next) {
434 switch(ref->status) {
435 case REF_STATUS_NONE:
436 case REF_STATUS_UPTODATE:
437 break;
438 default:
439 return 1;
442 return 0;
445 static void update_one_tracking_ref(struct remote *remote, char *refname,
446 struct object_id *new_oid, int deletion,
447 int verbose)
449 struct refspec_item rs;
451 memset(&rs, 0, sizeof(rs));
452 rs.src = refname;
453 rs.dst = NULL;
455 if (!remote_find_tracking(remote, &rs)) {
456 if (verbose)
457 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
458 if (deletion)
459 delete_ref(NULL, rs.dst, NULL, 0);
460 else
461 update_ref("update by push", rs.dst, new_oid,
462 NULL, 0, 0);
463 free(rs.dst);
467 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
469 char *refname;
470 struct object_id *new_oid;
471 struct ref_push_report *report;
473 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
474 return;
476 report = ref->report;
477 if (!report)
478 update_one_tracking_ref(remote, ref->name, &ref->new_oid,
479 ref->deletion, verbose);
480 else
481 for (; report; report = report->next) {
482 refname = report->ref_name ? (char *)report->ref_name : ref->name;
483 new_oid = report->new_oid ? report->new_oid : &ref->new_oid;
484 update_one_tracking_ref(remote, refname, new_oid,
485 is_null_oid(new_oid), verbose);
489 static void print_ref_status(char flag, const char *summary,
490 struct ref *to, struct ref *from, const char *msg,
491 struct ref_push_report *report,
492 int porcelain, int summary_width)
494 const char *to_name;
496 if (report && report->ref_name)
497 to_name = report->ref_name;
498 else
499 to_name = to->name;
501 if (porcelain) {
502 if (from)
503 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to_name);
504 else
505 fprintf(stdout, "%c\t:%s\t", flag, to_name);
506 if (msg)
507 fprintf(stdout, "%s (%s)\n", summary, msg);
508 else
509 fprintf(stdout, "%s\n", summary);
510 } else {
511 const char *red = "", *reset = "";
512 if (push_had_errors(to)) {
513 red = transport_get_color(TRANSPORT_COLOR_REJECTED);
514 reset = transport_get_color(TRANSPORT_COLOR_RESET);
516 fprintf(stderr, " %s%c %-*s%s ", red, flag, summary_width,
517 summary, reset);
518 if (from)
519 fprintf(stderr, "%s -> %s",
520 prettify_refname(from->name),
521 prettify_refname(to_name));
522 else
523 fputs(prettify_refname(to_name), stderr);
524 if (msg) {
525 fputs(" (", stderr);
526 fputs(msg, stderr);
527 fputc(')', stderr);
529 fputc('\n', stderr);
533 static void print_ok_ref_status(struct ref *ref,
534 struct ref_push_report *report,
535 int porcelain, int summary_width)
537 struct object_id *old_oid;
538 struct object_id *new_oid;
539 const char *ref_name;
540 int forced_update;
542 if (report && report->old_oid)
543 old_oid = report->old_oid;
544 else
545 old_oid = &ref->old_oid;
546 if (report && report->new_oid)
547 new_oid = report->new_oid;
548 else
549 new_oid = &ref->new_oid;
550 if (report && report->forced_update)
551 forced_update = report->forced_update;
552 else
553 forced_update = ref->forced_update;
554 if (report && report->ref_name)
555 ref_name = report->ref_name;
556 else
557 ref_name = ref->name;
559 if (ref->deletion)
560 print_ref_status('-', "[deleted]", ref, NULL, NULL,
561 report, porcelain, summary_width);
562 else if (is_null_oid(old_oid))
563 print_ref_status('*',
564 (starts_with(ref_name, "refs/tags/")
565 ? "[new tag]"
566 : (starts_with(ref_name, "refs/heads/")
567 ? "[new branch]"
568 : "[new reference]")),
569 ref, ref->peer_ref, NULL,
570 report, porcelain, summary_width);
571 else {
572 struct strbuf quickref = STRBUF_INIT;
573 char type;
574 const char *msg;
576 strbuf_add_unique_abbrev(&quickref, old_oid,
577 DEFAULT_ABBREV);
578 if (forced_update) {
579 strbuf_addstr(&quickref, "...");
580 type = '+';
581 msg = "forced update";
582 } else {
583 strbuf_addstr(&quickref, "..");
584 type = ' ';
585 msg = NULL;
587 strbuf_add_unique_abbrev(&quickref, new_oid,
588 DEFAULT_ABBREV);
590 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
591 report, porcelain, summary_width);
592 strbuf_release(&quickref);
596 static int print_one_push_report(struct ref *ref, const char *dest, int count,
597 struct ref_push_report *report,
598 int porcelain, int summary_width)
600 if (!count) {
601 char *url = transport_anonymize_url(dest);
602 fprintf(porcelain ? stdout : stderr, "To %s\n", url);
603 free(url);
606 switch(ref->status) {
607 case REF_STATUS_NONE:
608 print_ref_status('X', "[no match]", ref, NULL, NULL,
609 report, porcelain, summary_width);
610 break;
611 case REF_STATUS_REJECT_NODELETE:
612 print_ref_status('!', "[rejected]", ref, NULL,
613 "remote does not support deleting refs",
614 report, porcelain, summary_width);
615 break;
616 case REF_STATUS_UPTODATE:
617 print_ref_status('=', "[up to date]", ref,
618 ref->peer_ref, NULL,
619 report, porcelain, summary_width);
620 break;
621 case REF_STATUS_REJECT_NONFASTFORWARD:
622 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
623 "non-fast-forward",
624 report, porcelain, summary_width);
625 break;
626 case REF_STATUS_REJECT_ALREADY_EXISTS:
627 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
628 "already exists",
629 report, porcelain, summary_width);
630 break;
631 case REF_STATUS_REJECT_FETCH_FIRST:
632 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
633 "fetch first",
634 report, porcelain, summary_width);
635 break;
636 case REF_STATUS_REJECT_NEEDS_FORCE:
637 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
638 "needs force",
639 report, porcelain, summary_width);
640 break;
641 case REF_STATUS_REJECT_STALE:
642 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
643 "stale info",
644 report, porcelain, summary_width);
645 break;
646 case REF_STATUS_REJECT_REMOTE_UPDATED:
647 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
648 "remote ref updated since checkout",
649 report, porcelain, summary_width);
650 break;
651 case REF_STATUS_REJECT_SHALLOW:
652 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
653 "new shallow roots not allowed",
654 report, porcelain, summary_width);
655 break;
656 case REF_STATUS_REMOTE_REJECT:
657 print_ref_status('!', "[remote rejected]", ref,
658 ref->deletion ? NULL : ref->peer_ref,
659 ref->remote_status,
660 report, porcelain, summary_width);
661 break;
662 case REF_STATUS_EXPECTING_REPORT:
663 print_ref_status('!', "[remote failure]", ref,
664 ref->deletion ? NULL : ref->peer_ref,
665 "remote failed to report status",
666 report, porcelain, summary_width);
667 break;
668 case REF_STATUS_ATOMIC_PUSH_FAILED:
669 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
670 "atomic push failed",
671 report, porcelain, summary_width);
672 break;
673 case REF_STATUS_OK:
674 print_ok_ref_status(ref, report, porcelain, summary_width);
675 break;
678 return 1;
681 static int print_one_push_status(struct ref *ref, const char *dest, int count,
682 int porcelain, int summary_width)
684 struct ref_push_report *report;
685 int n = 0;
687 if (!ref->report)
688 return print_one_push_report(ref, dest, count,
689 NULL, porcelain, summary_width);
691 for (report = ref->report; report; report = report->next)
692 print_one_push_report(ref, dest, count + n++,
693 report, porcelain, summary_width);
694 return n;
697 static int measure_abbrev(const struct object_id *oid, int sofar)
699 char hex[GIT_MAX_HEXSZ + 1];
700 int w = find_unique_abbrev_r(hex, oid, DEFAULT_ABBREV);
702 return (w < sofar) ? sofar : w;
705 int transport_summary_width(const struct ref *refs)
707 int maxw = -1;
709 for (; refs; refs = refs->next) {
710 maxw = measure_abbrev(&refs->old_oid, maxw);
711 maxw = measure_abbrev(&refs->new_oid, maxw);
713 if (maxw < 0)
714 maxw = FALLBACK_DEFAULT_ABBREV;
715 return (2 * maxw + 3);
718 void transport_print_push_status(const char *dest, struct ref *refs,
719 int verbose, int porcelain, unsigned int *reject_reasons)
721 struct ref *ref;
722 int n = 0;
723 char *head;
724 int summary_width = transport_summary_width(refs);
726 if (transport_color_config() < 0)
727 warning(_("could not parse transport.color.* config"));
729 head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
731 if (verbose) {
732 for (ref = refs; ref; ref = ref->next)
733 if (ref->status == REF_STATUS_UPTODATE)
734 n += print_one_push_status(ref, dest, n,
735 porcelain, summary_width);
738 for (ref = refs; ref; ref = ref->next)
739 if (ref->status == REF_STATUS_OK)
740 n += print_one_push_status(ref, dest, n,
741 porcelain, summary_width);
743 *reject_reasons = 0;
744 for (ref = refs; ref; ref = ref->next) {
745 if (ref->status != REF_STATUS_NONE &&
746 ref->status != REF_STATUS_UPTODATE &&
747 ref->status != REF_STATUS_OK)
748 n += print_one_push_status(ref, dest, n,
749 porcelain, summary_width);
750 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
751 if (head != NULL && !strcmp(head, ref->name))
752 *reject_reasons |= REJECT_NON_FF_HEAD;
753 else
754 *reject_reasons |= REJECT_NON_FF_OTHER;
755 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
756 *reject_reasons |= REJECT_ALREADY_EXISTS;
757 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
758 *reject_reasons |= REJECT_FETCH_FIRST;
759 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
760 *reject_reasons |= REJECT_NEEDS_FORCE;
761 } else if (ref->status == REF_STATUS_REJECT_REMOTE_UPDATED) {
762 *reject_reasons |= REJECT_REF_NEEDS_UPDATE;
765 free(head);
768 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
770 struct git_transport_data *data = transport->data;
771 struct send_pack_args args;
772 int ret = 0;
774 if (transport_color_config() < 0)
775 return -1;
777 if (!data->got_remote_heads)
778 get_refs_via_connect(transport, 1, NULL);
780 memset(&args, 0, sizeof(args));
781 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
782 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
783 args.use_thin_pack = data->options.thin;
784 args.verbose = (transport->verbose > 0);
785 args.quiet = (transport->verbose < 0);
786 args.progress = transport->progress;
787 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
788 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
789 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
790 args.push_options = transport->push_options;
791 args.url = transport->url;
793 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
794 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
795 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
796 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
797 else
798 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
800 switch (data->version) {
801 case protocol_v2:
802 die(_("support for protocol v2 not implemented yet"));
803 break;
804 case protocol_v1:
805 case protocol_v0:
806 ret = send_pack(&args, data->fd, data->conn, remote_refs,
807 &data->extra_have);
808 break;
809 case protocol_unknown_version:
810 BUG("unknown protocol version");
813 close(data->fd[1]);
814 close(data->fd[0]);
816 * Atomic push may abort the connection early and close the pipe,
817 * which may cause an error for `finish_connect()`. Ignore this error
818 * for atomic git-push.
820 if (ret || args.atomic)
821 finish_connect(data->conn);
822 else
823 ret = finish_connect(data->conn);
824 data->conn = NULL;
825 data->got_remote_heads = 0;
827 return ret;
830 static int connect_git(struct transport *transport, const char *name,
831 const char *executable, int fd[2])
833 struct git_transport_data *data = transport->data;
834 data->conn = git_connect(data->fd, transport->url,
835 executable, 0);
836 fd[0] = data->fd[0];
837 fd[1] = data->fd[1];
838 return 0;
841 static int disconnect_git(struct transport *transport)
843 struct git_transport_data *data = transport->data;
844 if (data->conn) {
845 if (data->got_remote_heads && !transport->stateless_rpc)
846 packet_flush(data->fd[1]);
847 close(data->fd[0]);
848 close(data->fd[1]);
849 finish_connect(data->conn);
852 free(data);
853 return 0;
856 static struct transport_vtable taken_over_vtable = {
857 NULL,
858 get_refs_via_connect,
859 fetch_refs_via_pack,
860 git_transport_push,
861 NULL,
862 disconnect_git
865 void transport_take_over(struct transport *transport,
866 struct child_process *child)
868 struct git_transport_data *data;
870 if (!transport->smart_options)
871 BUG("taking over transport requires non-NULL "
872 "smart_options field.");
874 data = xcalloc(1, sizeof(*data));
875 data->options = *transport->smart_options;
876 data->conn = child;
877 data->fd[0] = data->conn->out;
878 data->fd[1] = data->conn->in;
879 data->got_remote_heads = 0;
880 transport->data = data;
882 transport->vtable = &taken_over_vtable;
883 transport->smart_options = &(data->options);
885 transport->cannot_reuse = 1;
888 static int is_file(const char *url)
890 struct stat buf;
891 if (stat(url, &buf))
892 return 0;
893 return S_ISREG(buf.st_mode);
896 static int external_specification_len(const char *url)
898 return strchr(url, ':') - url;
901 static const struct string_list *protocol_whitelist(void)
903 static int enabled = -1;
904 static struct string_list allowed = STRING_LIST_INIT_DUP;
906 if (enabled < 0) {
907 const char *v = getenv("GIT_ALLOW_PROTOCOL");
908 if (v) {
909 string_list_split(&allowed, v, ':', -1);
910 string_list_sort(&allowed);
911 enabled = 1;
912 } else {
913 enabled = 0;
917 return enabled ? &allowed : NULL;
920 enum protocol_allow_config {
921 PROTOCOL_ALLOW_NEVER = 0,
922 PROTOCOL_ALLOW_USER_ONLY,
923 PROTOCOL_ALLOW_ALWAYS
926 static enum protocol_allow_config parse_protocol_config(const char *key,
927 const char *value)
929 if (!strcasecmp(value, "always"))
930 return PROTOCOL_ALLOW_ALWAYS;
931 else if (!strcasecmp(value, "never"))
932 return PROTOCOL_ALLOW_NEVER;
933 else if (!strcasecmp(value, "user"))
934 return PROTOCOL_ALLOW_USER_ONLY;
936 die(_("unknown value for config '%s': %s"), key, value);
939 static enum protocol_allow_config get_protocol_config(const char *type)
941 char *key = xstrfmt("protocol.%s.allow", type);
942 char *value;
944 /* first check the per-protocol config */
945 if (!git_config_get_string(key, &value)) {
946 enum protocol_allow_config ret =
947 parse_protocol_config(key, value);
948 free(key);
949 free(value);
950 return ret;
952 free(key);
954 /* if defined, fallback to user-defined default for unknown protocols */
955 if (!git_config_get_string("protocol.allow", &value)) {
956 enum protocol_allow_config ret =
957 parse_protocol_config("protocol.allow", value);
958 free(value);
959 return ret;
962 /* fallback to built-in defaults */
963 /* known safe */
964 if (!strcmp(type, "http") ||
965 !strcmp(type, "https") ||
966 !strcmp(type, "git") ||
967 !strcmp(type, "ssh") ||
968 !strcmp(type, "file"))
969 return PROTOCOL_ALLOW_ALWAYS;
971 /* known scary; err on the side of caution */
972 if (!strcmp(type, "ext"))
973 return PROTOCOL_ALLOW_NEVER;
975 /* unknown; by default let them be used only directly by the user */
976 return PROTOCOL_ALLOW_USER_ONLY;
979 int is_transport_allowed(const char *type, int from_user)
981 const struct string_list *whitelist = protocol_whitelist();
982 if (whitelist)
983 return string_list_has_string(whitelist, type);
985 switch (get_protocol_config(type)) {
986 case PROTOCOL_ALLOW_ALWAYS:
987 return 1;
988 case PROTOCOL_ALLOW_NEVER:
989 return 0;
990 case PROTOCOL_ALLOW_USER_ONLY:
991 if (from_user < 0)
992 from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
993 return from_user;
996 BUG("invalid protocol_allow_config type");
999 void transport_check_allowed(const char *type)
1001 if (!is_transport_allowed(type, -1))
1002 die(_("transport '%s' not allowed"), type);
1005 static struct transport_vtable bundle_vtable = {
1006 NULL,
1007 get_refs_from_bundle,
1008 fetch_refs_from_bundle,
1009 NULL,
1010 NULL,
1011 close_bundle
1014 static struct transport_vtable builtin_smart_vtable = {
1015 NULL,
1016 get_refs_via_connect,
1017 fetch_refs_via_pack,
1018 git_transport_push,
1019 connect_git,
1020 disconnect_git
1023 struct transport *transport_get(struct remote *remote, const char *url)
1025 const char *helper;
1026 struct transport *ret = xcalloc(1, sizeof(*ret));
1028 ret->progress = isatty(2);
1029 string_list_init(&ret->pack_lockfiles, 1);
1031 if (!remote)
1032 BUG("No remote provided to transport_get()");
1034 ret->got_remote_refs = 0;
1035 ret->remote = remote;
1036 helper = remote->foreign_vcs;
1038 if (!url && remote->url)
1039 url = remote->url[0];
1040 ret->url = url;
1042 /* maybe it is a foreign URL? */
1043 if (url) {
1044 const char *p = url;
1046 while (is_urlschemechar(p == url, *p))
1047 p++;
1048 if (starts_with(p, "::"))
1049 helper = xstrndup(url, p - url);
1052 if (helper) {
1053 transport_helper_init(ret, helper);
1054 } else if (starts_with(url, "rsync:")) {
1055 die(_("git-over-rsync is no longer supported"));
1056 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
1057 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
1058 transport_check_allowed("file");
1059 ret->data = data;
1060 ret->vtable = &bundle_vtable;
1061 ret->smart_options = NULL;
1062 } else if (!is_url(url)
1063 || starts_with(url, "file://")
1064 || starts_with(url, "git://")
1065 || starts_with(url, "ssh://")
1066 || starts_with(url, "git+ssh://") /* deprecated - do not use */
1067 || starts_with(url, "ssh+git://") /* deprecated - do not use */
1070 * These are builtin smart transports; "allowed" transports
1071 * will be checked individually in git_connect.
1073 struct git_transport_data *data = xcalloc(1, sizeof(*data));
1074 ret->data = data;
1075 ret->vtable = &builtin_smart_vtable;
1076 ret->smart_options = &(data->options);
1078 data->conn = NULL;
1079 data->got_remote_heads = 0;
1080 } else {
1081 /* Unknown protocol in URL. Pass to external handler. */
1082 int len = external_specification_len(url);
1083 char *handler = xmemdupz(url, len);
1084 transport_helper_init(ret, handler);
1087 if (ret->smart_options) {
1088 ret->smart_options->thin = 1;
1089 ret->smart_options->uploadpack = "git-upload-pack";
1090 if (remote->uploadpack)
1091 ret->smart_options->uploadpack = remote->uploadpack;
1092 ret->smart_options->receivepack = "git-receive-pack";
1093 if (remote->receivepack)
1094 ret->smart_options->receivepack = remote->receivepack;
1097 ret->hash_algo = &hash_algos[GIT_HASH_SHA1];
1099 return ret;
1102 const struct git_hash_algo *transport_get_hash_algo(struct transport *transport)
1104 return transport->hash_algo;
1107 int transport_set_option(struct transport *transport,
1108 const char *name, const char *value)
1110 int git_reports = 1, protocol_reports = 1;
1112 if (transport->smart_options)
1113 git_reports = set_git_option(transport->smart_options,
1114 name, value);
1116 if (transport->vtable->set_option)
1117 protocol_reports = transport->vtable->set_option(transport,
1118 name, value);
1120 /* If either report is 0, report 0 (success). */
1121 if (!git_reports || !protocol_reports)
1122 return 0;
1123 /* If either reports -1 (invalid value), report -1. */
1124 if ((git_reports == -1) || (protocol_reports == -1))
1125 return -1;
1126 /* Otherwise if both report unknown, report unknown. */
1127 return 1;
1130 void transport_set_verbosity(struct transport *transport, int verbosity,
1131 int force_progress)
1133 if (verbosity >= 1)
1134 transport->verbose = verbosity <= 3 ? verbosity : 3;
1135 if (verbosity < 0)
1136 transport->verbose = -1;
1139 * Rules used to determine whether to report progress (processing aborts
1140 * when a rule is satisfied):
1142 * . Report progress, if force_progress is 1 (ie. --progress).
1143 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1144 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1145 * . Report progress if isatty(2) is 1.
1147 if (force_progress >= 0)
1148 transport->progress = !!force_progress;
1149 else
1150 transport->progress = verbosity >= 0 && isatty(2);
1153 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
1155 int i;
1157 fprintf(stderr, _("The following submodule paths contain changes that can\n"
1158 "not be found on any remote:\n"));
1159 for (i = 0; i < needs_pushing->nr; i++)
1160 fprintf(stderr, " %s\n", needs_pushing->items[i].string);
1161 fprintf(stderr, _("\nPlease try\n\n"
1162 " git push --recurse-submodules=on-demand\n\n"
1163 "or cd to the path and use\n\n"
1164 " git push\n\n"
1165 "to push them to a remote.\n\n"));
1167 string_list_clear(needs_pushing, 0);
1169 die(_("Aborting."));
1172 static int run_pre_push_hook(struct transport *transport,
1173 struct ref *remote_refs)
1175 int ret = 0, x;
1176 struct ref *r;
1177 struct child_process proc = CHILD_PROCESS_INIT;
1178 struct strbuf buf;
1179 const char *argv[4];
1181 if (!(argv[0] = find_hook("pre-push")))
1182 return 0;
1184 argv[1] = transport->remote->name;
1185 argv[2] = transport->url;
1186 argv[3] = NULL;
1188 proc.argv = argv;
1189 proc.in = -1;
1190 proc.trace2_hook_name = "pre-push";
1192 if (start_command(&proc)) {
1193 finish_command(&proc);
1194 return -1;
1197 sigchain_push(SIGPIPE, SIG_IGN);
1199 strbuf_init(&buf, 256);
1201 for (r = remote_refs; r; r = r->next) {
1202 if (!r->peer_ref) continue;
1203 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
1204 if (r->status == REF_STATUS_REJECT_STALE) continue;
1205 if (r->status == REF_STATUS_REJECT_REMOTE_UPDATED) continue;
1206 if (r->status == REF_STATUS_UPTODATE) continue;
1208 strbuf_reset(&buf);
1209 strbuf_addf( &buf, "%s %s %s %s\n",
1210 r->peer_ref->name, oid_to_hex(&r->new_oid),
1211 r->name, oid_to_hex(&r->old_oid));
1213 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
1214 /* We do not mind if a hook does not read all refs. */
1215 if (errno != EPIPE)
1216 ret = -1;
1217 break;
1221 strbuf_release(&buf);
1223 x = close(proc.in);
1224 if (!ret)
1225 ret = x;
1227 sigchain_pop(SIGPIPE);
1229 x = finish_command(&proc);
1230 if (!ret)
1231 ret = x;
1233 return ret;
1236 int transport_push(struct repository *r,
1237 struct transport *transport,
1238 struct refspec *rs, int flags,
1239 unsigned int *reject_reasons)
1241 *reject_reasons = 0;
1243 if (transport_color_config() < 0)
1244 return -1;
1246 if (transport->vtable->push_refs) {
1247 struct ref *remote_refs;
1248 struct ref *local_refs = get_local_heads();
1249 int match_flags = MATCH_REFS_NONE;
1250 int verbose = (transport->verbose > 0);
1251 int quiet = (transport->verbose < 0);
1252 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1253 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1254 int push_ret, ret, err;
1255 struct transport_ls_refs_options transport_options =
1256 TRANSPORT_LS_REFS_OPTIONS_INIT;
1258 if (check_push_refs(local_refs, rs) < 0)
1259 return -1;
1261 refspec_ref_prefixes(rs, &transport_options.ref_prefixes);
1263 trace2_region_enter("transport_push", "get_refs_list", r);
1264 remote_refs = transport->vtable->get_refs_list(transport, 1,
1265 &transport_options);
1266 trace2_region_leave("transport_push", "get_refs_list", r);
1268 strvec_clear(&transport_options.ref_prefixes);
1270 if (flags & TRANSPORT_PUSH_ALL)
1271 match_flags |= MATCH_REFS_ALL;
1272 if (flags & TRANSPORT_PUSH_MIRROR)
1273 match_flags |= MATCH_REFS_MIRROR;
1274 if (flags & TRANSPORT_PUSH_PRUNE)
1275 match_flags |= MATCH_REFS_PRUNE;
1276 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1277 match_flags |= MATCH_REFS_FOLLOW_TAGS;
1279 if (match_push_refs(local_refs, &remote_refs, rs, match_flags))
1280 return -1;
1282 if (transport->smart_options &&
1283 transport->smart_options->cas &&
1284 !is_empty_cas(transport->smart_options->cas))
1285 apply_push_cas(transport->smart_options->cas,
1286 transport->remote, remote_refs);
1288 set_ref_status_for_push(remote_refs,
1289 flags & TRANSPORT_PUSH_MIRROR,
1290 flags & TRANSPORT_PUSH_FORCE);
1292 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1293 if (run_pre_push_hook(transport, remote_refs))
1294 return -1;
1296 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1297 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1298 !is_bare_repository()) {
1299 struct ref *ref = remote_refs;
1300 struct oid_array commits = OID_ARRAY_INIT;
1302 trace2_region_enter("transport_push", "push_submodules", r);
1303 for (; ref; ref = ref->next)
1304 if (!is_null_oid(&ref->new_oid))
1305 oid_array_append(&commits,
1306 &ref->new_oid);
1308 if (!push_unpushed_submodules(r,
1309 &commits,
1310 transport->remote,
1312 transport->push_options,
1313 pretend)) {
1314 oid_array_clear(&commits);
1315 trace2_region_leave("transport_push", "push_submodules", r);
1316 die(_("failed to push all needed submodules"));
1318 oid_array_clear(&commits);
1319 trace2_region_leave("transport_push", "push_submodules", r);
1322 if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1323 ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1324 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1325 !pretend)) && !is_bare_repository()) {
1326 struct ref *ref = remote_refs;
1327 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1328 struct oid_array commits = OID_ARRAY_INIT;
1330 trace2_region_enter("transport_push", "check_submodules", r);
1331 for (; ref; ref = ref->next)
1332 if (!is_null_oid(&ref->new_oid))
1333 oid_array_append(&commits,
1334 &ref->new_oid);
1336 if (find_unpushed_submodules(r,
1337 &commits,
1338 transport->remote->name,
1339 &needs_pushing)) {
1340 oid_array_clear(&commits);
1341 trace2_region_leave("transport_push", "check_submodules", r);
1342 die_with_unpushed_submodules(&needs_pushing);
1344 string_list_clear(&needs_pushing, 0);
1345 oid_array_clear(&commits);
1346 trace2_region_leave("transport_push", "check_submodules", r);
1349 if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY)) {
1350 trace2_region_enter("transport_push", "push_refs", r);
1351 push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1352 trace2_region_leave("transport_push", "push_refs", r);
1353 } else
1354 push_ret = 0;
1355 err = push_had_errors(remote_refs);
1356 ret = push_ret | err;
1358 if (!quiet || err)
1359 transport_print_push_status(transport->url, remote_refs,
1360 verbose | porcelain, porcelain,
1361 reject_reasons);
1363 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1364 set_upstreams(transport, remote_refs, pretend);
1366 if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1367 TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1368 struct ref *ref;
1369 for (ref = remote_refs; ref; ref = ref->next)
1370 transport_update_tracking_ref(transport->remote, ref, verbose);
1373 if (porcelain && !push_ret)
1374 puts("Done");
1375 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1376 fprintf(stderr, "Everything up-to-date\n");
1378 return ret;
1380 return 1;
1383 const struct ref *transport_get_remote_refs(struct transport *transport,
1384 struct transport_ls_refs_options *transport_options)
1386 if (!transport->got_remote_refs) {
1387 transport->remote_refs =
1388 transport->vtable->get_refs_list(transport, 0,
1389 transport_options);
1390 transport->got_remote_refs = 1;
1393 return transport->remote_refs;
1396 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1398 int rc;
1399 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1400 struct ref **heads = NULL;
1401 struct ref *rm;
1403 for (rm = refs; rm; rm = rm->next) {
1404 nr_refs++;
1405 if (rm->peer_ref &&
1406 !is_null_oid(&rm->old_oid) &&
1407 oideq(&rm->peer_ref->old_oid, &rm->old_oid))
1408 continue;
1409 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1410 heads[nr_heads++] = rm;
1413 if (!nr_heads) {
1415 * When deepening of a shallow repository is requested,
1416 * then local and remote refs are likely to still be equal.
1417 * Just feed them all to the fetch method in that case.
1418 * This condition shouldn't be met in a non-deepening fetch
1419 * (see builtin/fetch.c:quickfetch()).
1421 ALLOC_ARRAY(heads, nr_refs);
1422 for (rm = refs; rm; rm = rm->next)
1423 heads[nr_heads++] = rm;
1426 rc = transport->vtable->fetch(transport, nr_heads, heads);
1428 free(heads);
1429 return rc;
1432 void transport_unlock_pack(struct transport *transport)
1434 int i;
1436 for (i = 0; i < transport->pack_lockfiles.nr; i++)
1437 unlink_or_warn(transport->pack_lockfiles.items[i].string);
1438 string_list_clear(&transport->pack_lockfiles, 0);
1441 int transport_connect(struct transport *transport, const char *name,
1442 const char *exec, int fd[2])
1444 if (transport->vtable->connect)
1445 return transport->vtable->connect(transport, name, exec, fd);
1446 else
1447 die(_("operation not supported by protocol"));
1450 int transport_disconnect(struct transport *transport)
1452 int ret = 0;
1453 if (transport->vtable->disconnect)
1454 ret = transport->vtable->disconnect(transport);
1455 free(transport);
1456 return ret;
1460 * Strip username (and password) from a URL and return
1461 * it in a newly allocated string.
1463 char *transport_anonymize_url(const char *url)
1465 char *scheme_prefix, *anon_part;
1466 size_t anon_len, prefix_len = 0;
1468 anon_part = strchr(url, '@');
1469 if (url_is_local_not_ssh(url) || !anon_part)
1470 goto literal_copy;
1472 anon_len = strlen(++anon_part);
1473 scheme_prefix = strstr(url, "://");
1474 if (!scheme_prefix) {
1475 if (!strchr(anon_part, ':'))
1476 /* cannot be "me@there:/path/name" */
1477 goto literal_copy;
1478 } else {
1479 const char *cp;
1480 /* make sure scheme is reasonable */
1481 for (cp = url; cp < scheme_prefix; cp++) {
1482 switch (*cp) {
1483 /* RFC 1738 2.1 */
1484 case '+': case '.': case '-':
1485 break; /* ok */
1486 default:
1487 if (isalnum(*cp))
1488 break;
1489 /* it isn't */
1490 goto literal_copy;
1493 /* @ past the first slash does not count */
1494 cp = strchr(scheme_prefix + 3, '/');
1495 if (cp && cp < anon_part)
1496 goto literal_copy;
1497 prefix_len = scheme_prefix - url + 3;
1499 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1500 (int)anon_len, anon_part);
1501 literal_copy:
1502 return xstrdup(url);