Blame src/config_cache.c

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