Merge branch 'ab/repo-settings-cleanup'

Code cleanup.

* ab/repo-settings-cleanup:
  repository.h: don't use a mix of int and bitfields
  repo-settings.c: simplify the setup
  read-cache & fetch-negotiator: check "enum" values in switch()
  environment.c: remove test-specific "ignore_untracked..." variable
  wrapper.c: add x{un,}setenv(), and use xsetenv() in environment.c
This commit is contained in:
Junio C Hamano 2021-10-06 13:40:11 -07:00
commit d8d33378ed
9 changed files with 98 additions and 94 deletions

View File

@ -1682,13 +1682,6 @@ int update_server_info(int);
const char *get_log_output_encoding(void);
const char *get_commit_output_encoding(void);
/*
* This is a hack for test programs like test-dump-untracked-cache to
* ensure that they do not modify the untracked cache when reading it.
* Do not use it otherwise!
*/
extern int ignore_untracked_cache_config;
int committer_ident_sufficiently_given(void);
int author_ident_sufficiently_given(void);

View File

@ -95,13 +95,6 @@ int auto_comment_line_char;
/* Parallel index stat data preload? */
int core_preload_index = 1;
/*
* This is a hack for test programs like test-dump-untracked-cache to
* ensure that they do not modify the untracked cache when reading it.
* Do not use it otherwise!
*/
int ignore_untracked_cache_config;
/* This is set by setup_git_dir_gently() and/or git_default_config() */
char *git_work_tree_cfg;
@ -329,8 +322,7 @@ char *get_graft_file(struct repository *r)
static void set_git_dir_1(const char *path)
{
if (setenv(GIT_DIR_ENVIRONMENT, path, 1))
die(_("could not set GIT_DIR to '%s'"), path);
xsetenv(GIT_DIR_ENVIRONMENT, path, 1);
setup_git_env(path);
}

View File

@ -19,7 +19,6 @@ void fetch_negotiator_init(struct repository *r,
return;
case FETCH_NEGOTIATION_DEFAULT:
default:
default_negotiator_init(negotiator);
return;
}

View File

@ -878,6 +878,8 @@ void *xmemdupz(const void *data, size_t len);
char *xstrndup(const char *str, size_t len);
void *xrealloc(void *ptr, size_t size);
void *xcalloc(size_t nmemb, size_t size);
void xsetenv(const char *name, const char *value, int overwrite);
void xunsetenv(const char *name);
void *xmmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
const char *mmap_os_err(void);
void *xmmap_gently(void *start, size_t length, int prot, int flags, int fd, off_t offset);

View File

@ -1944,13 +1944,22 @@ static void tweak_untracked_cache(struct index_state *istate)
prepare_repo_settings(r);
if (r->settings.core_untracked_cache == UNTRACKED_CACHE_REMOVE) {
switch (r->settings.core_untracked_cache) {
case UNTRACKED_CACHE_REMOVE:
remove_untracked_cache(istate);
return;
}
if (r->settings.core_untracked_cache == UNTRACKED_CACHE_WRITE)
break;
case UNTRACKED_CACHE_WRITE:
add_untracked_cache(istate);
break;
case UNTRACKED_CACHE_KEEP:
/*
* Either an explicit "core.untrackedCache=keep", the
* default if "core.untrackedCache" isn't configured,
* or a fallback on an unknown "core.untrackedCache"
* value.
*/
break;
}
}
static void tweak_split_index(struct index_state *istate)

View File

