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