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