clean up and optimize nth_packed_object_sha1() usage
authorNicolas Pitre <nico@cam.org>
Wed, 16 Jul 2008 06:31:36 +0000 (02:31 -0400)
committerJunio C Hamano <gitster@pobox.com>
Wed, 16 Jul 2008 16:33:28 +0000 (09:33 -0700)
Let's avoid the open coded pack index reference in pack-object and use
nth_packed_object_sha1() instead.  This will help encapsulating index
format differences in one place.

And while at it there is no reason to copy SHA1's over and over while a
direct pointer to it in the index will do just fine.

(based on commit d72308e01c5977177cda0aed06cfeee9192e1247)

Signed-off-by: Nicolas Pitre <nico@cam.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin-pack-objects.c
cache.h
fsck-objects.c
pack-check.c
sha1_file.c
sha1_name.c

index ae051f90a31b6ca3c58cb6fb25d8562d8698a3b1..4c345d5d6c7dc5acf6c323f40e5b109ae7e1c24b 100644 (file)
@@ -227,7 +227,7 @@ static const unsigned char *find_packed_object_name(struct packed_git *p,
                                                    unsigned long ofs)
 {
        struct revindex_entry *entry = find_packed_object(p, ofs);
-       return ((unsigned char *)p->index_data) + 4 * 256 + 24 * entry->nr + 4;
+       return nth_packed_object_sha1(p, entry->nr);
 }
 
 static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)
diff --git a/cache.h b/cache.h
index cc8e84e7f5dfe2d74ba00ba86dd182d48211ce60..191c738ed96abd747185e1b40f6d34f566ac7b48 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -388,7 +388,7 @@ extern int use_packed_git(struct packed_git *);
 extern void unuse_packed_git(struct packed_git *);
 extern struct packed_git *add_packed_git(char *, int, int);
 extern int num_packed_objects(const struct packed_git *p);
-extern int nth_packed_object_sha1(const struct packed_git *, int, unsigned char*);
+extern const unsigned char *nth_packed_object_sha1(const struct packed_git *, unsigned int);
 extern unsigned long find_pack_entry_one(const unsigned char *, struct packed_git *);
 extern void *unpack_entry_gently(struct packed_git *, unsigned long, char *, unsigned long *);
 extern unsigned long unpack_object_header_gently(const unsigned char *buf, unsigned long len, enum object_type *type, unsigned long *sizep);
index 46b628cb94375e3f645f868efb04547ffc20e6e7..f6015a80f90d96f1897597aa2b86465427b2272c 100644 (file)
@@ -289,7 +289,7 @@ static int fsck_tag(struct tag *tag)
        return 0;
 }
 
-static int fsck_sha1(unsigned char *sha1)
+static int fsck_sha1(const unsigned char *sha1)
 {
        struct object *obj = parse_object(sha1);
        if (!obj)
@@ -551,11 +551,8 @@ int main(int argc, char **argv)
 
                for (p = packed_git; p; p = p->next) {
                        int num = num_packed_objects(p);
-                       for (i = 0; i < num; i++) {
-                               unsigned char sha1[20];
-                               nth_packed_object_sha1(p, i, sha1);
-                               fsck_sha1(sha1);
-                       }
+                       for (i = 0; i < num; i++)
+                               fsck_sha1(nth_packed_object_sha1(p, i));
                }
        }
 
