6216c68a4cc64b90efc66fb6dd26065b1939e91c
   1/*
   2 * Copyright (C) 2005 Junio C Hamano
   3 */
   4#include "cache.h"
   5#include "commit.h"
   6#include "fetch.h"
   7
   8static int use_link = 0;
   9static int use_symlink = 0;
  10static int use_filecopy = 1;
  11
  12static char *path; /* "Remote" git repository */
  13
  14void prefetch(unsigned char *sha1)
  15{
  16}
  17
  18static struct packed_git *packs = NULL;
  19
  20static void setup_index(unsigned char *sha1)
  21{
  22        struct packed_git *new_pack;
  23        char filename[PATH_MAX];
  24        strcpy(filename, path);
  25        strcat(filename, "/objects/pack/pack-");
  26        strcat(filename, sha1_to_hex(sha1));
  27        strcat(filename, ".idx");
  28        new_pack = parse_pack_index_file(sha1, filename);
  29        new_pack->next = packs;
  30        packs = new_pack;
  31}
  32
  33static int setup_indices(void)
  34{
  35        DIR *dir;
  36        struct dirent *de;
  37        char filename[PATH_MAX];
  38        unsigned char sha1[20];
  39        sprintf(filename, "%s/objects/pack/", path);
  40        dir = opendir(filename);
  41        if (!dir)
  42                return -1;
  43        while ((de = readdir(dir)) != NULL) {
  44                int namelen = strlen(de->d_name);
  45                if (namelen != 50 || 
  46                    strcmp(de->d_name + namelen - 5, ".pack"))
  47                        continue;
  48                get_sha1_hex(de->d_name + 5, sha1);
  49                setup_index(sha1);
  50        }
  51        closedir(dir);
  52        return 0;
  53}
  54
  55static int copy_file(const char *source, const char *dest, const char *hex)
  56{
  57        if (use_link) {
  58                if (!link(source, dest)) {
  59                        pull_say("link %s\n", hex);
  60                        return 0;
  61                }
  62                /* If we got ENOENT there is no point continuing. */
  63                if (errno == ENOENT) {
  64                        fprintf(stderr, "does not exist %s\n", source);
  65                        return -1;
  66                }
  67        }
  68        if (use_symlink) {
  69                struct stat st;
  70                if (stat(source, &st)) {
  71                        fprintf(stderr, "cannot stat %s: %s\n", source,
  72                                strerror(errno));
  73                        return -1;
  74                }
  75                if (!symlink(source, dest)) {
  76                        pull_say("symlink %s\n", hex);
  77                        return 0;
  78                }
  79        }
  80        if (use_filecopy) {
  81                int ifd, ofd, status;
  82                struct stat st;
  83                void *map;
  84                ifd = open(source, O_RDONLY);
  85                if (ifd < 0 || fstat(ifd, &st) < 0) {
  86                        if (ifd >= 0)
  87                                close(ifd);
  88                        fprintf(stderr, "cannot open %s\n", source);
  89                        return -1;
  90                }
  91                map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, ifd, 0);
  92                close(ifd);
  93                if (map == MAP_FAILED) {
  94                        fprintf(stderr, "cannot mmap %s\n", source);
  95                        return -1;
  96                }
  97                ofd = open(dest, O_WRONLY | O_CREAT | O_EXCL, 0666);
  98                status = ((ofd < 0) ||
  99                          (write(ofd, map, st.st_size) != st.st_size));
 100                munmap(map, st.st_size);
 101                if (ofd >= 0)
 102                        close(ofd);
 103                if (status)
 104                        fprintf(stderr, "cannot write %s\n", dest);
 105                else
 106                        pull_say("copy %s\n", hex);
 107                return status;
 108        }
 109        fprintf(stderr, "failed to copy %s with given copy methods.\n", hex);
 110        return -1;
 111}
 112
 113static int fetch_pack(const unsigned char *sha1)
 114{
 115        struct packed_git *target;
 116        char filename[PATH_MAX];
 117        if (setup_indices())
 118                return -1;
 119        target = find_sha1_pack(sha1, packs);
 120        if (!target)
 121                return error("Couldn't find %s: not separate or in any pack", 
 122                             sha1_to_hex(sha1));
 123        if (get_verbosely) {
 124                fprintf(stderr, "Getting pack %s\n",
 125                        sha1_to_hex(target->sha1));
 126                fprintf(stderr, " which contains %s\n",
 127                        sha1_to_hex(sha1));
 128        }
 129        sprintf(filename, "%s/objects/pack/pack-%s.pack", 
 130                path, sha1_to_hex(target->sha1));
 131        copy_file(filename, sha1_pack_name(target->sha1),
 132                  sha1_to_hex(target->sha1));
 133        sprintf(filename, "%s/objects/pack/pack-%s.idx", 
 134                path, sha1_to_hex(target->sha1));
 135        copy_file(filename, sha1_pack_index_name(target->sha1),
 136                  sha1_to_hex(target->sha1));
 137        install_packed_git(target);
 138        return 0;
 139}
 140
 141static int fetch_file(const unsigned char *sha1)
 142{
 143        static int object_name_start = -1;
 144        static char filename[PATH_MAX];
 145        char *hex = sha1_to_hex(sha1);
 146        const char *dest_filename = sha1_file_name(sha1);
 147
 148        if (object_name_start < 0) {
 149                strcpy(filename, path); /* e.g. git.git */
 150                strcat(filename, "/objects/");
 151                object_name_start = strlen(filename);
 152        }
 153        filename[object_name_start+0] = hex[0];
 154        filename[object_name_start+1] = hex[1];
 155        filename[object_name_start+2] = '/';
 156        strcpy(filename + object_name_start + 3, hex + 2);
 157        return copy_file(filename, dest_filename, hex);
 158}
 159
 160int fetch(unsigned char *sha1)
 161{
 162        return fetch_file(sha1) && fetch_pack(sha1);
 163}
 164
 165int fetch_ref(char *ref, unsigned char *sha1)
 166{
 167        static int ref_name_start = -1;
 168        static char filename[PATH_MAX];
 169        static char hex[41];
 170        int ifd;
 171
 172        if (ref_name_start < 0) {
 173                sprintf(filename, "%s/refs/", path);
 174                ref_name_start = strlen(filename);
 175        }
 176        strcpy(filename + ref_name_start, ref);
 177        ifd = open(filename, O_RDONLY);
 178        if (ifd < 0) {
 179                close(ifd);
 180                fprintf(stderr, "cannot open %s\n", filename);
 181                return -1;
 182        }
 183        if (read(ifd, hex, 40) != 40 || get_sha1_hex(hex, sha1)) {
 184                close(ifd);
 185                fprintf(stderr, "cannot read from %s\n", filename);
 186                return -1;
 187        }
 188        close(ifd);
 189        pull_say("ref %s\n", sha1_to_hex(sha1));
 190        return 0;
 191}
 192
 193static const char local_pull_usage[] =
 194"git-local-fetch [-c] [-t] [-a] [-d] [-v] [-w filename] [--recover] [-l] [-s] [-n] commit-id path";
 195
 196/* 
 197 * By default we only use file copy.
 198 * If -l is specified, a hard link is attempted.
 199 * If -s is specified, then a symlink is attempted.
 200 * If -n is _not_ specified, then a regular file-to-file copy is done.
 201 */
 202int main(int argc, char **argv)
 203{
 204        char *commit_id;
 205        int arg = 1;
 206
 207        while (arg < argc && argv[arg][0] == '-') {
 208                if (argv[arg][1] == 't')
 209                        get_tree = 1;
 210                else if (argv[arg][1] == 'c')
 211                        get_history = 1;
 212                else if (argv[arg][1] == 'a') {
 213                        get_all = 1;
 214                        get_tree = 1;
 215                        get_history = 1;
 216                }
 217                else if (argv[arg][1] == 'l')
 218                        use_link = 1;
 219                else if (argv[arg][1] == 's')
 220                        use_symlink = 1;
 221                else if (argv[arg][1] == 'n')
 222                        use_filecopy = 0;
 223                else if (argv[arg][1] == 'v')
 224                        get_verbosely = 1;
 225                else if (argv[arg][1] == 'w')
 226                        write_ref = argv[++arg];
 227                else
 228                        usage(local_pull_usage);
 229                arg++;
 230        }
 231        if (argc < arg + 2)
 232                usage(local_pull_usage);
 233        commit_id = argv[arg];
 234        path = argv[arg + 1];
 235
 236        if (pull(commit_id))
 237                return 1;
 238
 239        return 0;
 240}