Merge branch 'po/object-id'
[gitweb.git] / sha1_file.c
index b60ae15f7068c157df6407933ea7ee94a53f0640..73a4a0c98e8770e4f471726e5c023ed86fe28dc2 100644 (file)
@@ -347,6 +347,7 @@ static int alt_odb_usable(struct strbuf *path, const char *normalized_objdir)
  * SHA1, an extra slash for the first level indirection, and the
  * terminating NUL.
  */
+static void read_info_alternates(const char * relative_base, int depth);
 static int link_alt_odb_entry(const char *entry, const char *relative_base,
        int depth, const char *normalized_objdir)
 {
@@ -448,7 +449,7 @@ static void link_alt_odb_entries(const char *alt, int len, int sep,
        strbuf_release(&objdirbuf);
 }
 
-void read_info_alternates(const char * relative_base, int depth)
+static void read_info_alternates(const char * relative_base, int depth)
 {
        char *map;
        size_t mapsz;
@@ -2444,6 +2445,9 @@ int packed_object_info(struct packed_git *p, off_t obj_offset,
                        hashclr(oi->delta_base_sha1);
        }
 
+       oi->whence = in_delta_base_cache(p, obj_offset) ? OI_DBCACHED :
+                                                         OI_PACKED;
+
 out:
        unuse_pack(&w_curs);
        return type;
@@ -2542,8 +2546,8 @@ void *unpack_entry(struct packed_git *p, off_t obj_offset,
                                error("bad packed object CRC for %s",
                                      sha1_to_hex(sha1));
                                mark_bad_packed_object(p, sha1);
-                               unuse_pack(&w_curs);
-                               return NULL;
+                               data = NULL;
+                               goto out;
                        }
                }
 
@@ -2681,6 +2685,7 @@ void *unpack_entry(struct packed_git *p, off_t obj_offset,
        if (final_size)
                *final_size = size;
 
+out:
        unuse_pack(&w_curs);
 
        if (delta_stack != small_delta_stack)
@@ -2759,7 +2764,6 @@ off_t find_pack_entry_one(const unsigned char *sha1,
        const uint32_t *level1_ofs = p->index_data;
        const unsigned char *index = p->index_data;
        unsigned hi, lo, stride;
-       static int use_lookup = -1;
        static int debug_lookup = -1;
 
        if (debug_lookup < 0)
@@ -2789,17 +2793,7 @@ off_t find_pack_entry_one(const unsigned char *sha1,
                printf("%02x%02x%02x... lo %u hi %u nr %"PRIu32"\n",
                       sha1[0], sha1[1], sha1[2], lo, hi, p->num_objects);
 
-       if (use_lookup < 0)
-               use_lookup = !!getenv("GIT_USE_LOOKUP");
-       if (use_lookup) {
-               int pos = sha1_entry_pos(index, stride, 0,
-                                        lo, hi, p->num_objects, sha1);
-               if (pos < 0)
-                       return 0;
-               return nth_packed_object_offset(p, pos);
-       }
-
-       do {
+       while (lo < hi) {
                unsigned mi = (lo + hi) / 2;
                int cmp = hashcmp(index + mi * stride, sha1);
 
@@ -2812,7 +2806,7 @@ off_t find_pack_entry_one(const unsigned char *sha1,
                        hi = mi;
                else
                        lo = mi+1;
-       } while (lo < hi);
+       }
        return 0;
 }
 
@@ -2973,6 +2967,7 @@ static int sha1_loose_object_info(const unsigned char *sha1,
        if (oi->sizep == &size_scratch)
                oi->sizep = NULL;
        strbuf_release(&hdrbuf);
+       oi->whence = OI_LOOSE;
        return (status < 0) ? status : 0;
 }
 
@@ -3010,10 +3005,8 @@ int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi,
 
        if (!find_pack_entry(real, &e)) {
                /* Most likely it's a loose object. */
-               if (!sha1_loose_object_info(real, oi, flags)) {
-                       oi->whence = OI_LOOSE;
+               if (!sha1_loose_object_info(real, oi, flags))
                        return 0;
-               }
 
                /* Not a loose object; someone else may have just packed it. */
                if (flags & OBJECT_INFO_QUICK) {
@@ -3036,10 +3029,7 @@ int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi,
        if (rtype < 0) {
                mark_bad_packed_object(e.p, real);
                return sha1_object_info_extended(real, oi, 0);
-       } else if (in_delta_base_cache(e.p, e.offset)) {
-               oi->whence = OI_DBCACHED;
-       } else {
-               oi->whence = OI_PACKED;
+       } else if (oi->whence == OI_PACKED) {
                oi->u.packed.offset = e.offset;
                oi->u.packed.pack = e.p;
                oi->u.packed.is_delta = (rtype == OBJ_REF_DELTA ||
@@ -3063,30 +3053,6 @@ int sha1_object_info(const unsigned char *sha1, unsigned long *sizep)
        return type;
 }
 
-static void *read_packed_sha1(const unsigned char *sha1,
-                             enum object_type *type, unsigned long *size)
-{
-       struct pack_entry e;
-       void *data;
-
-       if (!find_pack_entry(sha1, &e))
-               return NULL;
-       data = cache_or_unpack_entry(e.p, e.offset, size, type);
-       if (!data) {
-               /*
-                * We're probably in deep shit, but let's try to fetch
-                * the required object anyway from another pack or loose.
-                * This should happen only in the presence of a corrupted
-                * pack, and is better than failing outright.
-                */
-               error("failed to read object %s at offset %"PRIuMAX" from %s",
-                     sha1_to_hex(sha1), (uintmax_t)e.offset, e.p->pack_name);
-               mark_bad_packed_object(e.p, sha1);
-               data = read_object(sha1, type, size);
-       }
-       return data;
-}
-
 int pretend_sha1_file(void *buf, unsigned long len, enum object_type type,
                      unsigned char *sha1)
 {
@@ -3437,7 +3403,7 @@ int write_sha1_file(const void *buf, unsigned long len, const char *type, unsign
 }
 
 int hash_sha1_file_literally(const void *buf, unsigned long len, const char *type,
-                            unsigned char *sha1, unsigned flags)
+                            struct object_id *oid, unsigned flags)
 {
        char *header;
        int hdrlen, status = 0;
@@ -3445,13 +3411,13 @@ int hash_sha1_file_literally(const void *buf, unsigned long len, const char *typ
        /* type string, SP, %lu of the length plus NUL must fit this */
        hdrlen = strlen(type) + 32;
        header = xmalloc(hdrlen);
-       write_sha1_file_prepare(buf, len, type, sha1, header, &hdrlen);
+       write_sha1_file_prepare(buf, len, type, oid->hash, header, &hdrlen);
 
        if (!(flags & HASH_WRITE_OBJECT))
                goto cleanup;
-       if (freshen_packed_object(sha1) || freshen_loose_object(sha1))
+       if (freshen_packed_object(oid->hash) || freshen_loose_object(oid->hash))
                goto cleanup;
-       status = write_loose_object(sha1, header, hdrlen, buf, len, 0);
+       status = write_loose_object(oid->hash, header, hdrlen, buf, len, 0);
 
 cleanup:
        free(header);
@@ -3469,7 +3435,7 @@ int force_object_loose(const unsigned char *sha1, time_t mtime)
 
        if (has_loose_object(sha1))
                return 0;
-       buf = read_packed_sha1(sha1, &type, &len);
+       buf = read_object(sha1, &type, &len);
        if (!buf)
                return error("cannot read sha1_file for %s", sha1_to_hex(sha1));
        hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", typename(type), len) + 1;
@@ -3655,14 +3621,14 @@ static int index_core(unsigned char *sha1, int fd, size_t size,
  * binary blobs, they generally do not want to get any conversion, and
  * callers should avoid this code path when filters are requested.
  */
-static int index_stream(unsigned char *sha1, int fd, size_t size,
+static int index_stream(struct object_id *oid, int fd, size_t size,
                        enum object_type type, const char *path,
                        unsigned flags)
 {
-       return index_bulk_checkin(sha1, fd, size, type, path, flags);
+       return index_bulk_checkin(oid->hash, fd, size, type, path, flags);
 }
 
-int index_fd(unsigned char *sha1, int fd, struct stat *st,
+int index_fd(struct object_id *oid, int fd, struct stat *st,
             enum object_type type, const char *path, unsigned flags)
 {
        int ret;
@@ -3672,21 +3638,21 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st,
         * die() for large files.
         */
        if (type == OBJ_BLOB && path && would_convert_to_git_filter_fd(path))
-               ret = index_stream_convert_blob(sha1, fd, path, flags);
+               ret = index_stream_convert_blob(oid->hash, fd, path, flags);
        else if (!S_ISREG(st->st_mode))
-               ret = index_pipe(sha1, fd, type, path, flags);
+               ret = index_pipe(oid->hash, fd, type, path, flags);
        else if (st->st_size <= big_file_threshold || type != OBJ_BLOB ||
                 (path && would_convert_to_git(&the_index, path)))
-               ret = index_core(sha1, fd, xsize_t(st->st_size), type, path,
+               ret = index_core(oid->hash, fd, xsize_t(st->st_size), type, path,
                                 flags);
        else
-               ret = index_stream(sha1, fd, xsize_t(st->st_size), type, path,
+               ret = index_stream(oid, fd, xsize_t(st->st_size), type, path,
                                   flags);
        close(fd);
        return ret;
 }
 
-int index_path(unsigned char *sha1, const char *path, struct stat *st, unsigned flags)
+int index_path(struct object_id *oid, const char *path, struct stat *st, unsigned flags)
 {
        int fd;
        struct strbuf sb = STRBUF_INIT;
@@ -3696,7 +3662,7 @@ int index_path(unsigned char *sha1, const char *path, struct stat *st, unsigned
                fd = open(path, O_RDONLY);
                if (fd < 0)
                        return error_errno("open(\"%s\")", path);
-               if (index_fd(sha1, fd, st, OBJ_BLOB, path, flags) < 0)
+               if (index_fd(oid, fd, st, OBJ_BLOB, path, flags) < 0)
                        return error("%s: failed to insert into database",
                                     path);
                break;
@@ -3704,14 +3670,14 @@ int index_path(unsigned char *sha1, const char *path, struct stat *st, unsigned
                if (strbuf_readlink(&sb, path, st->st_size))
                        return error_errno("readlink(\"%s\")", path);
                if (!(flags & HASH_WRITE_OBJECT))
-                       hash_sha1_file(sb.buf, sb.len, blob_type, sha1);
-               else if (write_sha1_file(sb.buf, sb.len, blob_type, sha1))
+                       hash_sha1_file(sb.buf, sb.len, blob_type, oid->hash);
+               else if (write_sha1_file(sb.buf, sb.len, blob_type, oid->hash))
                        return error("%s: failed to insert into database",
                                     path);
                strbuf_release(&sb);
                break;
        case S_IFDIR:
-               return resolve_gitlink_ref(path, "HEAD", sha1);
+               return resolve_gitlink_ref(path, "HEAD", oid->hash);
        default:
                return error("%s: unsupported file type", path);
        }