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