strbuf_add_commented_lines(): avoid SP-HT sequence in commented lines
authorJunio C Hamano <gitster@pobox.com>
Mon, 27 Oct 2014 21:13:15 +0000 (14:13 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 27 Oct 2014 21:13:59 +0000 (14:13 -0700)
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>
strbuf.c
t/t0030-stripspace.sh
index 33018d847f08424211c02cff8002680f5a2f02e9..a4486a9e499d74bda3b3db10ef42d4975f7f7cbf 100644 (file)
--- a/strbuf.c
+++ b/strbuf.c
@@ -222,7 +222,8 @@ static void add_lines(struct strbuf *out,
                const char *next = memchr(buf, '\n', size);
                next = next ? (next + 1) : (buf + size);
 
-               prefix = (prefix2 && buf[0] == '\n') ? prefix2 : prefix1;
+               prefix = ((prefix2 && (buf[0] == '\n' || buf[0] == '\t'))
+                         ? prefix2 : prefix1);
                strbuf_addstr(out, prefix);
                strbuf_add(out, buf, next - buf);
                size -= next - buf;
index 0333dd9875af9fbbad04cf05eb9f0e9fe37ae182..29e91d861cbd6473d80c7343a0e292b010d83173 100755 (executable)
@@ -432,4 +432,10 @@ test_expect_success '-c with changed comment char' '
        test_cmp expect actual
 '
 
+test_expect_success 'avoid SP-HT sequence in commented line' '
+       printf "#\tone\n#\n# two\n" >expect &&
+       printf "\tone\n\ntwo\n" | git stripspace -c >actual &&
+       test_cmp expect actual
+'
+
 test_done