Git development
 help / color / mirror / Atom feed
* [PATCH] config: skip continued lines when matching section
@ 2026-11-19 19:23 Dominik Maier
  2026-11-20  0:44 ` Junio C Hamano
  2026-11-24 18:05 ` [PATCH v2] " Dominik Maier
  0 siblings, 2 replies; 8+ 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(&copystr);
 					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] 8+ 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
  2026-11-24 18:05 ` [PATCH v2] " Dominik Maier
  1 sibling, 1 reply; 8+ 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] 8+ 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; 8+ 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] 8+ messages in thread

* [PATCH v2] 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-24 18:05 ` Dominik Maier
  2026-11-26 10:37   ` Junio C Hamano
  1 sibling, 1 reply; 8+ messages in thread
From: Dominik Maier @ 2026-11-24 18:05 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Phillip Wood, dmnk+artist

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, unlike git_parse_source().

When a value in the target section spans multiple lines via a trailing
backslash and a continuation line begins with '[other]', removing,
renaming, or copying the section mishandles '[other]' and can leave the
remainder of the continued value behind as an unquoted '[other]'
section.

Track backslash value continuations (matching git_parse_source() on
quoted subsections, quote state across continued lines, CRLF line
endings, and bare carriage returns inside subsections or values), only
treat '[' as a section header at the start of a logical line, and copy
any inline entry following a copied section header.

Signed-off-by: Dominik Maier <dmnk@google.com>
---
Changes in v2:
- Move was_continued declaration to the top of the loop block to fix
  -Wdeclaration-after-statement, and trim the Cc list (Junio).
- Treat bare '\r' as a regular character rather than a line break so
  subsections and values containing '\r' follow git_parse_source(), while
  still handling CRLF line continuations (Phillip Wood).
- Reject empty '[]' section headers and preserve inline entries after a
  copied section header so 'git branch -c' handles continued inline
  entries as well.

 config.c          | 146 +++++++++++++++++++++++++++++++++++++++++++++-
 t/t1300-config.sh |  77 ++++++++++++++++++++++++
 2 files changed, 220 insertions(+), 3 deletions(-)

diff --git a/config.c b/config.c
index 275e709f06..cbdf3431f3 100644
--- a/config.c
+++ b/config.c
@@ -3428,6 +3428,140 @@ static int section_name_is_ok(const char *name)
 
 #define GIT_CONFIG_MAX_LINE_LEN (512 * 1024)
 
+static int is_config_eol(const char *p, const char *end)
+{
+	return p >= end || *p == '\n' ||
+	       (*p == '\r' && p + 1 < end && p[1] == '\n');
+}
+
+static const char *skip_section_header(const char *p, const char *end)
+{
+	const char *start;
+
+	if (is_config_eol(p, end) || *p != '[')
+		return NULL;
+	for (start = ++p; !is_config_eol(p, end); p++) {
+		if (*p == ']')
+			return p > start ? p + 1 : NULL;
+		if (isspace(*p)) {
+			while (!is_config_eol(p, end) && isspace(*p))
+				p++;
+			if (is_config_eol(p, end) || *p != '"')
+				return NULL;
+			for (p++; !is_config_eol(p, end); p++) {
+				if (*p == '\\') {
+					p++;
+					if (is_config_eol(p, end))
+						return NULL;
+					continue;
+				}
+				if (*p == '"') {
+					p++;
+					return (!is_config_eol(p, end) &&
+						*p == ']') ? p + 1 : NULL;
+				}
+			}
+			return NULL;
+		}
+		if (!iskeychar(*p) && *p != '.')
+			return NULL;
+	}
+	return NULL;
+}
+
+static void update_line_continuation(const char *p, const char *end,
+				     int is_first_line, int *continued,
+				     int *in_quote)
+{
+	int in_value = *continued;
+
+	if (is_first_line) {
+		size_t rem = end - p;
+		skip_prefix_mem(p, rem, utf8_bom, &p, &rem);
+	}
+
+	if (!in_value) {
+		while (!is_config_eol(p, end)) {
+			if (isspace(*p)) {
+				p++;
+				continue;
+			}
+			if (*p == '#' || *p == ';') {
+				*continued = 0;
+				*in_quote = 0;
+				return;
+			}
+			if (*p == '[') {
+				p = skip_section_header(p, end);
+				if (!p) {
+					*continued = 0;
+					*in_quote = 0;
+					return;
+				}
+				continue;
+			}
+			if (!isalpha(*p)) {
+				*continued = 0;
+				*in_quote = 0;
+				return;
+			}
+			for (p++; !is_config_eol(p, end) && iskeychar(*p); p++)
+				; /* skip key name */
+			while (!is_config_eol(p, end) &&
+			       (*p == ' ' || *p == '\t'))
+				p++;
+			if (!is_config_eol(p, end) && *p == '=') {
+				p++;
+				in_value = 1;
+				break;
+			}
+			*continued = 0;
+			*in_quote = 0;
+			return;
+		}
+		if (!in_value) {
+			*continued = 0;
+			*in_quote = 0;
+			return;
+		}
+	}
+
+	for (; !is_config_eol(p, end); p++) {
+		if (!*in_quote && (*p == '#' || *p == ';')) {
+			*continued = 0;
+			return;
+		}
+		if (*p == '"') {
+			*in_quote = !*in_quote;
+			continue;
+		}
+		if (*p == '\\') {
+			p++;
+			if (is_config_eol(p, end)) {
+				*continued = (p < end);
+				if (!*continued)
+					*in_quote = 0;
+				return;
+			}
+			switch (*p) {
+			case 't':
+			case 'b':
+			case 'n':
+			case '\\':
+			case '"':
+				break;
+			default:
+				*continued = 0;
+				*in_quote = 0;
+				return;
+			}
+		}
+	}
+
+	*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,
@@ -3435,7 +3569,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;
@@ -3485,7 +3619,7 @@ static int repo_config_copy_or_rename_section_in_file(
 
 	while (!strbuf_getwholeline(&buf, config_file, '\n')) {
 		size_t i, length;
-		int is_section = 0;
+		int is_section = 0, was_continued = continued;
 		char *output = buf.buf;
 
 		line_nr++;
@@ -3499,7 +3633,9 @@ static int repo_config_copy_or_rename_section_in_file(
 
 		for (i = 0; buf.buf[i] && isspace(buf.buf[i]); i++)
 			; /* do nothing */
-		if (buf.buf[i] == '[') {
+		update_line_continuation(buf.buf, buf.buf + buf.len,
+					 line_nr == 1, &continued, &in_quote);
+		if (!was_continued && buf.buf[i] == '[') {
 			/* it's a section */
 			size_t offset;
 			is_section = 1;
@@ -3550,6 +3686,10 @@ static int repo_config_copy_or_rename_section_in_file(
 				} else {
 					strbuf_release(&copystr);
 					copystr = store_create_section(new_name, &store);
+					if (output[offset + i]) {
+						strbuf_addch(&copystr, '\t');
+						strbuf_addstr(&copystr, output + offset + i);
+					}
 				}
 			}
 			remove = 0;
diff --git a/t/t1300-config.sh b/t/t1300-config.sh
index 16bb396b63..b5b6fc2acc 100755
--- a/t/t1300-config.sh
+++ b/t/t1300-config.sh
@@ -3081,4 +3081,81 @@ 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 cr.cfg crlf.cfg emptysec.cfg raw actual expect" &&
+	cat >multiline.cfg <<-\EOF &&
+	[old]
+		key = "first \
+	[smuggled] \
+	# quoted hash \
+		evil = yes" ; comment \
+	[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 &&
+	test_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 &&
+
+	printf "[a \"b\rc\"]\rkey=\rv\ralue\\\\\n[smuggled]\n\tevil = yes\n[keep]\n\tok = true\n" >cr.cfg &&
+	git config --file=cr.cfg --rename-section "$(printf "a.b\rc")" renamed &&
+	test_must_fail git config --file=cr.cfg --get smuggled.evil &&
+	git config --file=cr.cfg --get renamed.key >raw &&
+	tr "\r" Q <raw >actual &&
+	echo "vQalue[smuggled]" >expect &&
+	test_cmp expect actual &&
+	git config --file=cr.cfg --remove-section renamed &&
+	test_must_fail git config --file=cr.cfg --get smuggled.evil &&
+	test "$(git config --file=cr.cfg --get keep.ok)" = true &&
+
+	printf "[old]\r\n\tkey = first \\\\\r\n[smuggled]\r\n\tevil = yes\r\n[keep]\r\n\tok = true\r\n" >crlf.cfg &&
+	git config --file=crlf.cfg --remove-section old &&
+	test_must_fail git config --file=crlf.cfg --get smuggled.evil &&
+	test "$(git config --file=crlf.cfg --get keep.ok)" = true &&
+
+	cat >emptysec.cfg <<-\EOF &&
+	[] k = v \
+	[old]
+		secret = gone
+	EOF
+	git config --file=emptysec.cfg --remove-section old &&
+	test_grep ! "secret" emptysec.cfg &&
+
+	test_when_finished "rm -rf copy-repo" &&
+	git init copy-repo &&
+	(
+		cd copy-repo &&
+		test_commit init &&
+		git branch src &&
+		cat >>.git/config <<-\EOF &&
+		[branch "src"] note = first \
+		[smuggled]
+			evil = yes
+		[keep]
+			ok = true
+		EOF
+		git branch -c src dst &&
+		test_must_fail git config --get smuggled.evil &&
+		test "$(git config --get branch.dst.note)" = "first [smuggled]" &&
+		test "$(git config --get branch.dst.evil)" = yes &&
+		test "$(git config --get keep.ok)" = true
+	)
+'
+
 test_done

Range-diff against v1:
1:  8f0e9e660a ! 1:  8047ce90c2 config: skip continued lines when matching section
    @@ Commit message
         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.
    +    values across lines, unlike git_parse_source().
     
         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.
    +    backslash and a continuation line begins with '[other]', removing,
    +    renaming, or copying the section mishandles '[other]' and can leave 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.
    +    Track backslash value continuations (matching git_parse_source() on
    +    quoted subsections, quote state across continued lines, CRLF line
    +    endings, and bare carriage returns inside subsections or values), only
    +    treat '[' as a section header at the start of a logical line, and copy
    +    any inline entry following a copied section header.
     
         Signed-off-by: Dominik Maier <dmnk@google.com>
     
    @@ config.c: 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)
    ++static int is_config_eol(const char *p, const char *end)
     +{
    -+	const char *p = line;
    ++	return p >= end || *p == '\n' ||
    ++	       (*p == '\r' && p + 1 < end && p[1] == '\n');
    ++}
    ++
    ++static const char *skip_section_header(const char *p, const char *end)
    ++{
    ++	const char *start;
     +
    -+	if (*p != '[')
    -+		return line;
    -+	for (p++; *p && *p != '\r' && *p != '\n'; p++) {
    ++	if (is_config_eol(p, end) || *p != '[')
    ++		return NULL;
    ++	for (start = ++p; !is_config_eol(p, end); p++) {
     +		if (*p == ']')
    -+			return p + 1;
    -+		if (*p == ' ' || *p == '\t') {
    -+			while (*p == ' ' || *p == '\t')
    ++			return p > start ? p + 1 : NULL;
    ++		if (isspace(*p)) {
    ++			while (!is_config_eol(p, end) && isspace(*p))
     +				p++;
    -+			if (*p != '"')
    ++			if (is_config_eol(p, end) || *p != '"')
     +				return NULL;
    -+			for (p++; *p && *p != '\r' && *p != '\n'; p++) {
    ++			for (p++; !is_config_eol(p, end); p++) {
     +				if (*p == '\\') {
     +					p++;
    -+					if (!*p || *p == '\r' || *p == '\n')
    ++					if (is_config_eol(p, end))
     +						return NULL;
     +					continue;
     +				}
     +				if (*p == '"') {
    -+					for (p++; *p == ' ' || *p == '\t'; p++)
    -+						; /* skip trailing space before ']' */
    -+					return (*p == ']') ? p + 1 : NULL;
    ++					p++;
    ++					return (!is_config_eol(p, end) &&
    ++						*p == ']') ? p + 1 : NULL;
     +				}
     +			}
     +			return NULL;
     +		}
    ++		if (!iskeychar(*p) && *p != '.')
    ++			return NULL;
     +	}
     +	return NULL;
     +}
     +
    -+static void update_line_continuation(const char *p, int *continued,
    ++static void update_line_continuation(const char *p, const char *end,
    ++				     int is_first_line, int *continued,
     +				     int *in_quote)
     +{
     +	int in_value = *continued;
     +
    ++	if (is_first_line) {
    ++		size_t rem = end - p;
    ++		skip_prefix_mem(p, rem, utf8_bom, &p, &rem);
    ++	}
    ++
     +	if (!in_value) {
    -+		while (isspace(*p))
    -+			p++;
    -+		if (*p == '[') {
    -+			p = skip_section_header(p);
    -+			if (!p) {
    ++		while (!is_config_eol(p, end)) {
    ++			if (isspace(*p)) {
    ++				p++;
    ++				continue;
    ++			}
    ++			if (*p == '#' || *p == ';') {
     +				*continued = 0;
     +				*in_quote = 0;
     +				return;
     +			}
    -+		}
    -+		for (; *p && *p != '\r' && *p != '\n'; p++) {
    -+			if (*p == '#' || *p == ';') {
    ++			if (*p == '[') {
    ++				p = skip_section_header(p, end);
    ++				if (!p) {
    ++					*continued = 0;
    ++					*in_quote = 0;
    ++					return;
    ++				}
    ++				continue;
    ++			}
    ++			if (!isalpha(*p)) {
     +				*continued = 0;
     +				*in_quote = 0;
     +				return;
     +			}
    -+			if (*p == '=') {
    ++			for (p++; !is_config_eol(p, end) && iskeychar(*p); p++)
    ++				; /* skip key name */
    ++			while (!is_config_eol(p, end) &&
    ++			       (*p == ' ' || *p == '\t'))
    ++				p++;
    ++			if (!is_config_eol(p, end) && *p == '=') {
     +				p++;
     +				in_value = 1;
     +				break;
     +			}
    ++			*continued = 0;
    ++			*in_quote = 0;
    ++			return;
     +		}
     +		if (!in_value) {
     +			*continued = 0;
    @@ config.c: static int section_name_is_ok(const char *name)
     +		}
     +	}
     +
    -+	for (; *p && *p != '\r' && *p != '\n'; p++) {
    ++	for (; !is_config_eol(p, end); p++) {
     +		if (!*in_quote && (*p == '#' || *p == ';')) {
     +			*continued = 0;
     +			return;
    @@ config.c: static int section_name_is_ok(const char *name)
     +			continue;
     +		}
     +		if (*p == '\\') {
    -+			if (!p[1] || p[1] == '\n' ||
    -+			    (p[1] == '\r' && (!p[2] || p[2] == '\n'))) {
    -+				*continued = 1;
    ++			p++;
    ++			if (is_config_eol(p, end)) {
    ++				*continued = (p < end);
    ++				if (!*continued)
    ++					*in_quote = 0;
    ++				return;
    ++			}
    ++			switch (*p) {
    ++			case 't':
    ++			case 'b':
    ++			case 'n':
    ++			case '\\':
    ++			case '"':
    ++				break;
    ++			default:
    ++				*continued = 0;
    ++				*in_quote = 0;
     +				return;
     +			}
    -+			p++;
     +		}
     +	}
     +
    @@ config.c: static int repo_config_copy_or_rename_section_in_file(
      	struct lock_file lock = LOCK_INIT;
      	int out_fd;
     @@ config.c: static int repo_config_copy_or_rename_section_in_file(
    - 			goto out;
    - 		}
      
    -+		int was_continued = continued;
    -+
    + 	while (!strbuf_getwholeline(&buf, config_file, '\n')) {
    + 		size_t i, length;
    +-		int is_section = 0;
    ++		int is_section = 0, was_continued = continued;
    + 		char *output = buf.buf;
    + 
    + 		line_nr++;
    +@@ config.c: static int repo_config_copy_or_rename_section_in_file(
    + 
      		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);
    ++		update_line_continuation(buf.buf, buf.buf + buf.len,
    ++					 line_nr == 1, &continued, &in_quote);
     +		if (!was_continued && buf.buf[i] == '[') {
      			/* it's a section */
      			size_t offset;
      			is_section = 1;
     @@ config.c: static int repo_config_copy_or_rename_section_in_file(
    + 				} else {
      					strbuf_release(&copystr);
      					copystr = store_create_section(new_name, &store);
    ++					if (output[offset + i]) {
    ++						strbuf_addch(&copystr, '\t');
    ++						strbuf_addstr(&copystr, output + offset + i);
    ++					}
      				}
    -+			} else {
    -+				remove = 0;
      			}
    --			remove = 0;
    - 		}
    - 		if (remove)
    - 			continue;
    + 			remove = 0;
     
      ## t/t1300-config.sh ##
     @@ t/t1300-config.sh: test_expect_success POSIXPERM 'refuse to "overwrite" a read-only file' '
    @@ t/t1300-config.sh: test_expect_success POSIXPERM 'refuse to "overwrite" a read-o
      '
      
     +test_expect_success 'remove-section and rename-section ignore bracketed continuation lines' '
    -+	test_when_finished "rm -f multiline.cfg rename.cfg subbracket.cfg" &&
    ++	test_when_finished "rm -f multiline.cfg rename.cfg subbracket.cfg cr.cfg crlf.cfg emptysec.cfg raw actual expect" &&
     +	cat >multiline.cfg <<-\EOF &&
     +	[old]
     +		key = "first \
     +	[smuggled] \
     +	# quoted hash \
    -+		evil = yes"
    ++		evil = yes" ; comment \
     +	[keep]
     +		ok = true
     +	EOF
    @@ t/t1300-config.sh: test_expect_success POSIXPERM 'refuse to "overwrite" a read-o
     +	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_grep "\[smuggled\]" actual &&
     +	test "$(git config --file=rename.cfg --get keep.ok)" = true &&
     +
     +	cat >subbracket.cfg <<-\EOF &&
    @@ t/t1300-config.sh: test_expect_success POSIXPERM 'refuse to "overwrite" a read-o
     +	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 "$(git config --file=subbracket.cfg --get "keep.a] = \".k")" = v &&
    ++
    ++	printf "[a \"b\rc\"]\rkey=\rv\ralue\\\\\n[smuggled]\n\tevil = yes\n[keep]\n\tok = true\n" >cr.cfg &&
    ++	git config --file=cr.cfg --rename-section "$(printf "a.b\rc")" renamed &&
    ++	test_must_fail git config --file=cr.cfg --get smuggled.evil &&
    ++	git config --file=cr.cfg --get renamed.key >raw &&
    ++	tr "\r" Q <raw >actual &&
    ++	echo "vQalue[smuggled]" >expect &&
    ++	test_cmp expect actual &&
    ++	git config --file=cr.cfg --remove-section renamed &&
    ++	test_must_fail git config --file=cr.cfg --get smuggled.evil &&
    ++	test "$(git config --file=cr.cfg --get keep.ok)" = true &&
    ++
    ++	printf "[old]\r\n\tkey = first \\\\\r\n[smuggled]\r\n\tevil = yes\r\n[keep]\r\n\tok = true\r\n" >crlf.cfg &&
    ++	git config --file=crlf.cfg --remove-section old &&
    ++	test_must_fail git config --file=crlf.cfg --get smuggled.evil &&
    ++	test "$(git config --file=crlf.cfg --get keep.ok)" = true &&
    ++
    ++	cat >emptysec.cfg <<-\EOF &&
    ++	[] k = v \
    ++	[old]
    ++		secret = gone
    ++	EOF
    ++	git config --file=emptysec.cfg --remove-section old &&
    ++	test_grep ! "secret" emptysec.cfg &&
    ++
    ++	test_when_finished "rm -rf copy-repo" &&
    ++	git init copy-repo &&
    ++	(
    ++		cd copy-repo &&
    ++		test_commit init &&
    ++		git branch src &&
    ++		cat >>.git/config <<-\EOF &&
    ++		[branch "src"] note = first \
    ++		[smuggled]
    ++			evil = yes
    ++		[keep]
    ++			ok = true
    ++		EOF
    ++		git branch -c src dst &&
    ++		test_must_fail git config --get smuggled.evil &&
    ++		test "$(git config --get branch.dst.note)" = "first [smuggled]" &&
    ++		test "$(git config --get branch.dst.evil)" = yes &&
    ++		test "$(git config --get keep.ok)" = true
    ++	)
     +'
     +
      test_done
-- 
2.56.1.831.g720f3f5937-goog


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH v2] config: skip continued lines when matching section
  2026-11-24 18:05 ` [PATCH v2] " Dominik Maier
