]> git.ipfire.org Git - thirdparty/git.git/commitdiff
environment: migrate apply_default_whitespace and apply_default_ignorewhitespace
authorTian Yuchen <cat@malon.dev>
Tue, 14 Jul 2026 03:25:21 +0000 (11:25 +0800)
committerJunio C Hamano <gitster@pobox.com>
Tue, 14 Jul 2026 14:30:23 +0000 (07:30 -0700)
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>
apply.c
environment.c
environment.h

diff --git a/apply.c b/apply.c
index 5e54453f799ea2ccc736e4f213641e4bdd95a07d..7e5b12323ea79fb9717c2e9bd5f3a1ff77946471 100644 (file)
--- a/apply.c
+++ b/apply.c
@@ -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);
 }
 
index 3857818da3f96c500d10f324c4fe82ea200b453f..20500658a20d679455711b39c1020fbef7b50b6d 100644 (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);
 }
index 856dc70cc47170c4d121b32750cff99f11d81b6f..f450242ac03badbeba2398bf8403e090cb0dafb5 100644 (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;