Merge branch 'ps/config-wo-the-repository'
Use of API functions that implicitly depend on the_repository object in the config subsystem has been rewritten to pass a repository object through the callchain. * ps/config-wo-the-repository: config: hide functions using `the_repository` by default global: prepare for hiding away repo-less config functions config: don't depend on `the_repository` with branch conditions config: don't have setters depend on `the_repository` config: pass repo to functions that rename or copy sections config: pass repo to `git_die_config()` config: pass repo to `git_config_get_expiry_in_days()` config: pass repo to `git_config_get_expiry()` config: pass repo to `git_config_get_max_percent_split_change()` config: pass repo to `git_config_get_split_index()` config: pass repo to `git_config_get_index_threads()` config: expose `repo_config_clear()` config: introduce missing setters that take repo as parameter path: hide functions using `the_repository` by default path: stop relying on `the_repository` in `worktree_git_path()` path: stop relying on `the_repository` when reporting garbage hooks: remove implicit dependency on `the_repository` editor: do not rely on `the_repository` for interactive edits path: expose `do_git_common_path()` as `repo_common_pathv()` path: expose `do_git_path()` as `repo_git_pathv()`
This commit is contained in:
225
config.c
225
config.c
@ -6,8 +6,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#define USE_THE_REPOSITORY_VARIABLE
|
||||
|
||||
#include "git-compat-util.h"
|
||||
#include "abspath.h"
|
||||
#include "advice.h"
|
||||
@ -300,13 +298,14 @@ done:
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int include_by_branch(const char *cond, size_t cond_len)
|
||||
static int include_by_branch(struct config_include_data *data,
|
||||
const char *cond, size_t cond_len)
|
||||
{
|
||||
int flags;
|
||||
int ret;
|
||||
struct strbuf pattern = STRBUF_INIT;
|
||||
const char *refname = !the_repository->gitdir ?
|
||||
NULL : refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
|
||||
const char *refname = (!data->repo || !data->repo->gitdir) ?
|
||||
NULL : refs_resolve_ref_unsafe(get_main_ref_store(data->repo),
|
||||
"HEAD", 0, NULL, &flags);
|
||||
const char *shortname;
|
||||
|
||||
@ -406,7 +405,7 @@ static int include_condition_is_true(const struct key_value_info *kvi,
|
||||
else if (skip_prefix_mem(cond, cond_len, "gitdir/i:", &cond, &cond_len))
|
||||
return include_by_gitdir(kvi, opts, cond, cond_len, 1);
|
||||
else if (skip_prefix_mem(cond, cond_len, "onbranch:", &cond, &cond_len))
|
||||
return include_by_branch(cond, cond_len);
|
||||
return include_by_branch(inc, cond, cond_len);
|
||||
else if (skip_prefix_mem(cond, cond_len, "hasconfig:remote.*.url:", &cond,
|
||||
&cond_len))
|
||||
return include_by_remote_url(inc, cond, cond_len);
|
||||
@ -2565,7 +2564,7 @@ static void git_config_check_init(struct repository *repo)
|
||||
repo_read_config(repo);
|
||||
}
|
||||
|
||||
static void repo_config_clear(struct repository *repo)
|
||||
void repo_config_clear(struct repository *repo)
|
||||
{
|
||||
if (!repo->config || !repo->config->hash_initialized)
|
||||
return;
|
||||
@ -2612,7 +2611,7 @@ int repo_config_get_string(struct repository *repo,
|
||||
git_config_check_init(repo);
|
||||
ret = git_configset_get_string(repo->config, key, dest);
|
||||
if (ret < 0)
|
||||
git_die_config(key, NULL);
|
||||
git_die_config(repo, key, NULL);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -2623,7 +2622,7 @@ int repo_config_get_string_tmp(struct repository *repo,
|
||||
git_config_check_init(repo);
|
||||
ret = git_configset_get_string_tmp(repo->config, key, dest);
|
||||
if (ret < 0)
|
||||
git_die_config(key, NULL);
|
||||
git_die_config(repo, key, NULL);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -2669,7 +2668,7 @@ int repo_config_get_pathname(struct repository *repo,
|
||||
git_config_check_init(repo);
|
||||
ret = git_configset_get_pathname(repo->config, key, dest);
|
||||
if (ret < 0)
|
||||
git_die_config(key, NULL);
|
||||
git_die_config(repo, key, NULL);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -2695,98 +2694,27 @@ void git_protected_config(config_fn_t fn, void *data)
|
||||
configset_iter(&protected_config, fn, data);
|
||||
}
|
||||
|
||||
/* Functions used historically to read configuration from 'the_repository' */
|
||||
void git_config(config_fn_t fn, void *data)
|
||||
int repo_config_get_expiry(struct repository *r, const char *key, const char **output)
|
||||
{
|
||||
repo_config(the_repository, fn, data);
|
||||
}
|
||||
|
||||
void git_config_clear(void)
|
||||
{
|
||||
repo_config_clear(the_repository);
|
||||
}
|
||||
|
||||
int git_config_get(const char *key)
|
||||
{
|
||||
return repo_config_get(the_repository, key);
|
||||
}
|
||||
|
||||
int git_config_get_value(const char *key, const char **value)
|
||||
{
|
||||
return repo_config_get_value(the_repository, key, value);
|
||||
}
|
||||
|
||||
int git_config_get_value_multi(const char *key, const struct string_list **dest)
|
||||
{
|
||||
return repo_config_get_value_multi(the_repository, key, dest);
|
||||
}
|
||||
|
||||
int git_config_get_string_multi(const char *key,
|
||||
const struct string_list **dest)
|
||||
{
|
||||
return repo_config_get_string_multi(the_repository, key, dest);
|
||||
}
|
||||
|
||||
int git_config_get_string(const char *key, char **dest)
|
||||
{
|
||||
return repo_config_get_string(the_repository, key, dest);
|
||||
}
|
||||
|
||||
int git_config_get_string_tmp(const char *key, const char **dest)
|
||||
{
|
||||
return repo_config_get_string_tmp(the_repository, key, dest);
|
||||
}
|
||||
|
||||
int git_config_get_int(const char *key, int *dest)
|
||||
{
|
||||
return repo_config_get_int(the_repository, key, dest);
|
||||
}
|
||||
|
||||
int git_config_get_ulong(const char *key, unsigned long *dest)
|
||||
{
|
||||
return repo_config_get_ulong(the_repository, key, dest);
|
||||
}
|
||||
|
||||
int git_config_get_bool(const char *key, int *dest)
|
||||
{
|
||||
return repo_config_get_bool(the_repository, key, dest);
|
||||
}
|
||||
|
||||
int git_config_get_bool_or_int(const char *key, int *is_bool, int *dest)
|
||||
{
|
||||
return repo_config_get_bool_or_int(the_repository, key, is_bool, dest);
|
||||
}
|
||||
|
||||
int git_config_get_maybe_bool(const char *key, int *dest)
|
||||
{
|
||||
return repo_config_get_maybe_bool(the_repository, key, dest);
|
||||
}
|
||||
|
||||
int git_config_get_pathname(const char *key, char **dest)
|
||||
{
|
||||
return repo_config_get_pathname(the_repository, key, dest);
|
||||
}
|
||||
|
||||
int git_config_get_expiry(const char *key, const char **output)
|
||||
{
|
||||
int ret = git_config_get_string(key, (char **)output);
|
||||
int ret = repo_config_get_string(r, key, (char **)output);
|
||||
if (ret)
|
||||
return ret;
|
||||
if (strcmp(*output, "now")) {
|
||||
timestamp_t now = approxidate("now");
|
||||
if (approxidate(*output) >= now)
|
||||
git_die_config(key, _("Invalid %s: '%s'"), key, *output);
|
||||
git_die_config(r, key, _("Invalid %s: '%s'"), key, *output);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int git_config_get_expiry_in_days(const char *key, timestamp_t *expiry, timestamp_t now)
|
||||
int repo_config_get_expiry_in_days(struct repository *r, const char *key,
|
||||
timestamp_t *expiry, timestamp_t now)
|
||||
{
|
||||
const char *expiry_string;
|
||||
intmax_t days;
|
||||
timestamp_t when;
|
||||
|
||||
if (git_config_get_string_tmp(key, &expiry_string))
|
||||
if (repo_config_get_string_tmp(r, key, &expiry_string))
|
||||
return 1; /* no such thing */
|
||||
|
||||
if (git_parse_signed(expiry_string, &days, maximum_signed_value_of_type(int))) {
|
||||
@ -2802,21 +2730,21 @@ int git_config_get_expiry_in_days(const char *key, timestamp_t *expiry, timestam
|
||||
return -1; /* thing exists but cannot be parsed */
|
||||
}
|
||||
|
||||
int git_config_get_split_index(void)
|
||||
int repo_config_get_split_index(struct repository *r)
|
||||
{
|
||||
int val;
|
||||
|
||||
if (!git_config_get_maybe_bool("core.splitindex", &val))
|
||||
if (!repo_config_get_maybe_bool(r, "core.splitindex", &val))
|
||||
return val;
|
||||
|
||||
return -1; /* default value */
|
||||
}
|
||||
|
||||
int git_config_get_max_percent_split_change(void)
|
||||
int repo_config_get_max_percent_split_change(struct repository *r)
|
||||
{
|
||||
int val = -1;
|
||||
|
||||
if (!git_config_get_int("splitindex.maxpercentchange", &val)) {
|
||||
if (!repo_config_get_int(r, "splitindex.maxpercentchange", &val)) {
|
||||
if (0 <= val && val <= 100)
|
||||
return val;
|
||||
|
||||
@ -2827,7 +2755,7 @@ int git_config_get_max_percent_split_change(void)
|
||||
return -1; /* default value */
|
||||
}
|
||||
|
||||
int git_config_get_index_threads(int *dest)
|
||||
int repo_config_get_index_threads(struct repository *r, int *dest)
|
||||
{
|
||||
int is_bool, val;
|
||||
|
||||
@ -2837,7 +2765,7 @@ int git_config_get_index_threads(int *dest)
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!git_config_get_bool_or_int("index.threads", &is_bool, &val)) {
|
||||
if (!repo_config_get_bool_or_int(r, "index.threads", &is_bool, &val)) {
|
||||
if (is_bool)
|
||||
*dest = val ? 0 : 1;
|
||||
else
|
||||
@ -2858,7 +2786,7 @@ void git_die_config_linenr(const char *key, const char *filename, int linenr)
|
||||
key, filename, linenr);
|
||||
}
|
||||
|
||||
void git_die_config(const char *key, const char *err, ...)
|
||||
void git_die_config(struct repository *r, const char *key, const char *err, ...)
|
||||
{
|
||||
const struct string_list *values;
|
||||
struct key_value_info *kv_info;
|
||||
@ -2870,7 +2798,7 @@ void git_die_config(const char *key, const char *err, ...)
|
||||
error_fn(err, params);
|
||||
va_end(params);
|
||||
}
|
||||
if (git_config_get_value_multi(key, &values))
|
||||
if (repo_config_get_value_multi(r, key, &values))
|
||||
BUG("for key '%s' we must have a value to report on", key);
|
||||
kv_info = values->items[values->nr - 1].util;
|
||||
git_die_config_linenr(key, kv_info->filename, kv_info->linenr);
|
||||
@ -3179,21 +3107,21 @@ static void maybe_remove_section(struct config_store_data *store,
|
||||
*end_offset = store->parsed[store->parsed_nr - 1].end;
|
||||
}
|
||||
|
||||
int git_config_set_in_file_gently(const char *config_filename,
|
||||
const char *key, const char *comment, const char *value)
|
||||
int repo_config_set_in_file_gently(struct repository *r, const char *config_filename,
|
||||
const char *key, const char *comment, const char *value)
|
||||
{
|
||||
return git_config_set_multivar_in_file_gently(config_filename, key, value, NULL, comment, 0);
|
||||
return repo_config_set_multivar_in_file_gently(r, config_filename, key, value, NULL, comment, 0);
|
||||
}
|
||||
|
||||
void git_config_set_in_file(const char *config_filename,
|
||||
const char *key, const char *value)
|
||||
void repo_config_set_in_file(struct repository *r, const char *config_filename,
|
||||
const char *key, const char *value)
|
||||
{
|
||||
git_config_set_multivar_in_file(config_filename, key, value, NULL, 0);
|
||||
repo_config_set_multivar_in_file(r, config_filename, key, value, NULL, 0);
|
||||
}
|
||||
|
||||
int git_config_set_gently(const char *key, const char *value)
|
||||
int repo_config_set_gently(struct repository *r, const char *key, const char *value)
|
||||
{
|
||||
return git_config_set_multivar_gently(key, value, NULL, 0);
|
||||
return repo_config_set_multivar_gently(r, key, value, NULL, 0);
|
||||
}
|
||||
|
||||
int repo_config_set_worktree_gently(struct repository *r,
|
||||
@ -3202,17 +3130,17 @@ int repo_config_set_worktree_gently(struct repository *r,
|
||||
/* Only use worktree-specific config if it is already enabled. */
|
||||
if (r->repository_format_worktree_config) {
|
||||
char *file = repo_git_path(r, "config.worktree");
|
||||
int ret = git_config_set_multivar_in_file_gently(
|
||||
file, key, value, NULL, NULL, 0);
|
||||
int ret = repo_config_set_multivar_in_file_gently(
|
||||
r, file, key, value, NULL, NULL, 0);
|
||||
free(file);
|
||||
return ret;
|
||||
}
|
||||
return repo_config_set_multivar_gently(r, key, value, NULL, 0);
|
||||
}
|
||||
|
||||
void git_config_set(const char *key, const char *value)
|
||||
void repo_config_set(struct repository *r, const char *key, const char *value)
|
||||
{
|
||||
git_config_set_multivar(key, value, NULL, 0);
|
||||
repo_config_set_multivar(r, key, value, NULL, 0);
|
||||
|
||||
trace2_cmd_set_config(key, value);
|
||||
}
|
||||
@ -3294,11 +3222,12 @@ static void validate_comment_string(const char *comment)
|
||||
* - the config file is removed and the lock file rename()d to it.
|
||||
*
|
||||
*/
|
||||
int git_config_set_multivar_in_file_gently(const char *config_filename,
|
||||
const char *key, const char *value,
|
||||
const char *value_pattern,
|
||||
const char *comment,
|
||||
unsigned flags)
|
||||
int repo_config_set_multivar_in_file_gently(struct repository *r,
|
||||
const char *config_filename,
|
||||
const char *key, const char *value,
|
||||
const char *value_pattern,
|
||||
const char *comment,
|
||||
unsigned flags)
|
||||
{
|
||||
int fd = -1, in_fd = -1;
|
||||
int ret;
|
||||
@ -3318,7 +3247,7 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
|
||||
store.multi_replace = (flags & CONFIG_FLAGS_MULTI_REPLACE) != 0;
|
||||
|
||||
if (!config_filename)
|
||||
config_filename = filename_buf = git_pathdup("config");
|
||||
config_filename = filename_buf = repo_git_path(r, "config");
|
||||
|
||||
/*
|
||||
* The lock serves a purpose in addition to locking: the new
|
||||
@ -3527,7 +3456,7 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
|
||||
ret = 0;
|
||||
|
||||
/* Invalidate the config cache */
|
||||
git_config_clear();
|
||||
repo_config_clear(r);
|
||||
|
||||
out_free:
|
||||
rollback_lock_file(&lock);
|
||||
@ -3544,12 +3473,13 @@ write_err_out:
|
||||
goto out_free;
|
||||
}
|
||||
|
||||
void git_config_set_multivar_in_file(const char *config_filename,
|
||||
const char *key, const char *value,
|
||||
const char *value_pattern, unsigned flags)
|
||||
void repo_config_set_multivar_in_file(struct repository *r,
|
||||
const char *config_filename,
|
||||
const char *key, const char *value,
|
||||
const char *value_pattern, unsigned flags)
|
||||
{
|
||||
if (!git_config_set_multivar_in_file_gently(config_filename, key, value,
|
||||
value_pattern, NULL, flags))
|
||||
if (!repo_config_set_multivar_in_file_gently(r, config_filename, key, value,
|
||||
value_pattern, NULL, flags))
|
||||
return;
|
||||
if (value)
|
||||
die(_("could not set '%s' to '%s'"), key, value);
|
||||
@ -3557,32 +3487,27 @@ void git_config_set_multivar_in_file(const char *config_filename,
|
||||
die(_("could not unset '%s'"), key);
|
||||
}
|
||||
|
||||
int git_config_set_multivar_gently(const char *key, const char *value,
|
||||
const char *value_pattern, unsigned flags)
|
||||
{
|
||||
return repo_config_set_multivar_gently(the_repository, key, value,
|
||||
value_pattern, flags);
|
||||
}
|
||||
|
||||
int repo_config_set_multivar_gently(struct repository *r, const char *key,
|
||||
const char *value,
|
||||
const char *value_pattern, unsigned flags)
|
||||
{
|
||||
char *file = repo_git_path(r, "config");
|
||||
int res = git_config_set_multivar_in_file_gently(file,
|
||||
key, value,
|
||||
value_pattern,
|
||||
NULL, flags);
|
||||
int res = repo_config_set_multivar_in_file_gently(r, file,
|
||||
key, value,
|
||||
value_pattern,
|
||||
NULL, flags);
|
||||
free(file);
|
||||
return res;
|
||||
}
|
||||
|
||||
void git_config_set_multivar(const char *key, const char *value,
|
||||
const char *value_pattern, unsigned flags)
|
||||
void repo_config_set_multivar(struct repository *r,
|
||||
const char *key, const char *value,
|
||||
const char *value_pattern, unsigned flags)
|
||||
{
|
||||
git_config_set_multivar_in_file(git_path("config"),
|
||||
key, value, value_pattern,
|
||||
flags);
|
||||
char *file = repo_git_path(r, "config");
|
||||
repo_config_set_multivar_in_file(r, file, key, value,
|
||||
value_pattern, flags);
|
||||
free(file);
|
||||
}
|
||||
|
||||
static size_t section_name_match (const char *buf, const char *name)
|
||||
@ -3644,9 +3569,11 @@ static int section_name_is_ok(const char *name)
|
||||
#define GIT_CONFIG_MAX_LINE_LEN (512 * 1024)
|
||||
|
||||
/* if new_name == NULL, the section is removed instead */
|
||||
static int git_config_copy_or_rename_section_in_file(const char *config_filename,
|
||||
const char *old_name,
|
||||
const char *new_name, int copy)
|
||||
static int repo_config_copy_or_rename_section_in_file(
|
||||
struct repository *r,
|
||||
const char *config_filename,
|
||||
const char *old_name,
|
||||
const char *new_name, int copy)
|
||||
{
|
||||
int ret = 0, remove = 0;
|
||||
char *filename_buf = NULL;
|
||||
@ -3667,7 +3594,7 @@ static int git_config_copy_or_rename_section_in_file(const char *config_filename
|
||||
}
|
||||
|
||||
if (!config_filename)
|
||||
config_filename = filename_buf = git_pathdup("config");
|
||||
config_filename = filename_buf = repo_git_path(r, "config");
|
||||
|
||||
out_fd = hold_lock_file_for_update(&lock, config_filename, 0);
|
||||
if (out_fd < 0) {
|
||||
@ -3810,28 +3737,28 @@ out_no_rollback:
|
||||
return ret;
|
||||
}
|
||||
|
||||
int git_config_rename_section_in_file(const char *config_filename,
|
||||
const char *old_name, const char *new_name)
|
||||
int repo_config_rename_section_in_file(struct repository *r, const char *config_filename,
|
||||
const char *old_name, const char *new_name)
|
||||
{
|
||||
return git_config_copy_or_rename_section_in_file(config_filename,
|
||||
return repo_config_copy_or_rename_section_in_file(r, config_filename,
|
||||
old_name, new_name, 0);
|
||||
}
|
||||
|
||||
int git_config_rename_section(const char *old_name, const char *new_name)
|
||||
int repo_config_rename_section(struct repository *r, const char *old_name, const char *new_name)
|
||||
{
|
||||
return git_config_rename_section_in_file(NULL, old_name, new_name);
|
||||
return repo_config_rename_section_in_file(r, NULL, old_name, new_name);
|
||||
}
|
||||
|
||||
int git_config_copy_section_in_file(const char *config_filename,
|
||||
const char *old_name, const char *new_name)
|
||||
int repo_config_copy_section_in_file(struct repository *r, const char *config_filename,
|
||||
const char *old_name, const char *new_name)
|
||||
{
|
||||
return git_config_copy_or_rename_section_in_file(config_filename,
|
||||
return repo_config_copy_or_rename_section_in_file(r, config_filename,
|
||||
old_name, new_name, 1);
|
||||
}
|
||||
|
||||
int git_config_copy_section(const char *old_name, const char *new_name)
|
||||
int repo_config_copy_section(struct repository *r, const char *old_name, const char *new_name)
|
||||
{
|
||||
return git_config_copy_section_in_file(NULL, old_name, new_name);
|
||||
return repo_config_copy_section_in_file(r, NULL, old_name, new_name);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user