Skip to contentSkip to conversation

fast-import: Create import-marks file when necessary

PATCH In review Ramkumar Ramachandra 3 participants 6 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fast-import: Create import-marks file when necessary

When both --import-marks and --export-marks are given, and specify the same file, that file should be created if it doesn't exist. Without this patch, the caller would have to check for the existence of a marks file and vary its fast-import invocation accordingly.

---
 fast-import.c |   17 ++++++++++++++++-
 1 files changed, 16 insertions(+), 1 deletions(-)
fast-import.c +16 -1
index 5055504..cb7a9c9 100644
@@ -3247,8 +3247,23 @@ static void parse_argv(void)
 		usage(fast_import_usage);
 
 	seen_data_command = 1;
-	if (import_marks_file)
+	if (import_marks_file) {
+		FILE *f = fopen(import_marks_file, "r");
+		if (!f) {
+			if (errno == ENOENT &&
+				!strcmp(import_marks_file, export_marks_file)) {
+				FILE *d = fopen(import_marks_file, "w");
+				if (!d)
+					die_errno("cannot create '%s'", import_marks_file);
+				fclose(d);
+			}
+			else
+				die_errno("cannot read '%s'", import_marks_file);
+		}
+		else
+			fclose(f);
 		read_marks();
+	}
 }
 
 int main(int argc, const char **argv)
Signature · 1 line
1.7.4.rc1.8.g98938a.dirty
#1 lore raw
Quoted 7 lines · Ramkumar Ramachandra

When both --import-marks and --export-marks are given, and specify the same file, that file should be created if it doesn't exist. Without this patch, the caller would have to check for the existence of a marks file and vary its fast-import invocation accordingly.

Do you mean that you are solving a problem for bootstrapping a script that says "read from this file if exists, do work, and then write to the same file for later invocation"?

Assuming that is what you are solving, why do you create the file in your patch in the import codepath? I would imagine a sane logic flow for "read if exists, do work and then write to the same" would be more like:

	if (import_marks) {
		FILE = *f = fopen(import_marks);
                if (f)
                	read_marks();
		else if (errno == ENOENT && !strcmp(import_marks, export_marks))
                	; /* Bootstrapping with "read if exists" */
		else
                	barf();
	}

no?

Also I find it a bad taste to use "The user gave the same file to import and export" as a hint for "read if exists".

The first two lines of the proposed commit message is way suboptimal, if the problem is "Not allowing read-if-exists-but-do-not-barf-if-missing makes it hard to bootstrap repeated invocations that use a single import/export marks file as the state-keeping mechanism".

It does not have to be created if it doesn't exist---the program only needs not to barf. The condition does not have to be "if they are the same file". The caller may want to script

	command --import-if-exists=state --export=new-state+ &&
        mv new-state+ state

to make sure that when the underlying command fails, the previous state is still kept intact without getting overwritten.

#2 lore raw

PATCH 1/1 fast-import: Don't barf if import-marks file is missing

The --import-marks option used to barf when the specified marks file doesn't exist. Change its meaning to "read marks file if it exists" so that callers don't have to handle bootstraping as a special case.

Thanks-to: Junio C Hamano <[email protected]>

---
 Documentation/git-fast-import.txt |    2 +-
 fast-import.c                     |    6 +++++-
 2 files changed, 6 insertions(+), 2 deletions(-)
Documentation/git-fast-import.txt +1 -1
index f56dfca..94fbe54 100644
@@ -72,7 +72,7 @@ OPTIONS
 
 --import-marks=<file>::
 	Before processing any input, load the marks specified in
-	<file>.  The input file must exist, must be readable, and
+	<file>, if it exists.  The input file must be readable, and
 	must use the same format as produced by \--export-marks.
 	Multiple options may be supplied to import more than one
 	set of marks.  If a mark is defined to different values,
