sha1_file: do not access pack if unneeded
[gitweb.git] / sha1_file.c
index 0c996370de83ef96be93282d8af0413904161c1a..bf6b64ec8f5053d6e705fb887ea1969ef4493fe2 100644 (file)
@@ -2005,19 +2005,6 @@ int parse_sha1_header(const char *hdr, unsigned long *sizep)
        return parse_sha1_header_extended(hdr, &oi, 0);
 }
 
-static void *unpack_sha1_file(void *map, unsigned long mapsize, enum object_type *type, unsigned long *size, const unsigned char *sha1)
-{
-       int ret;
-       git_zstream stream;
-       char hdr[8192];
-
-       ret = unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr));
-       if (ret < Z_OK || (*type = parse_sha1_header(hdr, size)) < 0)
-               return NULL;
-
-       return unpack_sha1_rest(&stream, hdr, *size, sha1);
-}
-
 unsigned long get_size_from_delta(struct packed_git *p,
                                  struct pack_window **w_curs,
                                  off_t curpos)
@@ -2326,8 +2313,10 @@ static void *cache_or_unpack_entry(struct packed_git *p, off_t base_offset,
        if (!ent)
                return unpack_entry(p, base_offset, type, base_size);
 
-       *type = ent->type;
-       *base_size = ent->size;
+       if (type)
+               *type = ent->type;
+       if (base_size)
+               *base_size = ent->size;
        return xmemdupz(ent->data, ent->size);
 }
 
