Skip to content

Commit 7f9f260

Browse files
committed
environment: move "core.attributesfile" into repo-setting
When handling multiple repositories within the same process, relying on global state for accessing the "core.attributesfile" configuration can lead to incorrect values being used. It also makes it harder to isolate repositories and hinders the libification of git. The functions `bootstrap_attr_stack()` and `git_attr_val_system()` retrieve "core.attributesfile" via `git_attr_global_file()` which reads from global state `git_attributes_file`. Move the "core.attributesfile" configuration into the `struct repo_settings` instead of relying on the global state. A new function `repo_settings_get_attributesfile_path()` is added and used to retrieve this setting in a repository-scoped manner. The functions to retrieve "core.attributesfile" are replaced with the new accessor function `repo_settings_get_attributesfile_path()` This improves multi-repository behaviour and aligns with the goal of libifying of Git. Note that in `bootstrap_attr_stack()`, the `index_state` is used only if it exists, else we default to `the_repository`. Reported-by: Ayush Chandekar <ayu.chandekar@gmail.com> Mentored-by: Christian Couder <christian.couder@gmail.com> Mentored-by: Usman Akinyemi <usmanakinyemi202@gmail.com> Signed-off-by: Olamide Caleb Bello <belkid98@gmail.com>
1 parent e85ae27 commit 7f9f260

File tree

7 files changed

+28
-21
lines changed

7 files changed

+28
-21
lines changed

attr.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -879,14 +879,6 @@ const char *git_attr_system_file(void)
879879
return system_wide;
880880
}
881881

882-
const char *git_attr_global_file(void)
883-
{
884-
if (!git_attributes_file)
885-
git_attributes_file = xdg_config_home("attributes");
886-
887-
return git_attributes_file;
888-
}
889-
890882
int git_attr_system_is_enabled(void)
891883
{
892884
return !git_env_bool("GIT_ATTR_NOSYSTEM", 0);
@@ -912,6 +904,8 @@ static void bootstrap_attr_stack(struct index_state *istate,
912904
{
913905
struct attr_stack *e;
914906
unsigned flags = READ_ATTR_MACRO_OK;
907+
const char *attributes_file_path;
908+
struct repository *repo;
915909

916910
if (*stack)
917911
return;
@@ -927,8 +921,13 @@ static void bootstrap_attr_stack(struct index_state *istate,
927921
}
928922

929923
/* home directory */
930-
if (git_attr_global_file()) {
931-
e = read_attr_from_file(git_attr_global_file(), flags);
924+
if (istate && istate->repo)
925+
repo = istate->repo;
926+
else
927+
repo = the_repository;
928+
attributes_file_path = repo_settings_get_attributesfile_path(repo);
929+
if (attributes_file_path) {
930+
e = read_attr_from_file(attributes_file_path, flags);
932931
push_stack(stack, e, NULL, 0);
933932
}
934933

attr.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,6 @@ void attr_start(void);
232232
/* Return the system gitattributes file. */
233233
const char *git_attr_system_file(void);
234234

235-
/* Return the global gitattributes file, if any. */
236-
const char *git_attr_global_file(void);
237-
238235
/* Return whether the system gitattributes file is enabled and should be used. */
239236
int git_attr_system_is_enabled(void);
240237

builtin/var.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ static char *git_attr_val_system(int ident_flag UNUSED)
7272

7373
static char *git_attr_val_global(int ident_flag UNUSED)
7474
{
75-
char *file = xstrdup_or_null(git_attr_global_file());
75+
char *file = xstrdup_or_null(repo_settings_get_attributesfile_path(the_repository));
7676
if (file) {
7777
normalize_path_copy(file, file);
7878
return file;

environment.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ char *git_commit_encoding;
5353
char *git_log_output_encoding;
5454
char *apply_default_whitespace;
5555
char *apply_default_ignorewhitespace;
56-
char *git_attributes_file;
5756
int zlib_compression_level = Z_BEST_SPEED;
5857
int pack_compression_level = Z_DEFAULT_COMPRESSION;
5958
int fsync_object_files = -1;
@@ -363,11 +362,6 @@ static int git_default_core_config(const char *var, const char *value,
363362
return 0;
364363
}
365364

366-
if (!strcmp(var, "core.attributesfile")) {
367-
FREE_AND_NULL(git_attributes_file);
368-
return git_config_pathname(&git_attributes_file, var, value);
369-
}
370-
371365
if (!strcmp(var, "core.bare")) {
372366
is_bare_repository_cfg = git_config_bool(var, value);
373367
return 0;

environment.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ extern int assume_unchanged;
152152
extern int warn_on_object_refname_ambiguity;
153153
extern char *apply_default_whitespace;
154154
extern char *apply_default_ignorewhitespace;
155-
extern char *git_attributes_file;
156155
extern int zlib_compression_level;
157156
extern int pack_compression_level;
158157
extern unsigned long pack_size_limit_cfg;

repo-settings.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "midx.h"
66
#include "pack-objects.h"
77
#include "setup.h"
8+
#include "path.h"
89

910
static void repo_cfg_bool(struct repository *r, const char *key, int *dest,
1011
int def)
@@ -158,6 +159,7 @@ void repo_settings_clear(struct repository *r)
158159
struct repo_settings empty = REPO_SETTINGS_INIT;
159160
FREE_AND_NULL(r->settings.fsmonitor);
160161
FREE_AND_NULL(r->settings.hooks_path);
162+
FREE_AND_NULL(r->settings.git_attributes_file);
161163
r->settings = empty;
162164
}
163165

@@ -230,3 +232,11 @@ void repo_settings_reset_shared_repository(struct repository *repo)
230232
{
231233
repo->settings.shared_repository_initialized = 0;
232234
}
235+
const char *repo_settings_get_attributesfile_path(struct repository *repo)
236+
{
237+
if (!repo->settings.git_attributes_file) {
238+
if (repo_config_get_pathname(repo, "core.attributesfile", &repo->settings.git_attributes_file))
239+
repo->settings.git_attributes_file = xdg_config_home("attributes");
240+
}
241+
return repo->settings.git_attributes_file;
242+
}

repo-settings.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ struct repo_settings {
6868
unsigned long big_file_threshold;
6969

7070
char *hooks_path;
71+
char *git_attributes_file;
7172
};
7273
#define REPO_SETTINGS_INIT { \
7374
.shared_repository = -1, \
@@ -99,4 +100,11 @@ int repo_settings_get_shared_repository(struct repository *repo);
99100
void repo_settings_set_shared_repository(struct repository *repo, int value);
100101
void repo_settings_reset_shared_repository(struct repository *repo);
101102

103+
/*
104+
* Read the value for "core.attributesfile".
105+
* Defaults to xdg_config_home("attributes") if the core.attributesfile
106+
* isn't available.
107+
*/
108+
const char *repo_settings_get_attributesfile_path(struct repository *repo);
109+
102110
#endif /* REPO_SETTINGS_H */

0 commit comments

Comments
 (0)