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