fast-import.c +5 -1
index 7857760..cbd5124 100644
@@ -1795,7 +1795,11 @@ static void read_marks(void)
 {
 	char line[512];
 	FILE *f = fopen(import_marks_file, "r");
-	if (!f)
+	if (f)
+		;
+	else if (errno == ENOENT)
+		return; /* Marks file does not exist */
+	else
 		die_errno("cannot read '%s'", import_marks_file);
 	while (fgets(line, sizeof(line), f)) {
 		uintmax_t mark;
Signature · 1 line
1.7.2.2.409.gdbb11.dirty
#3 lore raw

Re: [PATCH] fast-import: Don't barf if import-marks file is missing

Quoted 2 lines · Ramkumar Ramachandra

The --import-marks option used to barf when the specified marks file doesn't exist.

Micronit: change descriptions should contrast present behavior

The --import-marks options errors out when the marks file is missing.

with user requirements

But if a frontend is just using a marks file to ensure its state persists between runs, it would be simpler if missing marks files were tolerated. In some cases the frontend cannot even check for the existence of the marks file itself because with --relative-marks, the location of the marks file is backend-dependent.

and the proposed solution

Change the meaning of --import-marks to "read marks file if it exists so such frontends don't have to use a different command line when bootstrapping.

I.e., the preferred style is to use present tense for the description of present behavior, so the patch can be read as a bug report.

Documentation/git-fast-import.txt OPTIONS · line 75

Quoted 8 lines
Documentation/git-fast-import.txt +1 -1
@@ -72,7 +72,7 @@ OPTIONS
 
 --import-marks=<file>::
 	Before processing any input, load the marks specified in
-	<file>.  The input file must exist, must be readable, and
+	<file>, if it exists.

This means removing a typo detection facility.

	git fast-import --import-marks=something-like-a-real-marks-file.marks

would be accepted and result in an empty marks table instead of an error message. Is that considered worth it? (My hunch is no, but either way, I think it ought to be mentioned in the change description.)

fast-import.c static void read_marks(void)

Quoted 3 lines
fast-import.c +0 -0
@@ -1795,7 +1795,11 @@ static void read_marks(void)

Nice implementation. Maybe something like this on top?

---
 Documentation/git-fast-import.txt |    6 +++-
 fast-import.c                     |   12 ++++++--
 t/t9300-fast-import.sh            |   55 +++++++++++++++++++++++++++++++++++++
 3 files changed, 69 insertions(+), 4 deletions(-)
Documentation/git-fast-import.txt +5 -1
index 94fbe54..32a062c 100644
@@ -72,12 +72,16 @@ OPTIONS
 
 --import-marks=<file>::
 	Before processing any input, load the marks specified in
-	<file>, if it exists.  The input file must be readable, and
+	<file>.  The input file must exist, must be readable, and
 	must use the same format as produced by \--export-marks.
 	Multiple options may be supplied to import more than one
 	set of marks.  If a mark is defined to different values,
 	the last file wins.
 
+--import-marks-if-exists=<file>::
+	Like --import-marks but instead of erroring out, silently
+	skips the file if it does not exist.
+
 --relative-marks::
 	After specifying --relative-marks= the paths specified
 	with --import-marks= and --export-marks= are relative
fast-import.c +9 -3
index cbd5124..c525fda 100644
@@ -329,6 +329,7 @@ static struct mark_set *marks;
 static const char *export_marks_file;
 static const char *import_marks_file;
 static int import_marks_file_from_stream;
+static int import_marks_file_ignore_missing;
 static int relative_marks_paths;
 
 /* Our last blob */
@@ -1797,7 +1798,7 @@ static void read_marks(void)
 	FILE *f = fopen(import_marks_file, "r");
 	if (f)
 		;
-	else if (errno == ENOENT)
+	else if (import_marks_file_ignore_missing && errno == ENOENT)
 		return; /* Marks file does not exist */
 	else
 		die_errno("cannot read '%s'", import_marks_file);
@@ -2865,7 +2866,8 @@ static char* make_fast_import_path(const char *path)
 	return strbuf_detach(&abs_path, NULL);
 }
 
-static void option_import_marks(const char *marks, int from_stream)
+static void option_import_marks(const char *marks,
+					int from_stream, int ignore_missing)
 {
 	if (import_marks_file) {
 		if (from_stream)
@@ -2879,6 +2881,7 @@ static void option_import_marks(const char *marks, int from_stream)
 	import_marks_file = make_fast_import_path(marks);
 	safe_create_leading_directories_const(import_marks_file);
 	import_marks_file_from_stream = from_stream;
+	import_marks_file_ignore_missing = ignore_missing;
 }
 
 static void option_date_format(const char *fmt)
@@ -2978,7 +2981,10 @@ static int parse_one_feature(const char *feature, int from_stream)
 	if (!prefixcmp(feature, "date-format=")) {
 		option_date_format(feature + 12);
 	} else if (!prefixcmp(feature, "import-marks=")) {
-		option_import_marks(feature + 13, from_stream);
+		option_import_marks(feature + 13, from_stream, 0);
+	} else if (!prefixcmp(feature, "import-marks-if-exists=")) {
+		option_import_marks(feature + strlen("import-marks-if-exists="),
+					from_stream, 1);
 	} else if (!prefixcmp(feature, "export-marks=")) {
 		option_export_marks(feature + 13);
 	} else if (!strcmp(feature, "cat-blob")) {
t/t9300-fast-import.sh +55 -0

61 lines not on this page · raw message

Signature · 1 line
1.7.4.rc1
#4 lore raw

PATCH 1/1 fast-import: Introduce --import-marks-if-exists

The --import-marks option errors out when the specified marks file doesn't exist. If a frontend is just using a marks file to ensure its state persists between runs, it would be simpler if missing marks files were tolerated. In some cases, the frontend cannot even check for the existence of the marks file because with --relative-marks, the location of the marks file is backend-dependent. This can be solved by changing the meaning of --import-marks, but will result in trading off functionality that some frontends might desire. So, add new command-line option --import-marks-if-exists that's like --import-marks, except that it tolerates missing marks files.

---
Changes since last time: Junio suggested making this an optional feature, and Jonathan wrote a fixup patch for the same.

 Documentation/git-fast-import.txt |    4 +++
 fast-import.c                     |   16 +++++++++--
 t/t9300-fast-import.sh            |   55 +++++++++++++++++++++++++++++++++++++
 3 files changed, 72 insertions(+), 3 deletions(-)
Documentation/git-fast-import.txt +4 -0
index f56dfca..32a062c 100644
@@ -78,6 +78,10 @@ OPTIONS
 	set of marks.  If a mark is defined to different values,
 	the last file wins.
 
+--import-marks-if-exists=<file>::
+	Like --import-marks but instead of erroring out, silently
+	skips the file if it does not exist.
+
 --relative-marks::
 	After specifying --relative-marks= the paths specified
 	with --import-marks= and --export-marks= are relative
fast-import.c +13 -3
index 7857760..c525fda 100644
@@ -329,6 +329,7 @@ static struct mark_set *marks;
 static const char *export_marks_file;
 static const char *import_marks_file;
 static int import_marks_file_from_stream;
+static int import_marks_file_ignore_missing;
 static int relative_marks_paths;
 
 /* Our last blob */
@@ -1795,7 +1796,11 @@ static void read_marks(void)
 {
 	char line[512];
 	FILE *f = fopen(import_marks_file, "r");
-	if (!f)
+	if (f)
+		;
+	else if (import_marks_file_ignore_missing && errno == ENOENT)
+		return; /* Marks file does not exist */
+	else
 		die_errno("cannot read '%s'", import_marks_file);
 	while (fgets(line, sizeof(line), f)) {
 		uintmax_t mark;
@@ -2861,7 +2866,8 @@ static char* make_fast_import_path(const char *path)
 	return strbuf_detach(&abs_path, NULL);
 }
 
-static void option_import_marks(const char *marks, int from_stream)
+static void option_import_marks(const char *marks,
+					int from_stream, int ignore_missing)
 {
 	if (import_marks_file) {
 		if (from_stream)
@@ -2875,6 +2881,7 @@ static void option_import_marks(const char *marks, int from_stream)
 	import_marks_file = make_fast_import_path(marks);
 	safe_create_leading_directories_const(import_marks_file);
 	import_marks_file_from_stream = from_stream;
+	import_marks_file_ignore_missing = ignore_missing;
 }
 
 static void option_date_format(const char *fmt)
@@ -2974,7 +2981,10 @@ static int parse_one_feature(const char *feature, int from_stream)
 	if (!prefixcmp(feature, "date-format=")) {
 		option_date_format(feature + 12);
 	} else if (!prefixcmp(feature, "import-marks=")) {
-		option_import_marks(feature + 13, from_stream);
+		option_import_marks(feature + 13, from_stream, 0);
+	} else if (!prefixcmp(feature, "import-marks-if-exists=")) {
+		option_import_marks(feature + strlen("import-marks-if-exists="),
+					from_stream, 1);
 	} else if (!prefixcmp(feature, "export-marks=")) {
 		option_export_marks(feature + 13);
 	} else if (!strcmp(feature, "cat-blob")) {
t/t9300-fast-import.sh +55 -0

61 lines not on this page · raw message

Signature · 1 line
1.7.4.rc1.7.g2cf08.dirty
#5 lore raw

Re: [PATCH] fast-import: Don't barf if import-marks file is missing

In reply to Ramkumar Ramachandra
Quoted 3 lines · Ramkumar Ramachandra

The --import-marks option used to barf when the specified marks file doesn't exist. Change its meaning to "read marks file if it exists" so that callers don't have to handle bootstraping as a special case.

It is very plausible that people other than you are relying on "barf if it does not exist" semantics, and the above does not justify breaking their workflow.

I'd suggest making this an optional feature.