index 7db44e91c520de7a940cb1e6aec89a2b80cc5a08..11f6ed2117fbe5bded2408d82742fa4b724b54de 100644 (file)
@@ -51,12 +51,13 @@ static int verify_packfile(struct packed_git *p)
         * we do not do scan-streaming check on the pack file.
         */
        for (i = err = 0; i < nr_objects; i++) {
-               unsigned char sha1[20];
+               const unsigned char *sha1;
                void *data;
                char type[20];
                unsigned long size, offset;
 
-               if (nth_packed_object_sha1(p, i, sha1))
+               sha1 = nth_packed_object_sha1(p, i);
+               if (!sha1)
                        die("internal error pack-check nth-packed-object");
                offset = find_pack_entry_one(sha1, p);
                if (!offset)
@@ -93,14 +94,16 @@ static void show_pack_info(struct packed_git *p)
        memset(chain_histogram, 0, sizeof(chain_histogram));
 
        for (i = 0; i < nr_objects; i++) {
-               unsigned char sha1[20], base_sha1[20];
+               const unsigned char *sha1;
+               unsigned char base_sha1[20];
                char type[20];
                unsigned long size;
                unsigned long store_size;
                unsigned long offset;
                unsigned int delta_chain_length;
 
-               if (nth_packed_object_sha1(p, i, sha1))
+               sha1 = nth_packed_object_sha1(p, i);
+               if (!sha1)
                        die("internal error pack-check nth-packed-object");
                offset = find_pack_entry_one(sha1, p);
                if (!offset)
index df31462a1fcf7b6ac88078af1196df904f442fed..b4c5209d6511f201fa133ec70cb6349379491373 100644 (file)
@@ -1177,15 +1177,14 @@ int num_packed_objects(const struct packed_git *p)
        return (p->index_size - 20 - 20 - 4*256) / 24;
 }
 
-int nth_packed_object_sha1(const struct packed_git *p, int n,
-                          unsigned char* sha1)
+const unsigned char *nth_packed_object_sha1(const struct packed_git *p,
+                                           unsigned int n)
 {
        const unsigned char *index = p->index_data;
        index += 4 * 256;
-       if (n < 0 || num_packed_objects(p) <= n)
-               return -1;
-       hashcpy(sha1, index + 24 * n + 4);
-       return 0;
+       if (num_packed_objects(p) <= n)
+               return NULL;
+       return index + 24 * n + 4;
 }
 
 unsigned long find_pack_entry_one(const unsigned char *sha1,
index 6d7cd78381414aa2fef31d31d46fbb24b0aaab1d..d083096e2043301f189c456336a85bb4064e232e 100644 (file)
@@ -71,7 +71,7 @@ static int match_sha(unsigned len, const unsigned char *a, const unsigned char *
 static int find_short_packed_object(int len, const unsigned char *match, unsigned char *sha1)
 {
        struct packed_git *p;
-       unsigned char found_sha1[20];
+       const unsigned char *found_sha1 = NULL;
        int found = 0;
 
        prepare_packed_git();
@@ -80,10 +80,10 @@ static int find_short_packed_object(int len, const unsigned char *match, unsigne
                unsigned first = 0, last = num;
                while (first < last) {
                        unsigned mid = (first + last) / 2;
-                       unsigned char now[20];
+                       const unsigned char *now;
                        int cmp;
 
-                       nth_packed_object_sha1(p, mid, now);
+                       now = nth_packed_object_sha1(p, mid);
                        cmp = hashcmp(match, now);
                        if (!cmp) {
                                first = mid;
@@ -96,14 +96,14 @@ static int find_short_packed_object(int len, const unsigned char *match, unsigne
                        last = mid;
                }
                if (first < num) {
-                       unsigned char now[20], next[20];
-                       nth_packed_object_sha1(p, first, now);
+                       const unsigned char *now, *next;
+                      now = nth_packed_object_sha1(p, first);
                        if (match_sha(len, match, now)) {
-                               if (nth_packed_object_sha1(p, first+1, next) ||
-                                   !match_sha(len, match, next)) {
+                               next = nth_packed_object_sha1(p, first+1);
+                              if (!next|| !match_sha(len, match, next)) {
                                        /* unique within this pack */
                                        if (!found) {
-                                               hashcpy(found_sha1, now);
+                                               found_sha1 = now;
                                                found++;
                                        }
                                        else if (hashcmp(found_sha1, now)) {