config_file.c 30.3 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3
 *
Vicent Marti committed
4 5
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * 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 "array.h"
14
#include "str.h"
15
#include "config_backend.h"
16
#include "config_entries.h"
17 18
#include "config_parse.h"
#include "filebuf.h"
19
#include "regexp.h"
20
#include "sysdir.h"
21
#include "wildmatch.h"
22

23 24
/* Max depth for [include] directives */
#define MAX_INCLUDE_DEPTH 10
25

26
typedef struct config_file {
27 28 29
	git_futils_filestamp stamp;
	git_oid checksum;
	char *path;
30 31
	git_array_t(struct config_file) includes;
} config_file;
32

33
typedef struct {
34 35
	git_config_backend parent;
	git_mutex values_mutex;
36
	git_config_entries *entries;
37
	const git_repository *repo;
38
	git_config_level_t level;
39

40 41
	git_array_t(git_config_parser) readers;

42 43
	bool locked;
	git_filebuf locked_buf;
44
	git_str locked_content;
45

46 47
	config_file file;
} config_file_backend;
48

49
typedef struct {
50
	const git_repository *repo;
51
	config_file *file;
52
	git_config_entries *entries;
53 54
	git_config_level_t level;
	unsigned int depth;
55
} config_file_parse_data;
56

57 58 59
static int config_file_read(git_config_entries *entries, const git_repository *repo, config_file *file, git_config_level_t level, int depth);
static int config_file_read_buffer(git_config_entries *entries, const git_repository *repo, config_file *file, git_config_level_t level, int depth, const char *buf, size_t buflen);
static int config_file_write(config_file_backend *cfg, const char *orig_key, const char *key, const git_regexp *preg, const char *value);
60
static char *escape_value(const char *ptr);
61

62 63 64 65 66
/**
 * Take the current values map from the backend and increase its
 * refcount. This is its own function to make sure we use the mutex to
 * avoid the map pointer from changing under us.
 */
67
static int config_file_entries_take(git_config_entries **out, config_file_backend *b)
68
{
69
	int error;
70

71 72 73
	if ((error = git_mutex_lock(&b->values_mutex)) < 0) {
		git_error_set(GIT_ERROR_OS, "failed to lock config backend");
		return error;
74
	}
75

76 77
	git_config_entries_incref(b->entries);
	*out = b->entries;
78

79
	git_mutex_unlock(&b->values_mutex);
80

81
	return 0;
82 83
}

84
static void config_file_clear(config_file *file)
85
{
86
	config_file *include;
87 88 89 90 91 92 93 94 95 96 97 98 99
	uint32_t i;

	if (file == NULL)
		return;

	git_array_foreach(file->includes, i, include) {
		config_file_clear(include);
	}
	git_array_clear(file->includes);

	git__free(file->path);
}

100
static int config_file_open(git_config_backend *cfg, git_config_level_t level, const git_repository *repo)
101
{
102
	config_file_backend *b = GIT_CONTAINER_OF(cfg, config_file_backend, parent);
103
	int res;
104

105 106
	b->level = level;
	b->repo = repo;
107

108
	if ((res = git_config_entries_new(&b->entries)) < 0)
109
		return res;
110

111
	if (!git_fs_path_exists(b->file.path))
112 113
		return 0;

114 115 116 117 118 119 120 121 122
	/*
	 * git silently ignores configuration files that are not
	 * readable.  We emulate that behavior.  This is particularly
	 * important for sandboxed applications on macOS where the
	 * git configuration files may not be readable.
	 */
	if (p_access(b->file.path, R_OK) < 0)
		return GIT_ENOTFOUND;

123
	if (res < 0 || (res = config_file_read(b->entries, repo, &b->file, level, 0)) < 0) {
124 125
		git_config_entries_free(b->entries);
		b->entries = NULL;
126
	}
127

128 129 130
	return res;
}

131
static int config_file_is_modified(int *modified, config_file *file)
132
{
133
	config_file *include;
134
	git_str buf = GIT_STR_INIT;
135 136
	git_oid hash;
	uint32_t i;
137
	int error = 0;
138

139 140
	*modified = 0;

141 142 143
	if (!git_futils_filestamp_check(&file->stamp, file->path))
		goto check_includes;

144
	if ((error = git_futils_readbuffer(&buf, file->path)) < 0)
145
		goto out;
146

147
	if ((error = git_hash_buf(hash.id, buf.ptr, buf.size, GIT_HASH_ALGORITHM_SHA1)) < 0)
148
		goto out;
149

150 151 152 153 154
	if (!git_oid_equal(&hash, &file->checksum)) {
		*modified = 1;
		goto out;
	}

155
check_includes:
156
	git_array_foreach(file->includes, i, include) {
157
		if ((error = config_file_is_modified(modified, include)) < 0 || *modified)
158 159
			goto out;
	}
160 161

out:
162
	git_str_dispose(&buf);
163

164 165 166
	return error;
}

167
static void config_file_clear_includes(config_file_backend *cfg)
168 169 170 171 172 173 174 175 176
{
	config_file *include;
	uint32_t i;

	git_array_foreach(cfg->file.includes, i, include)
		config_file_clear(include);
	git_array_clear(cfg->file.includes);
}

