repo-settings: consolidate some config settings

There are a few important config settings that are not loaded during git_default_config. These are instead loaded on-demand. Centralize these config options to a single scan, and store all of the values in a repo_settings struct. The values for each setting are initialized as negative to indicate "unset". This centralization will be particularly important in a later change to introduce "meta" config settings that change the defaults for these config settings. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Aug 13, 2019 at 11:37 UTC 7211b9e7534e021d7c46117ec0c64482e7930560
7 files changed +58 -19
Makefile
+1
@@ -964,6 +964,7 @@ LIB_OBJS += refspec.o
964 LIB_OBJS += ref-filter.o
965 LIB_OBJS += remote.o
966 LIB_OBJS += replace-object.o
967 +LIB_OBJS += repo-settings.o
968 LIB_OBJS += repository.o
969 LIB_OBJS += rerere.o
970 LIB_OBJS += resolve-undo.o
builtin/gc.c
+5 -7
@@ -41,7 +41,6 @@ static int aggressive_depth = 50;
41 static int aggressive_window = 250;
42 static int gc_auto_threshold = 6700;
43 static int gc_auto_pack_limit = 50;
44 -static int gc_write_commit_graph;
44 static int detach_auto = 1;
45 static timestamp_t gc_log_expire_time;
46 static const char *gc_log_expire = "1.day.ago";
@@ -148,7 +147,6 @@ static void gc_config(void)
147 git_config_get_int("gc.aggressivedepth", &aggressive_depth);
148 git_config_get_int("gc.auto", &gc_auto_threshold);
149 git_config_get_int("gc.autopacklimit", &gc_auto_pack_limit);
151 - git_config_get_bool("gc.writecommitgraph", &gc_write_commit_graph);
150 git_config_get_bool("gc.autodetach", &detach_auto);
151 git_config_get_expiry("gc.pruneexpire", &prune_expire);
152 git_config_get_expiry("gc.worktreepruneexpire", &prune_worktrees_expire);
@@ -685,11 +683,11 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
683 clean_pack_garbage();
684 }
685
688 - if (gc_write_commit_graph &&
689 - write_commit_graph_reachable(get_object_directory(),
690 - !quiet && !daemonized ? COMMIT_GRAPH_PROGRESS : 0,
691 - NULL))
692 - return 1;
686 + prepare_repo_settings(the_repository);
687 + if (the_repository->settings.gc_write_commit_graph == 1)
688 + write_commit_graph_reachable(get_object_directory(),
689 + !quiet && !daemonized ? COMMIT_GRAPH_PROGRESS : 0,
690 + NULL);
691
692 if (auto_gc && too_many_loose_objects())
693 warning(_("There are too many unreachable loose objects; "
builtin/pack-objects.c
+4 -4
@@ -2709,10 +2709,6 @@ static int git_pack_config(const char *k, const char *v, void *cb)
2709 use_bitmap_index_default = git_config_bool(k, v);
2710 return 0;
2711 }
2712 - if (!strcmp(k, "pack.usesparse")) {
2713 - sparse = git_config_bool(k, v);
2714 - return 0;
2715 - }
2712 if (!strcmp(k, "pack.threads")) {
2713 delta_search_threads = git_config_int(k, v);
2714 if (delta_search_threads < 0)
@@ -3332,6 +3328,10 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
3328 read_replace_refs = 0;
3329
3330 sparse = git_env_bool("GIT_TEST_PACK_SPARSE", 0);
3331 + prepare_repo_settings(the_repository);
3332 + if (!sparse && the_repository->settings.pack_use_sparse != -1)
3333 + sparse = the_repository->settings.pack_use_sparse;
3334 +
3335 reset_pack_idx_option(&pack_idx_opts);
3336 git_config(git_pack_config, NULL);
3337
commit-graph.c
+3 -3
@@ -466,7 +466,6 @@ static void prepare_commit_graph_one(struct repository *r, const char *obj_dir)
466 static int prepare_commit_graph(struct repository *r)
467 {
468 struct object_directory *odb;
469 - int config_value;
469
470 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD, 0))
471 die("dying as requested by the '%s' variable on commit-graph load!",
@@ -476,9 +475,10 @@ static int prepare_commit_graph(struct repository *r)
475 return !!r->objects->commit_graph;
476 r->objects->commit_graph_attempted = 1;
477
478 + prepare_repo_settings(r);
479 +
480 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
480 - (repo_config_get_bool(r, "core.commitgraph", &config_value) ||
481 - !config_value))
481 + r->settings.core_commit_graph != 1)
482 /*
483 * This repository is not configured to use commit graphs, so
484 * do not load one. (But report commit_graph_attempted anyway
read-cache.c
+6 -5
@@ -1599,16 +1599,17 @@ struct cache_entry *refresh_cache_entry(struct index_state *istate,
1599
1600 #define INDEX_FORMAT_DEFAULT 3
1601
1602 -static unsigned int get_index_format_default(void)
1602 +static unsigned int get_index_format_default(struct repository *r)
1603 {
1604 char *envversion = getenv("GIT_INDEX_VERSION");
1605 char *endp;
1606 - int value;
1606 unsigned int version = INDEX_FORMAT_DEFAULT;
1607
1608 if (!envversion) {
1610 - if (!git_config_get_int("index.version", &value))
1611 - version = value;
1609 + prepare_repo_settings(r);
1610 +
1611 + if (r->settings.index_version >= 0)
1612 + version = r->settings.index_version;
1613 if (version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < version) {
1614 warning(_("index.version set, but the value is invalid.\n"
1615 "Using version %i"), INDEX_FORMAT_DEFAULT);
@@ -2765,7 +2766,7 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
2766 }
2767
2768 if (!istate->version) {
2768 - istate->version = get_index_format_default();
2769 + istate->version = get_index_format_default(the_repository);
2770 if (git_env_bool("GIT_TEST_SPLIT_INDEX", 0))
2771 init_split_index(istate);
2772 }
repo-settings.c new
+25
@@ -0,0 +1,25 @@
1 +#include "cache.h"
2 +#include "config.h"
3 +#include "repository.h"
4 +
5 +void prepare_repo_settings(struct repository *r)
6 +{
7 + int value;
8 +
9 + if (r->settings.initialized)
10 + return;
11 +
12 + /* Defaults */
13 + memset(&r->settings, -1, sizeof(r->settings));
14 +
15 + if (!repo_config_get_bool(r, "core.commitgraph", &value))
16 + r->settings.core_commit_graph = value;
17 + if (!repo_config_get_bool(r, "gc.writecommitgraph", &value))
18 + r->settings.gc_write_commit_graph = value;
19 +
20 + if (!repo_config_get_bool(r, "index.version", &value))
21 + r->settings.index_version = value;
22 +
23 + if (!repo_config_get_bool(r, "pack.usesparse", &value))
24 + r->settings.pack_use_sparse = value;
25 +}
repository.h
+14
@@ -11,6 +11,17 @@ struct pathspec;
11 struct raw_object_store;
12 struct submodule_cache;
13
14 +struct repo_settings {
15 + int initialized;
16 +
17 + int core_commit_graph;
18 + int gc_write_commit_graph;
19 +
20 + int index_version;
21 +
22 + int pack_use_sparse;
23 +};
24 +
25 struct repository {
26 /* Environment */
27 /*
@@ -72,6 +83,8 @@ struct repository {
83 */
84 char *submodule_prefix;
85
86 + struct repo_settings settings;
87 +
88 /* Subsystems */
89 /*
90 * Repository's config which contains key-value pairs from the usual
@@ -157,5 +170,6 @@ int repo_read_index_unmerged(struct repository *);
170 */
171 void repo_update_index_if_able(struct repository *, struct lock_file *);
172
173 +void prepare_repo_settings(struct repository *r);
174
175 #endif /* REPOSITORY_H */