Skip to content

Commit d72308e

Browse files
Nicolas PitreJunio C Hamano
authored andcommitted
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>
1 parent d5ad36f commit d72308e

File tree

6 files changed

+24
-25
lines changed

6 files changed

+24
-25
lines changed

builtin-fsck.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ static int fsck_tag(struct tag *tag)
348348
return 0;
349349
}
350350

351-
static int fsck_sha1(unsigned char *sha1)
351+
static int fsck_sha1(const unsigned char *sha1)
352352
{
353353
struct object *obj = parse_object(sha1);
354354
if (!obj) {
@@ -648,11 +648,8 @@ int cmd_fsck(int argc, char **argv, const char *prefix)
648648

649649
for (p = packed_git; p; p = p->next) {
650650
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));
656653
}
657654
}
658655

builtin-pack-objects.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ static const unsigned char *find_packed_object_name(struct packed_git *p,
222222
off_t ofs)
223223
{
224224
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);
226226
}
227227

228228
static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)

cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ extern unsigned char* use_pack(struct packed_git *, struct pack_window **, off_t
428428
extern void unuse_pack(struct pack_window **);
429429
extern struct packed_git *add_packed_git(const char *, int, int);
430430
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);
432432
extern off_t find_pack_entry_one(const unsigned char *, struct packed_git *);
433433
extern void *unpack_entry(struct packed_git *, off_t, enum object_type *, unsigned long *);
434434
extern unsigned long unpack_object_header_gently(const unsigned char *buf, unsigned long len, enum object_type *type, unsigned long *sizep);

pack-check.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,14 @@ static int verify_packfile(struct packed_git *p,
4242
*/
4343
nr_objects = num_packed_objects(p);
4444
for (i = 0, err = 0; i < nr_objects; i++) {
45-
unsigned char sha1[20];
45+
const unsigned char *sha1;
4646
void *data;
4747
enum object_type type;
4848
unsigned long size;
4949
off_t offset;
5050

51-
if (nth_packed_object_sha1(p, i, sha1))
51+
sha1 = nth_packed_object_sha1(p, i);
52+
if (!sha1)
5253
die("internal error pack-check nth-packed-object");
5354
offset = find_pack_entry_one(sha1, p);
5455
if (!offset)
@@ -82,14 +83,16 @@ static void show_pack_info(struct packed_git *p)
8283
memset(chain_histogram, 0, sizeof(chain_histogram));
8384

8485
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];
8688
const char *type;
8789
unsigned long size;
8890
unsigned long store_size;
8991
off_t offset;
9092
unsigned int delta_chain_length;
9193

92-
if (nth_packed_object_sha1(p, i, sha1))
94+
sha1 = nth_packed_object_sha1(p, i);
95+
if (!sha1)
9396
die("internal error pack-check nth-packed-object");
9497
offset = find_pack_entry_one(sha1, p);
9598
if (!offset)

sha1_file.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,15 +1532,14 @@ uint32_t num_packed_objects(const struct packed_git *p)
15321532
return (uint32_t)((p->index_size - 20 - 20 - 4*256) / 24);
15331533
}
15341534

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)
15371537
{
15381538
const unsigned char *index = p->index_data;
15391539
index += 4 * 256;
15401540
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;
15441543
}
15451544

15461545
off_t find_pack_entry_one(const unsigned char *sha1,

sha1_name.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ static int match_sha(unsigned len, const unsigned char *a, const unsigned char *
7171
static int find_short_packed_object(int len, const unsigned char *match, unsigned char *sha1)
7272
{
7373
struct packed_git *p;
74-
unsigned char found_sha1[20];
74+
const unsigned char *found_sha1 = NULL;
7575
int found = 0;
7676

7777
prepare_packed_git();
@@ -80,10 +80,10 @@ static int find_short_packed_object(int len, const unsigned char *match, unsigne
8080
uint32_t first = 0, last = num;
8181
while (first < last) {
8282
uint32_t mid = (first + last) / 2;
83-
unsigned char now[20];
83+
const unsigned char *now;
8484
int cmp;
8585

86-
nth_packed_object_sha1(p, mid, now);
86+
now = nth_packed_object_sha1(p, mid);
8787
cmp = hashcmp(match, now);
8888
if (!cmp) {
8989
first = mid;
@@ -96,14 +96,14 @@ static int find_short_packed_object(int len, const unsigned char *match, unsigne
9696
last = mid;
9797
}
9898
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);
101101
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)) {
104104
/* unique within this pack */
105105
if (!found) {
106-
hashcpy(found_sha1, now);
106+
found_sha1 = now;
107107
found++;
108108
}
109109
else if (hashcmp(found_sha1, now)) {

0 commit comments

Comments
 (0)