177
static int config_file_set_entries(git_config_backend *cfg, git_config_entries *entries)
178
{
179
	config_file_backend *b = GIT_CONTAINER_OF(cfg, config_file_backend, parent);
180 181
	git_config_entries *old = NULL;
	int error;
182

183 184 185 186
	if (b->parent.readonly) {
		git_error_set(GIT_ERROR_CONFIG, "this backend is read-only");
		return -1;
	}
187

188
	if ((error = git_mutex_lock(&b->values_mutex)) < 0) {
189
		git_error_set(GIT_ERROR_OS, "failed to lock config backend");
190 191
		goto out;
	}
192

193 194
	old = b->entries;
	b->entries = entries;
195

196
	git_mutex_unlock(&b->values_mutex);
197

198
out:
199 200 201 202
	git_config_entries_free(old);
	return error;
}

203
static int config_file_refresh_from_buffer(git_config_backend *cfg, const char *buf, size_t buflen)
204
{
205
	config_file_backend *b = GIT_CONTAINER_OF(cfg, config_file_backend, parent);
206 207 208
	git_config_entries *entries = NULL;
	int error;

209 210
	config_file_clear_includes(b);

211
	if ((error = git_config_entries_new(&entries)) < 0 ||
212 213 214
	    (error = config_file_read_buffer(entries, b->repo, &b->file,
					     b->level, 0, buf, buflen)) < 0 ||
	    (error = config_file_set_entries(cfg, entries)) < 0)
215 216 217 218 219 220 221 222
		goto out;

	entries = NULL;
out:
	git_config_entries_free(entries);
	return error;
}

223
static int config_file_refresh(git_config_backend *cfg)
224
{
225
	config_file_backend *b = GIT_CONTAINER_OF(cfg, config_file_backend, parent);
226 227 228
	git_config_entries *entries = NULL;
	int error, modified;

229 230 231
	if (cfg->readonly)
		return 0;

232
	if ((error = config_file_is_modified(&modified, &b->file)) < 0 && error != GIT_ENOTFOUND)
233 234 235 236 237
		goto out;

	if (!modified)
		return 0;

238 239
	config_file_clear_includes(b);

240
	if ((error = git_config_entries_new(&entries)) < 0 ||
241 242
	    (error = config_file_read(entries, b->repo, &b->file, b->level, 0)) < 0 ||
	    (error = config_file_set_entries(cfg, entries)) < 0)
243 244 245 246
		goto out;

	entries = NULL;
out:
247
	git_config_entries_free(entries);
248

249
	return (error == GIT_ENOTFOUND) ? 0 : error;
250 251
}

252
static void config_file_free(git_config_backend *_backend)
253
{
254
	config_file_backend *backend = GIT_CONTAINER_OF(_backend, config_file_backend, parent);
255 256 257 258

	if (backend == NULL)
		return;

259
	config_file_clear(&backend->file);
260 261
	git_config_entries_free(backend->entries);
	git_mutex_free(&backend->values_mutex);
262
	git__free(backend);
263 264
}

265
static int config_file_iterator(
266
	git_config_iterator **iter,
267
	struct git_config_backend *backend)
268
{
269
	config_file_backend *b = GIT_CONTAINER_OF(backend, config_file_backend, parent);
270
	git_config_entries *dupped = NULL, *entries = NULL;
271
	int error;
272

273
	if ((error = config_file_refresh(backend)) < 0 ||
274 275 276
	    (error = config_file_entries_take(&entries, b)) < 0 ||
	    (error = git_config_entries_dup(&dupped, entries)) < 0 ||
	    (error = git_config_entries_iterator_new(iter, dupped)) < 0)
277
		goto out;
278

279 280 281
out:
	/* Let iterator delete duplicated entries when it's done */
	git_config_entries_free(entries);
282
	git_config_entries_free(dupped);
283
	return error;
284 285
}

286 287 288 289 290 291
static int config_file_snapshot(git_config_backend **out, git_config_backend *backend)
{
	return git_config_backend_snapshot(out, backend);
}

static int config_file_set(git_config_backend *cfg, const char *name, const char *value)
292
{
293
	config_file_backend *b = GIT_CONTAINER_OF(cfg, config_file_backend, parent);
294
	git_config_entries *entries;
295
	git_config_entry *existing;
296
	char *key, *esc_value = NULL;
297
	int error;
298

299 300
	if ((error = git_config__normalize_name(name, &key)) < 0)
		return error;
301

302 303
	if ((error = config_file_entries_take(&entries, b)) < 0)
		return error;
304

305 306 307
	/* Check whether we'd be modifying an included or multivar key */
	if ((error = git_config_entries_get_unique(&existing, entries, key)) < 0) {
		if (error != GIT_ENOTFOUND)
308
			goto out;
309 310 311
		error = 0;
	} else if ((!existing->value && !value) ||
		   (existing->value && value && !strcmp(existing->value, value))) {
Russell Belfer committed
312
		/* don't update if old and new values already match */
313 314
		error = 0;
		goto out;
315 316
	}

317
	/* No early returns due to sanity checks, let's write it out and refresh */
318
	if (value) {
319
		esc_value = escape_value(value);
320
		GIT_ERROR_CHECK_ALLOC(esc_value);
321 322
	}

323
	if ((error = config_file_write(b, name, key, NULL, esc_value)) < 0)
324
		goto out;
325

326
out:
327
	git_config_entries_free(entries);
328 329
	git__free(esc_value);
	git__free(key);
330
	return error;
331 332
}

