Skip to content

Commit d35711a

Browse files
j6tgitster
authored andcommitted
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>
1 parent a347b17 commit d35711a

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

t/t4124-apply-ws-rule.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,34 @@ test_expect_success 'whitespace=error-all, no rule (attribute)' '
121121
122122
'
123123

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+
124152
for t in - ''
125153
do
126154
case "$t" in '') tt='!' ;; *) tt= ;; esac

ws.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,12 +362,13 @@ void ws_fix_copy(struct strbuf *dst, const char *src, int len, unsigned ws_rule,
362362
fixed = 1;
363363
} else if ((ws_rule & WS_TAB_IN_INDENT) && last_tab_in_indent >= 0) {
364364
/* Expand tabs into spaces */
365+
int start = dst->len;
365366
int last = last_tab_in_indent + 1;
366367
for (i = 0; i < last; i++) {
367368
if (src[i] == '\t')
368369
do {
369370
strbuf_addch(dst, ' ');
370-
} while (dst->len % 8);
371+
} while ((dst->len - start) % 8);
371372
else
372373
strbuf_addch(dst, src[i]);
373374
}

0 commit comments

Comments
 (0)