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 <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Nov 22, 2009 at 17:15 UTC
37bb5d744309df6ff05ea5f34112082cdc301d69
2 files changed
+38
-9
t/t4202-log.sh
+21
@@ -63,6 +63,27 @@ test_expect_success 'format' '
63
test_cmp expect actual
64
'
65
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
+
87
cat > expect << EOF
88
804a787 sixth
89
394ef78 fifth
utf8.c
+17
-9
@@ -298,6 +298,22 @@ static void print_spaces(struct strbuf *buf, int count)
298
strbuf_write(buf, s, count);
299
}
300
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
+
317
/*
318
* Wrap the text, if necessary. The variable indent is the indent for the
319
* first line, indent2 is the indent for all other lines.
@@ -311,15 +327,7 @@ int strbuf_add_wrapped_text(struct strbuf *buf,
327
const char *bol = text, *space = NULL;
328
329
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);
331
return 1;
332
}
333