Git with autocrlf = true checks files with mixed as-is line endings

So, I always thought that when core.autocrlf=true

Git replaces all endings LF

CRLF

when checking out a file in the working directory.

From the Git book :

If you are on a Windows machine, set it to true - this will convert LF endings to CRLF when checking the code

However, when checking out a file with mixed line endings and core.autocrlf

on true

my version, Git checks the file as is.

I found a very handy GitHub repository to test this behavior - https://github.com/YueLinHo/TestAutoCrlf

Test results:

  • File with endings LF

    (LF.txt)
    • C autocrlf=false

      : outputted as-is (all line endings LF

      )
    • C autocrlf=true

      : All line endings are changed to CRLF

      at checkout

So far so good, everything as I expected. Now for a file with mixed line endings:

  • Mixed line endings file (MIX-more_CRLF.txt, MIX-more_LF.txt)
    • C autocrlf=false

      : outputted as -is (combination of LF

      and CRLF

      )
    • C autocrlf=true

      : checked out as-is (combination of LF

      and CRLF

      )

Why is this happening? I have not seen anything about autocrlf=true

not dealing with files with mixed line endings.

Are my Git settings a problem? I verified the installation core.autocrlf

by running git config --get core.autocrlf

in the repository folder, after checking with autocrlf=true

in the global .gitconfig, and the command returned true. There is no .gitattributes file to overwrite settings.

All tests were run in the Git version 1.9.5.msysgit.0

.

EDIT: Same behavior in the latest version of msysgit 1.9.5.msysgit.1

.

My initial problem is that I somehow managed to commit the mixed line ending file with only ending LF

, while being core.autocrlf

set to true

, that is, the file was unloaded as is, but CRLF

changed from changed to LF

. I am currently working from a different computer and cannot reproduce this behavior in my version of msysgit.

+3


source to share


2 answers


I am posting an answer that was deleted by its owner because I think it provides a better explanation. I have no idea why the author removed it, I think it is correct and I voted to restore it.

Apparently this behavior is hardcoded in Git and does not depend on core.safecrlf (and I tested this, the mixed files remained intact even if I installed git config core.safecrlf false

.

The original answer follows:


Autocrlf does not convert mixed line endings, as the Git source says:

https://github.com/git/git/commit/a0ad53c18100226cb1a138cb9b3bc3615170be8f

Note the comments here:



/* No "naked" LF? Nothing to convert, regardless. */

      

and

/* If we have any CR or CRLF line endings, we do not touch it */
/* This is the new safer autocrlf-handling */

      

Converting mixed line endings is not reversible, when this happened Git crashed.

So, if you want to automatically convert the line endings of your files, it might be a good idea to install the . gitattributes dealing with the end of the line. For example:

LF.txt eol=lf
CRLF.txt eol=crlf

      

+1


source


What is the meaning core.safecrlf

?



If the parameter is core.safecrlf

set to a value true

, then mixed string files will not be converted. (Since the conversion is not reversible if the line ends are mixed)

+2


source







All Articles