fast-export: add support for --import-marks-if-exists
fast-import has support for both an --import-marks flag and an --import-marks-if-exists flag; the latter of which will not die() if the file does not exist. fast-export only had support for an --import-marks flag; add an --import-marks-if-exists flag for consistency. Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Elijah Newren committed
Oct 3, 2019 at 13:27 UTC
208d69246ed2a7f66dd8357771eb7afe83e80a43
2 files changed
+23
-10
builtin/fast-export.c
+19
-4
@@ -1052,11 +1052,16 @@ static void export_marks(char *file)
1052
error("Unable to write marks file %s.", file);
1053
}
1054
1055
-static void import_marks(char *input_file)
1055
+static void import_marks(char *input_file, int check_exists)
1056
{
1057
char line[512];
1058
- FILE *f = xfopen(input_file, "r");
1058
+ FILE *f;
1059
+ struct stat sb;
1060
+
1061
+ if (check_exists && stat(input_file, &sb))
1062
+ return;
1063
1064
+ f = xfopen(input_file, "r");
1065
while (fgets(line, sizeof(line), f)) {
1066
uint32_t mark;
1067
char *line_end, *mark_end;
@@ -1120,7 +1125,9 @@ int cmd_fast_export(int argc, const char **argv, const char *prefix)
1125
struct rev_info revs;
1126
struct object_array commits = OBJECT_ARRAY_INIT;
1127
struct commit *commit;
1123
- char *export_filename = NULL, *import_filename = NULL;
1128
+ char *export_filename = NULL,
1129
+ *import_filename = NULL,
1130
+ *import_filename_if_exists = NULL;
1131
uint32_t lastimportid;
1132
struct string_list refspecs_list = STRING_LIST_INIT_NODUP;
1133
struct string_list paths_of_changed_objects = STRING_LIST_INIT_DUP;
@@ -1140,6 +1147,10 @@ int cmd_fast_export(int argc, const char **argv, const char *prefix)
1147
N_("Dump marks to this file")),
1148
OPT_STRING(0, "import-marks", &import_filename, N_("file"),
1149
N_("Import marks from this file")),
1150
+ OPT_STRING(0, "import-marks-if-exists",
1151
+ &import_filename_if_exists,
1152
+ N_("file"),
1153
+ N_("Import marks from this file if it exists")),
1154
OPT_BOOL(0, "fake-missing-tagger", &fake_missing_tagger,
1155
N_("Fake a tagger when tags lack one")),
1156
OPT_BOOL(0, "full-tree", &full_tree,
@@ -1187,8 +1198,12 @@ int cmd_fast_export(int argc, const char **argv, const char *prefix)
1198
if (use_done_feature)
1199
printf("feature done\n");
1200
1201
+ if (import_filename && import_filename_if_exists)
1202
+ die(_("Cannot pass both --import-marks and --import-marks-if-exists"));
1203
if (import_filename)
1191
- import_marks(import_filename);
1204
+ import_marks(import_filename, 0);
1205
+ else if (import_filename_if_exists)
1206
+ import_marks(import_filename_if_exists, 1);
1207
lastimportid = last_idnum;
1208
1209
if (import_filename && revs.prune_data.nr)
t/t9350-fast-export.sh
+4
-6
@@ -580,17 +580,15 @@ test_expect_success 'fast-export quotes pathnames' '
580
'
581
582
test_expect_success 'test bidirectionality' '
583
- >marks-cur &&
584
- >marks-new &&
583
git init marks-test &&
586
- git fast-export --export-marks=marks-cur --import-marks=marks-cur --branches | \
587
- git --git-dir=marks-test/.git fast-import --export-marks=marks-new --import-marks=marks-new &&
584
+ git fast-export --export-marks=marks-cur --import-marks-if-exists=marks-cur --branches | \
585
+ git --git-dir=marks-test/.git fast-import --export-marks=marks-new --import-marks-if-exists=marks-new &&
586
(cd marks-test &&
587
git reset --hard &&
588
echo Wohlauf > file &&
589
git commit -a -m "back in time") &&
592
- git --git-dir=marks-test/.git fast-export --export-marks=marks-new --import-marks=marks-new --branches | \
593
- git fast-import --export-marks=marks-cur --import-marks=marks-cur
590
+ git --git-dir=marks-test/.git fast-export --export-marks=marks-new --import-marks-if-exists=marks-new --branches | \
591
+ git fast-import --export-marks=marks-cur --import-marks-if-exists=marks-cur
592
'
593
594
cat > expected << EOF