+ struct object_entry *a = *((struct object_entry**)_a);
+ struct object_entry *b = *((struct object_entry**)_b);
+ return memcmp(a->sha1, b->sha1, sizeof(a->sha1));
+}
+
+static void write_index(const char *idx_name)
+{
+ struct sha1file *f;
+ struct object_entry **idx, **c, **last;
+ struct object_entry *e;
+ struct overflow_object_entry *o;
+ unsigned int array[256];
+ int i;
+
+ /* Build the sorted table of object IDs. */
+ idx = xmalloc(object_count * sizeof(struct object_entry*));
+ c = idx;
+ for (e = pool_start; e != pool_next; e++)
+ *c++ = e;
+ for (o = overflow; o; o = o->next)
+ *c++ = &o->oe;
+ last = idx + object_count;
+ qsort(idx, object_count, sizeof(struct object_entry*), oecmp);
+
+ /* Generate the fan-out array. */
+ c = idx;
+ for (i = 0; i < 256; i++) {
+ struct object_entry **next = c;;
+ while (next < last) {
+ if ((*next)->sha1[0] != i)
+ break;
+ next++;
+ }
+ array[i] = htonl(next - idx);
+ c = next;
+ }
+
+ f = sha1create("%s", idx_name);
+ sha1write(f, array, 256 * sizeof(int));
+ for (c = idx; c != last; c++) {
+ unsigned int offset = htonl((*c)->offset);
+ sha1write(f, &offset, 4);
+ sha1write(f, (*c)->sha1, sizeof((*c)->sha1));
+ }
+ sha1write(f, packsha1, sizeof(packsha1));
+ sha1close(f, NULL, 1);
+ free(idx);
+}
+
+int main(int argc, const char **argv)
+{
+ const char *base_name = argv[1];
+ int est_obj_cnt = atoi(argv[2]);
+ char *pack_name;
+ char *idx_name;
+
+ pack_name = xmalloc(strlen(base_name) + 6);
+ sprintf(pack_name, "%s.pack", base_name);
+ idx_name = xmalloc(strlen(base_name) + 5);
+ sprintf(idx_name, "%s.idx", base_name);
+
+ packfd = open(pack_name, O_RDWR|O_CREAT|O_TRUNC, 0666);