Code

ad25b98ae1d094167dfe0c9152b55e953409aa21
[git.git] / transport.c
1 #include "cache.h"
2 #include "transport.h"
3 #include "run-command.h"
4 #include "pkt-line.h"
5 #include "fetch-pack.h"
6 #include "send-pack.h"
7 #include "walker.h"
8 #include "bundle.h"
9 #include "dir.h"
10 #include "refs.h"
12 /* rsync support */
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  */
23 static int str_cmp(const void *a, const void *b)
24 {
25         const char *s1 = a;
26         const char *s2 = b;
28         return strcmp(s1, s2);
29 }
31 /* path->buf + name_offset is expected to point to "refs/" */
33 static 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;
44         if (!dir)
45                 return -1;
47         memset (&list, 0, sizeof(list));
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);
57         /* sort the list */
59         qsort(list.entries, list.nr, sizeof(char *), str_cmp);
61         pathlen = path->len;
62         strbuf_addch(path, '/');
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;
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);
87         for (i = 0; i < list.nr; i++)
88                 free(list.entries[i]);
89         free(list.entries);
91         return 0;
92 }
94 /* insert the packed refs for which no loose refs were found */
96 static 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];
101         if (!f)
102                 return;
104         for (;;) {
105                 int cmp = cmp, len;
107                 if (!fgets(buffer, sizeof(buffer), f)) {
108                         fclose(f);
109                         return;
110                 }
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         }
138 static const char *rsync_url(const char *url)
140         return prefixcmp(url, "rsync://") ? skip_prefix(url, "rsync:") : url;
143 static struct ref *get_refs_via_rsync(struct transport *transport, int for_push)
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;
151         if (for_push)
152                 return NULL;
154         /* copy the refs to the temporary directory */
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;
161         strbuf_addstr(&buf, rsync_url(transport->url));
162         strbuf_addstr(&buf, "/refs");
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;
173         if (run_command(&rsync))
174                 die ("Could not run rsync to get refs");
176         strbuf_reset(&buf);
177         strbuf_addstr(&buf, rsync_url(transport->url));
178         strbuf_addstr(&buf, "/packed-refs");
180         args[2] = buf.buf;
182         if (run_command(&rsync))
183                 die ("Could not run rsync to get refs");
185         /* read the copied refs */
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);
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);
196         if (remove_dir_recursively(&temp_dir, 0))
197                 warning ("Error removing temporary directory %s.",
198                                 temp_dir.buf);
200         strbuf_release(&buf);
201         strbuf_release(&temp_dir);
203         return dummy.next;
206 static int fetch_objs_via_rsync(struct transport *transport,
207                                 int nr_objs, struct ref **to_fetch)
209         struct strbuf buf = STRBUF_INIT;
210         struct child_process rsync;
211         const char *args[8];
212         int result;
214         strbuf_addstr(&buf, rsync_url(transport->url));
215         strbuf_addstr(&buf, "/objects/");
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;
229         /* NEEDSWORK: handle one level of alternates */
230         result = run_command(&rsync);
232         strbuf_release(&buf);
234         return result;
237 static int write_one_ref(const char *name, const unsigned char *sha1,
238                 int flags, void *data)
240         struct strbuf *buf = data;
241         int len = buf->len;
242         FILE *f;
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;
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;
259 static int write_refs_to_temp_dir(struct strbuf *temp_dir,
260                 int refspec_nr, const char **refspec)
262         int i;
264         for (i = 0; i < refspec_nr; i++) {
265                 unsigned char sha1[20];
266                 char *ref;
268                 if (dwim_ref(refspec[i], strlen(refspec[i]), sha1, &ref) != 1)
269                         return error("Could not get ref %s", refspec[i]);
271                 if (write_one_ref(ref, sha1, 0, temp_dir)) {
272                         free(ref);
273                         return -1;
274                 }
275                 free(ref);
276         }
277         return 0;
280 static int rsync_transport_push(struct transport *transport,
281                 int refspec_nr, const char **refspec, int flags)
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];
288         if (flags & TRANSPORT_PUSH_MIRROR)
289                 return error("rsync transport does not support mirror mode");
291         /* first push the objects */
293         strbuf_addstr(&buf, rsync_url(transport->url));
294         strbuf_addch(&buf, '/');
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;
313         if (run_command(&rsync))
314                 return error("Could not push objects to %s",
315                                 rsync_url(transport->url));
317         /* copy the refs to the temporary directory; they could be packed. */
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, '/');
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;
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));
342         if (remove_dir_recursively(&temp_dir, 0))
343                 warning ("Could not remove temporary directory %s.",
344                                 temp_dir.buf);
346         strbuf_release(&buf);
347         strbuf_release(&temp_dir);
349         return result;
352 struct bundle_transport_data {
353         int fd;
354         struct bundle_header header;
355 };
357 static struct ref *get_refs_from_bundle(struct transport *transport, int for_push)
359         struct bundle_transport_data *data = transport->data;
360         struct ref *result = NULL;
361         int i;
363         if (for_push)
364                 return NULL;
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;
381 static int fetch_refs_from_bundle(struct transport *transport,
382                                int nr_heads, struct ref **to_fetch)
384         struct bundle_transport_data *data = transport->data;
385         return unbundle(&data->header, data->fd);
388 static int close_bundle(struct transport *transport)
390         struct bundle_transport_data *data = transport->data;
391         if (data->fd > 0)
392                 close(data->fd);
393         free(data);
394         return 0;
397 struct git_transport_data {
398         struct git_transport_options options;
399         struct child_process *conn;
400         int fd[2];
401         unsigned got_remote_heads : 1;
402         struct extra_have_objects extra_have;
403 };
405 static int set_git_option(struct git_transport_options *opts,
406                           const char *name, const char *value)
408         if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
409                 opts->uploadpack = value;
410                 return 0;
411         } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
412                 opts->receivepack = value;
413                 return 0;
414         } else if (!strcmp(name, TRANS_OPT_THIN)) {
415                 opts->thin = !!value;
416                 return 0;
417         } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
418                 opts->followtags = !!value;
419                 return 0;
420         } else if (!strcmp(name, TRANS_OPT_KEEP)) {
421                 opts->keep = !!value;
422                 return 0;
423         } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
424                 if (!value)
425                         opts->depth = 0;
426                 else
427                         opts->depth = atoi(value);
428                 return 0;
429         }
430         return 1;
433 static int connect_setup(struct transport *transport, int for_push, int verbose)
435         struct git_transport_data *data = transport->data;
437         if (data->conn)
438                 return 0;
440         data->conn = git_connect(data->fd, transport->url,
441                                  for_push ? data->options.receivepack :
442                                  data->options.uploadpack,
443                                  verbose ? CONNECT_VERBOSE : 0);
445         return 0;
448 static struct ref *get_refs_via_connect(struct transport *transport, int for_push)
450         struct git_transport_data *data = transport->data;
451         struct ref *refs;
453         connect_setup(transport, for_push, 0);
454         get_remote_heads(data->fd[0], &refs, 0, NULL,
455                          for_push ? REF_NORMAL : 0, &data->extra_have);
456         data->got_remote_heads = 1;
458         return refs;
461 static int fetch_refs_via_pack(struct transport *transport,
462                                int nr_heads, struct ref **to_fetch)
464         struct git_transport_data *data = transport->data;
465         char **heads = xmalloc(nr_heads * sizeof(*heads));
466         char **origh = xmalloc(nr_heads * sizeof(*origh));
467         const struct ref *refs;
468         char *dest = xstrdup(transport->url);
469         struct fetch_pack_args args;
470         int i;
471         struct ref *refs_tmp = NULL;
473         memset(&args, 0, sizeof(args));
474         args.uploadpack = data->options.uploadpack;
475         args.keep_pack = data->options.keep;
476         args.lock_pack = 1;
477         args.use_thin_pack = data->options.thin;
478         args.include_tag = data->options.followtags;
479         args.verbose = (transport->verbose > 0);
480         args.quiet = (transport->verbose < 0);
481         args.no_progress = args.quiet || (!transport->progress && !isatty(1));
482         args.depth = data->options.depth;
484         for (i = 0; i < nr_heads; i++)
485                 origh[i] = heads[i] = xstrdup(to_fetch[i]->name);
487         if (!data->got_remote_heads) {
488                 connect_setup(transport, 0, 0);
489                 get_remote_heads(data->fd[0], &refs_tmp, 0, NULL, 0, NULL);
490                 data->got_remote_heads = 1;
491         }
493         refs = fetch_pack(&args, data->fd, data->conn,
494                           refs_tmp ? refs_tmp : transport->remote_refs,
495                           dest, nr_heads, heads, &transport->pack_lockfile);
496         close(data->fd[0]);
497         close(data->fd[1]);
498         if (finish_connect(data->conn))
499                 refs = NULL;
500         data->conn = NULL;
501         data->got_remote_heads = 0;
503         free_refs(refs_tmp);
505         for (i = 0; i < nr_heads; i++)
506                 free(origh[i]);
507         free(origh);
508         free(heads);
509         free(dest);
510         return (refs ? 0 : -1);
513 static int push_had_errors(struct ref *ref)
515         for (; ref; ref = ref->next) {
516                 switch (ref->status) {
517                 case REF_STATUS_NONE:
518                 case REF_STATUS_UPTODATE:
519                 case REF_STATUS_OK:
520                         break;
521                 default:
522                         return 1;
523                 }
524         }
525         return 0;
528 static int refs_pushed(struct ref *ref)
530         for (; ref; ref = ref->next) {
531                 switch(ref->status) {
532                 case REF_STATUS_NONE:
533                 case REF_STATUS_UPTODATE:
534                         break;
535                 default:
536                         return 1;
537                 }
538         }
539         return 0;
542 static void update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
544         struct refspec rs;
546         if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
547                 return;
549         rs.src = ref->name;
550         rs.dst = NULL;
552         if (!remote_find_tracking(remote, &rs)) {
553                 if (verbose)
554                         fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
555                 if (ref->deletion) {
556                         delete_ref(rs.dst, NULL, 0);
557                 } else
558                         update_ref("update by push", rs.dst,
559                                         ref->new_sha1, NULL, 0, 0);
560                 free(rs.dst);
561         }
564 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
566 static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg, int porcelain)
568         if (porcelain) {
569                 if (from)
570                         fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
571                 else
572                         fprintf(stdout, "%c\t:%s\t", flag, to->name);
573                 if (msg)
574                         fprintf(stdout, "%s (%s)\n", summary, msg);
575                 else
576                         fprintf(stdout, "%s\n", summary);
577         } else {
578                 fprintf(stderr, " %c %-*s ", flag, SUMMARY_WIDTH, summary);
579                 if (from)
580                         fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
581                 else
582                         fputs(prettify_refname(to->name), stderr);
583                 if (msg) {
584                         fputs(" (", stderr);
585                         fputs(msg, stderr);
586                         fputc(')', stderr);
587                 }
588                 fputc('\n', stderr);
589         }
592 static const char *status_abbrev(unsigned char sha1[20])
594         return find_unique_abbrev(sha1, DEFAULT_ABBREV);
597 static void print_ok_ref_status(struct ref *ref, int porcelain)
599         if (ref->deletion)
600                 print_ref_status('-', "[deleted]", ref, NULL, NULL, porcelain);
601         else if (is_null_sha1(ref->old_sha1))
602                 print_ref_status('*',
603                         (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" :
604                         "[new branch]"),
605                         ref, ref->peer_ref, NULL, porcelain);
606         else {
607                 char quickref[84];
608                 char type;
609                 const char *msg;
611                 strcpy(quickref, status_abbrev(ref->old_sha1));
612                 if (ref->nonfastforward) {
613                         strcat(quickref, "...");
614                         type = '+';
615                         msg = "forced update";
616                 } else {
617                         strcat(quickref, "..");
618                         type = ' ';
619                         msg = NULL;
620                 }
621                 strcat(quickref, status_abbrev(ref->new_sha1));
623                 print_ref_status(type, quickref, ref, ref->peer_ref, msg, porcelain);
624         }
627 static int print_one_push_status(struct ref *ref, const char *dest, int count, int porcelain)
629         if (!count)
630                 fprintf(stderr, "To %s\n", dest);
632         switch(ref->status) {
633         case REF_STATUS_NONE:
634                 print_ref_status('X', "[no match]", ref, NULL, NULL, porcelain);
635                 break;
636         case REF_STATUS_REJECT_NODELETE:
637                 print_ref_status('!', "[rejected]", ref, NULL,
638                                                  "remote does not support deleting refs", porcelain);
639                 break;
640         case REF_STATUS_UPTODATE:
641                 print_ref_status('=', "[up to date]", ref,
642                                                  ref->peer_ref, NULL, porcelain);
643                 break;
644         case REF_STATUS_REJECT_NONFASTFORWARD:
645                 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
646                                                  "non-fast-forward", porcelain);
647                 break;
648         case REF_STATUS_REMOTE_REJECT:
649                 print_ref_status('!', "[remote rejected]", ref,
650                                                  ref->deletion ? NULL : ref->peer_ref,
651                                                  ref->remote_status, porcelain);
652                 break;
653         case REF_STATUS_EXPECTING_REPORT:
654                 print_ref_status('!', "[remote failure]", ref,
655                                                  ref->deletion ? NULL : ref->peer_ref,
656                                                  "remote failed to report status", porcelain);
657                 break;
658         case REF_STATUS_OK:
659                 print_ok_ref_status(ref, porcelain);
660                 break;
661         }
663         return 1;
666 static void print_push_status(const char *dest, struct ref *refs,
667                               int verbose, int porcelain, int * nonfastforward)
669         struct ref *ref;
670         int n = 0;
672         if (verbose) {
673                 for (ref = refs; ref; ref = ref->next)
674                         if (ref->status == REF_STATUS_UPTODATE)
675                                 n += print_one_push_status(ref, dest, n, porcelain);
676         }
678         for (ref = refs; ref; ref = ref->next)
679                 if (ref->status == REF_STATUS_OK)
680                         n += print_one_push_status(ref, dest, n, porcelain);
682         *nonfastforward = 0;
683         for (ref = refs; ref; ref = ref->next) {
684                 if (ref->status != REF_STATUS_NONE &&
685                     ref->status != REF_STATUS_UPTODATE &&
686                     ref->status != REF_STATUS_OK)
687                         n += print_one_push_status(ref, dest, n, porcelain);
688                 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD)
689                         *nonfastforward = 1;
690         }
693 static void verify_remote_names(int nr_heads, const char **heads)
695         int i;
697         for (i = 0; i < nr_heads; i++) {
698                 const char *local = heads[i];
699                 const char *remote = strrchr(heads[i], ':');
701                 if (*local == '+')
702                         local++;
704                 /* A matching refspec is okay.  */
705                 if (remote == local && remote[1] == '\0')
706                         continue;
708                 remote = remote ? (remote + 1) : local;
709                 switch (check_ref_format(remote)) {
710                 case 0: /* ok */
711                 case CHECK_REF_FORMAT_ONELEVEL:
712                         /* ok but a single level -- that is fine for
713                          * a match pattern.
714                          */
715                 case CHECK_REF_FORMAT_WILDCARD:
716                         /* ok but ends with a pattern-match character */
717                         continue;
718                 }
719                 die("remote part of refspec is not a valid name in %s",
720                     heads[i]);
721         }
724 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
726         struct git_transport_data *data = transport->data;
727         struct send_pack_args args;
728         int ret;
730         if (!data->got_remote_heads) {
731                 struct ref *tmp_refs;
732                 connect_setup(transport, 1, 0);
734                 get_remote_heads(data->fd[0], &tmp_refs, 0, NULL, REF_NORMAL,
735                                  NULL);
736                 data->got_remote_heads = 1;
737         }
739         memset(&args, 0, sizeof(args));
740         args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
741         args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
742         args.use_thin_pack = data->options.thin;
743         args.verbose = !!(flags & TRANSPORT_PUSH_VERBOSE);
744         args.quiet = !!(flags & TRANSPORT_PUSH_QUIET);
745         args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
747         ret = send_pack(&args, data->fd, data->conn, remote_refs,
748                         &data->extra_have);
750         close(data->fd[1]);
751         close(data->fd[0]);
752         ret |= finish_connect(data->conn);
753         data->conn = NULL;
754         data->got_remote_heads = 0;
756         return ret;
759 static int disconnect_git(struct transport *transport)
761         struct git_transport_data *data = transport->data;
762         if (data->conn) {
763                 if (data->got_remote_heads)
764                         packet_flush(data->fd[1]);
765                 close(data->fd[0]);
766                 close(data->fd[1]);
767                 finish_connect(data->conn);
768         }
770         free(data);
771         return 0;
774 void transport_take_over(struct transport *transport,
775                          struct child_process *child)
777         struct git_transport_data *data;
779         if (!transport->smart_options)
780                 die("Bug detected: Taking over transport requires non-NULL "
781                     "smart_options field.");
783         data = xcalloc(1, sizeof(*data));
784         data->options = *transport->smart_options;
785         data->conn = child;
786         data->fd[0] = data->conn->out;
787         data->fd[1] = data->conn->in;
788         data->got_remote_heads = 0;
789         transport->data = data;
791         transport->set_option = NULL;
792         transport->get_refs_list = get_refs_via_connect;
793         transport->fetch = fetch_refs_via_pack;
794         transport->push = NULL;
795         transport->push_refs = git_transport_push;
796         transport->disconnect = disconnect_git;
797         transport->smart_options = &(data->options);
800 static int is_local(const char *url)
802         const char *colon = strchr(url, ':');
803         const char *slash = strchr(url, '/');
804         return !colon || (slash && slash < colon) ||
805                 has_dos_drive_prefix(url);
808 static int is_file(const char *url)
810         struct stat buf;
811         if (stat(url, &buf))
812                 return 0;
813         return S_ISREG(buf.st_mode);
816 static int is_url(const char *url)
818         const char *url2, *first_slash;
820         if (!url)
821                 return 0;
822         url2 = url;
823         first_slash = strchr(url, '/');
825         /* Input with no slash at all or slash first can't be URL. */
826         if (!first_slash || first_slash == url)
827                 return 0;
828         /* Character before must be : and next must be /. */
829         if (first_slash[-1] != ':' || first_slash[1] != '/')
830                 return 0;
831         /* There must be something before the :// */
832         if (first_slash == url + 1)
833                 return 0;
834         /*
835          * Check all characters up to first slash - 1. Only alphanum
836          * is allowed.
837          */
838         url2 = url;
839         while (url2 < first_slash - 1) {
840                 if (!isalnum((unsigned char)*url2))
841                         return 0;
842                 url2++;
843         }
845         /* Valid enough. */
846         return 1;
849 static int external_specification_len(const char *url)
851         return strchr(url, ':') - url;
854 struct transport *transport_get(struct remote *remote, const char *url)
856         struct transport *ret = xcalloc(1, sizeof(*ret));
858         if (!remote)
859                 die("No remote provided to transport_get()");
861         ret->remote = remote;
863         if (!url && remote && remote->url)
864                 url = remote->url[0];
865         ret->url = url;
867         /* maybe it is a foreign URL? */
868         if (url) {
869                 const char *p = url;
871                 while (isalnum(*p))
872                         p++;
873                 if (!prefixcmp(p, "::"))
874                         remote->foreign_vcs = xstrndup(url, p - url);
875         }
877         if (remote && remote->foreign_vcs) {
878                 transport_helper_init(ret, remote->foreign_vcs);
879         } else if (!prefixcmp(url, "rsync:")) {
880                 ret->get_refs_list = get_refs_via_rsync;
881                 ret->fetch = fetch_objs_via_rsync;
882                 ret->push = rsync_transport_push;
883                 ret->smart_options = NULL;
884         } else if (is_local(url) && is_file(url)) {
885                 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
886                 ret->data = data;
887                 ret->get_refs_list = get_refs_from_bundle;
888                 ret->fetch = fetch_refs_from_bundle;
889                 ret->disconnect = close_bundle;
890                 ret->smart_options = NULL;
891         } else if (!is_url(url)
892                 || !prefixcmp(url, "file://")
893                 || !prefixcmp(url, "git://")
894                 || !prefixcmp(url, "ssh://")
895                 || !prefixcmp(url, "git+ssh://")
896                 || !prefixcmp(url, "ssh+git://")) {
897                 /* These are builtin smart transports. */
898                 struct git_transport_data *data = xcalloc(1, sizeof(*data));
899                 ret->data = data;
900                 ret->set_option = NULL;
901                 ret->get_refs_list = get_refs_via_connect;
902                 ret->fetch = fetch_refs_via_pack;
903                 ret->push_refs = git_transport_push;
904                 ret->disconnect = disconnect_git;
905                 ret->smart_options = &(data->options);
907                 data->conn = NULL;
908                 data->got_remote_heads = 0;
909         } else if (!prefixcmp(url, "http://")
910                 || !prefixcmp(url, "https://")
911                 || !prefixcmp(url, "ftp://")) {
912                 /* These three are just plain special. */
913                 transport_helper_init(ret, "curl");
914 #ifdef NO_CURL
915                 error("git was compiled without libcurl support.");
916 #endif
917         } else {
918                 /* Unknown protocol in URL. Pass to external handler. */
919                 int len = external_specification_len(url);
920                 char *handler = xmalloc(len + 1);
921                 handler[len] = 0;
922                 strncpy(handler, url, len);
923                 transport_helper_init(ret, handler);
924         }
926         if (ret->smart_options) {
927                 ret->smart_options->thin = 1;
928                 ret->smart_options->uploadpack = "git-upload-pack";
929                 if (remote->uploadpack)
930                         ret->smart_options->uploadpack = remote->uploadpack;
931                 ret->smart_options->receivepack = "git-receive-pack";
932                 if (remote->receivepack)
933                         ret->smart_options->receivepack = remote->receivepack;
934         }
936         return ret;
939 int transport_set_option(struct transport *transport,
940                          const char *name, const char *value)
942         int git_reports = 1, protocol_reports = 1;
944         if (transport->smart_options)
945                 git_reports = set_git_option(transport->smart_options,
946                                              name, value);
948         if (transport->set_option)
949                 protocol_reports = transport->set_option(transport, name,
950                                                         value);
952         /* If either report is 0, report 0 (success). */
953         if (!git_reports || !protocol_reports)
954                 return 0;
955         /* If either reports -1 (invalid value), report -1. */
956         if ((git_reports == -1) || (protocol_reports == -1))
957                 return -1;
958         /* Otherwise if both report unknown, report unknown. */
959         return 1;
962 int transport_push(struct transport *transport,
963                    int refspec_nr, const char **refspec, int flags,
964                    int *nonfastforward)
966         *nonfastforward = 0;
967         verify_remote_names(refspec_nr, refspec);
969         if (transport->push) {
970                 return transport->push(transport, refspec_nr, refspec, flags);
971         } else if (transport->push_refs) {
972                 struct ref *remote_refs =
973                         transport->get_refs_list(transport, 1);
974                 struct ref *local_refs = get_local_heads();
975                 int match_flags = MATCH_REFS_NONE;
976                 int verbose = flags & TRANSPORT_PUSH_VERBOSE;
977                 int quiet = flags & TRANSPORT_PUSH_QUIET;
978                 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
979                 int ret;
981                 if (flags & TRANSPORT_PUSH_ALL)
982                         match_flags |= MATCH_REFS_ALL;
983                 if (flags & TRANSPORT_PUSH_MIRROR)
984                         match_flags |= MATCH_REFS_MIRROR;
986                 if (match_refs(local_refs, &remote_refs,
987                                refspec_nr, refspec, match_flags)) {
988                         return -1;
989                 }
991                 ret = transport->push_refs(transport, remote_refs, flags);
993                 if (!quiet || push_had_errors(remote_refs))
994                         print_push_status(transport->url, remote_refs,
995                                         verbose | porcelain, porcelain,
996                                         nonfastforward);
998                 if (!(flags & TRANSPORT_PUSH_DRY_RUN)) {
999                         struct ref *ref;
1000                         for (ref = remote_refs; ref; ref = ref->next)
1001                                 update_tracking_ref(transport->remote, ref, verbose);
1002                 }
1004                 if (!quiet && !ret && !refs_pushed(remote_refs))
1005                         fprintf(stderr, "Everything up-to-date\n");
1006                 return ret;
1007         }
1008         return 1;
1011 const struct ref *transport_get_remote_refs(struct transport *transport)
1013         if (!transport->remote_refs)
1014                 transport->remote_refs = transport->get_refs_list(transport, 0);
1016         return transport->remote_refs;
1019 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1021         int rc;
1022         int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1023         struct ref **heads = NULL;
1024         struct ref *rm;
1026         for (rm = refs; rm; rm = rm->next) {
1027                 nr_refs++;
1028                 if (rm->peer_ref &&
1029                     !is_null_sha1(rm->old_sha1) &&
1030                     !hashcmp(rm->peer_ref->old_sha1, rm->old_sha1))
1031                         continue;
1032                 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1033                 heads[nr_heads++] = rm;
1034         }
1036         if (!nr_heads) {
1037                 /*
1038                  * When deepening of a shallow repository is requested,
1039                  * then local and remote refs are likely to still be equal.
1040                  * Just feed them all to the fetch method in that case.
1041                  * This condition shouldn't be met in a non-deepening fetch
1042                  * (see builtin-fetch.c:quickfetch()).
1043                  */
1044                 heads = xmalloc(nr_refs * sizeof(*heads));
1045                 for (rm = refs; rm; rm = rm->next)
1046                         heads[nr_heads++] = rm;
1047         }
1049         rc = transport->fetch(transport, nr_heads, heads);
1051         free(heads);
1052         return rc;
1055 void transport_unlock_pack(struct transport *transport)
1057         if (transport->pack_lockfile) {
1058                 unlink_or_warn(transport->pack_lockfile);
1059                 free(transport->pack_lockfile);
1060                 transport->pack_lockfile = NULL;
1061         }
1064 int transport_disconnect(struct transport *transport)
1066         int ret = 0;
1067         if (transport->disconnect)
1068                 ret = transport->disconnect(transport);
1069         free(transport);
1070         return ret;
1073 /*
1074  * Strip username (and password) from an url and return
1075  * it in a newly allocated string.
1076  */
1077 char *transport_anonymize_url(const char *url)
1079         char *anon_url, *scheme_prefix, *anon_part;
1080         size_t anon_len, prefix_len = 0;
1082         anon_part = strchr(url, '@');
1083         if (is_local(url) || !anon_part)
1084                 goto literal_copy;
1086         anon_len = strlen(++anon_part);
1087         scheme_prefix = strstr(url, "://");
1088         if (!scheme_prefix) {
1089                 if (!strchr(anon_part, ':'))
1090                         /* cannot be "me@there:/path/name" */
1091                         goto literal_copy;
1092         } else {
1093                 const char *cp;
1094                 /* make sure scheme is reasonable */
1095                 for (cp = url; cp < scheme_prefix; cp++) {
1096                         switch (*cp) {
1097                                 /* RFC 1738 2.1 */
1098                         case '+': case '.': case '-':
1099                                 break; /* ok */
1100                         default:
1101                                 if (isalnum(*cp))
1102                                         break;
1103                                 /* it isn't */
1104                                 goto literal_copy;
1105                         }
1106                 }
1107                 /* @ past the first slash does not count */
1108                 cp = strchr(scheme_prefix + 3, '/');
1109                 if (cp && cp < anon_part)
1110                         goto literal_copy;
1111                 prefix_len = scheme_prefix - url + 3;
1112         }
1113         anon_url = xcalloc(1, 1 + prefix_len + anon_len);
1114         memcpy(anon_url, url, prefix_len);
1115         memcpy(anon_url + prefix_len, anon_part, anon_len);
1116         return anon_url;
1117 literal_copy:
1118         return xstrdup(url);