Fix 'crlf' attribute semantics.
Earlier we said 'crlf lets the path go through core.autocrlf process while !crlf disables it altogether'. This fixes the semantics to: - Lack of 'crlf' attribute makes core.autocrlf to apply (i.e. we guess based on the contents and if platform expresses its desire to have CRLF line endings via core.autocrlf, we do so). - Setting 'crlf' attribute to true forces CRLF line endings in working tree files, even if blob does not look like text (e.g. contains NUL or other bytes we consider binary). - Setting 'crlf' attribute to false disables conversion. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Apr 15, 2007 at 13:35 UTC
201ac8efc79668353281583629aa15ac7f36e843
1 file changed
+78
-44
convert.c
+78
-44
@@ -74,13 +74,13 @@ static int is_binary(unsigned long size, struct text_stat *stats)
74
return 0;
75
}
76
77
-static int autocrlf_to_git(const char *path, char **bufp, unsigned long *sizep)
77
+static int crlf_to_git(const char *path, char **bufp, unsigned long *sizep, int guess)
78
{
79
char *buffer, *nbuf;
80
unsigned long size, nsize;
81
struct text_stat stats;
82
83
- if (!auto_crlf)
83
+ if (guess && !auto_crlf)
84
return 0;
85
86
size = *sizep;
@@ -94,19 +94,21 @@ static int autocrlf_to_git(const char *path, char **bufp, unsigned long *sizep)
94
if (!stats.cr)
95
return 0;
96
97
- /*
98
- * We're currently not going to even try to convert stuff
99
- * that has bare CR characters. Does anybody do that crazy
100
- * stuff?
101
- */
102
- if (stats.cr != stats.crlf)
103
- return 0;
104
-
105
- /*
106
- * And add some heuristics for binary vs text, of course...
107
- */
108
- if (is_binary(size, &stats))
109
- return 0;
97
+ if (guess) {
98
+ /*
99
+ * We're currently not going to even try to convert stuff
100
+ * that has bare CR characters. Does anybody do that crazy
101
+ * stuff?
102
+ */
103
+ if (stats.cr != stats.crlf)
104
+ return 0;
105
+
106
+ /*
107
+ * And add some heuristics for binary vs text, of course...
108
+ */
109
+ if (is_binary(size, &stats))
110
+ return 0;
111
+ }
112
113
/*
114
* Ok, allocate a new buffer, fill it in, and return true
@@ -116,28 +118,42 @@ static int autocrlf_to_git(const char *path, char **bufp, unsigned long *sizep)
118
nbuf = xmalloc(nsize);
119
*bufp = nbuf;
120
*sizep = nsize;
119
- do {
120
- unsigned char c = *buffer++;
121
- if (c != '\r')
122
- *nbuf++ = c;
123
- } while (--size);
121
+
122
+ if (guess) {
123
+ do {
124
+ unsigned char c = *buffer++;
125
+ if (c != '\r')
126
+ *nbuf++ = c;
127
+ } while (--size);
128
+ } else {
129
+ do {
130
+ unsigned char c = *buffer++;
131
+ if (! (c == '\r' && (1 < size && *buffer == '\n')))
132
+ *nbuf++ = c;
133
+ } while (--size);
134
+ }
135
136
return 1;
137
}
138
128
-static int autocrlf_to_working_tree(const char *path, char **bufp, unsigned long *sizep)
139
+static int autocrlf_to_git(const char *path, char **bufp, unsigned long *sizep)
140
+{
141
+ return crlf_to_git(path, bufp, sizep, 1);
142
+}
143
+
144
+static int forcecrlf_to_git(const char *path, char **bufp, unsigned long *sizep)
145
+{
146
+ return crlf_to_git(path, bufp, sizep, 0);
147
+}
148
+
149
+static int crlf_to_working_tree(const char *path, char **bufp, unsigned long *sizep, int guess)
150
{
151
char *buffer, *nbuf;
152
unsigned long size, nsize;
153
struct text_stat stats;
154
unsigned char last;
155
135
- /*
136
- * FIXME! Other pluggable conversions should go here,
137
- * based on filename patterns. Right now we just do the
138
- * stupid auto-CRLF one.
139
- */
140
- if (auto_crlf <= 0)
156
+ if (guess && auto_crlf <= 0)
157
return 0;
158
159
size = *sizep;
@@ -155,12 +171,14 @@ static int autocrlf_to_working_tree(const char *path, char **bufp, unsigned long
171
if (stats.lf == stats.crlf)
172
return 0;
173
158
- /* If we have any bare CR characters, we're not going to touch it */
159
- if (stats.cr != stats.crlf)
160
- return 0;
174
+ if (guess) {
175
+ /* If we have any bare CR characters, we're not going to touch it */
176
+ if (stats.cr != stats.crlf)
177
+ return 0;
178
162
- if (is_binary(size, &stats))
163
- return 0;
179
+ if (is_binary(size, &stats))
180
+ return 0;
181
+ }
182
183
/*
184
* Ok, allocate a new buffer, fill it in, and return true
@@ -182,6 +200,16 @@ static int autocrlf_to_working_tree(const char *path, char **bufp, unsigned long
200
return 1;
201
}
202
203
+static int autocrlf_to_working_tree(const char *path, char **bufp, unsigned long *sizep)
204
+{
205
+ return crlf_to_working_tree(path, bufp, sizep, 1);
206
+}
207
+
208
+static int forcecrlf_to_working_tree(const char *path, char **bufp, unsigned long *sizep)
209
+{
210
+ return crlf_to_working_tree(path, bufp, sizep, 0);
211
+}
212
+
213
static void setup_crlf_check(struct git_attr_check *check)
214
{
215
static struct git_attr *attr_crlf;
@@ -191,31 +219,37 @@ static void setup_crlf_check(struct git_attr_check *check)
219
check->attr = attr_crlf;
220
}
221
194
-static int git_path_is_binary(const char *path)
222
+static int git_path_check_crlf(const char *path)
223
{
224
struct git_attr_check attr_crlf_check;
225
226
setup_crlf_check(&attr_crlf_check);
227
200
- /*
201
- * If crlf is not mentioned, default to autocrlf;
202
- * disable autocrlf only when crlf attribute is explicitly
203
- * unset.
204
- */
205
- return (!git_checkattr(path, 1, &attr_crlf_check) &&
206
- (0 == attr_crlf_check.isset));
228
+ if (git_checkattr(path, 1, &attr_crlf_check))
229
+ return -1;
230
+ return attr_crlf_check.isset;
231
}
232
233
int convert_to_git(const char *path, char **bufp, unsigned long *sizep)
234
{
211
- if (git_path_is_binary(path))
235
+ switch (git_path_check_crlf(path)) {
236
+ case 0:
237
return 0;
213
- return autocrlf_to_git(path, bufp, sizep);
238
+ case 1:
239
+ return forcecrlf_to_git(path, bufp, sizep);
240
+ default:
241
+ return autocrlf_to_git(path, bufp, sizep);
242
+ }
243
}
244
245
int convert_to_working_tree(const char *path, char **bufp, unsigned long *sizep)
246
{
218
- if (git_path_is_binary(path))
247
+ switch (git_path_check_crlf(path)) {
248
+ case 0:
249
return 0;
220
- return autocrlf_to_working_tree(path, bufp, sizep);
250
+ case 1:
251
+ return forcecrlf_to_working_tree(path, bufp, sizep);
252
+ default:
253
+ return autocrlf_to_working_tree(path, bufp, sizep);
254
+ }
255
}