fast-import: put marks reading in its own function
All options do nothing but set settings, with the exception of the --input-marks option. Delay the reading of the marks file till after all options have been parsed. Also, rename mark_file to export_marks_file as it is now ambiguous. 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
07cd9328b6256d3fdc05546e10c589144b5c63c5
1 file changed
+51
-42
fast-import.c
+51
-42
@@ -318,7 +318,8 @@ static unsigned int object_entry_alloc = 5000;
318
static struct object_entry_pool *blocks;
319
static struct object_entry *object_table[1 << 16];
320
static struct mark_set *marks;
321
-static const char *mark_file;
321
+static const char *export_marks_file;
322
+static const char *import_marks_file;
323
324
/* Our last blob */
325
static struct last_object last_blob = { STRBUF_INIT, 0, 0, 0 };
@@ -455,8 +456,8 @@ static void write_crash_report(const char *err)
456
fputc('\n', rpt);
457
fputs("Marks\n", rpt);
458
fputs("-----\n", rpt);
458
- if (mark_file)
459
- fprintf(rpt, " exported to %s\n", mark_file);
459
+ if (export_marks_file)
460
+ fprintf(rpt, " exported to %s\n", export_marks_file);
461
else
462
dump_marks_helper(rpt, 0, marks);
463
@@ -1603,13 +1604,13 @@ static void dump_marks(void)
1604
int mark_fd;
1605
FILE *f;
1606
1606
- if (!mark_file)
1607
+ if (!export_marks_file)
1608
return;
1609
1609
- mark_fd = hold_lock_file_for_update(&mark_lock, mark_file, 0);
1610
+ mark_fd = hold_lock_file_for_update(&mark_lock, export_marks_file, 0);
1611
if (mark_fd < 0) {
1612
failure |= error("Unable to write marks file %s: %s",
1612
- mark_file, strerror(errno));
1613
+ export_marks_file, strerror(errno));
1614
return;
1615
}
1616
@@ -1618,7 +1619,7 @@ static void dump_marks(void)
1619
int saved_errno = errno;
1620
rollback_lock_file(&mark_lock);
1621
failure |= error("Unable to write marks file %s: %s",
1621
- mark_file, strerror(saved_errno));
1622
+ export_marks_file, strerror(saved_errno));
1623
return;
1624
}
1625
@@ -1634,7 +1635,7 @@ static void dump_marks(void)
1635
int saved_errno = errno;
1636
rollback_lock_file(&mark_lock);
1637
failure |= error("Unable to write marks file %s: %s",
1637
- mark_file, strerror(saved_errno));
1638
+ export_marks_file, strerror(saved_errno));
1639
return;
1640
}
1641
@@ -1642,11 +1643,47 @@ static void dump_marks(void)
1643
int saved_errno = errno;
1644
rollback_lock_file(&mark_lock);
1645
failure |= error("Unable to commit marks file %s: %s",
1645
- mark_file, strerror(saved_errno));
1646
+ export_marks_file, strerror(saved_errno));
1647
return;
1648
}
1649
}
1650
1651
+static void read_marks(void)
1652
+{
1653
+ char line[512];
1654
+ FILE *f = fopen(import_marks_file, "r");
1655
+ if (!f)
1656
+ die_errno("cannot read '%s'", import_marks_file);
1657
+ while (fgets(line, sizeof(line), f)) {
1658
+ uintmax_t mark;
1659
+ char *end;
1660
+ unsigned char sha1[20];
1661
+ struct object_entry *e;
1662
+
1663
+ end = strchr(line, '\n');
1664
+ if (line[0] != ':' || !end)
1665
+ die("corrupt mark line: %s", line);
1666
+ *end = 0;
1667
+ mark = strtoumax(line + 1, &end, 10);
1668
+ if (!mark || end == line + 1
1669
+ || *end != ' ' || get_sha1(end + 1, sha1))
1670
+ die("corrupt mark line: %s", line);
1671
+ e = find_object(sha1);
1672
+ if (!e) {
1673
+ enum object_type type = sha1_object_info(sha1, NULL);
1674
+ if (type < 0)
1675
+ die("object not found: %s", sha1_to_hex(sha1));
1676
+ e = insert_object(sha1);
1677
+ e->type = type;
1678
+ e->pack_id = MAX_PACK_ID;
1679
+ e->offset = 1; /* just not zero! */
1680
+ }
1681
+ insert_mark(mark, e);
1682
+ }
1683
+ fclose(f);
1684
+}
1685
+
1686
+
1687
static int read_next_command(void)
1688
{
1689
static int stdin_eof = 0;
@@ -2421,39 +2458,9 @@ static void parse_progress(void)
2458
skip_optional_lf();
2459
}
2460
2424
-static void option_import_marks(const char *input_file)
2461
+static void option_import_marks(const char *marks)
2462
{
2426
- char line[512];
2427
- FILE *f = fopen(input_file, "r");
2428
- if (!f)
2429
- die_errno("cannot read '%s'", input_file);
2430
- while (fgets(line, sizeof(line), f)) {
2431
- uintmax_t mark;
2432
- char *end;
2433
- unsigned char sha1[20];
2434
- struct object_entry *e;
2435
-
2436
- end = strchr(line, '\n');
2437
- if (line[0] != ':' || !end)
2438
- die("corrupt mark line: %s", line);
2439
- *end = 0;
2440
- mark = strtoumax(line + 1, &end, 10);
2441
- if (!mark || end == line + 1
2442
- || *end != ' ' || get_sha1(end + 1, sha1))
2443
- die("corrupt mark line: %s", line);
2444
- e = find_object(sha1);
2445
- if (!e) {
2446
- enum object_type type = sha1_object_info(sha1, NULL);
2447
- if (type < 0)
2448
- die("object not found: %s", sha1_to_hex(sha1));
2449
- e = insert_object(sha1);
2450
- e->type = type;
2451
- e->pack_id = MAX_PACK_ID;
2452
- e->offset = 1; /* just not zero! */
2453
- }
2454
- insert_mark(mark, e);
2455
- }
2456
- fclose(f);
2463
+ import_marks_file = xstrdup(marks);
2464
}
2465
2466
static void option_date_format(const char *fmt)
@@ -2487,7 +2494,7 @@ static void option_active_branches(const char *branches)
2494
2495
static void option_export_marks(const char *marks)
2496
{
2490
- mark_file = xstrdup(marks);
2497
+ export_marks_file = xstrdup(marks);
2498
}
2499
2500
static void option_export_pack_edges(const char *edges)
@@ -2581,6 +2588,8 @@ int main(int argc, const char **argv)
2588
}
2589
if (i != argc)
2590
usage(fast_import_usage);
2591
+ if (import_marks_file)
2592
+ read_marks();
2593
2594
rc_free = pool_alloc(cmd_save * sizeof(*rc_free));
2595
for (i = 0; i < (cmd_save - 1); i++)