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