environment: migrate apply_default_whitespace and apply_default_ignorewhitespace

The global variables 'apply_default_whitespace' and
'apply_default_ignorewhitespace' are used to store the default
whitespace configuration for 'git apply'. Move these variables
into 'struct repo_config_values' to continue the libification
effort.

Dynamically allocated strings fetched via 'repo_config_get_string()'
are now tracked per-repository and safely freed in
'repo_config_values_clear()'.

As part of this transition, update 'git_apply_config()' to accept a
'struct repository *' argument rather than relying on the
'the_repository' global.

Mentored-by: Christian Couder <christian.couder@gmail.com>
Mentored-by: Ayush Chandekar <ayu.chandekar@gmail.com>
Mentored-by: Olamide Caleb Bello <belkid98@gmail.com>
Signed-off-by: Tian Yuchen <cat@malon.dev>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Tian Yuchen
2026-07-14 11:25:21 +08:00
committed by Junio C Hamano
parent 48cbe40079
commit a9f5e90fb9
3 changed files with 26 additions and 12 deletions

28
apply.c
View File

@@ -47,11 +47,17 @@ struct gitdiff_data {
int p_value;
};
static void git_apply_config(void)
static void git_apply_config(struct repository *repo)
{
repo_config_get_string(the_repository, "apply.whitespace", &apply_default_whitespace);
repo_config_get_string(the_repository, "apply.ignorewhitespace", &apply_default_ignorewhitespace);
repo_config(the_repository, git_xmerge_config, NULL);
struct repo_config_values *cfg = repo_config_values(repo);
FREE_AND_NULL(cfg->apply_default_whitespace);
repo_config_get_string(repo, "apply.whitespace",
&cfg->apply_default_whitespace);
FREE_AND_NULL(cfg->apply_default_ignorewhitespace);
repo_config_get_string(repo, "apply.ignorewhitespace",
&cfg->apply_default_ignorewhitespace);
repo_config(repo, git_xmerge_config, NULL);
}
static int parse_whitespace_option(struct apply_state *state, const char *option)
@@ -109,6 +115,8 @@ int init_apply_state(struct apply_state *state,
struct repository *repo,
const char *prefix)
{
struct repo_config_values *cfg = repo_config_values(repo);
memset(state, 0, sizeof(*state));
state->prefix = prefix;
state->repo = repo;
@@ -126,10 +134,13 @@ int init_apply_state(struct apply_state *state,
strset_init(&state->kept_symlinks);
strbuf_init(&state->root, 0);
git_apply_config();
if (apply_default_whitespace && parse_whitespace_option(state, apply_default_whitespace))
git_apply_config(repo);
if (cfg->apply_default_whitespace &&
parse_whitespace_option(state, cfg->apply_default_whitespace))
return -1;
if (apply_default_ignorewhitespace && parse_ignorewhitespace_option(state, apply_default_ignorewhitespace))
if (cfg->apply_default_ignorewhitespace &&
parse_ignorewhitespace_option(state, cfg->apply_default_ignorewhitespace))
return -1;
return 0;
}
@@ -192,7 +203,8 @@ int check_apply_state(struct apply_state *state, int force_apply)
static void set_default_whitespace_mode(struct apply_state *state)
{
if (!state->whitespace_option && !apply_default_whitespace)
if (!state->whitespace_option &&
!repo_config_values(state->repo)->apply_default_whitespace)
state->ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);
}

View File

@@ -49,8 +49,6 @@ int assume_unchanged;
int is_bare_repository_cfg = -1; /* unspecified */
char *git_commit_encoding;
char *git_log_output_encoding;
char *apply_default_whitespace;
char *apply_default_ignorewhitespace;
int fsync_object_files = -1;
int use_fsync = -1;
enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
@@ -726,6 +724,8 @@ void repo_config_values_init(struct repo_config_values *cfg)
cfg->editor_program = NULL;
cfg->pager_program = NULL;
cfg->askpass_program = NULL;
cfg->apply_default_whitespace = NULL;
cfg->apply_default_ignorewhitespace = NULL;
cfg->apply_sparse_checkout = 0;
cfg->branch_track = BRANCH_TRACK_REMOTE;
cfg->trust_ctime = 1;
@@ -745,4 +745,6 @@ void repo_config_values_clear(struct repo_config_values *cfg)
FREE_AND_NULL(cfg->editor_program);
FREE_AND_NULL(cfg->pager_program);
FREE_AND_NULL(cfg->askpass_program);
FREE_AND_NULL(cfg->apply_default_whitespace);
FREE_AND_NULL(cfg->apply_default_ignorewhitespace);
}

View File

@@ -94,6 +94,8 @@ struct repo_config_values {
char *editor_program;
char *pager_program;
char *askpass_program;
char *apply_default_whitespace;
char *apply_default_ignorewhitespace;
int apply_sparse_checkout;
int trust_ctime;
int check_stat;
@@ -182,8 +184,6 @@ extern int has_symlinks;
extern int minimum_abbrev, default_abbrev;
extern int ignore_case;
extern int assume_unchanged;
extern char *apply_default_whitespace;
extern char *apply_default_ignorewhitespace;
extern unsigned long pack_size_limit_cfg;
extern int protect_hfs;