Subject: [PATCH 1/2 maint] apply --whitespace=fix: fix tab-in-indent

From: Johannes Sixt <[email-masked]>
Date: Tue, 30 Nov 2010 09:22:04 +0100

From: Johannes Sixt <[email-masked]>

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 <[email-masked]>
---
 This can go on top of a347b17f or its ancestor.

 t/t4124-apply-ws-rule.sh |   28 ++++++++++++++++++++++++++++
 ws.c                     |    3 ++-
 2 files changed, 30 insertions(+), 1 deletions(-)

diff --git a/t/t4124-apply-ws-rule.sh b/t/t4124-apply-ws-rule.sh
index 61bfc56..40e5842 100755
--- a/t/t4124-apply-ws-rule.sh
+++ b/t/t4124-apply-ws-rule.sh
@@ -121,6 +121,34 @@ test_expect_success 'whitespace=error-all, no rule (attribute)' '
 
 '
 
+test_expect_success 'spaces inserted by tab-in-indent' '
+
+	git config core.whitespace -trailing,-space,-indent,tab &&
+	rm -f .gitattributes &&
+	test_fix % &&
+	sed -e "s/_/ /g" -e "s/>/	/" <<-\EOF >expect &&
+		An_SP in an ordinary line>and a HT.
+		________A HT (%).
+		________A SP and a HT (@%).
+		_________A SP, a HT and a SP (@%).
+		_______Seven SP.
+		________Eight SP (#).
+		________Seven SP and a HT (@%).
+		________________Eight SP and a HT (@#%).
+		_________Seven SP, a HT and a SP (@%).
+		_________________Eight SP, a HT and a SP (@#%).
+		_______________Fifteen SP (#).
+		________________Fifteen SP and a HT (@#%).
+		________________Sixteen SP (#).
+		________________________Sixteen SP and a HT (@#%).
+		_____a__Five SP, a non WS, two SP.
+		A line with a (!) trailing SP_
+		A line with a (!) trailing HT>
+	EOF
+	test_cmp expect target
+
+'
+
 for t in - ''
 do
 	case "$t" in '') tt='!' ;; *) tt= ;; esac
diff --git a/ws.c b/ws.c
index 7302f8f..e350436 100644
--- a/ws.c
+++ b/ws.c
@@ -363,12 +363,13 @@ void ws_fix_copy(struct strbuf *dst, const char *src, int len, unsigned ws_rule,
 		fixed = 1;
 	} else if ((ws_rule & WS_TAB_IN_INDENT) && last_tab_in_indent >= 0) {
 		/* Expand tabs into spaces */
+		int start = dst->len;
 		int last = last_tab_in_indent + 1;
 		for (i = 0; i < last; i++) {
 			if (src[i] == '\t')
 				do {
 					strbuf_addch(dst, ' ');
-				} while (dst->len % 8);
+				} while ((dst->len - start) % 8);
 			else
 				strbuf_addch(dst, src[i]);
 		}
-- 
1.7.3.67.gcc234