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