333
/* release the map containing the entry as an equivalent to freeing it */
334
static void config_file_entry_free(git_config_entry *entry)
335
{
336 337
	git_config_entries *entries = (git_config_entries *) entry->payload;
	git_config_entries_free(entries);
338 339
}

340 341 342
/*
 * Internal function that actually gets the value in string form
 */
343
static int config_file_get(git_config_backend *cfg, const char *key, git_config_entry **out)
344
{
345
	config_file_backend *h = GIT_CONTAINER_OF(cfg, config_file_backend, parent);
346 347
	git_config_entries *entries = NULL;
	git_config_entry *entry;
348
	int error = 0;
349

350
	if (!h->parent.readonly && ((error = config_file_refresh(cfg)) < 0))
351 352
		return error;

353 354
	if ((error = config_file_entries_take(&entries, h)) < 0)
		return error;
355

356
	if ((error = (git_config_entries_get(&entry, entries, key))) < 0) {
357
		git_config_entries_free(entries);
358
		return error;
359
	}
360

361
	entry->free = config_file_entry_free;
362 363
	entry->payload = entries;
	*out = entry;
364

365
	return 0;
366 367
}

368
static int config_file_set_multivar(
Ben Straub committed
369
	git_config_backend *cfg, const char *name, const char *regexp, const char *value)
370
{
371
	config_file_backend *b = GIT_CONTAINER_OF(cfg, config_file_backend, parent);
372
	git_regexp preg;
373
	int result;
374
	char *key;
375

376
	GIT_ASSERT_ARG(regexp);
377

378
	if ((result = git_config__normalize_name(name, &key)) < 0)
379
		return result;
380

381
	if ((result = git_regexp_compile(&preg, regexp, 0)) < 0)
382
		goto out;
383

384 385
	/* If we do have it, set call config_file_write() and reload */
	if ((result = config_file_write(b, name, key, &preg, value)) < 0)
386
		goto out;
387

388
out:
389
	git__free(key);
390
	git_regexp_dispose(&preg);
391 392

	return result;
393 394
}

395
static int config_file_delete(git_config_backend *cfg, const char *name)
396
{
397
	config_file_backend *b = GIT_CONTAINER_OF(cfg, config_file_backend, parent);
398 399 400 401
	git_config_entries *entries = NULL;
	git_config_entry *entry;
	char *key = NULL;
	int error;
402

403 404
	if ((error = git_config__normalize_name(name, &key)) < 0)
		goto out;
405

406
	if ((error = config_file_entries_take(&entries, b)) < 0)
407
		goto out;
408

409 410 411
	/* Check whether we'd be modifying an included or multivar key */
	if ((error = git_config_entries_get_unique(&entry, entries, key)) < 0) {
		if (error == GIT_ENOTFOUND)
412
			git_error_set(GIT_ERROR_CONFIG, "could not find key '%s' to delete", name);
413
		goto out;
414 415
	}

416
	if ((error = config_file_write(b, name, entry->name, NULL, NULL)) < 0)
417
		goto out;
418

419 420 421 422
out:
	git_config_entries_free(entries);
	git__free(key);
	return error;
423 424
}

425
static int config_file_delete_multivar(git_config_backend *cfg, const char *name, const char *regexp)
426
{
427
	config_file_backend *b = GIT_CONTAINER_OF(cfg, config_file_backend, parent);
428 429
	git_config_entries *entries = NULL;
	git_config_entry *entry = NULL;
430
	git_regexp preg = GIT_REGEX_INIT;
431
	char *key = NULL;
432 433 434
	int result;

	if ((result = git_config__normalize_name(name, &key)) < 0)
435
		goto out;
436

437
	if ((result = config_file_entries_take(&entries, b)) < 0)
438
		goto out;
439

440 441
	if ((result = git_config_entries_get(&entry, entries, key)) < 0) {
		if (result == GIT_ENOTFOUND)
442
			git_error_set(GIT_ERROR_CONFIG, "could not find key '%s' to delete", name);
443 444
		goto out;
	}
445

446
	if ((result = git_regexp_compile(&preg, regexp, 0)) < 0)
447
		goto out;
448

449
	if ((result = config_file_write(b, name, key, &preg, NULL)) < 0)
450
		goto out;
451

452
out:
453
	git_config_entries_free(entries);
454
	git__free(key);
455
	git_regexp_dispose(&preg);
456 457 458
	return result;
}

