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