Skip to content

Commit 1b32908

Browse files
committed
config_file: refuse modifying included variables
Modifying variables pulled in by an included file currently succeeds, but it doesn't actually do what one would expect, as refreshing the configuration will cause the values to reappear. As we are currently not really able to support this use case, we will instead just return an error for deleting and setting variables which were included via an include.
1 parent 28c2cc3 commit 1b32908

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/config_file.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,12 @@ static int config_set(git_config_backend *cfg, const char *name, const char *val
490490
goto out;
491491
}
492492

493+
if (existing->included) {
494+
giterr_set(GITERR_CONFIG, "modifying included variable is not supported");
495+
ret = -1;
496+
goto out;
497+
}
498+
493499
/* don't update if old and new values already match */
494500
if ((!existing->entry->value && !value) ||
495501
(existing->entry->value && value &&
@@ -624,6 +630,11 @@ static int config_delete(git_config_backend *cfg, const char *name)
624630
var = git_strmap_value_at(values, pos);
625631
refcounted_strmap_free(map);
626632

633+
if (var->included) {
634+
giterr_set(GITERR_CONFIG, "cannot delete included variable");
635+
return -1;
636+
}
637+
627638
if (var->next != NULL) {
628639
giterr_set(GITERR_CONFIG, "cannot delete multivar with a single delete");
629640
return -1;

tests/config/include.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,21 @@ void test_config_include__rewriting_include_refreshes_values(void)
146146
cl_git_pass(git_config_get_string_buf(&buf, cfg, "first.other"));
147147
cl_assert_equal_s(buf.ptr, "value");
148148
}
149+
150+
void test_config_include__included_variables_cannot_be_deleted(void)
151+
{
152+
cl_git_mkfile("top-level", "[include]\npath = included\n");
153+
cl_git_mkfile("included", "[foo]\nbar = value");
154+
155+
cl_git_pass(git_config_open_ondisk(&cfg, "top-level"));
156+
cl_git_fail(git_config_delete_entry(cfg, "foo.bar"));
157+
}
158+
159+
void test_config_include__included_variables_cannot_be_modified(void)
160+
{
161+
cl_git_mkfile("top-level", "[include]\npath = included\n");
162+
cl_git_mkfile("included", "[foo]\nbar = value");
163+
164+
cl_git_pass(git_config_open_ondisk(&cfg, "top-level"));
165+
cl_git_fail(git_config_set_string(cfg, "foo.bar", "other-value"));
166+
}

0 commit comments

Comments
 (0)