attrcache: make the attribute cache opaque

This minor refactoring helps remove some of the coupling that pieces
have to the attribute cache.
This commit is contained in:
Edward Thomson
2024-09-21 13:16:16 +01:00
parent 3b500a92ad
commit 419948ef23
4 changed files with 54 additions and 16 deletions

View File

@@ -384,6 +384,8 @@ static int attr_setup(
git_attr_file_source index_source = { GIT_ATTR_FILE_SOURCE_INDEX, NULL, GIT_ATTR_FILE, NULL };
git_attr_file_source head_source = { GIT_ATTR_FILE_SOURCE_HEAD, NULL, GIT_ATTR_FILE, NULL };
git_attr_file_source commit_source = { GIT_ATTR_FILE_SOURCE_COMMIT, NULL, GIT_ATTR_FILE, NULL };
git_attr_cache *attrcache;
const char *attr_cfg_file = NULL;
git_index *idx = NULL;
const char *workdir;
int error = 0;
@@ -407,8 +409,10 @@ static int attr_setup(
error = 0;
}
if ((error = preload_attr_file(repo, attr_session, NULL,
git_repository_attr_cache(repo)->cfg_attr_file)) < 0)
if ((attrcache = git_repository_attr_cache(repo)) != NULL)
attr_cfg_file = git_attr_cache_attributesfile(attrcache);
if ((error = preload_attr_file(repo, attr_session, NULL, attr_cfg_file)) < 0)
goto out;
if ((error = git_repository__item_path(&info, repo, GIT_REPOSITORY_ITEM_INFO)) < 0 ||
@@ -464,6 +468,7 @@ int git_attr_add_macro(
{
int error;
git_attr_rule *macro = NULL;
git_attr_cache *attrcache;
git_pool *pool;
GIT_ASSERT_ARG(repo);
@@ -475,7 +480,8 @@ int git_attr_add_macro(
macro = git__calloc(1, sizeof(git_attr_rule));
GIT_ERROR_CHECK_ALLOC(macro);
pool = &git_repository_attr_cache(repo)->pool;
attrcache = git_repository_attr_cache(repo);
pool = git_attr_cache_pool(attrcache);
macro->match.pattern = git_pool_strdup(pool, name);
GIT_ERROR_CHECK_ALLOC(macro->match.pattern);
@@ -631,6 +637,8 @@ static int collect_attr_files(
int error = 0;
git_str dir = GIT_STR_INIT, attrfile = GIT_STR_INIT;
const char *workdir = git_repository_workdir(repo);
git_attr_cache *attrcache;
const char *attr_cfg_file = NULL;
attr_walk_up_info info = { NULL };
GIT_ASSERT(!git_fs_path_is_absolute(path));
@@ -679,8 +687,13 @@ static int collect_attr_files(
if (error < 0)
goto cleanup;
if (git_repository_attr_cache(repo)->cfg_attr_file != NULL) {
error = push_attr_file(repo, attr_session, files, NULL, git_repository_attr_cache(repo)->cfg_attr_file);
if ((attrcache = git_repository_attr_cache(repo)) != NULL)
attr_cfg_file = git_attr_cache_attributesfile(attrcache);
if (attr_cfg_file) {
error = push_attr_file(repo, attr_session, files, NULL, attr_cfg_file);
if (error < 0)
goto cleanup;
}

View File

@@ -14,6 +14,30 @@
#include "ignore.h"
#include "path.h"
struct git_attr_cache {
char *cfg_attr_file; /* cached value of core.attributesfile */
char *cfg_excl_file; /* cached value of core.excludesfile */
git_strmap *files; /* hash path to git_attr_cache_entry records */
git_strmap *macros; /* hash name to vector<git_attr_assignment> */
git_mutex lock;
git_pool pool;
};
const char *git_attr_cache_attributesfile(git_attr_cache *cache)
{
return cache->cfg_attr_file;
}
const char *git_attr_cache_excludesfile(git_attr_cache *cache)
{
return cache->cfg_excl_file;
}
git_pool *git_attr_cache_pool(git_attr_cache *cache)
{
return &cache->pool;
}
GIT_INLINE(int) attr_cache_lock(git_attr_cache *cache)
{
GIT_UNUSED(cache); /* avoid warning if threading is off */

View File

@@ -15,17 +15,14 @@
#define GIT_ATTR_CONFIG "core.attributesfile"
#define GIT_IGNORE_CONFIG "core.excludesfile"
typedef struct {
char *cfg_attr_file; /* cached value of core.attributesfile */
char *cfg_excl_file; /* cached value of core.excludesfile */
git_strmap *files; /* hash path to git_attr_cache_entry records */
git_strmap *macros; /* hash name to vector<git_attr_assignment> */
git_mutex lock;
git_pool pool;
} git_attr_cache;
typedef struct git_attr_cache git_attr_cache;
extern int git_attr_cache__init(git_repository *repo);
extern const char *git_attr_cache_attributesfile(git_attr_cache *ac);
extern const char *git_attr_cache_excludesfile(git_attr_cache *ac);
extern git_pool *git_attr_cache_pool(git_attr_cache *ac);
/* get file - loading and reload as needed */
extern int git_attr_cache__get(
git_attr_file **file,

View File

@@ -296,6 +296,8 @@ int git_ignore__for_path(
{
int error = 0;
const char *workdir = git_repository_workdir(repo);
git_attr_cache *attrcache;
const char *excludes_file = NULL;
git_str infopath = GIT_STR_INIT;
GIT_ASSERT_ARG(repo);
@@ -358,10 +360,12 @@ int git_ignore__for_path(
}
/* load core.excludesfile */
if (git_repository_attr_cache(repo)->cfg_excl_file != NULL)
attrcache = git_repository_attr_cache(repo);
excludes_file = git_attr_cache_excludesfile(attrcache);
if (excludes_file != NULL)
error = push_ignore_file(
ignores, &ignores->ign_global, NULL,
git_repository_attr_cache(repo)->cfg_excl_file);
ignores, &ignores->ign_global, NULL, excludes_file);
cleanup:
git_str_dispose(&infopath);