fast-import: do not truncate exported marks file

Certain lines of the marks file might be corrupted (or the objects missing due to a garbage collection), but that's no reason to truncate the file and essentially destroy the rest of it. Ideally missing objects should not cause a crash, we could just skip them, but that's another patch. Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Felipe Contreras committed May 17, 2016 at 16:40 UTC f4beed60d5ef3fdbd31ac5bd3162182fdf2bf0d3
2 files changed +20 -2
fast-import.c
+5 -2
@@ -329,6 +329,7 @@ static const char *export_marks_file;
329 static const char *import_marks_file;
330 static int import_marks_file_from_stream;
331 static int import_marks_file_ignore_missing;
332 +static int import_marks_file_done;
333 static int relative_marks_paths;
334
335 /* Our last blob */
@@ -1802,7 +1803,7 @@ static void dump_marks(void)
1803 static struct lock_file mark_lock;
1804 FILE *f;
1805
1805 - if (!export_marks_file)
1806 + if (!export_marks_file || (import_marks_file && !import_marks_file_done))
1807 return;
1808
1809 if (hold_lock_file_for_update(&mark_lock, export_marks_file, 0) < 0) {
@@ -1835,7 +1836,7 @@ static void read_marks(void)
1836 if (f)
1837 ;
1838 else if (import_marks_file_ignore_missing && errno == ENOENT)
1838 - return; /* Marks file does not exist */
1839 + goto done; /* Marks file does not exist */
1840 else
1841 die_errno("cannot read '%s'", import_marks_file);
1842 while (fgets(line, sizeof(line), f)) {
@@ -1865,6 +1866,8 @@ static void read_marks(void)
1866 insert_mark(mark, e);
1867 }
1868 fclose(f);
1869 +done:
1870 + import_marks_file_done = 1;
1871 }
1872
1873
t/t9300-fast-import.sh
+15
@@ -2650,6 +2650,21 @@ test_expect_success 'R: ignore non-git options' '
2650 git fast-import <input
2651 '
2652
2653 +test_expect_success 'R: corrupt lines do not mess marks file' '
2654 + rm -f io.marks &&
2655 + blob=$(echo hi | git hash-object --stdin) &&
2656 + cat >expect <<-EOF &&
2657 + :3 0000000000000000000000000000000000000000
2658 + :1 $blob
2659 + :2 $blob
2660 + EOF
2661 + cp expect io.marks &&
2662 + test_must_fail git fast-import --import-marks=io.marks --export-marks=io.marks <<-\EOF &&
2663 +
2664 + EOF
2665 + test_cmp expect io.marks
2666 +'
2667 +
2668 ##
2669 ## R: very large blobs
2670 ##