checkout-index: fix --temp relative path mangling
[gitweb.git] / pack-revindex.c
index b4d2b35bb37120f97ec000524d12a85d91949119..5c8376e9789301b921188e3bf19fba85960a7967 100644 (file)
  * get the object sha1 from the main index.
  */
 
-struct pack_revindex {
-       struct packed_git *p;
-       struct revindex_entry *revindex;
-};
-
 static struct pack_revindex *pack_revindex;
 static int pack_revindex_hashsz;
 
@@ -50,7 +45,7 @@ static void init_pack_revindex(void)
        if (!num)
                return;
        pack_revindex_hashsz = num * 11;
-       pack_revindex = xcalloc(sizeof(*pack_revindex), pack_revindex_hashsz);
+       pack_revindex = xcalloc(pack_revindex_hashsz, sizeof(*pack_revindex));
        for (p = packed_git; p; p = p->next) {
                num = pack_revindex_ix(p);
                num = - 1 - num;
@@ -201,15 +196,14 @@ static void create_pack_revindex(struct pack_revindex *rix)
        sort_revindex(rix->revindex, num_ent, p->pack_size);
 }
 
-struct revindex_entry *find_pack_revindex(struct packed_git *p, off_t ofs)
+struct pack_revindex *revindex_for_pack(struct packed_git *p)
 {
        int num;
-       unsigned lo, hi;
        struct pack_revindex *rix;
-       struct revindex_entry *revindex;
 
        if (!pack_revindex_hashsz)
                init_pack_revindex();
+
        num = pack_revindex_ix(p);
        if (num < 0)
                die("internal error: pack revindex fubar");
@@ -217,30 +211,37 @@ struct revindex_entry *find_pack_revindex(struct packed_git *p, off_t ofs)
        rix = &pack_revindex[num];
        if (!rix->revindex)
                create_pack_revindex(rix);
-       revindex = rix->revindex;
 
-       lo = 0;
-       hi = p->num_objects + 1;
+       return rix;
+}
+
+int find_revindex_position(struct pack_revindex *pridx, off_t ofs)
+{
+       int lo = 0;
+       int hi = pridx->p->num_objects + 1;
+       struct revindex_entry *revindex = pridx->revindex;
+
        do {
                unsigned mi = lo + (hi - lo) / 2;
                if (revindex[mi].offset == ofs) {
-                       return revindex + mi;
+                       return mi;
                } else if (ofs < revindex[mi].offset)
                        hi = mi;
                else
                        lo = mi + 1;
        } while (lo < hi);
+
        error("bad offset for revindex");
-       return NULL;
+       return -1;
 }
 
-void discard_revindex(void)
+struct revindex_entry *find_pack_revindex(struct packed_git *p, off_t ofs)
 {
-       if (pack_revindex_hashsz) {
-               int i;
-               for (i = 0; i < pack_revindex_hashsz; i++)
-                       free(pack_revindex[i].revindex);
-               free(pack_revindex);
-               pack_revindex_hashsz = 0;
-       }
+       struct pack_revindex *pridx = revindex_for_pack(p);
+       int pos = find_revindex_position(pridx, ofs);
+
+       if (pos < 0)
+               return NULL;
+
+       return pridx->revindex + pos;
 }