@ 2026-11-26 10:37   ` Junio C Hamano
  2026-11-26 11:00     ` Chris Torek
                       ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Junio C Hamano @ 2026-11-26 10:37 UTC (permalink / raw)
  To: Dominik Maier; +Cc: git, Phillip Wood, dmnk+artist

Dominik Maier <dmnk@google.com> writes:

> 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, unlike git_parse_source().

That sums up the mismatch cleanly.  Because git_parse_source() already
treats a trailing backslash on a value line as continuing onto the next
line, keeping repo_config_copy_or_rename_section_in_file() in sync fixes
a real corruption bug for remove, rename, and copy operations without
changing how config files are read.

> diff --git a/config.c b/config.c
> index 275e709f06..cbdf3431f3 100644
> --- a/config.c
> +++ b/config.c
> @@ -3428,6 +3428,140 @@ static int section_name_is_ok(const char *name)
>  
>  #define GIT_CONFIG_MAX_LINE_LEN (512 * 1024)
>  
> +static int is_config_eol(const char *p, const char *end)
> +{
> +	return p >= end || *p == '\n' ||
> +	       (*p == '\r' && p + 1 < end && p[1] == '\n');
> +}

This matches get_next_char() in config.c, which only collapses a '\r'
followed by '\n' into '\n' and otherwise passes bare '\r' through as
part of a subsection name or value.

