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