@ -3,40 +3,77 @@
#include "repository.h"
#include "midx.h"
#define UPDATE_DEFAULT_BOOL(s,v) do { if (s == -1) { s = v; } } while(0)
static void repo_cfg_bool(struct repository *r, const char *key, int *dest,
int def)
{
if (repo_config_get_bool(r, key, dest))
*dest = def;
}
void prepare_repo_settings(struct repository *r)
{
int experimental;
int value;
char *strval;
int manyfiles;
if (r->settings.initialized)
if (r->settings.initialized++)
return;
/* Defaults */
memset(&r->settings, -1, sizeof(r->settings));
r->settings.index_version = -1;
r->settings.core_untracked_cache = UNTRACKED_CACHE_KEEP;
r->settings.fetch_negotiation_algorithm = FETCH_NEGOTIATION_DEFAULT;
if (!repo_config_get_bool(r, "core.commitgraph", &value))
r->settings.core_commit_graph = value;
if (!repo_config_get_bool(r, "commitgraph.readchangedpaths", &value))
r->settings.commit_graph_read_changed_paths = value;
if (!repo_config_get_bool(r, "gc.writecommitgraph", &value))
r->settings.gc_write_commit_graph = value;
UPDATE_DEFAULT_BOOL(r->settings.core_commit_graph, 1);
UPDATE_DEFAULT_BOOL(r->settings.commit_graph_read_changed_paths, 1);
UPDATE_DEFAULT_BOOL(r->settings.gc_write_commit_graph, 1);
/* Booleans config or default, cascades to other settings */
repo_cfg_bool(r, "feature.manyfiles", &manyfiles, 0);
repo_cfg_bool(r, "feature.experimental", &experimental, 0);
/* Defaults modified by feature.* */
if (experimental) {
r->settings.fetch_negotiation_algorithm = FETCH_NEGOTIATION_SKIPPING;
}
if (manyfiles) {
r->settings.index_version = 4;
r->settings.core_untracked_cache = UNTRACKED_CACHE_WRITE;
}
/* Boolean config or default, does not cascade (simple) */
repo_cfg_bool(r, "core.commitgraph", &r->settings.core_commit_graph, 1);
repo_cfg_bool(r, "commitgraph.readchangedpaths", &r->settings.commit_graph_read_changed_paths, 1);
repo_cfg_bool(r, "gc.writecommitgraph", &r->settings.gc_write_commit_graph, 1);
repo_cfg_bool(r, "fetch.writecommitgraph", &r->settings.fetch_write_commit_graph, 0);
repo_cfg_bool(r, "pack.usesparse", &r->settings.pack_use_sparse, 1);
repo_cfg_bool(r, "core.multipackindex", &r->settings.core_multi_pack_index, 1);
repo_cfg_bool(r, "index.sparse", &r->settings.sparse_index, 0);
/*
* The GIT_TEST_MULTI_PACK_INDEX variable is special in that
* either it *or* the config sets
* r->settings.core_multi_pack_index if true. We don't take
* the environment variable if it exists (even if false) over
* any config, as in most other cases.
*/
if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0))
r->settings.core_multi_pack_index = 1;
/*
* Non-boolean config
*/
if (!repo_config_get_int(r, "index.version", &value))
r->settings.index_version = value;
if (!repo_config_get_maybe_bool(r, "core.untrackedcache", &value)) {
if (value == 0)
r->settings.core_untracked_cache = UNTRACKED_CACHE_REMOVE;
else
r->settings.core_untracked_cache = UNTRACKED_CACHE_WRITE;
} else if (!repo_config_get_string(r, "core.untrackedcache", &strval)) {
if (!strcasecmp(strval, "keep"))
r->settings.core_untracked_cache = UNTRACKED_CACHE_KEEP;
if (!repo_config_get_string(r, "core.untrackedcache", &strval)) {
int v = git_parse_maybe_bool(strval);
/*
* If it's set to "keep", or some other non-boolean
* value then "v < 0". Then we do nothing and keep it
* at the default of UNTRACKED_CACHE_KEEP.
*/
if (v >= 0)
r->settings.core_untracked_cache = v ?
UNTRACKED_CACHE_WRITE : UNTRACKED_CACHE_REMOVE;
free(strval);
}
@ -45,39 +82,8 @@ void prepare_repo_settings(struct repository *r)
r->settings.fetch_negotiation_algorithm = FETCH_NEGOTIATION_SKIPPING;
else if (!strcasecmp(strval, "noop"))
r->settings.fetch_negotiation_algorithm = FETCH_NEGOTIATION_NOOP;
else
r->settings.fetch_negotiation_algorithm = FETCH_NEGOTIATION_DEFAULT;
}
if (!repo_config_get_bool(r, "pack.usesparse", &value))
r->settings.pack_use_sparse = value;
UPDATE_DEFAULT_BOOL(r->settings.pack_use_sparse, 1);
value = git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0);
if (value || !repo_config_get_bool(r, "core.multipackindex", &value))
r->settings.core_multi_pack_index = value;
UPDATE_DEFAULT_BOOL(r->settings.core_multi_pack_index, 1);
if (!repo_config_get_bool(r, "feature.manyfiles", &value) && value) {
UPDATE_DEFAULT_BOOL(r->settings.index_version, 4);
UPDATE_DEFAULT_BOOL(r->settings.core_untracked_cache, UNTRACKED_CACHE_WRITE);
}
if (!repo_config_get_bool(r, "fetch.writecommitgraph", &value))
r->settings.fetch_write_commit_graph = value;
UPDATE_DEFAULT_BOOL(r->settings.fetch_write_commit_graph, 0);
if (!repo_config_get_bool(r, "feature.experimental", &value) && value)
UPDATE_DEFAULT_BOOL(r->settings.fetch_negotiation_algorithm, FETCH_NEGOTIATION_SKIPPING);
/* Hack for test programs like test-dump-untracked-cache */
if (ignore_untracked_cache_config)
r->settings.core_untracked_cache = UNTRACKED_CACHE_KEEP;
else
UPDATE_DEFAULT_BOOL(r->settings.core_untracked_cache, UNTRACKED_CACHE_KEEP);
UPDATE_DEFAULT_BOOL(r->settings.fetch_negotiation_algorithm, FETCH_NEGOTIATION_DEFAULT);
/*
* This setting guards all index reads to require a full index
* over a sparse index. After suitable guards are placed in the
@ -85,11 +91,4 @@ void prepare_repo_settings(struct repository *r)
* removed.
*/
r->settings.command_requires_full_index = 1;
/*
* Initialize this as off.
*/
r->settings.sparse_index = 0;
if (!repo_config_get_bool(r, "index.sparse", &value) && value)
r->settings.sparse_index = 1;
}

