@@ -219,6 +219,7 @@ struct patch {
219
219
unsigned int recount :1 ;
220
220
unsigned int conflicted_threeway :1 ;
221
221
unsigned int direct_to_threeway :1 ;
222
+ unsigned int crlf_in_old :1 ;
222
223
struct fragment * fragments ;
223
224
char * result ;
224
225
size_t resultsize ;
@@ -1661,6 +1662,19 @@ static void check_whitespace(struct apply_state *state,
1661
1662
record_ws_error (state , result , line + 1 , len - 2 , state -> linenr );
1662
1663
}
1663
1664
1665
+ /*
1666
+ * Check if the patch has context lines with CRLF or
1667
+ * the patch wants to remove lines with CRLF.
1668
+ */
1669
+ static void check_old_for_crlf (struct patch * patch , const char * line , int len )
1670
+ {
1671
+ if (len >= 2 && line [len - 1 ] == '\n' && line [len - 2 ] == '\r' ) {
1672
+ patch -> ws_rule |= WS_CR_AT_EOL ;
1673
+ patch -> crlf_in_old = 1 ;
1674
+ }
1675
+ }
1676
+
1677
+
1664
1678
/*
1665
1679
* Parse a unified diff. Note that this really needs to parse each
1666
1680
* fragment separately, since the only way to know the difference
@@ -1711,11 +1725,14 @@ static int parse_fragment(struct apply_state *state,
1711
1725
if (!deleted && !added )
1712
1726
leading ++ ;
1713
1727
trailing ++ ;
1728
+ check_old_for_crlf (patch , line , len );
1714
1729
if (!state -> apply_in_reverse &&
1715
1730
state -> ws_error_action == correct_ws_error )
1716
1731
check_whitespace (state , line , len , patch -> ws_rule );
1717
1732
break ;
1718
1733
case '-' :
1734
+ if (!state -> apply_in_reverse )
1735
+ check_old_for_crlf (patch , line , len );
1719
1736
if (state -> apply_in_reverse &&
1720
1737
state -> ws_error_action != nowarn_ws_error )
1721
1738
check_whitespace (state , line , len , patch -> ws_rule );
@@ -1724,6 +1741,8 @@ static int parse_fragment(struct apply_state *state,
1724
1741
trailing = 0 ;
1725
1742
break ;
1726
1743
case '+' :
1744
+ if (state -> apply_in_reverse )
1745
+ check_old_for_crlf (patch , line , len );
1727
1746
if (!state -> apply_in_reverse &&
1728
1747
state -> ws_error_action != nowarn_ws_error )
1729
1748
check_whitespace (state , line , len , patch -> ws_rule );
@@ -2266,8 +2285,11 @@ static void show_stats(struct apply_state *state, struct patch *patch)
2266
2285
add , pluses , del , minuses );
2267
2286
}
2268
2287
2269
- static int read_old_data (struct stat * st , const char * path , struct strbuf * buf )
2288
+ static int read_old_data (struct stat * st , struct patch * patch ,
2289
+ const char * path , struct strbuf * buf )
2270
2290
{
2291
+ enum safe_crlf safe_crlf = patch -> crlf_in_old ?
2292
+ SAFE_CRLF_KEEP_CRLF : SAFE_CRLF_RENORMALIZE ;
2271
2293
switch (st -> st_mode & S_IFMT ) {
2272
2294
case S_IFLNK :
2273
2295
if (strbuf_readlink (buf , path , st -> st_size ) < 0 )
@@ -2276,7 +2298,15 @@ static int read_old_data(struct stat *st, const char *path, struct strbuf *buf)
2276
2298
case S_IFREG :
2277
2299
if (strbuf_read_file (buf , path , st -> st_size ) != st -> st_size )
2278
2300
return error (_ ("unable to open or read %s" ), path );
2279
- convert_to_git (& the_index , path , buf -> buf , buf -> len , buf , 0 );
2301
+ /*
2302
+ * "git apply" without "--index/--cached" should never look
2303
+ * at the index; the target file may not have been added to
2304
+ * the index yet, and we may not even be in any Git repository.
2305
+ * Pass NULL to convert_to_git() to stress this; the function
2306
+ * should never look at the index when explicit crlf option
2307
+ * is given.
2308
+ */
2309
+ convert_to_git (NULL , path , buf -> buf , buf -> len , buf , safe_crlf );
2280
2310
return 0 ;
2281
2311
default :
2282
2312
return -1 ;
@@ -3379,6 +3409,7 @@ static int load_patch_target(struct apply_state *state,
3379
3409
struct strbuf * buf ,
3380
3410
const struct cache_entry * ce ,
3381
3411
struct stat * st ,
3412
+ struct patch * patch ,
3382
3413
const char * name ,
3383
3414
unsigned expected_mode )
3384
3415
{
@@ -3394,7 +3425,7 @@ static int load_patch_target(struct apply_state *state,
3394
3425
} else if (has_symlink_leading_path (name , strlen (name ))) {
3395
3426
return error (_ ("reading from '%s' beyond a symbolic link" ), name );
3396
3427
} else {
3397
- if (read_old_data (st , name , buf ))
3428
+ if (read_old_data (st , patch , name , buf ))
3398
3429
return error (_ ("failed to read %s" ), name );
3399
3430
}
3400
3431
}
@@ -3427,7 +3458,7 @@ static int load_preimage(struct apply_state *state,
3427
3458
/* We have a patched copy in memory; use that. */
3428
3459
strbuf_add (& buf , previous -> result , previous -> resultsize );
3429
3460
} else {
3430
- status = load_patch_target (state , & buf , ce , st ,
3461
+ status = load_patch_target (state , & buf , ce , st , patch ,
3431
3462
patch -> old_name , patch -> old_mode );
3432
3463
if (status < 0 )
3433
3464
return status ;
@@ -3515,7 +3546,7 @@ static int load_current(struct apply_state *state,
3515
3546
if (verify_index_match (ce , & st ))
3516
3547
return error (_ ("%s: does not match index" ), name );
3517
3548
3518
- status = load_patch_target (state , & buf , ce , & st , name , mode );
3549
+ status = load_patch_target (state , & buf , ce , & st , patch , name , mode );
3519
3550
if (status < 0 )
3520
3551
return status ;
3521
3552
else if (status )
0 commit comments