repo-settings: add repo_settings_get_is_bare
RFC PATCH v3 1/2 repo-settings: add repo_settings_get_is_bare
The is_bare_repository() function relies on the global the_repository variable, making it unsuitable for use in library code that may operate on arbitrary repositories. Additionally, calling repo_config_get_bool repeatedly to check core.bare can be expensive if done frequently.
Add a lazy-loaded is_bare field to struct repo_settings and expose it via repo_settings_get_is_bare(). This allows call sites to check bareness cheaply and correctly using a repository context.
--- repo-settings.c | 12 ++++++++++++ repo-settings.h | 5 +++++ 2 files changed, 17 insertions(+)
repo-settings.c +12 -0
@@ -233,3 +233,15 @@ void repo_settings_reset_shared_repository(struct repository *repo) { repo->settings.shared_repository_initialized =0 ; } + +int repo_settings_get_is_bare (struct repository *repo) +{ +if (repo->settings.is_bare <0 ) { +int is_bare_cfg =0 ; +if (repo_config_get_bool (repo,"core.bare" , &is_bare_cfg)) + repo->settings.is_bare = !repo_get_work_tree (repo); +else + repo->settings.is_bare = is_bare_cfg && !repo_get_work_tree (repo); + } +return repo->settings.is_bare; +}
repo-settings.h +5 -0
@@ -26,6 +26,7 @@ enum log_refs_config {struct repo_settings {int initialized; +int is_bare;int core_commit_graph;int commit_graph_generation_version;int commit_graph_changed_paths_version;
@@ -74,6 +75,7 @@ struct repo_settings {#define REPO_SETTINGS_INIT { \ .shared_repository = -1, \ .index_version = -1, \ + .is_bare = -1, \ .core_untracked_cache = UNTRACKED_CACHE_KEEP, \ .fetch_negotiation_algorithm = FETCH_NEGOTIATION_CONSECUTIVE, \ .warn_ambiguous_refs = -1, \
@@ -102,4 +104,7 @@ int repo_settings_get_shared_repository(struct repository *repo);void repo_settings_set_shared_repository (struct repository *repo,int value);void repo_settings_reset_shared_repository (struct repository *repo); +/* Read and set the value for "core.bare". */ +int repo_settings_get_is_bare (struct repository *repo); +#endif /* REPO_SETTINGS_H */
Signature · 1 line
2.53.0.windows.1
RFC PATCH v3 2/2 attr: use local repository state in read_attr
The read_attr function currently relies on is_bare_repository() to decide whether to check the index or working tree for attributes. This function implicitly depends on the_repository, which is incorrect for library code handling secondary repositories.
Update read_attr to use the new repo_settings_get_is_bare(istate->repo)
helper. This ensures the logic respects the context of the specific repository associated with the index, while also benefiting from the lazy-loading optimization added in the previous commit.
--- attr.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
attr.c +2 -1
@@ -23,6 +23,7 @@#include "refs.h" #include "revision.h" #include "odb.h" +#include "repo-settings.h" #include "setup.h" #include "thread-utils.h" #include "tree-walk.h"
@@ -848,7 +849,7 @@ static struct attr_stack *read_attr(struct index_state *istate, res =read_attr_from_index (istate, path, flags); }else if (tree_oid) { res =read_attr_from_blob (istate, tree_oid, path, flags); - }else if (!is_bare_repository ()) { + }else if (!repo_settings_get_is_bare (istate->repo)) {if (direction ==GIT_ATTR_CHECKOUT ) { res =read_attr_from_index (istate, path, flags);if (!res)
Signature · 1 line
2.53.0.windows.1