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 <nico@fluxnic.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nicolas Pitre committed
Feb 17, 2010 at 14:05 UTC
427cb22c40484f9b8c2881bc9e99636591a22655
1 file changed
+17
-46
fast-import.c
+17
-46
@@ -854,67 +854,30 @@ static void start_packfile(void)
854
all_packs[pack_id] = p;
855
}
856
857
-static int oecmp (const void *a_, const void *b_)
857
+static const char *create_index(void)
858
{
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;
862
struct object_entry_pool *o;
871
- uint32_t array[256];
872
- int i, idx_fd;
863
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));
866
c = idx;
867
for (o = blocks; o; o = o->next_pool)
868
for (e = o->next_free; e-- != o->entries;)
869
if (pack_id == e->pack_id)
880
- *c++ = e;
870
+ *c++ = &e->idx;
871
last = idx + object_count;
872
if (c != last)
873
die("internal consistency error creating the index");
884
- qsort(idx, object_count, sizeof(struct object_entry*), oecmp);
874
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);
876
free(idx);
913
- git_SHA1_Final(pack_data->sha1, &ctx);
877
return tmpfile;
878
}
879
917
-static char *keep_pack(char *curr_index_name)
880
+static char *keep_pack(const char *curr_index_name)
881
{
882
static char name[PATH_MAX];
883
static const char *keep_msg = "fast-import";
@@ -936,6 +899,7 @@ static char *keep_pack(char *curr_index_name)
899
get_object_directory(), sha1_to_hex(pack_data->sha1));
900
if (move_temp_to_file(curr_index_name, name))
901
die("cannot store index file");
902
+ free((void *)curr_index_name);
903
return name;
904
}
905
@@ -1134,6 +1098,8 @@ static int store_object(
1098
object_count++;
1099
object_count_by_type[type]++;
1100
1101
+ crc32_begin(pack_file);
1102
+
1103
if (delta) {
1104
unsigned long ofs = e->idx.offset - last->offset;
1105
unsigned pos = sizeof(hdr) - 1;
@@ -1160,6 +1126,8 @@ static int store_object(
1126
sha1write(pack_file, out, s.total_out);
1127
pack_size += s.total_out;
1128
1129
+ e->idx.crc32 = crc32_end(pack_file);
1130
+
1131
free(out);
1132
free(delta);
1133
if (last) {
@@ -1219,6 +1187,8 @@ static void stream_blob(uintmax_t len, unsigned char *sha1out, uintmax_t mark)
1187
git_SHA1_Init(&c);
1188
git_SHA1_Update(&c, out_buf, hdrlen);
1189
1190
+ crc32_begin(pack_file);
1191
+
1192
memset(&s, 0, sizeof(s));
1193
deflateInit(&s, pack_compression_level);
1194
@@ -1288,6 +1258,7 @@ static void stream_blob(uintmax_t len, unsigned char *sha1out, uintmax_t mark)
1258
e->type = OBJ_BLOB;
1259
e->pack_id = pack_id;
1260
e->idx.offset = offset;
1261
+ e->idx.crc32 = crc32_end(pack_file);
1262
object_count++;
1263
object_count_by_type[OBJ_BLOB]++;
1264
}