]> git.ipfire.org Git - thirdparty/git.git/blob - transport.c
treewide: remove unnecessary includes of cache.h
[thirdparty/git.git] / transport.c
1 #include "cache.h"
2 #include "alloc.h"
3 #include "config.h"
4 #include "hex.h"
5 #include "transport.h"
6 #include "hook.h"
7 #include "pkt-line.h"
8 #include "fetch-pack.h"
9 #include "remote.h"
10 #include "connect.h"
11 #include "send-pack.h"
12 #include "walker.h"
13 #include "bundle.h"
14 #include "dir.h"
15 #include "gettext.h"
16 #include "refs.h"
17 #include "refspec.h"
18 #include "branch.h"
19 #include "url.h"
20 #include "submodule.h"
21 #include "string-list.h"
22 #include "oid-array.h"
23 #include "sigchain.h"
24 #include "transport-internal.h"
25 #include "protocol.h"
26 #include "object-store.h"
27 #include "color.h"
28 #include "bundle-uri.h"
29 #include "wrapper.h"
30
31 static int transport_use_color = -1;
32 static char transport_colors[][COLOR_MAXLEN] = {
33 GIT_COLOR_RESET,
34 GIT_COLOR_RED /* REJECTED */
35 };
36
37 enum color_transport {
38 TRANSPORT_COLOR_RESET = 0,
39 TRANSPORT_COLOR_REJECTED = 1
40 };
41
42 static int transport_color_config(void)
43 {
44 const char *keys[] = {
45 "color.transport.reset",
46 "color.transport.rejected"
47 }, *key = "color.transport";
48 char *value;
49 int i;
50 static int initialized;
51
52 if (initialized)
53 return 0;
54 initialized = 1;
55
56 if (!git_config_get_string(key, &value))
57 transport_use_color = git_config_colorbool(key, value);
58
59 if (!want_color_stderr(transport_use_color))
60 return 0;
61
62 for (i = 0; i < ARRAY_SIZE(keys); i++)
63 if (!git_config_get_string(keys[i], &value)) {
64 if (!value)
65 return config_error_nonbool(keys[i]);
66 if (color_parse(value, transport_colors[i]) < 0)
67 return -1;
68 }
69
70 return 0;
71 }
72
73 static const char *transport_get_color(enum color_transport ix)
74 {
75 if (want_color_stderr(transport_use_color))
76 return transport_colors[ix];
77 return "";
78 }
79
80 static void set_upstreams(struct transport *transport, struct ref *refs,
81 int pretend)
82 {
83 struct ref *ref;
84 for (ref = refs; ref; ref = ref->next) {
85 const char *localname;
86 const char *tmp;
87 const char *remotename;
88 int flag = 0;
89 /*
90 * Check suitability for tracking. Must be successful /
91 * already up-to-date ref create/modify (not delete).
92 */
93 if (ref->status != REF_STATUS_OK &&
94 ref->status != REF_STATUS_UPTODATE)
95 continue;
96 if (!ref->peer_ref)
97 continue;
98 if (is_null_oid(&ref->new_oid))
99 continue;
100
101 /* Follow symbolic refs (mainly for HEAD). */
102 localname = ref->peer_ref->name;
103 remotename = ref->name;
104 tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING,
105 NULL, &flag);
106 if (tmp && flag & REF_ISSYMREF &&
107 starts_with(tmp, "refs/heads/"))
108 localname = tmp;
109
110 /* Both source and destination must be local branches. */
111 if (!localname || !starts_with(localname, "refs/heads/"))
112 continue;
113 if (!remotename || !starts_with(remotename, "refs/heads/"))
114 continue;
115
116 if (!pretend) {
117 int flag = transport->verbose < 0 ? 0 : BRANCH_CONFIG_VERBOSE;
118 install_branch_config(flag, localname + 11,
119 transport->remote->name, remotename);
120 } else if (transport->verbose >= 0)
121 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
122 localname + 11, remotename + 11,
123 transport->remote->name);
124 }
125 }
126
127 struct bundle_transport_data {
128 int fd;
129 struct bundle_header header;
130 unsigned get_refs_from_bundle_called : 1;
131 };
132
133 static void get_refs_from_bundle_inner(struct transport *transport)
134 {
135 struct bundle_transport_data *data = transport->data;
136
137 data->get_refs_from_bundle_called = 1;
138
139 if (data->fd > 0)
140 close(data->fd);
141 data->fd = read_bundle_header(transport->url, &data->header);
142 if (data->fd < 0)
143 die(_("could not read bundle '%s'"), transport->url);
144
145 transport->hash_algo = data->header.hash_algo;
146 }
147
148 static struct ref *get_refs_from_bundle(struct transport *transport,
149 int for_push,
150 struct transport_ls_refs_options *transport_options UNUSED)
151 {
152 struct bundle_transport_data *data = transport->data;
153 struct ref *result = NULL;
154 int i;
155
156 if (for_push)
157 return NULL;
158
159 get_refs_from_bundle_inner(transport);
160
161 for (i = 0; i < data->header.references.nr; i++) {
162 struct string_list_item *e = data->header.references.items + i;
163 const char *name = e->string;
164 struct ref *ref = alloc_ref(name);
165 struct object_id *oid = e->util;
166 oidcpy(&ref->old_oid, oid);
167 ref->next = result;
168 result = ref;
169 }
170 return result;
171 }
172
173 static int fetch_refs_from_bundle(struct transport *transport,
174 int nr_heads, struct ref **to_fetch)
175 {
176 struct bundle_transport_data *data = transport->data;
177 struct strvec extra_index_pack_args = STRVEC_INIT;
178 int ret;
179
180 if (transport->progress)
181 strvec_push(&extra_index_pack_args, "-v");
182
183 if (!data->get_refs_from_bundle_called)
184 get_refs_from_bundle_inner(transport);
185 ret = unbundle(the_repository, &data->header, data->fd,
186 &extra_index_pack_args, 0);
187 transport->hash_algo = data->header.hash_algo;
188 return ret;
189 }
190
191 static int close_bundle(struct transport *transport)
192 {
193 struct bundle_transport_data *data = transport->data;
194 if (data->fd > 0)
195 close(data->fd);
196 bundle_header_release(&data->header);
197 free(data);
198 return 0;
199 }
200
201 struct git_transport_data {
202 struct git_transport_options options;
203 struct child_process *conn;
204 int fd[2];
205 unsigned finished_handshake : 1;
206 enum protocol_version version;
207 struct oid_array extra_have;
208 struct oid_array shallow;
209 };
210
211 static int set_git_option(struct git_transport_options *opts,
212 const char *name, const char *value)
213 {
214 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
215 opts->uploadpack = value;
216 return 0;
217 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
218 opts->receivepack = value;
219 return 0;
220 } else if (!strcmp(name, TRANS_OPT_THIN)) {
221 opts->thin = !!value;
222 return 0;
223 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
224 opts->followtags = !!value;
225 return 0;
226 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
227 opts->keep = !!value;
228 return 0;
229 } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
230 opts->update_shallow = !!value;
231 return 0;
232 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
233 if (!value)
234 opts->depth = 0;
235 else {
236 char *end;
237 opts->depth = strtol(value, &end, 0);
238 if (*end)
239 die(_("transport: invalid depth option '%s'"), value);
240 }
241 return 0;
242 } else if (!strcmp(name, TRANS_OPT_DEEPEN_SINCE)) {
243 opts->deepen_since = value;
244 return 0;
245 } else if (!strcmp(name, TRANS_OPT_DEEPEN_NOT)) {
246 opts->deepen_not = (const struct string_list *)value;
247 return 0;
248 } else if (!strcmp(name, TRANS_OPT_DEEPEN_RELATIVE)) {
249 opts->deepen_relative = !!value;
250 return 0;
251 } else if (!strcmp(name, TRANS_OPT_FROM_PROMISOR)) {
252 opts->from_promisor = !!value;
253 return 0;
254 } else if (!strcmp(name, TRANS_OPT_LIST_OBJECTS_FILTER)) {
255 list_objects_filter_die_if_populated(&opts->filter_options);
256 parse_list_objects_filter(&opts->filter_options, value);
257 return 0;
258 } else if (!strcmp(name, TRANS_OPT_REFETCH)) {
259 opts->refetch = !!value;
260 return 0;
261 } else if (!strcmp(name, TRANS_OPT_REJECT_SHALLOW)) {
262 opts->reject_shallow = !!value;
263 return 0;
264 }
265 return 1;
266 }
267
268 static int connect_setup(struct transport *transport, int for_push)
269 {
270 struct git_transport_data *data = transport->data;
271 int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
272
273 if (data->conn)
274 return 0;
275
276 switch (transport->family) {
277 case TRANSPORT_FAMILY_ALL: break;
278 case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
279 case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
280 }
281
282 data->conn = git_connect(data->fd, transport->url,
283 for_push ? data->options.receivepack :
284 data->options.uploadpack,
285 flags);
286
287 return 0;
288 }
289
290 static void die_if_server_options(struct transport *transport)
291 {
292 if (!transport->server_options || !transport->server_options->nr)
293 return;
294 advise(_("see protocol.version in 'git help config' for more details"));
295 die(_("server options require protocol version 2 or later"));
296 }
297
298 /*
299 * Obtains the protocol version from the transport and writes it to
300 * transport->data->version, first connecting if not already connected.
301 *
302 * If the protocol version is one that allows skipping the listing of remote
303 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
304 * this function returns NULL. Otherwise, this function returns the list of
305 * remote refs.
306 */
307 static struct ref *handshake(struct transport *transport, int for_push,
308 struct transport_ls_refs_options *options,
309 int must_list_refs)
310 {
311 struct git_transport_data *data = transport->data;
312 struct ref *refs = NULL;
313 struct packet_reader reader;
314 int sid_len;
315 const char *server_sid;
316
317 connect_setup(transport, for_push);
318
319 packet_reader_init(&reader, data->fd[0], NULL, 0,
320 PACKET_READ_CHOMP_NEWLINE |
321 PACKET_READ_GENTLE_ON_EOF |
322 PACKET_READ_DIE_ON_ERR_PACKET);
323
324 data->version = discover_version(&reader);
325 switch (data->version) {
326 case protocol_v2:
327 if (server_feature_v2("session-id", &server_sid))
328 trace2_data_string("transfer", NULL, "server-sid", server_sid);
329 if (must_list_refs)
330 get_remote_refs(data->fd[1], &reader, &refs, for_push,
331 options,
332 transport->server_options,
333 transport->stateless_rpc);
334 break;
335 case protocol_v1:
336 case protocol_v0:
337 die_if_server_options(transport);
338 get_remote_heads(&reader, &refs,
339 for_push ? REF_NORMAL : 0,
340 &data->extra_have,
341 &data->shallow);
342 server_sid = server_feature_value("session-id", &sid_len);
343 if (server_sid) {
344 char *sid = xstrndup(server_sid, sid_len);
345 trace2_data_string("transfer", NULL, "server-sid", sid);
346 free(sid);
347 }
348 break;
349 case protocol_unknown_version:
350 BUG("unknown protocol version");
351 }
352 data->finished_handshake = 1;
353 transport->hash_algo = reader.hash_algo;
354
355 if (reader.line_peeked)
356 BUG("buffer must be empty at the end of handshake()");
357
358 return refs;
359 }
360
361 static struct ref *get_refs_via_connect(struct transport *transport, int for_push,
362 struct transport_ls_refs_options *options)
363 {
364 return handshake(transport, for_push, options, 1);
365 }
366
367 static int get_bundle_uri(struct transport *transport)
368 {
369 struct git_transport_data *data = transport->data;
370 struct packet_reader reader;
371 int stateless_rpc = transport->stateless_rpc;
372
373 if (!transport->bundles) {
374 CALLOC_ARRAY(transport->bundles, 1);
375 init_bundle_list(transport->bundles);
376 }
377
378 if (!data->finished_handshake) {
379 struct ref *refs = handshake(transport, 0, NULL, 0);
380
381 if (refs)
382 free_refs(refs);
383 }
384
385 /*
386 * "Support" protocol v0 and v2 without bundle-uri support by
387 * silently degrading to a NOOP.
388 */
389 if (!server_supports_v2("bundle-uri"))
390 return 0;
391
392 packet_reader_init(&reader, data->fd[0], NULL, 0,
393 PACKET_READ_CHOMP_NEWLINE |
394 PACKET_READ_GENTLE_ON_EOF);
395
396 return get_remote_bundle_uri(data->fd[1], &reader,
397 transport->bundles, stateless_rpc);
398 }
399
400 static int fetch_refs_via_pack(struct transport *transport,
401 int nr_heads, struct ref **to_fetch)
402 {
403 int ret = 0;
404 struct git_transport_data *data = transport->data;
405 struct ref *refs = NULL;
406 struct fetch_pack_args args;
407 struct ref *refs_tmp = NULL;
408
409 memset(&args, 0, sizeof(args));
410 args.uploadpack = data->options.uploadpack;
411 args.keep_pack = data->options.keep;
412 args.lock_pack = 1;
413 args.use_thin_pack = data->options.thin;
414 args.include_tag = data->options.followtags;
415 args.verbose = (transport->verbose > 1);
416 args.quiet = (transport->verbose < 0);
417 args.no_progress = !transport->progress;
418 args.depth = data->options.depth;
419 args.deepen_since = data->options.deepen_since;
420 args.deepen_not = data->options.deepen_not;
421 args.deepen_relative = data->options.deepen_relative;
422 args.check_self_contained_and_connected =
423 data->options.check_self_contained_and_connected;
424 args.cloning = transport->cloning;
425 args.update_shallow = data->options.update_shallow;
426 args.from_promisor = data->options.from_promisor;
427 list_objects_filter_copy(&args.filter_options,
428 &data->options.filter_options);
429 args.refetch = data->options.refetch;
430 args.stateless_rpc = transport->stateless_rpc;
431 args.server_options = transport->server_options;
432 args.negotiation_tips = data->options.negotiation_tips;
433 args.reject_shallow_remote = transport->smart_options->reject_shallow;
434
435 if (!data->finished_handshake) {
436 int i;
437 int must_list_refs = 0;
438 for (i = 0; i < nr_heads; i++) {
439 if (!to_fetch[i]->exact_oid) {
440 must_list_refs = 1;
441 break;
442 }
443 }
444 refs_tmp = handshake(transport, 0, NULL, must_list_refs);
445 }
446
447 if (data->version == protocol_unknown_version)
448 BUG("unknown protocol version");
449 else if (data->version <= protocol_v1)
450 die_if_server_options(transport);
451
452 if (data->options.acked_commits) {
453 if (data->version < protocol_v2) {
454 warning(_("--negotiate-only requires protocol v2"));
455 ret = -1;
456 } else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
457 warning(_("server does not support wait-for-done"));
458 ret = -1;
459 } else {
460 negotiate_using_fetch(data->options.negotiation_tips,
461 transport->server_options,
462 transport->stateless_rpc,
463 data->fd,
464 data->options.acked_commits);
465 ret = 0;
466 }
467 goto cleanup;
468 }
469
470 refs = fetch_pack(&args, data->fd,
471 refs_tmp ? refs_tmp : transport->remote_refs,
472 to_fetch, nr_heads, &data->shallow,
473 &transport->pack_lockfiles, data->version);
474
475 data->finished_handshake = 0;
476 data->options.self_contained_and_connected =
477 args.self_contained_and_connected;
478 data->options.connectivity_checked = args.connectivity_checked;
479
480 if (!refs)
481 ret = -1;
482 if (report_unmatched_refs(to_fetch, nr_heads))
483 ret = -1;
484
485 cleanup:
486 close(data->fd[0]);
487 if (data->fd[1] >= 0)
488 close(data->fd[1]);
489 if (finish_connect(data->conn))
490 ret = -1;
491 data->conn = NULL;
492
493 free_refs(refs_tmp);
494 free_refs(refs);
495 list_objects_filter_release(&args.filter_options);
496 return ret;
497 }
498
499 static int push_had_errors(struct ref *ref)
500 {
501 for (; ref; ref = ref->next) {
502 switch (ref->status) {
503 case REF_STATUS_NONE:
504 case REF_STATUS_UPTODATE:
505 case REF_STATUS_OK:
506 break;
507 default:
508 return 1;
509 }
510 }
511 return 0;
512 }
513
514 int transport_refs_pushed(struct ref *ref)
515 {
516 for (; ref; ref = ref->next) {
517 switch(ref->status) {
518 case REF_STATUS_NONE:
519 case REF_STATUS_UPTODATE:
520 break;
521 default:
522 return 1;
523 }
524 }
525 return 0;
526 }
527
528 static void update_one_tracking_ref(struct remote *remote, char *refname,
529 struct object_id *new_oid, int deletion,
530 int verbose)
531 {
532 struct refspec_item rs;
533
534 memset(&rs, 0, sizeof(rs));
535 rs.src = refname;
536 rs.dst = NULL;
537
538 if (!remote_find_tracking(remote, &rs)) {
539 if (verbose)
540 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
541 if (deletion)
542 delete_ref(NULL, rs.dst, NULL, 0);
543 else
544 update_ref("update by push", rs.dst, new_oid,
545 NULL, 0, 0);
546 free(rs.dst);
547 }
548 }
549
550 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
551 {
552 char *refname;
553 struct object_id *new_oid;
554 struct ref_push_report *report;
555
556 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
557 return;
558
559 report = ref->report;
560 if (!report)
561 update_one_tracking_ref(remote, ref->name, &ref->new_oid,
562 ref->deletion, verbose);
563 else
564 for (; report; report = report->next) {
565 refname = report->ref_name ? (char *)report->ref_name : ref->name;
566 new_oid = report->new_oid ? report->new_oid : &ref->new_oid;
567 update_one_tracking_ref(remote, refname, new_oid,
568 is_null_oid(new_oid), verbose);
569 }
570 }
571
572 static void print_ref_status(char flag, const char *summary,
573 struct ref *to, struct ref *from, const char *msg,
574 struct ref_push_report *report,
575 int porcelain, int summary_width)
576 {
577 const char *to_name;
578
579 if (report && report->ref_name)
580 to_name = report->ref_name;
581 else
582 to_name = to->name;
583
584 if (porcelain) {
585 if (from)
586 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to_name);
587 else
588 fprintf(stdout, "%c\t:%s\t", flag, to_name);
589 if (msg)
590 fprintf(stdout, "%s (%s)\n", summary, msg);
591 else
592 fprintf(stdout, "%s\n", summary);
593 } else {
594 const char *red = "", *reset = "";
595 if (push_had_errors(to)) {
596 red = transport_get_color(TRANSPORT_COLOR_REJECTED);
597 reset = transport_get_color(TRANSPORT_COLOR_RESET);
598 }
599 fprintf(stderr, " %s%c %-*s%s ", red, flag, summary_width,
600 summary, reset);
601 if (from)
602 fprintf(stderr, "%s -> %s",
603 prettify_refname(from->name),
604 prettify_refname(to_name));
605 else
606 fputs(prettify_refname(to_name), stderr);
607 if (msg) {
608 fputs(" (", stderr);
609 fputs(msg, stderr);
610 fputc(')', stderr);
611 }
612 fputc('\n', stderr);
613 }
614 }
615
616 static void print_ok_ref_status(struct ref *ref,
617 struct ref_push_report *report,
618 int porcelain, int summary_width)
619 {
620 struct object_id *old_oid;
621 struct object_id *new_oid;
622 const char *ref_name;
623 int forced_update;
624
625 if (report && report->old_oid)
626 old_oid = report->old_oid;
627 else
628 old_oid = &ref->old_oid;
629 if (report && report->new_oid)
630 new_oid = report->new_oid;
631 else
632 new_oid = &ref->new_oid;
633 if (report && report->forced_update)
634 forced_update = report->forced_update;
635 else
636 forced_update = ref->forced_update;
637 if (report && report->ref_name)
638 ref_name = report->ref_name;
639 else
640 ref_name = ref->name;
641
642 if (ref->deletion)
643 print_ref_status('-', "[deleted]", ref, NULL, NULL,
644 report, porcelain, summary_width);
645 else if (is_null_oid(old_oid))
646 print_ref_status('*',
647 (starts_with(ref_name, "refs/tags/")
648 ? "[new tag]"
649 : (starts_with(ref_name, "refs/heads/")
650 ? "[new branch]"
651 : "[new reference]")),
652 ref, ref->peer_ref, NULL,
653 report, porcelain, summary_width);
654 else {
655 struct strbuf quickref = STRBUF_INIT;
656 char type;
657 const char *msg;
658
659 strbuf_add_unique_abbrev(&quickref, old_oid,
660 DEFAULT_ABBREV);
661 if (forced_update) {
662 strbuf_addstr(&quickref, "...");
663 type = '+';
664 msg = "forced update";
665 } else {
666 strbuf_addstr(&quickref, "..");
667 type = ' ';
668 msg = NULL;
669 }
670 strbuf_add_unique_abbrev(&quickref, new_oid,
671 DEFAULT_ABBREV);
672
673 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
674 report, porcelain, summary_width);
675 strbuf_release(&quickref);
676 }
677 }
678
679 static int print_one_push_report(struct ref *ref, const char *dest, int count,
680 struct ref_push_report *report,
681 int porcelain, int summary_width)
682 {
683 if (!count) {
684 char *url = transport_anonymize_url(dest);
685 fprintf(porcelain ? stdout : stderr, "To %s\n", url);
686 free(url);
687 }
688
689 switch(ref->status) {
690 case REF_STATUS_NONE:
691 print_ref_status('X', "[no match]", ref, NULL, NULL,
692 report, porcelain, summary_width);
693 break;
694 case REF_STATUS_REJECT_NODELETE:
695 print_ref_status('!', "[rejected]", ref, NULL,
696 "remote does not support deleting refs",
697 report, porcelain, summary_width);
698 break;
699 case REF_STATUS_UPTODATE:
700 print_ref_status('=', "[up to date]", ref,
701 ref->peer_ref, NULL,
702 report, porcelain, summary_width);
703 break;
704 case REF_STATUS_REJECT_NONFASTFORWARD:
705 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
706 "non-fast-forward",
707 report, porcelain, summary_width);
708 break;
709 case REF_STATUS_REJECT_ALREADY_EXISTS:
710 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
711 "already exists",
712 report, porcelain, summary_width);
713 break;
714 case REF_STATUS_REJECT_FETCH_FIRST:
715 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
716 "fetch first",
717 report, porcelain, summary_width);
718 break;
719 case REF_STATUS_REJECT_NEEDS_FORCE:
720 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
721 "needs force",
722 report, porcelain, summary_width);
723 break;
724 case REF_STATUS_REJECT_STALE:
725 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
726 "stale info",
727 report, porcelain, summary_width);
728 break;
729 case REF_STATUS_REJECT_REMOTE_UPDATED:
730 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
731 "remote ref updated since checkout",
732 report, porcelain, summary_width);
733 break;
734 case REF_STATUS_REJECT_SHALLOW:
735 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
736 "new shallow roots not allowed",
737 report, porcelain, summary_width);
738 break;
739 case REF_STATUS_REMOTE_REJECT:
740 print_ref_status('!', "[remote rejected]", ref,
741 ref->deletion ? NULL : ref->peer_ref,
742 ref->remote_status,
743 report, porcelain, summary_width);
744 break;
745 case REF_STATUS_EXPECTING_REPORT:
746 print_ref_status('!', "[remote failure]", ref,
747 ref->deletion ? NULL : ref->peer_ref,
748 "remote failed to report status",
749 report, porcelain, summary_width);
750 break;
751 case REF_STATUS_ATOMIC_PUSH_FAILED:
752 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
753 "atomic push failed",
754 report, porcelain, summary_width);
755 break;
756 case REF_STATUS_OK:
757 print_ok_ref_status(ref, report, porcelain, summary_width);
758 break;
759 }
760
761 return 1;
762 }
763
764 static int print_one_push_status(struct ref *ref, const char *dest, int count,
765 int porcelain, int summary_width)
766 {
767 struct ref_push_report *report;
768 int n = 0;
769
770 if (!ref->report)
771 return print_one_push_report(ref, dest, count,
772 NULL, porcelain, summary_width);
773
774 for (report = ref->report; report; report = report->next)
775 print_one_push_report(ref, dest, count + n++,
776 report, porcelain, summary_width);
777 return n;
778 }
779
780 static int measure_abbrev(const struct object_id *oid, int sofar)
781 {
782 char hex[GIT_MAX_HEXSZ + 1];
783 int w = find_unique_abbrev_r(hex, oid, DEFAULT_ABBREV);
784
785 return (w < sofar) ? sofar : w;
786 }
787
788 int transport_summary_width(const struct ref *refs)
789 {
790 int maxw = -1;
791
792 for (; refs; refs = refs->next) {
793 maxw = measure_abbrev(&refs->old_oid, maxw);
794 maxw = measure_abbrev(&refs->new_oid, maxw);
795 }
796 if (maxw < 0)
797 maxw = FALLBACK_DEFAULT_ABBREV;
798 return (2 * maxw + 3);
799 }
800
801 void transport_print_push_status(const char *dest, struct ref *refs,
802 int verbose, int porcelain, unsigned int *reject_reasons)
803 {
804 struct ref *ref;
805 int n = 0;
806 char *head;
807 int summary_width = transport_summary_width(refs);
808
809 if (transport_color_config() < 0)
810 warning(_("could not parse transport.color.* config"));
811
812 head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
813
814 if (verbose) {
815 for (ref = refs; ref; ref = ref->next)
816 if (ref->status == REF_STATUS_UPTODATE)
817 n += print_one_push_status(ref, dest, n,
818 porcelain, summary_width);
819 }
820
821 for (ref = refs; ref; ref = ref->next)
822 if (ref->status == REF_STATUS_OK)
823 n += print_one_push_status(ref, dest, n,
824 porcelain, summary_width);
825
826 *reject_reasons = 0;
827 for (ref = refs; ref; ref = ref->next) {
828 if (ref->status != REF_STATUS_NONE &&
829 ref->status != REF_STATUS_UPTODATE &&
830 ref->status != REF_STATUS_OK)
831 n += print_one_push_status(ref, dest, n,
832 porcelain, summary_width);
833 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
834 if (head != NULL && !strcmp(head, ref->name))
835 *reject_reasons |= REJECT_NON_FF_HEAD;
836 else
837 *reject_reasons |= REJECT_NON_FF_OTHER;
838 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
839 *reject_reasons |= REJECT_ALREADY_EXISTS;
840 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
841 *reject_reasons |= REJECT_FETCH_FIRST;
842 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
843 *reject_reasons |= REJECT_NEEDS_FORCE;
844 } else if (ref->status == REF_STATUS_REJECT_REMOTE_UPDATED) {
845 *reject_reasons |= REJECT_REF_NEEDS_UPDATE;
846 }
847 }
848 free(head);
849 }
850
851 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
852 {
853 struct git_transport_data *data = transport->data;
854 struct send_pack_args args;
855 int ret = 0;
856
857 if (transport_color_config() < 0)
858 return -1;
859
860 if (!data->finished_handshake)
861 get_refs_via_connect(transport, 1, NULL);
862
863 memset(&args, 0, sizeof(args));
864 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
865 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
866 args.use_thin_pack = data->options.thin;
867 args.verbose = (transport->verbose > 0);
868 args.quiet = (transport->verbose < 0);
869 args.progress = transport->progress;
870 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
871 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
872 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
873 args.push_options = transport->push_options;
874 args.url = transport->url;
875
876 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
877 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
878 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
879 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
880 else
881 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
882
883 switch (data->version) {
884 case protocol_v2:
885 die(_("support for protocol v2 not implemented yet"));
886 break;
887 case protocol_v1:
888 case protocol_v0:
889 ret = send_pack(&args, data->fd, data->conn, remote_refs,
890 &data->extra_have);
891 break;
892 case protocol_unknown_version:
893 BUG("unknown protocol version");
894 }
895
896 close(data->fd[1]);
897 close(data->fd[0]);
898 /*
899 * Atomic push may abort the connection early and close the pipe,
900 * which may cause an error for `finish_connect()`. Ignore this error
901 * for atomic git-push.
902 */
903 if (ret || args.atomic)
904 finish_connect(data->conn);
905 else
906 ret = finish_connect(data->conn);
907 data->conn = NULL;
908 data->finished_handshake = 0;
909
910 return ret;
911 }
912
913 static int connect_git(struct transport *transport, const char *name,
914 const char *executable, int fd[2])
915 {
916 struct git_transport_data *data = transport->data;
917 data->conn = git_connect(data->fd, transport->url,
918 executable, 0);
919 fd[0] = data->fd[0];
920 fd[1] = data->fd[1];
921 return 0;
922 }
923
924 static int disconnect_git(struct transport *transport)
925 {
926 struct git_transport_data *data = transport->data;
927 if (data->conn) {
928 if (data->finished_handshake && !transport->stateless_rpc)
929 packet_flush(data->fd[1]);
930 close(data->fd[0]);
931 if (data->fd[1] >= 0)
932 close(data->fd[1]);
933 finish_connect(data->conn);
934 }
935
936 list_objects_filter_release(&data->options.filter_options);
937 free(data);
938 return 0;
939 }
940
941 static struct transport_vtable taken_over_vtable = {
942 .get_refs_list = get_refs_via_connect,
943 .get_bundle_uri = get_bundle_uri,
944 .fetch_refs = fetch_refs_via_pack,
945 .push_refs = git_transport_push,
946 .disconnect = disconnect_git
947 };
948
949 void transport_take_over(struct transport *transport,
950 struct child_process *child)
951 {
952 struct git_transport_data *data;
953
954 if (!transport->smart_options)
955 BUG("taking over transport requires non-NULL "
956 "smart_options field.");
957
958 CALLOC_ARRAY(data, 1);
959 data->options = *transport->smart_options;
960 data->conn = child;
961 data->fd[0] = data->conn->out;
962 data->fd[1] = data->conn->in;
963 data->finished_handshake = 0;
964 transport->data = data;
965
966 transport->vtable = &taken_over_vtable;
967 transport->smart_options = &(data->options);
968
969 transport->cannot_reuse = 1;
970 }
971
972 static int is_file(const char *url)
973 {
974 struct stat buf;
975 if (stat(url, &buf))
976 return 0;
977 return S_ISREG(buf.st_mode);
978 }
979
980 static int external_specification_len(const char *url)
981 {
982 return strchr(url, ':') - url;
983 }
984
985 static const struct string_list *protocol_allow_list(void)
986 {
987 static int enabled = -1;
988 static struct string_list allowed = STRING_LIST_INIT_DUP;
989
990 if (enabled < 0) {
991 const char *v = getenv("GIT_ALLOW_PROTOCOL");
992 if (v) {
993 string_list_split(&allowed, v, ':', -1);
994 string_list_sort(&allowed);
995 enabled = 1;
996 } else {
997 enabled = 0;
998 }
999 }
1000
1001 return enabled ? &allowed : NULL;
1002 }
1003
1004 enum protocol_allow_config {
1005 PROTOCOL_ALLOW_NEVER = 0,
1006 PROTOCOL_ALLOW_USER_ONLY,
1007 PROTOCOL_ALLOW_ALWAYS
1008 };
1009
1010 static enum protocol_allow_config parse_protocol_config(const char *key,
1011 const char *value)
1012 {
1013 if (!strcasecmp(value, "always"))
1014 return PROTOCOL_ALLOW_ALWAYS;
1015 else if (!strcasecmp(value, "never"))
1016 return PROTOCOL_ALLOW_NEVER;
1017 else if (!strcasecmp(value, "user"))
1018 return PROTOCOL_ALLOW_USER_ONLY;
1019
1020 die(_("unknown value for config '%s': %s"), key, value);
1021 }
1022
1023 static enum protocol_allow_config get_protocol_config(const char *type)
1024 {
1025 char *key = xstrfmt("protocol.%s.allow", type);
1026 char *value;
1027
1028 /* first check the per-protocol config */
1029 if (!git_config_get_string(key, &value)) {
1030 enum protocol_allow_config ret =
1031 parse_protocol_config(key, value);
1032 free(key);
1033 free(value);
1034 return ret;
1035 }
1036 free(key);
1037
1038 /* if defined, fallback to user-defined default for unknown protocols */
1039 if (!git_config_get_string("protocol.allow", &value)) {
1040 enum protocol_allow_config ret =
1041 parse_protocol_config("protocol.allow", value);
1042 free(value);
1043 return ret;
1044 }
1045
1046 /* fallback to built-in defaults */
1047 /* known safe */
1048 if (!strcmp(type, "http") ||
1049 !strcmp(type, "https") ||
1050 !strcmp(type, "git") ||
1051 !strcmp(type, "ssh"))
1052 return PROTOCOL_ALLOW_ALWAYS;
1053
1054 /* known scary; err on the side of caution */
1055 if (!strcmp(type, "ext"))
1056 return PROTOCOL_ALLOW_NEVER;
1057
1058 /* unknown; by default let them be used only directly by the user */
1059 return PROTOCOL_ALLOW_USER_ONLY;
1060 }
1061
1062 int is_transport_allowed(const char *type, int from_user)
1063 {
1064 const struct string_list *allow_list = protocol_allow_list();
1065 if (allow_list)
1066 return string_list_has_string(allow_list, type);
1067
1068 switch (get_protocol_config(type)) {
1069 case PROTOCOL_ALLOW_ALWAYS:
1070 return 1;
1071 case PROTOCOL_ALLOW_NEVER:
1072 return 0;
1073 case PROTOCOL_ALLOW_USER_ONLY:
1074 if (from_user < 0)
1075 from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1076 return from_user;
1077 }
1078
1079 BUG("invalid protocol_allow_config type");
1080 }
1081
1082 void transport_check_allowed(const char *type)
1083 {
1084 if (!is_transport_allowed(type, -1))
1085 die(_("transport '%s' not allowed"), type);
1086 }
1087
1088 static struct transport_vtable bundle_vtable = {
1089 .get_refs_list = get_refs_from_bundle,
1090 .fetch_refs = fetch_refs_from_bundle,
1091 .disconnect = close_bundle
1092 };
1093
1094 static struct transport_vtable builtin_smart_vtable = {
1095 .get_refs_list = get_refs_via_connect,
1096 .get_bundle_uri = get_bundle_uri,
1097 .fetch_refs = fetch_refs_via_pack,
1098 .push_refs = git_transport_push,
1099 .connect = connect_git,
1100 .disconnect = disconnect_git
1101 };
1102
1103 struct transport *transport_get(struct remote *remote, const char *url)
1104 {
1105 const char *helper;
1106 struct transport *ret = xcalloc(1, sizeof(*ret));
1107
1108 ret->progress = isatty(2);
1109 string_list_init_dup(&ret->pack_lockfiles);
1110
1111 CALLOC_ARRAY(ret->bundles, 1);
1112 init_bundle_list(ret->bundles);
1113
1114 if (!remote)
1115 BUG("No remote provided to transport_get()");
1116
1117 ret->got_remote_refs = 0;
1118 ret->remote = remote;
1119 helper = remote->foreign_vcs;
1120
1121 if (!url && remote->url)
1122 url = remote->url[0];
1123 ret->url = url;
1124
1125 /* maybe it is a foreign URL? */
1126 if (url) {
1127 const char *p = url;
1128
1129 while (is_urlschemechar(p == url, *p))
1130 p++;
1131 if (starts_with(p, "::"))
1132 helper = xstrndup(url, p - url);
1133 }
1134
1135 if (helper) {
1136 transport_helper_init(ret, helper);
1137 } else if (starts_with(url, "rsync:")) {
1138 die(_("git-over-rsync is no longer supported"));
1139 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
1140 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
1141 bundle_header_init(&data->header);
1142 transport_check_allowed("file");
1143 ret->data = data;
1144 ret->vtable = &bundle_vtable;
1145 ret->smart_options = NULL;
1146 } else if (!is_url(url)
1147 || starts_with(url, "file://")
1148 || starts_with(url, "git://")
1149 || starts_with(url, "ssh://")
1150 || starts_with(url, "git+ssh://") /* deprecated - do not use */
1151 || starts_with(url, "ssh+git://") /* deprecated - do not use */
1152 ) {
1153 /*
1154 * These are builtin smart transports; "allowed" transports
1155 * will be checked individually in git_connect.
1156 */
1157 struct git_transport_data *data = xcalloc(1, sizeof(*data));
1158 list_objects_filter_init(&data->options.filter_options);
1159 ret->data = data;
1160 ret->vtable = &builtin_smart_vtable;
1161 ret->smart_options = &(data->options);
1162
1163 data->conn = NULL;
1164 data->finished_handshake = 0;
1165 } else {
1166 /* Unknown protocol in URL. Pass to external handler. */
1167 int len = external_specification_len(url);
1168 char *handler = xmemdupz(url, len);
1169 transport_helper_init(ret, handler);
1170 }
1171
1172 if (ret->smart_options) {
1173 ret->smart_options->thin = 1;
1174 ret->smart_options->uploadpack = "git-upload-pack";
1175 if (remote->uploadpack)
1176 ret->smart_options->uploadpack = remote->uploadpack;
1177 ret->smart_options->receivepack = "git-receive-pack";
1178 if (remote->receivepack)
1179 ret->smart_options->receivepack = remote->receivepack;
1180 }
1181
1182 ret->hash_algo = &hash_algos[GIT_HASH_SHA1];
1183
1184 return ret;
1185 }
1186
1187 const struct git_hash_algo *transport_get_hash_algo(struct transport *transport)
1188 {
1189 return transport->hash_algo;
1190 }
1191
1192 int transport_set_option(struct transport *transport,
1193 const char *name, const char *value)
1194 {
1195 int git_reports = 1, protocol_reports = 1;
1196
1197 if (transport->smart_options)
1198 git_reports = set_git_option(transport->smart_options,
1199 name, value);
1200
1201 if (transport->vtable->set_option)
1202 protocol_reports = transport->vtable->set_option(transport,
1203 name, value);
1204
1205 /* If either report is 0, report 0 (success). */
1206 if (!git_reports || !protocol_reports)
1207 return 0;
1208 /* If either reports -1 (invalid value), report -1. */
1209 if ((git_reports == -1) || (protocol_reports == -1))
1210 return -1;
1211 /* Otherwise if both report unknown, report unknown. */
1212 return 1;
1213 }
1214
1215 void transport_set_verbosity(struct transport *transport, int verbosity,
1216 int force_progress)
1217 {
1218 if (verbosity >= 1)
1219 transport->verbose = verbosity <= 3 ? verbosity : 3;
1220 if (verbosity < 0)
1221 transport->verbose = -1;
1222
1223 /**
1224 * Rules used to determine whether to report progress (processing aborts
1225 * when a rule is satisfied):
1226 *
1227 * . Report progress, if force_progress is 1 (ie. --progress).
1228 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1229 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1230 * . Report progress if isatty(2) is 1.
1231 **/
1232 if (force_progress >= 0)
1233 transport->progress = !!force_progress;
1234 else
1235 transport->progress = verbosity >= 0 && isatty(2);
1236 }
1237
1238 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
1239 {
1240 int i;
1241
1242 fprintf(stderr, _("The following submodule paths contain changes that can\n"
1243 "not be found on any remote:\n"));
1244 for (i = 0; i < needs_pushing->nr; i++)
1245 fprintf(stderr, " %s\n", needs_pushing->items[i].string);
1246 fprintf(stderr, _("\nPlease try\n\n"
1247 " git push --recurse-submodules=on-demand\n\n"
1248 "or cd to the path and use\n\n"
1249 " git push\n\n"
1250 "to push them to a remote.\n\n"));
1251
1252 string_list_clear(needs_pushing, 0);
1253
1254 die(_("Aborting."));
1255 }
1256
1257 static int run_pre_push_hook(struct transport *transport,
1258 struct ref *remote_refs)
1259 {
1260 int ret = 0, x;
1261 struct ref *r;
1262 struct child_process proc = CHILD_PROCESS_INIT;
1263 struct strbuf buf;
1264 const char *hook_path = find_hook("pre-push");
1265
1266 if (!hook_path)
1267 return 0;
1268
1269 strvec_push(&proc.args, hook_path);
1270 strvec_push(&proc.args, transport->remote->name);
1271 strvec_push(&proc.args, transport->url);
1272
1273 proc.in = -1;
1274 proc.trace2_hook_name = "pre-push";
1275
1276 if (start_command(&proc)) {
1277 finish_command(&proc);
1278 return -1;
1279 }
1280
1281 sigchain_push(SIGPIPE, SIG_IGN);
1282
1283 strbuf_init(&buf, 256);
1284
1285 for (r = remote_refs; r; r = r->next) {
1286 if (!r->peer_ref) continue;
1287 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
1288 if (r->status == REF_STATUS_REJECT_STALE) continue;
1289 if (r->status == REF_STATUS_REJECT_REMOTE_UPDATED) continue;
1290 if (r->status == REF_STATUS_UPTODATE) continue;
1291
1292 strbuf_reset(&buf);
1293 strbuf_addf( &buf, "%s %s %s %s\n",
1294 r->peer_ref->name, oid_to_hex(&r->new_oid),
1295 r->name, oid_to_hex(&r->old_oid));
1296
1297 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
1298 /* We do not mind if a hook does not read all refs. */
1299 if (errno != EPIPE)
1300 ret = -1;
1301 break;
1302 }
1303 }
1304
1305 strbuf_release(&buf);
1306
1307 x = close(proc.in);
1308 if (!ret)
1309 ret = x;
1310
1311 sigchain_pop(SIGPIPE);
1312
1313 x = finish_command(&proc);
1314 if (!ret)
1315 ret = x;
1316
1317 return ret;
1318 }
1319
1320 int transport_push(struct repository *r,
1321 struct transport *transport,
1322 struct refspec *rs, int flags,
1323 unsigned int *reject_reasons)
1324 {
1325 struct ref *remote_refs = NULL;
1326 struct ref *local_refs = NULL;
1327 int match_flags = MATCH_REFS_NONE;
1328 int verbose = (transport->verbose > 0);
1329 int quiet = (transport->verbose < 0);
1330 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1331 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1332 int push_ret, err;
1333 int ret = -1;
1334 struct transport_ls_refs_options transport_options =
1335 TRANSPORT_LS_REFS_OPTIONS_INIT;
1336
1337 *reject_reasons = 0;
1338
1339 if (transport_color_config() < 0)
1340 goto done;
1341
1342 if (!transport->vtable->push_refs)
1343 goto done;
1344
1345 local_refs = get_local_heads();
1346
1347 if (check_push_refs(local_refs, rs) < 0)
1348 goto done;
1349
1350 refspec_ref_prefixes(rs, &transport_options.ref_prefixes);
1351
1352 trace2_region_enter("transport_push", "get_refs_list", r);
1353 remote_refs = transport->vtable->get_refs_list(transport, 1,
1354 &transport_options);
1355 trace2_region_leave("transport_push", "get_refs_list", r);
1356
1357 transport_ls_refs_options_release(&transport_options);
1358
1359 if (flags & TRANSPORT_PUSH_ALL)
1360 match_flags |= MATCH_REFS_ALL;
1361 if (flags & TRANSPORT_PUSH_MIRROR)
1362 match_flags |= MATCH_REFS_MIRROR;
1363 if (flags & TRANSPORT_PUSH_PRUNE)
1364 match_flags |= MATCH_REFS_PRUNE;
1365 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1366 match_flags |= MATCH_REFS_FOLLOW_TAGS;
1367
1368 if (match_push_refs(local_refs, &remote_refs, rs, match_flags))
1369 goto done;
1370
1371 if (transport->smart_options &&
1372 transport->smart_options->cas &&
1373 !is_empty_cas(transport->smart_options->cas))
1374 apply_push_cas(transport->smart_options->cas,
1375 transport->remote, remote_refs);
1376
1377 set_ref_status_for_push(remote_refs,
1378 flags & TRANSPORT_PUSH_MIRROR,
1379 flags & TRANSPORT_PUSH_FORCE);
1380
1381 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1382 if (run_pre_push_hook(transport, remote_refs))
1383 goto done;
1384
1385 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1386 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1387 !is_bare_repository()) {
1388 struct ref *ref = remote_refs;
1389 struct oid_array commits = OID_ARRAY_INIT;
1390
1391 trace2_region_enter("transport_push", "push_submodules", r);
1392 for (; ref; ref = ref->next)
1393 if (!is_null_oid(&ref->new_oid))
1394 oid_array_append(&commits,
1395 &ref->new_oid);
1396
1397 if (!push_unpushed_submodules(r,
1398 &commits,
1399 transport->remote,
1400 rs,
1401 transport->push_options,
1402 pretend)) {
1403 oid_array_clear(&commits);
1404 trace2_region_leave("transport_push", "push_submodules", r);
1405 die(_("failed to push all needed submodules"));
1406 }
1407 oid_array_clear(&commits);
1408 trace2_region_leave("transport_push", "push_submodules", r);
1409 }
1410
1411 if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1412 ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1413 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1414 !pretend)) && !is_bare_repository()) {
1415 struct ref *ref = remote_refs;
1416 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1417 struct oid_array commits = OID_ARRAY_INIT;
1418
1419 trace2_region_enter("transport_push", "check_submodules", r);
1420 for (; ref; ref = ref->next)
1421 if (!is_null_oid(&ref->new_oid))
1422 oid_array_append(&commits,
1423 &ref->new_oid);
1424
1425 if (find_unpushed_submodules(r,
1426 &commits,
1427 transport->remote->name,
1428 &needs_pushing)) {
1429 oid_array_clear(&commits);
1430 trace2_region_leave("transport_push", "check_submodules", r);
1431 die_with_unpushed_submodules(&needs_pushing);
1432 }
1433 string_list_clear(&needs_pushing, 0);
1434 oid_array_clear(&commits);
1435 trace2_region_leave("transport_push", "check_submodules", r);
1436 }
1437
1438 if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY)) {
1439 trace2_region_enter("transport_push", "push_refs", r);
1440 push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1441 trace2_region_leave("transport_push", "push_refs", r);
1442 } else
1443 push_ret = 0;
1444 err = push_had_errors(remote_refs);
1445 ret = push_ret | err;
1446
1447 if (!quiet || err)
1448 transport_print_push_status(transport->url, remote_refs,
1449 verbose | porcelain, porcelain,
1450 reject_reasons);
1451
1452 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1453 set_upstreams(transport, remote_refs, pretend);
1454
1455 if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1456 TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1457 struct ref *ref;
1458 for (ref = remote_refs; ref; ref = ref->next)
1459 transport_update_tracking_ref(transport->remote, ref, verbose);
1460 }
1461
1462 if (porcelain && !push_ret)
1463 puts("Done");
1464 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1465 fprintf(stderr, "Everything up-to-date\n");
1466
1467 done:
1468 free_refs(local_refs);
1469 free_refs(remote_refs);
1470 return ret;
1471 }
1472
1473 const struct ref *transport_get_remote_refs(struct transport *transport,
1474 struct transport_ls_refs_options *transport_options)
1475 {
1476 if (!transport->got_remote_refs) {
1477 transport->remote_refs =
1478 transport->vtable->get_refs_list(transport, 0,
1479 transport_options);
1480 transport->got_remote_refs = 1;
1481 }
1482
1483 return transport->remote_refs;
1484 }
1485
1486 void transport_ls_refs_options_release(struct transport_ls_refs_options *opts)
1487 {
1488 strvec_clear(&opts->ref_prefixes);
1489 free((char *)opts->unborn_head_target);
1490 }
1491
1492 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1493 {
1494 int rc;
1495 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1496 struct ref **heads = NULL;
1497 struct ref *rm;
1498
1499 for (rm = refs; rm; rm = rm->next) {
1500 nr_refs++;
1501 if (rm->peer_ref &&
1502 !is_null_oid(&rm->old_oid) &&
1503 oideq(&rm->peer_ref->old_oid, &rm->old_oid))
1504 continue;
1505 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1506 heads[nr_heads++] = rm;
1507 }
1508
1509 if (!nr_heads) {
1510 /*
1511 * When deepening of a shallow repository is requested,
1512 * then local and remote refs are likely to still be equal.
1513 * Just feed them all to the fetch method in that case.
1514 * This condition shouldn't be met in a non-deepening fetch
1515 * (see builtin/fetch.c:quickfetch()).
1516 */
1517 ALLOC_ARRAY(heads, nr_refs);
1518 for (rm = refs; rm; rm = rm->next)
1519 heads[nr_heads++] = rm;
1520 }
1521
1522 rc = transport->vtable->fetch_refs(transport, nr_heads, heads);
1523
1524 free(heads);
1525 return rc;
1526 }
1527
1528 int transport_get_remote_bundle_uri(struct transport *transport)
1529 {
1530 int value = 0;
1531 const struct transport_vtable *vtable = transport->vtable;
1532
1533 /* Check config only once. */
1534 if (transport->got_remote_bundle_uri)
1535 return 0;
1536 transport->got_remote_bundle_uri = 1;
1537
1538 /*
1539 * Don't request bundle-uri from the server unless configured to
1540 * do so by the transfer.bundleURI=true config option.
1541 */
1542 if (git_config_get_bool("transfer.bundleuri", &value) || !value)
1543 return 0;
1544
1545 if (!transport->bundles->baseURI)
1546 transport->bundles->baseURI = xstrdup(transport->url);
1547
1548 if (!vtable->get_bundle_uri)
1549 return error(_("bundle-uri operation not supported by protocol"));
1550
1551 if (vtable->get_bundle_uri(transport) < 0)
1552 return error(_("could not retrieve server-advertised bundle-uri list"));
1553 return 0;
1554 }
1555
1556 void transport_unlock_pack(struct transport *transport, unsigned int flags)
1557 {
1558 int in_signal_handler = !!(flags & TRANSPORT_UNLOCK_PACK_IN_SIGNAL_HANDLER);
1559 int i;
1560
1561 for (i = 0; i < transport->pack_lockfiles.nr; i++)
1562 if (in_signal_handler)
1563 unlink(transport->pack_lockfiles.items[i].string);
1564 else
1565 unlink_or_warn(transport->pack_lockfiles.items[i].string);
1566 if (!in_signal_handler)
1567 string_list_clear(&transport->pack_lockfiles, 0);
1568 }
1569
1570 int transport_connect(struct transport *transport, const char *name,
1571 const char *exec, int fd[2])
1572 {
1573 if (transport->vtable->connect)
1574 return transport->vtable->connect(transport, name, exec, fd);
1575 else
1576 die(_("operation not supported by protocol"));
1577 }
1578
1579 int transport_disconnect(struct transport *transport)
1580 {
1581 int ret = 0;
1582 if (transport->vtable->disconnect)
1583 ret = transport->vtable->disconnect(transport);
1584 if (transport->got_remote_refs)
1585 free_refs((void *)transport->remote_refs);
1586 clear_bundle_list(transport->bundles);
1587 free(transport->bundles);
1588 free(transport);
1589 return ret;
1590 }
1591
1592 /*
1593 * Strip username (and password) from a URL and return
1594 * it in a newly allocated string.
1595 */
1596 char *transport_anonymize_url(const char *url)
1597 {
1598 char *scheme_prefix, *anon_part;
1599 size_t anon_len, prefix_len = 0;
1600
1601 anon_part = strchr(url, '@');
1602 if (url_is_local_not_ssh(url) || !anon_part)
1603 goto literal_copy;
1604
1605 anon_len = strlen(++anon_part);
1606 scheme_prefix = strstr(url, "://");
1607 if (!scheme_prefix) {
1608 if (!strchr(anon_part, ':'))
1609 /* cannot be "me@there:/path/name" */
1610 goto literal_copy;
1611 } else {
1612 const char *cp;
1613 /* make sure scheme is reasonable */
1614 for (cp = url; cp < scheme_prefix; cp++) {
1615 switch (*cp) {
1616 /* RFC 1738 2.1 */
1617 case '+': case '.': case '-':
1618 break; /* ok */
1619 default:
1620 if (isalnum(*cp))
1621 break;
1622 /* it isn't */
1623 goto literal_copy;
1624 }
1625 }
1626 /* @ past the first slash does not count */
1627 cp = strchr(scheme_prefix + 3, '/');
1628 if (cp && cp < anon_part)
1629 goto literal_copy;
1630 prefix_len = scheme_prefix - url + 3;
1631 }
1632 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1633 (int)anon_len, anon_part);
1634 literal_copy:
1635 return xstrdup(url);
1636 }