@matheusbsbs / matheus-bagliano / commits / d72308e01c
Fork 0

clean up and optimize nth_packed_object_sha1() usage

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>

Nicolas Pitre committed Apr 4, 2007 at 16:49 UTC d72308e01c5977177cda0aed06cfeee9192e1247
6 files changed +24 -25
builtin-fsck.c
+3 -6
@@ -348,7 +348,7 @@ static int fsck_tag(struct tag *tag)
348 return 0;
349 }
350
351 -static int fsck_sha1(unsigned char *sha1)
351 +static int fsck_sha1(const unsigned char *sha1)
352 {
353 struct object *obj = parse_object(sha1);
354 if (!obj) {
@@ -648,11 +648,8 @@ int cmd_fsck(int argc, char **argv, const char *prefix)
648
649 for (p = packed_git; p; p = p->next) {
650 uint32_t i, num = num_packed_objects(p);
651 - for (i = 0; i < num; i++) {
652 - unsigned char sha1[20];
653 - nth_packed_object_sha1(p, i, sha1);
654 - fsck_sha1(sha1);
655 - }
651 + for (i = 0; i < num; i++)
652 + fsck_sha1(nth_packed_object_sha1(p, i));
653 }
654 }
655
builtin-pack-objects.c
+1 -1
@@ -222,7 +222,7 @@ static const unsigned char *find_packed_object_name(struct packed_git *p,
222 off_t ofs)
223 {
224 struct revindex_entry *entry = find_packed_object(p, ofs);
225 - return ((unsigned char *)p->index_data) + 4 * 256 + 24 * entry->nr + 4;
225 + return nth_packed_object_sha1(p, entry->nr);
226 }
227
228 static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)
cache.h
+1 -1
@@ -428,7 +428,7 @@ extern unsigned char* use_pack(struct packed_git *, struct pack_window **, off_t
428 extern void unuse_pack(struct pack_window **);
429 extern struct packed_git *add_packed_git(const char *, int, int);
430 extern uint32_t num_packed_objects(const struct packed_git *p);
431 -extern int nth_packed_object_sha1(const struct packed_git *, uint32_t, unsigned char*);
431 +extern const unsigned char *nth_packed_object_sha1(const struct packed_git *, uint32_t);
432 extern off_t find_pack_entry_one(const unsigned char *, struct packed_git *);
433 extern void *unpack_entry(struct packed_git *, off_t, enum object_type *, unsigned long *);
434 extern unsigned long unpack_object_header_gently(const unsigned char *buf, unsigned long len, enum object_type *type, unsigned long *sizep);
pack-check.c
+7 -4
@@ -42,13 +42,14 @@ static int verify_packfile(struct packed_git *p,
42 */
43 nr_objects = num_packed_objects(p);
44 for (i = 0, err = 0; i < nr_objects; i++) {
45 - unsigned char sha1[20];
45 + const unsigned char *sha1;
46 void *data;
47 enum object_type type;
48 unsigned long size;
49 off_t offset;
50
51 - if (nth_packed_object_sha1(p, i, sha1))
51 + sha1 = nth_packed_object_sha1(p, i);
52 + if (!sha1)
53 die("internal error pack-check nth-packed-object");
54 offset = find_pack_entry_one(sha1, p);
55 if (!offset)
@@ -82,14 +83,16 @@ static void show_pack_info(struct packed_git *p)
83 memset(chain_histogram, 0, sizeof(chain_histogram));
84
85 for (i = 0; i < nr_objects; i++) {
85 - unsigned char sha1[20], base_sha1[20];
86 + const unsigned char *sha1;
87 + unsigned char base_sha1[20];
88 const char *type;
89 unsigned long size;
90 unsigned long store_size;
91 off_t offset;
92 unsigned int delta_chain_length;
93
92 - if (nth_packed_object_sha1(p, i, sha1))
94 + sha1 = nth_packed_object_sha1(p, i);
95 + if (!sha1)
96 die("internal error pack-check nth-packed-object");
97 offset = find_pack_entry_one(sha1, p);
98 if (!offset)
sha1_file.c
+4 -5
@@ -1532,15 +1532,14 @@ uint32_t num_packed_objects(const struct packed_git *p)
1532 return (uint32_t)((p->index_size - 20 - 20 - 4*256) / 24);
1533 }
1534
1535 -int nth_packed_object_sha1(const struct packed_git *p, uint32_t n,
1536 - unsigned char* sha1)
1535 +const unsigned char *nth_packed_object_sha1(const struct packed_git *p,
1536 + uint32_t n)
1537 {
1538 const unsigned char *index = p->index_data;
1539 index += 4 * 256;
1540 if (num_packed_objects(p) <= n)
1541 - return -1;
1542 - hashcpy(sha1, index + 24 * n + 4);
1543 - return 0;
1541 + return NULL;
1542 + return index + 24 * n + 4;
1543 }
1544
1545 off_t find_pack_entry_one(const unsigned char *sha1,
sha1_name.c
+8 -8
@@ -71,7 +71,7 @@ static int match_sha(unsigned len, const unsigned char *a, const unsigned char *
71 static int find_short_packed_object(int len, const unsigned char *match, unsigned char *sha1)
72 {
73 struct packed_git *p;
74 - unsigned char found_sha1[20];
74 + const unsigned char *found_sha1 = NULL;
75 int found = 0;
76
77 prepare_packed_git();
@@ -80,10 +80,10 @@ static int find_short_packed_object(int len, const unsigned char *match, unsigne
80 uint32_t first = 0, last = num;
81 while (first < last) {
82 uint32_t mid = (first + last) / 2;
83 - unsigned char now[20];
83 + const unsigned char *now;
84 int cmp;
85
86 - nth_packed_object_sha1(p, mid, now);
86 + now = nth_packed_object_sha1(p, mid);
87 cmp = hashcmp(match, now);
88 if (!cmp) {
89 first = mid;
@@ -96,14 +96,14 @@ static int find_short_packed_object(int len, const unsigned char *match, unsigne
96 last = mid;
97 }
98 if (first < num) {
99 - unsigned char now[20], next[20];
100 - nth_packed_object_sha1(p, first, now);
99 + const unsigned char *now, *next;
100 + now = nth_packed_object_sha1(p, first);
101 if (match_sha(len, match, now)) {
102 - if (nth_packed_object_sha1(p, first+1, next) ||
103 - !match_sha(len, match, next)) {
102 + next = nth_packed_object_sha1(p, first+1);
103 + if (!next|| !match_sha(len, match, next)) {
104 /* unique within this pack */
105 if (!found) {
106 - hashcpy(found_sha1, now);
106 + found_sha1 = now;
107 found++;
108 }
109 else if (hashcmp(found_sha1, now)) {