packfile: allow find_pack_entry to handle arbitrary repositories
[gitweb.git] / sha1_file.c
index aed9a558c5824b540423c3f39c384cf7fe7e4e7e..314ff55b4769a4262a76e3fa4c639f7d9c9f4b88 100644 (file)
@@ -867,23 +867,22 @@ int git_open_cloexec(const char *name, int flags)
  * Note that it may point to static storage and is only valid until another
  * call to sha1_file_name(), etc.
  */
-#define stat_sha1_file(r, s, st, p) stat_sha1_file_##r(s, st, p)
-static int stat_sha1_file_the_repository(const unsigned char *sha1,
-                                        struct stat *st, const char **path)
+static int stat_sha1_file(struct repository *r, const unsigned char *sha1,
+                         struct stat *st, const char **path)
 {
        struct alternate_object_database *alt;
        static struct strbuf buf = STRBUF_INIT;
 
        strbuf_reset(&buf);
-       sha1_file_name(the_repository, &buf, sha1);
+       sha1_file_name(r, &buf, sha1);
        *path = buf.buf;
 
        if (!lstat(*path, st))
                return 0;
 
-       prepare_alt_odb(the_repository);
+       prepare_alt_odb(r);
        errno = ENOENT;
-       for (alt = the_repository->objects->alt_odb_list; alt; alt = alt->next) {
+       for (alt = r->objects->alt_odb_list; alt; alt = alt->next) {
                *path = alt_sha1_path(alt, sha1);
                if (!lstat(*path, st))
                        return 0;
@@ -896,9 +895,8 @@ static int stat_sha1_file_the_repository(const unsigned char *sha1,
  * Like stat_sha1_file(), but actually open the object and return the
  * descriptor. See the caveats on the "path" parameter above.
  */
-#define open_sha1_file(r, s, p) open_sha1_file_##r(s, p)
-static int open_sha1_file_the_repository(const unsigned char *sha1,
-                                        const char **path)
+static int open_sha1_file(struct repository *r,
+                         const unsigned char *sha1, const char **path)
 {
        int fd;
        struct alternate_object_database *alt;
@@ -906,7 +904,7 @@ static int open_sha1_file_the_repository(const unsigned char *sha1,
        static struct strbuf buf = STRBUF_INIT;
 
        strbuf_reset(&buf);
-       sha1_file_name(the_repository, &buf, sha1);
+       sha1_file_name(r, &buf, sha1);
        *path = buf.buf;
 
        fd = git_open(*path);
@@ -914,8 +912,8 @@ static int open_sha1_file_the_repository(const unsigned char *sha1,
                return fd;
        most_interesting_errno = errno;
 
-       prepare_alt_odb(the_repository);
-       for (alt = the_repository->objects->alt_odb_list; alt; alt = alt->next) {
+       prepare_alt_odb(r);
+       for (alt = r->objects->alt_odb_list; alt; alt = alt->next) {
                *path = alt_sha1_path(alt, sha1);
                fd = git_open(*path);
                if (fd >= 0)
@@ -931,10 +929,8 @@ static int open_sha1_file_the_repository(const unsigned char *sha1,
  * Map the loose object at "path" if it is not NULL, or the path found by
  * searching for a loose object named "sha1".
  */
-#define map_sha1_file_1(r, p, s, si) map_sha1_file_1_##r(p, s, si)
-static void *map_sha1_file_1_the_repository(const char *path,
-                                           const unsigned char *sha1,
-                                           unsigned long *size)
+static void *map_sha1_file_1(struct repository *r, const char *path,
+                            const unsigned char *sha1, unsigned long *size)
 {
        void *map;
        int fd;
@@ -942,7 +938,7 @@ static void *map_sha1_file_1_the_repository(const char *path,
        if (path)
                fd = git_open(path);
        else
-               fd = open_sha1_file(the_repository, sha1, &path);
+               fd = open_sha1_file(r, sha1, &path);
        map = NULL;
        if (fd >= 0) {
                struct stat st;
@@ -961,9 +957,10 @@ static void *map_sha1_file_1_the_repository(const char *path,
        return map;
 }
 
-void *map_sha1_file_the_repository(const unsigned char *sha1, unsigned long *size)
+void *map_sha1_file(struct repository *r,
+                   const unsigned char *sha1, unsigned long *size)
 {
-       return map_sha1_file_1(the_repository, NULL, sha1, size);
+       return map_sha1_file_1(r, NULL, sha1, size);
 }
 
 static int unpack_sha1_short_header(git_zstream *stream,
@@ -1152,10 +1149,9 @@ int parse_sha1_header(const char *hdr, unsigned long *sizep)
        return parse_sha1_header_extended(hdr, &oi, 0);
 }
 
-#define sha1_loose_object_info(r, s, o, f) sha1_loose_object_info_##r(s, o, f)
-static int sha1_loose_object_info_the_repository(const unsigned char *sha1,
-                                                struct object_info *oi,
-                                                int flags)
+static int sha1_loose_object_info(struct repository *r,
+                                 const unsigned char *sha1,
+                                 struct object_info *oi, int flags)
 {
        int status = 0;
        unsigned long mapsize;
@@ -1179,14 +1175,14 @@ static int sha1_loose_object_info_the_repository(const unsigned char *sha1,
        if (!oi->typep && !oi->typename && !oi->sizep && !oi->contentp) {
                const char *path;
                struct stat st;
-               if (stat_sha1_file(the_repository, sha1, &st, &path) < 0)
+               if (stat_sha1_file(r, sha1, &st, &path) < 0)
                        return -1;
                if (oi->disk_sizep)
                        *oi->disk_sizep = st.st_size;
                return 0;
        }
 
-       map = map_sha1_file(the_repository, sha1, &mapsize);
+       map = map_sha1_file(r, sha1, &mapsize);
        if (!map)
                return -1;
 
@@ -1270,7 +1266,7 @@ int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi,
        }
 
        while (1) {
-               if (find_pack_entry(real, &e))
+               if (find_pack_entry(the_repository, real, &e))
                        break;
 
                /* Most likely it's a loose object. */
@@ -1278,8 +1274,8 @@ int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi,
                        return 0;
 
                /* Not a loose object; someone else may have just packed it. */
-               reprepare_packed_git();
-               if (find_pack_entry(real, &e))
+               reprepare_packed_git(the_repository);
+               if (find_pack_entry(the_repository, real, &e))
                        break;
 
                /* Check if it is a missing object */
@@ -1659,7 +1655,7 @@ static int freshen_loose_object(const unsigned char *sha1)
 static int freshen_packed_object(const unsigned char *sha1)
 {
        struct pack_entry e;
-       if (!find_pack_entry(sha1, &e))
+       if (!find_pack_entry(the_repository, sha1, &e))
                return 0;
        if (e.p->freshened)
                return 1;