@matheusbsbs / matheus-bagliano / commits / d35711adc4
Fork 0

apply --whitespace=fix: fix tab-in-indent

When the whitespace rule tab-in-indent is enabled, apply --whitespace=fix replaces tabs by the appropriate amount of blanks. The code used "dst->len % 8" as the criterion to stop adding blanks. But it forgot that dst holds more than just the current line. Consequently, the modulus was computed correctly only for the first added line, but not for the second and subsequent lines. Fix it. Signed-off-by: Johannes Sixt <j6t@kdbg.org> Acked-by: Chris Webb <chris@arachsys.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Sixt committed Nov 30, 2010 at 09:22 UTC d35711adc4cffe0c4aebd85c197080c7163bcb77
2 files changed +30 -1
t/t4124-apply-ws-rule.sh
+28
@@ -121,6 +121,34 @@ test_expect_success 'whitespace=error-all, no rule (attribute)' '
121
122 '
123
124 +test_expect_success 'spaces inserted by tab-in-indent' '
125 +
126 + git config core.whitespace -trailing,-space,-indent,tab &&
127 + rm -f .gitattributes &&
128 + test_fix % &&
129 + sed -e "s/_/ /g" -e "s/>/ /" <<-\EOF >expect &&
130 + An_SP in an ordinary line>and a HT.
131 + ________A HT (%).
132 + ________A SP and a HT (@%).
133 + _________A SP, a HT and a SP (@%).
134 + _______Seven SP.
135 + ________Eight SP (#).
136 + ________Seven SP and a HT (@%).
137 + ________________Eight SP and a HT (@#%).
138 + _________Seven SP, a HT and a SP (@%).
139 + _________________Eight SP, a HT and a SP (@#%).
140 + _______________Fifteen SP (#).
141 + ________________Fifteen SP and a HT (@#%).
142 + ________________Sixteen SP (#).
143 + ________________________Sixteen SP and a HT (@#%).
144 + _____a__Five SP, a non WS, two SP.
145 + A line with a (!) trailing SP_
146 + A line with a (!) trailing HT>
147 + EOF
148 + test_cmp expect target
149 +
150 +'
151 +
152 for t in - ''
153 do
154 case "$t" in '') tt='!' ;; *) tt= ;; esac
ws.c
+2 -1
@@ -362,12 +362,13 @@ void ws_fix_copy(struct strbuf *dst, const char *src, int len, unsigned ws_rule,
362 fixed = 1;
363 } else if ((ws_rule & WS_TAB_IN_INDENT) && last_tab_in_indent >= 0) {
364 /* Expand tabs into spaces */
365 + int start = dst->len;
366 int last = last_tab_in_indent + 1;
367 for (i = 0; i < last; i++) {
368 if (src[i] == '\t')
369 do {
370 strbuf_addch(dst, ' ');
370 - } while (dst->len % 8);
371 + } while ((dst->len - start) % 8);
372 else
373 strbuf_addch(dst, src[i]);
374 }