@matheusbsbs / matheus-bagliano / commits / d83971688b
Fork 0

Added mark store/find to fast-import.

Marks are now saved when the mark directive gets used by the frontend and may be used in place of a SHA1 expression to locate a previous SHA1 which fast-import may have generated. This is particularly useful with commits where the frontend does not (easily) have the ability to compute the SHA1 for an arbitrary commit but needs it to generate a branch or tag from that commit. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed Aug 23, 2006 at 04:17 UTC d83971688ba42e4cd37908f4d776801a997ca421
1 file changed +87 -17
fast-import.c
+87 -17
@@ -121,6 +121,15 @@ struct object_entry_pool
121 struct object_entry entries[FLEX_ARRAY]; /* more */
122 };
123
124 +struct mark_set
125 +{
126 + int shift;
127 + union {
128 + struct object_entry *marked[1024];
129 + struct mark_set *sets[1024];
130 + } data;
131 +};
132 +
133 struct last_object
134 {
135 void *data;
@@ -183,6 +192,7 @@ static unsigned long alloc_count;
192 static unsigned long branch_count;
193 static unsigned long object_count;
194 static unsigned long duplicate_count;
195 +static unsigned long marks_set_count;
196 static unsigned long object_count_by_type[9];
197 static unsigned long duplicate_count_by_type[9];
198
@@ -205,6 +215,7 @@ static unsigned char pack_sha1[20];
215 static unsigned int object_entry_alloc = 1000;
216 static struct object_entry_pool *blocks;
217 static struct object_entry *object_table[1 << 16];
218 +static struct mark_set *marks;
219
220 /* Our last blob */
221 static struct last_object last_blob;
@@ -224,7 +235,7 @@ static struct branch *active_branches;
235
236 /* Input stream parsing */
237 static struct strbuf command_buf;
227 -static unsigned long command_mark;
238 +static unsigned long next_mark;
239
240
241 static void alloc_objects(int cnt)
@@ -335,6 +346,48 @@ static char* pool_strdup(const char *s)
346 return r;
347 }
348
349 +static void insert_mark(unsigned long idnum, struct object_entry *oe)
350 +{
351 + struct mark_set *s = marks;
352 + while ((idnum >> s->shift) >= 1024) {
353 + s = pool_calloc(1, sizeof(struct mark_set));
354 + s->shift = marks->shift + 10;
355 + s->data.sets[0] = marks;
356 + marks = s;
357 + }
358 + while (s->shift) {
359 + unsigned long i = idnum >> s->shift;
360 + idnum -= i << s->shift;
361 + if (!s->data.sets[i]) {
362 + s->data.sets[i] = pool_calloc(1, sizeof(struct mark_set));
363 + s->data.sets[i]->shift = s->shift - 10;
364 + }
365 + s = s->data.sets[i];
366 + }
367 + if (!s->data.marked[idnum])
368 + marks_set_count++;
369 + s->data.marked[idnum] = oe;
370 +}
371 +
372 +static struct object_entry* find_mark(unsigned long idnum)
373 +{
374 + unsigned long orig_idnum = idnum;
375 + struct mark_set *s = marks;
376 + struct object_entry *oe = NULL;
377 + if ((idnum >> s->shift) < 1024) {
378 + while (s && s->shift) {
379 + unsigned long i = idnum >> s->shift;
380 + idnum -= i << s->shift;
381 + s = s->data.sets[i];
382 + }
383 + if (s)
384 + oe = s->data.marked[idnum];
385 + }
386 + if (!oe)
387 + die("mark :%lu not declared", orig_idnum);
388 + return oe;
389 +}
390 +
391 static struct atom_str* to_atom(const char *s, size_t len)
392 {
393 unsigned int hc = hc_str(s, len) % atom_table_sz;
@@ -523,7 +576,8 @@ static int store_object(
576 void *dat,
577 size_t datlen,
578 struct last_object *last,
526 - unsigned char *sha1out)
579 + unsigned char *sha1out,
580 + unsigned long mark)
581 {
582 void *out, *delta;
583 struct object_entry *e;
@@ -542,6 +596,8 @@ static int store_object(
596 memcpy(sha1out, sha1, sizeof(sha1));
597
598 e = insert_object(sha1);
599 + if (mark)
600 + insert_mark(mark, e);
601 if (e->offset) {
602 duplicate_count++;
603 duplicate_count_by_type[type]++;
@@ -695,7 +751,7 @@ static void store_tree(struct tree_entry *root)
751 memcpy(c, e->sha1, 20);
752 c += 20;
753 }
698 - store_object(OBJ_TREE, buf, c - buf, NULL, root->sha1);
754 + store_object(OBJ_TREE, buf, c - buf, NULL, root->sha1, 0);
755 free(buf);
756 }
757
@@ -921,11 +977,11 @@ static void read_next_command()
977 static void cmd_mark()
978 {
979 if (!strncmp("mark :", command_buf.buf, 6)) {
924 - command_mark = strtoul(command_buf.buf + 6, NULL, 10);
980 + next_mark = strtoul(command_buf.buf + 6, NULL, 10);
981 read_next_command();
982 }
983 else
928 - command_mark = 0;
984 + next_mark = 0;
985 }
986
987 static void* cmd_data (size_t *size)
@@ -956,16 +1012,15 @@ static void* cmd_data (size_t *size)
1012
1013 static void cmd_new_blob()
1014 {
959 - size_t datlen;
960 - void *dat;
961 - unsigned char sha1[20];
1015 + size_t l;
1016 + void *d;
1017
1018 read_next_command();
1019 cmd_mark();
965 - dat = cmd_data(&datlen);
1020 + d = cmd_data(&l);
1021
967 - if (store_object(OBJ_BLOB, dat, datlen, &last_blob, sha1))
968 - free(dat);
1022 + if (store_object(OBJ_BLOB, d, l, &last_blob, NULL, next_mark))
1023 + free(d);
1024 }
1025
1026 static void unload_one_branch()
@@ -1031,9 +1086,16 @@ static void file_change_m(struct branch *b)
1086 die("Corrupt mode: %s", command_buf.buf);
1087 }
1088
1034 - if (get_sha1_hex(p, sha1))
1035 - die("Invalid SHA1: %s", command_buf.buf);
1036 - p += 40;
1089 + if (*p == ':') {
1090 + char *x;
1091 + oe = find_mark(strtoul(p + 1, &x, 10));
1092 + p = x;
1093 + } else {
1094 + if (get_sha1_hex(p, sha1))
1095 + die("Invalid SHA1: %s", command_buf.buf);
1096 + oe = find_object(sha1);
1097 + p += 40;
1098 + }
1099 if (*p++ != ' ')
1100 die("Missing space after SHA1: %s", command_buf.buf);
1101
@@ -1044,7 +1106,6 @@ static void file_change_m(struct branch *b)
1106 p = p_uq;
1107 }
1108
1047 - oe = find_object(sha1);
1109 if (oe) {
1110 if (oe->type != OBJ_BLOB)
1111 die("Not a blob (actually a %s): %s",
@@ -1161,7 +1222,7 @@ static void cmd_new_commit()
1222 free(committer);
1223 free(msg);
1224
1164 - store_object(OBJ_COMMIT, body, sp - body, NULL, b->sha1);
1225 + store_object(OBJ_COMMIT, body, sp - body, NULL, b->sha1, next_mark);
1226 free(body);
1227 b->last_commit = object_count_by_type[OBJ_COMMIT];
1228 }
@@ -1205,6 +1266,13 @@ static void cmd_new_branch()
1266 else if (s) {
1267 memcpy(b->sha1, s->sha1, 20);
1268 memcpy(b->branch_tree.sha1, s->branch_tree.sha1, 20);
1269 + } else if (*from == ':') {
1270 + unsigned long idnum = strtoul(from + 1, NULL, 10);
1271 + struct object_entry *oe = find_mark(idnum);
1272 + if (oe->type != OBJ_COMMIT)
1273 + die("Mark :%lu not a commit", idnum);
1274 + memcpy(b->sha1, oe->sha1, 20);
1275 + memcpy(b->branch_tree.sha1, null_sha1, 20);
1276 } else if (!get_sha1(from, b->sha1)) {
1277 if (!memcmp(b->sha1, null_sha1, 20))
1278 memcpy(b->branch_tree.sha1, null_sha1, 20);
@@ -1285,6 +1353,7 @@ int main(int argc, const char **argv)
1353 atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
1354 branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
1355 avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
1356 + marks = pool_calloc(1, sizeof(struct mark_set));
1357
1358 for (;;) {
1359 read_next_command();
@@ -1314,7 +1383,8 @@ int main(int argc, const char **argv)
1383 fprintf(stderr, " commits: %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_COMMIT], duplicate_count_by_type[OBJ_COMMIT]);
1384 fprintf(stderr, " tags : %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_TAG], duplicate_count_by_type[OBJ_TAG]);
1385 fprintf(stderr, "Total branches: %10lu\n", branch_count);
1317 - fprintf(stderr, "Total atoms: %10u\n", atom_cnt);
1386 + fprintf(stderr, " atoms: %10u\n", atom_cnt);
1387 + fprintf(stderr, " marks: %10u (%10lu unique )\n", (1 << marks->shift) * 1024, marks_set_count);
1388 fprintf(stderr, "Memory total: %10lu KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024);
1389 fprintf(stderr, " pools: %10lu KiB\n", total_allocd/1024);
1390 fprintf(stderr, " objects: %10lu KiB\n", (alloc_count*sizeof(struct object_entry))/1024);