Skip to content

Commit 9c8398f

Browse files
SRabbeliergitster
authored andcommitted
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. Non-git options are ignored, unrecognised options result in an error. Signed-off-by: Sverre Rabbelier <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f963bd5 commit 9c8398f

File tree

2 files changed

+94
-25
lines changed

2 files changed

+94
-25
lines changed

Documentation/git-fast-import.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,11 @@ and control the current import process. More detailed discussion
307307
Require that fast-import supports the specified feature, or
308308
abort if it does not.
309309

310+
`option`::
311+
Specify any of the options listed under OPTIONS that do not
312+
change stream semantic to suit the frontend's needs. This
313+
command is optional and is not needed to perform an import.
314+
310315
`commit`
311316
~~~~~~~~
312317
Create or update a branch with a new commit, recording one logical
@@ -871,6 +876,33 @@ The following features are currently supported:
871876
* export-marks
872877
* force
873878

879+
`option`
880+
~~~~~~~~
881+
Processes the specified option so that git fast-import behaves in a
882+
way that suits the frontend's needs.
883+
Note that options specified by the frontend are overridden by any
884+
options the user may specify to git fast-import itself.
885+
886+
....
887+
'option' SP <option> LF
888+
....
889+
890+
The `<option>` part of the command may contain any of the options
891+
listed in the OPTIONS section that do not change import semantics,
892+
without the leading '--' and is treated in the same way.
893+
894+
Option commands must be the first commands on the input (not counting
895+
feature commands), to give an option command after any non-option
896+
command is an error.
897+
898+
The following commandline options change import semantics and may therefore
899+
not be passed as option:
900+
901+
* date-format
902+
* import-marks
903+
* export-marks
904+
* force
905+
874906
Crash Reports
875907
-------------
876908
If fast-import is supplied invalid input it will terminate with a

fast-import.c

Lines changed: 62 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,8 @@ static unsigned long branch_load_count;
296296
static int failure;
297297
static FILE *pack_edges;
298298
static unsigned int show_stats = 1;
299+
static int global_argc;
300+
static const char **global_argv;
299301

300302
/* Memory pools */
301303
static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool);
@@ -355,6 +357,8 @@ static uintmax_t next_mark;
355357
static struct strbuf new_data = STRBUF_INIT;
356358
static int seen_data_command;
357359

360+
static void parse_argv(void);
361+
358362
static void write_branch_report(FILE *rpt, struct branch *b)
359363
{
360364
fprintf(rpt, "%s:\n", b->name);
@@ -1706,8 +1710,9 @@ static int read_next_command(void)
17061710
return EOF;
17071711

17081712
if (!seen_data_command
1709-
&& prefixcmp(command_buf.buf, "feature ")) {
1710-
seen_data_command = 1;
1713+
&& prefixcmp(command_buf.buf, "feature ")
1714+
&& prefixcmp(command_buf.buf, "option ")) {
1715+
parse_argv();
17111716
}
17121717

17131718
rc = rc_free;
@@ -2512,31 +2517,25 @@ static void option_export_pack_edges(const char *edges)
25122517
die_errno("Cannot open '%s'", edges);
25132518
}
25142519

