Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Whitespace rules |
| 3 | * |
| 4 | * Copyright (c) 2007 Junio C Hamano |
| 5 | */ |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 6 | #include "cache.h" |
| 7 | #include "attr.h" |
| 8 | |
| 9 | static struct whitespace_rule { |
| 10 | const char *rule_name; |
| 11 | unsigned rule_bits; |
Junio C Hamano | 727c371 | 2010-04-03 00:37:00 +0100 | [diff] [blame] | 12 | unsigned loosens_error:1, |
| 13 | exclude_default:1; |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 14 | } whitespace_rule_names[] = { |
Junio C Hamano | a437900 | 2009-06-21 02:35:18 -0700 | [diff] [blame] | 15 | { "trailing-space", WS_TRAILING_SPACE, 0 }, |
| 16 | { "space-before-tab", WS_SPACE_BEFORE_TAB, 0 }, |
| 17 | { "indent-with-non-tab", WS_INDENT_WITH_NON_TAB, 0 }, |
| 18 | { "cr-at-eol", WS_CR_AT_EOL, 1 }, |
Junio C Hamano | afd9db4 | 2009-09-15 03:28:08 -0700 | [diff] [blame] | 19 | { "blank-at-eol", WS_BLANK_AT_EOL, 0 }, |
| 20 | { "blank-at-eof", WS_BLANK_AT_EOF, 0 }, |
Chris Webb | 3e3ec2a | 2010-04-03 00:37:08 +0100 | [diff] [blame] | 21 | { "tab-in-indent", WS_TAB_IN_INDENT, 0, 1 }, |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 22 | }; |
| 23 | |
| 24 | unsigned parse_whitespace_rule(const char *string) |
| 25 | { |
| 26 | unsigned rule = WS_DEFAULT_RULE; |
| 27 | |
| 28 | while (string) { |
| 29 | int i; |
| 30 | size_t len; |
| 31 | const char *ep; |
| 32 | int negated = 0; |
| 33 | |
| 34 | string = string + strspn(string, ", \t\n\r"); |
Rohit Mani | 2c5495f | 2014-03-07 22:48:31 -0800 | [diff] [blame] | 35 | ep = strchrnul(string, ','); |
| 36 | len = ep - string; |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 37 | |
| 38 | if (*string == '-') { |
| 39 | negated = 1; |
| 40 | string++; |
| 41 | len--; |
| 42 | } |
| 43 | if (!len) |
| 44 | break; |
| 45 | for (i = 0; i < ARRAY_SIZE(whitespace_rule_names); i++) { |
| 46 | if (strncmp(whitespace_rule_names[i].rule_name, |
| 47 | string, len)) |
| 48 | continue; |
| 49 | if (negated) |
| 50 | rule &= ~whitespace_rule_names[i].rule_bits; |
| 51 | else |
| 52 | rule |= whitespace_rule_names[i].rule_bits; |
| 53 | break; |
| 54 | } |
Johannes Sixt | f4b05a4 | 2010-11-30 09:29:11 +0100 | [diff] [blame] | 55 | if (strncmp(string, "tabwidth=", 9) == 0) { |
| 56 | unsigned tabwidth = atoi(string + 9); |
| 57 | if (0 < tabwidth && tabwidth < 0100) { |
| 58 | rule &= ~WS_TAB_WIDTH_MASK; |
| 59 | rule |= tabwidth; |
| 60 | } |
| 61 | else |
| 62 | warning("tabwidth %.*s out of range", |
| 63 | (int)(len - 9), string + 9); |
| 64 | } |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 65 | string = ep; |
| 66 | } |
Chris Webb | 3e3ec2a | 2010-04-03 00:37:08 +0100 | [diff] [blame] | 67 | |
| 68 | if (rule & WS_TAB_IN_INDENT && rule & WS_INDENT_WITH_NON_TAB) |
| 69 | die("cannot enforce both tab-in-indent and indent-with-non-tab"); |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 70 | return rule; |
| 71 | } |
| 72 | |
Nguyễn Thái Ngọc Duy | 26d024e | 2018-09-21 17:57:37 +0200 | [diff] [blame] | 73 | unsigned whitespace_rule(struct index_state *istate, const char *pathname) |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 74 | { |
Junio C Hamano | 2aef63d | 2017-01-27 18:01:57 -0800 | [diff] [blame] | 75 | static struct attr_check *attr_whitespace_rule; |
Torsten Bögershausen | d64324c | 2018-09-12 21:32:02 +0200 | [diff] [blame] | 76 | const char *value; |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 77 | |
Junio C Hamano | 2aef63d | 2017-01-27 18:01:57 -0800 | [diff] [blame] | 78 | if (!attr_whitespace_rule) |
| 79 | attr_whitespace_rule = attr_check_initl("whitespace", NULL); |
| 80 | |
Junio C Hamano | 11877b9 | 2018-10-19 13:34:02 +0900 | [diff] [blame] | 81 | git_check_attr(istate, pathname, attr_whitespace_rule); |
Torsten Bögershausen | d64324c | 2018-09-12 21:32:02 +0200 | [diff] [blame] | 82 | value = attr_whitespace_rule->items[0].value; |
| 83 | if (ATTR_TRUE(value)) { |
| 84 | /* true (whitespace) */ |
| 85 | unsigned all_rule = ws_tab_width(whitespace_rule_cfg); |
| 86 | int i; |
| 87 | for (i = 0; i < ARRAY_SIZE(whitespace_rule_names); i++) |
| 88 | if (!whitespace_rule_names[i].loosens_error && |
| 89 | !whitespace_rule_names[i].exclude_default) |
| 90 | all_rule |= whitespace_rule_names[i].rule_bits; |
| 91 | return all_rule; |
| 92 | } else if (ATTR_FALSE(value)) { |
| 93 | /* false (-whitespace) */ |
| 94 | return ws_tab_width(whitespace_rule_cfg); |
| 95 | } else if (ATTR_UNSET(value)) { |
| 96 | /* reset to default (!whitespace) */ |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 97 | return whitespace_rule_cfg; |
Torsten Bögershausen | d64324c | 2018-09-12 21:32:02 +0200 | [diff] [blame] | 98 | } else { |
| 99 | /* string */ |
| 100 | return parse_whitespace_rule(value); |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 101 | } |
| 102 | } |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 103 | |
| 104 | /* The returned string should be freed by the caller. */ |
| 105 | char *whitespace_error_string(unsigned ws) |
| 106 | { |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 107 | struct strbuf err = STRBUF_INIT; |
Junio C Hamano | aeb84b0 | 2009-09-05 22:21:17 -0700 | [diff] [blame] | 108 | if ((ws & WS_TRAILING_SPACE) == WS_TRAILING_SPACE) |
Wincent Colaiuta | 420f4f0 | 2007-12-14 12:23:43 +0100 | [diff] [blame] | 109 | strbuf_addstr(&err, "trailing whitespace"); |
Junio C Hamano | aeb84b0 | 2009-09-05 22:21:17 -0700 | [diff] [blame] | 110 | else { |
| 111 | if (ws & WS_BLANK_AT_EOL) |
| 112 | strbuf_addstr(&err, "trailing whitespace"); |
| 113 | if (ws & WS_BLANK_AT_EOF) { |
| 114 | if (err.len) |
| 115 | strbuf_addstr(&err, ", "); |
| 116 | strbuf_addstr(&err, "new blank line at EOF"); |
| 117 | } |
| 118 | } |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 119 | if (ws & WS_SPACE_BEFORE_TAB) { |
| 120 | if (err.len) |
| 121 | strbuf_addstr(&err, ", "); |
Wincent Colaiuta | 420f4f0 | 2007-12-14 12:23:43 +0100 | [diff] [blame] | 122 | strbuf_addstr(&err, "space before tab in indent"); |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 123 | } |
| 124 | if (ws & WS_INDENT_WITH_NON_TAB) { |
| 125 | if (err.len) |
| 126 | strbuf_addstr(&err, ", "); |
Wincent Colaiuta | 420f4f0 | 2007-12-14 12:23:43 +0100 | [diff] [blame] | 127 | strbuf_addstr(&err, "indent with spaces"); |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 128 | } |
Chris Webb | 3e3ec2a | 2010-04-03 00:37:08 +0100 | [diff] [blame] | 129 | if (ws & WS_TAB_IN_INDENT) { |
| 130 | if (err.len) |
| 131 | strbuf_addstr(&err, ", "); |
| 132 | strbuf_addstr(&err, "tab in indent"); |
| 133 | } |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 134 | return strbuf_detach(&err, NULL); |
| 135 | } |
| 136 | |
| 137 | /* If stream is non-NULL, emits the line after checking. */ |
Junio C Hamano | 8f8841e | 2008-06-26 15:35:21 -0700 | [diff] [blame] | 138 | static unsigned ws_check_emit_1(const char *line, int len, unsigned ws_rule, |
| 139 | FILE *stream, const char *set, |
| 140 | const char *reset, const char *ws) |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 141 | { |
| 142 | unsigned result = 0; |
J. Bruce Fields | 954ecd4 | 2007-12-16 11:31:39 -0500 | [diff] [blame] | 143 | int written = 0; |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 144 | int trailing_whitespace = -1; |
| 145 | int trailing_newline = 0; |
Junio C Hamano | b2979ff | 2008-01-15 00:59:05 -0800 | [diff] [blame] | 146 | int trailing_carriage_return = 0; |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 147 | int i; |
| 148 | |
| 149 | /* Logic is simpler if we temporarily ignore the trailing newline. */ |
| 150 | if (len > 0 && line[len - 1] == '\n') { |
| 151 | trailing_newline = 1; |
| 152 | len--; |
| 153 | } |
Junio C Hamano | b2979ff | 2008-01-15 00:59:05 -0800 | [diff] [blame] | 154 | if ((ws_rule & WS_CR_AT_EOL) && |
| 155 | len > 0 && line[len - 1] == '\r') { |
| 156 | trailing_carriage_return = 1; |
| 157 | len--; |
| 158 | } |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 159 | |
| 160 | /* Check for trailing whitespace. */ |
Junio C Hamano | aeb84b0 | 2009-09-05 22:21:17 -0700 | [diff] [blame] | 161 | if (ws_rule & WS_BLANK_AT_EOL) { |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 162 | for (i = len - 1; i >= 0; i--) { |
| 163 | if (isspace(line[i])) { |
| 164 | trailing_whitespace = i; |
Junio C Hamano | aeb84b0 | 2009-09-05 22:21:17 -0700 | [diff] [blame] | 165 | result |= WS_BLANK_AT_EOL; |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 166 | } |
| 167 | else |
| 168 | break; |
| 169 | } |
| 170 | } |
| 171 | |
Kevin Ballard | cfd1a98 | 2010-10-20 15:17:26 -0700 | [diff] [blame] | 172 | if (trailing_whitespace == -1) |
| 173 | trailing_whitespace = len; |
| 174 | |
Chris Webb | 3e3ec2a | 2010-04-03 00:37:08 +0100 | [diff] [blame] | 175 | /* Check indentation */ |
Kevin Ballard | cfd1a98 | 2010-10-20 15:17:26 -0700 | [diff] [blame] | 176 | for (i = 0; i < trailing_whitespace; i++) { |
J. Bruce Fields | 9afa2d4 | 2007-12-16 11:31:40 -0500 | [diff] [blame] | 177 | if (line[i] == ' ') |
J. Bruce Fields | 1020999 | 2007-12-16 11:31:38 -0500 | [diff] [blame] | 178 | continue; |
J. Bruce Fields | 1020999 | 2007-12-16 11:31:38 -0500 | [diff] [blame] | 179 | if (line[i] != '\t') |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 180 | break; |
J. Bruce Fields | ffe5688 | 2007-12-16 11:31:41 -0500 | [diff] [blame] | 181 | if ((ws_rule & WS_SPACE_BEFORE_TAB) && written < i) { |
J. Bruce Fields | 1020999 | 2007-12-16 11:31:38 -0500 | [diff] [blame] | 182 | result |= WS_SPACE_BEFORE_TAB; |
J. Bruce Fields | ffe5688 | 2007-12-16 11:31:41 -0500 | [diff] [blame] | 183 | if (stream) { |
| 184 | fputs(ws, stream); |
| 185 | fwrite(line + written, i - written, 1, stream); |
| 186 | fputs(reset, stream); |
Chris Webb | 3e3ec2a | 2010-04-03 00:37:08 +0100 | [diff] [blame] | 187 | fwrite(line + i, 1, 1, stream); |
J. Bruce Fields | ffe5688 | 2007-12-16 11:31:41 -0500 | [diff] [blame] | 188 | } |
Chris Webb | 3e3ec2a | 2010-04-03 00:37:08 +0100 | [diff] [blame] | 189 | } else if (ws_rule & WS_TAB_IN_INDENT) { |
| 190 | result |= WS_TAB_IN_INDENT; |
| 191 | if (stream) { |
| 192 | fwrite(line + written, i - written, 1, stream); |
| 193 | fputs(ws, stream); |
| 194 | fwrite(line + i, 1, 1, stream); |
| 195 | fputs(reset, stream); |
| 196 | } |
| 197 | } else if (stream) { |
| 198 | fwrite(line + written, i - written + 1, 1, stream); |
| 199 | } |
J. Bruce Fields | 9afa2d4 | 2007-12-16 11:31:40 -0500 | [diff] [blame] | 200 | written = i + 1; |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /* Check for indent using non-tab. */ |
Johannes Sixt | f4b05a4 | 2010-11-30 09:29:11 +0100 | [diff] [blame] | 204 | if ((ws_rule & WS_INDENT_WITH_NON_TAB) && i - written >= ws_tab_width(ws_rule)) { |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 205 | result |= WS_INDENT_WITH_NON_TAB; |
J. Bruce Fields | ffe5688 | 2007-12-16 11:31:41 -0500 | [diff] [blame] | 206 | if (stream) { |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 207 | fputs(ws, stream); |
J. Bruce Fields | ffe5688 | 2007-12-16 11:31:41 -0500 | [diff] [blame] | 208 | fwrite(line + written, i - written, 1, stream); |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 209 | fputs(reset, stream); |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 210 | } |
J. Bruce Fields | ffe5688 | 2007-12-16 11:31:41 -0500 | [diff] [blame] | 211 | written = i; |
| 212 | } |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 213 | |
J. Bruce Fields | ffe5688 | 2007-12-16 11:31:41 -0500 | [diff] [blame] | 214 | if (stream) { |
Junio C Hamano | b2979ff | 2008-01-15 00:59:05 -0800 | [diff] [blame] | 215 | /* |
| 216 | * Now the rest of the line starts at "written". |
| 217 | * The non-highlighted part ends at "trailing_whitespace". |
| 218 | */ |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 219 | |
| 220 | /* Emit non-highlighted (middle) segment. */ |
J. Bruce Fields | 954ecd4 | 2007-12-16 11:31:39 -0500 | [diff] [blame] | 221 | if (trailing_whitespace - written > 0) { |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 222 | fputs(set, stream); |
J. Bruce Fields | 954ecd4 | 2007-12-16 11:31:39 -0500 | [diff] [blame] | 223 | fwrite(line + written, |
| 224 | trailing_whitespace - written, 1, stream); |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 225 | fputs(reset, stream); |
| 226 | } |
| 227 | |
| 228 | /* Highlight errors in trailing whitespace. */ |
| 229 | if (trailing_whitespace != len) { |
| 230 | fputs(ws, stream); |
| 231 | fwrite(line + trailing_whitespace, |
| 232 | len - trailing_whitespace, 1, stream); |
| 233 | fputs(reset, stream); |
| 234 | } |
Junio C Hamano | b2979ff | 2008-01-15 00:59:05 -0800 | [diff] [blame] | 235 | if (trailing_carriage_return) |
| 236 | fputc('\r', stream); |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 237 | if (trailing_newline) |
| 238 | fputc('\n', stream); |
| 239 | } |
| 240 | return result; |
| 241 | } |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 242 | |
Junio C Hamano | 8f8841e | 2008-06-26 15:35:21 -0700 | [diff] [blame] | 243 | void ws_check_emit(const char *line, int len, unsigned ws_rule, |
| 244 | FILE *stream, const char *set, |
| 245 | const char *reset, const char *ws) |
| 246 | { |
| 247 | (void)ws_check_emit_1(line, len, ws_rule, stream, set, reset, ws); |
| 248 | } |
| 249 | |
| 250 | unsigned ws_check(const char *line, int len, unsigned ws_rule) |
| 251 | { |
| 252 | return ws_check_emit_1(line, len, ws_rule, NULL, NULL, NULL, NULL); |
| 253 | } |
| 254 | |
Junio C Hamano | 877f23c | 2008-06-26 15:36:59 -0700 | [diff] [blame] | 255 | int ws_blank_line(const char *line, int len, unsigned ws_rule) |
| 256 | { |
| 257 | /* |
| 258 | * We _might_ want to treat CR differently from other |
| 259 | * whitespace characters when ws_rule has WS_CR_AT_EOL, but |
| 260 | * for now we just use this stupid definition. |
| 261 | */ |
| 262 | while (len-- > 0) { |
| 263 | if (!isspace(*line)) |
| 264 | return 0; |
| 265 | line++; |
| 266 | } |
| 267 | return 1; |
| 268 | } |
| 269 | |
Chris Webb | d511bd3 | 2010-04-03 00:37:23 +0100 | [diff] [blame] | 270 | /* Copy the line onto the end of the strbuf while fixing whitespaces */ |
| 271 | void ws_fix_copy(struct strbuf *dst, const char *src, int len, unsigned ws_rule, int *error_count) |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 272 | { |
| 273 | /* |
| 274 | * len is number of bytes to be copied from src, starting |
| 275 | * at src. Typically src[len-1] is '\n', unless this is |
| 276 | * the incomplete last line. |
| 277 | */ |
| 278 | int i; |
| 279 | int add_nl_to_tail = 0; |
| 280 | int add_cr_to_tail = 0; |
| 281 | int fixed = 0; |
| 282 | int last_tab_in_indent = -1; |
| 283 | int last_space_in_indent = -1; |
| 284 | int need_fix_leading_space = 0; |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 285 | |
| 286 | /* |
| 287 | * Strip trailing whitespace |
| 288 | */ |
Junio C Hamano | afd9db4 | 2009-09-15 03:28:08 -0700 | [diff] [blame] | 289 | if (ws_rule & WS_BLANK_AT_EOL) { |
Junio C Hamano | 422a82f | 2009-07-25 01:29:20 -0700 | [diff] [blame] | 290 | if (0 < len && src[len - 1] == '\n') { |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 291 | add_nl_to_tail = 1; |
| 292 | len--; |
Junio C Hamano | 422a82f | 2009-07-25 01:29:20 -0700 | [diff] [blame] | 293 | if (0 < len && src[len - 1] == '\r') { |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 294 | add_cr_to_tail = !!(ws_rule & WS_CR_AT_EOL); |
| 295 | len--; |
| 296 | } |
| 297 | } |
| 298 | if (0 < len && isspace(src[len - 1])) { |
| 299 | while (0 < len && isspace(src[len-1])) |
| 300 | len--; |
| 301 | fixed = 1; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | /* |
| 306 | * Check leading whitespaces (indent) |
| 307 | */ |
| 308 | for (i = 0; i < len; i++) { |
| 309 | char ch = src[i]; |
| 310 | if (ch == '\t') { |
| 311 | last_tab_in_indent = i; |
| 312 | if ((ws_rule & WS_SPACE_BEFORE_TAB) && |
| 313 | 0 <= last_space_in_indent) |
| 314 | need_fix_leading_space = 1; |
| 315 | } else if (ch == ' ') { |
| 316 | last_space_in_indent = i; |
| 317 | if ((ws_rule & WS_INDENT_WITH_NON_TAB) && |
Johannes Sixt | f4b05a4 | 2010-11-30 09:29:11 +0100 | [diff] [blame] | 318 | ws_tab_width(ws_rule) <= i - last_tab_in_indent) |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 319 | need_fix_leading_space = 1; |
| 320 | } else |
| 321 | break; |
| 322 | } |
| 323 | |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 324 | if (need_fix_leading_space) { |
| 325 | /* Process indent ourselves */ |
| 326 | int consecutive_spaces = 0; |
| 327 | int last = last_tab_in_indent + 1; |
| 328 | |
| 329 | if (ws_rule & WS_INDENT_WITH_NON_TAB) { |
| 330 | /* have "last" point at one past the indent */ |
| 331 | if (last_tab_in_indent < last_space_in_indent) |
| 332 | last = last_space_in_indent + 1; |
| 333 | else |
| 334 | last = last_tab_in_indent + 1; |
| 335 | } |
| 336 | |
| 337 | /* |
| 338 | * between src[0..last-1], strip the funny spaces, |
| 339 | * updating them to tab as needed. |
| 340 | */ |
| 341 | for (i = 0; i < last; i++) { |
| 342 | char ch = src[i]; |
| 343 | if (ch != ' ') { |
| 344 | consecutive_spaces = 0; |
Chris Webb | d511bd3 | 2010-04-03 00:37:23 +0100 | [diff] [blame] | 345 | strbuf_addch(dst, ch); |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 346 | } else { |
| 347 | consecutive_spaces++; |
Johannes Sixt | f4b05a4 | 2010-11-30 09:29:11 +0100 | [diff] [blame] | 348 | if (consecutive_spaces == ws_tab_width(ws_rule)) { |
Chris Webb | d511bd3 | 2010-04-03 00:37:23 +0100 | [diff] [blame] | 349 | strbuf_addch(dst, '\t'); |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 350 | consecutive_spaces = 0; |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | while (0 < consecutive_spaces--) |
Chris Webb | d511bd3 | 2010-04-03 00:37:23 +0100 | [diff] [blame] | 355 | strbuf_addch(dst, ' '); |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 356 | len -= last; |
| 357 | src += last; |
| 358 | fixed = 1; |
Chris Webb | 4e35c51 | 2010-04-03 00:37:30 +0100 | [diff] [blame] | 359 | } else if ((ws_rule & WS_TAB_IN_INDENT) && last_tab_in_indent >= 0) { |
| 360 | /* Expand tabs into spaces */ |
Johannes Sixt | d35711a | 2010-11-30 09:22:04 +0100 | [diff] [blame] | 361 | int start = dst->len; |
Chris Webb | 4e35c51 | 2010-04-03 00:37:30 +0100 | [diff] [blame] | 362 | int last = last_tab_in_indent + 1; |
| 363 | for (i = 0; i < last; i++) { |
| 364 | if (src[i] == '\t') |
| 365 | do { |
| 366 | strbuf_addch(dst, ' '); |
Johannes Sixt | f4b05a4 | 2010-11-30 09:29:11 +0100 | [diff] [blame] | 367 | } while ((dst->len - start) % ws_tab_width(ws_rule)); |
Chris Webb | 4e35c51 | 2010-04-03 00:37:30 +0100 | [diff] [blame] | 368 | else |
| 369 | strbuf_addch(dst, src[i]); |
| 370 | } |
| 371 | len -= last; |
| 372 | src += last; |
| 373 | fixed = 1; |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Chris Webb | d511bd3 | 2010-04-03 00:37:23 +0100 | [diff] [blame] | 376 | strbuf_add(dst, src, len); |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 377 | if (add_cr_to_tail) |
Chris Webb | d511bd3 | 2010-04-03 00:37:23 +0100 | [diff] [blame] | 378 | strbuf_addch(dst, '\r'); |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 379 | if (add_nl_to_tail) |
Chris Webb | d511bd3 | 2010-04-03 00:37:23 +0100 | [diff] [blame] | 380 | strbuf_addch(dst, '\n'); |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 381 | if (fixed && error_count) |
| 382 | (*error_count)++; |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 383 | } |