@@ -85,8 +85,39 @@ static int is_binary(unsigned long size, struct text_stat *stats)
8585 return 0 ;
8686}
8787
88+ static void check_safe_crlf (const char * path , int action ,
89+ struct text_stat * stats , enum safe_crlf checksafe )
90+ {
91+ if (!checksafe )
92+ return ;
93+
94+ if (action == CRLF_INPUT || auto_crlf <= 0 ) {
95+ /*
96+ * CRLFs would not be restored by checkout:
97+ * check if we'd remove CRLFs
98+ */
99+ if (stats -> crlf ) {
100+ if (checksafe == SAFE_CRLF_WARN )
101+ warning ("CRLF will be replaced by LF in %s." , path );
102+ else /* i.e. SAFE_CRLF_FAIL */
103+ die ("CRLF would be replaced by LF in %s." , path );
104+ }
105+ } else if (auto_crlf > 0 ) {
106+ /*
107+ * CRLFs would be added by checkout:
108+ * check if we have "naked" LFs
109+ */
110+ if (stats -> lf != stats -> crlf ) {
111+ if (checksafe == SAFE_CRLF_WARN )
112+ warning ("LF will be replaced by CRLF in %s" , path );
113+ else /* i.e. SAFE_CRLF_FAIL */
114+ die ("LF would be replaced by CRLF in %s" , path );
115+ }
116+ }
117+ }
118+
88119static int crlf_to_git (const char * path , const char * src , size_t len ,
89- struct strbuf * buf , int action )
120+ struct strbuf * buf , int action , enum safe_crlf checksafe )
90121{
91122 struct text_stat stats ;
92123 char * dst ;
@@ -95,9 +126,6 @@ static int crlf_to_git(const char *path, const char *src, size_t len,
95126 return 0 ;
96127
97128 gather_stats (src , len , & stats );
98- /* No CR? Nothing to convert, regardless. */
99- if (!stats .cr )
100- return 0 ;
101129
102130 if (action == CRLF_GUESS ) {
103131 /*
@@ -115,6 +143,12 @@ static int crlf_to_git(const char *path, const char *src, size_t len,
115143 return 0 ;
116144 }
117145
146+ check_safe_crlf (path , action , & stats , checksafe );
147+
148+ /* Optimization: No CR? Nothing to convert, regardless. */
149+ if (!stats .cr )
150+ return 0 ;
151+
118152 /* only grow if not in place */
119153 if (strbuf_avail (buf ) + buf -> len < len )
120154 strbuf_grow (buf , len - buf -> len );
@@ -536,7 +570,8 @@ static int git_path_check_ident(const char *path, struct git_attr_check *check)
536570 return !!ATTR_TRUE (value );
537571}
538572
539- int convert_to_git (const char * path , const char * src , size_t len , struct strbuf * dst )
573+ int convert_to_git (const char * path , const char * src , size_t len ,
574+ struct strbuf * dst , enum safe_crlf checksafe )
540575{
541576 struct git_attr_check check [3 ];
542577 int crlf = CRLF_GUESS ;
@@ -558,7 +593,7 @@ int convert_to_git(const char *path, const char *src, size_t len, struct strbuf
558593 src = dst -> buf ;
559594 len = dst -> len ;
560595 }
561- ret |= crlf_to_git (path , src , len , dst , crlf );
596+ ret |= crlf_to_git (path , src , len , dst , crlf , checksafe );
562597 if (ret ) {
563598 src = dst -> buf ;
564599 len = dst -> len ;
0 commit comments