2515-
static void parse_one_option(const char *option)
2520+
static int parse_one_option(const char *option)
25162521
{
2517-
if (!prefixcmp(option, "date-format=")) {
2518-
option_date_format(option + 12);
2519-
} else if (!prefixcmp(option, "max-pack-size=")) {
2522+
if (!prefixcmp(option, "max-pack-size=")) {
25202523
option_max_pack_size(option + 14);
25212524
} else if (!prefixcmp(option, "depth=")) {
25222525
option_depth(option + 6);
25232526
} else if (!prefixcmp(option, "active-branches=")) {
25242527
option_active_branches(option + 16);
2525-
} else if (!prefixcmp(option, "import-marks=")) {
2526-
option_import_marks(option + 13);
2527-
} else if (!prefixcmp(option, "export-marks=")) {
2528-
option_export_marks(option + 13);
25292528
} else if (!prefixcmp(option, "export-pack-edges=")) {
25302529
option_export_pack_edges(option + 18);
2531-
} else if (!prefixcmp(option, "force")) {
2532-
force_update = 1;
25332530
} else if (!prefixcmp(option, "quiet")) {
25342531
show_stats = 0;
25352532
} else if (!prefixcmp(option, "stats")) {
25362533
show_stats = 1;
25372534
} else {
2538-
die("Unsupported option: %s", option);
2535+
return 0;
25392536
}
2537+
2538+
return 1;
25402539
}
25412540

25422541
static int parse_one_feature(const char *feature)
@@ -2569,6 +2568,19 @@ static void parse_feature(void)
25692568
die("This version of fast-import does not support feature %s.", feature);
25702569
}
25712570

2571+
static void parse_option(void)
2572+
{
2573+
char *option = command_buf.buf + 11;
2574+
2575+
if (seen_data_command)
2576+
die("Got option command '%s' after data command", option);
2577+
2578+
if (parse_one_option(option))
2579+
return;
2580+
2581+
die("This version of fast-import does not support option: %s", option);
2582+
}
2583+
25722584
static int git_pack_config(const char *k, const char *v, void *cb)
25732585
{
25742586
if (!strcmp(k, "pack.depth")) {
@@ -2593,6 +2605,32 @@ static int git_pack_config(const char *k, const char *v, void *cb)
25932605
static const char fast_import_usage[] =
25942606
"git fast-import [--date-format=f] [--max-pack-size=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file]";
25952607

2608+
static void parse_argv(void)
2609+
{
2610+
unsigned int i;
2611+
2612+
for (i = 1; i < global_argc; i++) {
2613+
const char *a = global_argv[i];
2614+
2615+
if (*a != '-' || !strcmp(a, "--"))
2616+
break;
2617+
2618+
if (parse_one_option(a + 2))
2619+
continue;
2620+
2621+
if (parse_one_feature(a + 2))
2622+
continue;
2623+
2624+
die("unknown option %s", a);
2625+
}
2626+
if (i != global_argc)
2627+
usage(fast_import_usage);
2628+
2629+
seen_data_command = 1;
2630+
if (import_marks_file)
2631+
read_marks();
2632+
}
2633+
25962634
int main(int argc, const char **argv)
25972635
{
25982636
unsigned int i;
@@ -2614,18 +2652,8 @@ int main(int argc, const char **argv)
26142652
avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
26152653
marks = pool_calloc(1, sizeof(struct mark_set));
26162654

2617-
for (i = 1; i < argc; i++) {
2618-
const char *a = argv[i];
2619-
2620-
if (*a != '-' || !strcmp(a, "--"))
2621-
break;
2622-
2623-
parse_one_option(a + 2);
2624-
}
2625-
if (i != argc)
2626-
usage(fast_import_usage);
2627-
if (import_marks_file)
2628-
read_marks();
2655+
global_argc = argc;
2656+
global_argv = argv;
26292657

26302658
rc_free = pool_alloc(cmd_save * sizeof(*rc_free));
26312659
for (i = 0; i < (cmd_save - 1); i++)
@@ -2650,9 +2678,18 @@ int main(int argc, const char **argv)
26502678
parse_progress();
26512679
else if (!prefixcmp(command_buf.buf, "feature "))
26522680
parse_feature();
2681+
else if (!prefixcmp(command_buf.buf, "option git "))
2682+
parse_option();
2683+
else if (!prefixcmp(command_buf.buf, "option "))
2684+
/* ignore non-git options*/;
26532685
else
26542686
die("Unsupported command: %s", command_buf.buf);
26552687
}
2688+
2689+
/* argv hasn't been parsed yet, do so */
2690+
if (!seen_data_command)
2691+
parse_argv();
2692+
26562693
end_packfile();
26572694

26582695
dump_branches();

0 commit comments

Comments
 (0)