* [PATCH] config: skip continued lines when matching section
@ 2026-11-19 19:23 Dominik Maier
2026-11-20 0:44 ` Junio C Hamano
0 siblings, 1 reply; 3+ messages in thread
From: Dominik Maier @ 2026-11-19 19:23 UTC (permalink / raw)
To: git
Cc: Junio C Hamano, Patrick Steinhardt, Jeff King, Taylor Blau,
Johannes Schindelin, dmnk+artist, Dominik Maier
repo_config_copy_or_rename_section_in_file() scans the config file line
by line and treats any line whose first non-whitespace character is '['
as the start of a new section, without tracking backslash-continued
values across lines.
When a value in the target section spans multiple lines via a trailing
backslash and a continuation line begins with '[other]', removing or
renaming the section stops early at '[other]' and leaves the remainder
of the continued value behind as an unquoted '[other]' section.
Track backslash value continuations (including quoted subsections and
quote state across continued lines so '#' or ';' inside quotes are not
mistaken for comments) and only treat '[' as a section header at the
start of a logical line.
Signed-off-by: Dominik Maier <dmnk@google.com>
---
config.c | 101 ++++++++++++++++++++++++++++++++++++++++++++--
t/t1300-config.sh | 33 +++++++++++++++
2 files changed, 131 insertions(+), 3 deletions(-)
diff --git a/config.c b/config.c
index 215dacb6b3..f5fcbd9b84 100644
--- a/config.c
+++ b/config.c
@@ -3427,6 +3427,97 @@ static int section_name_is_ok(const char *name)
#define GIT_CONFIG_MAX_LINE_LEN (512 * 1024)
+static const char *skip_section_header(const char *line)
+{
+ const char *p = line;
+
+ if (*p != '[')
+ return line;
+ for (p++; *p && *p != '\r' && *p != '\n'; p++) {
+ if (*p == ']')
+ return p + 1;
+ if (*p == ' ' || *p == '\t') {
+ while (*p == ' ' || *p == '\t')
+ p++;
+ if (*p != '"')
+ return NULL;
+ for (p++; *p && *p != '\r' && *p != '\n'; p++) {
+ if (*p == '\\') {
+ p++;
+ if (!*p || *p == '\r' || *p == '\n')
+ return NULL;
+ continue;
+ }
+ if (*p == '"') {
+ for (p++; *p == ' ' || *p == '\t'; p++)
+ ; /* skip trailing space before ']' */
+ return (*p == ']') ? p + 1 : NULL;
+ }
+ }
+ return NULL;
+ }
+ }
+ return NULL;
+}
+
+static void update_line_continuation(const char *p, int *continued,
+ int *in_quote)
+{
+ int in_value = *continued;
+
+ if (!in_value) {
+ while (isspace(*p))
+ p++;
+ if (*p == '[') {
+ p = skip_section_header(p);
+ if (!p) {
+ *continued = 0;
+ *in_quote = 0;
+ return;
+ }
+ }
+ for (; *p && *p != '\r' && *p != '\n'; p++) {
+ if (*p == '#' || *p == ';') {
+ *continued = 0;
+ *in_quote = 0;
+ return;
+ }
+ if (*p == '=') {
+ p++;
+ in_value = 1;
+ break;
+ }
+ }
+ if (!in_value) {
+ *continued = 0;
+ *in_quote = 0;
+ return;
+ }
+ }
+
+ for (; *p && *p != '\r' && *p != '\n'; p++) {
+ if (!*in_quote && (*p == '#' || *p == ';')) {
+ *continued = 0;
+ return;
+ }
+ if (*p == '"') {
+ *in_quote = !*in_quote;
+ continue;
+ }
+ if (*p == '\\') {
+ if (!p[1] || p[1] == '\n' ||
+ (p[1] == '\r' && (!p[2] || p[2] == '\n'))) {
+ *continued = 1;
+ return;
+ }
+ p++;
+ }
+ }
+
+ *continued = 0;
+ *in_quote = 0;
+}
+
/* if new_name == NULL, the section is removed instead */
static int repo_config_copy_or_rename_section_in_file(
struct repository *r,
@@ -3434,7 +3525,7 @@ static int repo_config_copy_or_rename_section_in_file(
const char *old_name,
const char *new_name, int copy)
{
- int ret = 0, remove = 0;
+ int ret = 0, remove = 0, continued = 0, in_quote = 0;
char *filename_buf = NULL;
struct lock_file lock = LOCK_INIT;
int out_fd;
@@ -3496,9 +3587,12 @@ static int repo_config_copy_or_rename_section_in_file(
goto out;
}
+ int was_continued = continued;
+
for (i = 0; buf.buf[i] && isspace(buf.buf[i]); i++)
; /* do nothing */
- if (buf.buf[i] == '[') {
+ update_line_continuation(buf.buf, &continued, &in_quote);
+ if (!was_continued && buf.buf[i] == '[') {
/* it's a section */
size_t offset;
is_section = 1;
@@ -3550,8 +3644,9 @@ static int repo_config_copy_or_rename_section_in_file(
strbuf_release(©str);
copystr = store_create_section(new_name, &store);
}
+ } else {
+ remove = 0;
}
- remove = 0;
}
if (remove)
continue;
diff --git a/t/t1300-config.sh b/t/t1300-config.sh
index 876d376cc7..3b323f95f8 100755
--- a/t/t1300-config.sh
+++ b/t/t1300-config.sh
@@ -3049,4 +3049,37 @@ test_expect_success POSIXPERM 'refuse to "overwrite" a read-only file' '
)
'
+test_expect_success 'remove-section and rename-section ignore bracketed continuation lines' '
+ test_when_finished "rm -f multiline.cfg rename.cfg subbracket.cfg" &&
+ cat >multiline.cfg <<-\EOF &&
+ [old]
+ key = "first \
+ [smuggled] \
+ # quoted hash \
+ evil = yes"
+ [keep]
+ ok = true
+ EOF
+ cp multiline.cfg rename.cfg &&
+
+ git config --file=multiline.cfg --remove-section old &&
+ test_must_fail git config --file=multiline.cfg --get smuggled.evil &&
+ test "$(git config --file=multiline.cfg --get keep.ok)" = true &&
+
+ git config --file=rename.cfg --rename-section old renamed &&
+ test_must_fail git config --file=rename.cfg --get smuggled.evil &&
+ git config --file=rename.cfg --get renamed.key >actual &&
+ grep "\[smuggled\]" actual &&
+ test "$(git config --file=rename.cfg --get keep.ok)" = true &&
+
+ cat >subbracket.cfg <<-\EOF &&
+ [keep "a] = \""] k = v ; \
+ [old]
+ secret = should_be_removed
+ EOF
+ git config --file=subbracket.cfg --remove-section old &&
+ test_must_fail git config --file=subbracket.cfg --get old.secret &&
+ test "$(git config --file=subbracket.cfg --get "keep.a] = \".k")" = v
+'
+
test_done
--
2.56.1.750.gf70dc682cf-goog
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH] config: skip continued lines when matching section
2026-11-19 19:23 [PATCH] config: skip continued lines when matching section Dominik Maier
@ 2026-11-20 0:44 ` Junio C Hamano
2026-11-20 14:33 ` Phillip Wood
0 siblings, 1 reply; 3+ messages in thread
From: Junio C Hamano @ 2026-11-20 0:44 UTC (permalink / raw)
To: Dominik Maier
Cc: git, Patrick Steinhardt, Jeff King, Taylor Blau,
Johannes Schindelin, dmnk+artist
Dominik Maier <dmnk@google.com> writes:
> When a value in the target section spans multiple lines via a trailing
> backslash and a continuation line begins with '[other]', removing or
> renaming the section stops early at '[other]' and leaves the remainder
> of the continued value behind as an unquoted '[other]' section.
If the behaviour was to treat these as continued values from day
one, I would have agreed that it would be a reasonably design, but
offhand I do not know if this is a breaking change in behaviour that
may negatively affect existing configuration files.
> @@ -3434,7 +3525,7 @@ static int repo_config_copy_or_rename_section_in_file(
> const char *old_name,
> const char *new_name, int copy)
> {
> - int ret = 0, remove = 0;
> + int ret = 0, remove = 0, continued = 0, in_quote = 0;
> char *filename_buf = NULL;
> struct lock_file lock = LOCK_INIT;
> int out_fd;
> @@ -3496,9 +3587,12 @@ static int repo_config_copy_or_rename_section_in_file(
> goto out;
> }
>
> + int was_continued = continued;
> +
This triggers -Wdeclaration-after-statement doesn't it?
(administrivia)
I do not consider myself an area expert for the configuration file
editing code. Please do not directly Cc: random people unless you
know they are specifically familiar with the area or they are
positively interested in the topic and they will regret if they miss
your message if they fail to notice.
Thanks.
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] config: skip continued lines when matching section
2026-11-20 0:44 ` Junio C Hamano
@ 2026-11-20 14:33 ` Phillip Wood
0 siblings, 0 replies; 3+ messages in thread
From: Phillip Wood @ 2026-11-20 14:33 UTC (permalink / raw)
To: Junio C Hamano, Dominik Maier
Cc: git, Patrick Steinhardt, Jeff King, Taylor Blau,
Johannes Schindelin, dmnk+artist
On 20/11/2026 00:44, Junio C Hamano wrote:
> Dominik Maier <dmnk@google.com> writes:
>
>> When a value in the target section spans multiple lines via a trailing
>> backslash and a continuation line begins with '[other]', removing or
>> renaming the section stops early at '[other]' and leaves the remainder
>> of the continued value behind as an unquoted '[other]' section.
>
> If the behaviour was to treat these as continued values from day
> one, I would have agreed that it would be a reasonably design, but
> offhand I do not know if this is a breaking change in behaviour that
> may negatively affect existing configuration files.
As I understand it, this patch changes the way we parse the config when
we're renaming or copying a section to match the way we parse it when we
read config values. It does not change how we read config settings so I
think it is simply a bug fix for an unlikely corner case when copying or
renaming a section. It's a pity we don't share a common code path for
the parsing and end up implementing it twice.
I had a quick look at the code in this patch and I'm not sure it handles
values and subsection names that contain carriage returns correctly. As
far as I can tell
printf '[a "b\rc"]\rkey=\rv\ralue' >/tmp/test.config
creates a valid config file. For example
git config get -f /tmp/test.config "$(printf 'a.b\rc.key')" |
tr '\r' Q
prints "vQal" but this patch treats '\r' like '\n'
Thanks
Phillip
>> @@ -3434,7 +3525,7 @@ static int repo_config_copy_or_rename_section_in_file(
>> const char *old_name,
>> const char *new_name, int copy)
>> {
>> - int ret = 0, remove = 0;
>> + int ret = 0, remove = 0, continued = 0, in_quote = 0;
>> char *filename_buf = NULL;
>> struct lock_file lock = LOCK_INIT;
>> int out_fd;
>> @@ -3496,9 +3587,12 @@ static int repo_config_copy_or_rename_section_in_file(
>> goto out;
>> }
>>
>> + int was_continued = continued;
>> +
>
> This triggers -Wdeclaration-after-statement doesn't it?
>
> (administrivia)
>
> I do not consider myself an area expert for the configuration file
> editing code. Please do not directly Cc: random people unless you
> know they are specifically familiar with the area or they are
> positively interested in the topic and they will regret if they miss
> your message if they fail to notice.
>
> Thanks.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-20 14:33 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 19:23 [PATCH] config: skip continued lines when matching section Dominik Maier
2026-11-20 0:44 ` Junio C Hamano
2026-11-20 14:33 ` Phillip Wood
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.