Skip to contentSkip to conversation

fast-import: add a new option command

PATCH Superseded Sverre Rabbelier 2 participants 8 messages Jun 15, 2016 Documentation

v3 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v3 cover fast-import: add a new option command

Now we delay reading the marks file till option parsing is done.

Sverre Rabbelier (3):
fast-import: put option parsing code in seperate functions
fast-import: add option command
fast-import: test the new option command

 Documentation/git-fast-import.txt |   23 ++++
 fast-import.c                     |  235 +++++++++++++++++++++++++------------
 t/t9300-fast-import.sh            |   58 +++++++++
 3 files changed, 239 insertions(+), 77 deletions(-)
#1 lore raw

PATCH v3 1/3 fast-import: put option parsing code in seperate functions

Putting the options in their own functions increases readability of the option parsing block and makes it easier to reuse the option parsing code later on.

---

Unchanged from v2.

 fast-import.c |  115 +++++++++++++++++++++++++++++++++++++--------------------
 1 files changed, 75 insertions(+), 40 deletions(-)
fast-import.c +75 -40
index 7ef9865..b904f20 100644
@@ -291,6 +291,7 @@ static unsigned long branch_count;
 static unsigned long branch_load_count;
 static int failure;
 static FILE *pack_edges;
+static unsigned int show_stats = 1;
 
 /* Memory pools */
 static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool);
@@ -2337,7 +2338,7 @@ static void parse_progress(void)
 	skip_optional_lf();
 }
 
-static void import_marks(const char *input_file)
+static void option_import_marks(const char *input_file)
 {
 	char line[512];
 	FILE *f = fopen(input_file, "r");
@@ -2372,6 +2373,76 @@ static void import_marks(const char *input_file)
 	fclose(f);
 }
 
