strbuf_add_commented_lines(): avoid SP-HT sequence in commented lines

The strbuf_add_commented_lines() function passes a pair of prefixes, one to be used for a non-empty line, and the other for an empty line, to underlying add_lines(). The former is set to a comment char followed by a SP, while the latter is set to just the comment char. This is designed to give a SP after the comment character, e.g. "# <user text>\n", on a line with some text, and to avoid emitting an unsightly "# \n" for an empty line. Teach this machinery to also use the latter space-less prefix when the payload line begins with a tab, to show e.g. "#\t<user text>\n"; otherwise we will end up showing "# \t<user text>\n" which is similarly unsightly. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Oct 27, 2014 at 14:13 UTC d55aeb7687448189327ad058096b55431da5ea42
2 files changed +8 -1
strbuf.c
+2 -1
@@ -222,7 +222,8 @@ static void add_lines(struct strbuf *out,
222 const char *next = memchr(buf, '\n', size);
223 next = next ? (next + 1) : (buf + size);
224
225 - prefix = (prefix2 && buf[0] == '\n') ? prefix2 : prefix1;
225 + prefix = ((prefix2 && (buf[0] == '\n' || buf[0] == '\t'))
226 + ? prefix2 : prefix1);
227 strbuf_addstr(out, prefix);
228 strbuf_add(out, buf, next - buf);
229 size -= next - buf;
t/t0030-stripspace.sh
+6
@@ -432,4 +432,10 @@ test_expect_success '-c with changed comment char' '
432 test_cmp expect actual
433 '
434
435 +test_expect_success 'avoid SP-HT sequence in commented line' '
436 + printf "#\tone\n#\n# two\n" >expect &&
437 + printf "\tone\n\ntwo\n" | git stripspace -c >actual &&
438 + test_cmp expect actual
439 +'
440 +
441 test_done