{"thread":{"id":"66540","subject":"[PATCH] pretty: fix lost indent after newline in %w()","startedAt":"2026-10-14T17:23:33Z","lastAt":"2026-10-17T08:26:07Z","messageCount":6,"participants":["Andy Koppe","René Scharfe","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"555052","messageId":"20261014172333.2224123-1-andy.koppe@gmail.com","threadId":"66540","inReplyTo":null,"subject":"[PATCH] pretty: fix lost indent after newline in %w()","fromName":"Andy Koppe","fromEmail":"andy.koppe@gmail.com","sentAt":"2026-10-14T17:23:33Z","receivedAt":"2026-10-14T17:23:33Z","isPatch":true,"sender":{"key":"andy.koppe@gmail.com","avatar":"https://avatars.githubusercontent.com/u/223411?v=4"},"body":"When strbuf_add_wrapped_text() meets a newline followed by a\nnon-alphanumeric character, it treats it as a hard line break and jumps\nto the new_line label. That code was written for the case where a line\ngets too long. It moves past the space where the line was broken by\nsetting the next line's start to 'space + isspace(*space)'.\n\nAfter a hard line break, 'space' already points to the first character\nof the next line. If that character is whitespace, it was wrongly\nskipped, so the line lost one column of indentation. For example,\n\n  git log --format=\"%w(11) This is%n  the %s%n  commit.\"\n\nprinted \" the ...\" and \" commit.\" rather than \"  the ...\" and\n\"  commit.\".\n\nFix this by skipping the space only in the line-too-long case. In the\ndouble-newline case, skip the second newline explicitly, because that\ncase relied on the old skipping.\n\nAdd a test for this, and update the expected output of the \"magical\nwrapping directives\" test in t4205. There, %+d emits a newline followed\nby \" (tag: ...)\", and that leading space is no longer lost. With a wrap\nwidth of 1, the space now causes an extra line break.\n\nSigned-off-by: Andy Koppe <andy.koppe@gmail.com>\n---\n t/t4202-log.sh                | 6 ++++++\n t/t4205-log-pretty-formats.sh | 2 +-\n utf8.c                        | 4 +++-\n 3 files changed, 10 insertions(+), 2 deletions(-)\n\ndiff --git a/t/t4202-log.sh b/t/t4202-log.sh\nindex 06552a6271..5895780e8f 100755\n--- a/t/t4202-log.sh\n+++ b/t/t4202-log.sh\n@@ -94,6 +94,12 @@ test_expect_success 'format %w(,1,2)' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'format %w(11)' '\n+\n+\tgit log -2 --format=\"%w(11) This is%n  the %s%n  commit.\" > actual &&\n+\ttest_cmp expect actual\n+'\n+\n cat > expect << EOF\n $(git rev-parse --short :/sixth  ) sixth\n $(git rev-parse --short :/fifth  ) fifth\ndiff --git a/t/t4205-log-pretty-formats.sh b/t/t4205-log-pretty-formats.sh\nindex 4be5c51489..0733f22d58 100755\n--- a/t/t4205-log-pretty-formats.sh\n+++ b/t/t4205-log-pretty-formats.sh\n@@ -1139,7 +1139,7 @@ test_expect_success 'log --pretty with invalid padding format' '\n test_expect_success 'log --pretty with magical wrapping directives' '\n \tcommit_id=$(git commit-tree HEAD^{tree} -m \"describe me\") &&\n \tgit tag describe-me $commit_id &&\n-\tprintf \"\\n(tag:\\ndescribe-me)%%+w(2)\" >expect &&\n+\tprintf \"\\n\\n(tag:\\ndescribe-me)%%+w(2)\" >expect &&\n \tgit log -1 --pretty=\"format:%w(1)%+d%+w(2)\" $commit_id >actual &&\n \ttest_cmp expect actual\n '\ndiff --git a/utf8.c b/utf8.c\nindex 96460cc414..bb55bf357f 100644\n--- a/utf8.c\n+++ b/utf8.c\n@@ -322,6 +322,7 @@ void strbuf_add_wrapped_text(struct strbuf *buf,\n \t\t\t\t\tspace++;\n \t\t\t\t\tif (*space == '\\n') {\n \t\t\t\t\t\tstrbuf_addch(buf, '\\n');\n+\t\t\t\t\t\tspace++;\n \t\t\t\t\t\tgoto new_line;\n \t\t\t\t\t}\n \t\t\t\t\telse if (!isalnum(*space))\n@@ -333,9 +334,10 @@ void strbuf_add_wrapped_text(struct strbuf *buf,\n \t\t\t\ttext++;\n \t\t\t}\n \t\t\telse {\n+\t\t\t\tspace += isspace(*space);\n new_line:\n \t\t\t\tstrbuf_addch(buf, '\\n');\n-\t\t\t\ttext = bol = space + isspace(*space);\n+\t\t\t\ttext = bol = space;\n \t\t\t\tspace = NULL;\n \t\t\t\tw = indent = indent2;\n \t\t\t}\n-- \n2.56.0\n\n\n"},{"id":"555191","messageId":"CAHWeT-ZuZ8ScKpdiV94UjvGNJmsv8vwy41Hm8OSCSETciFRh8g@mail.gmail.com","threadId":"66540","inReplyTo":"20261014172333.2224123-1-andy.koppe@gmail.com","subject":"Re: [PATCH] pretty: fix lost indent after newline in %w()","fromName":"Andy Koppe","fromEmail":"andy.koppe@gmail.com","sentAt":"2026-10-15T20:11:24Z","receivedAt":"2026-10-15T20:11:24Z","isPatch":true,"sender":{"key":"andy.koppe@gmail.com","avatar":"https://avatars.githubusercontent.com/u/223411?v=4"},"body":"Andy Koppe wrote:\n> After a hard line break, 'space' already points to the first character\n> of the next line. If that character is whitespace, it was wrongly\n> skipped, so the line lost one column of indentation. For example,\n>\n>   git log --format=\"%w(11) This is%n  the %s%n  commit.\"\n>\n> printed \" the ...\" and \" commit.\" rather than \"  the ...\" and\n> \"  commit.\".\n\nAdmittedly, that's a bit of a silly example and test, just to keep it\nsmall. My actual use case is \"%w(72)%B\", because I often have to deal\nwith repos that don't stick to line length standards, which can make a\nvisual mess with --graph or with wide terminal windows.\n\nAs a better example, lists of files and their changes like this ...\n\n- foo.c\n  Frobble.\n- bar.c\n  Bobble.\n\n... lose a space of indentation, thus breaking alignment:\n\n- foo.c\n Frobble.\n- bar.c\n Bobble.\n\n"},{"id":"555192","messageId":"fc0dabfb-3dda-4b12-a1fb-184d1e553623@web.de","threadId":"66540","inReplyTo":"20261014172333.2224123-1-andy.koppe@gmail.com","subject":"Re: [PATCH] pretty: fix lost indent after newline in %w()","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2026-10-15T20:42:28Z","receivedAt":"2026-10-15T20:42:28Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"On 10/14/26 7:23 PM, Andy Koppe wrote:\n> When strbuf_add_wrapped_text() meets a newline followed by a\n> non-alphanumeric character, it treats it as a hard line break and jumps\n> to the new_line label. \n\nI assume you're referring to ae0b270230 (print_wrapped_text(): allow\nhard newlines, 2007-11-11), which started to replace single newlines\npreceding alphanumeric characters with a space, and to keep double\nnewlines and calling them hard.\n\nE.g. the following format pairs are equivalent:\n\n   - hard newline:                    \"%w(9)%n%nA\" == \"%n%nA\"\n   - replace newline before alpha:    \"%w(9)%nA\"   == \" A\"\n   - keep newline before punctuation: \"%w(9)%n-A\"  == \"%n-A\"\n\n> That code was written for the case where a line\n> gets too long. It moves past the space where the line was broken by\n> setting the next line's start to 'space + isspace(*space)'.\n> \n> After a hard line break, 'space' already points to the first character\n> of the next line. If that character is whitespace, it was wrongly\n> skipped, so the line lost one column of indentation. For example,\n> \n>   git log --format=\"%w(11) This is%n  the %s%n  commit.\"\n> \n> printed \" the ...\" and \" commit.\" rather than \"  the ...\" and\n> \"  commit.\".\n\nSo currently a newline before whitespace is kept, but that whitespace\ngets eaten, and you want to keep it instead:\n\n  - now:   \"%w(9)%n A\" == \"%nA\"\n  - patch: \"%w(9)%n A\" == \"%n A\"\n\nThe commit message of ae0b270230 doesn't mention this case specifically.\nI also get the impression that this consumption of the first whitespace\ncharacter was not intended.\n\n> Fix this by skipping the space only in the line-too-long case. In the\n> double-newline case, skip the second newline explicitly, because that\n> case relied on the old skipping.\n> \n> Add a test for this, and update the expected output of the \"magical\n> wrapping directives\" test in t4205. There, %+d emits a newline followed\n> by \" (tag: ...)\", and that leading space is no longer lost. With a wrap\n> width of 1, the space now causes an extra line break.\n\nThis behavior stood for more than 18 years.  Someone might be relying\non it -- this test is one example.  And you can easily work around it\nby adding one more space to the format.\n\nOn the other hand this hard newline concept is not explained in the\ndocs as far as I can see.  That makes it fair game for change in my\nbook (within reason), as long as that doc gap is filled.  My 2ct..\n\nRené\n\n\n"},{"id":"555196","messageId":"xmqqwlrillxg.fsf@gitster.g","threadId":"66540","inReplyTo":"fc0dabfb-3dda-4b12-a1fb-184d1e553623@web.de","subject":"Re: [PATCH] pretty: fix lost indent after newline in %w()","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-15T20:59:39Z","receivedAt":"2026-10-15T20:59:39Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"René Scharfe <l.s.r@web.de> writes:\n\n> On the other hand this hard newline concept is not explained in the\n> docs as far as I can see.  That makes it fair game for change in my\n> book (within reason), as long as that doc gap is filled.  My 2ct..\n\nYeah, to me, it sounds more productive use of our time and a good\ndirection.\n\nThanks.\n\n"},{"id":"555304","messageId":"20261017081150.2295695-1-andy.koppe@gmail.com","threadId":"66540","inReplyTo":"20261014172333.2224123-1-andy.koppe@gmail.com","subject":"[PATCH v2] pretty: fix lost indent after newline in %w()","fromName":"Andy Koppe","fromEmail":"andy.koppe@gmail.com","sentAt":"2026-10-17T08:11:50Z","receivedAt":"2026-10-17T08:11:50Z","isPatch":true,"sender":{"key":"andy.koppe@gmail.com","avatar":"https://avatars.githubusercontent.com/u/223411?v=4"},"body":"When strbuf_add_wrapped_text() meets a newline followed by a\nnon-alphanumeric character, it treats it as a hard line break and jumps\nto the new_line label. That code was written for the case where a line\ngets too long. It moves past the space where the line was broken by\nsetting the next line's start to 'space + isspace(*space)'.\n\nAfter a hard line break, 'space' already points to the first character\nof the next line. If that character is whitespace, it was wrongly\nskipped, so the line lost one column of indentation. This affected\nthings like code examples or multi-line list items starting with \"- \"\nand continuing with two spaces of indentation.\n\nFix this by skipping the space only in the line-too-long case. In the\ndouble-newline case, skip the second newline explicitly, because that\ncase relied on the old skipping.\n\nAdd a test for this, and update the expected output of the \"magical\nwrapping directives\" test in t4205. There, %+d emits a newline followed\nby \" (tag: ...)\", and that leading space is no longer lost. With a wrap\nwidth of 1, the space now causes an extra line break.\n\nDocument the %w placeholder properly, removing the reference to `git\nshortlog -w`, which has different defaults and isn't concerned with\nparagraph reflowing.\n\nSigned-off-by: Andy Koppe <andy.koppe@gmail.com>\n---\n Documentation/pretty-formats.adoc | 9 +++++++--\n t/t4202-log.sh                    | 6 ++++++\n t/t4205-log-pretty-formats.sh     | 2 +-\n utf8.c                            | 4 +++-\n 4 files changed, 17 insertions(+), 4 deletions(-)\n\ndiff --git a/Documentation/pretty-formats.adoc b/Documentation/pretty-formats.adoc\nindex 2ae0eb11a9..4fb0419715 100644\n--- a/Documentation/pretty-formats.adoc\n+++ b/Documentation/pretty-formats.adoc\n@@ -169,8 +169,13 @@ The placeholders are:\n \t    on the next placeholders until the color is switched\n \t    again.\n ++%m++:: left (`<`), right (`>`) or boundary (`-`) mark\n-++%w(++`[<w>[,<i1>[,<i2>]]]`++)++:: switch line wrapping, like the `-w` option of\n-\t\t\t    linkgit:git-shortlog[1].\n+++%w(++`[<w>[,<i1>[,<i2>]]]`++)++:: set line wrapping and indentation.\n+\t    Output is word-wrapped at _<w>_ columns, with the first line\n+\t    indented by _<i1>_ spaces and subsequent lines indented by\n+\t    _<i2>_ spaces. A width of 0 disables wrapping. Omitted values\n+\t    default to 0, so ++%w()++ turns off both wrapping and indentation.\n+\t    Newlines that are followed by an ASCII letter or digit are replaced\n+\t    by spaces to reflow paragraphs. Existing indentation is kept.\n ++%<(++`<n>[,(trunc|ltrunc|mtrunc)]`++)++:: make the next placeholder take at\n \t\t\t\t  least N column widths, padding spaces on\n \t\t\t\t  the right if necessary.  Optionally\ndiff --git a/t/t4202-log.sh b/t/t4202-log.sh\nindex 06552a6271..5895780e8f 100755\n--- a/t/t4202-log.sh\n+++ b/t/t4202-log.sh\n@@ -94,6 +94,12 @@ test_expect_success 'format %w(,1,2)' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'format %w(11)' '\n+\n+\tgit log -2 --format=\"%w(11) This is%n  the %s%n  commit.\" > actual &&\n+\ttest_cmp expect actual\n+'\n+\n cat > expect << EOF\n $(git rev-parse --short :/sixth  ) sixth\n $(git rev-parse --short :/fifth  ) fifth\ndiff --git a/t/t4205-log-pretty-formats.sh b/t/t4205-log-pretty-formats.sh\nindex 4be5c51489..0733f22d58 100755\n--- a/t/t4205-log-pretty-formats.sh\n+++ b/t/t4205-log-pretty-formats.sh\n@@ -1139,7 +1139,7 @@ test_expect_success 'log --pretty with invalid padding format' '\n test_expect_success 'log --pretty with magical wrapping directives' '\n \tcommit_id=$(git commit-tree HEAD^{tree} -m \"describe me\") &&\n \tgit tag describe-me $commit_id &&\n-\tprintf \"\\n(tag:\\ndescribe-me)%%+w(2)\" >expect &&\n+\tprintf \"\\n\\n(tag:\\ndescribe-me)%%+w(2)\" >expect &&\n \tgit log -1 --pretty=\"format:%w(1)%+d%+w(2)\" $commit_id >actual &&\n \ttest_cmp expect actual\n '\ndiff --git a/utf8.c b/utf8.c\nindex 96460cc414..bb55bf357f 100644\n--- a/utf8.c\n+++ b/utf8.c\n@@ -322,6 +322,7 @@ void strbuf_add_wrapped_text(struct strbuf *buf,\n \t\t\t\t\tspace++;\n \t\t\t\t\tif (*space == '\\n') {\n \t\t\t\t\t\tstrbuf_addch(buf, '\\n');\n+\t\t\t\t\t\tspace++;\n \t\t\t\t\t\tgoto new_line;\n \t\t\t\t\t}\n \t\t\t\t\telse if (!isalnum(*space))\n@@ -333,9 +334,10 @@ void strbuf_add_wrapped_text(struct strbuf *buf,\n \t\t\t\ttext++;\n \t\t\t}\n \t\t\telse {\n+\t\t\t\tspace += isspace(*space);\n new_line:\n \t\t\t\tstrbuf_addch(buf, '\\n');\n-\t\t\t\ttext = bol = space + isspace(*space);\n+\t\t\t\ttext = bol = space;\n \t\t\t\tspace = NULL;\n \t\t\t\tw = indent = indent2;\n \t\t\t}\n-- \n2.56.0\n\n\n"},{"id":"555306","messageId":"CAHWeT-Z1C5V8jud=_jK6QByt6DxoFLFk0LYcgbjnGhunzA7-Lg@mail.gmail.com","threadId":"66540","inReplyTo":"fc0dabfb-3dda-4b12-a1fb-184d1e553623@web.de","subject":"Re: [PATCH] pretty: fix lost indent after newline in %w()","fromName":"Andy Koppe","fromEmail":"andy.koppe@gmail.com","sentAt":"2026-10-17T08:26:07Z","receivedAt":"2026-10-17T08:26:07Z","isPatch":true,"sender":{"key":"andy.koppe@gmail.com","avatar":"https://avatars.githubusercontent.com/u/223411?v=4"},"body":"On Thu, 15 Oct 2026 at 21:42, René Scharfe <l.s.r@web.de> wrote:\n> On 10/14/26 7:23 PM, Andy Koppe wrote:\n> > When strbuf_add_wrapped_text() meets a newline followed by a\n> > non-alphanumeric character, it treats it as a hard line break and jumps\n> > to the new_line label.\n>\n> I assume you're referring to ae0b270230 (print_wrapped_text(): allow\n> hard newlines, 2007-11-11), which started to replace single newlines\n> preceding alphanumeric characters with a space, and to keep double\n> newlines and calling them hard.\n>\n> E.g. the following format pairs are equivalent:\n>\n>    - hard newline:                    \"%w(9)%n%nA\" == \"%n%nA\"\n>    - replace newline before alpha:    \"%w(9)%nA\"   == \" A\"\n>    - keep newline before punctuation: \"%w(9)%n-A\"  == \"%n-A\"\n>\n> > That code was written for the case where a line\n> > gets too long. It moves past the space where the line was broken by\n> > setting the next line's start to 'space + isspace(*space)'.\n> >\n> > After a hard line break, 'space' already points to the first character\n> > of the next line. If that character is whitespace, it was wrongly\n> > skipped, so the line lost one column of indentation. For example,\n> >\n> >   git log --format=\"%w(11) This is%n  the %s%n  commit.\"\n> >\n> > printed \" the ...\" and \" commit.\" rather than \"  the ...\" and\n> > \"  commit.\".\n>\n> So currently a newline before whitespace is kept, but that whitespace\n> gets eaten, and you want to keep it instead:\n>\n>   - now:   \"%w(9)%n A\" == \"%nA\"\n>   - patch: \"%w(9)%n A\" == \"%n A\"\n\nCorrect.\n\n> The commit message of ae0b270230 doesn't mention this case specifically.\n> I also get the impression that this consumption of the first whitespace\n> character was not intended.\n\nI agree.\n\n> > Fix this by skipping the space only in the line-too-long case. In the\n> > double-newline case, skip the second newline explicitly, because that\n> > case relied on the old skipping.\n> >\n> > Add a test for this, and update the expected output of the \"magical\n> > wrapping directives\" test in t4205. There, %+d emits a newline followed\n> > by \" (tag: ...)\", and that leading space is no longer lost. With a wrap\n> > width of 1, the space now causes an extra line break.\n>\n> This behavior stood for more than 18 years.  Someone might be relying\n> on it -- this test is one example.  And you can easily work around it\n> by adding one more space to the format.\n\nTrue, but that isn't the actual use case. In v2 I've amended the commit message:\n\"This affected things like code examples or multi-line list items\nstarting with \"- \"\nand continuing with two spaces of indentation.\"\n\n> On the other hand this hard newline concept is not explained in the\n> docs as far as I can see.  That makes it fair game for change in my\n> book (within reason), as long as that doc gap is filled.  My 2ct..\n\nVery good point. I've added dedicated documentation for %w in v2.\n\nBtw, the detection of paragraph continuations by looking for an\nalphanumeric ASCII character at the start of the next line could be\nimproved by looking for quotes and opening parentheses too and by\nsupporting Unicode. But I haven't attempted that here.\n\nAndy\n\n"}]}