189
}
190
191
static void check_safe_crlf(const char *path, enum crlf_action crlf_action,
192
- struct text_stat *stats, enum safe_crlf checksafe)
192
+ struct text_stat *old_stats, struct text_stat *new_stats,
193
+ enum safe_crlf checksafe)
194
{
194
- if (!checksafe)
195
- return;
196
-
197
- if (output_eol(crlf_action) == EOL_LF) {
195
+ if (old_stats->crlf && !new_stats->crlf ) {
196
/*
199
- * CRLFs would not be restored by checkout:
200
- * check if we'd remove CRLFs
197
+ * CRLFs would not be restored by checkout
198
*/
202
- if (stats->crlf) {
203
- if (checksafe == SAFE_CRLF_WARN)
204
- warning("CRLF will be replaced by LF in %s.\nThe file will have its original line endings in your working directory.", path);
205
- else /* i.e. SAFE_CRLF_FAIL */
206
- die("CRLF would be replaced by LF in %s.", path);
207
- }
208
- } else if (output_eol(crlf_action) == EOL_CRLF) {
199
+ if (checksafe == SAFE_CRLF_WARN)
200
+ warning("CRLF will be replaced by LF in %s.\nThe file will have its original line endings in your working directory.", path);
201
+ else /* i.e. SAFE_CRLF_FAIL */
202
+ die("CRLF would be replaced by LF in %s.", path);
203
+ } else if (old_stats->lonelf && !new_stats->lonelf ) {
204
/*
210
- * CRLFs would be added by checkout:
211
- * check if we have "naked" LFs
205
+ * CRLFs would be added by checkout
206
*/
213
- if (stats->lonelf) {
214
- if (checksafe == SAFE_CRLF_WARN)
215
- warning("LF will be replaced by CRLF in %s.\nThe file will have its original line endings in your working directory.", path);
216
- else /* i.e. SAFE_CRLF_FAIL */
217
- die("LF would be replaced by CRLF in %s", path);
218
- }
207
+ if (checksafe == SAFE_CRLF_WARN)
208
+ warning("LF will be replaced by CRLF in %s.\nThe file will have its original line endings in your working directory.", path);
209
+ else /* i.e. SAFE_CRLF_FAIL */
210
+ die("LF would be replaced by CRLF in %s", path);
211
}
212
}
213
225
return has_cr;
226
}
227
228
+static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats,
229
+ enum crlf_action crlf_action)
230
+{
231
+ if (output_eol(crlf_action) != EOL_CRLF)
232
+ return 0;
233
+ /* No "naked" LF? Nothing to convert, regardless. */
234
+ if (!stats->lonelf)
235
+ return 0;
236
+
237
+ if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
238
+ /* If we have any CR or CRLF line endings, we do not touch it */
239
+ /* This is the new safer autocrlf-handling */
240
+ if (stats->lonecr || stats->crlf)
241
+ return 0;
242
+
243
+ if (convert_is_binary(len, stats))
244
+ return 0;
245
+ }
246
+ return 1;
247
+
248
+}
249
+
250
static int crlf_to_git(const char *path, const char *src, size_t len,
251
struct strbuf *buf,
252
enum crlf_action crlf_action, enum safe_crlf checksafe)
253
{
254
struct text_stat stats;
255
char *dst;
256
+ int convert_crlf_into_lf;
257
258
if (crlf_action == CRLF_BINARY ||
259
(src && !len))
267
return 1;
268
269
gather_stats(src, len, &stats);
270
+ /* Optimization: No CRLF? Nothing to convert, regardless. */
271
+ convert_crlf_into_lf = !!stats.crlf;
272
273
if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
274
if (convert_is_binary(len, &stats))
280
if (checksafe == SAFE_CRLF_RENORMALIZE)
281
checksafe = SAFE_CRLF_FALSE;
282
else if (has_cr_in_index(path))
266
- return 0;
283
+ convert_crlf_into_lf = 0;
284
}
268
- check_safe_crlf(path, crlf_action, &stats, checksafe);
269
-
270
- /* Optimization: No CRLF? Nothing to convert, regardless. */
271
- if (!stats.crlf)
285
+ if (checksafe && len) {
286
+ struct text_stat new_stats;
287
+ memcpy(&new_stats, &stats, sizeof(new_stats));
288
+ /* simulate "git add" */
289
+ if (convert_crlf_into_lf) {
290
+ new_stats.lonelf += new_stats.crlf;
291
+ new_stats.crlf = 0;
292
+ }
293
+ /* simulate "git checkout" */
294
+ if (will_convert_lf_to_crlf(len, &new_stats, crlf_action)) {
295
+ new_stats.crlf += new_stats.lonelf;
296
+ new_stats.lonelf = 0;
297
+ }
298
+ check_safe_crlf(path, crlf_action, &stats, &new_stats, checksafe);
299
+ }
300
+ if (!convert_crlf_into_lf)
301
return 0;
302
303
/*
343
return 0;
344
345
gather_stats(src, len, &stats);
317
-
318
- /* No "naked" LF? Nothing to convert, regardless. */
319
- if (!stats.lonelf)
346
+ if (!will_convert_lf_to_crlf(len, &stats, crlf_action))
347
return 0;
348
322
- if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
323
- /* If we have any CR or CRLF line endings, we do not touch it */
324
- /* This is the new safer autocrlf-handling */
325
- if (stats.lonecr || stats.crlf )
326
- return 0;
327
-
328
- if (convert_is_binary(len, &stats))
329
- return 0;
330
- }
331
-
349
/* are we "faking" in place editing ? */
350
if (src == buf->buf)
351
to_free = strbuf_detach(buf, NULL);