]>
git.proxmox.com Git - libgit2.git/blob - src/config.c
2 * Copyright (C) 2009-2011 the libgit2 contributors
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.
10 #include "hashtable.h"
12 #include "git2/config.h"
18 git_config_file
*file
;
22 void git_config_free(git_config
*cfg
)
25 git_config_file
*file
;
26 file_internal
*internal
;
28 for(i
= 0; i
< cfg
->files
.length
; ++i
){
29 internal
= git_vector_get(&cfg
->files
, i
);
30 file
= internal
->file
;
35 git_vector_free(&cfg
->files
);
39 static int config_backend_cmp(const void *a
, const void *b
)
41 const file_internal
*bk_a
= (const file_internal
*)(a
);
42 const file_internal
*bk_b
= (const file_internal
*)(b
);
44 return bk_b
->priority
- bk_a
->priority
;
47 int git_config_new(git_config
**out
)
51 cfg
= git__malloc(sizeof(git_config
));
55 memset(cfg
, 0x0, sizeof(git_config
));
57 if (git_vector_init(&cfg
->files
, 3, config_backend_cmp
) < 0) {
67 int git_config_add_file_ondisk(git_config
*cfg
, const char *path
, int priority
)
69 git_config_file
*file
= NULL
;
72 error
= git_config_file__ondisk(&file
, path
);
73 if (error
< GIT_SUCCESS
)
76 error
= git_config_add_file(cfg
, file
, priority
);
77 if (error
< GIT_SUCCESS
) {
79 * free manually; the file is not owned by the config
80 * instance yet and will not be freed on cleanup
89 int git_config_open_ondisk(git_config
**cfg
, const char *path
)
93 error
= git_config_new(cfg
);
94 if (error
< GIT_SUCCESS
)
97 error
= git_config_add_file_ondisk(*cfg
, path
, 1);
98 if (error
< GIT_SUCCESS
)
99 git_config_free(*cfg
);
104 int git_config_add_file(git_config
*cfg
, git_config_file
*file
, int priority
)
106 file_internal
*internal
;
111 if ((error
= file
->open(file
)) < GIT_SUCCESS
)
112 return git__rethrow(error
, "Failed to open config file");
114 internal
= git__malloc(sizeof(file_internal
));
115 if (internal
== NULL
)
118 internal
->file
= file
;
119 internal
->priority
= priority
;
121 if (git_vector_insert(&cfg
->files
, internal
) < 0) {
126 git_vector_sort(&cfg
->files
);
127 internal
->file
->cfg
= cfg
;
133 * Loop over all the variables
136 int git_config_foreach(git_config
*cfg
, int (*fn
)(const char *, const char *, void *), void *data
)
138 int ret
= GIT_SUCCESS
;
140 file_internal
*internal
;
141 git_config_file
*file
;
143 for(i
= 0; i
< cfg
->files
.length
&& ret
== 0; ++i
) {
144 internal
= git_vector_get(&cfg
->files
, i
);
145 file
= internal
->file
;
146 ret
= file
->foreach(file
, fn
, data
);
152 int git_config_delete(git_config
*cfg
, const char *name
)
154 return git_config_set_string(cfg
, name
, NULL
);
161 int git_config_set_long(git_config
*cfg
, const char *name
, long int value
)
163 char str_value
[32]; /* All numbers should fit in here */
164 p_snprintf(str_value
, sizeof(str_value
), "%ld", value
);
165 return git_config_set_string(cfg
, name
, str_value
);
168 int git_config_set_int(git_config
*cfg
, const char *name
, int value
)
170 return git_config_set_long(cfg
, name
, value
);
173 int git_config_set_bool(git_config
*cfg
, const char *name
, int value
)
175 return git_config_set_string(cfg
, name
, value
? "true" : "false");
178 int git_config_set_string(git_config
*cfg
, const char *name
, const char *value
)
180 file_internal
*internal
;
181 git_config_file
*file
;
183 if (cfg
->files
.length
== 0)
184 return git__throw(GIT_EINVALIDARGS
, "Cannot set variable value; no files open in the `git_config` instance");
186 internal
= git_vector_get(&cfg
->files
, 0);
187 file
= internal
->file
;
189 return file
->set(file
, name
, value
);
196 int git_config_get_long(git_config
*cfg
, const char *name
, long int *out
)
198 const char *value
, *num_end
;
202 ret
= git_config_get_string(cfg
, name
, &value
);
203 if (ret
< GIT_SUCCESS
)
204 return git__rethrow(ret
, "Failed to get value for %s", name
);
206 ret
= git__strtol32(&num
, value
, &num_end
, 0);
207 if (ret
< GIT_SUCCESS
)
208 return git__rethrow(ret
, "Failed to get value for %s", name
);
223 num
*= 1024 * 1024 * 1024;
226 return git__throw(GIT_EINVALIDTYPE
, "Failed to get value for %s. Value is of invalid type", name
);
234 int git_config_get_int(git_config
*cfg
, const char *name
, int *out
)
239 ret
= git_config_get_long(cfg
, name
, &tmp
);
246 int git_config_get_bool(git_config
*cfg
, const char *name
, int *out
)
249 int error
= GIT_SUCCESS
;
251 error
= git_config_get_string(cfg
, name
, &value
);
252 if (error
< GIT_SUCCESS
)
253 return git__rethrow(error
, "Failed to get value for %s", name
);
255 /* A missing value means true */
261 if (!strcasecmp(value
, "true") ||
262 !strcasecmp(value
, "yes") ||
263 !strcasecmp(value
, "on")) {
267 if (!strcasecmp(value
, "false") ||
268 !strcasecmp(value
, "no") ||
269 !strcasecmp(value
, "off")) {
274 /* Try to parse it as an integer */
275 error
= git_config_get_int(cfg
, name
, out
);
276 if (error
== GIT_SUCCESS
)
279 if (error
< GIT_SUCCESS
)
280 return git__rethrow(error
, "Failed to get value for %s", name
);
284 int git_config_get_string(git_config
*cfg
, const char *name
, const char **out
)
286 file_internal
*internal
;
287 git_config_file
*file
;
288 int error
= GIT_ENOTFOUND
;
291 if (cfg
->files
.length
== 0)
292 return git__throw(GIT_EINVALIDARGS
, "Cannot get variable value; no files open in the `git_config` instance");
294 for (i
= 0; i
< cfg
->files
.length
; ++i
) {
295 internal
= git_vector_get(&cfg
->files
, i
);
296 file
= internal
->file
;
297 if ((error
= file
->get(file
, name
, out
)) == GIT_SUCCESS
)
301 return git__throw(error
, "Config value '%s' not found", name
);
304 int git_config_find_global(char *global_config_path
)
308 home
= getenv("HOME");
312 home
= getenv("USERPROFILE");
316 return git__throw(GIT_EOSERR
, "Failed to open global config file. Cannot locate the user's home directory");
318 git_path_join(global_config_path
, home
, GIT_CONFIG_FILENAME
);
320 if (git_futils_exists(global_config_path
) < GIT_SUCCESS
)
321 return git__throw(GIT_EOSERR
, "Failed to open global config file. The file does not exist");
326 int git_config_open_global(git_config
**out
)
329 char global_path
[GIT_PATH_MAX
];
331 if ((error
= git_config_find_global(global_path
)) < GIT_SUCCESS
)
334 return git_config_open_ondisk(out
, global_path
);