459
static int config_file_lock(git_config_backend *_cfg)
460
{
461
	config_file_backend *cfg = GIT_CONTAINER_OF(_cfg, config_file_backend, parent);
462 463
	int error;

464
	if ((error = git_filebuf_open(&cfg->locked_buf, cfg->file.path, 0, GIT_CONFIG_FILE_MODE)) < 0)
465 466
		return error;

467
	error = git_futils_readbuffer(&cfg->locked_content, cfg->file.path);
468 469 470 471 472 473 474 475 476 477
	if (error < 0 && error != GIT_ENOTFOUND) {
		git_filebuf_cleanup(&cfg->locked_buf);
		return error;
	}

	cfg->locked = true;
	return 0;

}

478
static int config_file_unlock(git_config_backend *_cfg, int success)
479
{
480
	config_file_backend *cfg = GIT_CONTAINER_OF(_cfg, config_file_backend, parent);
481 482 483 484 485 486 487 488
	int error = 0;

	if (success) {
		git_filebuf_write(&cfg->locked_buf, cfg->locked_content.ptr, cfg->locked_content.size);
		error = git_filebuf_commit(&cfg->locked_buf);
	}

	git_filebuf_cleanup(&cfg->locked_buf);
489
	git_str_dispose(&cfg->locked_content);
490 491 492 493 494
	cfg->locked = false;

	return error;
}

495
int git_config_backend_from_file(git_config_backend **out, const char *path)
496
{
497
	config_file_backend *backend;
498

499
	backend = git__calloc(1, sizeof(config_file_backend));
500
	GIT_ERROR_CHECK_ALLOC(backend);
501

502 503
	backend->parent.version = GIT_CONFIG_BACKEND_VERSION;
	git_mutex_init(&backend->values_mutex);
504

505
	backend->file.path = git__strdup(path);
506
	GIT_ERROR_CHECK_ALLOC(backend->file.path);
507
	git_array_init(backend->file.includes);
508

509 510 511 512 513 514 515 516 517 518 519
	backend->parent.open = config_file_open;
	backend->parent.get = config_file_get;
	backend->parent.set = config_file_set;
	backend->parent.set_multivar = config_file_set_multivar;
	backend->parent.del = config_file_delete;
	backend->parent.del_multivar = config_file_delete_multivar;
	backend->parent.iterator = config_file_iterator;
	backend->parent.snapshot = config_file_snapshot;
	backend->parent.lock = config_file_lock;
	backend->parent.unlock = config_file_unlock;
	backend->parent.free = config_file_free;
520 521 522 523 524 525

	*out = (git_config_backend *)backend;

	return 0;
}

526
static int included_path(git_str *out, const char *dir, const char *path)
527 528 529
{
	/* From the user's home */
	if (path[0] == '~' && path[1] == '/')
530
		return git_sysdir_expand_global_file(out, &path[1]);
531

532
	return git_fs_path_join_unrooted(out, path, dir, NULL);
533 534
}

535 536 537
/* Escape the values to write them to the file */
static char *escape_value(const char *ptr)
{
538
	git_str buf;
539 540 541
	size_t len;
	const char *esc;

542
	GIT_ASSERT_ARG_WITH_RETVAL(ptr, NULL);
543 544

	len = strlen(ptr);
545 546 547
	if (!len)
		return git__calloc(1, sizeof(char));

548
	if (git_str_init(&buf, len) < 0)
549
		return NULL;
550 551

	while (*ptr != '\0') {
552
		if ((esc = strchr(git_config_escaped, *ptr)) != NULL) {
553 554
			git_str_putc(&buf, '\\');
			git_str_putc(&buf, git_config_escapes[esc - git_config_escaped]);
555
		} else {
556
			git_str_putc(&buf, *ptr);
557 558 559 560
		}
		ptr++;
	}

561
	if (git_str_oom(&buf))
562 563
		return NULL;

564
	return git_str_detach(&buf);
565 566
}

567
static int parse_include(config_file_parse_data *parse_data, const char *file)
568
{
569
	config_file *include;
570
	git_str path = GIT_STR_INIT;
571 572 573
	char *dir;
	int result;

574 575 576
	if (!file)
		return 0;

577
	if ((result = git_fs_path_dirname_r(&path, parse_data->file->path)) < 0)
578 579
		return result;

580
	dir = git_str_detach(&path);
581 582 583 584 585 586
	result = included_path(&path, dir, file);
	git__free(dir);

	if (result < 0)
		return result;

587
	include = git_array_alloc(parse_data->file->includes);
588
	GIT_ERROR_CHECK_ALLOC(include);
589 590
	memset(include, 0, sizeof(*include));
	git_array_init(include->includes);
591
	include->path = git_str_detach(&path);
592

593 594
	result = config_file_read(parse_data->entries, parse_data->repo, include,
				  parse_data->level, parse_data->depth+1);
595 596

	if (result == GIT_ENOTFOUND) {
597
		git_error_clear();
598 599 600 601 602 603
		result = 0;
	}

	return result;
}

604
static int do_match_gitdir(
605 606 607
	int *matches,
	const git_repository *repo,
	const char *cfg_file,
608
	const char *condition,
609
	bool case_insensitive)
