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