Looking up the offset in the packfile for a given SHA1 involves the
following:

        - Finding the position in the index for the given SHA1
        - Accessing the offset cache in the index for the found position

There are cases however where we'd like to find the position of a SHA1
in the index without looking up the packfile offset (e.g. when accessing
information that has been indexed based on index offsets).

This refactoring implements `find_pack_object_pos`, returning the
position in the index, and re-implements `find_pack_entry_one`(returning
the actual offset in the packfile) to use the new function.
---
 cache.h     |    1 +
 sha1_file.c |   27 +++++++++++++++++----------
 2 files changed, 18 insertions(+), 10 deletions(-)

diff --git a/cache.h b/cache.h
index ec8240f..a29645e 100644
--- a/cache.h
+++ b/cache.h
@@ -1101,6 +1101,7 @@ extern void clear_delta_base_cache(void);
 extern struct packed_git *add_packed_git(const char *, int, int);
 extern const unsigned char *nth_packed_object_sha1(struct packed_git *, 
uint32_t);
 extern off_t nth_packed_object_offset(const struct packed_git *, uint32_t);
+extern int find_pack_entry_pos(const unsigned char *sha1, struct packed_git 
*p);
 extern off_t find_pack_entry_one(const unsigned char *, struct packed_git *);
 extern int is_pack_valid(struct packed_git *);
 extern void *unpack_entry(struct packed_git *, off_t, enum object_type *, 
unsigned long *);
diff --git a/sha1_file.c b/sha1_file.c
index 0af19c0..371e295 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -2205,8 +2205,7 @@ off_t nth_packed_object_offset(const struct packed_git 
*p, uint32_t n)
        }
 }
 
-off_t find_pack_entry_one(const unsigned char *sha1,
-                                 struct packed_git *p)
+int find_pack_entry_pos(const unsigned char *sha1, struct packed_git *p)
 {
        const uint32_t *level1_ofs = p->index_data;
        const unsigned char *index = p->index_data;
@@ -2219,7 +2218,7 @@ off_t find_pack_entry_one(const unsigned char *sha1,
 
        if (!index) {
                if (open_pack_index(p))
-                       return 0;
+                       return -1;
                level1_ofs = p->index_data;
                index = p->index_data;
        }
@@ -2243,12 +2242,9 @@ off_t find_pack_entry_one(const unsigned char *sha1,
 
        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);
+               return sha1_entry_pos(index, stride, 0, lo, hi, p->num_objects, 
sha1);
        }
 
        do {
@@ -2259,13 +2255,24 @@ off_t find_pack_entry_one(const unsigned char *sha1,
                        printf("lo %u hi %u rg %u mi %u\n",
                               lo, hi, hi - lo, mi);
                if (!cmp)
-                       return nth_packed_object_offset(p, mi);
+                       return mi;
                if (cmp > 0)
                        hi = mi;
                else
                        lo = mi+1;
        } while (lo < hi);
-       return 0;
+
+       return -1;
+}
+
+off_t find_pack_entry_one(const unsigned char *sha1, struct packed_git *p)
+{
+       int pos;
+
+       if ((pos = find_pack_entry_pos(sha1, p)) < 0)
+               return 0;
+
+       return nth_packed_object_offset(p, (uint32_t)pos);
 }
 
 int is_pack_valid(struct packed_git *p)
-- 
1.7.9.5

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to