environment: move access to "core.sharedRepository" into repo settings

Similar as with the preceding commit, we track "core.sharedRepository" via a pair of global variables. Move them into `struct repo_settings` so that we can instead track them per-repository. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Feb 7, 2025 at 12:03 UTC f1ce861c34bffbc02998173016b0bca0f6d9f6c4
8 files changed +58 -58
builtin/init-db.c
+4 -4
@@ -132,8 +132,8 @@ int cmd_init_db(int argc,
132 * and we know shared_repository should always be 0;
133 * but just in case we play safe.
134 */
135 - saved = get_shared_repository();
136 - set_shared_repository(0);
135 + saved = repo_settings_get_shared_repository(the_repository);
136 + repo_settings_set_shared_repository(the_repository, 0);
137 switch (safe_create_leading_directories_const(argv[0])) {
138 case SCLD_OK:
139 case SCLD_PERMS:
@@ -145,7 +145,7 @@ int cmd_init_db(int argc,
145 die_errno(_("cannot mkdir %s"), argv[0]);
146 break;
147 }
148 - set_shared_repository(saved);
148 + repo_settings_set_shared_repository(the_repository, saved);
149 if (mkdir(argv[0], 0777) < 0)
150 die_errno(_("cannot mkdir %s"), argv[0]);
151 mkdir_tried = 1;
@@ -175,7 +175,7 @@ int cmd_init_db(int argc,
175 }
176
177 if (init_shared_repository != -1)
178 - set_shared_repository(init_shared_repository);
178 + repo_settings_set_shared_repository(the_repository, init_shared_repository);
179
180 /*
181 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
builtin/log.c
+3 -3
@@ -2309,8 +2309,8 @@ int cmd_format_patch(int argc,
2309 * We consider <outdir> as 'outside of gitdir', therefore avoid
2310 * applying adjust_shared_perm in s-c-l-d.
2311 */
2312 - saved = get_shared_repository();
2313 - set_shared_repository(0);
2312 + saved = repo_settings_get_shared_repository(the_repository);
2313 + repo_settings_set_shared_repository(the_repository, 0);
2314 switch (safe_create_leading_directories_const(output_directory)) {
2315 case SCLD_OK:
2316 case SCLD_EXISTS:
@@ -2319,7 +2319,7 @@ int cmd_format_patch(int argc,
2319 die(_("could not create leading directories "
2320 "of '%s'"), output_directory);
2321 }
2322 - set_shared_repository(saved);
2322 + repo_settings_set_shared_repository(the_repository, saved);
2323 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
2324 die_errno(_("could not create directory '%s'"),
2325 output_directory);
environment.c
-26
@@ -206,32 +206,6 @@ const char *get_commit_output_encoding(void)
206 return git_commit_encoding ? git_commit_encoding : "UTF-8";
207 }
208
209 -static int the_shared_repository = PERM_UMASK;
210 -static int need_shared_repository_from_config = 1;
211 -
212 -void set_shared_repository(int value)
213 -{
214 - the_shared_repository = value;
215 - need_shared_repository_from_config = 0;
216 -}
217 -
218 -int get_shared_repository(void)
219 -{
220 - if (need_shared_repository_from_config) {
221 - const char *var = "core.sharedrepository";
222 - const char *value;
223 - if (!git_config_get_value(var, &value))
224 - the_shared_repository = git_config_perm(var, value);
225 - need_shared_repository_from_config = 0;
226 - }
227 - return the_shared_repository;
228 -}
229 -
230 -void reset_shared_repository(void)
231 -{
232 - need_shared_repository_from_config = 1;
233 -}
234 -
209 int use_optional_locks(void)
210 {
211 return git_env_bool(GIT_OPTIONAL_LOCKS_ENVIRONMENT, 1);
environment.h
-10
@@ -134,16 +134,6 @@ void setup_git_env(const char *git_dir);
134 */
135 int have_git_dir(void);
136
137 -/*
138 - * Accessors for the core.sharedrepository config which lazy-load the value
139 - * from the config (if not already set). The "reset" function can be
140 - * used to unset "set" or cached value, meaning that the value will be loaded
141 - * fresh from the config file on the next call to get_shared_repository().
142 - */
143 -void set_shared_repository(int value);
144 -int get_shared_repository(void);
145 -void reset_shared_repository(void);
146 -
137 extern int is_bare_repository_cfg;
138 int is_bare_repository(void);
139 extern char *git_work_tree_cfg;
path.c
+5 -5
@@ -844,17 +844,17 @@ int calc_shared_perm(int mode)
844 {
845 int tweak;
846
847 - if (get_shared_repository() < 0)
848 - tweak = -get_shared_repository();
847 + if (repo_settings_get_shared_repository(the_repository) < 0)
848 + tweak = -repo_settings_get_shared_repository(the_repository);
849 else
850 - tweak = get_shared_repository();
850 + tweak = repo_settings_get_shared_repository(the_repository);
851
852 if (!(mode & S_IWUSR))
853 tweak &= ~0222;
854 if (mode & S_IXUSR)
855 /* Copy read bits to execute bits */
856 tweak |= (tweak & 0444) >> 2;
857 - if (get_shared_repository() < 0)
857 + if (repo_settings_get_shared_repository(the_repository) < 0)
858 mode = (mode & ~0777) | tweak;
859 else
860 mode |= tweak;
@@ -867,7 +867,7 @@ int adjust_shared_perm(const char *path)
867 {
868 int old_mode, new_mode;
869
870 - if (!get_shared_repository())
870 + if (!repo_settings_get_shared_repository(the_repository))
871 return 0;
872 if (get_st_mode_bits(path, &old_mode) < 0)
873 return -1;
repo-settings.c
+26
@@ -4,6 +4,7 @@
4 #include "repository.h"
5 #include "midx.h"
6 #include "pack-objects.h"
7 +#include "setup.h"
8
9 static void repo_cfg_bool(struct repository *r, const char *key, int *dest,
10 int def)
@@ -181,3 +182,28 @@ const char *repo_settings_get_hooks_path(struct repository *repo)
182 repo_config_get_pathname(repo, "core.hookspath", &repo->settings.hooks_path);
183 return repo->settings.hooks_path;
184 }
185 +
186 +int repo_settings_get_shared_repository(struct repository *repo)
187 +{
188 + if (!repo->settings.shared_repository_initialized) {
189 + const char *var = "core.sharedrepository";
190 + const char *value;
191 + if (!repo_config_get_value(repo, var, &value))
192 + repo->settings.shared_repository = git_config_perm(var, value);
193 + else
194 + repo->settings.shared_repository = PERM_UMASK;
195 + repo->settings.shared_repository_initialized = 1;
196 + }
197 + return repo->settings.shared_repository;
198 +}
199 +
200 +void repo_settings_set_shared_repository(struct repository *repo, int value)
201 +{
202 + repo->settings.shared_repository = value;
203 + repo->settings.shared_repository_initialized = 1;
204 +}
205 +
206 +void repo_settings_reset_shared_repository(struct repository *repo)
207 +{
208 + repo->settings.shared_repository_initialized = 0;
209 +}
repo-settings.h
+9
@@ -37,6 +37,9 @@ struct repo_settings {
37 int pack_use_bitmap_boundary_traversal;
38 int pack_use_multi_pack_reuse;
39
40 + int shared_repository;
41 + int shared_repository_initialized;
42 +
43 /*
44 * Does this repository have core.useReplaceRefs=true (on by
45 * default)? This provides a repository-scoped version of this
@@ -65,6 +68,7 @@ struct repo_settings {
68 char *hooks_path;
69 };
70 #define REPO_SETTINGS_INIT { \
71 + .shared_repository = -1, \
72 .index_version = -1, \
73 .core_untracked_cache = UNTRACKED_CACHE_KEEP, \
74 .fetch_negotiation_algorithm = FETCH_NEGOTIATION_CONSECUTIVE, \
@@ -84,4 +88,9 @@ int repo_settings_get_warn_ambiguous_refs(struct repository *repo);
88 /* Read the value for "core.hooksPath". */
89 const char *repo_settings_get_hooks_path(struct repository *repo);
90
91 +/* Read, set or reset the value for "core.sharedRepository". */
92 +int repo_settings_get_shared_repository(struct repository *repo);
93 +void repo_settings_set_shared_repository(struct repository *repo, int value);
94 +void repo_settings_reset_shared_repository(struct repository *repo);
95 +
96 #endif /* REPO_SETTINGS_H */
setup.c
+11 -10
@@ -2332,7 +2332,7 @@ static int create_default_files(const char *template_path,
2332 */
2333 copy_templates(template_path);
2334 git_config_clear();
2335 - reset_shared_repository();
2335 + repo_settings_reset_shared_repository(the_repository);
2336 git_config(git_default_config, NULL);
2337
2338 reinit = is_reinit();
@@ -2342,7 +2342,8 @@ static int create_default_files(const char *template_path,
2342 * values we might have just re-read from the config.
2343 */
2344 if (init_shared_repository != -1)
2345 - set_shared_repository(init_shared_repository);
2345 + repo_settings_set_shared_repository(the_repository,
2346 + init_shared_repository);
2347
2348 is_bare_repository_cfg = !work_tree;
2349
@@ -2350,7 +2351,7 @@ static int create_default_files(const char *template_path,
2351 * We would have created the above under user's umask -- under
2352 * shared-repository settings, we would need to fix them up.
2353 */
2353 - if (get_shared_repository()) {
2354 + if (repo_settings_get_shared_repository(the_repository)) {
2355 adjust_shared_perm(repo_get_git_dir(the_repository));
2356 }
2357
@@ -2597,7 +2598,7 @@ int init_db(const char *git_dir, const char *real_git_dir,
2598 initial_branch, flags & INIT_DB_QUIET);
2599 create_object_directory();
2600
2600 - if (get_shared_repository()) {
2601 + if (repo_settings_get_shared_repository(the_repository)) {
2602 char buf[10];
2603 /* We do not spell "group" and such, so that
2604 * the configuration can be read by older version
@@ -2605,12 +2606,12 @@ int init_db(const char *git_dir, const char *real_git_dir,
2606 * and compatibility values for PERM_GROUP and
2607 * PERM_EVERYBODY.
2608 */
2608 - if (get_shared_repository() < 0)
2609 + if (repo_settings_get_shared_repository(the_repository) < 0)
2610 /* force to the mode value */
2610 - xsnprintf(buf, sizeof(buf), "0%o", -get_shared_repository());
2611 - else if (get_shared_repository() == PERM_GROUP)
2611 + xsnprintf(buf, sizeof(buf), "0%o", -repo_settings_get_shared_repository(the_repository));
2612 + else if (repo_settings_get_shared_repository(the_repository) == PERM_GROUP)
2613 xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_GROUP);
2613 - else if (get_shared_repository() == PERM_EVERYBODY)
2614 + else if (repo_settings_get_shared_repository(the_repository) == PERM_EVERYBODY)
2615 xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_EVERYBODY);
2616 else
2617 BUG("invalid value for shared_repository");
@@ -2622,12 +2623,12 @@ int init_db(const char *git_dir, const char *real_git_dir,
2623 int len = strlen(git_dir);
2624
2625 if (reinit)
2625 - printf(get_shared_repository()
2626 + printf(repo_settings_get_shared_repository(the_repository)
2627 ? _("Reinitialized existing shared Git repository in %s%s\n")
2628 : _("Reinitialized existing Git repository in %s%s\n"),
2629 git_dir, len && git_dir[len-1] != '/' ? "/" : "");
2630 else
2630 - printf(get_shared_repository()
2631 + printf(repo_settings_get_shared_repository(the_repository)
2632 ? _("Initialized empty shared Git repository in %s%s\n")
2633 : _("Initialized empty Git repository in %s%s\n"),
2634 git_dir, len && git_dir[len-1] != '/' ? "/" : "");