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