+static void option_date_format(const char *fmt)
+{
+	if (!strcmp(fmt, "raw"))
+		whenspec = WHENSPEC_RAW;
+	else if (!strcmp(fmt, "rfc2822"))
+		whenspec = WHENSPEC_RFC2822;
+	else if (!strcmp(fmt, "now"))
+		whenspec = WHENSPEC_NOW;
+	else
+		die("unknown --date-format argument %s", fmt);
+}
+
+static void option_max_pack_size(const char *packsize)
+{
+	max_packsize = strtoumax(packsize, NULL, 0) * 1024 * 1024;
+}
+
+static void option_depth(const char *depth)
+{
+	max_depth = strtoul(depth, NULL, 0);
+	if (max_depth > MAX_DEPTH)
+		die("--depth cannot exceed %u", MAX_DEPTH);
+}
+
+static void option_active_branches(const char *branches)
+{
+	max_active_branches = strtoul(branches, NULL, 0);
+}
+
+static void option_export_marks(const char *marks)
+{
+	mark_file = xstrdup(marks);
+}
+
+static void option_export_pack_edges(const char *edges)
+{
+	if (pack_edges)
+		fclose(pack_edges);
+	pack_edges = fopen(edges, "a");
+	if (!pack_edges)
+		die_errno("Cannot open '%s'", edges);
+}
+
+static void parse_one_option(const char *option)
+{
+	if (!prefixcmp(option, "date-format=")) {
+		option_date_format(option + 12);
+	} else if (!prefixcmp(option, "max-pack-size=")) {
+		option_max_pack_size(option + 14);
+	} else if (!prefixcmp(option, "depth=")) {
+		option_depth(option + 6);
+	} else if (!prefixcmp(option, "active-branches=")) {
+		option_active_branches(option + 16);
+	} else if (!prefixcmp(option, "import-marks=")) {
+		option_import_marks(option + 13);
+	} else if (!prefixcmp(option, "export-marks=")) {
+		option_export_marks(option + 13);
+	} else if (!prefixcmp(option, "export-pack-edges=")) {
+		option_export_pack_edges(option + 18);
+	} else if (!prefixcmp(option, "force")) {
+		force_update = 1;
+	} else if (!prefixcmp(option, "quiet")) {
+		show_stats = 0;
+	} else if (!prefixcmp(option, "stats")) {
+		show_stats = 1;
+	} else {
+		die("Unsupported option: %s", option);
+	}
+}
+
 static int git_pack_config(const char *k, const char *v, void *cb)
 {
 	if (!strcmp(k, "pack.depth")) {
@@ -2398,7 +2469,7 @@ static const char fast_import_usage[] =
 
 int main(int argc, const char **argv)
 {
-	unsigned int i, show_stats = 1;
+	unsigned int i;
 
 	git_extract_argv0_path(argv[0]);
 
@@ -2419,44 +2490,8 @@ int main(int argc, const char **argv)
 
 		if (*a != '-' || !strcmp(a, "--"))
 			break;
-		else if (!prefixcmp(a, "--date-format=")) {
-			const char *fmt = a + 14;
-			if (!strcmp(fmt, "raw"))
-				whenspec = WHENSPEC_RAW;
-			else if (!strcmp(fmt, "rfc2822"))
-				whenspec = WHENSPEC_RFC2822;
-			else if (!strcmp(fmt, "now"))
-				whenspec = WHENSPEC_NOW;
-			else
-				die("unknown --date-format argument %s", fmt);
-		}
-		else if (!prefixcmp(a, "--max-pack-size="))
-			max_packsize = strtoumax(a + 16, NULL, 0) * 1024 * 1024;
-		else if (!prefixcmp(a, "--depth=")) {
-			max_depth = strtoul(a + 8, NULL, 0);
-			if (max_depth > MAX_DEPTH)
-				die("--depth cannot exceed %u", MAX_DEPTH);
-		}
-		else if (!prefixcmp(a, "--active-branches="))
-			max_active_branches = strtoul(a + 18, NULL, 0);
-		else if (!prefixcmp(a, "--import-marks="))
-			import_marks(a + 15);
-		else if (!prefixcmp(a, "--export-marks="))
-			mark_file = a + 15;
-		else if (!prefixcmp(a, "--export-pack-edges=")) {
-			if (pack_edges)
-				fclose(pack_edges);
-			pack_edges = fopen(a + 20, "a");
-			if (!pack_edges)
-				die_errno("Cannot open '%s'", a + 20);
-		} else if (!strcmp(a, "--force"))
-			force_update = 1;
-		else if (!strcmp(a, "--quiet"))
-			show_stats = 0;
-		else if (!strcmp(a, "--stats"))
-			show_stats = 1;
-		else
-			die("unknown option %s", a);
+
+		parse_one_option(a + 2);
 	}
 	if (i != argc)
 		usage(fast_import_usage);
Signature · 1 line
1.6.4.122.g6ffd7
#2 lore raw

PATCH v3 2/3 fast-import: add option command

This allows the frontend to specify any of the supported options as long as no non-option command has been given. This way the user does not have to include any frontend-specific options, but instead she can rely on the frontend to tell fast-import what it needs.

Also factor out parsing of argv and have it execute when we reach the first non-option command, or after all commands have been read and no non-option command has been encountered.

Lastly do not read the marks file till after all options have been parsed, instead of when receiving the option.

---

Now we delay reading the marks file till option parsing is done.

 Documentation/git-fast-import.txt |   23 +++++++
 fast-import.c                     |  130 +++++++++++++++++++++++++------------
 2 files changed, 111 insertions(+), 42 deletions(-)
Documentation/git-fast-import.txt +23 -0
index c2f483a..ed8bd0d 100644
@@ -303,6 +303,11 @@ and control the current import process.  More detailed discussion
 	standard output.  This command is optional and is not needed
 	to perform an import.
 
+`option`::
+    Specify any of the options listed under OPTIONS to change
+    fast-import's behavior to suit the frontend's needs. This command
+    is optional and is not needed to perform an import.
+
 `commit`
 ~~~~~~~~
 Create or update a branch with a new commit, recording one logical
@@ -813,6 +818,24 @@ Placing a `progress` command immediately after a `checkpoint` will
 inform the reader when the `checkpoint` has been completed and it
 can safely access the refs that fast-import updated.
 
+`option`
+~~~~~~~~
+Processes the specified option so that git fast-import behaves in a
+way that suits the frontend's needs.
+Note that options specified by the frontend are overridden by any
+options the user may specify to git fast-import itself.
+
+....
+    'option' SP <option> LF
+....
+
+The `<option>` part of the command may contain any of the options
+listed in the OPTIONS section, without the leading '--' and is
+treated in the same way.
+
+Option commands must be the first commands on the input, to give an
+option command after any non-option command is an error.
+
 Crash Reports
 -------------
 If fast-import is supplied invalid input it will terminate with a
fast-import.c +88 -42

194 lines not on this page · raw message

Signature · 1 line
1.6.4.122.g6ffd7
#3 lore raw

PATCH v3 3/3 fast-import: test the new option command

Test three options (quiet and import/export-marks) and verify that the commandline options override these.

---

Added some tests to verify that the marks file is handled properly as suggested by Dscho and Shawn.

 t/t9300-fast-import.sh |   58 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 58 insertions(+), 0 deletions(-)
t/t9300-fast-import.sh +58 -0
index 821be7c..62369e5 100755
@@ -1088,4 +1088,62 @@ INPUT_END
 test_expect_success 'P: fail on blob mark in gitlink' '
     test_must_fail git fast-import <input'
 
+###
+### series Q (options)
+###
+
+cat >input << EOF
+option quiet
+blob
+data 3
+hi
+
+EOF
+
+touch empty
+
+test_expect_success 'Q: quiet option results in no stats being output' '
+    cat input | git fast-import 2> output &&
+    test_cmp empty output
+'
+
+cat >input << EOF
+option export-marks=git.marks
+blob
+mark :1
+data 3
+hi
+
+EOF
+
+test_expect_success \
+    'Q: export-marks option results in a marks file being created' \
+    'cat input | git fast-import &&
+    grep :1 git.marks'
+
+test_expect_success \
+    'Q: export-marks options can be overriden by commandline options' \
+    'cat input | git fast-import --export-marks=other.marks &&
+    grep :1 other.marks'
+
+cat >input << EOF
+option import-marks=marks.out
+option export-marks=marks.new
+EOF
+
+test_expect_success \
+    'Q: import to output marks works without any content' \
+    'cat input | git fast-import &&
+    test_cmp marks.out marks.new'
+
+cat >input <<EOF
+option import-marks=nonexistant.marks
+option export-marks=marks.new
+EOF
+
+test_expect_success \
+    'Q: import marks uses the commandline marks file when the stream specifies one' \
+    'cat input | git fast-import --import-marks=marks.out &&
+    test_cmp marks.out marks.new'
+
 test_done
Signature · 1 line
1.6.4.122.g6ffd7
#4 lore raw

Re: [PATCH v3 2/3] fast-import: add option command

In reply to Sverre Rabbelier

Documentation/git-fast-import.txt Placing a `progress` command immediately after a `checkpoint` will · line 826

Quoted 6 lines · Sverre Rabbelier

+option
+~~~~~~~~

Documentation/git-fast-import.txt +4 -0
+Processes the specified option so that git fast-import behaves in a
+way that suits the frontend's needs.
+Note that options specified by the frontend are overridden by any
+options the user may specify to git fast-import itself.

Wha? This disagrees with the code.

Quoted 4 lines
fast-import.c +4 -0
+static void read_marks(void)
+{
+	char line[512];
+	FILE *f = fopen(input_file, "r");

...

fast-import.c static void parse_progress(void) · line 2344

Quoted 4 lines
fast-import.c +1 -2
+static void option_import_marks(const char *marks)
 {
-	char line[512];
-	FILE *f = fopen(input_file, "r");

This is a nasty refactoring, I would prefer to see it done in its own commit, "move option_import_marks so it can be called during command processing".

fast-import.c int main(int argc, const char **argv) · line 2567

Quoted 13 lines
fast-import.c +6 -0
@@ -2517,9 +2556,16 @@ int main(int argc, const char **argv)
 			parse_checkpoint();
 		else if (!prefixcmp(command_buf.buf, "progress "))
 			parse_progress();
+		else if (!prefixcmp(command_buf.buf, "option "))
+			parse_option();
 		else
 			die("Unsupported command: %s", command_buf.buf);
 	}
+
+	// argv hasn't been parsed yet, do so
+	if (!seen_non_option_command)
+		parse_argv();

This is too late. Options like --date-format or --max-pack-size or --depth or --active-branches all influence the command processing above. Parsing these at the end means they have no affect on the import, which is wrong.

Oh, and --active-branches or --max-pack-size or --depth are really good examples of things that maybe you do want to override on the command line. They have impact only on memory usage of the running import process, and the local disk usage. Maybe the frontend has given too many active branches for your physical memory, and you want a lower threshold.

So yea, I really do think its a good idea for command line options to override stream options, despite what Dscho may think. :-)

Signature · 1 line
Shawn.
#5 lore raw

Re: [PATCH v3 2/3] fast-import: add option command

Heya,

Quoted 9 lines · Shawn O. Pearce

Documentation/git-fast-import.txt Placing a `progress` command immediately after a `checkpoint` will · line 826

Quoted 6 lines · Sverre Rabbelier

+option
+~~~~~~~~

Documentation/git-fast-import.txt +4 -0
+Processes the specified option so that git fast-import behaves in a
+way that suits the frontend's needs.
+Note that options specified by the frontend are overridden by any
+options the user may specify to git fast-import itself.

Wha?  This disagrees with the code.

It does? It's saying that the user my override what the frontend specifies, which is what the current version does.

Quoted 3 lines

This is a nasty refactoring, I would prefer to see it done in its own commit, "move option_import_marks so it can be called during command processing".

Ok, will do.

Quoted 5 lines
Quoted 3 lines

+     // argv hasn't been parsed yet, do so
+     if (!seen_non_option_command)
+             parse_argv();

This is too late.

No it's not. Earlier in the patch, at the other 'seen_non_option_command', we call parse_argv() as well (which happens when a non-option command is issued). This statement is here to deal with options that affect an empty stream, such as 'git format-patch --import-marks=marks.old --export-marks=marks.new < /dev/null && test_cmp marks.old marks.new'.

Quoted 2 lines

So yea, I really do think its a good idea for command line options to override stream options, despite what Dscho may think.  :-)

Which is what the current version does, yes?

Signature · 3 lines
Cheers,

Sverre Rabbelier
#6 lore raw

Re: [PATCH v3 2/3] fast-import: add option command

Quoted 11 lines · Sverre Rabbelier
Quoted 9 lines · Shawn O. Pearce

Documentation/git-fast-import.txt Placing a `progress` command immediately after a `checkpoint` will · line 826

Quoted 6 lines · Sverre Rabbelier

+option
+~~~~~~~~

Documentation/git-fast-import.txt +4 -0
+Processes the specified option so that git fast-import behaves in a
+way that suits the frontend's needs.
+Note that options specified by the frontend are overridden by any
+options the user may specify to git fast-import itself.

Wha? ?This disagrees with the code.

Quoted 2 lines

It does? It's saying that the user my override what the frontend specifies, which is what the current version does.

Sorry, lack of caffeine. Rereading the docs makes sense.

Quoted 11 lines
Quoted 4 lines
Quoted 2 lines

+ ? ? if (!seen_non_option_command)
+ ? ? ? ? ? ? parse_argv();

This is too late.

No it's not. Earlier in the patch, at the other 'seen_non_option_command', we call parse_argv() as well (which happens when a non-option command is issued). This statement is here to deal with options that affect an empty stream, such as 'git format-patch --import-marks=marks.old --export-marks=marks.new < /dev/null && test_cmp marks.old marks.new'.

Oy, I missed that call. I looked for it but gave up too soon I guess.

Quoted 4 lines
Quoted 2 lines

So yea, I really do think its a good idea for command line options to override stream options, despite what Dscho may think. ?:-)

Which is what the current version does, yes?

Yes. :-)

Signature · 1 line
Shawn.
#7 lore raw

Re: [PATCH v3 2/3] fast-import: add option command

Heya,

Quoted 1 line · Shawn O. Pearce

Sorry, lack of caffeine.  Rereading the docs makes sense.

Perfectly acceptable, I totally though the 'break' in a 'switch' statement would break out of the outer for loop the other day.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

So yea, I really do think its a good idea for command line options to override stream options, despite what Dscho may think. ?:-)

Which is what the current version does, yes?

Yes.  :-)

Ok, I think perhaps we can wait 'nother day to see if anyone notices anything (possibly after more caffeine) and then ask Junio to apply?

Signature · 3 lines
Cheers,

Sverre Rabbelier