610
{
611
	git_str pattern = GIT_STR_INIT, gitdir = GIT_STR_INIT;
612
	int error;
613

614 615
	if (condition[0] == '.' && git_fs_path_is_dirsep(condition[1])) {
		git_fs_path_dirname_r(&pattern, cfg_file);
616
		git_str_joinpath(&pattern, pattern.ptr, condition + 2);
617
	} else if (condition[0] == '~' && git_fs_path_is_dirsep(condition[1]))
618
		git_sysdir_expand_global_file(&pattern, condition + 1);
619
	else if (!git_fs_path_is_absolute(condition))
620
		git_str_joinpath(&pattern, "**", condition);
621
	else
622
		git_str_sets(&pattern, condition);
623

624
	if (git_fs_path_is_dirsep(condition[strlen(condition) - 1]))
625
		git_str_puts(&pattern, "**");
626

627
	if (git_str_oom(&pattern)) {
628 629 630 631
		error = -1;
		goto out;
	}

632
	if ((error = git_repository__item_path(&gitdir, repo, GIT_REPOSITORY_ITEM_GITDIR)) < 0)
633 634
		goto out;

635
	if (git_fs_path_is_dirsep(gitdir.ptr[gitdir.size - 1]))
636
		git_str_truncate(&gitdir, gitdir.size - 1);
637

638 639
	*matches = wildmatch(pattern.ptr, gitdir.ptr,
			     WM_PATHNAME | (case_insensitive ? WM_CASEFOLD : 0)) == WM_MATCH;
640
out:
641 642
	git_str_dispose(&pattern);
	git_str_dispose(&gitdir);
643 644 645
	return error;
}

646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
static int conditional_match_gitdir(
	int *matches,
	const git_repository *repo,
	const char *cfg_file,
	const char *value)
{
	return do_match_gitdir(matches, repo, cfg_file, value, false);
}

static int conditional_match_gitdir_i(
	int *matches,
	const git_repository *repo,
	const char *cfg_file,
	const char *value)
{
	return do_match_gitdir(matches, repo, cfg_file, value, true);
}

664 665 666 667 668 669
static int conditional_match_onbranch(
	int *matches,
	const git_repository *repo,
	const char *cfg_file,
	const char *condition)
{
670
	git_str reference = GIT_STR_INIT, buf = GIT_STR_INIT;
671 672 673 674 675 676 677 678 679 680 681 682
	int error;

	GIT_UNUSED(cfg_file);

	/*
	 * NOTE: you cannot use `git_repository_head` here. Looking up the
	 * HEAD reference will create the ODB, which causes us to read the
	 * repo's config for keys like core.precomposeUnicode. As we're
	 * just parsing the config right now, though, this would result in
	 * an endless recursion.
	 */

683
	if ((error = git_str_joinpath(&buf, git_repository_path(repo), GIT_HEAD_FILE)) < 0 ||
684 685
	    (error = git_futils_readbuffer(&reference, buf.ptr)) < 0)
		goto out;
686
	git_str_rtrim(&reference);
687 688 689

	if (git__strncmp(reference.ptr, GIT_SYMREF, strlen(GIT_SYMREF)))
		goto out;
690
	git_str_consume(&reference, reference.ptr + strlen(GIT_SYMREF));
691 692 693

	if (git__strncmp(reference.ptr, GIT_REFS_HEADS_DIR, strlen(GIT_REFS_HEADS_DIR)))
		goto out;
694
	git_str_consume(&reference, reference.ptr + strlen(GIT_REFS_HEADS_DIR));
695 696 697 698 699

	/*
	 * If the condition ends with a '/', then we should treat it as if
	 * it had '**' appended.
	 */
700
	if ((error = git_str_sets(&buf, condition)) < 0)
701
		goto out;
702
	if (git_fs_path_is_dirsep(condition[strlen(condition) - 1]) &&
703
	    (error = git_str_puts(&buf, "**")) < 0)
704 705 706 707
		goto out;

	*matches = wildmatch(buf.ptr, reference.ptr, WM_PATHNAME) == WM_MATCH;
out:
708 709
	git_str_dispose(&reference);
	git_str_dispose(&buf);
710 711 712 713 714

	return error;

}

715 716 717 718
static const struct {
	const char *prefix;
	int (*matches)(int *matches, const git_repository *repo, const char *cfg, const char *value);
} conditions[] = {
719
	{ "gitdir:", conditional_match_gitdir },
720 721
	{ "gitdir/i:", conditional_match_gitdir_i },
	{ "onbranch:", conditional_match_onbranch }
722 723
};

724
static int parse_conditional_include(config_file_parse_data *parse_data, const char *section, const char *file)
725 726
{
	char *condition;
727
	size_t section_len, i;
728 729
	int error = 0, matches;

730
	if (!parse_data->repo || !file)
731 732
		return 0;

733 734 735 736 737 738 739 740 741 742
	section_len = strlen(section);

	/*
	 * We checked that the string starts with `includeIf.` and ends
	 * in `.path` to get here.  Make sure it consists of more.
	 */
	if (section_len < CONST_STRLEN("includeIf.") + CONST_STRLEN(".path"))
		return 0;

	condition = git__substrdup(section + CONST_STRLEN("includeIf."),
743 744 745
		section_len - CONST_STRLEN("includeIf.") - CONST_STRLEN(".path"));

	GIT_ERROR_CHECK_ALLOC(condition);
746 747 748 749 750 751 752

	for (i = 0; i < ARRAY_SIZE(conditions); i++) {
		if (git__prefixcmp(condition, conditions[i].prefix))
			continue;

		if ((error = conditions[i].matches(&matches,
						   parse_data->repo,
753
						   parse_data->file->path,
754 755 756 757
						   condition + strlen(conditions[i].prefix))) < 0)
			break;

		if (matches)
758
			error = parse_include(parse_data, file);
759 760 761 762 763 764 765 766

		break;
	}

	git__free(condition);
	return error;
}

