Code

clean up and optimize nth_packed_object_sha1() usage
authorNicolas Pitre <nico@cam.org>
Wed, 4 Apr 2007 20:49:04 +0000 (16:49 -0400)
committerJunio C Hamano <junkio@cox.net>
Thu, 5 Apr 2007 21:59:47 +0000 (14:59 -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.

Signed-off-by: Nicolas Pitre <nico@cam.org>
Acked-by: Shawn O. Pearce <spearce@spearce.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
builtin-fsck.c
builtin-pack-objects.c
cache.h
pack-check.c
sha1_file.c
sha1_name.c

index 21f1f9e91d5a1ba8e8c3fd968b56d5c860b2aeac..4e5aa33dfb9921e4be98ea893cea34fc66e35735 100644 (file)
@@ -348,7 +348,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) {
@@ -648,11 +648,8 @@ int cmd_fsck(int argc, char **argv, const char *prefix)
 
                for (p = packed_git; p; p = p->next) {
                        uint32_t i, 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 b5f9648e809a3ef7f381239470c031208ade4a34..45ac3e482acc1c0f8f2bad043f1ba50019dec505 100644 (file)
@@ -222,7 +222,7 @@ static const unsigned char *find_packed_object_name(struct packed_git *p,
                                                    off_t 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 384b260227dfb2fa30ecc175681b9b8d73a3577a..1f128d55d206e59ed42e04244a3a77728bae8593 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -428,7 +428,7 @@ extern unsigned char* use_pack(struct packed_git *, struct pack_window **, off_t
 extern void unuse_pack(struct pack_window **);
 extern struct packed_git *add_packed_git(const char *, int, int);
 extern uint32_t num_packed_objects(const struct packed_git *p);
-extern int nth_packed_object_sha1(const struct packed_git *, uint32_t, unsigned char*);
+extern const unsigned char *nth_packed_object_sha1(const struct packed_git *, uint32_t);
 extern off_t find_pack_entry_one(const unsigned char *, struct packed_git *);
 extern void *unpack_entry(struct packed_git *, off_t, enum object_type *, unsigned long *);
 extern unsigned long unpack_object_header_gently(const unsigned char *buf, unsigned long len, enum object_type *type, unsigned long *sizep);
index d9883225eabf10ad9a3a169e7049c9ee25e5d9cd..f58083d11e0cfb974861d340bdea4ae18d2469e8 100644 (file)
@@ -42,13 +42,14 @@ static int verify_packfile(struct packed_git *p,
         */
        nr_objects = num_packed_objects(p);
        for (i = 0, err = 0; i < nr_objects; i++) {
-               unsigned char sha1[20];
+               const unsigned char *sha1;
                void *data;
                enum object_type type;
                unsigned long size;
                off_t 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)
@@ -82,14 +83,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];
                const char *type;
                unsigned long size;
                unsigned long store_size;
                off_t 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 9c260384201857eb32d07c87e1178fd3947968ee..4304fe9bbc2b8e796e944fa7ddb2ea2791000adf 100644 (file)
@@ -1532,15 +1532,14 @@ uint32_t num_packed_objects(const struct packed_git *p)
        return (uint32_t)((p->index_size - 20 - 20 - 4*256) / 24);
 }
 
-int nth_packed_object_sha1(const struct packed_git *p, uint32_t n,
-                          unsigned char* sha1)
+const unsigned char *nth_packed_object_sha1(const struct packed_git *p,
+                                           uint32_t n)
 {
        const unsigned char *index = p->index_data;
        index += 4 * 256;
        if (num_packed_objects(p) <= n)
-               return -1;
-       hashcpy(sha1, index + 24 * n + 4);
-       return 0;
+               return NULL;
+       return index + 24 * n + 4;
 }
 
 off_t find_pack_entry_one(const unsigned char *sha1,
index bede0e5b0659db31a1dfdaab3d17627199150ea7..267ea3f3edc63600bc1591d2115ee85222f3e8c5 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
                uint32_t first = 0, last = num;
                while (first < last) {
                        uint32_t 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)) {