Remove unnecessary local in get_ref_sha1.
[gitweb.git] / local-fetch.c
index 8176532320e6d11e6134d686091632d721e5e5c6..fa9e697fd3fa8fe305184f78fdf500532c14ab63 100644 (file)
@@ -38,6 +38,8 @@ static int setup_indices(void)
        unsigned char sha1[20];
        sprintf(filename, "%s/objects/pack/", path);
        dir = opendir(filename);
+       if (!dir)
+               return -1;
        while ((de = readdir(dir)) != NULL) {
                int namelen = strlen(de->d_name);
                if (namelen != 50 || 
@@ -46,11 +48,14 @@ static int setup_indices(void)
                get_sha1_hex(de->d_name + 5, sha1);
                setup_index(sha1);
        }
+       closedir(dir);
        return 0;
 }
 
-static int copy_file(const char *source, const char *dest, const char *hex)
+static int copy_file(const char *source, char *dest, const char *hex,
+                    int warn_if_not_exists)
 {
+       safe_create_leading_directories(dest);
        if (use_link) {
                if (!link(source, dest)) {
                        pull_say("link %s\n", hex);
@@ -58,34 +63,42 @@ static int copy_file(const char *source, const char *dest, const char *hex)
                }
                /* If we got ENOENT there is no point continuing. */
                if (errno == ENOENT) {
-                       fprintf(stderr, "does not exist %s\n", source);
+                       if (warn_if_not_exists)
+                               fprintf(stderr, "does not exist %s\n", source);
                        return -1;
                }
        }
-       if (use_symlink && !symlink(source, dest)) {
-               pull_say("symlink %s\n", hex);
-               return 0;
+       if (use_symlink) {
+               struct stat st;
+               if (stat(source, &st)) {
+                       if (!warn_if_not_exists && errno == ENOENT)
+                               return -1;
+                       fprintf(stderr, "cannot stat %s: %s\n", source,
+                               strerror(errno));
+                       return -1;
+               }
+               if (!symlink(source, dest)) {
+                       pull_say("symlink %s\n", hex);
+                       return 0;
+               }
        }
        if (use_filecopy) {
-               int ifd, ofd, status;
-               struct stat st;
-               void *map;
+               int ifd, ofd, status = 0;
+
                ifd = open(source, O_RDONLY);
-               if (ifd < 0 || fstat(ifd, &st) < 0) {
-                       close(ifd);
+               if (ifd < 0) {
+                       if (!warn_if_not_exists && errno == ENOENT)
+                               return -1;
                        fprintf(stderr, "cannot open %s\n", source);
                        return -1;
                }
-               map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, ifd, 0);
-               close(ifd);
-               if (map == MAP_FAILED) {
-                       fprintf(stderr, "cannot mmap %s\n", source);
+               ofd = open(dest, O_WRONLY | O_CREAT | O_EXCL, 0666);
+               if (ofd < 0) {
+                       fprintf(stderr, "cannot open %s\n", dest);
+                       close(ifd);
                        return -1;
                }
-               ofd = open(dest, O_WRONLY | O_CREAT | O_EXCL, 0666);
-               status = ((ofd < 0) ||
-                         (write(ofd, map, st.st_size) != st.st_size));
-               munmap(map, st.st_size);
+               status = copy_fd(ifd, ofd);
                close(ofd);
                if (status)
                        fprintf(stderr, "cannot write %s\n", dest);
@@ -116,11 +129,11 @@ static int fetch_pack(const unsigned char *sha1)
        sprintf(filename, "%s/objects/pack/pack-%s.pack", 
                path, sha1_to_hex(target->sha1));
        copy_file(filename, sha1_pack_name(target->sha1),
-                 sha1_to_hex(target->sha1));
+                 sha1_to_hex(target->sha1), 1);
        sprintf(filename, "%s/objects/pack/pack-%s.idx", 
                path, sha1_to_hex(target->sha1));
        copy_file(filename, sha1_pack_index_name(target->sha1),
-                 sha1_to_hex(target->sha1));
+                 sha1_to_hex(target->sha1), 1);
        install_packed_git(target);
        return 0;
 }
@@ -130,7 +143,7 @@ static int fetch_file(const unsigned char *sha1)
        static int object_name_start = -1;
        static char filename[PATH_MAX];
        char *hex = sha1_to_hex(sha1);
-       const char *dest_filename = sha1_file_name(sha1);
+       char *dest_filename = sha1_file_name(sha1);
 
        if (object_name_start < 0) {
                strcpy(filename, path); /* e.g. git.git */
@@ -141,12 +154,15 @@ static int fetch_file(const unsigned char *sha1)
        filename[object_name_start+1] = hex[1];
        filename[object_name_start+2] = '/';
        strcpy(filename + object_name_start + 3, hex + 2);
-       return copy_file(filename, dest_filename, hex);
+       return copy_file(filename, dest_filename, hex, 0);
 }
 
 int fetch(unsigned char *sha1)
 {
-       return fetch_file(sha1) && fetch_pack(sha1);
+       if (has_sha1_file(sha1))
+               return 0;
+       else
+               return fetch_file(sha1) && fetch_pack(sha1);
 }
 
 int fetch_ref(char *ref, unsigned char *sha1)
@@ -191,6 +207,8 @@ int main(int argc, char **argv)
        char *commit_id;
        int arg = 1;
 
+       setup_git_directory();
+
        while (arg < argc && argv[arg][0] == '-') {
                if (argv[arg][1] == 't')
                        get_tree = 1;
@@ -211,6 +229,8 @@ int main(int argc, char **argv)
                        get_verbosely = 1;
                else if (argv[arg][1] == 'w')
                        write_ref = argv[++arg];
+               else if (!strcmp(argv[arg], "--recover"))
+                       get_recover = 1;
                else
                        usage(local_pull_usage);
                arg++;