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