Wiki

Clone wiki

VIT-Projects / crlfheadaches

The problem

Unix (ie Linux) stores text-files with LF for newlines.
DOS (ie WIndows) stores CRLF instead. See Newline

It would seem surprising that such a small thing can be such a big problem.

A picturesque description of the problem is here. More in links below.

The Solution

Solution has two parts -- part in your local machine config and part in the repo. Repo things are shared so others don't need to do it again if they pull/clone from a properly setup repo (but it needs to be properly setup!)

Machine Setup

Git setup

Everyone needs to do (on windows AND Linux ie ALL git using machines)

$ git config --global core.autocrlf false
$ git config --global core.safecrlf true

The --global makes it for all git repos Else you need to do this for all your repos -- better keep it global.

Other machine setup

  • The linux utility file will tell whether a file is LF CRLF or binary.
  • The linux utilities dos2unix and unix2dos (in ubuntu package dos2unix) can help convert.
  • Emacs can do both
    • convert C-x RET f
    • inform M-x describe-coding-system
  • The emacs choices are easiest on Windows (assuming you have emacs!)

Repo Setup

Every repo, every branch should have .gitattributes and .gitignore files.

.gitignore should typically have things that dont go into the repo eg

  • Executables
  • Objects -- *.o , *.obj
  • Libraries -- *.a , *.so , *.dll
  • Backup (of course these should be taken care of in editor itself)
  • Other generated (and regeneratable) files eg *.png
  • TAGS
  • etc

It's important to get .gitignore in place:

  • before first commit
  • before first build

else you could fatten your repo with object files, loading the server, slowing subsequent searches.

.gitattributes should describe every file that goes into your repo -- usually with a glob such as *.c matches every C file.
Majority of cases -- .c , .h .py .pov -- should be text
Some eg .pdf should be binary.

It is possible to have windows-always eol=crlf or Unix-always eol=lf text files (see /usr/share/doc/git/html/gitattributes.html -- gitattributes local doc -- needs git-doc apt-installed) but in general this is unnecessary because when the repo is cloned on a specific OS-type the text files will be rendered natively in the work-tree if explicitly listed as text.

The main point to get is that every file of your work-tree should be covered.

  • Those NOT to be under git-versioning should be in .gitignore
  • Those in git should be in .gitattributes with a specified format -- usually binary or text, but if needed unix-text ( eol=lf ) or dos-text ( eol=crlf )

Checking

We need to make sure a DOS-file does not allow to get git add on linux and likewise make sure a unix-file does not allow to get git add on windows. As follows:

  • Assuming that *.py is marked as text in .gitattributes, make say a foodos.py and foounix.py as two test DOS and Unix files.
    Check that the wrong one is NOT git add -able whereas the right one is.
  • If you get a fatal error on git add your setup is reasonably 'CRLF-safe'.
    Else expect problems in future, especially if moving between Windows and Linux.

Links

Updated