e6eb20ea947eb54ebc3cc78d38f6b3dae1864195
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 "send-pack.h"
7#include "walker.h"
8#include "bundle.h"
9#include "dir.h"
10#include "refs.h"
11
12/* rsync support */
13
14/*
15 * We copy packed-refs and refs/ into a temporary file, then read the
16 * loose refs recursively (sorting whenever possible), and then inserting
17 * those packed refs that are not yet in the list (not validating, but
18 * assuming that the file is sorted).
19 *
20 * Appears refactoring this from refs.c is too cumbersome.
21 */
22
23static int str_cmp(const void *a, const void *b)
24{
25 const char *s1 = a;
26 const char *s2 = b;
27
28 return strcmp(s1, s2);
29}
30
31/* path->buf + name_offset is expected to point to "refs/" */
32
33static int read_loose_refs(struct strbuf *path, int name_offset,
34 struct ref **tail)
35{
36 DIR *dir = opendir(path->buf);
37 struct dirent *de;
38 struct {
39 char **entries;
40 int nr, alloc;
41 } list;
42 int i, pathlen;
43
44 if (!dir)
45 return -1;
46
47 memset (&list, 0, sizeof(list));
48
49 while ((de = readdir(dir))) {
50 if (is_dot_or_dotdot(de->d_name))
51 continue;
52 ALLOC_GROW(list.entries, list.nr + 1, list.alloc);
53 list.entries[list.nr++] = xstrdup(de->d_name);
54 }
55 closedir(dir);
56
57 /* sort the list */
58
59 qsort(list.entries, list.nr, sizeof(char *), str_cmp);
60
61 pathlen = path->len;
62 strbuf_addch(path, '/');
63
64 for (i = 0; i < list.nr; i++, strbuf_setlen(path, pathlen + 1)) {
65 strbuf_addstr(path, list.entries[i]);
66 if (read_loose_refs(path, name_offset, tail)) {
67 int fd = open(path->buf, O_RDONLY);
68 char buffer[40];
69 struct ref *next;
70
71 if (fd < 0)
72 continue;
73 next = alloc_ref(path->buf + name_offset);
74 if (read_in_full(fd, buffer, 40) != 40 ||
75 get_sha1_hex(buffer, next->old_sha1)) {
76 close(fd);
77 free(next);
78 continue;
79 }
80 close(fd);
81 (*tail)->next = next;
82 *tail = next;
83 }
84 }
85 strbuf_setlen(path, pathlen);
86
87 for (i = 0; i < list.nr; i++)
88 free(list.entries[i]);
89 free(list.entries);
90
91 return 0;
92}
93
94/* insert the packed refs for which no loose refs were found */
95
96static void insert_packed_refs(const char *packed_refs, struct ref **list)
97{
98 FILE *f = fopen(packed_refs, "r");
99 static char buffer[PATH_MAX];
100
101 if (!f)
102 return;
103
104 for (;;) {
105 int cmp = cmp, len;
106
107 if (!fgets(buffer, sizeof(buffer), f)) {
108 fclose(f);
109 return;
110 }
111
112 if (hexval(buffer[0]) > 0xf)
113 continue;
114 len = strlen(buffer);
115 if (len && buffer[len - 1] == '\n')
116 buffer[--len] = '\0';
117 if (len < 41)
118 continue;
119 while ((*list)->next &&
120 (cmp = strcmp(buffer + 41,
121 (*list)->next->name)) > 0)
122 list = &(*list)->next;
123 if (!(*list)->next || cmp < 0) {
124 struct ref *next = alloc_ref(buffer + 41);
125 buffer[40] = '\0';
126 if (get_sha1_hex(buffer, next->old_sha1)) {
127 warning ("invalid SHA-1: %s", buffer);
128 free(next);
129 continue;
130 }
131 next->next = (*list)->next;
132 (*list)->next = next;
133 list = &(*list)->next;
134 }
135 }
136}
137
138static const char *rsync_url(const char *url)
139{
140 return prefixcmp(url, "rsync://") ? skip_prefix(url, "rsync:") : url;
141}
142
143static struct ref *get_refs_via_rsync(struct transport *transport, int for_push)
144{
145 struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;
146 struct ref dummy, *tail = &dummy;
147 struct child_process rsync;
148 const char *args[5];
149 int temp_dir_len;
150
151 if (for_push)
152 return NULL;
153
154 /* copy the refs to the temporary directory */
155
156 strbuf_addstr(&temp_dir, git_path("rsync-refs-XXXXXX"));
157 if (!mkdtemp(temp_dir.buf))
158 die_errno ("Could not make temporary directory");
159 temp_dir_len = temp_dir.len;
160
161 strbuf_addstr(&buf, rsync_url(transport->url));
162 strbuf_addstr(&buf, "/refs");
163
164 memset(&rsync, 0, sizeof(rsync));
165 rsync.argv = args;
166 rsync.stdout_to_stderr = 1;
167 args[0] = "rsync";
168 args[1] = (transport->verbose > 0) ? "-rv" : "-r";
169 args[2] = buf.buf;
170 args[3] = temp_dir.buf;
171 args[4] = NULL;
172
173 if (run_command(&rsync))
174 die ("Could not run rsync to get refs");
175
176 strbuf_reset(&buf);
177 strbuf_addstr(&buf, rsync_url(transport->url));
178 strbuf_addstr(&buf, "/packed-refs");
179
180 args[2] = buf.buf;
181
182 if (run_command(&rsync))
183 die ("Could not run rsync to get refs");
184
185 /* read the copied refs */
186
187 strbuf_addstr(&temp_dir, "/refs");
188 read_loose_refs(&temp_dir, temp_dir_len + 1, &tail);
189 strbuf_setlen(&temp_dir, temp_dir_len);
190
191 tail = &dummy;
192 strbuf_addstr(&temp_dir, "/packed-refs");
193 insert_packed_refs(temp_dir.buf, &tail);
194 strbuf_setlen(&temp_dir, temp_dir_len);
195
196 if (remove_dir_recursively(&temp_dir, 0))
197 warning ("Error removing temporary directory %s.",
198 temp_dir.buf);
199
200 strbuf_release(&buf);
201 strbuf_release(&temp_dir);
202
203 return dummy.next;
204}
205
206static int fetch_objs_via_rsync(struct transport *transport,
207 int nr_objs, struct ref **to_fetch)
208{
209 struct strbuf buf = STRBUF_INIT;
210 struct child_process rsync;
211 const char *args[8];
212 int result;
213
214 strbuf_addstr(&buf, rsync_url(transport->url));
215 strbuf_addstr(&buf, "/objects/");
216
217 memset(&rsync, 0, sizeof(rsync));
218 rsync.argv = args;
219 rsync.stdout_to_stderr = 1;
220 args[0] = "rsync";
221 args[1] = (transport->verbose > 0) ? "-rv" : "-r";
222 args[2] = "--ignore-existing";
223 args[3] = "--exclude";
224 args[4] = "info";
225 args[5] = buf.buf;
226 args[6] = get_object_directory();
227 args[7] = NULL;
228
229 /* NEEDSWORK: handle one level of alternates */
230 result = run_command(&rsync);
231
232 strbuf_release(&buf);
233
234 return result;
235}
236
237static int write_one_ref(const char *name, const unsigned char *sha1,
238 int flags, void *data)
239{
240 struct strbuf *buf = data;
241 int len = buf->len;
242 FILE *f;
243
244 /* when called via for_each_ref(), flags is non-zero */
245 if (flags && prefixcmp(name, "refs/heads/") &&
246 prefixcmp(name, "refs/tags/"))
247 return 0;
248
249 strbuf_addstr(buf, name);
250 if (safe_create_leading_directories(buf->buf) ||
251 !(f = fopen(buf->buf, "w")) ||
252 fprintf(f, "%s\n", sha1_to_hex(sha1)) < 0 ||
253 fclose(f))
254 return error("problems writing temporary file %s", buf->buf);
255 strbuf_setlen(buf, len);
256 return 0;
257}
258
259static int write_refs_to_temp_dir(struct strbuf *temp_dir,
260 int refspec_nr, const char **refspec)
261{
262 int i;
263
264 for (i = 0; i < refspec_nr; i++) {
265 unsigned char sha1[20];
266 char *ref;
267
268 if (dwim_ref(refspec[i], strlen(refspec[i]), sha1, &ref) != 1)
269 return error("Could not get ref %s", refspec[i]);
270
271 if (write_one_ref(ref, sha1, 0, temp_dir)) {
272 free(ref);
273 return -1;
274 }
275 free(ref);
276 }
277 return 0;
278}
279
280static int rsync_transport_push(struct transport *transport,
281 int refspec_nr, const char **refspec, int flags)
282{
283 struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;
284 int result = 0, i;
285 struct child_process rsync;
286 const char *args[10];
287
288 if (flags & TRANSPORT_PUSH_MIRROR)
289 return error("rsync transport does not support mirror mode");
290
291 /* first push the objects */
292
293 strbuf_addstr(&buf, rsync_url(transport->url));
294 strbuf_addch(&buf, '/');
295
296 memset(&rsync, 0, sizeof(rsync));
297 rsync.argv = args;
298 rsync.stdout_to_stderr = 1;
299 i = 0;
300 args[i++] = "rsync";
301 args[i++] = "-a";
302 if (flags & TRANSPORT_PUSH_DRY_RUN)
303 args[i++] = "--dry-run";
304 if (transport->verbose > 0)
305 args[i++] = "-v";
306 args[i++] = "--ignore-existing";
307 args[i++] = "--exclude";
308 args[i++] = "info";
309 args[i++] = get_object_directory();
310 args[i++] = buf.buf;
311 args[i++] = NULL;
312
313 if (run_command(&rsync))
314 return error("Could not push objects to %s",
315 rsync_url(transport->url));
316
317 /* copy the refs to the temporary directory; they could be packed. */
318
319 strbuf_addstr(&temp_dir, git_path("rsync-refs-XXXXXX"));
320 if (!mkdtemp(temp_dir.buf))
321 die_errno ("Could not make temporary directory");
322 strbuf_addch(&temp_dir, '/');
323
324 if (flags & TRANSPORT_PUSH_ALL) {
325 if (for_each_ref(write_one_ref, &temp_dir))
326 return -1;
327 } else if (write_refs_to_temp_dir(&temp_dir, refspec_nr, refspec))
328 return -1;
329
330 i = 2;
331 if (flags & TRANSPORT_PUSH_DRY_RUN)
332 args[i++] = "--dry-run";
333 if (!(flags & TRANSPORT_PUSH_FORCE))
334 args[i++] = "--ignore-existing";
335 args[i++] = temp_dir.buf;
336 args[i++] = rsync_url(transport->url);
337 args[i++] = NULL;
338 if (run_command(&rsync))
339 result = error("Could not push to %s",
340 rsync_url(transport->url));
341
342 if (remove_dir_recursively(&temp_dir, 0))
343 warning ("Could not remove temporary directory %s.",
344 temp_dir.buf);
345
346 strbuf_release(&buf);
347 strbuf_release(&temp_dir);
348
349 return result;
350}
351
352struct bundle_transport_data {
353 int fd;
354 struct bundle_header header;
355};
356
357static struct ref *get_refs_from_bundle(struct transport *transport, int for_push)
358{
359 struct bundle_transport_data *data = transport->data;
360 struct ref *result = NULL;
361 int i;
362
363 if (for_push)
364 return NULL;
365
366 if (data->fd > 0)
367 close(data->fd);
368 data->fd = read_bundle_header(transport->url, &data->header);
369 if (data->fd < 0)
370 die ("Could not read bundle '%s'.", transport->url);
371 for (i = 0; i < data->header.references.nr; i++) {
372 struct ref_list_entry *e = data->header.references.list + i;
373 struct ref *ref = alloc_ref(e->name);
374 hashcpy(ref->old_sha1, e->sha1);
375 ref->next = result;
376 result = ref;
377 }
378 return result;
379}
380
381static int fetch_refs_from_bundle(struct transport *transport,
382 int nr_heads, struct ref **to_fetch)
383{
384 struct bundle_transport_data *data = transport->data;
385 return unbundle(&data->header, data->fd);
386}
387
388static int close_bundle(struct transport *transport)
389{
390 struct bundle_transport_data *data = transport->data;
391 if (data->fd > 0)
392 close(data->fd);
393 free(data);
394 return 0;
395}
396
397struct git_transport_data {
398 struct git_transport_options options;
399 struct child_process *conn;
400 int fd[2];
401 struct extra_have_objects extra_have;
402};
403
404static int set_git_option(struct git_transport_options *opts,
405 const char *name, const char *value)
406{
407 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
408 opts->uploadpack = value;
409 return 0;
410 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
411 opts->receivepack = value;
412 return 0;
413 } else if (!strcmp(name, TRANS_OPT_THIN)) {
414 opts->thin = !!value;
415 return 0;
416 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
417 opts->followtags = !!value;
418 return 0;
419 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
420 opts->keep = !!value;
421 return 0;
422 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
423 if (!value)
424 opts->depth = 0;
425 else
426 opts->depth = atoi(value);
427 return 0;
428 }
429 return 1;
430}
431
432static int connect_setup(struct transport *transport, int for_push, int verbose)
433{
434 struct git_transport_data *data = transport->data;
435 data->conn = git_connect(data->fd, transport->url,
436 for_push ? data->options.receivepack :
437 data->options.uploadpack,
438 verbose ? CONNECT_VERBOSE : 0);
439 return 0;
440}
441
442static struct ref *get_refs_via_connect(struct transport *transport, int for_push)
443{
444 struct git_transport_data *data = transport->data;
445 struct ref *refs;
446
447 connect_setup(transport, for_push, 0);
448 get_remote_heads(data->fd[0], &refs, 0, NULL,
449 for_push ? REF_NORMAL : 0, &data->extra_have);
450
451 return refs;
452}
453
454static int fetch_refs_via_pack(struct transport *transport,
455 int nr_heads, struct ref **to_fetch)
456{
457 struct git_transport_data *data = transport->data;
458 char **heads = xmalloc(nr_heads * sizeof(*heads));
459 char **origh = xmalloc(nr_heads * sizeof(*origh));
460 const struct ref *refs;
461 char *dest = xstrdup(transport->url);
462 struct fetch_pack_args args;
463 int i;
464 struct ref *refs_tmp = NULL;
465
466 memset(&args, 0, sizeof(args));
467 args.uploadpack = data->options.uploadpack;
468 args.keep_pack = data->options.keep;
469 args.lock_pack = 1;
470 args.use_thin_pack = data->options.thin;
471 args.include_tag = data->options.followtags;
472 args.verbose = (transport->verbose > 0);
473 args.quiet = (transport->verbose < 0);
474 args.no_progress = args.quiet || (!transport->progress && !isatty(1));
475 args.depth = data->options.depth;
476
477 for (i = 0; i < nr_heads; i++)
478 origh[i] = heads[i] = xstrdup(to_fetch[i]->name);
479
480 if (!data->conn) {
481 connect_setup(transport, 0, 0);
482 get_remote_heads(data->fd[0], &refs_tmp, 0, NULL, 0, NULL);
483 }
484
485 refs = fetch_pack(&args, data->fd, data->conn,
486 refs_tmp ? refs_tmp : transport->remote_refs,
487 dest, nr_heads, heads, &transport->pack_lockfile);
488 close(data->fd[0]);
489 close(data->fd[1]);
490 if (finish_connect(data->conn))
491 refs = NULL;
492 data->conn = NULL;
493
494 free_refs(refs_tmp);
495
496 for (i = 0; i < nr_heads; i++)
497 free(origh[i]);
498 free(origh);
499 free(heads);
500 free(dest);
501 return (refs ? 0 : -1);
502}
503
504static int push_had_errors(struct ref *ref)
505{
506 for (; ref; ref = ref->next) {
507 switch (ref->status) {
508 case REF_STATUS_NONE:
509 case REF_STATUS_UPTODATE:
510 case REF_STATUS_OK:
511 break;
512 default:
513 return 1;
514 }
515 }
516 return 0;
517}
518
519static int refs_pushed(struct ref *ref)
520{
521 for (; ref; ref = ref->next) {
522 switch(ref->status) {
523 case REF_STATUS_NONE:
524 case REF_STATUS_UPTODATE:
525 break;
526 default:
527 return 1;
528 }
529 }
530 return 0;
531}
532
533static void update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
534{
535 struct refspec rs;
536
537 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
538 return;
539
540 rs.src = ref->name;
541 rs.dst = NULL;
542
543 if (!remote_find_tracking(remote, &rs)) {
544 if (verbose)
545 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
546 if (ref->deletion) {
547 delete_ref(rs.dst, NULL, 0);
548 } else
549 update_ref("update by push", rs.dst,
550 ref->new_sha1, NULL, 0, 0);
551 free(rs.dst);
552 }
553}
554
555#define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
556
557static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg, int porcelain)
558{
559 if (porcelain) {
560 if (from)
561 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
562 else
563 fprintf(stdout, "%c\t:%s\t", flag, to->name);
564 if (msg)
565 fprintf(stdout, "%s (%s)\n", summary, msg);
566 else
567 fprintf(stdout, "%s\n", summary);
568 } else {
569 fprintf(stderr, " %c %-*s ", flag, SUMMARY_WIDTH, summary);
570 if (from)
571 fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
572 else
573 fputs(prettify_refname(to->name), stderr);
574 if (msg) {
575 fputs(" (", stderr);
576 fputs(msg, stderr);
577 fputc(')', stderr);
578 }
579 fputc('\n', stderr);
580 }
581}
582
583static const char *status_abbrev(unsigned char sha1[20])
584{
585 return find_unique_abbrev(sha1, DEFAULT_ABBREV);
586}
587
588static void print_ok_ref_status(struct ref *ref, int porcelain)
589{
590 if (ref->deletion)
591 print_ref_status('-', "[deleted]", ref, NULL, NULL, porcelain);
592 else if (is_null_sha1(ref->old_sha1))
593 print_ref_status('*',
594 (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" :
595 "[new branch]"),
596 ref, ref->peer_ref, NULL, porcelain);
597 else {
598 char quickref[84];
599 char type;
600 const char *msg;
601
602 strcpy(quickref, status_abbrev(ref->old_sha1));
603 if (ref->nonfastforward) {
604 strcat(quickref, "...");
605 type = '+';
606 msg = "forced update";
607 } else {
608 strcat(quickref, "..");
609 type = ' ';
610 msg = NULL;
611 }
612 strcat(quickref, status_abbrev(ref->new_sha1));
613
614 print_ref_status(type, quickref, ref, ref->peer_ref, msg, porcelain);
615 }
616}
617
618static int print_one_push_status(struct ref *ref, const char *dest, int count, int porcelain)
619{
620 if (!count)
621 fprintf(stderr, "To %s\n", dest);
622
623 switch(ref->status) {
624 case REF_STATUS_NONE:
625 print_ref_status('X', "[no match]", ref, NULL, NULL, porcelain);
626 break;
627 case REF_STATUS_REJECT_NODELETE:
628 print_ref_status('!', "[rejected]", ref, NULL,
629 "remote does not support deleting refs", porcelain);
630 break;
631 case REF_STATUS_UPTODATE:
632 print_ref_status('=', "[up to date]", ref,
633 ref->peer_ref, NULL, porcelain);
634 break;
635 case REF_STATUS_REJECT_NONFASTFORWARD:
636 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
637 "non-fast-forward", porcelain);
638 break;
639 case REF_STATUS_REMOTE_REJECT:
640 print_ref_status('!', "[remote rejected]", ref,
641 ref->deletion ? NULL : ref->peer_ref,
642 ref->remote_status, porcelain);
643 break;
644 case REF_STATUS_EXPECTING_REPORT:
645 print_ref_status('!', "[remote failure]", ref,
646 ref->deletion ? NULL : ref->peer_ref,
647 "remote failed to report status", porcelain);
648 break;
649 case REF_STATUS_OK:
650 print_ok_ref_status(ref, porcelain);
651 break;
652 }
653
654 return 1;
655}
656
657static void print_push_status(const char *dest, struct ref *refs,
658 int verbose, int porcelain, int * nonfastforward)
659{
660 struct ref *ref;
661 int n = 0;
662
663 if (verbose) {
664 for (ref = refs; ref; ref = ref->next)
665 if (ref->status == REF_STATUS_UPTODATE)
666 n += print_one_push_status(ref, dest, n, porcelain);
667 }
668
669 for (ref = refs; ref; ref = ref->next)
670 if (ref->status == REF_STATUS_OK)
671 n += print_one_push_status(ref, dest, n, porcelain);
672
673 *nonfastforward = 0;
674 for (ref = refs; ref; ref = ref->next) {
675 if (ref->status != REF_STATUS_NONE &&
676 ref->status != REF_STATUS_UPTODATE &&
677 ref->status != REF_STATUS_OK)
678 n += print_one_push_status(ref, dest, n, porcelain);
679 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD)
680 *nonfastforward = 1;
681 }
682}
683
684static void verify_remote_names(int nr_heads, const char **heads)
685{
686 int i;
687
688 for (i = 0; i < nr_heads; i++) {
689 const char *local = heads[i];
690 const char *remote = strrchr(heads[i], ':');
691
692 if (*local == '+')
693 local++;
694
695 /* A matching refspec is okay. */
696 if (remote == local && remote[1] == '\0')
697 continue;
698
699 remote = remote ? (remote + 1) : local;
700 switch (check_ref_format(remote)) {
701 case 0: /* ok */
702 case CHECK_REF_FORMAT_ONELEVEL:
703 /* ok but a single level -- that is fine for
704 * a match pattern.
705 */
706 case CHECK_REF_FORMAT_WILDCARD:
707 /* ok but ends with a pattern-match character */
708 continue;
709 }
710 die("remote part of refspec is not a valid name in %s",
711 heads[i]);
712 }
713}
714
715static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
716{
717 struct git_transport_data *data = transport->data;
718 struct send_pack_args args;
719 int ret;
720
721 if (!data->conn) {
722 struct ref *tmp_refs;
723 connect_setup(transport, 1, 0);
724
725 get_remote_heads(data->fd[0], &tmp_refs, 0, NULL, REF_NORMAL,
726 NULL);
727 }
728
729 memset(&args, 0, sizeof(args));
730 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
731 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
732 args.use_thin_pack = data->options.thin;
733 args.verbose = !!(flags & TRANSPORT_PUSH_VERBOSE);
734 args.quiet = !!(flags & TRANSPORT_PUSH_QUIET);
735 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
736
737 ret = send_pack(&args, data->fd, data->conn, remote_refs,
738 &data->extra_have);
739
740 close(data->fd[1]);
741 close(data->fd[0]);
742 ret |= finish_connect(data->conn);
743 data->conn = NULL;
744
745 return ret;
746}
747
748static int disconnect_git(struct transport *transport)
749{
750 struct git_transport_data *data = transport->data;
751 if (data->conn) {
752 packet_flush(data->fd[1]);
753 close(data->fd[0]);
754 close(data->fd[1]);
755 finish_connect(data->conn);
756 }
757
758 free(data);
759 return 0;
760}
761
762static int is_local(const char *url)
763{
764 const char *colon = strchr(url, ':');
765 const char *slash = strchr(url, '/');
766 return !colon || (slash && slash < colon) ||
767 has_dos_drive_prefix(url);
768}
769
770static int is_file(const char *url)
771{
772 struct stat buf;
773 if (stat(url, &buf))
774 return 0;
775 return S_ISREG(buf.st_mode);
776}
777
778static int is_url(const char *url)
779{
780 const char *url2, *first_slash;
781
782 if (!url)
783 return 0;
784 url2 = url;
785 first_slash = strchr(url, '/');
786
787 /* Input with no slash at all or slash first can't be URL. */
788 if (!first_slash || first_slash == url)
789 return 0;
790 /* Character before must be : and next must be /. */
791 if (first_slash[-1] != ':' || first_slash[1] != '/')
792 return 0;
793 /* There must be something before the :// */
794 if (first_slash == url + 1)
795 return 0;
796 /*
797 * Check all characters up to first slash - 1. Only alphanum
798 * is allowed.
799 */
800 url2 = url;
801 while (url2 < first_slash - 1) {
802 if (!isalnum((unsigned char)*url2))
803 return 0;
804 url2++;
805 }
806
807 /* Valid enough. */
808 return 1;
809}
810
811static int external_specification_len(const char *url)
812{
813 return strchr(url, ':') - url;
814}
815
816struct transport *transport_get(struct remote *remote, const char *url)
817{
818 struct transport *ret = xcalloc(1, sizeof(*ret));
819
820 if (!remote)
821 die("No remote provided to transport_get()");
822
823 ret->remote = remote;
824
825 if (!url && remote && remote->url)
826 url = remote->url[0];
827 ret->url = url;
828
829 /* maybe it is a foreign URL? */
830 if (url) {
831 const char *p = url;
832
833 while (isalnum(*p))
834 p++;
835 if (!prefixcmp(p, "::"))
836 remote->foreign_vcs = xstrndup(url, p - url);
837 }
838
839 if (remote && remote->foreign_vcs) {
840 transport_helper_init(ret, remote->foreign_vcs);
841 } else if (!prefixcmp(url, "rsync:")) {
842 ret->get_refs_list = get_refs_via_rsync;
843 ret->fetch = fetch_objs_via_rsync;
844 ret->push = rsync_transport_push;
845 ret->smart_options = NULL;
846 } else if (is_local(url) && is_file(url)) {
847 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
848 ret->data = data;
849 ret->get_refs_list = get_refs_from_bundle;
850 ret->fetch = fetch_refs_from_bundle;
851 ret->disconnect = close_bundle;
852 ret->smart_options = NULL;
853 } else if (!is_url(url)
854 || !prefixcmp(url, "file://")
855 || !prefixcmp(url, "git://")
856 || !prefixcmp(url, "ssh://")
857 || !prefixcmp(url, "git+ssh://")
858 || !prefixcmp(url, "ssh+git://")) {
859 /* These are builtin smart transports. */
860 struct git_transport_data *data = xcalloc(1, sizeof(*data));
861 ret->data = data;
862 ret->set_option = NULL;
863 ret->get_refs_list = get_refs_via_connect;
864 ret->fetch = fetch_refs_via_pack;
865 ret->push_refs = git_transport_push;
866 ret->disconnect = disconnect_git;
867 ret->smart_options = &(data->options);
868
869 data->conn = NULL;
870 } else if (!prefixcmp(url, "http://")
871 || !prefixcmp(url, "https://")
872 || !prefixcmp(url, "ftp://")) {
873 /* These three are just plain special. */
874 transport_helper_init(ret, "curl");
875#ifdef NO_CURL
876 error("git was compiled without libcurl support.");
877#endif
878 } else {
879 /* Unknown protocol in URL. Pass to external handler. */
880 int len = external_specification_len(url);
881 char *handler = xmalloc(len + 1);
882 handler[len] = 0;
883 strncpy(handler, url, len);
884 transport_helper_init(ret, handler);
885 }
886
887 if (ret->smart_options) {
888 ret->smart_options->thin = 1;
889 ret->smart_options->uploadpack = "git-upload-pack";
890 if (remote->uploadpack)
891 ret->smart_options->uploadpack = remote->uploadpack;
892 ret->smart_options->receivepack = "git-receive-pack";
893 if (remote->receivepack)
894 ret->smart_options->receivepack = remote->receivepack;
895 }
896
897 return ret;
898}
899
900int transport_set_option(struct transport *transport,
901 const char *name, const char *value)
902{
903 int git_reports = 1, protocol_reports = 1;
904
905 if (transport->smart_options)
906 git_reports = set_git_option(transport->smart_options,
907 name, value);
908
909 if (transport->set_option)
910 protocol_reports = transport->set_option(transport, name,
911 value);
912
913 /* If either report is 0, report 0 (success). */
914 if (!git_reports || !protocol_reports)
915 return 0;
916 /* If either reports -1 (invalid value), report -1. */
917 if ((git_reports == -1) || (protocol_reports == -1))
918 return -1;
919 /* Otherwise if both report unknown, report unknown. */
920 return 1;
921}
922
923int transport_push(struct transport *transport,
924 int refspec_nr, const char **refspec, int flags,
925 int *nonfastforward)
926{
927 *nonfastforward = 0;
928 verify_remote_names(refspec_nr, refspec);
929
930 if (transport->push)
931 return transport->push(transport, refspec_nr, refspec, flags);
932 if (transport->push_refs) {
933 struct ref *remote_refs =
934 transport->get_refs_list(transport, 1);
935 struct ref *local_refs = get_local_heads();
936 int match_flags = MATCH_REFS_NONE;
937 int verbose = flags & TRANSPORT_PUSH_VERBOSE;
938 int quiet = flags & TRANSPORT_PUSH_QUIET;
939 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
940 int ret;
941
942 if (flags & TRANSPORT_PUSH_ALL)
943 match_flags |= MATCH_REFS_ALL;
944 if (flags & TRANSPORT_PUSH_MIRROR)
945 match_flags |= MATCH_REFS_MIRROR;
946
947 if (match_refs(local_refs, &remote_refs,
948 refspec_nr, refspec, match_flags)) {
949 return -1;
950 }
951
952 ret = transport->push_refs(transport, remote_refs, flags);
953
954 if (!quiet || push_had_errors(remote_refs))
955 print_push_status(transport->url, remote_refs,
956 verbose | porcelain, porcelain,
957 nonfastforward);
958
959 if (!(flags & TRANSPORT_PUSH_DRY_RUN)) {
960 struct ref *ref;
961 for (ref = remote_refs; ref; ref = ref->next)
962 update_tracking_ref(transport->remote, ref, verbose);
963 }
964
965 if (!quiet && !ret && !refs_pushed(remote_refs))
966 fprintf(stderr, "Everything up-to-date\n");
967 return ret;
968 }
969 return 1;
970}
971
972const struct ref *transport_get_remote_refs(struct transport *transport)
973{
974 if (!transport->remote_refs)
975 transport->remote_refs = transport->get_refs_list(transport, 0);
976 return transport->remote_refs;
977}
978
979int transport_fetch_refs(struct transport *transport, struct ref *refs)
980{
981 int rc;
982 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
983 struct ref **heads = NULL;
984 struct ref *rm;
985
986 for (rm = refs; rm; rm = rm->next) {
987 nr_refs++;
988 if (rm->peer_ref &&
989 !is_null_sha1(rm->old_sha1) &&
990 !hashcmp(rm->peer_ref->old_sha1, rm->old_sha1))
991 continue;
992 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
993 heads[nr_heads++] = rm;
994 }
995
996 if (!nr_heads) {
997 /*
998 * When deepening of a shallow repository is requested,
999 * then local and remote refs are likely to still be equal.
1000 * Just feed them all to the fetch method in that case.
1001 * This condition shouldn't be met in a non-deepening fetch
1002 * (see builtin-fetch.c:quickfetch()).
1003 */
1004 heads = xmalloc(nr_refs * sizeof(*heads));
1005 for (rm = refs; rm; rm = rm->next)
1006 heads[nr_heads++] = rm;
1007 }
1008
1009 rc = transport->fetch(transport, nr_heads, heads);
1010 free(heads);
1011 return rc;
1012}
1013
1014void transport_unlock_pack(struct transport *transport)
1015{
1016 if (transport->pack_lockfile) {
1017 unlink_or_warn(transport->pack_lockfile);
1018 free(transport->pack_lockfile);
1019 transport->pack_lockfile = NULL;
1020 }
1021}
1022
1023int transport_disconnect(struct transport *transport)
1024{
1025 int ret = 0;
1026 if (transport->disconnect)
1027 ret = transport->disconnect(transport);
1028 free(transport);
1029 return ret;
1030}
1031
1032/*
1033 * Strip username (and password) from an url and return
1034 * it in a newly allocated string.
1035 */
1036char *transport_anonymize_url(const char *url)
1037{
1038 char *anon_url, *scheme_prefix, *anon_part;
1039 size_t anon_len, prefix_len = 0;
1040
1041 anon_part = strchr(url, '@');
1042 if (is_local(url) || !anon_part)
1043 goto literal_copy;
1044
1045 anon_len = strlen(++anon_part);
1046 scheme_prefix = strstr(url, "://");
1047 if (!scheme_prefix) {
1048 if (!strchr(anon_part, ':'))
1049 /* cannot be "me@there:/path/name" */
1050 goto literal_copy;
1051 } else {
1052 const char *cp;
1053 /* make sure scheme is reasonable */
1054 for (cp = url; cp < scheme_prefix; cp++) {
1055 switch (*cp) {
1056 /* RFC 1738 2.1 */
1057 case '+': case '.': case '-':
1058 break; /* ok */
1059 default:
1060 if (isalnum(*cp))
1061 break;
1062 /* it isn't */
1063 goto literal_copy;
1064 }
1065 }
1066 /* @ past the first slash does not count */
1067 cp = strchr(scheme_prefix + 3, '/');
1068 if (cp && cp < anon_part)
1069 goto literal_copy;
1070 prefix_len = scheme_prefix - url + 3;
1071 }
1072 anon_url = xcalloc(1, 1 + prefix_len + anon_len);
1073 memcpy(anon_url, url, prefix_len);
1074 memcpy(anon_url + prefix_len, anon_part, anon_len);
1075 return anon_url;
1076literal_copy:
1077 return xstrdup(url);
1078}