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