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>
This commit is contained in:
Elijah Newren 2019-10-03 13:27:06 -07:00 committed by Junio C Hamano
parent b8f50e5b60
commit 208d69246e
2 changed files with 23 additions and 10 deletions

View file

@ -1052,11 +1052,16 @@ static void export_marks(char *file)
error("Unable to write marks file %s.", file); error("Unable to write marks file %s.", file);
} }
static void import_marks(char *input_file) static void import_marks(char *input_file, int check_exists)
{ {
char line[512]; char line[512];
FILE *f = xfopen(input_file, "r"); FILE *f;
struct stat sb;
if (check_exists && stat(input_file, &sb))
return;
f = xfopen(input_file, "r");
while (fgets(line, sizeof(line), f)) { while (fgets(line, sizeof(line), f)) {
uint32_t mark; uint32_t mark;
char *line_end, *mark_end; char *line_end, *mark_end;
@ -1120,7 +1125,9 @@ int cmd_fast_export(int argc, const char **argv, const char *prefix)
struct rev_info revs; struct rev_info revs;
struct object_array commits = OBJECT_ARRAY_INIT; struct object_array commits = OBJECT_ARRAY_INIT;
struct commit *commit; struct commit *commit;
char *export_filename = NULL, *import_filename = NULL; char *export_filename = NULL,
*import_filename = NULL,
*import_filename_if_exists = NULL;
uint32_t lastimportid; uint32_t lastimportid;
struct string_list refspecs_list = STRING_LIST_INIT_NODUP; struct string_list refspecs_list = STRING_LIST_INIT_NODUP;
struct string_list paths_of_changed_objects = STRING_LIST_INIT_DUP; 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)
N_("Dump marks to this file")), N_("Dump marks to this file")),
OPT_STRING(0, "import-marks", &import_filename, N_("file"), OPT_STRING(0, "import-marks", &import_filename, N_("file"),
N_("Import marks from this file")), N_("Import marks from this file")),
OPT_STRING(0, "import-marks-if-exists",
&import_filename_if_exists,
N_("file"),
N_("Import marks from this file if it exists")),
OPT_BOOL(0, "fake-missing-tagger", &fake_missing_tagger, OPT_BOOL(0, "fake-missing-tagger", &fake_missing_tagger,
N_("Fake a tagger when tags lack one")), N_("Fake a tagger when tags lack one")),
OPT_BOOL(0, "full-tree", &full_tree, OPT_BOOL(0, "full-tree", &full_tree,
@ -1187,8 +1198,12 @@ int cmd_fast_export(int argc, const char **argv, const char *prefix)
if (use_done_feature) if (use_done_feature)
printf("feature done\n"); printf("feature done\n");
if (import_filename && import_filename_if_exists)
die(_("Cannot pass both --import-marks and --import-marks-if-exists"));
if (import_filename) if (import_filename)
import_marks(import_filename); import_marks(import_filename, 0);
else if (import_filename_if_exists)
import_marks(import_filename_if_exists, 1);
lastimportid = last_idnum; lastimportid = last_idnum;
if (import_filename && revs.prune_data.nr) if (import_filename && revs.prune_data.nr)

View file

@ -580,17 +580,15 @@ test_expect_success 'fast-export quotes pathnames' '
' '
test_expect_success 'test bidirectionality' ' test_expect_success 'test bidirectionality' '
>marks-cur &&
>marks-new &&
git init marks-test && git init marks-test &&
git fast-export --export-marks=marks-cur --import-marks=marks-cur --branches | \ git fast-export --export-marks=marks-cur --import-marks-if-exists=marks-cur --branches | \
git --git-dir=marks-test/.git fast-import --export-marks=marks-new --import-marks=marks-new && git --git-dir=marks-test/.git fast-import --export-marks=marks-new --import-marks-if-exists=marks-new &&
(cd marks-test && (cd marks-test &&
git reset --hard && git reset --hard &&
echo Wohlauf > file && echo Wohlauf > file &&
git commit -a -m "back in time") && git commit -a -m "back in time") &&
git --git-dir=marks-test/.git fast-export --export-marks=marks-new --import-marks=marks-new --branches | \ git --git-dir=marks-test/.git fast-export --export-marks=marks-new --import-marks-if-exists=marks-new --branches | \
git fast-import --export-marks=marks-cur --import-marks=marks-cur git fast-import --export-marks=marks-cur --import-marks-if-exists=marks-cur
' '
cat > expected << EOF cat > expected << EOF