Skip to content

Commit 1087e7f

Browse files
malon7782gitster
authored andcommitted
environment: move excludes_file into repo_config_values
The global variable 'excludes_file' is used to track the path to the global ignore file. If this variable is NULL, 'setup_standard_excludes()' in 'dir.c' forcefully evaluates and assigns the XDG default path to it. Continue the libification effort by encapsulating this lazy-loading fallback logic into a proper getter and moving the variable into 'struct repo_config_values'. Since 'excludes_file' is a dynamically allocated string, it requires proper heap memory management. Introduce repo_config_values_clear() and wire it up in 'repo_clear()' to safely free this memory when a repository instance is destroyed. Also clean up the heap-allocated 'attributes_file' in this new destructor while we are at it. Note on transition: Defensive checks are temporarily retained in both the getter (returning NULL if uninitialized) and the destructor (bypassing non-the_repository instances) to maintain bug-to-bug compatibility. These are marked with NEEDSWORK comments. Future work will track down and fix the offending callers to eventually replace these shields with stricter BUG() assertions. 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>
1 parent ab776a6 commit 1087e7f

4 files changed

Lines changed: 55 additions & 6 deletions

File tree

dir.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3481,11 +3481,11 @@ static GIT_PATH_FUNC(git_path_info_exclude, "info/exclude")
34813481

34823482
void setup_standard_excludes(struct dir_struct *dir)
34833483
{
3484+
const char *excludes_file = repo_excludes_file(the_repository);
3485+
34843486
dir->exclude_per_dir = ".gitignore";
34853487

34863488
/* core.excludesfile defaulting to $XDG_CONFIG_HOME/git/ignore */
3487-
if (!excludes_file)
3488-
excludes_file = xdg_config_home("ignore");
34893489
if (excludes_file && !access_or_warn(excludes_file, R_OK, 0))
34903490
add_patterns_from_file_1(dir, excludes_file,
34913491
dir->untracked ? &dir->internal.ss_excludes_file : NULL);

environment.c

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
5757
enum fsync_component fsync_components = FSYNC_COMPONENTS_DEFAULT;
5858
char *editor_program;
5959
char *askpass_program;
60-
char *excludes_file;
6160
enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
6261
enum eol core_eol = EOL_UNSET;
6362
int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
@@ -134,6 +133,24 @@ int is_bare_repository(void)
134133
return is_bare_repository_cfg && !repo_get_work_tree(the_repository);
135134
}
136135

136+
const char *repo_excludes_file(struct repository *repo)
137+
{
138+
/*
139+
* NEEDSWORK: This is a temporary shield to maintain bug-to-bug
140+
* compatibility during the libification transition.
141+
*
142+
* Once offending callers are properly fixed, this check should
143+
* be upgraded to a BUG() assertion and eventually removed entirely.
144+
*/
145+
if (!repo || !repo->initialized)
146+
return NULL;
147+
148+
if (!repo_config_values(repo)->excludes_file)
149+
repo_config_values(repo)->excludes_file = xdg_config_home("ignore");
150+
151+
return repo_config_values(repo)->excludes_file;
152+
}
153+
137154
int have_git_dir(void)
138155
{
139156
return startup_info->have_repository
@@ -461,8 +478,8 @@ int git_default_core_config(const char *var, const char *value,
461478
}
462479

463480
if (!strcmp(var, "core.excludesfile")) {
464-
FREE_AND_NULL(excludes_file);
465-
return git_config_pathname(&excludes_file, var, value);
481+
FREE_AND_NULL(cfg->excludes_file);
482+
return git_config_pathname(&cfg->excludes_file, var, value);
466483
}
467484

468485
if (!strcmp(var, "core.whitespace")) {
@@ -715,6 +732,7 @@ int git_default_config(const char *var, const char *value,
715732
void repo_config_values_init(struct repo_config_values *cfg)
716733
{
717734
cfg->attributes_file = NULL;
735+
cfg->excludes_file = NULL;
718736
cfg->apply_sparse_checkout = 0;
719737
cfg->branch_track = BRANCH_TRACK_REMOTE;
720738
cfg->trust_ctime = 1;
@@ -726,3 +744,22 @@ void repo_config_values_init(struct repo_config_values *cfg)
726744
cfg->sparse_expect_files_outside_of_patterns = 0;
727745
cfg->warn_on_object_refname_ambiguity = 1;
728746
}
747+
748+
void repo_config_values_clear(struct repository *repo)
749+
{
750+
struct repo_config_values *cfg;
751+
752+
/*
753+
* NEEDSWORK: Temporary shield to prevent temporary/uninitialized
754+
* submodules from triggering the BUG() at repository.c:59
755+
* during repo_clear(). This should be removed once submodule
756+
* lifecycle and per-repo config support are fully resolved.
757+
*/
758+
if (repo != the_repository)
759+
return;
760+
761+
cfg = repo_config_values(repo);
762+
763+
FREE_AND_NULL(cfg->attributes_file);
764+
FREE_AND_NULL(cfg->excludes_file);
765+
}

environment.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ struct repository;
9090
struct repo_config_values {
9191
/* section "core" config values */
9292
char *attributes_file;
93+
char *excludes_file;
9394
int apply_sparse_checkout;
9495
int trust_ctime;
9596
int check_stat;
@@ -133,8 +134,19 @@ int git_default_config(const char *, const char *,
133134
int git_default_core_config(const char *var, const char *value,
134135
const struct config_context *ctx, void *cb);
135136

137+
const char *repo_excludes_file(struct repository *repo);
138+
136139
void repo_config_values_init(struct repo_config_values *cfg);
137140

141+
/*
142+
* Frees memory allocated for dynamically loaded configuration values
143+
* inside `repo_config_values`.
144+
*
145+
* As dynamically allocated variables are migrated into this struct,
146+
* their FREE_AND_NULL() calls should be appended here.
147+
*/
148+
void repo_config_values_clear(struct repository *repo);
149+
138150
/*
139151
* TODO: All the below state either explicitly or implicitly relies on
140152
* `the_repository`. We should eventually get rid of these and make the
@@ -208,7 +220,6 @@ extern char *git_log_output_encoding;
208220

209221
extern char *editor_program;
210222
extern char *askpass_program;
211-
extern char *excludes_file;
212223

213224
/*
214225
* The character that begins a commented line in user-editable file

repository.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ void repo_clear(struct repository *repo)
388388
FREE_AND_NULL(repo->parsed_objects);
389389

390390
repo_settings_clear(repo);
391+
repo_config_values_clear(repo);
391392

392393
if (repo->config) {
393394
git_configset_clear(repo->config);

0 commit comments

Comments
 (0)