fast-import: move command state globals into 'struct fast_import_state'

A previous commit introduced 'struct fast_import_state' to hold some command state, and reduce the need for global variables. Let's continue in the same direction and move two more global variables that describe the command state into it: 'seen_data_command' and 'allow_unsafe_features'. All the sites accessing these variables are already in functions that receive the 'state' parameter (or in cmd_fast_import() which owns the struct), so no additional threading is needed. As 'state->allow_unsafe_features' is now dereferenced in check_unsafe_feature(), its 'state' parameter is no longer unused, so the UNUSED marker is removed. The fast_import_state_init() call is moved up before the early command-line scan for '--allow-unsafe-features', so that this option can be recorded directly into the struct without being clobbered by the memset() in fast_import_state_init(). This is a mechanical refactoring with no intended behavior change. Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Christian Couder committed Jul 16, 2026 at 18:55 UTC f254d37b89ff9c9d742ace65e852ef37b73540a0
1 file changed +12 -12
builtin/fast-import.c
+12 -12
@@ -257,9 +257,7 @@ static struct recent_command *rc_free;
257 static unsigned int cmd_save = 100;
258 static uintmax_t next_mark;
259 static struct strbuf new_data = STRBUF_INIT;
260 -static int seen_data_command;
260 static int require_explicit_termination;
262 -static int allow_unsafe_features;
261
262 /* Signal handling */
263 static volatile sig_atomic_t checkpoint_requested;
@@ -277,6 +275,8 @@ struct fast_import_state {
275 int argc;
276 const char **argv;
277 const char *prefix;
278 + int seen_data_command;
279 + int allow_unsafe_features;
280 };
281
282 static void fast_import_state_init(struct fast_import_state *state,
@@ -1876,7 +1876,7 @@ static int read_next_command(struct fast_import_state *state)
1876 if (stdin_eof)
1877 return EOF;
1878
1879 - if (!seen_data_command
1879 + if (!state->seen_data_command
1880 && !starts_with(command_buf.buf, "feature ")
1881 && !starts_with(command_buf.buf, "option ")) {
1882 parse_argv(state);
@@ -3809,9 +3809,9 @@ static int parse_one_option(struct fast_import_state *state, const char *option)
3809 return 1;
3810 }
3811
3812 -static void check_unsafe_feature(struct fast_import_state *state UNUSED, const char *feature, int from_stream)
3812 +static void check_unsafe_feature(struct fast_import_state *state, const char *feature, int from_stream)
3813 {
3814 - if (from_stream && !allow_unsafe_features)
3814 + if (from_stream && !state->allow_unsafe_features)
3815 die(_("feature '%s' forbidden in input without --allow-unsafe-features"),
3816 feature);
3817 }
@@ -3860,7 +3860,7 @@ static int parse_one_feature(struct fast_import_state *state, const char *featur
3860
3861 static void parse_feature(struct fast_import_state *state, const char *feature)
3862 {
3863 - if (seen_data_command)
3863 + if (state->seen_data_command)
3864 die(_("got feature command '%s' after data command"), feature);
3865
3866 if (parse_one_feature(state, feature, 1))
@@ -3871,7 +3871,7 @@ static void parse_feature(struct fast_import_state *state, const char *feature)
3871
3872 static void parse_option(struct fast_import_state *state, const char *option)
3873 {
3874 - if (seen_data_command)
3874 + if (state->seen_data_command)
3875 die(_("got option command '%s' after data command"), option);
3876
3877 if (parse_one_option(state, option))
@@ -3939,7 +3939,7 @@ static void parse_argv(struct fast_import_state *state)
3939 if (i != state->argc)
3940 usage(fast_import_usage);
3941
3942 - seen_data_command = 1;
3942 + state->seen_data_command = 1;
3943 if (import_marks_file)
3944 read_marks();
3945 build_mark_map(&sub_marks_from, &sub_marks_to);
@@ -3954,6 +3954,8 @@ int cmd_fast_import(int argc,
3954
3955 show_usage_if_asked(argc, argv, fast_import_usage);
3956
3957 + fast_import_state_init(&state, argc, argv, prefix);
3958 +
3959 reset_pack_idx_option(&pack_idx_opts);
3960 git_pack_config();
3961
@@ -3977,11 +3979,9 @@ int cmd_fast_import(int argc,
3979 if (*arg != '-' || !strcmp(arg, "--"))
3980 break;
3981 if (!strcmp(arg, "--allow-unsafe-features"))
3980 - allow_unsafe_features = 1;
3982 + state.allow_unsafe_features = 1;
3983 }
3984
3983 - fast_import_state_init(&state, argc, argv, prefix);
3984 -
3985 rc_free = mem_pool_alloc(&fi_mem_pool, cmd_save * sizeof(*rc_free));
3986 for (unsigned int i = 0; i < (cmd_save - 1); i++)
3987 rc_free[i].next = &rc_free[i + 1];
@@ -4028,7 +4028,7 @@ int cmd_fast_import(int argc,
4028 }
4029
4030 /* argv hasn't been parsed yet, do so */
4031 - if (!seen_data_command)
4031 + if (!state.seen_data_command)
4032 parse_argv(&state);
4033
4034 if (require_explicit_termination && feof(stdin))