From 3976e7eb4104fb18b2d099299203885612bbb72e Mon Sep 17 00:00:00 2001 From: Tian Yuchen Date: Wed, 24 Jun 2026 00:44:39 +0800 Subject: [PATCH 1/2] commit #1 migrate the lazy parsing logic of setup_standard_excludes() in dir.c into a newly introduced getter in environment.c the reason for this is that I thought dir.c/setup_standard_excludes() might not be the best place to call xdg_config_home(). is it? Signed-off-by: Tian Yuchen --- dir.c | 4 ++-- environment.c | 7 +++++++ environment.h | 6 ++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/dir.c b/dir.c index 7a73690fbc7440..d6cb43efd8a3ac 100644 --- a/dir.c +++ b/dir.c @@ -3481,11 +3481,11 @@ static GIT_PATH_FUNC(git_path_info_exclude, "info/exclude") void setup_standard_excludes(struct dir_struct *dir) { + const char *excludes_file; dir->exclude_per_dir = ".gitignore"; /* core.excludesfile defaulting to $XDG_CONFIG_HOME/git/ignore */ - if (!excludes_file) - excludes_file = xdg_config_home("ignore"); + excludes_file = repo_excludes_file(the_repository); if (excludes_file && !access_or_warn(excludes_file, R_OK, 0)) add_patterns_from_file_1(dir, excludes_file, dir->untracked ? &dir->internal.ss_excludes_file : NULL); diff --git a/environment.c b/environment.c index ba2c60103ff51c..8efcaeafa6fe31 100644 --- a/environment.c +++ b/environment.c @@ -134,6 +134,13 @@ int is_bare_repository(void) return is_bare_repository_cfg && !repo_get_work_tree(the_repository); } +const char *repo_excludes_file(struct repository *repo) +{ + if (!excludes_file) + excludes_file = xdg_config_home("ignore"); + return excludes_file; +} + int have_git_dir(void) { return startup_info->have_repository diff --git a/environment.h b/environment.h index 6f182869558395..a8a9a3fa60938d 100644 --- a/environment.h +++ b/environment.h @@ -133,6 +133,12 @@ int git_default_config(const char *, const char *, int git_default_core_config(const char *var, const char *value, const struct config_context *ctx, void *cb); +/* + * TODO: This still relies on the global state. + * It will be moved to repo_config_values in the subsequent commit. + */ +const char *repo_excludes_file(struct repository *repo); + void repo_config_values_init(struct repo_config_values *cfg); /* From 360d046ff35899b979096e0bb15fd8393cc7e7d6 Mon Sep 17 00:00:00 2001 From: Tian Yuchen Date: Wed, 24 Jun 2026 01:09:02 +0800 Subject: [PATCH 2/2] commit #2 pass in the repo context in order to eliminate the global state introduce repo_config_values_clear() Signed-off-by: Tian Yuchen --- environment.c | 23 +++++++++++++++++------ environment.h | 4 +++- repository.c | 1 + 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/environment.c b/environment.c index 8efcaeafa6fe31..9369035e4b3481 100644 --- a/environment.c +++ b/environment.c @@ -57,7 +57,6 @@ enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT; enum fsync_component fsync_components = FSYNC_COMPONENTS_DEFAULT; char *editor_program; char *askpass_program; -char *excludes_file; enum auto_crlf auto_crlf = AUTO_CRLF_FALSE; enum eol core_eol = EOL_UNSET; int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN; @@ -136,9 +135,13 @@ int is_bare_repository(void) const char *repo_excludes_file(struct repository *repo) { - if (!excludes_file) - excludes_file = xdg_config_home("ignore"); - return excludes_file; + if (!repo || !repo->initialized) + return NULL; + + if (!repo_config_values(repo)->excludes_file) + repo_config_values(repo)->excludes_file = xdg_config_home("ignore"); + + return repo_config_values(repo)->excludes_file; } int have_git_dir(void) @@ -468,8 +471,8 @@ int git_default_core_config(const char *var, const char *value, } if (!strcmp(var, "core.excludesfile")) { - FREE_AND_NULL(excludes_file); - return git_config_pathname(&excludes_file, var, value); + FREE_AND_NULL(cfg->excludes_file); + return git_config_pathname(&cfg->excludes_file, var, value); } if (!strcmp(var, "core.whitespace")) { @@ -733,3 +736,11 @@ void repo_config_values_init(struct repo_config_values *cfg) cfg->sparse_expect_files_outside_of_patterns = 0; cfg->warn_on_object_refname_ambiguity = 1; } + +void repo_config_values_clear(struct repository *repo) +{ + struct repo_config_values *cfg = repo_config_values(repo); + if (!cfg) + return; + FREE_AND_NULL(cfg->excludes_file); +} diff --git a/environment.h b/environment.h index a8a9a3fa60938d..cc818517105c2c 100644 --- a/environment.h +++ b/environment.h @@ -98,6 +98,7 @@ struct repo_config_values { int precomposed_unicode; int core_sparse_checkout_cone; int warn_on_object_refname_ambiguity; + char *excludes_file; /* section "sparse" config values */ int sparse_expect_files_outside_of_patterns; @@ -141,6 +142,8 @@ const char *repo_excludes_file(struct repository *repo); void repo_config_values_init(struct repo_config_values *cfg); +void repo_config_values_clear(struct repository *repo); + /* * TODO: All the below state either explicitly or implicitly relies on * `the_repository`. We should eventually get rid of these and make the @@ -214,7 +217,6 @@ extern char *git_log_output_encoding; extern char *editor_program; extern char *askpass_program; -extern char *excludes_file; /* * The character that begins a commented line in user-editable file diff --git a/repository.c b/repository.c index 187dd471c4e607..b31f1b785288a8 100644 --- a/repository.c +++ b/repository.c @@ -388,6 +388,7 @@ void repo_clear(struct repository *repo) FREE_AND_NULL(repo->parsed_objects); repo_settings_clear(repo); + repo_config_values_clear(repo); if (repo->config) { git_configset_clear(repo->config);