Skip to content

Commit fc180d9

Browse files
committed
Merge branch 'rr/fi-import-marks-if-exists'
* rr/fi-import-marks-if-exists: fast-import: Introduce --import-marks-if-exists
2 parents 5bb20ec + dded4f1 commit fc180d9

File tree

3 files changed

+72
-3
lines changed

3 files changed

+72
-3
lines changed

Documentation/git-fast-import.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ OPTIONS
7878
set of marks. If a mark is defined to different values,
7979
the last file wins.
8080

81+
--import-marks-if-exists=<file>::
82+
Like --import-marks but instead of erroring out, silently
83+
skips the file if it does not exist.
84+
8185
--relative-marks::
8286
After specifying --relative-marks= the paths specified
8387
with --import-marks= and --export-marks= are relative

fast-import.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ static struct mark_set *marks;
329329
static const char *export_marks_file;
330330
static const char *import_marks_file;
331331
static int import_marks_file_from_stream;
332+
static int import_marks_file_ignore_missing;
332333
static int relative_marks_paths;
333334

334335
/* Our last blob */
@@ -1795,7 +1796,11 @@ static void read_marks(void)
17951796
{
17961797
char line[512];
17971798
FILE *f = fopen(import_marks_file, "r");
1798-
if (!f)
1799+
if (f)
1800+
;
1801+
else if (import_marks_file_ignore_missing && errno == ENOENT)
1802+
return; /* Marks file does not exist */
1803+
else
17991804
die_errno("cannot read '%s'", import_marks_file);
18001805
while (fgets(line, sizeof(line), f)) {
18011806
uintmax_t mark;
@@ -2867,7 +2872,8 @@ static char* make_fast_import_path(const char *path)
28672872
return strbuf_detach(&abs_path, NULL);
28682873
}
28692874

2870-
static void option_import_marks(const char *marks, int from_stream)
2875+
static void option_import_marks(const char *marks,
2876+
int from_stream, int ignore_missing)
28712877
{
28722878
if (import_marks_file) {
28732879
if (from_stream)
@@ -2881,6 +2887,7 @@ static void option_import_marks(const char *marks, int from_stream)
28812887
import_marks_file = make_fast_import_path(marks);
28822888
safe_create_leading_directories_const(import_marks_file);
28832889
import_marks_file_from_stream = from_stream;
2890+
import_marks_file_ignore_missing = ignore_missing;
28842891
}
28852892

28862893
static void option_date_format(const char *fmt)
@@ -2980,7 +2987,10 @@ static int parse_one_feature(const char *feature, int from_stream)
29802987
if (!prefixcmp(feature, "date-format=")) {
29812988
option_date_format(feature + 12);
29822989
} else if (!prefixcmp(feature, "import-marks=")) {
2983-
option_import_marks(feature + 13, from_stream);
2990+
option_import_marks(feature + 13, from_stream, 0);
2991+
} else if (!prefixcmp(feature, "import-marks-if-exists=")) {
2992+
option_import_marks(feature + strlen("import-marks-if-exists="),
2993+
from_stream, 1);
29842994
} else if (!prefixcmp(feature, "export-marks=")) {
29852995
option_export_marks(feature + 13);
29862996
} else if (!strcmp(feature, "cat-blob")) {

t/t9300-fast-import.sh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,6 +1748,61 @@ test_expect_success \
17481748
'cat input | git fast-import --export-marks=other.marks &&
17491749
grep :1 other.marks'
17501750

1751+
test_expect_success 'R: catch typo in marks file name' '
1752+
test_must_fail git fast-import --import-marks=nonexistent.marks </dev/null &&
1753+
echo "feature import-marks=nonexistent.marks" |
1754+
test_must_fail git fast-import
1755+
'
1756+
1757+
test_expect_success 'R: import and output marks can be the same file' '
1758+
rm -f io.marks &&
1759+
blob=$(echo hi | git hash-object --stdin) &&
1760+
cat >expect <<-EOF &&
1761+
:1 $blob
1762+
:2 $blob
1763+
EOF
1764+
git fast-import --export-marks=io.marks <<-\EOF &&
1765+
blob
1766+
mark :1
1767+
data 3
1768+
hi
1769+
1770+
EOF
1771+
git fast-import --import-marks=io.marks --export-marks=io.marks <<-\EOF &&
1772+
blob
1773+
mark :2
1774+
data 3
1775+
hi
1776+
1777+
EOF
1778+
test_cmp expect io.marks
1779+
'
1780+
1781+
test_expect_success 'R: --import-marks=foo --output-marks=foo to create foo fails' '
1782+
rm -f io.marks &&
1783+
test_must_fail git fast-import --import-marks=io.marks --export-marks=io.marks <<-\EOF
1784+
blob
1785+
mark :1
1786+
data 3
1787+
hi
1788+
1789+
EOF
1790+
'
1791+
1792+
test_expect_success 'R: --import-marks-if-exists' '
1793+
rm -f io.marks &&
1794+
blob=$(echo hi | git hash-object --stdin) &&
1795+
echo ":1 $blob" >expect &&
1796+
git fast-import --import-marks-if-exists=io.marks --export-marks=io.marks <<-\EOF &&
1797+
blob
1798+
mark :1
1799+
data 3
1800+
hi
1801+
1802+
EOF
1803+
test_cmp expect io.marks
1804+
'
1805+
17511806
cat >input << EOF
17521807
feature import-marks=marks.out
17531808
feature export-marks=marks.new

0 commit comments

Comments
 (0)