767
static int read_on_variable(
768
	git_config_parser *reader,
769
	const char *current_section,
770 771
	const char *var_name,
	const char *var_value,
772 773 774
	const char *line,
	size_t line_len,
	void *data)
775
{
776
	config_file_parse_data *parse_data = (config_file_parse_data *)data;
777
	git_str buf = GIT_STR_INIT;
778
	git_config_entry *entry;
779
	const char *c;
780 781
	int result = 0;

782
	GIT_UNUSED(reader);
783 784 785
	GIT_UNUSED(line);
	GIT_UNUSED(line_len);

786
	if (current_section) {
Nelson Elhage committed
787 788 789 790
		/* TODO: Once warnings lang, we should likely warn
		 * here. Git appears to warn in most cases if it sees
		 * un-namespaced config options.
		 */
791 792
		git_str_puts(&buf, current_section);
		git_str_putc(&buf, '.');
793
	}
794

795
	for (c = var_name; *c; c++)
796
		git_str_putc(&buf, git__tolower(*c));
797

798
	if (git_str_oom(&buf))
799 800
		return -1;

801
	entry = git__calloc(1, sizeof(git_config_entry));
802
	GIT_ERROR_CHECK_ALLOC(entry);
803
	entry->name = git_str_detach(&buf);
804
	entry->value = var_value ? git__strdup(var_value) : NULL;
805
	entry->level = parse_data->level;
806
	entry->include_depth = parse_data->depth;
807

808
	if ((result = git_config_entries_append(parse_data->entries, entry)) < 0)
809 810 811 812 813
		return result;

	result = 0;

	/* Add or append the new config option */
814
	if (!git__strcmp(entry->name, "include.path"))
815
		result = parse_include(parse_data, entry->value);
816 817
	else if (!git__prefixcmp(entry->name, "includeif.") &&
	         !git__suffixcmp(entry->name, ".path"))
818
		result = parse_conditional_include(parse_data, entry->name, entry->value);
819

820 821 822
	return result;
}

823
static int config_file_read_buffer(
824
	git_config_entries *entries,
825
	const git_repository *repo,
826
	config_file *file,
827
	git_config_level_t level,
828 829 830
	int depth,
	const char *buf,
	size_t buflen)
831
{
832
	config_file_parse_data parse_data;
833
	git_config_parser reader;
834
	int error;
835 836

	if (depth >= MAX_INCLUDE_DEPTH) {
837
		git_error_set(GIT_ERROR_CONFIG, "maximum config include depth reached");
838 839 840 841
		return -1;
	}

	/* Initialize the reading position */
842
	reader.path = file->path;
843
	git_parse_ctx_init(&reader.ctx, buf, buflen);
844 845

	/* If the file is empty, there's nothing for us to do */
846 847
	if (!reader.ctx.content || *reader.ctx.content == '\0') {
		error = 0;
848
		goto out;
849
	}
850

851
	parse_data.repo = repo;
852
	parse_data.file = file;
853
	parse_data.entries = entries;
854 855 856
	parse_data.level = level;
	parse_data.depth = depth;

857
	error = git_config_parse(&reader, NULL, read_on_variable, NULL, NULL, &parse_data);
858 859

out:
860 861 862
	return error;
}

863
static int config_file_read(
864 865
	git_config_entries *entries,
	const git_repository *repo,
866
	config_file *file,
867 868 869
	git_config_level_t level,
	int depth)
{
870
	git_str contents = GIT_STR_INIT;
871 872 873 874
	struct stat st;
	int error;

	if (p_stat(file->path, &st) < 0) {
875
		error = git_fs_path_set_error(errno, file->path, "stat");
876 877 878 879 880 881 882
		goto out;
	}

	if ((error = git_futils_readbuffer(&contents, file->path)) < 0)
		goto out;

	git_futils_filestamp_set_from_stat(&file->stamp, &st);
883
	if ((error = git_hash_buf(file->checksum.id, contents.ptr, contents.size, GIT_HASH_ALGORITHM_SHA1)) < 0)
884 885
		goto out;

886 887
	if ((error = config_file_read_buffer(entries, repo, file, level, depth,
					     contents.ptr, contents.size)) < 0)
888 889 890
		goto out;

out:
891
	git_str_dispose(&contents);
892
	return error;
893 894
}

