10
* translation when the "auto_crlf" option is set.
11
*/
12
13
+#define CRLF_GUESS (-1)
14
+#define CRLF_BINARY 0
15
+#define CRLF_TEXT 1
16
+#define CRLF_INPUT 2
17
+
18
struct text_stat {
19
/* CR, LF and CRLF counts */
20
unsigned cr, lf, crlf;
79
return 0;
80
}
81
77
-static int crlf_to_git(const char *path, char **bufp, unsigned long *sizep, int guess)
82
+static int crlf_to_git(const char *path, char **bufp, unsigned long *sizep, int action)
83
{
84
char *buffer, *nbuf;
85
unsigned long size, nsize;
86
struct text_stat stats;
87
83
- if (guess && !auto_crlf)
88
+ if ((action == CRLF_BINARY) || (action == CRLF_GUESS && !auto_crlf))
89
return 0;
90
91
size = *sizep;
99
if (!stats.cr)
100
return 0;
101
97
- if (guess) {
102
+ if (action == CRLF_GUESS) {
103
/*
104
* We're currently not going to even try to convert stuff
105
* that has bare CR characters. Does anybody do that crazy
124
*bufp = nbuf;
125
*sizep = nsize;
126
122
- if (guess) {
127
+ if (action == CRLF_GUESS) {
128
+ /*
129
+ * If we guessed, we already know we rejected a file with
130
+ * lone CR, and we can strip a CR without looking at what
131
+ * follow it.
132
+ */
133
do {
134
unsigned char c = *buffer++;
135
if (c != '\r')
146
return 1;
147
}
148
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)
149
+static int crlf_to_worktree(const char *path, char **bufp, unsigned long *sizep, int action)
150
{
151
char *buffer, *nbuf;
152
unsigned long size, nsize;
153
struct text_stat stats;
154
unsigned char last;
155
156
- if (guess && auto_crlf <= 0)
156
+ if ((action == CRLF_BINARY) || (action == CRLF_INPUT) ||
157
+ (action == CRLF_GUESS && auto_crlf <= 0))
158
return 0;
159
160
size = *sizep;
172
if (stats.lf == stats.crlf)
173
return 0;
174
174
- if (guess) {
175
+ if (action == CRLF_GUESS) {
176
/* If we have any bare CR characters, we're not going to touch it */
177
if (stats.cr != stats.crlf)
178
return 0;
201
return 1;
202
}
203
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
-
204
static void setup_crlf_check(struct git_attr_check *check)
205
{
206
static struct git_attr *attr_crlf;
219
if (!git_checkattr(path, 1, &attr_crlf_check)) {
220
const char *value = attr_crlf_check.value;
221
if (ATTR_TRUE(value))
231
- return 1;
222
+ return CRLF_TEXT;
223
else if (ATTR_FALSE(value))
233
- return 0;
224
+ return CRLF_BINARY;
225
else if (ATTR_UNSET(value))
226
;
236
- else
237
- die("unknown value %s given to 'crlf' attribute",
238
- (char *)value);
227
+ else if (!strcmp(value, "input"))
228
+ return CRLF_INPUT;
229
+ /* fallthru */
230
}
240
- return -1;
231
+ return CRLF_GUESS;
232
}
233
234
int convert_to_git(const char *path, char **bufp, unsigned long *sizep)
235
{
245
- switch (git_path_check_crlf(path)) {
246
- case 0:
247
- return 0;
248
- case 1:
249
- return forcecrlf_to_git(path, bufp, sizep);
250
- default:
251
- return autocrlf_to_git(path, bufp, sizep);
252
- }
236
+ return crlf_to_git(path, bufp, sizep, git_path_check_crlf(path));
237
}
238
239
int convert_to_working_tree(const char *path, char **bufp, unsigned long *sizep)
240
{
257
- switch (git_path_check_crlf(path)) {
258
- case 0:
259
- return 0;
260
- case 1:
261
- return forcecrlf_to_working_tree(path, bufp, sizep);
262
- default:
263
- return autocrlf_to_working_tree(path, bufp, sizep);
264
- }
241
+ return crlf_to_worktree(path, bufp, sizep, git_path_check_crlf(path));
242
}