Blame src/config_cache.c

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
#include "common.h"
Packit Service 20376f
#include "fileops.h"
Packit Service 20376f
#include "repository.h"
Packit Service 20376f
#include "config.h"
Packit Service 20376f
#include "git2/config.h"
Packit Service 20376f
#include "vector.h"
Packit Service 20376f
#include "filter.h"
Packit Service 20376f
Packit Service 20376f
struct map_data {
Packit Service 20376f
	const char *cvar_name;
Packit Service 20376f
	git_cvar_map *maps;
Packit Service 20376f
	size_t map_count;
Packit Service 20376f
	int default_value;
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 *	core.eol
Packit Service 20376f
 *		Sets the line ending type to use in the working directory for
Packit Service 20376f
 *	files that have the text property set. Alternatives are lf, crlf
Packit Service 20376f
 *	and native, which uses the platform's native line ending. The default
Packit Service 20376f
 *	value is native. See gitattributes(5) for more information on
Packit Service 20376f
 *	end-of-line conversion.
Packit Service 20376f
 */
Packit Service 20376f
static git_cvar_map _cvar_map_eol[] = {
Packit Service 20376f
	{GIT_CVAR_FALSE, NULL, GIT_EOL_UNSET},
Packit Service 20376f
	{GIT_CVAR_STRING, "lf", GIT_EOL_LF},
Packit Service 20376f
	{GIT_CVAR_STRING, "crlf", GIT_EOL_CRLF},
Packit Service 20376f
	{GIT_CVAR_STRING, "native", GIT_EOL_NATIVE}
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 *	core.autocrlf
Packit Service 20376f
 *		Setting this variable to "true" is almost the same as setting
Packit Service 20376f
 *	the text attribute to "auto" on all files except that text files are
Packit Service 20376f
 *	not guaranteed to be normalized: files that contain CRLF in the
Packit Service 20376f
 *	repository will not be touched. Use this setting if you want to have
Packit Service 20376f
 *	CRLF line endings in your working directory even though the repository
Packit Service 20376f
 *	does not have normalized line endings. This variable can be set to input,
Packit Service 20376f
 *	in which case no output conversion is performed.
Packit Service 20376f
 */
Packit Service 20376f
static git_cvar_map _cvar_map_autocrlf[] = {
Packit Service 20376f
	{GIT_CVAR_FALSE, NULL, GIT_AUTO_CRLF_FALSE},
Packit Service 20376f
	{GIT_CVAR_TRUE, NULL, GIT_AUTO_CRLF_TRUE},
Packit Service 20376f
	{GIT_CVAR_STRING, "input", GIT_AUTO_CRLF_INPUT}
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
static git_cvar_map _cvar_map_safecrlf[] = {
Packit Service 20376f
	{GIT_CVAR_FALSE, NULL, GIT_SAFE_CRLF_FALSE},
Packit Service 20376f
	{GIT_CVAR_TRUE, NULL, GIT_SAFE_CRLF_FAIL},
Packit Service 20376f
	{GIT_CVAR_STRING, "warn", GIT_SAFE_CRLF_WARN}
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * Generic map for integer values
Packit Service 20376f
 */
Packit Service 20376f
static git_cvar_map _cvar_map_int[] = {
Packit Service 20376f
	{GIT_CVAR_INT32, NULL, 0},
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
static struct map_data _cvar_maps[] = {
Packit Service 20376f
	{"core.autocrlf", _cvar_map_autocrlf, ARRAY_SIZE(_cvar_map_autocrlf), GIT_AUTO_CRLF_DEFAULT},
Packit Service 20376f
	{"core.eol", _cvar_map_eol, ARRAY_SIZE(_cvar_map_eol), GIT_EOL_DEFAULT},
Packit Service 20376f
	{"core.symlinks", NULL, 0, GIT_SYMLINKS_DEFAULT },
Packit Service 20376f
	{"core.ignorecase", NULL, 0, GIT_IGNORECASE_DEFAULT },
Packit Service 20376f
	{"core.filemode", NULL, 0, GIT_FILEMODE_DEFAULT },
Packit Service 20376f
	{"core.ignorestat", NULL, 0, GIT_IGNORESTAT_DEFAULT },
Packit Service 20376f
	{"core.trustctime", NULL, 0, GIT_TRUSTCTIME_DEFAULT },
Packit Service 20376f
	{"core.abbrev", _cvar_map_int, 1, GIT_ABBREV_DEFAULT },
Packit Service 20376f
	{"core.precomposeunicode", NULL, 0, GIT_PRECOMPOSE_DEFAULT },
Packit Service 20376f
	{"core.safecrlf", _cvar_map_safecrlf, ARRAY_SIZE(_cvar_map_safecrlf), GIT_SAFE_CRLF_DEFAULT},
Packit Service 20376f
	{"core.logallrefupdates", NULL, 0, GIT_LOGALLREFUPDATES_DEFAULT },
Packit Service 20376f
	{"core.protecthfs", NULL, 0, GIT_PROTECTHFS_DEFAULT },
Packit Service 20376f
	{"core.protectntfs", NULL, 0, GIT_PROTECTNTFS_DEFAULT },
Packit Service 20376f
	{"core.fsyncobjectfiles", NULL, 0, GIT_FSYNCOBJECTFILES_DEFAULT },
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
int git_config__cvar(int *out, git_config *config, git_cvar_cached cvar)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	struct map_data *data = &_cvar_maps[(int)cvar];
Packit Service 20376f
	git_config_entry *entry;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_config__lookup_entry(&entry, config, data->cvar_name, false)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if (!entry)
Packit Service 20376f
		*out = data->default_value;
Packit Service 20376f
	else if (data->maps)
Packit Service 20376f
		error = git_config_lookup_map_value(
Packit Service 20376f
			out, data->maps, data->map_count, entry->value);
Packit Service 20376f
	else
Packit Service 20376f
		error = git_config_parse_bool(out, entry->value);
Packit Service 20376f
Packit Service 20376f
	git_config_entry_free(entry);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository__cvar(int *out, git_repository *repo, git_cvar_cached cvar)
Packit Service 20376f
{
Packit Service 20376f
	*out = repo->cvar_cache[(int)cvar];
Packit Service 20376f
Packit Service 20376f
	if (*out == GIT_CVAR_NOT_CACHED) {
Packit Service 20376f
		int error;
Packit Service 20376f
		git_config *config;
Packit Service 20376f
Packit Service 20376f
		if ((error = git_repository_config__weakptr(&config, repo)) < 0 ||
Packit Service 20376f
			(error = git_config__cvar(out, config, cvar)) < 0)
Packit Service 20376f
			return error;
Packit Service 20376f
Packit Service 20376f
		repo->cvar_cache[(int)cvar] = *out;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_repository__cvar_cache_clear(git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	int i;
Packit Service 20376f
Packit Service 20376f
	for (i = 0; i < GIT_CVAR_CACHE_MAX; ++i)
Packit Service 20376f
		repo->cvar_cache[i] = GIT_CVAR_NOT_CACHED;
Packit Service 20376f
}
Packit Service 20376f