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>
This commit is contained in:
Nicolas Pitre 2007-04-04 16:49:04 -04:00 committed by Junio C Hamano
parent d5ad36fe35
commit d72308e01c
6 changed files with 24 additions and 25 deletions

View File

@ -348,7 +348,7 @@ static int fsck_tag(struct tag *tag)
return 0; return 0;
} }
static int fsck_sha1(unsigned char *sha1) static int fsck_sha1(const unsigned char *sha1)
{ {
struct object *obj = parse_object(sha1); struct object *obj = parse_object(sha1);
if (!obj) { if (!obj) {
@ -648,11 +648,8 @@ int cmd_fsck(int argc, char **argv, const char *prefix)
for (p = packed_git; p; p = p->next) { for (p = packed_git; p; p = p->next) {
uint32_t i, num = num_packed_objects(p); uint32_t i, num = num_packed_objects(p);
for (i = 0; i < num; i++) { for (i = 0; i < num; i++)
unsigned char sha1[20]; fsck_sha1(nth_packed_object_sha1(p, i));
nth_packed_object_sha1(p, i, sha1);
fsck_sha1(sha1);
}
} }
} }

View File

@ -222,7 +222,7 @@ static const unsigned char *find_packed_object_name(struct packed_git *p,
off_t ofs) off_t ofs)
{ {
struct revindex_entry *entry = find_packed_object(p, 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) static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)

View File

@ -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 void unuse_pack(struct pack_window **);
extern struct packed_git *add_packed_git(const char *, int, int); extern struct packed_git *add_packed_git(const char *, int, int);
extern uint32_t num_packed_objects(const struct packed_git *p); 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 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 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); extern unsigned long unpack_object_header_gently(const unsigned char *buf, unsigned long len, enum object_type *type, unsigned long *sizep);

View File

@ -42,13 +42,14 @@ static int verify_packfile(struct packed_git *p,
*/ */
nr_objects = num_packed_objects(p); nr_objects = num_packed_objects(p);
for (i = 0, err = 0; i < nr_objects; i++) { for (i = 0, err = 0; i < nr_objects; i++) {
unsigned char sha1[20]; const unsigned char *sha1;
void *data; void *data;
enum object_type type; enum object_type type;
unsigned long size; unsigned long size;
off_t offset; 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"); die("internal error pack-check nth-packed-object");
offset = find_pack_entry_one(sha1, p); offset = find_pack_entry_one(sha1, p);
if (!offset) if (!offset)
@ -82,14 +83,16 @@ static void show_pack_info(struct packed_git *p)
memset(chain_histogram, 0, sizeof(chain_histogram)); memset(chain_histogram, 0, sizeof(chain_histogram));
for (i = 0; i < nr_objects; i++) { 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; const char *type;
unsigned long size; unsigned long size;
unsigned long store_size; unsigned long store_size;
off_t offset; off_t offset;
unsigned int delta_chain_length; 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"); die("internal error pack-check nth-packed-object");
offset = find_pack_entry_one(sha1, p); offset = find_pack_entry_one(sha1, p);
if (!offset) if (!offset)

View 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); return (uint32_t)((p->index_size - 20 - 20 - 4*256) / 24);
} }
int nth_packed_object_sha1(const struct packed_git *p, uint32_t n, const unsigned char *nth_packed_object_sha1(const struct packed_git *p,
unsigned char* sha1) uint32_t n)
{ {
const unsigned char *index = p->index_data; const unsigned char *index = p->index_data;
index += 4 * 256; index += 4 * 256;
if (num_packed_objects(p) <= n) if (num_packed_objects(p) <= n)
return -1; return NULL;
hashcpy(sha1, index + 24 * n + 4); return index + 24 * n + 4;
return 0;
} }
off_t find_pack_entry_one(const unsigned char *sha1, off_t find_pack_entry_one(const unsigned char *sha1,

View 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) static int find_short_packed_object(int len, const unsigned char *match, unsigned char *sha1)
{ {
struct packed_git *p; struct packed_git *p;
unsigned char found_sha1[20]; const unsigned char *found_sha1 = NULL;
int found = 0; int found = 0;
prepare_packed_git(); 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; uint32_t first = 0, last = num;
while (first < last) { while (first < last) {
uint32_t mid = (first + last) / 2; uint32_t mid = (first + last) / 2;
unsigned char now[20]; const unsigned char *now;
int cmp; int cmp;
nth_packed_object_sha1(p, mid, now); now = nth_packed_object_sha1(p, mid);
cmp = hashcmp(match, now); cmp = hashcmp(match, now);
if (!cmp) { if (!cmp) {
first = mid; first = mid;
@ -96,14 +96,14 @@ static int find_short_packed_object(int len, const unsigned char *match, unsigne
last = mid; last = mid;
} }
if (first < num) { if (first < num) {
unsigned char now[20], next[20]; const unsigned char *now, *next;
nth_packed_object_sha1(p, first, now); now = nth_packed_object_sha1(p, first);
if (match_sha(len, match, now)) { if (match_sha(len, match, now)) {
if (nth_packed_object_sha1(p, first+1, next) || next = nth_packed_object_sha1(p, first+1);
!match_sha(len, match, next)) { if (!next|| !match_sha(len, match, next)) {
/* unique within this pack */ /* unique within this pack */
if (!found) { if (!found) {
hashcpy(found_sha1, now); found_sha1 = now;
found++; found++;
} }
else if (hashcmp(found_sha1, now)) { else if (hashcmp(found_sha1, now)) {