View File

@ -13,18 +13,15 @@ struct submodule_cache;
struct promisor_remote_config;
enum untracked_cache_setting {
UNTRACKED_CACHE_UNSET = -1,
UNTRACKED_CACHE_REMOVE = 0,
UNTRACKED_CACHE_KEEP = 1,
UNTRACKED_CACHE_WRITE = 2
UNTRACKED_CACHE_KEEP,
UNTRACKED_CACHE_REMOVE,
UNTRACKED_CACHE_WRITE,
};
enum fetch_negotiation_setting {
FETCH_NEGOTIATION_UNSET = -1,
FETCH_NEGOTIATION_NONE = 0,
FETCH_NEGOTIATION_DEFAULT = 1,
FETCH_NEGOTIATION_SKIPPING = 2,
FETCH_NEGOTIATION_NOOP = 3,
FETCH_NEGOTIATION_DEFAULT,
FETCH_NEGOTIATION_SKIPPING,
FETCH_NEGOTIATION_NOOP,
};
struct repo_settings {
@ -34,6 +31,8 @@ struct repo_settings {
int commit_graph_read_changed_paths;
int gc_write_commit_graph;
int fetch_write_commit_graph;
int command_requires_full_index;
int sparse_index;
int index_version;
enum untracked_cache_setting core_untracked_cache;
@ -42,9 +41,6 @@ struct repo_settings {
enum fetch_negotiation_setting fetch_negotiation_algorithm;
int core_multi_pack_index;
unsigned command_requires_full_index:1,
sparse_index:1;
};
struct repository {

View File

@ -45,8 +45,10 @@ int cmd__dump_untracked_cache(int ac, const char **av)
struct untracked_cache *uc;
struct strbuf base = STRBUF_INIT;
/* Hack to avoid modifying the untracked cache when we read it */
ignore_untracked_cache_config = 1;
/* Set core.untrackedCache=keep before setup_git_directory() */
xsetenv("GIT_CONFIG_COUNT", "1", 1);
xsetenv("GIT_CONFIG_KEY_0", "core.untrackedCache", 1);
xsetenv("GIT_CONFIG_VALUE_0", "keep", 1);
setup_git_directory();
if (read_cache() < 0)

View File

@ -145,6 +145,18 @@ void *xcalloc(size_t nmemb, size_t size)
return ret;
}
void xsetenv(const char *name, const char *value, int overwrite)
{
if (setenv(name, value, overwrite))
die_errno(_("could not setenv '%s'"), name ? name : "(null)");
}
void xunsetenv(const char *name)
{
if (!unsetenv(name))
die_errno(_("could not unsetenv '%s'"), name ? name : "(null)");
}
/*
* Limit size of IO chunks, because huge chunks only cause pain. OS X
* 64-bit is buggy, returning EINVAL if len >= INT_MAX; and even in