Skip to content

Commit 427cb22

Browse files
npitregitster
authored andcommitted
fast-import: use write_idx_file() instead of custom code
This allows for the creation of pack index version 2 with its object CRC and the possibility for a pack to be larger than 4 GB. Signed-off-by: Nicolas Pitre <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2128181 commit 427cb22

File tree

1 file changed

+17
-46
lines changed

1 file changed

+17
-46
lines changed

fast-import.c

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -854,67 +854,30 @@ static void start_packfile(void)
854854
all_packs[pack_id] = p;
855855
}
856856

857-
static int oecmp (const void *a_, const void *b_)
857+
static const char *create_index(void)
858858
{
859-
struct object_entry *a = *((struct object_entry**)a_);
860-
struct object_entry *b = *((struct object_entry**)b_);
861-
return hashcmp(a->idx.sha1, b->idx.sha1);
862-
}
863-
864-
static char *create_index(void)
865-
{
866-
static char tmpfile[PATH_MAX];
867-
git_SHA_CTX ctx;
868-
struct sha1file *f;
869-
struct object_entry **idx, **c, **last, *e;
859+
const char *tmpfile;
860+
struct pack_idx_entry **idx, **c, **last;
861+
struct object_entry *e;
870862
struct object_entry_pool *o;
871-
uint32_t array[256];
872-
int i, idx_fd;
873863

874-
/* Build the sorted table of object IDs. */
875-
idx = xmalloc(object_count * sizeof(struct object_entry*));
864+
/* Build the table of object IDs. */
865+
idx = xmalloc(object_count * sizeof(*idx));
876866
c = idx;
877867
for (o = blocks; o; o = o->next_pool)
878868
for (e = o->next_free; e-- != o->entries;)
879869
if (pack_id == e->pack_id)
880-
*c++ = e;
870+
*c++ = &e->idx;
881871
last = idx + object_count;
882872
if (c != last)
883873
die("internal consistency error creating the index");
884-
qsort(idx, object_count, sizeof(struct object_entry*), oecmp);
885874

886-
/* Generate the fan-out array. */
887-
c = idx;
888-
for (i = 0; i < 256; i++) {
889-
struct object_entry **next = c;
890-
while (next < last) {
891-
if ((*next)->idx.sha1[0] != i)
892-
break;
893-
next++;
894-
}
895-
array[i] = htonl(next - idx);
896-
c = next;
897-
}
898-
899-
idx_fd = odb_mkstemp(tmpfile, sizeof(tmpfile),
900-
"pack/tmp_idx_XXXXXX");
901-
f = sha1fd(idx_fd, tmpfile);
902-
sha1write(f, array, 256 * sizeof(int));
903-
git_SHA1_Init(&ctx);
904-
for (c = idx; c != last; c++) {
905-
uint32_t offset = htonl((*c)->idx.offset);
906-
sha1write(f, &offset, 4);
907-
sha1write(f, (*c)->idx.sha1, sizeof((*c)->idx.sha1));
908-
git_SHA1_Update(&ctx, (*c)->idx.sha1, 20);
909-
}
910-
sha1write(f, pack_data->sha1, sizeof(pack_data->sha1));
911-
sha1close(f, NULL, CSUM_FSYNC);
875+
tmpfile = write_idx_file(NULL, idx, object_count, pack_data->sha1);
912876
free(idx);
913-
git_SHA1_Final(pack_data->sha1, &ctx);
914877
return tmpfile;
915878
}
916879

917-
static char *keep_pack(char *curr_index_name)
880+
static char *keep_pack(const char *curr_index_name)
918881
{
919882
static char name[PATH_MAX];
920883
static const char *keep_msg = "fast-import";
@@ -936,6 +899,7 @@ static char *keep_pack(char *curr_index_name)
936899
get_object_directory(), sha1_to_hex(pack_data->sha1));
937900
if (move_temp_to_file(curr_index_name, name))
938901
die("cannot store index file");
902+
free((void *)curr_index_name);
939903
return name;
940904
}
941905

@@ -1134,6 +1098,8 @@ static int store_object(
11341098
object_count++;
11351099
object_count_by_type[type]++;
11361100

1101+
crc32_begin(pack_file);
1102+
11371103
if (delta) {
11381104
unsigned long ofs = e->idx.offset - last->offset;
11391105
unsigned pos = sizeof(hdr) - 1;
@@ -1160,6 +1126,8 @@ static int store_object(
11601126
sha1write(pack_file, out, s.total_out);
11611127
pack_size += s.total_out;
11621128

1129+
e->idx.crc32 = crc32_end(pack_file);
1130+
11631131
free(out);
11641132
free(delta);
11651133
if (last) {
@@ -1219,6 +1187,8 @@ static void stream_blob(uintmax_t len, unsigned char *sha1out, uintmax_t mark)
12191187
git_SHA1_Init(&c);
12201188
git_SHA1_Update(&c, out_buf, hdrlen);
12211189

1190+
crc32_begin(pack_file);
1191+
12221192
memset(&s, 0, sizeof(s));
12231193
deflateInit(&s, pack_compression_level);
12241194

@@ -1288,6 +1258,7 @@ static void stream_blob(uintmax_t len, unsigned char *sha1out, uintmax_t mark)
12881258
e->type = OBJ_BLOB;
12891259
e->pack_id = pack_id;
12901260
e->idx.offset = offset;
1261+
e->idx.crc32 = crc32_end(pack_file);
12911262
object_count++;
12921263
object_count_by_type[OBJ_BLOB]++;
12931264
}

0 commit comments

Comments
 (0)