@@ -2388,9 +2377,16 @@ int packed_object_info(struct packed_git *p, off_t obj_offset,
         * We always get the representation type, but only convert it to
         * a "real" type later if the caller is interested.
         */
-       type = unpack_object_header(p, &w_curs, &curpos, &size);
+       if (oi->contentp) {
+               *oi->contentp = cache_or_unpack_entry(p, obj_offset, oi->sizep,
+                                                     &type);
+               if (!*oi->contentp)
+                       type = OBJ_BAD;
+       } else {
+               type = unpack_object_header(p, &w_curs, &curpos, &size);
+       }
 
-       if (oi->sizep) {
+       if (!oi->contentp && oi->sizep) {
                if (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) {
                        off_t tmp_pos = curpos;
                        off_t base_offset = get_delta_base(p, &w_curs, &tmp_pos,
@@ -2679,8 +2675,10 @@ void *unpack_entry(struct packed_git *p, off_t obj_offset,
                free(external_base);
        }
 
-       *final_type = type;
-       *final_size = size;
+       if (final_type)
+               *final_type = type;
+       if (final_size)
+               *final_size = size;
 
        unuse_pack(&w_curs);
 
@@ -2914,6 +2912,7 @@ static int sha1_loose_object_info(const unsigned char *sha1,
        git_zstream stream;
        char hdr[32];
        struct strbuf hdrbuf = STRBUF_INIT;
+       unsigned long size_scratch;
 
        if (oi->delta_base_sha1)
                hashclr(oi->delta_base_sha1);
@@ -2926,7 +2925,7 @@ static int sha1_loose_object_info(const unsigned char *sha1,
         * return value implicitly indicates whether the
         * object even exists.
         */
-       if (!oi->typep && !oi->typename && !oi->sizep) {
+       if (!oi->typep && !oi->typename && !oi->sizep && !oi->contentp) {
                const char *path;
                struct stat st;
                if (stat_sha1_file(sha1, &st, &path) < 0)
@@ -2939,6 +2938,10 @@ static int sha1_loose_object_info(const unsigned char *sha1,
        map = map_sha1_file(sha1, &mapsize);
        if (!map)
                return -1;
+
+       if (!oi->sizep)
+               oi->sizep = &size_scratch;
+
        if (oi->disk_sizep)
                *oi->disk_sizep = mapsize;
        if ((flags & OBJECT_INFO_ALLOW_UNKNOWN_TYPE)) {
@@ -2956,37 +2959,52 @@ static int sha1_loose_object_info(const unsigned char *sha1,
                                       sha1_to_hex(sha1));
        } else if ((status = parse_sha1_header_extended(hdr, oi, flags)) < 0)
                status = error("unable to parse %s header", sha1_to_hex(sha1));
-       git_inflate_end(&stream);
+
+       if (status >= 0 && oi->contentp)
+               *oi->contentp = unpack_sha1_rest(&stream, hdr,
+                                                *oi->sizep, sha1);
+       else
+               git_inflate_end(&stream);
+
        munmap(map, mapsize);
        if (status && oi->typep)
                *oi->typep = status;
+       if (oi->sizep == &size_scratch)
+               oi->sizep = NULL;
        strbuf_release(&hdrbuf);
        return (status < 0) ? status : 0;
 }
 
 int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi, unsigned flags)
 {
-       struct cached_object *co;
+       static struct object_info blank_oi = OBJECT_INFO_INIT;
        struct pack_entry e;
        int rtype;
        const unsigned char *real = (flags & OBJECT_INFO_LOOKUP_REPLACE) ?
                                    lookup_replace_object(sha1) :
                                    sha1;
 
-       co = find_cached_object(real);
-       if (co) {
-               if (oi->typep)
-                       *(oi->typep) = co->type;
-               if (oi->sizep)
-                       *(oi->sizep) = co->size;
-               if (oi->disk_sizep)
-                       *(oi->disk_sizep) = 0;
-               if (oi->delta_base_sha1)
-                       hashclr(oi->delta_base_sha1);
-               if (oi->typename)
-                       strbuf_addstr(oi->typename, typename(co->type));
-               oi->whence = OI_CACHED;
-               return 0;
+       if (!oi)
+               oi = &blank_oi;
+
+       if (!(flags & OBJECT_INFO_SKIP_CACHED)) {
+               struct cached_object *co = find_cached_object(real);
+               if (co) {
+                       if (oi->typep)
+                               *(oi->typep) = co->type;
+                       if (oi->sizep)
+                               *(oi->sizep) = co->size;
+                       if (oi->disk_sizep)
+                               *(oi->disk_sizep) = 0;
+                       if (oi->delta_base_sha1)
+                               hashclr(oi->delta_base_sha1);
+                       if (oi->typename)
+                               strbuf_addstr(oi->typename, typename(co->type));
+                       if (oi->contentp)
+                               *oi->contentp = xmemdupz(co->buf, co->size);
+                       oi->whence = OI_CACHED;
+                       return 0;
+               }
        }
 
        if (!find_pack_entry(real, &e)) {
@@ -2997,11 +3015,22 @@ int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi,
                }
 
                /* Not a loose object; someone else may have just packed it. */
-               reprepare_packed_git();
-               if (!find_pack_entry(real, &e))
+               if (flags & OBJECT_INFO_QUICK) {
                        return -1;
+               } else {
+                       reprepare_packed_git();
+                       if (!find_pack_entry(real, &e))
+                               return -1;
+               }
        }
 
+       if (oi == &blank_oi)
+               /*
+                * We know that the caller doesn't actually need the
+                * information below, so return early.
+                */
+               return 0;
+
        rtype = packed_object_info(e.p, e.offset, oi);
        if (rtype < 0) {
                mark_bad_packed_object(e.p, real);
@@ -3078,28 +3107,15 @@ int pretend_sha1_file(void *buf, unsigned long len, enum object_type type,
 static void *read_object(const unsigned char *sha1, enum object_type *type,
                         unsigned long *size)
 {
-       unsigned long mapsize;
-       void *map, *buf;
-       struct cached_object *co;
-
-       co = find_cached_object(sha1);
-       if (co) {
-               *type = co->type;
-               *size = co->size;
-               return xmemdupz(co->buf, co->size);
-       }
+       struct object_info oi = OBJECT_INFO_INIT;
+       void *content;
+       oi.typep = type;
+       oi.sizep = size;
+       oi.contentp = &content;
 
-       buf = read_packed_sha1(sha1, type, size);
-       if (buf)
-               return buf;
-       map = map_sha1_file(sha1, &mapsize);
-       if (map) {
-               buf = unpack_sha1_file(map, mapsize, type, size, sha1);
-               munmap(map, mapsize);
-               return buf;
-       }
-       reprepare_packed_git();
-       return read_packed_sha1(sha1, type, size);
+       if (sha1_object_info_extended(sha1, &oi, 0) < 0)
+               return NULL;
+       return content;
 }
 
 /*