@matheusbsbs / matheus-bagliano / commits / 6bb5b3291d
Fork 0

Implemented branch handling and basic tree support in fast-import.

This provides the basic data structures needed to store trees in memory while we are processing them for a branch. What we are attempting to do is track one complete tree for each branch that the frontend has registered with us through the 'newb' (new_branch) command. When the frontend edits that tree through 'updf' or 'delf' commands we'll mark the affected tree(s) as being dirty and recompute their objects during 'comt' (commit). Currently the protocol is decidedly _not_ user friendly. I crashed fast-import by giving it bad input data from Perl. I may try to improve upon it, or at least upon its error handling. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed Aug 8, 2006 at 03:36 UTC 6bb5b3291df99bf050c91ab742b406d2404b8f73
1 file changed +165 -5
fast-import.c
+165 -5
@@ -24,14 +24,39 @@ struct object_entry_block
24 struct last_object
25 {
26 void *data;
27 - unsigned long len;
28 - int depth;
27 + unsigned int len;
28 + unsigned int depth;
29 unsigned char sha1[20];
30 };
31
32 +struct tree;
33 +struct tree_entry
34 +{
35 + struct tree *tree;
36 + mode_t mode;
37 + unsigned char sha1[20];
38 + char name[FLEX_ARRAY]; /* more */
39 +};
40 +
41 +struct tree
42 +{
43 + struct last_object last_tree;
44 + unsigned long entry_count;
45 + struct tree_entry **entries;
46 +};
47 +
48 +struct branch
49 +{
50 + struct branch *next_branch;
51 + struct tree_entry tree;
52 + unsigned char sha1[20];
53 + const char *name;
54 +};
55 +
56 /* Stats and misc. counters. */
57 static int max_depth = 10;
58 static unsigned long alloc_count;
59 +static unsigned long branch_count;
60 static unsigned long object_count;
61 static unsigned long duplicate_count;
62 static unsigned long object_count_by_type[9];
@@ -49,6 +74,10 @@ struct object_entry *object_table[1 << 16];
74 /* Our last blob */
75 struct last_object last_blob;
76
77 +/* Branch data */
78 +struct branch *branches;
79 +struct branch *current_branch;
80 +
81 static void alloc_objects(int cnt)
82 {
83 struct object_entry_block *b;
@@ -129,6 +158,32 @@ static ssize_t ywrite(int fd, void *buffer, size_t length)
158 return ret;
159 }
160
161 +static const char* read_string()
162 +{
163 + static char sn[PATH_MAX];
164 + unsigned long slen;
165 +
166 + if (yread(0, &slen, 4) != 4)
167 + die("Can't obtain string");
168 + if (!slen)
169 + return 0;
170 + if (slen > (PATH_MAX - 1))
171 + die("Can't handle excessive string length %lu", slen);
172 +
173 + if (yread(0, sn, slen) != slen)
174 + die("Can't obtain string of length %lu", slen);
175 + sn[slen] = 0;
176 + return sn;
177 +}
178 +
179 +static const char* read_required_string()
180 +{
181 + const char *r = read_string();
182 + if (!r)
183 + die("Expected string command parameter, didn't find one");
184 + return r;
185 +}
186 +
187 static unsigned long encode_header(
188 enum object_type type,
189 unsigned long size,
@@ -156,7 +211,8 @@ static int store_object(
211 enum object_type type,
212 void *dat,
213 unsigned long datlen,
159 - struct last_object *last)
214 + struct last_object *last,
215 + unsigned char *sha1out)
216 {
217 void *out, *delta;
218 struct object_entry *e;
@@ -171,6 +227,8 @@ static int store_object(
227 SHA1_Update(&c, hdr, hdrlen);
228 SHA1_Update(&c, dat, datlen);
229 SHA1_Final(sha1, &c);
230 + if (sha1out)
231 + memcpy(sha1out, sha1, sizeof(sha1));
232
233 e = insert_object(sha1);
234 if (e->offset) {
@@ -347,10 +405,108 @@ static void new_blob()
405 if (yread(0, dat, datlen) != datlen)
406 die("Con't obtain %lu bytes of blob data", datlen);
407
350 - if (!store_object(OBJ_BLOB, dat, datlen, &last_blob))
408 + if (!store_object(OBJ_BLOB, dat, datlen, &last_blob, 0))
409 free(dat);
410 }
411
412 +static struct branch* lookup_branch(const char *name)
413 +{
414 + struct branch *b;
415 + for (b = branches; b; b = b->next_branch) {
416 + if (!strcmp(name, b->name))
417 + return b;
418 + }
419 + die("No branch named '%s' has been declared", name);
420 +}
421 +
422 +static struct tree* deep_copy_tree (struct tree *t)
423 +{
424 + struct tree *r = xmalloc(sizeof(struct tree));
425 + unsigned long i;
426 +
427 + if (t->last_tree.data) {
428 + r->last_tree.data = xmalloc(t->last_tree.len);
429 + r->last_tree.len = t->last_tree.len;
430 + r->last_tree.depth = t->last_tree.depth;
431 + memcpy(r->last_tree.data, t->last_tree.data, t->last_tree.len);
432 + memcpy(r->last_tree.sha1, t->last_tree.sha1, sizeof(t->last_tree.sha1));
433 + }
434 +
435 + r->entry_count = t->entry_count;
436 + r->entries = xmalloc(t->entry_count * sizeof(struct tree_entry*));
437 + for (i = 0; i < t->entry_count; i++) {
438 + struct tree_entry *a = t->entries[i];
439 + struct tree_entry *b;
440 +
441 + b = xmalloc(sizeof(struct tree_entry) + strlen(a->name) + 1);
442 + b->tree = a->tree ? deep_copy_tree(a->tree) : 0;
443 + b->mode = a->mode;
444 + memcpy(b->sha1, a->sha1, sizeof(a->sha1));
445 + strcpy(b->name, a->name);
446 + r->entries[i] = b;
447 + }
448 +
449 + return r;
450 +}
451 +
452 +static void store_tree (struct tree_entry *e)
453 +{
454 + struct tree *t = e->tree;
455 + unsigned long maxlen, i;
456 + char *buf, *c;
457 +
458 + if (memcmp(null_sha1, e->sha1, sizeof(e->sha1)))
459 + return;
460 +
461 + maxlen = t->entry_count * 32;
462 + for (i = 0; i < t->entry_count; i++)
463 + maxlen += strlen(t->entries[i]->name);
464 +
465 + buf = c = xmalloc(maxlen);
466 + for (i = 0; i < t->entry_count; i++) {
467 + struct tree_entry *e = t->entries[i];
468 + c += sprintf(c, "%o %s", e->mode, e->name) + 1;
469 + if (e->tree)
470 + store_tree(e);
471 + memcpy(c, e->sha1, sizeof(e->sha1));
472 + c += sizeof(e->sha1);
473 + }
474 +
475 + if (!store_object(OBJ_TREE, buf, c - buf, &t->last_tree, e->sha1))
476 + free(buf);
477 +}
478 +
479 +static void new_branch()
480 +{
481 + struct branch *nb = xcalloc(1, sizeof(struct branch));
482 + const char *source_name;
483 +
484 + nb->name = strdup(read_required_string());
485 + source_name = read_string();
486 + if (source_name) {
487 + struct branch *sb = lookup_branch(source_name);
488 + nb->tree.tree = deep_copy_tree(sb->tree.tree);
489 + memcpy(nb->tree.sha1, sb->tree.sha1, sizeof(sb->tree.sha1));
490 + memcpy(nb->sha1, sb->sha1, sizeof(sb->sha1));
491 + } else {
492 + nb->tree.tree = xcalloc(1, sizeof(struct tree));
493 + nb->tree.tree->entries = xmalloc(8*sizeof(struct tree_entry*));
494 + }
495 + nb->next_branch = branches;
496 + branches = nb;
497 + branch_count++;
498 +}
499 +
500 +static void set_branch()
501 +{
502 + current_branch = lookup_branch(read_required_string());
503 +}
504 +
505 +static void commit()
506 +{
507 + store_tree(&current_branch->tree);
508 +}
509 +
510 int main(int argc, const char **argv)
511 {
512 const char *base_name = argv[1];
@@ -376,7 +532,10 @@ int main(int argc, const char **argv)
532 break;
533
534 switch (cmd) {
379 - case 'blob': new_blob(); break;
535 + case 'blob': new_blob(); break;
536 + case 'newb': new_branch(); break;
537 + case 'setb': set_branch(); break;
538 + case 'comt': commit(); break;
539 default:
540 die("Invalid command %lu", cmd);
541 }
@@ -393,6 +552,7 @@ int main(int argc, const char **argv)
552 fprintf(stderr, " trees : %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_TREE], duplicate_count_by_type[OBJ_TREE]);
553 fprintf(stderr, " commits: %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_COMMIT], duplicate_count_by_type[OBJ_COMMIT]);
554 fprintf(stderr, " tags : %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_TAG], duplicate_count_by_type[OBJ_TAG]);
555 + fprintf(stderr, "Total branches: %10lu\n", branch_count);
556 fprintf(stderr, "---------------------------------------------------\n");
557
558 stat(pack_name, &sb);