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