Implemented manual packfile switching in fast-import.
To help importers which are dealing with massive amounts of data fast-import needs to be able to close the packfile it is currently writing to and open a new packfile for any additional data that will be received. A new 'checkpoint' command has been introduced which can be used by the frontend import process to force this to occur at any time. This may be useful to ensure a very long running import doesn't lose any work due to unexpected failures. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Shawn O. Pearce committed
Jan 15, 2007 at 06:35 UTC
7bfe6e261378a30980886994dabc0e7e4c9ce3d8
1 file changed
+65
-24
fast-import.c
+65
-24
@@ -40,6 +40,9 @@ Format of STDIN stream:
40
('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
41
lf;
42
43
+ checkpoint ::= 'checkpoint' lf
44
+ lf;
45
+
46
# note: the first idnum in a stream should be 1 and subsequent
47
# idnums should not have gaps between values as this will cause
48
# the stream parser to reserve space for the gapped values. An
@@ -112,6 +115,7 @@ struct object_entry
115
struct object_entry *next;
116
unsigned long offset;
117
unsigned type : TYPE_BITS;
118
+ unsigned pack_id : 16;
119
unsigned char sha1[20];
120
};
121
@@ -234,10 +238,10 @@ static struct atom_str **atom_table;
238
239
/* The .pack file being generated */
240
static const char *base_name;
237
-static unsigned int pack_count;
238
-static char *pack_name;
241
+static unsigned int pack_id;
242
static char *idx_name;
243
static struct packed_git *pack_data;
244
+static struct packed_git **all_packs;
245
static int pack_fd;
246
static unsigned long pack_size;
247
static unsigned char pack_sha1[20];
@@ -299,6 +303,7 @@ static struct object_entry* new_object(unsigned char *sha1)
303
alloc_objects(object_entry_alloc);
304
305
e = blocks->next_free++;
306
+ e->pack_id = pack_id;
307
hashcpy(e->sha1, sha1);
308
return e;
309
}
@@ -597,29 +602,30 @@ static void yread(int fd, void *buffer, size_t length)
602
603
static void start_packfile()
604
{
605
+ struct packed_git *p;
606
struct pack_header hdr;
607
602
- pack_count++;
603
- pack_name = xmalloc(strlen(base_name) + 11);
608
idx_name = xmalloc(strlen(base_name) + 11);
605
- sprintf(pack_name, "%s%5.5i.pack", base_name, pack_count);
606
- sprintf(idx_name, "%s%5.5i.idx", base_name, pack_count);
609
+ p = xcalloc(1, sizeof(*p) + strlen(base_name) + 13);
610
+ sprintf(p->pack_name, "%s%5.5i.pack", base_name, pack_id + 1);
611
+ sprintf(idx_name, "%s%5.5i.idx", base_name, pack_id + 1);
612
608
- pack_fd = open(pack_name, O_RDWR|O_CREAT|O_EXCL, 0666);
613
+ pack_fd = open(p->pack_name, O_RDWR|O_CREAT|O_EXCL, 0666);
614
if (pack_fd < 0)
610
- die("Can't create %s: %s", pack_name, strerror(errno));
611
-
612
- pack_data = xcalloc(1, sizeof(*pack_data) + strlen(pack_name) + 2);
613
- strcpy(pack_data->pack_name, pack_name);
614
- pack_data->pack_fd = pack_fd;
615
+ die("Can't create %s: %s", p->pack_name, strerror(errno));
616
+ p->pack_fd = pack_fd;
617
618
hdr.hdr_signature = htonl(PACK_SIGNATURE);
619
hdr.hdr_version = htonl(2);
620
hdr.hdr_entries = 0;
619
-
621
write_or_die(pack_fd, &hdr, sizeof(hdr));
622
+
623
+ pack_data = p;
624
pack_size = sizeof(hdr);
625
object_count = 0;
626
+
627
+ all_packs = xrealloc(all_packs, sizeof(*all_packs) * (pack_id + 1));
628
+ all_packs[pack_id] = p;
629
}
630
631
static void fixup_header_footer()
@@ -673,7 +679,8 @@ static void write_index(const char *idx_name)
679
c = idx;
680
for (o = blocks; o; o = o->next_pool)
681
for (e = o->entries; e != o->next_free; e++)
676
- *c++ = e;
682
+ if (pack_id == e->pack_id)
683
+ *c++ = e;
684
last = idx + object_count;
685
qsort(idx, object_count, sizeof(struct object_entry*), oecmp);
686
@@ -704,13 +711,28 @@ static void write_index(const char *idx_name)
711
712
static void end_packfile()
713
{
714
+ struct packed_git *old_p = pack_data, *new_p;
715
+
716
fixup_header_footer();
708
- close(pack_fd);
717
write_index(idx_name);
718
711
- free(pack_name);
719
+ /* Register the packfile with core git's machinary. */
720
+ new_p = add_packed_git(idx_name, strlen(idx_name), 1);
721
+ if (!new_p)
722
+ die("core git rejected index %s", idx_name);
723
+ new_p->windows = old_p->windows;
724
+ new_p->pack_fd = old_p->pack_fd;
725
+ all_packs[pack_id++] = new_p;
726
+ install_packed_git(new_p);
727
+ free(old_p);
728
free(idx_name);
713
- free(pack_data);
729
+
730
+ /* We can't carry a delta across packfiles. */
731
+ free(last_blob.data);
732
+ last_blob.data = NULL;
733
+ last_blob.len = 0;
734
+ last_blob.offset = 0;
735
+ last_blob.depth = 0;
736
}
737
738
static size_t encode_header(
@@ -832,11 +854,15 @@ static int store_object(
854
return 0;
855
}
856
835
-static void *gfi_unpack_entry(unsigned long ofs, unsigned long *sizep)
857
+static void *gfi_unpack_entry(
858
+ struct object_entry *oe,
859
+ unsigned long *sizep)
860
{
837
- char type[20];
838
- pack_data->pack_size = pack_size + 20;
839
- return unpack_entry(pack_data, ofs, type, sizep);
861
+ static char type[20];
862
+ struct packed_git *p = all_packs[oe->pack_id];
863
+ if (p == pack_data)
864
+ p->pack_size = pack_size + 20;
865
+ return unpack_entry(p, oe->offset, type, sizep);
866
}
867
868
static const char *get_mode(const char *str, unsigned int *modep)
@@ -871,7 +897,7 @@ static void load_tree(struct tree_entry *root)
897
if (myoe->type != OBJ_TREE)
898
die("Not a tree: %s", sha1_to_hex(sha1));
899
t->delta_depth = 0;
874
- buf = gfi_unpack_entry(myoe->offset, &size);
900
+ buf = gfi_unpack_entry(myoe, &size);
901
} else {
902
char type[20];
903
buf = read_sha1_file(sha1, type, &size);
@@ -971,7 +997,9 @@ static void store_tree(struct tree_entry *root)
997
}
998
999
le = find_object(root->versions[0].sha1);
974
- if (!S_ISDIR(root->versions[0].mode) || !le) {
1000
+ if (!S_ISDIR(root->versions[0].mode)
1001
+ || !le
1002
+ || le->pack_id != pack_id) {
1003
lo.data = NULL;
1004
lo.depth = 0;
1005
} else {
@@ -1385,7 +1413,7 @@ static void cmd_from(struct branch *b)
1413
if (oe->type != OBJ_COMMIT)
1414
die("Mark :%lu not a commit", idnum);
1415
hashcpy(b->sha1, oe->sha1);
1388
- buf = gfi_unpack_entry(oe->offset, &size);
1416
+ buf = gfi_unpack_entry(oe, &size);
1417
if (!buf || size < 46)
1418
die("Not a valid commit: %s", from);
1419
if (memcmp("tree ", buf, 5)
@@ -1713,6 +1741,15 @@ static void cmd_reset_branch()
1741
cmd_from(b);
1742
}
1743
1744
+static void cmd_checkpoint()
1745
+{
1746
+ if (object_count) {
1747
+ end_packfile();
1748
+ start_packfile();
1749
+ }
1750
+ read_next_command();
1751
+}
1752
+
1753
static const char fast_import_usage[] =
1754
"git-fast-import [--objects=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file] [--branch-log=log] temp.pack";
1755
@@ -1771,6 +1808,8 @@ int main(int argc, const char **argv)
1808
cmd_new_tag();
1809
else if (!strncmp("reset ", command_buf.buf, 6))
1810
cmd_reset_branch();
1811
+ else if (!strcmp("checkpoint", command_buf.buf))
1812
+ cmd_checkpoint();
1813
else
1814
die("Unsupported command: %s", command_buf.buf);
1815
}
@@ -1800,6 +1839,8 @@ int main(int argc, const char **argv)
1839
fprintf(stderr, " pools: %10lu KiB\n", total_allocd/1024);
1840
fprintf(stderr, " objects: %10lu KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
1841
fprintf(stderr, "---------------------------------------------------------------------\n");
1842
+ pack_report();
1843
+ fprintf(stderr, "---------------------------------------------------------------------\n");
1844
fprintf(stderr, "\n");
1845
1846
return 0;