pretty: fix lost indent after newline in %w()

When strbuf_add_wrapped_text() meets a newline followed by a
non-alphanumeric character, it treats it as a hard line break and jumps
to the new_line label. That code was written for the case where a line
gets too long. It moves past the space where the line was broken by
setting the next line's start to 'space + isspace(*space)'.

After a hard line break, 'space' already points to the first character
of the next line. If that character is whitespace, it was wrongly
skipped, so the line lost one column of indentation. This affected
things like code examples or multi-line list items starting with "- "
and continuing with two spaces of indentation.

Fix this by skipping the space only in the line-too-long case. In the
double-newline case, skip the second newline explicitly, because that
case relied on the old skipping.

Add a test for this, and update the expected output of the "magical
wrapping directives" test in t4205. There, %+d emits a newline followed
by " (tag: ...)", and that leading space is no longer lost. With a wrap
width of 1, the space now causes an extra line break.

Document the %w placeholder properly, removing the reference to `git
shortlog -w`, which has different defaults and isn't concerned with
paragraph reflowing.

Signed-off-by: Andy Koppe <andy.koppe@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Andy Koppe authored and Junio C Hamano committed 2026-10-17 10:01:14 -07:00
1 parent a018953688
commit dc9e74fbab
4 files changed
+17 -4

No files matched your search

+7 -2
View File
@@ -169,8 +169,13 @@ The placeholders are:
on the next placeholders until the color is switched
again.
++%m++:: left (`<`), right (`>`) or boundary (`-`) mark
++%w(++`[<w>[,<i1>[,<i2>]]]`++)++:: switch line wrapping, like the `-w` option of
linkgit:git-shortlog[1].
++%w(++`[<w>[,<i1>[,<i2>]]]`++)++:: set line wrapping and indentation.
Output is word-wrapped at _<w>_ columns, with the first line
indented by _<i1>_ spaces and subsequent lines indented by
_<i2>_ spaces. A width of 0 disables wrapping. Omitted values
default to 0, so ++%w()++ turns off both wrapping and indentation.
Newlines that are followed by an ASCII letter or digit are replaced
by spaces to reflow paragraphs. Existing indentation is kept.
++%<(++`<n>[,(trunc|ltrunc|mtrunc)]`++)++:: make the next placeholder take at
least N column widths, padding spaces on
the right if necessary. Optionally
+6
View File
@@ -94,6 +94,12 @@ test_expect_success 'format %w(,1,2)' '
test_cmp expect actual
'
test_expect_success 'format %w(11)' '
git log -2 --format="%w(11) This is%n the %s%n commit." > actual &&
test_cmp expect actual
'
cat > expect << EOF
$(git rev-parse --short :/sixth ) sixth
$(git rev-parse --short :/fifth ) fifth
+1 -1
View File
@@ -1139,7 +1139,7 @@ test_expect_success 'log --pretty with invalid padding format' '
test_expect_success 'log --pretty with magical wrapping directives' '
commit_id=$(git commit-tree HEAD^{tree} -m "describe me") &&
git tag describe-me $commit_id &&
printf "\n(tag:\ndescribe-me)%%+w(2)" >expect &&
printf "\n\n(tag:\ndescribe-me)%%+w(2)" >expect &&
git log -1 --pretty="format:%w(1)%+d%+w(2)" $commit_id >actual &&
test_cmp expect actual
'
+3 -1
View File
@@ -322,6 +322,7 @@ retry:
space++;
if (*space == '\n') {
strbuf_addch(buf, '\n');
space++;
goto new_line;
}
else if (!isalnum(*space))
@@ -333,9 +334,10 @@ retry:
text++;
}
else {
space += isspace(*space);
new_line:
strbuf_addch(buf, '\n');
text = bol = space + isspace(*space);
text = bol = space;
space = NULL;
w = indent = indent2;
}