wrap shared_repository global in get/set accessors

It would be useful to control access to the global shared_repository, so that we can lazily load its config. The first step to doing so is to make sure all access goes through a set of functions. This step is purely mechanical, and should result in no change of behavior. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Mar 11, 2016 at 17:36 UTC 7875acb6ecf85a7dc29554d193955ce5e265764f
5 files changed +33 -20
builtin/init-db.c
+12 -12
@@ -199,13 +199,13 @@ static int create_default_files(const char *template_path)
199
200 /* reading existing config may have overwrote it */
201 if (init_shared_repository != -1)
202 - shared_repository = init_shared_repository;
202 + set_shared_repository(init_shared_repository);
203
204 /*
205 * We would have created the above under user's umask -- under
206 * shared-repository settings, we would need to fix them up.
207 */
208 - if (shared_repository) {
208 + if (get_shared_repository()) {
209 adjust_shared_perm(get_git_dir());
210 adjust_shared_perm(git_path_buf(&buf, "refs"));
211 adjust_shared_perm(git_path_buf(&buf, "refs/heads"));
@@ -369,7 +369,7 @@ int init_db(const char *template_dir, unsigned int flags)
369
370 create_object_directory();
371
372 - if (shared_repository) {
372 + if (get_shared_repository()) {
373 char buf[10];
374 /* We do not spell "group" and such, so that
375 * the configuration can be read by older version
@@ -377,12 +377,12 @@ int init_db(const char *template_dir, unsigned int flags)
377 * and compatibility values for PERM_GROUP and
378 * PERM_EVERYBODY.
379 */
380 - if (shared_repository < 0)
380 + if (get_shared_repository() < 0)
381 /* force to the mode value */
382 - xsnprintf(buf, sizeof(buf), "0%o", -shared_repository);
383 - else if (shared_repository == PERM_GROUP)
382 + xsnprintf(buf, sizeof(buf), "0%o", -get_shared_repository());
383 + else if (get_shared_repository() == PERM_GROUP)
384 xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_GROUP);
385 - else if (shared_repository == PERM_EVERYBODY)
385 + else if (get_shared_repository() == PERM_EVERYBODY)
386 xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_EVERYBODY);
387 else
388 die("BUG: invalid value for shared_repository");
@@ -398,7 +398,7 @@ int init_db(const char *template_dir, unsigned int flags)
398 "", and the last '%s%s' is the verbatim directory name. */
399 printf(_("%s%s Git repository in %s%s\n"),
400 reinit ? _("Reinitialized existing") : _("Initialized empty"),
401 - shared_repository ? _(" shared") : "",
401 + get_shared_repository() ? _(" shared") : "",
402 git_dir, len && git_dir[len-1] != '/' ? "/" : "");
403 }
404
@@ -493,8 +493,8 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
493 * and we know shared_repository should always be 0;
494 * but just in case we play safe.
495 */
496 - saved = shared_repository;
497 - shared_repository = 0;
496 + saved = get_shared_repository();
497 + set_shared_repository(0);
498 switch (safe_create_leading_directories_const(argv[0])) {
499 case SCLD_OK:
500 case SCLD_PERMS:
@@ -506,7 +506,7 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
506 die_errno(_("cannot mkdir %s"), argv[0]);
507 break;
508 }
509 - shared_repository = saved;
509 + set_shared_repository(saved);
510 if (mkdir(argv[0], 0777) < 0)
511 die_errno(_("cannot mkdir %s"), argv[0]);
512 mkdir_tried = 1;
@@ -524,7 +524,7 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
524 }
525
526 if (init_shared_repository != -1)
527 - shared_repository = init_shared_repository;
527 + set_shared_repository(init_shared_repository);
528
529 /*
530 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
cache.h
+3 -1
@@ -651,7 +651,6 @@ extern int prefer_symlink_refs;
651 extern int log_all_ref_updates;
652 extern int warn_ambiguous_refs;
653 extern int warn_on_object_refname_ambiguity;
654 -extern int shared_repository;
654 extern const char *apply_default_whitespace;
655 extern const char *apply_default_ignorewhitespace;
656 extern const char *git_attributes_file;
@@ -664,6 +663,9 @@ extern size_t delta_base_cache_limit;
663 extern unsigned long big_file_threshold;
664 extern unsigned long pack_size_limit_cfg;
665
666 +void set_shared_repository(int value);
667 +int get_shared_repository(void);
668 +
669 /*
670 * Do replace refs need to be checked this run? This variable is
671 * initialized to true unless --no-replace-object is used or
environment.c
+12 -1
@@ -29,7 +29,6 @@ int repository_format_version;
29 int repository_format_precious_objects;
30 const char *git_commit_encoding;
31 const char *git_log_output_encoding;
32 -int shared_repository = PERM_UMASK;
32 const char *apply_default_whitespace;
33 const char *apply_default_ignorewhitespace;
34 const char *git_attributes_file;
@@ -325,3 +324,15 @@ const char *get_commit_output_encoding(void)
324 {
325 return git_commit_encoding ? git_commit_encoding : "UTF-8";
326 }
327 +
328 +static int the_shared_repository = PERM_UMASK;
329 +
330 +void set_shared_repository(int value)
331 +{
332 + the_shared_repository = value;
333 +}
334 +
335 +int get_shared_repository(void)
336 +{
337 + return the_shared_repository;
338 +}
path.c
+5 -5
@@ -699,17 +699,17 @@ static int calc_shared_perm(int mode)
699 {
700 int tweak;
701
702 - if (shared_repository < 0)
703 - tweak = -shared_repository;
702 + if (get_shared_repository() < 0)
703 + tweak = -get_shared_repository();
704 else
705 - tweak = shared_repository;
705 + tweak = get_shared_repository();
706
707 if (!(mode & S_IWUSR))
708 tweak &= ~0222;
709 if (mode & S_IXUSR)
710 /* Copy read bits to execute bits */
711 tweak |= (tweak & 0444) >> 2;
712 - if (shared_repository < 0)
712 + if (get_shared_repository() < 0)
713 mode = (mode & ~0777) | tweak;
714 else
715 mode |= tweak;
@@ -722,7 +722,7 @@ int adjust_shared_perm(const char *path)
722 {
723 int old_mode, new_mode;
724
725 - if (!shared_repository)
725 + if (!get_shared_repository())
726 return 0;
727 if (get_st_mode_bits(path, &old_mode) < 0)
728 return -1;
setup.c
+1 -1
@@ -377,7 +377,7 @@ static int check_repo_format(const char *var, const char *value, void *cb)
377 if (strcmp(var, "core.repositoryformatversion") == 0)
378 repository_format_version = git_config_int(var, value);
379 else if (strcmp(var, "core.sharedrepository") == 0)
380 - shared_repository = git_config_perm(var, value);
380 + set_shared_repository(git_config_perm(var, value));
381 else if (skip_prefix(var, "extensions.", &ext)) {
382 /*
383 * record any known extensions here; otherwise,