Skip to content

Commit

Permalink
clean up and optimize nth_packed_object_sha1() usage
Browse files Browse the repository at this point in the history
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 d72308e)

Signed-off-by: Nicolas Pitre <nico@cam.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Nicolas Pitre authored and Junio C Hamano committed Jul 16, 2008
1 parent c86a54b commit 3205364
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 26 deletions.
2 changes: 1 addition & 1 deletion builtin-pack-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 3 additions & 6 deletions fsck-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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));
}
}

Expand Down
11 changes: 7 additions & 4 deletions pack-check.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 5 additions & 6 deletions sha1_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 8 additions & 8 deletions sha1_name.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand All @@ -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)) {
Expand Down

0 comments on commit 3205364

Please sign in to comment.