repo-settings: parse core.untrackedCache

The core.untrackedCache config setting is slightly complicated, so clarify its use and centralize its parsing into the repo settings. The default value is "keep" (returned as -1), which persists the untracked cache if it exists. If the value is set as "false" (returned as 0), then remove the untracked cache if it exists. If the value is set as "true" (returned as 1), then write the untracked cache and persist it. Instead of relying on magic values of -1, 0, and 1, split these options into an enum. This allows the use of "-1" as a default value. After parsing the config options, if the value is unset we can initialize it to UNTRACKED_CACHE_KEEP. 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 ad0fb65999382052cf21408df490d0b39800d487
5 files changed +40 -36
builtin/update-index.c
+4 -2
@@ -966,6 +966,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
966 struct parse_opt_ctx_t ctx;
967 strbuf_getline_fn getline_fn;
968 int parseopt_state = PARSE_OPT_UNKNOWN;
969 + struct repository *r = the_repository;
970 struct option options[] = {
971 OPT_BIT('q', NULL, &refresh_args.flags,
972 N_("continue refresh even when index needs update"),
@@ -1180,11 +1181,12 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
1181 remove_split_index(&the_index);
1182 }
1183
1184 + prepare_repo_settings(r);
1185 switch (untracked_cache) {
1186 case UC_UNSPECIFIED:
1187 break;
1188 case UC_DISABLE:
1187 - if (git_config_get_untracked_cache() == 1)
1189 + if (r->settings.core_untracked_cache == UNTRACKED_CACHE_WRITE)
1190 warning(_("core.untrackedCache is set to true; "
1191 "remove or change it, if you really want to "
1192 "disable the untracked cache"));
@@ -1196,7 +1198,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
1198 return !test_if_untracked_cache_is_supported();
1199 case UC_ENABLE:
1200 case UC_FORCE:
1199 - if (git_config_get_untracked_cache() == 0)
1201 + if (r->settings.core_untracked_cache == UNTRACKED_CACHE_REMOVE)
1202 warning(_("core.untrackedCache is set to false; "
1203 "remove or change it, if you really want to "
1204 "enable the untracked cache"));
config.c
-24
@@ -2277,30 +2277,6 @@ int git_config_get_expiry_in_days(const char *key, timestamp_t *expiry, timestam
2277 return -1; /* thing exists but cannot be parsed */
2278 }
2279
2280 -int git_config_get_untracked_cache(void)
2281 -{
2282 - int val = -1;
2283 - const char *v;
2284 -
2285 - /* Hack for test programs like test-dump-untracked-cache */
2286 - if (ignore_untracked_cache_config)
2287 - return -1;
2288 -
2289 - if (!git_config_get_maybe_bool("core.untrackedcache", &val))
2290 - return val;
2291 -
2292 - if (!git_config_get_value("core.untrackedcache", &v)) {
2293 - if (!strcasecmp(v, "keep"))
2294 - return -1;
2295 -
2296 - error(_("unknown core.untrackedCache value '%s'; "
2297 - "using 'keep' default value"), v);
2298 - return -1;
2299 - }
2300 -
2301 - return -1; /* default value */
2302 -}
2303 -
2280 int git_config_get_split_index(void)
2281 {
2282 int val;
read-cache.c
+9 -10
@@ -1845,18 +1845,17 @@ static void check_ce_order(struct index_state *istate)
1845
1846 static void tweak_untracked_cache(struct index_state *istate)
1847 {
1848 - switch (git_config_get_untracked_cache()) {
1849 - case -1: /* keep: do nothing */
1850 - break;
1851 - case 0: /* false */
1848 + struct repository *r = the_repository;
1849 +
1850 + prepare_repo_settings(r);
1851 +
1852 + if (r->settings.core_untracked_cache == UNTRACKED_CACHE_REMOVE) {
1853 remove_untracked_cache(istate);
1853 - break;
1854 - case 1: /* true */
1855 - add_untracked_cache(istate);
1856 - break;
1857 - default: /* unknown value: do nothing */
1858 - break;
1854 + return;
1855 }
1856 +
1857 + if (r->settings.core_untracked_cache == UNTRACKED_CACHE_WRITE)
1858 + add_untracked_cache(istate);
1859 }
1860
1861 static void tweak_split_index(struct index_state *istate)
repo-settings.c
+19
@@ -7,6 +7,7 @@
7 void prepare_repo_settings(struct repository *r)
8 {
9 int value;
10 + char *strval;
11
12 if (r->settings.initialized)
13 return;
@@ -23,7 +24,25 @@ void prepare_repo_settings(struct repository *r)
24
25 if (!repo_config_get_bool(r, "index.version", &value))
26 r->settings.index_version = value;
27 + if (!repo_config_get_maybe_bool(r, "core.untrackedcache", &value)) {
28 + if (value == 0)
29 + r->settings.core_untracked_cache = UNTRACKED_CACHE_REMOVE;
30 + else
31 + r->settings.core_untracked_cache = UNTRACKED_CACHE_WRITE;
32 + } else if (!repo_config_get_string(r, "core.untrackedcache", &strval)) {
33 + if (!strcasecmp(strval, "keep"))
34 + r->settings.core_untracked_cache = UNTRACKED_CACHE_KEEP;
35 +
36 + free(strval);
37 + }
38 +
39
40 if (!repo_config_get_bool(r, "pack.usesparse", &value))
41 r->settings.pack_use_sparse = value;
42 +
43 + /* Hack for test programs like test-dump-untracked-cache */
44 + if (ignore_untracked_cache_config)
45 + r->settings.core_untracked_cache = UNTRACKED_CACHE_KEEP;
46 + else
47 + UPDATE_DEFAULT_BOOL(r->settings.core_untracked_cache, UNTRACKED_CACHE_KEEP);
48 }
repository.h
+8
@@ -11,6 +11,13 @@ struct pathspec;
11 struct raw_object_store;
12 struct submodule_cache;
13
14 +enum untracked_cache_setting {
15 + UNTRACKED_CACHE_UNSET = -1,
16 + UNTRACKED_CACHE_REMOVE = 0,
17 + UNTRACKED_CACHE_KEEP = 1,
18 + UNTRACKED_CACHE_WRITE = 2
19 +};
20 +
21 struct repo_settings {
22 int initialized;
23
@@ -18,6 +25,7 @@ struct repo_settings {
25 int gc_write_commit_graph;
26
27 int index_version;
28 + enum untracked_cache_setting core_untracked_cache;
29
30 int pack_use_sparse;
31 };