deps/libgit2/src/libgit2/config.c

filter: Statementstmt 61.7% 432 / 700 Branchbran 42.9% 200 / 466 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 54.2% 632 / 1166 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« deps/libgit2/src/libgit2/refspec.c ↑ Summary deps/libgit2/src/libgit2/merge.c »
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
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 "config.h"
9  
10 #include "git2/config.h"
11 #include "git2/sys/config.h"
12  
13 #include "buf.h"
14 #include "config_backend.h"
15 #include "regexp.h"
16 #include "sysdir.h"
17 #include "transaction.h"
18 #include "vector.h"
19 #if GIT_WIN32
20 # include <windows.h>
21 #endif
22  
23 #include <ctype.h>
24  
25 3002 void git_config_entry_free(git_config_entry *entry)
26 {
27 3002 ▶ if (!entry)
Branch: 1 branch
BranchTrueFalseTotal
if (!entry) 2102 900 3002
28 2102 return;
29  
30 900 entry->free(entry);
31 }
32  
33 typedef struct {
34 git_refcount rc;
35  
36 git_config_backend *backend;
37 git_config_level_t level;
38 } backend_internal;
39  
40 1148 static void backend_internal_free(backend_internal *internal)
41 {
42 git_config_backend *backend;
43  
44 1148 backend = internal->backend;
45 1148 backend->free(backend);
46 1148 git__free(internal);
47 1148 }
48  
49 581 static void config_free(git_config *cfg)
50 {
51 size_t i;
52 backend_internal *internal;
53  
54 1729 ▶ for (i = 0; i < cfg->backends.length; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cfg->backends.length; ++i) { 1148 581 1729
55 1148 internal = git_vector_get(&cfg->backends, i);
56 1148 ▶ GIT_REFCOUNT_DEC(internal, backend_internal_free);
Branch: 2 branches
BranchTrueFalseTotal
GIT_REFCOUNT_DEC(internal, backend_internal_free); 1148 0 1148
GIT_REFCOUNT_DEC(internal, backend_internal_free); 1148 0 1148
57 }
58  
59 581 git_vector_free(&cfg->backends);
60  
61 581 git__memzero(cfg, sizeof(*cfg));
62 581 git__free(cfg);
63 581 }
64  
65 601 void git_config_free(git_config *cfg)
66 {
67 601 ▶ if (cfg == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (cfg == NULL) 5 596 601
68 5 return;
69  
70 596 ▶ GIT_REFCOUNT_DEC(cfg, config_free);
Branch: 2 branches
BranchTrueFalseTotal
GIT_REFCOUNT_DEC(cfg, config_free); 581 15 596
GIT_REFCOUNT_DEC(cfg, config_free); 581 0 581
71 }
72  
73 569 static int config_backend_cmp(const void *a, const void *b)
74 {
75 569 const backend_internal *bk_a = (const backend_internal *)(a);
76 569 const backend_internal *bk_b = (const backend_internal *)(b);
77  
78 569 return bk_b->level - bk_a->level;
79 }
80  
81 583 int git_config_new(git_config **out)
82 {
83 git_config *cfg;
84  
85 583 cfg = git__malloc(sizeof(git_config));
86 583 ▶ GIT_ERROR_CHECK_ALLOC(cfg);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(cfg); 0 583 583
87  
88 583 memset(cfg, 0x0, sizeof(git_config));
89  
90 583 ▶ if (git_vector_init(&cfg->backends, 3, config_backend_cmp) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (git_vector_init(&cfg->backends, 3, config_backend_cmp) < 0) { 0 583 583
91 0 git__free(cfg);
92 0 return -1;
93 }
94  
95 583 *out = cfg;
96 583 GIT_REFCOUNT_INC(cfg);
97 583 return 0;
98 }
99  
100 140 int git_config_add_file_ondisk(
101 git_config *cfg,
102 const char *path,
103 git_config_level_t level,
104 const git_repository *repo,
105 int force)
106 {
107 140 git_config_backend *file = NULL;
108 struct stat st;
109 int res;
110  
111 140 ▶ GIT_ASSERT_ARG(cfg);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(cfg); 0 140 140
112 140 ▶ GIT_ASSERT_ARG(path);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(path); 0 140 140
113  
114 140 res = p_stat(path, &st);
115 140 ▶ if (res < 0 && errno != ENOENT && errno != ENOTDIR) {
Branch: 3 branches
BranchTrueFalseTotal
if (res < 0 && errno != ENOENT && errno != ENOTDIR) { 3 137 140
if (res < 0 && errno != ENOENT && errno != ENOTDIR) { 0 3 3
if (res < 0 && errno != ENOENT && errno != ENOTDIR) { 0 0 0
116 0 git_error_set(GIT_ERROR_CONFIG, "failed to stat '%s'", path);
117 0 return -1;
118 }
119  
120 140 ▶ if (git_config_backend_from_file(&file, path) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_config_backend_from_file(&file, path) < 0) 0 140 140
121 0 return -1;
122  
123 140 ▶ if ((res = git_config_add_backend(cfg, file, level, repo, force)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((res = git_config_add_backend(cfg, file, level, repo, force)) < 0) { 0 140 140
124 /*
125 * free manually; the file is not owned by the config
126 * instance yet and will not be freed on cleanup
127 */
128 0 file->free(file);
129 0 return res;
130 }
131  
132 140 return 0;
133 }
134  
135 5 int git_config_open_ondisk(git_config **out, const char *path)
136 {
137 int error;
138 git_config *config;
139  
140 5 *out = NULL;
141  
142 5 ▶ if (git_config_new(&config) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_config_new(&config) < 0) 0 5 5
143 0 return -1;
144  
145 5 ▶ if ((error = git_config_add_file_ondisk(config, path, GIT_CONFIG_LEVEL_LOCAL, NULL, 0)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_config_add_file_ondisk(config, path, GIT_CONFIG_LEVEL_LOCAL, NULL, 0)) < 0) 0 5 5
146 0 git_config_free(config);
147 else
148 5 *out = config;
149  
150 5 return error;
151 }
152  
153 506 int git_config_snapshot(git_config **out, git_config *in)
154 {
155 506 int error = 0;
156 size_t i;
157 backend_internal *internal;
158 git_config *config;
159  
160 506 *out = NULL;
161  
162 506 ▶ if (git_config_new(&config) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_config_new(&config) < 0) 0 506 506
163 0 return -1;
164  
165 1518 ▶ git_vector_foreach(&in->backends, i, internal) {
Branch: 1 branch
BranchTrueFalseTotal
git_vector_foreach(&in->backends, i, internal) { 1012 506 1518
166 git_config_backend *b;
167  
168 1012 ▶ if ((error = internal->backend->snapshot(&b, internal->backend)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = internal->backend->snapshot(&b, internal->backend)) < 0) 0 1012 1012
169 0 break;
170  
171 1012 ▶ if ((error = git_config_add_backend(config, b, internal->level, NULL, 0)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_config_add_backend(config, b, internal->level, NULL, 0)) < 0) { 0 1012 1012
172 0 b->free(b);
173 0 break;
174 }
175 }
176  
177 506 ▶ if (error < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (error < 0) 0 506 506
178 0 git_config_free(config);
179 else
180 506 *out = config;
181  
182 506 return error;
183 }
184  
185 0 static int find_backend_by_level(
186 backend_internal **out,
187 const git_config *cfg,
188 git_config_level_t level)
189 {
190 0 int pos = -1;
191 backend_internal *internal;
192 size_t i;
193  
194 /* when passing GIT_CONFIG_HIGHEST_LEVEL, the idea is to get the config backend
195 * which has the highest level. As config backends are stored in a vector
196 * sorted by decreasing order of level, getting the backend at position 0
197 * will do the job.
198 */
199 0 ▶ if (level == GIT_CONFIG_HIGHEST_LEVEL) {
Branch: 1 branch
BranchTrueFalseTotal
if (level == GIT_CONFIG_HIGHEST_LEVEL) { 0 0 0
200 0 pos = 0;
201 } else {
202 0 ▶ git_vector_foreach(&cfg->backends, i, internal) {
Branch: 1 branch
BranchTrueFalseTotal
git_vector_foreach(&cfg->backends, i, internal) { 0 0 0
203 0 ▶ if (internal->level == level)
Branch: 1 branch
BranchTrueFalseTotal
if (internal->level == level) 0 0 0
204 0 pos = (int)i;
205 }
206 }
207  
208 0 ▶ if (pos == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (pos == -1) { 0 0 0
209 0 git_error_set(GIT_ERROR_CONFIG,
210 "no configuration exists for the given level '%i'", (int)level);
211 0 return GIT_ENOTFOUND;
212 }
213  
214 0 *out = git_vector_get(&cfg->backends, pos);
215  
216 0 return 0;
217 }
218  
219 0 static int duplicate_level(void **old_raw, void *new_raw)
220 {
221 0 backend_internal **old = (backend_internal **)old_raw;
222  
223 0 GIT_UNUSED(new_raw);
224  
225 0 git_error_set(GIT_ERROR_CONFIG, "there already exists a configuration for the given level (%i)", (int)(*old)->level);
226 0 return GIT_EEXISTS;
227 }
228  
229 0 static void try_remove_existing_backend(
230 git_config *cfg,
231 git_config_level_t level)
232 {
233 0 int pos = -1;
234 backend_internal *internal;
235 size_t i;
236  
237 0 ▶ git_vector_foreach(&cfg->backends, i, internal) {
Branch: 1 branch
BranchTrueFalseTotal
git_vector_foreach(&cfg->backends, i, internal) { 0 0 0
238 0 ▶ if (internal->level == level)
Branch: 1 branch
BranchTrueFalseTotal
if (internal->level == level) 0 0 0
239 0 pos = (int)i;
240 }
241  
242 0 ▶ if (pos == -1)
Branch: 1 branch
BranchTrueFalseTotal
if (pos == -1) 0 0 0
243 0 return;
244  
245 0 internal = git_vector_get(&cfg->backends, pos);
246  
247 0 ▶ if (git_vector_remove(&cfg->backends, pos) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_vector_remove(&cfg->backends, pos) < 0) 0 0 0
248 0 return;
249  
250 0 ▶ GIT_REFCOUNT_DEC(internal, backend_internal_free);
Branch: 2 branches
BranchTrueFalseTotal
GIT_REFCOUNT_DEC(internal, backend_internal_free); 0 0 0
GIT_REFCOUNT_DEC(internal, backend_internal_free); 0 0 0
251 }
252  
253 1152 static int git_config__add_internal(
254 git_config *cfg,
255 backend_internal *internal,
256 git_config_level_t level,
257 int force)
258 {
259 int result;
260  
261 /* delete existing config backend for level if it exists */
262 1152 ▶ if (force)
Branch: 1 branch
BranchTrueFalseTotal
if (force) 0 1152 1152
263 0 try_remove_existing_backend(cfg, level);
264  
265 1152 ▶ if ((result = git_vector_insert_sorted(&cfg->backends,
Branch: 1 branch
BranchTrueFalseTotal
if ((result = git_vector_insert_sorted(&cfg->backends, 0 1152 1152
266 internal, &duplicate_level)) < 0)
267 0 return result;
268  
269 1152 git_vector_sort(&cfg->backends);
270 1152 internal->backend->cfg = cfg;
271  
272 1152 GIT_REFCOUNT_INC(internal);
273  
274 1152 return 0;
275 }
276  
277 0 int git_config_open_global(git_config **cfg_out, git_config *cfg)
278 {
279 0 ▶ if (!git_config_open_level(cfg_out, cfg, GIT_CONFIG_LEVEL_XDG))
Branch: 1 branch
BranchTrueFalseTotal
if (!git_config_open_level(cfg_out, cfg, GIT_CONFIG_LEVEL_XDG)) 0 0 0
280 0 return 0;
281  
282 0 return git_config_open_level(cfg_out, cfg, GIT_CONFIG_LEVEL_GLOBAL);
283 }
284  
285 0 int git_config_open_level(
286 git_config **cfg_out,
287 const git_config *cfg_parent,
288 git_config_level_t level)
289 {
290 git_config *cfg;
291 backend_internal *internal;
292 int res;
293  
294 0 ▶ if ((res = find_backend_by_level(&internal, cfg_parent, level)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((res = find_backend_by_level(&internal, cfg_parent, level)) < 0) 0 0 0
295 0 return res;
296  
297 0 ▶ if ((res = git_config_new(&cfg)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((res = git_config_new(&cfg)) < 0) 0 0 0
298 0 return res;
299  
300 0 ▶ if ((res = git_config__add_internal(cfg, internal, level, true)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((res = git_config__add_internal(cfg, internal, level, true)) < 0) { 0 0 0
301 0 git_config_free(cfg);
302 0 return res;
303 }
304  
305 0 *cfg_out = cfg;
306  
307 0 return 0;
308 }
309  
310 1152 int git_config_add_backend(
311 git_config *cfg,
312 git_config_backend *backend,
313 git_config_level_t level,
314 const git_repository *repo,
315 int force)
316 {
317 backend_internal *internal;
318 int result;
319  
320 1152 ▶ GIT_ASSERT_ARG(cfg);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(cfg); 0 1152 1152
321 1152 ▶ GIT_ASSERT_ARG(backend);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(backend); 0 1152 1152
322  
323 1152 ▶ GIT_ERROR_CHECK_VERSION(backend, GIT_CONFIG_BACKEND_VERSION, "git_config_backend");
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_VERSION(backend, GIT_CONFIG_BACKEND_VERSION, "git_config_backend"); 0 1152 1152
324  
325 1152 ▶ if ((result = backend->open(backend, level, repo)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((result = backend->open(backend, level, repo)) < 0) 0 1152 1152
326 0 return result;
327  
328 1152 internal = git__malloc(sizeof(backend_internal));
329 1152 ▶ GIT_ERROR_CHECK_ALLOC(internal);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(internal); 0 1152 1152
330  
331 1152 memset(internal, 0x0, sizeof(backend_internal));
332  
333 1152 internal->backend = backend;
334 1152 internal->level = level;
335  
336 1152 ▶ if ((result = git_config__add_internal(cfg, internal, level, force)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((result = git_config__add_internal(cfg, internal, level, force)) < 0) { 0 1152 1152
337 0 git__free(internal);
338 0 return result;
339 }
340  
341 1152 return 0;
342 }
343  
344 /*
345 * Loop over all the variables
346 */
347  
348 typedef struct {
349 git_config_iterator parent;
350 git_config_iterator *current;
351 const git_config *cfg;
352 git_regexp regex;
353 size_t i;
354 } all_iter;
355  
356 199 static int find_next_backend(size_t *out, const git_config *cfg, size_t i)
357 {
358 backend_internal *internal;
359  
360 199 ▶ for (; i > 0; --i) {
Branch: 1 branch
BranchTrueFalseTotal
for (; i > 0; --i) { 133 66 199
361 133 internal = git_vector_get(&cfg->backends, i - 1);
362 133 ▶ if (!internal || !internal->backend)
Branch: 2 branches
BranchTrueFalseTotal
if (!internal || !internal->backend) 133 0 133
if (!internal || !internal->backend) 0 133 133
363 0 continue;
364  
365 133 *out = i;
366 133 return 0;
367 }
368  
369 66 return -1;
370 }
371  
372 1244 static int all_iter_next(git_config_entry **entry, git_config_iterator *_iter)
373 {
374 1244 all_iter *iter = (all_iter *) _iter;
375 backend_internal *internal;
376 git_config_backend *backend;
377 size_t i;
378 1244 int error = 0;
379  
380 2421 if (iter->current != NULL &&
381 1177 (error = iter->current->next(entry, iter->current)) == 0) {
382 1045 return 0;
383 }
384  
385 199 ▶ if (error < 0 && error != GIT_ITEROVER)
Branch: 2 branches
BranchTrueFalseTotal
if (error < 0 && error != GIT_ITEROVER) 67 132 199
if (error < 0 && error != GIT_ITEROVER) 132 0 132
386 0 return error;
387  
388 do {
389 199 ▶ if (find_next_backend(&i, iter->cfg, iter->i) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (find_next_backend(&i, iter->cfg, iter->i) < 0) 66 133 199
390 66 return GIT_ITEROVER;
391  
392 133 internal = git_vector_get(&iter->cfg->backends, i - 1);
393 133 backend = internal->backend;
394 133 iter->i = i - 1;
395  
396 133 ▶ if (iter->current)
Branch: 1 branch
BranchTrueFalseTotal
if (iter->current) 66 67 133
397 66 iter->current->free(iter->current);
398  
399 133 iter->current = NULL;
400 133 error = backend->iterator(&iter->current, backend);
401 133 ▶ if (error == GIT_ENOTFOUND)
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_ENOTFOUND) 0 133 133
402 0 continue;
403  
404 133 ▶ if (error < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (error < 0) 0 133 133
405 0 return error;
406  
407 133 error = iter->current->next(entry, iter->current);
408 /* If this backend is empty, then keep going */
409 133 ▶ if (error == GIT_ITEROVER)
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_ITEROVER) 0 133 133
410 0 continue;
411  
412 133 return error;
413  
414 } while(1);
415  
416 return GIT_ITEROVER;
417 }
418  
419 55 static int all_iter_glob_next(git_config_entry **entry, git_config_iterator *_iter)
420 {
421 int error;
422 55 all_iter *iter = (all_iter *) _iter;
423  
424 /*
425 * We use the "normal" function to grab the next one across
426 * backends and then apply the regex
427 */
428 793 ▶ while ((error = all_iter_next(entry, _iter)) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
while ((error = all_iter_next(entry, _iter)) == 0) { 752 41 793
429 /* skip non-matching keys if regexp was provided */
430 752 ▶ if (git_regexp_match(&iter->regex, (*entry)->name) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_regexp_match(&iter->regex, (*entry)->name) != 0) 738 14 752
431 738 continue;
432  
433 /* and simply return if we like the entry's name */
434 14 return 0;
435 }
436  
437 41 return error;
438 }
439  
440 67 static void all_iter_free(git_config_iterator *_iter)
441 {
442 67 all_iter *iter = (all_iter *) _iter;
443  
444 67 ▶ if (iter->current)
Branch: 1 branch
BranchTrueFalseTotal
if (iter->current) 67 0 67
445 67 iter->current->free(iter->current);
446  
447 67 git__free(iter);
448 67 }
449  
450 41 static void all_iter_glob_free(git_config_iterator *_iter)
451 {
452 41 all_iter *iter = (all_iter *) _iter;
453  
454 41 git_regexp_dispose(&iter->regex);
455 41 all_iter_free(_iter);
456 41 }
457  
458 26 int git_config_iterator_new(git_config_iterator **out, const git_config *cfg)
459 {
460 all_iter *iter;
461  
462 26 iter = git__calloc(1, sizeof(all_iter));
463 26 ▶ GIT_ERROR_CHECK_ALLOC(iter);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(iter); 0 26 26
464  
465 26 iter->parent.free = all_iter_free;
466 26 iter->parent.next = all_iter_next;
467  
468 26 iter->i = cfg->backends.length;
469 26 iter->cfg = cfg;
470  
471 26 *out = (git_config_iterator *) iter;
472  
473 26 return 0;
474 }
475  
476 43 int git_config_iterator_glob_new(git_config_iterator **out, const git_config *cfg, const char *regexp)
477 {
478 all_iter *iter;
479 int result;
480  
481 43 ▶ if (regexp == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (regexp == NULL) 2 41 43
482 2 return git_config_iterator_new(out, cfg);
483  
484 41 iter = git__calloc(1, sizeof(all_iter));
485 41 ▶ GIT_ERROR_CHECK_ALLOC(iter);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(iter); 0 41 41
486  
487 41 ▶ if ((result = git_regexp_compile(&iter->regex, regexp, 0)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((result = git_regexp_compile(&iter->regex, regexp, 0)) < 0) { 0 41 41
488 0 git__free(iter);
489 0 return -1;
490 }
491  
492 41 iter->parent.next = all_iter_glob_next;
493 41 iter->parent.free = all_iter_glob_free;
494 41 iter->i = cfg->backends.length;
495 41 iter->cfg = cfg;
496  
497 41 *out = (git_config_iterator *) iter;
498  
499 41 return 0;
500 }
501  
502 2 int git_config_foreach(
503 const git_config *cfg, git_config_foreach_cb cb, void *payload)
504 {
505 2 return git_config_foreach_match(cfg, NULL, cb, payload);
506 }
507  
508 0 int git_config_backend_foreach_match(
509 git_config_backend *backend,
510 const char *regexp,
511 git_config_foreach_cb cb,
512 void *payload)
513 {
514 git_config_entry *entry;
515 git_config_iterator *iter;
516 git_regexp regex;
517 0 int error = 0;
518  
519 0 ▶ GIT_ASSERT_ARG(backend);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(backend); 0 0 0
520 0 ▶ GIT_ASSERT_ARG(cb);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(cb); 0 0 0
521  
522 0 ▶ if (regexp && git_regexp_compile(&regex, regexp, 0) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (regexp && git_regexp_compile(&regex, regexp, 0) < 0) 0 0 0
if (regexp && git_regexp_compile(&regex, regexp, 0) < 0) 0 0 0
523 0 return -1;
524  
525 0 ▶ if ((error = backend->iterator(&iter, backend)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((error = backend->iterator(&iter, backend)) < 0) { 0 0 0
526 0 iter = NULL;
527 0 return -1;
528 }
529  
530 0 ▶ while (!(iter->next(&entry, iter) < 0)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!(iter->next(&entry, iter) < 0)) { 0 0 0
531 /* skip non-matching keys if regexp was provided */
532 0 ▶ if (regexp && git_regexp_match(&regex, entry->name) != 0)
Branch: 2 branches
BranchTrueFalseTotal
if (regexp && git_regexp_match(&regex, entry->name) != 0) 0 0 0
if (regexp && git_regexp_match(&regex, entry->name) != 0) 0 0 0
533 0 continue;
534  
535 /* abort iterator on non-zero return value */
536 0 ▶ if ((error = cb(entry, payload)) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((error = cb(entry, payload)) != 0) { 0 0 0
537 0 git_error_set_after_callback(error);
538 0 break;
539 }
540 }
541  
542 0 ▶ if (regexp != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (regexp != NULL) 0 0 0
543 0 git_regexp_dispose(&regex);
544  
545 0 iter->free(iter);
546  
547 0 return error;
548 }
549  
550 9 int git_config_foreach_match(
551 const git_config *cfg,
552 const char *regexp,
553 git_config_foreach_cb cb,
554 void *payload)
555 {
556 int error;
557 git_config_iterator *iter;
558 git_config_entry *entry;
559  
560 9 ▶ if ((error = git_config_iterator_glob_new(&iter, cfg, regexp)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_config_iterator_glob_new(&iter, cfg, regexp)) < 0) 0 9 9
561 0 return error;
562  
563 35 ▶ while (!(error = git_config_next(&entry, iter))) {
Branch: 1 branch
BranchTrueFalseTotal
while (!(error = git_config_next(&entry, iter))) { 27 8 35
564 27 ▶ if ((error = cb(entry, payload)) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((error = cb(entry, payload)) != 0) { 1 26 27
565 1 git_error_set_after_callback(error);
566 1 break;
567 }
568 }
569  
570 9 git_config_iterator_free(iter);
571  
572 9 ▶ if (error == GIT_ITEROVER)
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_ITEROVER) 8 1 9
573 8 error = 0;
574  
575 9 return error;
576 }
577  
578 /**************
579 * Setters
580 **************/
581  
582 typedef enum {
583 BACKEND_USE_SET,
584 BACKEND_USE_DELETE
585 } backend_use;
586  
587 static const char *uses[] = {
588 "set",
589 "delete"
590 };
591  
592 61 static int get_backend_for_use(git_config_backend **out,
593 git_config *cfg, const char *name, backend_use use)
594 {
595 size_t i;
596 backend_internal *backend;
597  
598 61 *out = NULL;
599  
600 61 ▶ if (git_vector_length(&cfg->backends) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (git_vector_length(&cfg->backends) == 0) { 0 61 61
601 0 git_error_set(GIT_ERROR_CONFIG,
602 "cannot %s value for '%s' when no config backends exist",
603 uses[use], name);
604 0 return GIT_ENOTFOUND;
605 }
606  
607 61 ▶ git_vector_foreach(&cfg->backends, i, backend) {
Branch: 1 branch
BranchTrueFalseTotal
git_vector_foreach(&cfg->backends, i, backend) { 61 0 61
608 61 ▶ if (!backend->backend->readonly) {
Branch: 1 branch
BranchTrueFalseTotal
if (!backend->backend->readonly) { 61 0 61
609 61 *out = backend->backend;
610 61 return 0;
611 }
612 }
613  
614 0 git_error_set(GIT_ERROR_CONFIG,
615 "cannot %s value for '%s' when all config backends are readonly",
616 uses[use], name);
617 0 return GIT_ENOTFOUND;
618 }
619  
620 15 int git_config_delete_entry(git_config *cfg, const char *name)
621 {
622 git_config_backend *backend;
623  
624 15 ▶ if (get_backend_for_use(&backend, cfg, name, BACKEND_USE_DELETE) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (get_backend_for_use(&backend, cfg, name, BACKEND_USE_DELETE) < 0) 0 15 15
625 0 return GIT_ENOTFOUND;
626  
627 15 return backend->del(backend, name);
628 }
629  
630 6 int git_config_set_int64(git_config *cfg, const char *name, int64_t value)
631 {
632 char str_value[32]; /* All numbers should fit in here */
633 6 p_snprintf(str_value, sizeof(str_value), "%" PRId64, value);
634 6 return git_config_set_string(cfg, name, str_value);
635 }
636  
637 6 int git_config_set_int32(git_config *cfg, const char *name, int32_t value)
638 {
639 6 return git_config_set_int64(cfg, name, (int64_t)value);
640 }
641  
642 17 int git_config_set_bool(git_config *cfg, const char *name, int value)
643 {
644 17 ▶ return git_config_set_string(cfg, name, value ? "true" : "false");
Branch: 1 branch
BranchTrueFalseTotal
return git_config_set_string(cfg, name, value ? "true" : "false"); 12 5 17
645 }
646  
647 41 int git_config_set_string(git_config *cfg, const char *name, const char *value)
648 {
649 int error;
650 git_config_backend *backend;
651  
652 41 ▶ if (!value) {
Branch: 1 branch
BranchTrueFalseTotal
if (!value) { 0 41 41
653 0 git_error_set(GIT_ERROR_CONFIG, "the value to set cannot be NULL");
654 0 return -1;
655 }
656  
657 41 ▶ if (get_backend_for_use(&backend, cfg, name, BACKEND_USE_SET) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (get_backend_for_use(&backend, cfg, name, BACKEND_USE_SET) < 0) 0 41 41
658 0 return GIT_ENOTFOUND;
659  
660 41 error = backend->set(backend, name, value);
661  
662 41 ▶ if (!error && GIT_REFCOUNT_OWNER(cfg) != NULL)
Branch: 2 branches
BranchTrueFalseTotal
if (!error && GIT_REFCOUNT_OWNER(cfg) != NULL) 41 0 41
if (!error && GIT_REFCOUNT_OWNER(cfg) != NULL) 21 20 41
663 21 git_repository__configmap_lookup_cache_clear(GIT_REFCOUNT_OWNER(cfg));
664  
665 41 return error;
666 }
667  
668 0 int git_config__update_entry(
669 git_config *config,
670 const char *key,
671 const char *value,
672 bool overwrite_existing,
673 bool only_if_existing)
674 {
675 0 int error = 0;
676 0 git_config_entry *ce = NULL;
677  
678 0 ▶ if ((error = git_config__lookup_entry(&ce, config, key, false)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_config__lookup_entry(&ce, config, key, false)) < 0) 0 0 0
679 0 return error;
680  
681 0 ▶ if (!ce && only_if_existing) /* entry doesn't exist */
Branch: 2 branches
BranchTrueFalseTotal
if (!ce && only_if_existing) /* entry doesn't exist */ 0 0 0
if (!ce && only_if_existing) /* entry doesn't exist */ 0 0 0
682 0 return 0;
683 0 ▶ if (ce && !overwrite_existing) /* entry would be overwritten */
Branch: 2 branches
BranchTrueFalseTotal
if (ce && !overwrite_existing) /* entry would be overwritten */ 0 0 0
if (ce && !overwrite_existing) /* entry would be overwritten */ 0 0 0
684 0 return 0;
685 0 ▶ if (value && ce && ce->value && !strcmp(ce->value, value)) /* no change */
Branch: 4 branches
BranchTrueFalseTotal
if (value && ce && ce->value && !strcmp(ce->value, value)) /* no change */ 0 0 0
if (value && ce && ce->value && !strcmp(ce->value, value)) /* no change */ 0 0 0
if (value && ce && ce->value && !strcmp(ce->value, value)) /* no change */ 0 0 0
if (value && ce && ce->value && !strcmp(ce->value, value)) /* no change */ 0 0 0
686 0 return 0;
687 0 ▶ if (!value && (!ce || !ce->value)) /* asked to delete absent entry */
Branch: 3 branches
BranchTrueFalseTotal
if (!value && (!ce || !ce->value)) /* asked to delete absent entry */ 0 0 0
if (!value && (!ce || !ce->value)) /* asked to delete absent entry */ 0 0 0
if (!value && (!ce || !ce->value)) /* asked to delete absent entry */ 0 0 0
688 0 return 0;
689  
690 0 ▶ if (!value)
Branch: 1 branch
BranchTrueFalseTotal
if (!value) 0 0 0
691 0 error = git_config_delete_entry(config, key);
692 else
693 0 error = git_config_set_string(config, key, value);
694  
695 0 git_config_entry_free(ce);
696 0 return error;
697 }
698  
699 /***********
700 * Getters
701 ***********/
702  
703 184 static int config_error_notfound(const char *name)
704 {
705 184 git_error_set(GIT_ERROR_CONFIG, "config value '%s' was not found", name);
706 184 return GIT_ENOTFOUND;
707 }
708  
709 enum {
710 GET_ALL_ERRORS = 0,
711 GET_NO_MISSING = 1,
712 GET_NO_ERRORS = 2
713 };
714  
715 3061 static int get_entry(
716 git_config_entry **out,
717 const git_config *cfg,
718 const char *name,
719 bool normalize_name,
720 int want_errors)
721 {
722 3061 int res = GIT_ENOTFOUND;
723 3061 const char *key = name;
724 3061 char *normalized = NULL;
725 size_t i;
726 backend_internal *internal;
727  
728 3061 *out = NULL;
729  
730 3061 ▶ if (normalize_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (normalize_name) { 507 2554 3061
731 507 ▶ if ((res = git_config__normalize_name(name, &normalized)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((res = git_config__normalize_name(name, &normalized)) < 0) 0 507 507
732 0 goto cleanup;
733 507 key = normalized;
734 }
735  
736 3061 res = GIT_ENOTFOUND;
737 7384 ▶ git_vector_foreach(&cfg->backends, i, internal) {
Branch: 1 branch
BranchTrueFalseTotal
git_vector_foreach(&cfg->backends, i, internal) { 5223 2161 7384
738 5223 ▶ if (!internal || !internal->backend)
Branch: 2 branches
BranchTrueFalseTotal
if (!internal || !internal->backend) 5223 0 5223
if (!internal || !internal->backend) 0 5223 5223
739 0 continue;
740  
741 5223 res = internal->backend->get(internal->backend, key, out);
742 5223 ▶ if (res != GIT_ENOTFOUND)
Branch: 1 branch
BranchTrueFalseTotal
if (res != GIT_ENOTFOUND) 900 4323 5223
743 900 break;
744 }
745  
746 3061 git__free(normalized);
747  
748 3061 cleanup:
749 3061 ▶ if (res == GIT_ENOTFOUND)
Branch: 1 branch
BranchTrueFalseTotal
if (res == GIT_ENOTFOUND) 2161 900 3061
750 2161 ▶ res = (want_errors > GET_ALL_ERRORS) ? 0 : config_error_notfound(name);
Branch: 1 branch
BranchTrueFalseTotal
res = (want_errors > GET_ALL_ERRORS) ? 0 : config_error_notfound(name); 174 1987 2161
751 900 ▶ else if (res && (want_errors == GET_NO_ERRORS)) {
Branch: 2 branches
BranchTrueFalseTotal
else if (res && (want_errors == GET_NO_ERRORS)) { 0 900 900
else if (res && (want_errors == GET_NO_ERRORS)) { 0 0 0
752 0 git_error_clear();
753 0 res = 0;
754 }
755  
756 3061 return res;
757 }
758  
759 77 int git_config_get_entry(
760 git_config_entry **out, const git_config *cfg, const char *name)
761 {
762 77 return get_entry(out, cfg, name, true, GET_ALL_ERRORS);
763 }
764  
765 1898 int git_config__lookup_entry(
766 git_config_entry **out,
767 const git_config *cfg,
768 const char *key,
769 bool no_errors)
770 {
771 1898 ▶ return get_entry(
Branch: 1 branch
BranchTrueFalseTotal
return get_entry( 284 1614 1898
772 out, cfg, key, false, no_errors ? GET_NO_ERRORS : GET_NO_MISSING);
773 }
774  
775 0 int git_config_get_mapped(
776 int *out,
777 const git_config *cfg,
778 const char *name,
779 const git_configmap *maps,
780 size_t map_n)
781 {
782 git_config_entry *entry;
783 int ret;
784  
785 0 ▶ if ((ret = get_entry(&entry, cfg, name, true, GET_ALL_ERRORS)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((ret = get_entry(&entry, cfg, name, true, GET_ALL_ERRORS)) < 0) 0 0 0
786 0 return ret;
787  
788 0 ret = git_config_lookup_map_value(out, maps, map_n, entry->value);
789 0 git_config_entry_free(entry);
790  
791 0 return ret;
792 }
793  
794 28 int git_config_get_int64(int64_t *out, const git_config *cfg, const char *name)
795 {
796 git_config_entry *entry;
797 int ret;
798  
799 28 ▶ if ((ret = get_entry(&entry, cfg, name, true, GET_ALL_ERRORS)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((ret = get_entry(&entry, cfg, name, true, GET_ALL_ERRORS)) < 0) 28 0 28
800 28 return ret;
801  
802 0 ret = git_config_parse_int64(out, entry->value);
803 0 git_config_entry_free(entry);
804  
805 0 return ret;
806 }
807  
808 65 int git_config_get_int32(int32_t *out, const git_config *cfg, const char *name)
809 {
810 git_config_entry *entry;
811 int ret;
812  
813 65 ▶ if ((ret = get_entry(&entry, cfg, name, true, GET_ALL_ERRORS)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((ret = get_entry(&entry, cfg, name, true, GET_ALL_ERRORS)) < 0) 1 64 65
814 1 return ret;
815  
816 64 ret = git_config_parse_int32(out, entry->value);
817 64 git_config_entry_free(entry);
818  
819 64 return ret;
820 }
821  
822 95 int git_config_get_bool(int *out, const git_config *cfg, const char *name)
823 {
824 git_config_entry *entry;
825 int ret;
826  
827 95 ▶ if ((ret = get_entry(&entry, cfg, name, true, GET_ALL_ERRORS)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((ret = get_entry(&entry, cfg, name, true, GET_ALL_ERRORS)) < 0) 30 65 95
828 30 return ret;
829  
830 65 ret = git_config_parse_bool(out, entry->value);
831 65 git_config_entry_free(entry);
832  
833 65 return ret;
834 }
835  
836 202 static int is_readonly(const git_config *cfg)
837 {
838 size_t i;
839 backend_internal *internal;
840  
841 606 ▶ git_vector_foreach(&cfg->backends, i, internal) {
Branch: 1 branch
BranchTrueFalseTotal
git_vector_foreach(&cfg->backends, i, internal) { 404 202 606
842 404 ▶ if (!internal || !internal->backend)
Branch: 2 branches
BranchTrueFalseTotal
if (!internal || !internal->backend) 404 0 404
if (!internal || !internal->backend) 0 404 404
843 0 continue;
844  
845 404 ▶ if (!internal->backend->readonly)
Branch: 1 branch
BranchTrueFalseTotal
if (!internal->backend->readonly) 0 404 404
846 0 return 0;
847 }
848  
849 202 return 1;
850 }
851  
852 0 static int git_config__parse_path(git_str *out, const char *value)
853 {
854 0 ▶ GIT_ASSERT_ARG(out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(out); 0 0 0
855 0 ▶ GIT_ASSERT_ARG(value);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(value); 0 0 0
856  
857 0 ▶ if (value[0] == '~') {
Branch: 1 branch
BranchTrueFalseTotal
if (value[0] == '~') { 0 0 0
858 0 ▶ if (value[1] != '\0' && value[1] != '/') {
Branch: 2 branches
BranchTrueFalseTotal
if (value[1] != '\0' && value[1] != '/') { 0 0 0
if (value[1] != '\0' && value[1] != '/') { 0 0 0
859 0 git_error_set(GIT_ERROR_CONFIG, "retrieving a homedir by name is not supported");
860 0 return -1;
861 }
862  
863 0 ▶ return git_sysdir_expand_global_file(out, value[1] ? &value[2] : NULL);
Branch: 1 branch
BranchTrueFalseTotal
return git_sysdir_expand_global_file(out, value[1] ? &value[2] : NULL); 0 0 0
864 }
865  
866 0 return git_str_sets(out, value);
867 }
868  
869 0 int git_config_parse_path(git_buf *out, const char *value)
870 {
871 0 ▶ GIT_BUF_WRAP_PRIVATE(out, git_config__parse_path, value);
Branch: 2 branches
BranchTrueFalseTotal
GIT_BUF_WRAP_PRIVATE(out, git_config__parse_path, value); 0 0 0
GIT_BUF_WRAP_PRIVATE(out, git_config__parse_path, value); 0 0 0
872 }
873  
874 0 int git_config_get_path(
875 git_buf *out,
876 const git_config *cfg,
877 const char *name)
878 {
879 0 ▶ GIT_BUF_WRAP_PRIVATE(out, git_config__get_path, cfg, name);
Branch: 2 branches
BranchTrueFalseTotal
GIT_BUF_WRAP_PRIVATE(out, git_config__get_path, cfg, name); 0 0 0
GIT_BUF_WRAP_PRIVATE(out, git_config__get_path, cfg, name); 0 0 0
880 }
881  
882 0 int git_config__get_path(
883 git_str *out,
884 const git_config *cfg,
885 const char *name)
886 {
887 git_config_entry *entry;
888 int error;
889  
890 0 ▶ if ((error = get_entry(&entry, cfg, name, true, GET_ALL_ERRORS)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = get_entry(&entry, cfg, name, true, GET_ALL_ERRORS)) < 0) 0 0 0
891 0 return error;
892  
893 0 error = git_config__parse_path(out, entry->value);
894 0 git_config_entry_free(entry);
895  
896 0 return error;
897 }
898  
899 202 int git_config_get_string(
900 const char **out, const git_config *cfg, const char *name)
901 {
902 git_config_entry *entry;
903 int ret;
904  
905 202 ▶ if (!is_readonly(cfg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_readonly(cfg)) { 0 202 202
906 0 git_error_set(GIT_ERROR_CONFIG, "get_string called on a live config object");
907 0 return -1;
908 }
909  
910 202 ret = get_entry(&entry, cfg, name, true, GET_ALL_ERRORS);
911 202 ▶ *out = !ret ? (entry->value ? entry->value : "") : NULL;
Branch: 2 branches
BranchTrueFalseTotal
*out = !ret ? (entry->value ? entry->value : "") : NULL; 171 31 202
*out = !ret ? (entry->value ? entry->value : "") : NULL; 171 0 171
912  
913 202 git_config_entry_free(entry);
914  
915 202 return ret;
916 }
917  
918 18 int git_config_get_string_buf(
919 git_buf *out, const git_config *cfg, const char *name)
920 {
921 18 ▶ GIT_BUF_WRAP_PRIVATE(out, git_config__get_string_buf, cfg, name);
Branch: 2 branches
BranchTrueFalseTotal
GIT_BUF_WRAP_PRIVATE(out, git_config__get_string_buf, cfg, name); 18 0 18
GIT_BUF_WRAP_PRIVATE(out, git_config__get_string_buf, cfg, name); 15 3 18
922 }
923  
924 40 int git_config__get_string_buf(
925 git_str *out, const git_config *cfg, const char *name)
926 {
927 git_config_entry *entry;
928 int ret;
929 const char *str;
930  
931 40 ▶ GIT_ASSERT_ARG(out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(out); 0 40 40
932 40 ▶ GIT_ASSERT_ARG(cfg);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(cfg); 0 40 40
933  
934 40 ret = get_entry(&entry, cfg, name, true, GET_ALL_ERRORS);
935 40 ▶ str = !ret ? (entry->value ? entry->value : "") : NULL;
Branch: 2 branches
BranchTrueFalseTotal
str = !ret ? (entry->value ? entry->value : "") : NULL; 20 20 40
str = !ret ? (entry->value ? entry->value : "") : NULL; 20 0 20
936  
937 40 ▶ if (str)
Branch: 1 branch
BranchTrueFalseTotal
if (str) 20 20 40
938 20 ret = git_str_puts(out, str);
939  
940 40 git_config_entry_free(entry);
941  
942 40 return ret;
943 }
944  
945 2 char *git_config__get_string_force(
946 const git_config *cfg, const char *key, const char *fallback_value)
947 {
948 git_config_entry *entry;
949 char *ret;
950  
951 2 get_entry(&entry, cfg, key, false, GET_NO_ERRORS);
952 2 ▶ ret = (entry && entry->value) ? git__strdup(entry->value) : fallback_value ? git__strdup(fallback_value) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
ret = (entry && entry->value) ? git__strdup(entry->value) : fallback_value ? git__strdup(fallback_value) : NULL; 0 2 2
ret = (entry && entry->value) ? git__strdup(entry->value) : fallback_value ? git__strdup(fallback_value) : NULL; 0 0 0
ret = (entry && entry->value) ? git__strdup(entry->value) : fallback_value ? git__strdup(fallback_value) : NULL; 2 0 2
953 2 git_config_entry_free(entry);
954  
955 2 return ret;
956 }
957  
958 598 int git_config__get_bool_force(
959 const git_config *cfg, const char *key, int fallback_value)
960 {
961 598 int val = fallback_value;
962 git_config_entry *entry;
963  
964 598 get_entry(&entry, cfg, key, false, GET_NO_ERRORS);
965  
966 598 ▶ if (entry && git_config_parse_bool(&val, entry->value) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (entry && git_config_parse_bool(&val, entry->value) < 0) 225 373 598
if (entry && git_config_parse_bool(&val, entry->value) < 0) 0 225 225
967 0 git_error_clear();
968  
969 598 git_config_entry_free(entry);
970 598 return val;
971 }
972  
973 56 int git_config__get_int_force(
974 const git_config *cfg, const char *key, int fallback_value)
975 {
976 56 int32_t val = (int32_t)fallback_value;
977 git_config_entry *entry;
978  
979 56 get_entry(&entry, cfg, key, false, GET_NO_ERRORS);
980  
981 56 ▶ if (entry && git_config_parse_int32(&val, entry->value) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (entry && git_config_parse_int32(&val, entry->value) < 0) 0 56 56
if (entry && git_config_parse_int32(&val, entry->value) < 0) 0 0 0
982 0 git_error_clear();
983  
984 56 git_config_entry_free(entry);
985 56 return (int)val;
986 }
987  
988 20 int git_config_get_multivar_foreach(
989 const git_config *cfg, const char *name, const char *regexp,
990 git_config_foreach_cb cb, void *payload)
991 {
992 int err, found;
993 git_config_iterator *iter;
994 git_config_entry *entry;
995  
996 20 ▶ if ((err = git_config_multivar_iterator_new(&iter, cfg, name, regexp)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((err = git_config_multivar_iterator_new(&iter, cfg, name, regexp)) < 0) 0 20 20
997 0 return err;
998  
999 20 found = 0;
1000 30 ▶ while ((err = iter->next(&entry, iter)) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
while ((err = iter->next(&entry, iter)) == 0) { 10 20 30
1001 10 found = 1;
1002  
1003 10 ▶ if ((err = cb(entry, payload)) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((err = cb(entry, payload)) != 0) { 0 10 10
1004 0 git_error_set_after_callback(err);
1005 0 break;
1006 }
1007 }
1008  
1009 20 iter->free(iter);
1010 20 ▶ if (err == GIT_ITEROVER)
Branch: 1 branch
BranchTrueFalseTotal
if (err == GIT_ITEROVER) 20 0 20
1011 20 err = 0;
1012  
1013 20 ▶ if (found == 0 && err == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (found == 0 && err == 0) 10 10 20
if (found == 0 && err == 0) 10 0 10
1014 10 err = config_error_notfound(name);
1015  
1016 20 return err;
1017 }
1018  
1019 typedef struct {
1020 git_config_iterator parent;
1021 git_config_iterator *iter;
1022 char *name;
1023 git_regexp regex;
1024 int have_regex;
1025 } multivar_iter;
1026  
1027 39 static int multivar_iter_next(git_config_entry **entry, git_config_iterator *_iter)
1028 {
1029 39 multivar_iter *iter = (multivar_iter *) _iter;
1030 39 int error = 0;
1031  
1032 437 ▶ while ((error = iter->iter->next(entry, iter->iter)) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
while ((error = iter->iter->next(entry, iter->iter)) == 0) { 413 24 437
1033 413 ▶ if (git__strcmp(iter->name, (*entry)->name))
Branch: 1 branch
BranchTrueFalseTotal
if (git__strcmp(iter->name, (*entry)->name)) 398 15 413
1034 398 continue;
1035  
1036 15 ▶ if (!iter->have_regex)
Branch: 1 branch
BranchTrueFalseTotal
if (!iter->have_regex) 15 0 15
1037 15 return 0;
1038  
1039 0 ▶ if (git_regexp_match(&iter->regex, (*entry)->value) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_regexp_match(&iter->regex, (*entry)->value) == 0) 0 0 0
1040 0 return 0;
1041 }
1042  
1043 24 return error;
1044 }
1045  
1046 24 static void multivar_iter_free(git_config_iterator *_iter)
1047 {
1048 24 multivar_iter *iter = (multivar_iter *) _iter;
1049  
1050 24 iter->iter->free(iter->iter);
1051  
1052 24 git__free(iter->name);
1053 24 ▶ if (iter->have_regex)
Branch: 1 branch
BranchTrueFalseTotal
if (iter->have_regex) 0 24 24
1054 0 git_regexp_dispose(&iter->regex);
1055 24 git__free(iter);
1056 24 }
1057  
1058 24 int git_config_multivar_iterator_new(git_config_iterator **out, const git_config *cfg, const char *name, const char *regexp)
1059 {
1060 24 multivar_iter *iter = NULL;
1061 24 git_config_iterator *inner = NULL;
1062 int error;
1063  
1064 24 ▶ if ((error = git_config_iterator_new(&inner, cfg)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_config_iterator_new(&inner, cfg)) < 0) 0 24 24
1065 0 return error;
1066  
1067 24 iter = git__calloc(1, sizeof(multivar_iter));
1068 24 ▶ GIT_ERROR_CHECK_ALLOC(iter);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(iter); 0 24 24
1069  
1070 24 ▶ if ((error = git_config__normalize_name(name, &iter->name)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_config__normalize_name(name, &iter->name)) < 0) 0 24 24
1071 0 goto on_error;
1072  
1073 24 ▶ if (regexp != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (regexp != NULL) { 0 24 24
1074 0 ▶ if ((error = git_regexp_compile(&iter->regex, regexp, 0)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_regexp_compile(&iter->regex, regexp, 0)) < 0) 0 0 0
1075 0 goto on_error;
1076  
1077 0 iter->have_regex = 1;
1078 }
1079  
1080 24 iter->iter = inner;
1081 24 iter->parent.free = multivar_iter_free;
1082 24 iter->parent.next = multivar_iter_next;
1083  
1084 24 *out = (git_config_iterator *) iter;
1085  
1086 24 return 0;
1087  
1088 0 on_error:
1089  
1090 0 inner->free(inner);
1091 0 git__free(iter);
1092 0 return error;
1093 }
1094  
1095 5 int git_config_set_multivar(git_config *cfg, const char *name, const char *regexp, const char *value)
1096 {
1097 git_config_backend *backend;
1098  
1099 5 ▶ if (get_backend_for_use(&backend, cfg, name, BACKEND_USE_DELETE) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (get_backend_for_use(&backend, cfg, name, BACKEND_USE_DELETE) < 0) 0 5 5
1100 0 return GIT_ENOTFOUND;
1101  
1102 5 return backend->set_multivar(backend, name, regexp, value);
1103 }
1104  
1105 0 int git_config_delete_multivar(git_config *cfg, const char *name, const char *regexp)
1106 {
1107 git_config_backend *backend;
1108  
1109 0 ▶ if (get_backend_for_use(&backend, cfg, name, BACKEND_USE_DELETE) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (get_backend_for_use(&backend, cfg, name, BACKEND_USE_DELETE) < 0) 0 0 0
1110 0 return GIT_ENOTFOUND;
1111  
1112 0 return backend->del_multivar(backend, name, regexp);
1113 }
1114  
1115 8573 int git_config_next(git_config_entry **entry, git_config_iterator *iter)
1116 {
1117 8573 return iter->next(entry, iter);
1118 }
1119  
1120 1059 void git_config_iterator_free(git_config_iterator *iter)
1121 {
1122 1059 ▶ if (iter == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (iter == NULL) 0 1059 1059
1123 0 return;
1124  
1125 1059 iter->free(iter);
1126 }
1127  
1128 0 int git_config_find_global(git_buf *path)
1129 {
1130 0 ▶ GIT_BUF_WRAP_PRIVATE(path, git_sysdir_find_global_file, GIT_CONFIG_FILENAME_GLOBAL);
Branch: 2 branches
BranchTrueFalseTotal
GIT_BUF_WRAP_PRIVATE(path, git_sysdir_find_global_file, GIT_CONFIG_FILENAME_GLOBAL); 0 0 0
GIT_BUF_WRAP_PRIVATE(path, git_sysdir_find_global_file, GIT_CONFIG_FILENAME_GLOBAL); 0 0 0
1131 }
1132  
1133 70 int git_config__find_global(git_str *path)
1134 {
1135 70 return git_sysdir_find_global_file(path, GIT_CONFIG_FILENAME_GLOBAL);
1136 }
1137  
1138 0 int git_config_find_xdg(git_buf *path)
1139 {
1140 0 ▶ GIT_BUF_WRAP_PRIVATE(path, git_sysdir_find_xdg_file, GIT_CONFIG_FILENAME_XDG);
Branch: 2 branches
BranchTrueFalseTotal
GIT_BUF_WRAP_PRIVATE(path, git_sysdir_find_xdg_file, GIT_CONFIG_FILENAME_XDG); 0 0 0
GIT_BUF_WRAP_PRIVATE(path, git_sysdir_find_xdg_file, GIT_CONFIG_FILENAME_XDG); 0 0 0
1141 }
1142  
1143 70 int git_config__find_xdg(git_str *path)
1144 {
1145 70 return git_sysdir_find_xdg_file(path, GIT_CONFIG_FILENAME_XDG);
1146 }
1147  
1148 0 int git_config_find_system(git_buf *path)
1149 {
1150 0 ▶ GIT_BUF_WRAP_PRIVATE(path, git_sysdir_find_system_file, GIT_CONFIG_FILENAME_SYSTEM);
Branch: 2 branches
BranchTrueFalseTotal
GIT_BUF_WRAP_PRIVATE(path, git_sysdir_find_system_file, GIT_CONFIG_FILENAME_SYSTEM); 0 0 0
GIT_BUF_WRAP_PRIVATE(path, git_sysdir_find_system_file, GIT_CONFIG_FILENAME_SYSTEM); 0 0 0
1151 }
1152  
1153 70 int git_config__find_system(git_str *path)
1154 {
1155 70 return git_sysdir_find_system_file(path, GIT_CONFIG_FILENAME_SYSTEM);
1156 }
1157  
1158 0 int git_config_find_programdata(git_buf *path)
1159 {
1160 0 git_str str = GIT_STR_INIT;
1161 int error;
1162  
1163 0 if ((error = git_buf_tostr(&str, path)) == 0 &&
1164 0 (error = git_config__find_programdata(&str)) == 0)
1165 0 error = git_buf_fromstr(path, &str);
1166  
1167 0 git_str_dispose(&str);
1168 0 return error;
1169 }
1170  
1171 70 int git_config__find_programdata(git_str *path)
1172 {
1173 70 git_fs_path_owner_t owner_level =
1174 GIT_FS_PATH_OWNER_CURRENT_USER |
1175 GIT_FS_PATH_OWNER_ADMINISTRATOR;
1176 bool is_safe;
1177  
1178 70 if (git_sysdir_find_programdata_file(path, GIT_CONFIG_FILENAME_PROGRAMDATA) < 0 ||
1179 0 git_fs_path_owner_is(&is_safe, path->ptr, owner_level) < 0)
1180 70 return -1;
1181  
1182 0 ▶ if (!is_safe) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_safe) { 0 0 0
1183 0 git_error_set(GIT_ERROR_CONFIG, "programdata path has invalid ownership");
1184 0 return -1;
1185 }
1186  
1187 0 return 0;
1188 }
1189  
1190 1 int git_config__global_location(git_str *buf)
1191 {
1192 const git_str *paths;
1193 const char *sep, *start;
1194  
1195 1 ▶ if (git_sysdir_get(&paths, GIT_SYSDIR_GLOBAL) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_sysdir_get(&paths, GIT_SYSDIR_GLOBAL) < 0) 0 1 1
1196 0 return -1;
1197  
1198 /* no paths, so give up */
1199 1 ▶ if (!paths || !git_str_len(paths))
Branch: 2 branches
BranchTrueFalseTotal
if (!paths || !git_str_len(paths)) 1 0 1
if (!paths || !git_str_len(paths)) 0 1 1
1200 0 return -1;
1201  
1202 /* find unescaped separator or end of string */
1203 16 ▶ for (sep = start = git_str_cstr(paths); *sep; ++sep) {
Branch: 1 branch
BranchTrueFalseTotal
for (sep = start = git_str_cstr(paths); *sep; ++sep) { 15 1 16
1204 15 ▶ if (*sep == GIT_PATH_LIST_SEPARATOR &&
Branch: 2 branches
BranchTrueFalseTotal
if (*sep == GIT_PATH_LIST_SEPARATOR && 0 15 15
if (*sep == GIT_PATH_LIST_SEPARATOR && 0 0 0
1205 0 ▶ (sep <= start || sep[-1] != '\\'))
Branch: 1 branch
BranchTrueFalseTotal
(sep <= start || sep[-1] != '\\')) 0 0 0
1206 break;
1207 }
1208  
1209 1 ▶ if (git_str_set(buf, start, (size_t)(sep - start)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_str_set(buf, start, (size_t)(sep - start)) < 0) 0 1 1
1210 0 return -1;
1211  
1212 1 return git_str_joinpath(buf, buf->ptr, GIT_CONFIG_FILENAME_GLOBAL);
1213 }
1214  
1215 7 int git_config_open_default(git_config **out)
1216 {
1217 int error;
1218 7 git_config *cfg = NULL;
1219 7 git_str buf = GIT_STR_INIT;
1220  
1221 7 ▶ if ((error = git_config_new(&cfg)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_config_new(&cfg)) < 0) 0 7 7
1222 0 return error;
1223  
1224 8 if (!git_config__find_global(&buf) ||
1225 1 !git_config__global_location(&buf)) {
1226 7 error = git_config_add_file_ondisk(cfg, buf.ptr,
1227 GIT_CONFIG_LEVEL_GLOBAL, NULL, 0);
1228 }
1229  
1230 7 ▶ if (!error && !git_config__find_xdg(&buf))
Branch: 2 branches
BranchTrueFalseTotal
if (!error && !git_config__find_xdg(&buf)) 7 0 7
if (!error && !git_config__find_xdg(&buf)) 0 7 7
1231 0 error = git_config_add_file_ondisk(cfg, buf.ptr,
1232 GIT_CONFIG_LEVEL_XDG, NULL, 0);
1233  
1234 7 ▶ if (!error && !git_config__find_system(&buf))
Branch: 2 branches
BranchTrueFalseTotal
if (!error && !git_config__find_system(&buf)) 7 0 7
if (!error && !git_config__find_system(&buf)) 0 7 7
1235 0 error = git_config_add_file_ondisk(cfg, buf.ptr,
1236 GIT_CONFIG_LEVEL_SYSTEM, NULL, 0);
1237  
1238 7 ▶ if (!error && !git_config__find_programdata(&buf))
Branch: 2 branches
BranchTrueFalseTotal
if (!error && !git_config__find_programdata(&buf)) 7 0 7
if (!error && !git_config__find_programdata(&buf)) 0 7 7
1239 0 error = git_config_add_file_ondisk(cfg, buf.ptr,
1240 GIT_CONFIG_LEVEL_PROGRAMDATA, NULL, 0);
1241  
1242 7 git_str_dispose(&buf);
1243  
1244 7 ▶ if (error) {
Branch: 1 branch
BranchTrueFalseTotal
if (error) { 0 7 7
1245 0 git_config_free(cfg);
1246 0 cfg = NULL;
1247 }
1248  
1249 7 *out = cfg;
1250  
1251 7 return error;
1252 }
1253  
1254 0 int git_config_lock(git_transaction **out, git_config *cfg)
1255 {
1256 int error;
1257 git_config_backend *backend;
1258 backend_internal *internal;
1259  
1260 0 ▶ GIT_ASSERT_ARG(cfg);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(cfg); 0 0 0
1261  
1262 0 internal = git_vector_get(&cfg->backends, 0);
1263 0 ▶ if (!internal || !internal->backend) {
Branch: 2 branches
BranchTrueFalseTotal
if (!internal || !internal->backend) { 0 0 0
if (!internal || !internal->backend) { 0 0 0
1264 0 git_error_set(GIT_ERROR_CONFIG, "cannot lock; the config has no backends");
1265 0 return -1;
1266 }
1267 0 backend = internal->backend;
1268  
1269 0 ▶ if ((error = backend->lock(backend)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = backend->lock(backend)) < 0) 0 0 0
1270 0 return error;
1271  
1272 0 return git_transaction_config_new(out, cfg);
1273 }
1274  
1275 0 int git_config_unlock(git_config *cfg, int commit)
1276 {
1277 git_config_backend *backend;
1278 backend_internal *internal;
1279  
1280 0 ▶ GIT_ASSERT_ARG(cfg);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(cfg); 0 0 0
1281  
1282 0 internal = git_vector_get(&cfg->backends, 0);
1283 0 ▶ if (!internal || !internal->backend) {
Branch: 2 branches
BranchTrueFalseTotal
if (!internal || !internal->backend) { 0 0 0
if (!internal || !internal->backend) { 0 0 0
1284 0 git_error_set(GIT_ERROR_CONFIG, "cannot lock; the config has no backends");
1285 0 return -1;
1286 }
1287  
1288 0 backend = internal->backend;
1289  
1290 0 return backend->unlock(backend, commit);
1291 }
1292  
1293 /***********
1294 * Parsers
1295 ***********/
1296  
1297 23 int git_config_lookup_map_value(
1298 int *out,
1299 const git_configmap *maps,
1300 size_t map_n,
1301 const char *value)
1302 {
1303 size_t i;
1304  
1305 53 ▶ for (i = 0; i < map_n; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < map_n; ++i) { 53 0 53
1306 53 const git_configmap *m = maps + i;
1307  
1308 53 switch (m->type) {
1309 46 case GIT_CONFIGMAP_FALSE:
1310 case GIT_CONFIGMAP_TRUE: {
1311 int bool_val;
1312  
1313 46 ▶ if (git_config_parse_bool(&bool_val, value) == 0 &&
Branch: 1 branch
BranchTrueFalseTotal
if (git_config_parse_bool(&bool_val, value) == 0 && 32 14 46
1314 32 ▶ bool_val == (int)m->type) {
Branch: 1 branch
BranchTrueFalseTotal
bool_val == (int)m->type) { 16 16 32
1315 16 *out = m->map_value;
1316 16 return 0;
1317 }
1318 30 break;
1319 }
1320  
1321 0 case GIT_CONFIGMAP_INT32:
1322 0 ▶ if (git_config_parse_int32(out, value) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_config_parse_int32(out, value) == 0) 0 0 0
1323 0 return 0;
1324 0 break;
1325  
1326 7 case GIT_CONFIGMAP_STRING:
1327 7 ▶ if (value && strcasecmp(value, m->str_match) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (value && strcasecmp(value, m->str_match) == 0) { 7 0 7
if (value && strcasecmp(value, m->str_match) == 0) { 7 0 7
1328 7 *out = m->map_value;
1329 7 return 0;
1330 }
1331 0 break;
1332 }
1333 }
1334  
1335 0 git_error_set(GIT_ERROR_CONFIG, "failed to map '%s'", value);
1336 0 return -1;
1337 }
1338  
1339 0 int git_config_lookup_map_enum(git_configmap_t *type_out, const char **str_out,
1340 const git_configmap *maps, size_t map_n, int enum_val)
1341 {
1342 size_t i;
1343  
1344 0 ▶ for (i = 0; i < map_n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < map_n; i++) { 0 0 0
1345 0 const git_configmap *m = &maps[i];
1346  
1347 0 ▶ if (m->map_value != enum_val)
Branch: 1 branch
BranchTrueFalseTotal
if (m->map_value != enum_val) 0 0 0
1348 0 continue;
1349  
1350 0 *type_out = m->type;
1351 0 *str_out = m->str_match;
1352 0 return 0;
1353 }
1354  
1355 0 git_error_set(GIT_ERROR_CONFIG, "invalid enum value");
1356 0 return GIT_ENOTFOUND;
1357 }
1358  
1359 655 int git_config_parse_bool(int *out, const char *value)
1360 {
1361 655 ▶ if (git__parse_bool(out, value) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git__parse_bool(out, value) == 0) 641 14 655
1362 641 return 0;
1363  
1364 14 ▶ if (git_config_parse_int32(out, value) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (git_config_parse_int32(out, value) == 0) { 0 14 14
1365 0 *out = !!(*out);
1366 0 return 0;
1367 }
1368  
1369 14 git_error_set(GIT_ERROR_CONFIG, "failed to parse '%s' as a boolean value", value);
1370 14 return -1;
1371 }
1372  
1373 78 int git_config_parse_int64(int64_t *out, const char *value)
1374 {
1375 const char *num_end;
1376 int64_t num;
1377  
1378 78 ▶ if (!value || git__strntol64(&num, value, strlen(value), &num_end, 0) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (!value || git__strntol64(&num, value, strlen(value), &num_end, 0) < 0) 78 0 78
if (!value || git__strntol64(&num, value, strlen(value), &num_end, 0) < 0) 14 64 78
1379 14 goto fail_parse;
1380  
1381 64 switch (*num_end) {
1382 0 case 'g':
1383 case 'G':
1384 0 num *= 1024;
1385 /* fallthrough */
1386  
1387 0 case 'm':
1388 case 'M':
1389 0 num *= 1024;
1390 /* fallthrough */
1391  
1392 0 case 'k':
1393 case 'K':
1394 0 num *= 1024;
1395  
1396 /* check that that there are no more characters after the
1397 * given modifier suffix */
1398 0 ▶ if (num_end[1] != '\0')
Branch: 1 branch
BranchTrueFalseTotal
if (num_end[1] != '\0') 0 0 0
1399 0 return -1;
1400  
1401 /* fallthrough */
1402  
1403 case '\0':
1404 64 *out = num;
1405 64 return 0;
1406  
1407 0 default:
1408 0 goto fail_parse;
1409 }
1410  
1411 14 fail_parse:
1412 14 ▶ git_error_set(GIT_ERROR_CONFIG, "failed to parse '%s' as an integer", value ? value : "(null)");
Branch: 1 branch
BranchTrueFalseTotal
git_error_set(GIT_ERROR_CONFIG, "failed to parse '%s' as an integer", value ? value : "(null)"); 14 0 14
1413 14 return -1;
1414 }
1415  
1416 78 int git_config_parse_int32(int32_t *out, const char *value)
1417 {
1418 int64_t tmp;
1419 int32_t truncate;
1420  
1421 78 ▶ if (git_config_parse_int64(&tmp, value) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_config_parse_int64(&tmp, value) < 0) 14 64 78
1422 14 goto fail_parse;
1423  
1424 64 truncate = tmp & 0xFFFFFFFF;
1425 64 ▶ if (truncate != tmp)
Branch: 1 branch
BranchTrueFalseTotal
if (truncate != tmp) 0 64 64
1426 0 goto fail_parse;
1427  
1428 64 *out = truncate;
1429 64 return 0;
1430  
1431 14 fail_parse:
1432 14 ▶ git_error_set(GIT_ERROR_CONFIG, "failed to parse '%s' as a 32-bit integer", value ? value : "(null)");
Branch: 1 branch
BranchTrueFalseTotal
git_error_set(GIT_ERROR_CONFIG, "failed to parse '%s' as a 32-bit integer", value ? value : "(null)"); 14 0 14
1433 14 return -1;
1434 }
1435  
1436 1186 static int normalize_section(char *start, char *end)
1437 {
1438 char *scan;
1439  
1440 1186 ▶ if (start == end)
Branch: 1 branch
BranchTrueFalseTotal
if (start == end) 0 1186 1186
1441 0 return GIT_EINVALIDSPEC;
1442  
1443 /* Validate and downcase range */
1444 8769 ▶ for (scan = start; *scan; ++scan) {
Branch: 1 branch
BranchTrueFalseTotal
for (scan = start; *scan; ++scan) { 8177 592 8769
1445 8177 ▶ if (end && scan >= end)
Branch: 2 branches
BranchTrueFalseTotal
if (end && scan >= end) 3236 4941 8177
if (end && scan >= end) 594 2642 3236
1446 594 break;
1447 7583 ▶ if (isalnum(*scan))
Branch: 1 branch
BranchTrueFalseTotal
if (isalnum(*scan)) 7583 0 7583
1448 7583 *scan = (char)git__tolower(*scan);
1449 0 ▶ else if (*scan != '-' || scan == start)
Branch: 2 branches
BranchTrueFalseTotal
else if (*scan != '-' || scan == start) 0 0 0
else if (*scan != '-' || scan == start) 0 0 0
1450 0 return GIT_EINVALIDSPEC;
1451 }
1452  
1453 1186 ▶ if (scan == start)
Branch: 1 branch
BranchTrueFalseTotal
if (scan == start) 0 1186 1186
1454 0 return GIT_EINVALIDSPEC;
1455  
1456 1186 return 0;
1457 }
1458  
1459  
1460 /* Take something the user gave us and make it nice for our hash function */
1461 592 int git_config__normalize_name(const char *in, char **out)
1462 {
1463 char *name, *fdot, *ldot;
1464  
1465 592 ▶ GIT_ASSERT_ARG(in);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(in); 0 592 592
1466 592 ▶ GIT_ASSERT_ARG(out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(out); 0 592 592
1467  
1468 592 name = git__strdup(in);
1469 592 ▶ GIT_ERROR_CHECK_ALLOC(name);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(name); 0 592 592
1470  
1471 592 fdot = strchr(name, '.');
1472 592 ldot = strrchr(name, '.');
1473  
1474 592 ▶ if (fdot == NULL || fdot == name || ldot == NULL || !ldot[1])
Branch: 4 branches
BranchTrueFalseTotal
if (fdot == NULL || fdot == name || ldot == NULL || !ldot[1]) 592 0 592
if (fdot == NULL || fdot == name || ldot == NULL || !ldot[1]) 592 0 592
if (fdot == NULL || fdot == name || ldot == NULL || !ldot[1]) 592 0 592
if (fdot == NULL || fdot == name || ldot == NULL || !ldot[1]) 0 592 592
1475 0 goto invalid;
1476  
1477 /* Validate and downcase up to first dot and after last dot */
1478 1184 if (normalize_section(name, fdot) < 0 ||
1479 592 normalize_section(ldot + 1, NULL) < 0)
1480 0 goto invalid;
1481  
1482 /* If there is a middle range, make sure it doesn't have newlines */
1483 1384 ▶ while (fdot < ldot)
Branch: 1 branch
BranchTrueFalseTotal
while (fdot < ldot) 792 592 1384
1484 792 ▶ if (*fdot++ == '\n')
Branch: 1 branch
BranchTrueFalseTotal
if (*fdot++ == '\n') 0 792 792
1485 0 goto invalid;
1486  
1487 592 *out = name;
1488 592 return 0;
1489  
1490 0 invalid:
1491 0 git__free(name);
1492 0 git_error_set(GIT_ERROR_CONFIG, "invalid config item name '%s'", in);
1493 0 return GIT_EINVALIDSPEC;
1494 }
1495  
1496 struct rename_data {
1497 git_config *config;
1498 git_str *name;
1499 size_t old_len;
1500 };
1501  
1502 4 static int rename_config_entries_cb(
1503 const git_config_entry *entry,
1504 void *payload)
1505 {
1506 4 int error = 0;
1507 4 struct rename_data *data = (struct rename_data *)payload;
1508 4 size_t base_len = git_str_len(data->name);
1509  
1510 6 if (base_len > 0 &&
1511 2 !(error = git_str_puts(data->name, entry->name + data->old_len)))
1512 {
1513 2 error = git_config_set_string(
1514 2 data->config, git_str_cstr(data->name), entry->value);
1515  
1516 2 git_str_truncate(data->name, base_len);
1517 }
1518  
1519 4 ▶ if (!error)
Branch: 1 branch
BranchTrueFalseTotal
if (!error) 4 0 4
1520 4 error = git_config_delete_entry(data->config, entry->name);
1521  
1522 4 return error;
1523 }
1524  
1525 3 int git_config_rename_section(
1526 git_repository *repo,
1527 const char *old_section_name,
1528 const char *new_section_name)
1529 {
1530 git_config *config;
1531 3 git_str pattern = GIT_STR_INIT, replace = GIT_STR_INIT;
1532 3 int error = 0;
1533 struct rename_data data;
1534  
1535 3 git_str_puts_escape_regex(&pattern, old_section_name);
1536  
1537 3 ▶ if ((error = git_str_puts(&pattern, "\\..+")) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_str_puts(&pattern, "\\..+")) < 0) 0 3 3
1538 0 goto cleanup;
1539  
1540 3 ▶ if ((error = git_repository_config__weakptr(&config, repo)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_repository_config__weakptr(&config, repo)) < 0) 0 3 3
1541 0 goto cleanup;
1542  
1543 3 data.config = config;
1544 3 data.name = &replace;
1545 3 data.old_len = strlen(old_section_name) + 1;
1546  
1547 3 ▶ if ((error = git_str_join(&replace, '.', new_section_name, "")) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_str_join(&replace, '.', new_section_name, "")) < 0) 0 3 3
1548 0 goto cleanup;
1549  
1550 5 if (new_section_name != NULL &&
1551 2 (error = normalize_section(replace.ptr, strchr(replace.ptr, '.'))) < 0)
1552 {
1553 0 git_error_set(
1554 GIT_ERROR_CONFIG, "invalid config section '%s'", new_section_name);
1555 0 goto cleanup;
1556 }
1557  
1558 3 error = git_config_foreach_match(
1559 config, git_str_cstr(&pattern), rename_config_entries_cb, &data);
1560  
1561 3 cleanup:
1562 3 git_str_dispose(&pattern);
1563 3 git_str_dispose(&replace);
1564  
1565 3 return error;
1566 }
1567  
1568 0 int git_config_init_backend(git_config_backend *backend, unsigned int version)
1569 {
1570 0 ▶ GIT_INIT_STRUCTURE_FROM_TEMPLATE(
Branch: 1 branch
BranchTrueFalseTotal
GIT_INIT_STRUCTURE_FROM_TEMPLATE( 0 0 0
1571 backend, version, git_config_backend, GIT_CONFIG_BACKEND_INIT);
1572 0 return 0;
1573 }
« deps/libgit2/src/libgit2/refspec.c ↑ Summary deps/libgit2/src/libgit2/merge.c »