Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert line-endings for whole directory tree (Git)

Following situation:

I'm working on a Mac running OS X and recently joined a project whose members so far all use Windows. One of my first tasks was to set up the codebase in a Git repository, so I pulled the directory tree from FTP and tried to check it into the Git repo I had prepared locally. When trying to do this, all I got was this

fatal: CRLF would be replaced by LF in blog/license.txt. 

Since this affects all files below the "blog" folder, I'm looking for a way to conveniently convert ALL files in the tree to Unix line-endings. Is there a tool that does that out of the box or do I get scripting something myself?

For reference, my Git config concerning line-endings:

core.safecrlf=true core.autocrlf=input 
like image 664
Lunikon Avatar asked Aug 15 '11 17:08

Lunikon


People also ask

Does Git convert line endings?

This is a good default option. text eol=crlf Git will always convert line endings to CRLF on checkout. You should use this for files that must keep CRLF endings, even on OSX or Linux. text eol=lf Git will always convert line endings to LF on checkout.

How does Git store line endings?

Git store whole files as "blobs" and only do conversion when reading (checking out) or writing (indexing). Git does not store "neutralized lines". So yes, Git can save files with mixed line endings, but that's usually a bad practice to avoid.

How do I change Unix line endings in Windows?

Converting using Notepad++ To write your file in this way, while you have the file open, go to the Edit menu, select the "EOL Conversion" submenu, and from the options that come up select "UNIX/OSX Format". The next time you save the file, its line endings will, all going well, be saved with UNIX-style line endings.


2 Answers

dos2unix does that for you. Fairly straight forward process.
dos2unix filename

Thanks to toolbear, here is a one-liner that recursively replaces line endings and properly handles whitespace, quotes, and shell meta chars.

find . -type f -exec dos2unix {} \;

If you're using dos2unix 6.0 binary files will be ignored.

like image 99
Andy Avatar answered Sep 18 '22 17:09

Andy


Assuming you have GNU grep and perl this will recursively convert CRLF to LF in non-binary files under the current directory:

find . -type f -exec grep -qIP '\r\n' {} ';' -exec perl -pi -e 's/\r\n/\n/g' {} '+' 

How it Works

Find recursively under current directory; change . to blog or whatev subdirectories to limit the replacement:

find . 

Only match regular files:

  -type f 

Test if file contains CRLF. Exclude binary files. Runs grep command for every regular file. That's the price of excluding binaries. If you have an old grep you could try building a test using the file command:

  -exec grep -qIP '\r\n' {} ';' 

Replace CRLF with LF. The '+' with the second -exec tells find to accumulate matching files and pass them to one (or as few as possible) invocations of the command -- like piping to xargs, but without problems if file path contains spaces, quotes, or other shell meta characters. The i in -pi tells perl to modify the file in place. You could use sed or awk here with some work, and you'll probably change '+' to ';' and invoke a separate process for each match:

  -exec perl -pi -e 's/\r\n/\n/g' {} '+' 
like image 27
toolbear Avatar answered Sep 16 '22 17:09

toolbear