{"thread":{"id":"66887","subject":"[PATCH] config: skip continued lines when matching section","startedAt":"2026-11-19T19:23:07Z","lastAt":"2026-11-20T00:44:52Z","messageCount":2,"participants":["Dominik Maier","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"558880","messageId":"20261119192307.2683812-1-dmnk@google.com","threadId":"66887","inReplyTo":null,"subject":"[PATCH] config: skip continued lines when matching section","fromName":"Dominik Maier","fromEmail":"dmnk@google.com","sentAt":"2026-11-19T19:23:07Z","receivedAt":"2026-11-19T19:23:07Z","isPatch":true,"sender":{"key":"dmnk@google.com","avatar":null},"body":"repo_config_copy_or_rename_section_in_file() scans the config file line\nby line and treats any line whose first non-whitespace character is '['\nas the start of a new section, without tracking backslash-continued\nvalues across lines.\n\nWhen a value in the target section spans multiple lines via a trailing\nbackslash and a continuation line begins with '[other]', removing or\nrenaming the section stops early at '[other]' and leaves the remainder\nof the continued value behind as an unquoted '[other]' section.\n\nTrack backslash value continuations (including quoted subsections and\nquote state across continued lines so '#' or ';' inside quotes are not\nmistaken for comments) and only treat '[' as a section header at the\nstart of a logical line.\n\nSigned-off-by: Dominik Maier <dmnk@google.com>\n---\n config.c          | 101 ++++++++++++++++++++++++++++++++++++++++++++--\n t/t1300-config.sh |  33 +++++++++++++++\n 2 files changed, 131 insertions(+), 3 deletions(-)\n\ndiff --git a/config.c b/config.c\nindex 215dacb6b3..f5fcbd9b84 100644\n--- a/config.c\n+++ b/config.c\n@@ -3427,6 +3427,97 @@ static int section_name_is_ok(const char *name)\n \n #define GIT_CONFIG_MAX_LINE_LEN (512 * 1024)\n \n+static const char *skip_section_header(const char *line)\n+{\n+\tconst char *p = line;\n+\n+\tif (*p != '[')\n+\t\treturn line;\n+\tfor (p++; *p && *p != '\\r' && *p != '\\n'; p++) {\n+\t\tif (*p == ']')\n+\t\t\treturn p + 1;\n+\t\tif (*p == ' ' || *p == '\\t') {\n+\t\t\twhile (*p == ' ' || *p == '\\t')\n+\t\t\t\tp++;\n+\t\t\tif (*p != '\"')\n+\t\t\t\treturn NULL;\n+\t\t\tfor (p++; *p && *p != '\\r' && *p != '\\n'; p++) {\n+\t\t\t\tif (*p == '\\\\') {\n+\t\t\t\t\tp++;\n+\t\t\t\t\tif (!*p || *p == '\\r' || *p == '\\n')\n+\t\t\t\t\t\treturn NULL;\n+\t\t\t\t\tcontinue;\n+\t\t\t\t}\n+\t\t\t\tif (*p == '\"') {\n+\t\t\t\t\tfor (p++; *p == ' ' || *p == '\\t'; p++)\n+\t\t\t\t\t\t; /* skip trailing space before ']' */\n+\t\t\t\t\treturn (*p == ']') ? p + 1 : NULL;\n+\t\t\t\t}\n+\t\t\t}\n+\t\t\treturn NULL;\n+\t\t}\n+\t}\n+\treturn NULL;\n+}\n+\n+static void update_line_continuation(const char *p, int *continued,\n+\t\t\t\t     int *in_quote)\n+{\n+\tint in_value = *continued;\n+\n+\tif (!in_value) {\n+\t\twhile (isspace(*p))\n+\t\t\tp++;\n+\t\tif (*p == '[') {\n+\t\t\tp = skip_section_header(p);\n+\t\t\tif (!p) {\n+\t\t\t\t*continued = 0;\n+\t\t\t\t*in_quote = 0;\n+\t\t\t\treturn;\n+\t\t\t}\n+\t\t}\n+\t\tfor (; *p && *p != '\\r' && *p != '\\n'; p++) {\n+\t\t\tif (*p == '#' || *p == ';') {\n+\t\t\t\t*continued = 0;\n+\t\t\t\t*in_quote = 0;\n+\t\t\t\treturn;\n+\t\t\t}\n+\t\t\tif (*p == '=') {\n+\t\t\t\tp++;\n+\t\t\t\tin_value = 1;\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t}\n+\t\tif (!in_value) {\n+\t\t\t*continued = 0;\n+\t\t\t*in_quote = 0;\n+\t\t\treturn;\n+\t\t}\n+\t}\n+\n+\tfor (; *p && *p != '\\r' && *p != '\\n'; p++) {\n+\t\tif (!*in_quote && (*p == '#' || *p == ';')) {\n+\t\t\t*continued = 0;\n+\t\t\treturn;\n+\t\t}\n+\t\tif (*p == '\"') {\n+\t\t\t*in_quote = !*in_quote;\n+\t\t\tcontinue;\n+\t\t}\n+\t\tif (*p == '\\\\') {\n+\t\t\tif (!p[1] || p[1] == '\\n' ||\n+\t\t\t    (p[1] == '\\r' && (!p[2] || p[2] == '\\n'))) {\n+\t\t\t\t*continued = 1;\n+\t\t\t\treturn;\n+\t\t\t}\n+\t\t\tp++;\n+\t\t}\n+\t}\n+\n+\t*continued = 0;\n+\t*in_quote = 0;\n+}\n+\n /* if new_name == NULL, the section is removed instead */\n static int repo_config_copy_or_rename_section_in_file(\n \tstruct repository *r,\n@@ -3434,7 +3525,7 @@ static int repo_config_copy_or_rename_section_in_file(\n \tconst char *old_name,\n \tconst char *new_name, int copy)\n {\n-\tint ret = 0, remove = 0;\n+\tint ret = 0, remove = 0, continued = 0, in_quote = 0;\n \tchar *filename_buf = NULL;\n \tstruct lock_file lock = LOCK_INIT;\n \tint out_fd;\n@@ -3496,9 +3587,12 @@ static int repo_config_copy_or_rename_section_in_file(\n \t\t\tgoto out;\n \t\t}\n \n+\t\tint was_continued = continued;\n+\n \t\tfor (i = 0; buf.buf[i] && isspace(buf.buf[i]); i++)\n \t\t\t; /* do nothing */\n-\t\tif (buf.buf[i] == '[') {\n+\t\tupdate_line_continuation(buf.buf, &continued, &in_quote);\n+\t\tif (!was_continued && buf.buf[i] == '[') {\n \t\t\t/* it's a section */\n \t\t\tsize_t offset;\n \t\t\tis_section = 1;\n@@ -3550,8 +3644,9 @@ static int repo_config_copy_or_rename_section_in_file(\n \t\t\t\t\tstrbuf_release(&copystr);\n \t\t\t\t\tcopystr = store_create_section(new_name, &store);\n \t\t\t\t}\n+\t\t\t} else {\n+\t\t\t\tremove = 0;\n \t\t\t}\n-\t\t\tremove = 0;\n \t\t}\n \t\tif (remove)\n \t\t\tcontinue;\ndiff --git a/t/t1300-config.sh b/t/t1300-config.sh\nindex 876d376cc7..3b323f95f8 100755\n--- a/t/t1300-config.sh\n+++ b/t/t1300-config.sh\n@@ -3049,4 +3049,37 @@ test_expect_success POSIXPERM 'refuse to \"overwrite\" a read-only file' '\n \t)\n '\n \n+test_expect_success 'remove-section and rename-section ignore bracketed continuation lines' '\n+\ttest_when_finished \"rm -f multiline.cfg rename.cfg subbracket.cfg\" &&\n+\tcat >multiline.cfg <<-\\EOF &&\n+\t[old]\n+\t\tkey = \"first \\\n+\t[smuggled] \\\n+\t# quoted hash \\\n+\t\tevil = yes\"\n+\t[keep]\n+\t\tok = true\n+\tEOF\n+\tcp multiline.cfg rename.cfg &&\n+\n+\tgit config --file=multiline.cfg --remove-section old &&\n+\ttest_must_fail git config --file=multiline.cfg --get smuggled.evil &&\n+\ttest \"$(git config --file=multiline.cfg --get keep.ok)\" = true &&\n+\n+\tgit config --file=rename.cfg --rename-section old renamed &&\n+\ttest_must_fail git config --file=rename.cfg --get smuggled.evil &&\n+\tgit config --file=rename.cfg --get renamed.key >actual &&\n+\tgrep \"\\[smuggled\\]\" actual &&\n+\ttest \"$(git config --file=rename.cfg --get keep.ok)\" = true &&\n+\n+\tcat >subbracket.cfg <<-\\EOF &&\n+\t[keep \"a] = \\\"\"] k = v ; \\\n+\t[old]\n+\t\tsecret = should_be_removed\n+\tEOF\n+\tgit config --file=subbracket.cfg --remove-section old &&\n+\ttest_must_fail git config --file=subbracket.cfg --get old.secret &&\n+\ttest \"$(git config --file=subbracket.cfg --get \"keep.a] = \\\".k\")\" = v\n+'\n+\n test_done\n-- \n2.56.1.750.gf70dc682cf-goog\n\n\n"},{"id":"558904","messageId":"xmqqbj7ktjp7.fsf@gitster.g","threadId":"66887","inReplyTo":"20261119192307.2683812-1-dmnk@google.com","subject":"Re: [PATCH] config: skip continued lines when matching section","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-20T00:44:52Z","receivedAt":"2026-11-20T00:44:52Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Dominik Maier <dmnk@google.com> writes:\n\n> When a value in the target section spans multiple lines via a trailing\n> backslash and a continuation line begins with '[other]', removing or\n> renaming the section stops early at '[other]' and leaves the remainder\n> of the continued value behind as an unquoted '[other]' section.\n\nIf the behaviour was to treat these as continued values from day\none, I would have agreed that it would be a reasonably design, but\noffhand I do not know if this is a breaking change in behaviour that\nmay negatively affect existing configuration files.\n\n> @@ -3434,7 +3525,7 @@ static int repo_config_copy_or_rename_section_in_file(\n>  \tconst char *old_name,\n>  \tconst char *new_name, int copy)\n>  {\n> -\tint ret = 0, remove = 0;\n> +\tint ret = 0, remove = 0, continued = 0, in_quote = 0;\n>  \tchar *filename_buf = NULL;\n>  \tstruct lock_file lock = LOCK_INIT;\n>  \tint out_fd;\n> @@ -3496,9 +3587,12 @@ static int repo_config_copy_or_rename_section_in_file(\n>  \t\t\tgoto out;\n>  \t\t}\n>  \n> +\t\tint was_continued = continued;\n> +\n\nThis triggers -Wdeclaration-after-statement doesn't it?\n\n(administrivia)\n\nI do not consider myself an area expert for the configuration file\nediting code.  Please do not directly Cc: random people unless you\nknow they are specifically familiar with the area or they are\npositively interested in the topic and they will regret if they miss\nyour message if they fail to notice.\n\nThanks.\n\n"}]}