895
static int write_section(git_str *fbuf, const char *key)
896 897 898
{
	int result;
	const char *dot;
899
	git_str buf = GIT_STR_INIT;
900 901 902

	/* All of this just for [section "subsection"] */
	dot = strchr(key, '.');
903
	git_str_putc(&buf, '[');
904
	if (dot == NULL) {
905
		git_str_puts(&buf, key);
906 907
	} else {
		char *escaped;
908
		git_str_put(&buf, key, dot - key);
909
		escaped = escape_value(dot + 1);
910
		GIT_ERROR_CHECK_ALLOC(escaped);
911
		git_str_printf(&buf, " \"%s\"", escaped);
912 913
		git__free(escaped);
	}
914
	git_str_puts(&buf, "]\n");
915

916
	if (git_str_oom(&buf))
917 918
		return -1;

919 920
	result = git_str_put(fbuf, git_str_cstr(&buf), buf.size);
	git_str_dispose(&buf);
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943

	return result;
}

static const char *quotes_for_value(const char *value)
{
	const char *ptr;

	if (value[0] == ' ' || value[0] == '\0')
		return "\"";

	for (ptr = value; *ptr; ++ptr) {
		if (*ptr == ';' || *ptr == '#')
			return "\"";
	}

	if (ptr[-1] == ' ')
		return "\"";

	return "";
}

struct write_data {
944 945
	git_str *buf;
	git_str buffered_comment;
946 947
	unsigned int in_section : 1,
		preg_replaced : 1;
948
	const char *orig_section;
949
	const char *section;
950
	const char *orig_name;
951
	const char *name;
952
	const git_regexp *preg;
953 954 955
	const char *value;
};

956
static int write_line_to(git_str *buf, const char *line, size_t line_len)
957
{
958
	int result = git_str_put(buf, line, line_len);
959 960

	if (!result && line_len && line[line_len-1] != '\n')
961
		result = git_str_printf(buf, "\n");
962 963 964 965

	return result;
}

966 967 968 969 970
static int write_line(struct write_data *write_data, const char *line, size_t line_len)
{
	return write_line_to(write_data->buf, line, line_len);
}

971 972 973 974 975 976
static int write_value(struct write_data *write_data)
{
	const char *q;
	int result;

	q = quotes_for_value(write_data->value);
977
	result = git_str_printf(write_data->buf,
978
		"\t%s = %s%s%s\n", write_data->orig_name, q, write_data->value, q);
979 980 981 982 983 984 985 986 987 988 989

	/* If we are updating a single name/value, we're done.  Setting `value`
	 * to `NULL` will prevent us from trying to write it again later (in
	 * `write_on_section`) if we see the same section repeated.
	 */
	if (!write_data->preg)
		write_data->value = NULL;

	return result;
}

990
static int write_on_section(
991
	git_config_parser *reader,
992 993 994 995
	const char *current_section,
	const char *line,
	size_t line_len,
	void *data)
996 997 998 999
{
	struct write_data *write_data = (struct write_data *)data;
	int result = 0;

1000 1001
	GIT_UNUSED(reader);

1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
	/* If we were previously in the correct section (but aren't anymore)
	 * and haven't written our value (for a simple name/value set, not
	 * a multivar), then append it to the end of the section before writing
	 * the new one.
	 */
	if (write_data->in_section && !write_data->preg && write_data->value)
		result = write_value(write_data);

	write_data->in_section = strcmp(current_section, write_data->section) == 0;

1012 1013 1014 1015
	/*
	 * If there were comments just before this section, dump them as well.
	 */
	if (!result) {
1016 1017
		result = git_str_put(write_data->buf, write_data->buffered_comment.ptr, write_data->buffered_comment.size);
		git_str_clear(&write_data->buffered_comment);
1018 1019
	}

1020
	if (!result)
1021
		result = write_line(write_data, line, line_len);
1022 1023 1024 1025

	return result;
}

1026
static int write_on_variable(
1027
	git_config_parser *reader,
1028
	const char *current_section,
1029 1030
	const char *var_name,
	const char *var_value,
1031 1032 1033
	const char *line,
	size_t line_len,
	void *data)
1034 1035 1036
{
	struct write_data *write_data = (struct write_data *)data;
	bool has_matched = false;
1037
	int error;
1038 1039 1040

	GIT_UNUSED(reader);
	GIT_UNUSED(current_section);
1041

1042 1043 1044
	/*
	 * If there were comments just before this variable, let's dump them as well.
	 */
1045
	if ((error = git_str_put(write_data->buf, write_data->buffered_comment.ptr, write_data->buffered_comment.size)) < 0)
1046 1047
		return error;

1048
	git_str_clear(&write_data->buffered_comment);
1049

1050 1051 1052 1053 1054 1055 1056
	/* See if we are to update this name/value pair; first examine name */
	if (write_data->in_section &&
		strcasecmp(write_data->name, var_name) == 0)
		has_matched = true;

	/* If we have a regex to match the value, see if it matches */
	if (has_matched && write_data->preg != NULL)
1057
		has_matched = (git_regexp_match(write_data->preg, var_value) == 0);
1058 1059 1060 1061

	/* If this isn't the name/value we're looking for, simply dump the
	 * existing data back out and continue on.
	 */
1062 1063
	if (!has_matched)
		return write_line(write_data, line, line_len);
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073

	write_data->preg_replaced = 1;

	/* If value is NULL, we are deleting this value; write nothing. */
	if (!write_data->value)
		return 0;

	return write_value(write_data);
}