Using p < end in the caller when setting *continued also avoids falsely
continuing when a file ends with a trailing backslash and no newline.

> +static const char *skip_section_header(const char *p, const char *end)
> +{
> +	const char *start;
> +
> +	if (is_config_eol(p, end) || *p != '[')
> +		return NULL;

We return when at the end of line, or not at '['.  Otherwise we go to
the next loop.  Wait...why check eol?  '\r' or '\n' are certainly not
'[' so wouldn't it be the same as "if (end <= p || *p != '[')"?  Wnat am
I missing?

Anyway, we saw '[' and come here.  We keep scanning ...

> +	for (start = ++p; !is_config_eol(p, end); p++) {
> +		if (*p == ']')
> +			return p > start ? p + 1 : NULL;

... and eventually find ']' before we see the end of line that had the
'[' on it.  Initialization is (start = ++p) so in the first iteration
we have (p == start), in which case we return NULL (i.e., we reject
"[]").  Otherwise we return the pointer past that closing ']'.

The proposed commit log message does not make it clear why we want to
reject an empty "[]".  Or what should happen to say "[ ]" for that
matter.

> +		if (isspace(*p)) {
> +			while (!is_config_eol(p, end) && isspace(*p))
> +				p++;

We allow whitespaces after the opening "[".  Then

> +			if (is_config_eol(p, end) || *p != '"')
> +				return NULL;

We want a double quote.  Wait, why double quote?

	[core] username = ...
	[remote "origin"] fetch = ...

We do not allow whitespace before 'core' or 'remote' in the above
example.  We see after this block we reject letters that are not key
letters or dot but otherwise keep looping.  So 'core' and 'remote' in
the above example are simply skipped over while this loop spins.  OK.
And after skipping over 'remote', we skip whitespaces, and then yes, we
expect double-quote at the beginning of 'origin'.

    Side note.  I recall we also support

	[remote.origin] fetch = ...

    as a less expressive (because you can only have keychars there)
    deprecated synonym.  They are treated just like [core] is treated by
    this code, I suppose.

> +			for (p++; !is_config_eol(p, end); p++) {

And we keep reading inside the double quotes.

> +				if (*p == '\\') {
> +					p++;
> +					if (is_config_eol(p, end))
> +						return NULL;
> +					continue;
> +				}

We allow a backslash to escape any single letter, and this can be used
to escape a double quote that is a part of the second level variable
name.  But we reject

	[foo "bar\
	... anything here ...

i.e., backslash and then end of line.

> +				if (*p == '"') {
> +					p++;
> +					return (!is_config_eol(p, end) &&
> +						*p == ']') ? p + 1 : NULL;
> +				}

After we see a closing double quote, if we are not yet at the end of
line, we may be ']' in which case we are happy and return the pointer to
the byte after that closing bracket; otherwise we reject and return NULL.

If we do not have ']' immediately after double quote we reject, which
matches what get_extended_base_var() does.

And all other letters are simply skipped in this loop.

> +			}
> +			return NULL;

If we exited the loop while looking for the closing ']', we reject.

> +		}
> +		if (!iskeychar(*p) && *p != '.')
> +			return NULL;
> +	}
> +	return NULL;
> +}

OK.  So we find [section "header"] that is syntactically valid?

> +	for (; !is_config_eol(p, end); p++) {
> +		if (!*in_quote && (*p == '#' || *p == ';')) {
> +			*continued = 0;
> +			return;
> +		}
> +		if (*p == '"') {
> +			*in_quote = !*in_quote;
> +			continue;
> +		}
> +		if (*p == '\\') {
> +			p++;
> +			if (is_config_eol(p, end)) {
> +				*continued = (p < end);
> +				if (!*continued)
> +					*in_quote = 0;
> +				return;
> +			}

Tracking *in_quote across continued lines makes sure '#' and ';' inside
a quoted multi-line value do not prematurely clear *continued, while an
invalid escape or a bare '\r' after '\' resets both flags just as
parse_value() rejects them.  Tricky.

> @@ -3550,6 +3686,10 @@ static int repo_config_copy_or_rename_section_in_file(
>  				} else {
>  					strbuf_release(&copystr);
>  					copystr = store_create_section(new_name, &store);
> +					if (output[offset + i]) {
> +						strbuf_addch(&copystr, '\t');
> +						strbuf_addstr(&copystr, output + offset + i);
> +					}
>  				}

For context, section_name_match() gobbles every trailing isspace()
character after ']', including '\r' and '\n', via

    i++;
    for (; buf[i] && isspace(buf[i]); i++)
            ; /* do nothing */
    return i;

So output[offset + i] is NUL on a plain "[section]\n" or "[section]\r\n"
line, and non-empty only when an inline entry or comment follows ']',
matching the existing rename branch.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2] config: skip continued lines when matching section
  2026-11-26 10:37   ` Junio C Hamano
@ 2026-11-26 11:00     ` Chris Torek
  2026-11-30 16:22     ` Dominik Maier
  2026-11-30 16:22     ` [PATCH v3] " Dominik Maier
  2 siblings, 0 replies; 8+ messages in thread
From: Chris Torek @ 2026-11-26 11:00 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Dominik Maier, git, Phillip Wood, dmnk+artist

On Thu, Nov 26, 2026 at 2:37 AM Junio C Hamano <gitster@pobox.com> wrote:
> That sums up the mismatch cleanly.  Because git_parse_source() already
> treats a trailing backslash on a value line as continuing onto the next
> line, keeping repo_config_copy_or_rename_section_in_file() in sync fixes
> a real corruption bug for remove, rename, and copy operations without
> changing how config files are read.

Right.

There are some peculiarities though, as I found out when I was trying
to pin down the very-precise behavior of config files. (In particular,
"git config set" when there are "include" directives, or implied
git-config-set operations by git operating on .git/config ...
misbehave when the values in question are actually found inside an
included config. Brian, I think, noted that some people are actually
using include directives here. Perhaps Git could note and warn/fail if
they cannot be updated.)

> This matches get_next_char() in config.c, which only collapses a '\r'
> followed by '\n' into '\n' and otherwise passes bare '\r' through as
> part of a subsection name or value.

Although it's invalid as a subsection name except inside quotes. That
whole "[sec.tion.name]" backwards compat thing... Anyway, I'm not sure
there's any actual value to a literal carriage return inside the
quoted part of a section name, but it's allowed!

> We allow a backslash to escape any single letter, and this can be used
> to escape a double quote that is a part of the second level variable
> name.  But we reject
>
>         [foo "bar\
>         ... anything here ...
>
> i.e., backslash and then end of line.

Yes, this isn't valid in section headers. Which I think is a little
inconsistent, but that's how it does work...

> Tracking *in_quote across continued lines makes sure '#' and ';' inside
> a quoted multi-line value do not prematurely clear *continued, while an
> invalid escape or a bare '\r' after '\' resets both flags just as
> parse_value() rejects them.  Tricky.

This one surprised me, because based on that code, I initially thought:

    [sec "tion"]
        var = "quoted
     value"

would therefore be valid, but it's not: you really do need the
backslash at the end of the continued line, even if in quotes.

Chris

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2] config: skip continued lines when matching section
  2026-11-26 10:37   ` Junio C Hamano
  2026-11-26 11:00     ` Chris Torek
@ 2026-11-30 16:22     ` Dominik Maier
  2026-11-30 16:22     ` [PATCH v3] " Dominik Maier
  2 siblings, 0 replies; 8+ messages in thread
From: Dominik Maier @ 2026-11-30 16:22 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Dominik Maier, git, Phillip Wood, Chris Torek, dmnk+artist

On Thu, Nov 26, 2026 at 02:37:38AM -0800, Junio C Hamano wrote:
> We return when at the end of line, or not at '['.  Otherwise we go to
> the next loop.  Wait...why check eol?  '\r' or '\n' are certainly not
> '[' so wouldn't it be the same as "if (end <= p || *p != '[')"?  Wnat am
> I missing?

Nothing missed, "if (end <= p || *p != '[')" is strictly simpler since
'*p != '['' already rejects '\r' and '\n'.

> The proposed commit log message does not make it clear why we want to
> reject an empty "[]".  Or what should happen to say "[ ]" for that
> matter.

In git_parse_source(), "[]" returns 0 from get_base_var() with an empty
strbuf and then stops at "var->len < 1", while "[ ]" enters
get_extended_base_var(), skips whitespace, sees ']' instead of '"', and
returns -1. Because git_parse_source() aborts on both rather than
treating the rest of the line as a key=value that could continue onto
the next line, skip_section_header() rejects both as well.

Sending v3 simplifying that guard, spelling out "[]" and "[ ]" in the
commit message, adding a "[ ]" test case, and rebasing cleanly onto
current master.

Best
Dominik

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v3] config: skip continued lines when matching section
  2026-11-26 10:37   ` Junio C Hamano
  2026-11-26 11:00     ` Chris Torek
  2026-11-30 16:22     ` Dominik Maier
@ 2026-11-30 16:22     ` Dominik Maier
  2 siblings, 0 replies; 8+ messages in thread
From: Dominik Maier @ 2026-11-30 16:22 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Phillip Wood, Chris Torek, dmnk+artist

Renaming, copying, or removing a config section treats a continued
multiline value starting with '[' as a new section header.

Unlike the main config parser, the section rewriter scans each physical
line on its own without tracking trailing backslash continuations. When
a value in the target section continues onto a line starting with
"[other]", removing or renaming the section stops early and leaves the
rest of the value behind as a real "[other]" section.

Track backslash and quote continuations across physical lines (including
CRLF endings, bare carriage returns, quoted subsections, and empty "[]"
or "[ ]" headers, which git_parse_source() rejects rather than reading
as key-value lines), match section headers only at the start of a
logical line, and keep any inline entry after a copied header.

Signed-off-by: Dominik Maier <dmnk@google.com>
---
Changes in v3:
- Simplify the opening guard in skip_section_header() to
  "if (end <= p || *p != '[')" (Junio C Hamano).
- Explain why empty "[]" and "[ ]" headers are rejected to match
  git_parse_source(), and add a "[ ]" test case (Junio C Hamano).
- Rebase onto master (86e79b2795).
- v2: https://lore.kernel.org/git/20261124180556.69724-1-dmnk@google.com/

 config.c          | 146 +++++++++++++++++++++++++++++++++++++++++++++-
 t/t1300-config.sh |  85 +++++++++++++++++++++++++++
 2 files changed, 228 insertions(+), 3 deletions(-)

diff --git a/config.c b/config.c
index 275e709f0..40eb1f15d 100644
--- a/config.c
+++ b/config.c
@@ -3428,6 +3428,140 @@ static int section_name_is_ok(const char *name)
 
 #define GIT_CONFIG_MAX_LINE_LEN (512 * 1024)
 
+static int is_config_eol(const char *p, const char *end)
+{
+	return p >= end || *p == '\n' ||
+	       (*p == '\r' && p + 1 < end && p[1] == '\n');
+}
+
+static const char *skip_section_header(const char *p, const char *end)
+{
+	const char *start;
+
+	if (end <= p || *p != '[')
+		return NULL;
+	for (start = ++p; !is_config_eol(p, end); p++) {
+		if (*p == ']')
+			return p > start ? p + 1 : NULL;
+		if (isspace(*p)) {
+			while (!is_config_eol(p, end) && isspace(*p))
+				p++;
+			if (is_config_eol(p, end) || *p != '"')
+				return NULL;
+			for (p++; !is_config_eol(p, end); p++) {
+				if (*p == '\\') {
+					p++;
+					if (is_config_eol(p, end))
+						return NULL;
+					continue;
+				}
+				if (*p == '"') {
+					p++;
+					return (!is_config_eol(p, end) &&
+						*p == ']') ? p + 1 : NULL;
+				}
+			}
+			return NULL;
+		}
+		if (!iskeychar(*p) && *p != '.')
+			return NULL;
+	}
+	return NULL;
+}
+
+static void update_line_continuation(const char *p, const char *end,
+				     int is_first_line, int *continued,
+				     int *in_quote)
+{
+	int in_value = *continued;
+
+	if (is_first_line) {
+		size_t rem = end - p;
+		skip_prefix_mem(p, rem, utf8_bom, &p, &rem);
+	}
+
+	if (!in_value) {
+		while (!is_config_eol(p, end)) {
+			if (isspace(*p)) {
+				p++;
+				continue;
+			}
+			if (*p == '#' || *p == ';') {
+				*continued = 0;
+				*in_quote = 0;
+				return;
+			}
+			if (*p == '[') {
+				p = skip_section_header(p, end);
+				if (!p) {
+					*continued = 0;
+					*in_quote = 0;
+					return;
+				}
+				continue;
+			}
+			if (!isalpha(*p)) {
+				*continued = 0;
+				*in_quote = 0;
+				return;
+			}
+			for (p++; !is_config_eol(p, end) && iskeychar(*p); p++)
+				; /* skip key name */
+			while (!is_config_eol(p, end) &&
+			       (*p == ' ' || *p == '\t'))
+				p++;
+			if (!is_config_eol(p, end) && *p == '=') {
+				p++;
+				in_value = 1;
+				break;
+			}
+			*continued = 0;
+			*in_quote = 0;
+			return;
+		}
+		if (!in_value) {
+			*continued = 0;
+			*in_quote = 0;
+			return;
+		}
+	}
+
+	for (; !is_config_eol(p, end); p++) {
+		if (!*in_quote && (*p == '#' || *p == ';')) {
+			*continued = 0;
+			return;
+		}
+		if (*p == '"') {
+			*in_quote = !*in_quote;
+			continue;
+		}
+		if (*p == '\\') {
+			p++;
+			if (is_config_eol(p, end)) {
+				*continued = (p < end);
+				if (!*continued)
+					*in_quote = 0;
+				return;
+			}
+			switch (*p) {
+			case 't':
+			case 'b':
+			case 'n':
+			case '\\':
+			case '"':
+				break;
+			default:
+				*continued = 0;
+				*in_quote = 0;
+				return;
+			}
+		}
+	}
+
+	*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,
@@ -3435,7 +3569,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;
@@ -3485,7 +3619,7 @@ static int repo_config_copy_or_rename_section_in_file(
 
 	while (!strbuf_getwholeline(&buf, config_file, '\n')) {
 		size_t i, length;
-		int is_section = 0;
+		int is_section = 0, was_continued = continued;
 		char *output = buf.buf;
 
 		line_nr++;
@@ -3499,7 +3633,9 @@ static int repo_config_copy_or_rename_section_in_file(
 
 		for (i = 0; buf.buf[i] && isspace(buf.buf[i]); i++)
 			; /* do nothing */
-		if (buf.buf[i] == '[') {
+		update_line_continuation(buf.buf, buf.buf + buf.len,
+					 line_nr == 1, &continued, &in_quote);
+		if (!was_continued && buf.buf[i] == '[') {
 			/* it's a section */
 			size_t offset;
 			is_section = 1;
@@ -3550,6 +3686,10 @@ static int repo_config_copy_or_rename_section_in_file(
 				} else {
 					strbuf_release(&copystr);
 					copystr = store_create_section(new_name, &store);
+					if (output[offset + i]) {
+						strbuf_addch(&copystr, '\t');
+						strbuf_addstr(&copystr, output + offset + i);
+					}
 				}
 			}
 			remove = 0;
diff --git a/t/t1300-config.sh b/t/t1300-config.sh
index 16bb396b6..d1bf5327f 100755
--- a/t/t1300-config.sh
+++ b/t/t1300-config.sh
@@ -3081,4 +3081,89 @@ 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 cr.cfg crlf.cfg emptysec.cfg spacesec.cfg raw actual expect" &&
+	cat >multiline.cfg <<-\EOF &&
+	[old]
+		key = "first \
+	[smuggled] \
+	# quoted hash \
+		evil = yes" ; comment \
+	[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 &&
+	test_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 &&
+
+	printf "[a \"b\rc\"]\rkey=\rv\ralue\\\\\n[smuggled]\n\tevil = yes\n[keep]\n\tok = true\n" >cr.cfg &&
+	git config --file=cr.cfg --rename-section "$(printf "a.b\rc")" renamed &&
+	test_must_fail git config --file=cr.cfg --get smuggled.evil &&
+	git config --file=cr.cfg --get renamed.key >raw &&
+	tr "\r" Q <raw >actual &&
+	echo "vQalue[smuggled]" >expect &&
+	test_cmp expect actual &&
+	git config --file=cr.cfg --remove-section renamed &&
+	test_must_fail git config --file=cr.cfg --get smuggled.evil &&
+	test "$(git config --file=cr.cfg --get keep.ok)" = true &&
+
+	printf "[old]\r\n\tkey = first \\\\\r\n[smuggled]\r\n\tevil = yes\r\n[keep]\r\n\tok = true\r\n" >crlf.cfg &&
+	git config --file=crlf.cfg --remove-section old &&
+	test_must_fail git config --file=crlf.cfg --get smuggled.evil &&
+	test "$(git config --file=crlf.cfg --get keep.ok)" = true &&
+
+	cat >emptysec.cfg <<-\EOF &&
+	[] k = v \
+	[old]
+		secret = gone
+	EOF
+	git config --file=emptysec.cfg --remove-section old &&
+	test_grep ! "secret" emptysec.cfg &&
+
+	cat >spacesec.cfg <<-\EOF &&
+	[ ] k = v \
+	[old]
+		secret = gone
+	EOF
+	git config --file=spacesec.cfg --remove-section old &&
+	test_grep ! "secret" spacesec.cfg &&
+
+	test_when_finished "rm -rf copy-repo" &&
+	git init copy-repo &&
+	(
+		cd copy-repo &&
+		test_commit init &&
+		git branch src &&
+		cat >>.git/config <<-\EOF &&
+		[branch "src"] note = first \
+		[smuggled]
+			evil = yes
+		[keep]
+			ok = true
+		EOF
+		git branch -c src dst &&
+		test_must_fail git config --get smuggled.evil &&
+		test "$(git config --get branch.dst.note)" = "first [smuggled]" &&
+		test "$(git config --get branch.dst.evil)" = yes &&
+		test "$(git config --get keep.ok)" = true
+	)
+'
+
 test_done

-- 
2.56.1.911.g883d8bcb20-goog


^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-11-30 16:22 UTC | newest]

Thread overview: 8+ 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
2026-11-24 18:05 ` [PATCH v2] " Dominik Maier
2026-11-26 10:37   ` Junio C Hamano
2026-11-26 11:00     ` Chris Torek
2026-11-30 16:22     ` Dominik Maier
2026-11-30 16:22     ` [PATCH v3] " Dominik Maier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox