]> git.proxmox.com Git - libgit2.git/blame - src/config_cache.c
patch: use strlen to mean string length
[libgit2.git] / src / config_cache.c
CommitLineData
97da3eae 1/*
359fc2d2 2 * Copyright (C) the libgit2 contributors. All rights reserved.
97da3eae
VM
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7
8#include "common.h"
9#include "fileops.h"
2b52a0bf 10#include "repository.h"
97da3eae
VM
11#include "config.h"
12#include "git2/config.h"
13#include "vector.h"
14#include "filter.h"
97da3eae
VM
15
16struct map_data {
17 const char *cvar_name;
18 git_cvar_map *maps;
19 size_t map_count;
20 int default_value;
21};
22
23/*
24 * core.eol
25 * Sets the line ending type to use in the working directory for
26 * files that have the text property set. Alternatives are lf, crlf
7ea3a79f 27 * and native, which uses the platform's native line ending. The default
97da3eae 28 * value is native. See gitattributes(5) for more information on
ab01cbd4 29 * end-of-line conversion.
97da3eae
VM
30 */
31static git_cvar_map _cvar_map_eol[] = {
32 {GIT_CVAR_FALSE, NULL, GIT_EOL_UNSET},
33 {GIT_CVAR_STRING, "lf", GIT_EOL_LF},
34 {GIT_CVAR_STRING, "crlf", GIT_EOL_CRLF},
35 {GIT_CVAR_STRING, "native", GIT_EOL_NATIVE}
36};
37
38/*
39 * core.autocrlf
ab01cbd4 40 * Setting this variable to "true" is almost the same as setting
97da3eae
VM
41 * the text attribute to "auto" on all files except that text files are
42 * not guaranteed to be normalized: files that contain CRLF in the
43 * repository will not be touched. Use this setting if you want to have
44 * CRLF line endings in your working directory even though the repository
45 * does not have normalized line endings. This variable can be set to input,
46 * in which case no output conversion is performed.
47 */
48static git_cvar_map _cvar_map_autocrlf[] = {
49 {GIT_CVAR_FALSE, NULL, GIT_AUTO_CRLF_FALSE},
50 {GIT_CVAR_TRUE, NULL, GIT_AUTO_CRLF_TRUE},
51 {GIT_CVAR_STRING, "input", GIT_AUTO_CRLF_INPUT}
52};
53
49837fd4
ET
54static git_cvar_map _cvar_map_safecrlf[] = {
55 {GIT_CVAR_FALSE, NULL, GIT_SAFE_CRLF_FALSE},
56 {GIT_CVAR_TRUE, NULL, GIT_SAFE_CRLF_FAIL},
57 {GIT_CVAR_STRING, "warn", GIT_SAFE_CRLF_WARN}
58};
59
ab01cbd4
RB
60/*
61 * Generic map for integer values
62 */
63static git_cvar_map _cvar_map_int[] = {
64 {GIT_CVAR_INT32, NULL, 0},
65};
66
97da3eae
VM
67static struct map_data _cvar_maps[] = {
68 {"core.autocrlf", _cvar_map_autocrlf, ARRAY_SIZE(_cvar_map_autocrlf), GIT_AUTO_CRLF_DEFAULT},
ab01cbd4
RB
69 {"core.eol", _cvar_map_eol, ARRAY_SIZE(_cvar_map_eol), GIT_EOL_DEFAULT},
70 {"core.symlinks", NULL, 0, GIT_SYMLINKS_DEFAULT },
71 {"core.ignorecase", NULL, 0, GIT_IGNORECASE_DEFAULT },
72 {"core.filemode", NULL, 0, GIT_FILEMODE_DEFAULT },
73 {"core.ignorestat", NULL, 0, GIT_IGNORESTAT_DEFAULT },
74 {"core.trustctime", NULL, 0, GIT_TRUSTCTIME_DEFAULT },
75 {"core.abbrev", _cvar_map_int, 1, GIT_ABBREV_DEFAULT },
2fe54afa 76 {"core.precomposeunicode", NULL, 0, GIT_PRECOMPOSE_DEFAULT },
49837fd4 77 {"core.safecrlf", _cvar_map_safecrlf, ARRAY_SIZE(_cvar_map_safecrlf), GIT_SAFE_CRLF_DEFAULT},
2b52a0bf 78 {"core.logallrefupdates", NULL, 0, GIT_LOGALLREFUPDATES_DEFAULT },
ec74b40c
ET
79 {"core.protecthfs", NULL, 0, GIT_PROTECTHFS_DEFAULT },
80 {"core.protectntfs", NULL, 0, GIT_PROTECTNTFS_DEFAULT },
97da3eae
VM
81};
82
2b52a0bf
RB
83int git_config__cvar(int *out, git_config *config, git_cvar_cached cvar)
84{
85 int error = 0;
86 struct map_data *data = &_cvar_maps[(int)cvar];
9a97f49e 87 git_config_entry *entry;
2b52a0bf 88
13c371dc
PS
89 if ((error = git_config__lookup_entry(&entry, config, data->cvar_name, false)) < 0)
90 return error;
2b52a0bf
RB
91
92 if (!entry)
93 *out = data->default_value;
94 else if (data->maps)
95 error = git_config_lookup_map_value(
96 out, data->maps, data->map_count, entry->value);
97 else
98 error = git_config_parse_bool(out, entry->value);
99
9a97f49e 100 git_config_entry_free(entry);
2b52a0bf
RB
101 return error;
102}
103
97da3eae
VM
104int git_repository__cvar(int *out, git_repository *repo, git_cvar_cached cvar)
105{
106 *out = repo->cvar_cache[(int)cvar];
107
108 if (*out == GIT_CVAR_NOT_CACHED) {
97da3eae 109 int error;
2b52a0bf 110 git_config *config;
9f77b3f6 111
2b52a0bf
RB
112 if ((error = git_repository_config__weakptr(&config, repo)) < 0 ||
113 (error = git_config__cvar(out, config, cvar)) < 0)
97da3eae
VM
114 return error;
115
116 repo->cvar_cache[(int)cvar] = *out;
117 }
118
e172cf08 119 return 0;
97da3eae
VM
120}
121
122void git_repository__cvar_cache_clear(git_repository *repo)
123{
124 int i;
125
126 for (i = 0; i < GIT_CVAR_CACHE_MAX; ++i)
127 repo->cvar_cache[i] = GIT_CVAR_NOT_CACHED;
128}
129