How can I convert all line endings to CRLF, LF, or CR during SVN operations
Asked Answered
F

5

24

So, you are all ready to do a big SVN Commit and it bombs because you have inconsistent line endings in some of your files. Fun part is, you're looking at 1,000s of files spanning dozens of folders of different depths.

What do you do?

Fairway answered 19/8, 2008 at 4:22 Comment(0)
P
32

I don't think the pre-commit hook can actually change the data that is being committed - it can disallow a commit, but I don't think it can do the conversion for you.

It sounds like you want the property 'svn:eol-style' set to 'native' - this will automatically convert newlines to whatever is used on your platform (use 'CRLF', 'CR' or 'LF' to get those regardless of what the OS wants).

You can use auto-properties so that all future files you create will have this property set (auto props are handled client-side, so you'd have to set this up for each user).

Pack answered 19/8, 2008 at 11:35 Comment(2)
The problem with svn:eol-style = native is that it'll fail if you have different line endings.Terti
As of SVN 1.8 auto props can now also be done server-side, which can fix this issue once and for all. See https://mcmap.net/q/103087/-how-to-avoid-mixed-eol-styles-in-a-svn-repository for detailsInnermost
T
6

First is to clean everything up. Are you on Windows or Unix/Linux/Mac?

If you're on Unix/Linux/Mac, you can try something like this:

$ find . -type f -name "*.java" -exec dos2unix {}\;

That's if you have dos2unix on your box. It's not on my Mac or any of the six Linux machines we have. Seems like we didn't install this particular package. Fortunately, it's easy enough to find.

Be careful using it because you don't want to to munge binary files.

Once you've cleaned everything up, you should put the svn:eol-style property on your files. Setting it to native will checkout the file with the correct line ending for your machine, but store them in Unix line ending format. The other three options are "LF" for Unix, "CRLF" for Windows, and "CR" for pre Mac OS X Macs. Most people find "native" to work out the best. The only problem with Native is that it won't check in a file with mixed line endings while "LF" and "CRLF" will.

Once you do that, you should get a pre-commit hook that will allow you to enforce line endings on particular files. Then, teach your developers to use autoproperties. The pre-commit hook will prevent any commits unless the property is placed on the file A developer gets their commit rejected once or twice, and they'll setup auto properties on their own.

Terti answered 15/3, 2012 at 20:20 Comment(1)
Shouldn't there be space before \; ?Federicofedirko
I
2

Add a pre-commit hook which parses the file content and performs the munging of CRLF/LF/CR/etc for you before it's written to SVN.

Inspect answered 19/8, 2008 at 4:39 Comment(1)
Sorry, a pre-commit hook can't change the files in your working directory. The pre-commit hook runs on the server, so it doesn't even have access to the working directory.Terti
T
2

You may consider using a command like Linux's dos2unix for the conversion. Being a Linux command, it is easy to use it in batch mode with scripts etc. I do not know whether there is an equivalent for other operating systems.

Therefrom answered 19/8, 2008 at 5:43 Comment(0)
T
0

you can use notepad++ to batch convert line endings. Make regex search:

([^\r])\n

and replace it with

$1\r\n

you then should choose a bunch of test files like:

*.xml;*.txt;*.csv;...asf.

this avoids that you accidently modify binary files

NOTE: the regex patterns skip empty lines, so you have to run a second replace job with \n\n and replace it with \n\r\n

Tiberias answered 10/1, 2020 at 11:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.