@matheusbsbs / matheus-bagliano / commits / 6143f0644e
Fork 0

Added basic command handler to fast-import.

Moved the new_blob logic off into a new subroutine and invoked it when getting the 'blob' command. Added statistics dump to STDERR when the program terminates listing what it did at a high level. This is somewhat interesting. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed Aug 8, 2006 at 01:14 UTC 6143f0644e79686407c1dc0e1b4dadff74e80046
1 file changed +46 -14
fast-import.c
+46 -14
@@ -34,6 +34,8 @@ static int max_depth = 10;
34 static unsigned long alloc_count;
35 static unsigned long object_count;
36 static unsigned long duplicate_count;
37 +static unsigned long object_count_by_type[9];
38 +static unsigned long duplicate_count_by_type[9];
39
40 /* The .pack file */
41 static int pack_fd;
@@ -173,10 +175,12 @@ static int store_object(
175 e = insert_object(sha1);
176 if (e->offset) {
177 duplicate_count++;
178 + duplicate_count_by_type[type]++;
179 return 0;
180 }
181 e->offset = pack_offset;
182 object_count++;
183 + object_count_by_type[type]++;
184
185 if (last->data && last->depth < max_depth)
186 delta = diff_delta(last->data, last->len,
@@ -232,7 +236,7 @@ static int store_object(
236 static void init_pack_header()
237 {
238 const char* magic = "PACK";
235 - unsigned long version = 2;
239 + unsigned long version = 3;
240 unsigned long zero = 0;
241
242 version = htonl(version);
@@ -331,12 +335,29 @@ static void write_index(const char *idx_name)
335 free(idx);
336 }
337
338 +static void new_blob()
339 +{
340 + unsigned long datlen;
341 + void *dat;
342 +
343 + if (yread(0, &datlen, 4) != 4)
344 + die("Can't obtain blob length");
345 +
346 + dat = xmalloc(datlen);
347 + if (yread(0, dat, datlen) != datlen)
348 + die("Con't obtain %lu bytes of blob data", datlen);
349 +
350 + if (!store_object(OBJ_BLOB, dat, datlen, &last_blob))
351 + free(dat);
352 +}
353 +
354 int main(int argc, const char **argv)
355 {
356 const char *base_name = argv[1];
357 int est_obj_cnt = atoi(argv[2]);
358 char *pack_name;
359 char *idx_name;
360 + struct stat sb;
361
362 pack_name = xmalloc(strlen(base_name) + 6);
363 sprintf(pack_name, "%s.pack", base_name);
@@ -345,30 +366,41 @@ int main(int argc, const char **argv)
366
367 pack_fd = open(pack_name, O_RDWR|O_CREAT|O_EXCL, 0666);
368 if (pack_fd < 0)
348 - die("Can't create pack file %s: %s", pack_name, strerror(errno));
369 + die("Can't create %s: %s", pack_name, strerror(errno));
370
371 alloc_objects(est_obj_cnt);
372 init_pack_header();
373 for (;;) {
353 - unsigned long datlen;
354 - void *dat;
355 -
356 - if (yread(0, &datlen, 4) != 4)
374 + unsigned long cmd;
375 + if (yread(0, &cmd, 4) != 4)
376 break;
377
359 - dat = xmalloc(datlen);
360 - if (yread(0, dat, datlen) != datlen)
361 - break;
362 -
363 - if (!store_object(OBJ_BLOB, dat, datlen, &last_blob))
364 - free(dat);
378 + switch (cmd) {
379 + case 'blob': new_blob(); break;
380 + default:
381 + die("Invalid command %lu", cmd);
382 + }
383 }
384 fixup_header_footer();
385 close(pack_fd);
386 write_index(idx_name);
387
370 - fprintf(stderr, "%lu objects, %lu duplicates, %lu allocated (%lu overflow)\n",
371 - object_count, duplicate_count, alloc_count, alloc_count - est_obj_cnt);
388 + fprintf(stderr, "%s statistics:\n", argv[0]);
389 + fprintf(stderr, "---------------------------------------------------\n");
390 + fprintf(stderr, "Alloc'd objects: %10lu (%10lu overflow )\n", alloc_count, alloc_count - est_obj_cnt);
391 + fprintf(stderr, "Total objects: %10lu (%10lu duplicates)\n", object_count, duplicate_count);
392 + fprintf(stderr, " blobs : %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_BLOB], duplicate_count_by_type[OBJ_BLOB]);
393 + fprintf(stderr, " trees : %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_TREE], duplicate_count_by_type[OBJ_TREE]);
394 + fprintf(stderr, " commits: %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_COMMIT], duplicate_count_by_type[OBJ_COMMIT]);
395 + fprintf(stderr, " tags : %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_TAG], duplicate_count_by_type[OBJ_TAG]);
396 + fprintf(stderr, "---------------------------------------------------\n");
397 +
398 + stat(pack_name, &sb);
399 + fprintf(stderr, "Pack size: %10lu KiB\n", (unsigned long)(sb.st_size/1024));
400 + stat(idx_name, &sb);
401 + fprintf(stderr, "Index size: %10lu KiB\n", (unsigned long)(sb.st_size/1024));
402 +
403 + fprintf(stderr, "\n");
404
405 return 0;
406 }