fast-import: put option parsing code in separate functions
Putting the options in their own functions increases readability of the option parsing block and makes it easier to reuse the option parsing code later on. Signed-off-by: Sverre Rabbelier <srabbelier@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Sverre Rabbelier committed
Dec 4, 2009 at 18:06 UTC
0f6927c2296d6f92f3f233fe9e073c30ca66e0ce
1 file changed
+75
-40
fast-import.c
+75
-40
@@ -295,6 +295,7 @@ static unsigned long branch_count;
295
static unsigned long branch_load_count;
296
static int failure;
297
static FILE *pack_edges;
298
+static unsigned int show_stats = 1;
299
300
/* Memory pools */
301
static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool);
@@ -2420,7 +2421,7 @@ static void parse_progress(void)
2421
skip_optional_lf();
2422
}
2423
2423
-static void import_marks(const char *input_file)
2424
+static void option_import_marks(const char *input_file)
2425
{
2426
char line[512];
2427
FILE *f = fopen(input_file, "r");
@@ -2455,6 +2456,76 @@ static void import_marks(const char *input_file)
2456
fclose(f);
2457
}
2458
2459
+static void option_date_format(const char *fmt)
2460
+{
2461
+ if (!strcmp(fmt, "raw"))
2462
+ whenspec = WHENSPEC_RAW;
2463
+ else if (!strcmp(fmt, "rfc2822"))
2464
+ whenspec = WHENSPEC_RFC2822;
2465
+ else if (!strcmp(fmt, "now"))
2466
+ whenspec = WHENSPEC_NOW;
2467
+ else
2468
+ die("unknown --date-format argument %s", fmt);
2469
+}
2470
+
2471
+static void option_max_pack_size(const char *packsize)
2472
+{
2473
+ max_packsize = strtoumax(packsize, NULL, 0) * 1024 * 1024;
2474
+}
2475
+
2476
+static void option_depth(const char *depth)
2477
+{
2478
+ max_depth = strtoul(depth, NULL, 0);
2479
+ if (max_depth > MAX_DEPTH)
2480
+ die("--depth cannot exceed %u", MAX_DEPTH);
2481
+}
2482
+
2483
+static void option_active_branches(const char *branches)
2484
+{
2485
+ max_active_branches = strtoul(branches, NULL, 0);
2486
+}
2487
+
2488
+static void option_export_marks(const char *marks)
2489
+{
2490
+ mark_file = xstrdup(marks);
2491
+}
2492
+
2493
+static void option_export_pack_edges(const char *edges)
2494
+{
2495
+ if (pack_edges)
2496
+ fclose(pack_edges);
2497
+ pack_edges = fopen(edges, "a");
2498
+ if (!pack_edges)
2499
+ die_errno("Cannot open '%s'", edges);
2500
+}
2501
+
2502
+static void parse_one_option(const char *option)
2503
+{
2504
+ if (!prefixcmp(option, "date-format=")) {
2505
+ option_date_format(option + 12);
2506
+ } else if (!prefixcmp(option, "max-pack-size=")) {
2507
+ option_max_pack_size(option + 14);
2508
+ } else if (!prefixcmp(option, "depth=")) {
2509
+ option_depth(option + 6);
2510
+ } else if (!prefixcmp(option, "active-branches=")) {
2511
+ option_active_branches(option + 16);
2512
+ } else if (!prefixcmp(option, "import-marks=")) {
2513
+ option_import_marks(option + 13);
2514
+ } else if (!prefixcmp(option, "export-marks=")) {
2515
+ option_export_marks(option + 13);
2516
+ } else if (!prefixcmp(option, "export-pack-edges=")) {
2517
+ option_export_pack_edges(option + 18);
2518
+ } else if (!prefixcmp(option, "force")) {
2519
+ force_update = 1;
2520
+ } else if (!prefixcmp(option, "quiet")) {
2521
+ show_stats = 0;
2522
+ } else if (!prefixcmp(option, "stats")) {
2523
+ show_stats = 1;
2524
+ } else {
2525
+ die("Unsupported option: %s", option);
2526
+ }
2527
+}
2528
+
2529
static int git_pack_config(const char *k, const char *v, void *cb)
2530
{
2531
if (!strcmp(k, "pack.depth")) {
@@ -2481,7 +2552,7 @@ static const char fast_import_usage[] =
2552
2553
int main(int argc, const char **argv)
2554
{
2484
- unsigned int i, show_stats = 1;
2555
+ unsigned int i;
2556
2557
git_extract_argv0_path(argv[0]);
2558
@@ -2505,44 +2576,8 @@ int main(int argc, const char **argv)
2576
2577
if (*a != '-' || !strcmp(a, "--"))
2578
break;
2508
- else if (!prefixcmp(a, "--date-format=")) {
2509
- const char *fmt = a + 14;
2510
- if (!strcmp(fmt, "raw"))
2511
- whenspec = WHENSPEC_RAW;
2512
- else if (!strcmp(fmt, "rfc2822"))
2513
- whenspec = WHENSPEC_RFC2822;
2514
- else if (!strcmp(fmt, "now"))
2515
- whenspec = WHENSPEC_NOW;
2516
- else
2517
- die("unknown --date-format argument %s", fmt);
2518
- }
2519
- else if (!prefixcmp(a, "--max-pack-size="))
2520
- max_packsize = strtoumax(a + 16, NULL, 0) * 1024 * 1024;
2521
- else if (!prefixcmp(a, "--depth=")) {
2522
- max_depth = strtoul(a + 8, NULL, 0);
2523
- if (max_depth > MAX_DEPTH)
2524
- die("--depth cannot exceed %u", MAX_DEPTH);
2525
- }
2526
- else if (!prefixcmp(a, "--active-branches="))
2527
- max_active_branches = strtoul(a + 18, NULL, 0);
2528
- else if (!prefixcmp(a, "--import-marks="))
2529
- import_marks(a + 15);
2530
- else if (!prefixcmp(a, "--export-marks="))
2531
- mark_file = a + 15;
2532
- else if (!prefixcmp(a, "--export-pack-edges=")) {
2533
- if (pack_edges)
2534
- fclose(pack_edges);
2535
- pack_edges = fopen(a + 20, "a");
2536
- if (!pack_edges)
2537
- die_errno("Cannot open '%s'", a + 20);
2538
- } else if (!strcmp(a, "--force"))
2539
- force_update = 1;
2540
- else if (!strcmp(a, "--quiet"))
2541
- show_stats = 0;
2542
- else if (!strcmp(a, "--stats"))
2543
- show_stats = 1;
2544
- else
2545
- die("unknown option %s", a);
2579
+
2580
+ parse_one_option(a + 2);
2581
}
2582
if (i != argc)
2583
usage(fast_import_usage);