pack-objects: get rid of create_final_object_list()
Because we don't have to know the SHA1 h(hence the name) of the pack up front anymore, let's get rid of yet another global sorted object list and sort them only in write_index_file(), then compute the object list SHA1 on the fly. This has the advantage of saving another chunk of memory, and the sorted list SHA1 won't be computed needlessly on servers during a fetch. Of course the cunning plan is also to make write_index_file() much like the function with the same name in index-pack.c for an eventual easy sharing. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Nicolas Pitre committed
Apr 16, 2007 at 12:31 UTC
81a216a5d6a12976b20d9a39829562f280ae96f2
1 file changed
+72
-55
builtin-pack-objects.c
+72
-55
@@ -59,17 +59,16 @@ struct object_entry {
59
* heuristics.
60
*/
61
62
-static unsigned char object_list_sha1[20];
62
static int non_empty;
63
static int no_reuse_delta;
64
static int local;
65
static int incremental;
66
static int allow_ofs_delta;
67
69
-static struct object_entry **sorted_by_sha;
68
static struct object_entry *objects;
69
static uint32_t nr_objects, nr_alloc, nr_result;
72
-static const char *base_name;
70
+static const char *pack_tmp_name, *idx_tmp_name;
71
+static char tmpname[PATH_MAX];
72
static unsigned char pack_file_sha1[20];
73
static int progress = 1;
74
static volatile sig_atomic_t progress_update;
@@ -578,13 +577,19 @@ static off_t write_pack_file(void)
577
unsigned last_percent = 999;
578
int do_progress = progress;
579
581
- if (!base_name) {
580
+ if (pack_to_stdout) {
581
f = sha1fd(1, "<stdout>");
582
do_progress >>= 1;
583
+ } else {
584
+ int fd;
585
+ snprintf(tmpname, sizeof(tmpname), "tmp_pack_XXXXXX");
586
+ fd = mkstemp(tmpname);
587
+ if (fd < 0)
588
+ die("unable to create %s: %s\n", tmpname, strerror(errno));
589
+ pack_tmp_name = xstrdup(tmpname);
590
+ f = sha1fd(fd, pack_tmp_name);
591
}
585
- else
586
- f = sha1create("%s-%s.%s", base_name,
587
- sha1_to_hex(object_list_sha1), "pack");
592
+
593
if (do_progress)
594
fprintf(stderr, "Writing %u objects.\n", nr_result);
595
@@ -618,18 +623,46 @@ static off_t write_pack_file(void)
623
return last_obj_offset;
624
}
625
626
+static int sha1_sort(const void *_a, const void *_b)
627
+{
628
+ const struct object_entry *a = *(struct object_entry **)_a;
629
+ const struct object_entry *b = *(struct object_entry **)_b;
630
+ return hashcmp(a->sha1, b->sha1);
631
+}
632
+
633
static uint32_t index_default_version = 1;
634
static uint32_t index_off32_limit = 0x7fffffff;
635
624
-static void write_index_file(off_t last_obj_offset)
636
+static void write_index_file(off_t last_obj_offset, unsigned char *sha1)
637
{
626
- uint32_t i;
627
- struct sha1file *f = sha1create("%s-%s.%s", base_name,
628
- sha1_to_hex(object_list_sha1), "idx");
629
- struct object_entry **list = sorted_by_sha;
630
- struct object_entry **last = list + nr_result;
638
+ struct sha1file *f;
639
+ struct object_entry **sorted_by_sha, **list, **last;
640
uint32_t array[256];
632
- uint32_t index_version;
641
+ uint32_t i, index_version;
642
+ SHA_CTX ctx;
643
+ int fd;
644
+
645
+ snprintf(tmpname, sizeof(tmpname), "tmp_idx_XXXXXX");
646
+ fd = mkstemp(tmpname);
647
+ if (fd < 0)
648
+ die("unable to create %s: %s\n", tmpname, strerror(errno));
649
+ idx_tmp_name = xstrdup(tmpname);
650
+ f = sha1fd(fd, idx_tmp_name);
651
+
652
+ if (nr_result) {
653
+ uint32_t j = 0;
654
+ sorted_by_sha =
655
+ xcalloc(nr_result, sizeof(struct object_entry *));
656
+ for (i = 0; i < nr_objects; i++)
657
+ if (!objects[i].preferred_base)
658
+ sorted_by_sha[j++] = objects + i;
659
+ if (j != nr_result)
660
+ die("listed %u objects while expecting %u", j, nr_result);
661
+ qsort(sorted_by_sha, nr_result, sizeof(*sorted_by_sha), sha1_sort);
662
+ list = sorted_by_sha;
663
+ last = sorted_by_sha + nr_result;
664
+ } else
665
+ sorted_by_sha = list = last = NULL;
666
667
/* if last object's offset is >= 2^31 we should use index V2 */
668
index_version = (last_obj_offset >> 31) ? 2 : index_default_version;
@@ -660,9 +693,10 @@ static void write_index_file(off_t last_obj_offset)
693
}
694
sha1write(f, array, 256 * 4);
695
663
- /*
664
- * Write the actual SHA1 entries..
665
- */
696
+ /* Compute the SHA1 hash of sorted object names. */
697
+ SHA1_Init(&ctx);
698
+
699
+ /* Write the actual SHA1 entries. */
700
list = sorted_by_sha;
701
for (i = 0; i < nr_result; i++) {
702
struct object_entry *entry = *list++;
@@ -671,6 +705,7 @@ static void write_index_file(off_t last_obj_offset)
705
sha1write(f, &offset, 4);
706
}
707
sha1write(f, entry->sha1, 20);
708
+ SHA1_Update(&ctx, entry->sha1, 20);
709
}
710
711
if (index_version >= 2) {
@@ -711,6 +746,8 @@ static void write_index_file(off_t last_obj_offset)
746
747
sha1write(f, pack_file_sha1, 20);
748
sha1close(f, NULL, 1);
749
+ free(sorted_by_sha);
750
+ SHA1_Final(sha1, &ctx);
751
}
752
753
static int locate_object_entry_hash(const unsigned char *sha1)
@@ -789,6 +826,8 @@ static int add_object_entry(const unsigned char *sha1, unsigned hash, int exclud
826
if (ix >= 0) {
827
if (exclude) {
828
entry = objects + object_ix[ix] - 1;
829
+ if (!entry->preferred_base)
830
+ nr_result--;
831
entry->preferred_base = 1;
832
}
833
return 0;
@@ -821,6 +860,8 @@ static int add_object_entry(const unsigned char *sha1, unsigned hash, int exclud
860
entry->hash = hash;
861
if (exclude)
862
entry->preferred_base = 1;
863
+ else
864
+ nr_result++;
865
if (found_pack) {
866
entry->in_pack = found_pack;
867
entry->in_pack_offset = found_offset;
@@ -1181,30 +1222,6 @@ static void get_object_details(void)
1222
check_object(entry);
1223
}
1224
1184
-static int sha1_sort(const void *_a, const void *_b)
1185
-{
1186
- const struct object_entry *a = *(struct object_entry **)_a;
1187
- const struct object_entry *b = *(struct object_entry **)_b;
1188
- return hashcmp(a->sha1, b->sha1);
1189
-}
1190
-
1191
-static struct object_entry **create_final_object_list(void)
1192
-{
1193
- struct object_entry **list;
1194
- uint32_t i, j;
1195
-
1196
- for (i = nr_result = 0; i < nr_objects; i++)
1197
- if (!objects[i].preferred_base)
1198
- nr_result++;
1199
- list = xmalloc(nr_result * sizeof(struct object_entry *));
1200
- for (i = j = 0; i < nr_objects; i++) {
1201
- if (!objects[i].preferred_base)
1202
- list[j++] = objects + i;
1203
- }
1204
- qsort(list, nr_result, sizeof(struct object_entry *), sha1_sort);
1205
- return list;
1206
-}
1207
-
1225
static int type_size_sort(const void *_a, const void *_b)
1226
{
1227
const struct object_entry *a = *(struct object_entry **)_a;
@@ -1561,13 +1578,12 @@ static void get_object_list(int ac, const char **av)
1578
1579
int cmd_pack_objects(int argc, const char **argv, const char *prefix)
1580
{
1564
- SHA_CTX ctx;
1581
int depth = 10;
1566
- struct object_entry **list;
1567
- off_t last_obj_offset;
1582
int use_internal_rev_list = 0;
1583
int thin = 0;
1584
uint32_t i;
1585
+ off_t last_obj_offset;
1586
+ const char *base_name = NULL;
1587
const char **rp_av;
1588
int rp_ac_alloc = 64;
1589
int rp_ac;
@@ -1712,20 +1728,10 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
1728
1729
if (progress)
1730
fprintf(stderr, "Done counting %u objects.\n", nr_objects);
1715
- sorted_by_sha = create_final_object_list();
1731
if (non_empty && !nr_result)
1732
return 0;
1718
-
1719
- SHA1_Init(&ctx);
1720
- list = sorted_by_sha;
1721
- for (i = 0; i < nr_result; i++) {
1722
- struct object_entry *entry = *list++;
1723
- SHA1_Update(&ctx, entry->sha1, 20);
1724
- }
1725
- SHA1_Final(object_list_sha1, &ctx);
1733
if (progress && (nr_objects != nr_result))
1734
fprintf(stderr, "Result has %u objects.\n", nr_result);
1728
-
1735
if (nr_result)
1736
prepare_pack(window, depth);
1737
if (progress == 1 && pack_to_stdout) {
@@ -1737,7 +1743,18 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
1743
}
1744
last_obj_offset = write_pack_file();
1745
if (!pack_to_stdout) {
1740
- write_index_file(last_obj_offset);
1746
+ unsigned char object_list_sha1[20];
1747
+ write_index_file(last_obj_offset, object_list_sha1);
1748
+ snprintf(tmpname, sizeof(tmpname), "%s-%s.pack",
1749
+ base_name, sha1_to_hex(object_list_sha1));
1750
+ if (rename(pack_tmp_name, tmpname))
1751
+ die("unable to rename temporary pack file: %s",
1752
+ strerror(errno));
1753
+ snprintf(tmpname, sizeof(tmpname), "%s-%s.idx",
1754
+ base_name, sha1_to_hex(object_list_sha1));
1755
+ if (rename(idx_tmp_name, tmpname))
1756
+ die("unable to rename temporary index file: %s",
1757
+ strerror(errno));
1758
puts(sha1_to_hex(object_list_sha1));
1759
}
1760
if (progress)