1074
static int write_on_comment(git_config_parser *reader, const char *line, size_t line_len, void *data)
1075
{
1076 1077 1078 1079 1080
	struct write_data *write_data;

	GIT_UNUSED(reader);

	write_data = (struct write_data *)data;
1081
	return write_line_to(&write_data->buffered_comment, line, line_len);
1082 1083
}

1084
static int write_on_eof(
1085
	git_config_parser *reader, const char *current_section, void *data)
1086 1087 1088 1089
{
	struct write_data *write_data = (struct write_data *)data;
	int result = 0;

1090 1091
	GIT_UNUSED(reader);

1092 1093 1094
	/*
	 * If we've buffered comments when reaching EOF, make sure to dump them.
	 */
1095
	if ((result = git_str_put(write_data->buf, write_data->buffered_comment.ptr, write_data->buffered_comment.size)) < 0)
1096 1097
		return result;

1098 1099 1100 1101 1102 1103
	/* If we are at the EOF and have not written our value (again, for a
	 * simple name/value set, not a multivar) then we have never seen the
	 * section in question and should create a new section and write the
	 * value.
	 */
	if ((!write_data->preg || !write_data->preg_replaced) && write_data->value) {
1104 1105
		/* write the section header unless we're already in it */
		if (!current_section || strcmp(current_section, write_data->section))
1106
			result = write_section(write_data->buf, write_data->orig_section);
1107 1108

		if (!result)
1109 1110 1111 1112 1113 1114 1115 1116 1117
			result = write_value(write_data);
	}

	return result;
}

/*
 * This is pretty much the parsing, except we write out anything we don't have
 */
1118
static int config_file_write(config_file_backend *cfg, const char *orig_key, const char *key, const git_regexp *preg, const char *value)
1119

1120
{
1121
	char *orig_section = NULL, *section = NULL, *orig_name, *name, *ldot;
1122
	git_str buf = GIT_STR_INIT, contents = GIT_STR_INIT;
1123
	git_config_parser parser = GIT_CONFIG_PARSER_INIT;
1124
	git_filebuf file = GIT_FILEBUF_INIT;
1125
	struct write_data write_data;
1126
	int error;
1127

1128
	memset(&write_data, 0, sizeof(write_data));
1129

1130
	if (cfg->locked) {
1131
		error = git_str_puts(&contents, git_str_cstr(&cfg->locked_content) == NULL ? "" : git_str_cstr(&cfg->locked_content));
1132
	} else {
1133 1134 1135
		if ((error = git_filebuf_open(&file, cfg->file.path, GIT_FILEBUF_HASH_CONTENTS,
					      GIT_CONFIG_FILE_MODE)) < 0)
			goto done;
1136

1137
		/* We need to read in our own config file */
1138
		error = git_futils_readbuffer(&contents, cfg->file.path);
1139
	}
1140 1141
	if (error < 0 && error != GIT_ENOTFOUND)
		goto done;
1142

1143 1144
	if ((git_config_parser_init(&parser, cfg->file.path, contents.ptr, contents.size)) < 0)
		goto done;
1145 1146 1147 1148

	ldot = strrchr(key, '.');
	name = ldot + 1;
	section = git__strndup(key, ldot - key);
1149
	GIT_ERROR_CHECK_ALLOC(section);
1150

1151 1152 1153
	ldot = strrchr(orig_key, '.');
	orig_name = ldot + 1;
	orig_section = git__strndup(orig_key, ldot - orig_key);
1154
	GIT_ERROR_CHECK_ALLOC(orig_section);
1155

1156
	write_data.buf = &buf;
1157
	write_data.orig_section = orig_section;
1158
	write_data.section = section;
1159
	write_data.orig_name = orig_name;
1160 1161 1162 1163
	write_data.name = name;
	write_data.preg = preg;
	write_data.value = value;

1164
	if ((error = git_config_parse(&parser, write_on_section, write_on_variable,
1165
				      write_on_comment, write_on_eof, &write_data)) < 0)
1166 1167
		goto done;

1168 1169 1170
	if (cfg->locked) {
		size_t len = buf.asize;
		/* Update our copy with the modified contents */
1171 1172
		git_str_dispose(&cfg->locked_content);
		git_str_attach(&cfg->locked_content, git_str_detach(&buf), len);
1173
	} else {
1174
		git_filebuf_write(&file, git_str_cstr(&buf), git_str_len(&buf));
1175

1176
		if ((error = git_filebuf_commit(&file)) < 0)
1177 1178
			goto done;

1179
		if ((error = config_file_refresh_from_buffer(&cfg->parent, buf.ptr, buf.size)) < 0)
1180
			goto done;
1181
	}
1182 1183

done:
1184 1185
	git__free(section);
	git__free(orig_section);
1186 1187 1188
	git_str_dispose(&write_data.buffered_comment);
	git_str_dispose(&buf);
	git_str_dispose(&contents);
1189
	git_filebuf_cleanup(&file);
1190
	git_config_parser_dispose(&parser);
1191 1192

	return error;
1193
}