Skip to content

Commit fb956c1

Browse files
committed
write_idx_file: need_large_offset() helper function
Signed-off-by: Junio C Hamano <[email protected]>
1 parent e337a04 commit fb956c1

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

pack-write.c

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ static int sha1_compare(const void *_a, const void *_b)
1616
return hashcmp(a->sha1, b->sha1);
1717
}
1818

19+
static int need_large_offset(off_t offset, const struct pack_idx_option *opts)
20+
{
21+
return (offset >> 31) || (opts->off32_limit < offset);
22+
}
23+
1924
/*
2025
* On entry *sha1 contains the pack content SHA1 hash, on exit it is
2126
* the SHA1 hash of sorted object names. The objects array passed in
@@ -65,7 +70,7 @@ const char *write_idx_file(const char *index_name, struct pack_idx_entry **objec
6570
}
6671

6772
/* if last object's offset is >= 2^31 we should use index V2 */
68-
index_version = (last_obj_offset >> 31) ? 2 : opts->version;
73+
index_version = need_large_offset(last_obj_offset, opts) ? 2 : opts->version;
6974

7075
/* index versions 2 and above need a header */
7176
if (index_version >= 2) {
@@ -125,8 +130,11 @@ const char *write_idx_file(const char *index_name, struct pack_idx_entry **objec
125130
list = sorted_by_sha;
126131
for (i = 0; i < nr_objects; i++) {
127132
struct pack_idx_entry *obj = *list++;
128-
uint32_t offset = (obj->offset <= opts->off32_limit) ?
129-
obj->offset : (0x80000000 | nr_large_offset++);
133+
uint32_t offset;
134+
135+
offset = (need_large_offset(obj->offset, opts)
136+
? (0x80000000 | nr_large_offset++)
137+
: obj->offset);
130138
offset = htonl(offset);
131139
sha1write(f, &offset, 4);
132140
}
@@ -136,13 +144,14 @@ const char *write_idx_file(const char *index_name, struct pack_idx_entry **objec
136144
while (nr_large_offset) {
137145
struct pack_idx_entry *obj = *list++;
138146
uint64_t offset = obj->offset;
139-
if (offset > opts->off32_limit) {
140-
uint32_t split[2];
141-
split[0] = htonl(offset >> 32);
142-
split[1] = htonl(offset & 0xffffffff);
143-
sha1write(f, split, 8);
144-
nr_large_offset--;
145-
}
147+
uint32_t split[2];
148+
149+
if (!need_large_offset(offset, opts))
150+
continue;
151+
split[0] = htonl(offset >> 32);
152+
split[1] = htonl(offset & 0xffffffff);
153+
sha1write(f, split, 8);
154+
nr_large_offset--;
146155
}
147156
}
148157

0 commit comments

Comments
 (0)