git: add --no-replace-objects option to disable replacing
[git] / ws.c
1 /*
2  * Whitespace rules
3  *
4  * Copyright (c) 2007 Junio C Hamano
5  */
6
7 #include "cache.h"
8 #include "attr.h"
9
10 static struct whitespace_rule {
11         const char *rule_name;
12         unsigned rule_bits;
13         unsigned loosens_error;
14 } whitespace_rule_names[] = {
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 },
19 };
20
21 unsigned parse_whitespace_rule(const char *string)
22 {
23         unsigned rule = WS_DEFAULT_RULE;
24
25         while (string) {
26                 int i;
27                 size_t len;
28                 const char *ep;
29                 int negated = 0;
30
31                 string = string + strspn(string, ", \t\n\r");
32                 ep = strchr(string, ',');
33                 if (!ep)
34                         len = strlen(string);
35                 else
36                         len = ep - string;
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                 }
55                 string = ep;
56         }
57         return rule;
58 }
59
60 static void setup_whitespace_attr_check(struct git_attr_check *check)
61 {
62         static struct git_attr *attr_whitespace;
63
64         if (!attr_whitespace)
65                 attr_whitespace = git_attr("whitespace", 10);
66         check[0].attr = attr_whitespace;
67 }
68
69 unsigned whitespace_rule(const char *pathname)
70 {
71         struct git_attr_check attr_whitespace_rule;
72
73         setup_whitespace_attr_check(&attr_whitespace_rule);
74         if (!git_checkattr(pathname, 1, &attr_whitespace_rule)) {
75                 const char *value;
76
77                 value = attr_whitespace_rule.value;
78                 if (ATTR_TRUE(value)) {
79                         /* true (whitespace) */
80                         unsigned all_rule = 0;
81                         int i;
82                         for (i = 0; i < ARRAY_SIZE(whitespace_rule_names); i++)
83                                 if (!whitespace_rule_names[i].loosens_error)
84                                         all_rule |= whitespace_rule_names[i].rule_bits;
85                         return all_rule;
86                 } else if (ATTR_FALSE(value)) {
87                         /* false (-whitespace) */
88                         return 0;
89                 } else if (ATTR_UNSET(value)) {
90                         /* reset to default (!whitespace) */
91                         return whitespace_rule_cfg;
92                 } else {
93                         /* string */
94                         return parse_whitespace_rule(value);
95                 }
96         } else {
97                 return whitespace_rule_cfg;
98         }
99 }
100
101 /* The returned string should be freed by the caller. */
102 char *whitespace_error_string(unsigned ws)
103 {
104         struct strbuf err = STRBUF_INIT;
105         if (ws & WS_TRAILING_SPACE)
106                 strbuf_addstr(&err, "trailing whitespace");
107         if (ws & WS_SPACE_BEFORE_TAB) {
108                 if (err.len)
109                         strbuf_addstr(&err, ", ");
110                 strbuf_addstr(&err, "space before tab in indent");
111         }
112         if (ws & WS_INDENT_WITH_NON_TAB) {
113                 if (err.len)
114                         strbuf_addstr(&err, ", ");
115                 strbuf_addstr(&err, "indent with spaces");
116         }
117         return strbuf_detach(&err, NULL);
118 }
119
120 /* If stream is non-NULL, emits the line after checking. */
121 static unsigned ws_check_emit_1(const char *line, int len, unsigned ws_rule,
122                                 FILE *stream, const char *set,
123                                 const char *reset, const char *ws)
124 {
125         unsigned result = 0;
126         int written = 0;
127         int trailing_whitespace = -1;
128         int trailing_newline = 0;
129         int trailing_carriage_return = 0;
130         int i;
131
132         /* Logic is simpler if we temporarily ignore the trailing newline. */
133         if (len > 0 && line[len - 1] == '\n') {
134                 trailing_newline = 1;
135                 len--;
136         }
137         if ((ws_rule & WS_CR_AT_EOL) &&
138             len > 0 && line[len - 1] == '\r') {
139                 trailing_carriage_return = 1;
140                 len--;
141         }
142
143         /* Check for trailing whitespace. */
144         if (ws_rule & WS_TRAILING_SPACE) {
145                 for (i = len - 1; i >= 0; i--) {
146                         if (isspace(line[i])) {
147                                 trailing_whitespace = i;
148                                 result |= WS_TRAILING_SPACE;
149                         }
150                         else
151                                 break;
152                 }
153         }
154
155         /* Check for space before tab in initial indent. */
156         for (i = 0; i < len; i++) {
157                 if (line[i] == ' ')
158                         continue;
159                 if (line[i] != '\t')
160                         break;
161                 if ((ws_rule & WS_SPACE_BEFORE_TAB) && written < i) {
162                         result |= WS_SPACE_BEFORE_TAB;
163                         if (stream) {
164                                 fputs(ws, stream);
165                                 fwrite(line + written, i - written, 1, stream);
166                                 fputs(reset, stream);
167                         }
168                 } else if (stream)
169                         fwrite(line + written, i - written, 1, stream);
170                 if (stream)
171                         fwrite(line + i, 1, 1, stream);
172                 written = i + 1;
173         }
174
175         /* Check for indent using non-tab. */
176         if ((ws_rule & WS_INDENT_WITH_NON_TAB) && i - written >= 8) {
177                 result |= WS_INDENT_WITH_NON_TAB;
178                 if (stream) {
179                         fputs(ws, stream);
180                         fwrite(line + written, i - written, 1, stream);
181                         fputs(reset, stream);
182                 }
183                 written = i;
184         }
185
186         if (stream) {
187                 /*
188                  * Now the rest of the line starts at "written".
189                  * The non-highlighted part ends at "trailing_whitespace".
190                  */
191                 if (trailing_whitespace == -1)
192                         trailing_whitespace = len;
193
194                 /* Emit non-highlighted (middle) segment. */
195                 if (trailing_whitespace - written > 0) {
196                         fputs(set, stream);
197                         fwrite(line + written,
198                             trailing_whitespace - written, 1, stream);
199                         fputs(reset, stream);
200                 }
201
202                 /* Highlight errors in trailing whitespace. */
203                 if (trailing_whitespace != len) {
204                         fputs(ws, stream);
205                         fwrite(line + trailing_whitespace,
206                             len - trailing_whitespace, 1, stream);
207                         fputs(reset, stream);
208                 }
209                 if (trailing_carriage_return)
210                         fputc('\r', stream);
211                 if (trailing_newline)
212                         fputc('\n', stream);
213         }
214         return result;
215 }
216
217 void ws_check_emit(const char *line, int len, unsigned ws_rule,
218                    FILE *stream, const char *set,
219                    const char *reset, const char *ws)
220 {
221         (void)ws_check_emit_1(line, len, ws_rule, stream, set, reset, ws);
222 }
223
224 unsigned ws_check(const char *line, int len, unsigned ws_rule)
225 {
226         return ws_check_emit_1(line, len, ws_rule, NULL, NULL, NULL, NULL);
227 }
228
229 int ws_blank_line(const char *line, int len, unsigned ws_rule)
230 {
231         /*
232          * We _might_ want to treat CR differently from other
233          * whitespace characters when ws_rule has WS_CR_AT_EOL, but
234          * for now we just use this stupid definition.
235          */
236         while (len-- > 0) {
237                 if (!isspace(*line))
238                         return 0;
239                 line++;
240         }
241         return 1;
242 }
243
244 /* Copy the line to the buffer while fixing whitespaces */
245 int ws_fix_copy(char *dst, const char *src, int len, unsigned ws_rule, int *error_count)
246 {
247         /*
248          * len is number of bytes to be copied from src, starting
249          * at src.  Typically src[len-1] is '\n', unless this is
250          * the incomplete last line.
251          */
252         int i;
253         int add_nl_to_tail = 0;
254         int add_cr_to_tail = 0;
255         int fixed = 0;
256         int last_tab_in_indent = -1;
257         int last_space_in_indent = -1;
258         int need_fix_leading_space = 0;
259         char *buf;
260
261         /*
262          * Strip trailing whitespace
263          */
264         if (ws_rule & WS_TRAILING_SPACE) {
265                 if (0 < len && src[len - 1] == '\n') {
266                         add_nl_to_tail = 1;
267                         len--;
268                         if (0 < len && src[len - 1] == '\r') {
269                                 add_cr_to_tail = !!(ws_rule & WS_CR_AT_EOL);
270                                 len--;
271                         }
272                 }
273                 if (0 < len && isspace(src[len - 1])) {
274                         while (0 < len && isspace(src[len-1]))
275                                 len--;
276                         fixed = 1;
277                 }
278         }
279
280         /*
281          * Check leading whitespaces (indent)
282          */
283         for (i = 0; i < len; i++) {
284                 char ch = src[i];
285                 if (ch == '\t') {
286                         last_tab_in_indent = i;
287                         if ((ws_rule & WS_SPACE_BEFORE_TAB) &&
288                             0 <= last_space_in_indent)
289                             need_fix_leading_space = 1;
290                 } else if (ch == ' ') {
291                         last_space_in_indent = i;
292                         if ((ws_rule & WS_INDENT_WITH_NON_TAB) &&
293                             8 <= i - last_tab_in_indent)
294                                 need_fix_leading_space = 1;
295                 } else
296                         break;
297         }
298
299         buf = dst;
300         if (need_fix_leading_space) {
301                 /* Process indent ourselves */
302                 int consecutive_spaces = 0;
303                 int last = last_tab_in_indent + 1;
304
305                 if (ws_rule & WS_INDENT_WITH_NON_TAB) {
306                         /* have "last" point at one past the indent */
307                         if (last_tab_in_indent < last_space_in_indent)
308                                 last = last_space_in_indent + 1;
309                         else
310                                 last = last_tab_in_indent + 1;
311                 }
312
313                 /*
314                  * between src[0..last-1], strip the funny spaces,
315                  * updating them to tab as needed.
316                  */
317                 for (i = 0; i < last; i++) {
318                         char ch = src[i];
319                         if (ch != ' ') {
320                                 consecutive_spaces = 0;
321                                 *dst++ = ch;
322                         } else {
323                                 consecutive_spaces++;
324                                 if (consecutive_spaces == 8) {
325                                         *dst++ = '\t';
326                                         consecutive_spaces = 0;
327                                 }
328                         }
329                 }
330                 while (0 < consecutive_spaces--)
331                         *dst++ = ' ';
332                 len -= last;
333                 src += last;
334                 fixed = 1;
335         }
336
337         memcpy(dst, src, len);
338         if (add_cr_to_tail)
339                 dst[len++] = '\r';
340         if (add_nl_to_tail)
341                 dst[len++] = '\n';
342         if (fixed && error_count)
343                 (*error_count)++;
344         return dst + len - buf;
345 }