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