Skip to content

Commit 37bb5d7

Browse files
René Scharfegitster
authored andcommitted
strbuf_add_wrapped_text(): factor out strbuf_add_indented_text()
Add a new helper function, strbuf_add_indented_text(), to indent text without a width limit, and call it from strbuf_add_wrapped_text(). It respects both indent (applied to the first line) and indent2 (applied to the rest of the lines); indent2 was ignored by the indent-only path of strbuf_add_wrapped_text() before the patch. Two simple test cases are added, one exercising strbuf_add_wrapped_text() and the other strbuf_add_indented_text(). Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b482759 commit 37bb5d7

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

t/t4202-log.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,27 @@ test_expect_success 'format' '
6363
test_cmp expect actual
6464
'
6565

66+
cat > expect << EOF
67+
This is
68+
the sixth
69+
commit.
70+
This is
71+
the fifth
72+
commit.
73+
EOF
74+
75+
test_expect_success 'format %w(12,1,2)' '
76+
77+
git log -2 --format="%w(12,1,2)This is the %s commit." > actual &&
78+
test_cmp expect actual
79+
'
80+
81+
test_expect_success 'format %w(,1,2)' '
82+
83+
git log -2 --format="%w(,1,2)This is%nthe %s%ncommit." > actual &&
84+
test_cmp expect actual
85+
'
86+
6687
cat > expect << EOF
6788
804a787 sixth
6889
394ef78 fifth

utf8.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,22 @@ static void print_spaces(struct strbuf *buf, int count)
298298
strbuf_write(buf, s, count);
299299
}
300300

301+
static void strbuf_add_indented_text(struct strbuf *buf, const char *text,
302+
int indent, int indent2)
303+
{
304+
if (indent < 0)
305+
indent = 0;
306+
while (*text) {
307+
const char *eol = strchrnul(text, '\n');
308+
if (*eol == '\n')
309+
eol++;
310+
print_spaces(buf, indent);
311+
strbuf_write(buf, text, eol - text);
312+
text = eol;
313+
indent = indent2;
314+
}
315+
}
316+
301317
/*
302318
* Wrap the text, if necessary. The variable indent is the indent for the
303319
* first line, indent2 is the indent for all other lines.
@@ -311,15 +327,7 @@ int strbuf_add_wrapped_text(struct strbuf *buf,
311327
const char *bol = text, *space = NULL;
312328

313329
if (width <= 0) {
314-
/* just indent */
315-
while (*text) {
316-
const char *eol = strchrnul(text, '\n');
317-
if (*eol == '\n')
318-
eol++;
319-
print_spaces(buf, indent);
320-
strbuf_write(buf, text, eol-text);
321-
text = eol;
322-
}
330+
strbuf_add_indented_text(buf, text, indent, indent2);
323331
return 1;
324332
}
325333

0 commit comments

Comments
 (0)