Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent EOL normalization for CSV files

I have a source repository that is used both from Windows and Linux.

I know that Git will automatically convert EOL to the local standard: \r\n on Windows and \n on Linux.

This is not an issue for source files.

But I have some CSV files that use a fixed format with a given EOL character (\r\n) which should not be changed, but Git converts them too, breaking some code.

I've tried to prevent EOL conversions for CSV files by creating a .gitattributes file at the root, next to the .gitignore file, with the following content:

*.csv    -text

I've applied what I've understood from: http://git-scm.com/docs/gitattributes

But Git is still converting \r\n to \n on Linux.

Should I play with another setting like auto.crlf?

Note that I have limited control of the Linux local repository as it is managed by the continuous integration server Jenkins.

Thanks for any input.

like image 299
Pragmateek Avatar asked Jan 27 '14 00:01

Pragmateek


People also ask

How to have Unix EOL instead of Windows EOL in CSV?

CR stands for Carriage Return and LF stands for Line Feed. Below is an example of an Unix EOL. There is just the LF character. FIX: Now if you require your CSV file to have Unix EOL instead of Windows EOL, just click Edit in the Notepad++ menu then click EOL Conversion. Then click on Unix (LF) to convert the whole file to Unix EOL.

Should EOL normalization be enabled by default?

Sorry, something went wrong. EOL normalization should be optional feature and off by default. We do not expect file editor will change non-edited code without user conscious. Sorry, something went wrong.

How does the current file model normalize line endings?

The current file model normalizes the line endings as soon as the file is read (i.e. each line does not keep track of its EOL), the model only keeps track of one EOL for the entire file (which is computed on file open as the predominantely used EOL character).

How do I enable line ending normalisation in VS Code?

Sign in to your account Upon saving a file edited with VS Code, it appears to perform line ending normalisation automatically and without user notification of any sort.


1 Answers

Please note that git uses LF as an internal representation of EOL.

This means that in your case, the *.csv files has got changed when they were added/committed.

So the solution goes roughly like this:

  1. remove all the *.csv files, commit that change
  2. edit .gitattributes, commit
  3. add back all the *.csv files, commit again

Actually, it can be all made in one commit, with the following commands:

### ... update .gitattributes
git rm --cached '*.csv'
### ... find -name '*.csv' -print0| xargs -0 unix2dos
git add '*.csv'
git add .gitattributes
git commit

Explanation:

  • git rm --cached removes all csv files from index, leaving them on the disk;
  • ensure the files have CRLF line endings (I'm using unix2dos as an example)
  • git add '*.csv' adds them back, this time without any transformation, according to new version of .gitattributes
like image 126
kasal Avatar answered Sep 23 '22 13:09

kasal