Saturday, March 6, 2010

svn and eol-style on Windows

Recently I had problems with one great tool - Resharper. Behaviour of this VS 2008 plugin in this particular situation wasn't perfect (showing boxes instead of text lines :), but it was easier to fix problem at our side than wait for resolution by developers.
Resharper doesn't like unix style (LF) or mixed style (somewhere LF, somewhere CRLF) EOLs (end of line-s). And our big solution had many C# source files with wrong (for windows platform) EOLs. So sometimes in some files refactoring or even just working with Resharper enabled was impossible.
I'm not sure about specific situation when our EOL's become damaged, but most likely this ocurred during migration from CVS to SVN. SVN contains built-in property to fix this problem (svn:eol-style set to native), so we only needed to:
1. Set autoproperties (most our developers are using tortoisesvn so it was easier for us to set tsvn:autoprops property one time for solution root, efficiently enabling autoproperties for all our working copies) to enable svn:eol-style option for all files added in future. So we set tsvn:autoprops property for project root to this value:
*.c = svn:eol-style=native
*.h = svn:eol-style=native
*.cpp = svn:eol-style=native
*.cs = svn:eol-style=native
*.strings = svn:eol-style=native
*.resx = svn:eol-style=native
*.csproj = svn:eol-style=native
*.sln = svn:eol-style=native
*.sql = svn:eol-style=native
*.config = svn:eol-style=native
*.build = svn:eol-style=native
*.tpl = svn:eol-style=native
2. Set svn:eol-style property for files already present at SVN repository. This was a tricky one (at least under windows), cause we needed to recurse all solution folders searching for .CS files, fix EOLs in them (otherwise svn won't accept eol-style property for these files), and set property. Ok, to make the script bellow work you'll need some client SVN binaries (I'd used sliksvn) and this my small regex processing tool (requires .Net 3.5sp1). Ok, contents of BAT file (I named it svnps.bat):
@echo off
for /R %%a IN (%1) DO (

echo ---- processing %%a
RegExRun.exe "(\r\n|\r|\n)" "\r\n" < "%%a" > "%%a.crlf"
rename "%%a" "%%~nxa.old"
rename "%%a.crlf" "%%~nxa"
del /F /Q "%%a.old"
"C:\Program Files\SlikSvn\bin\svn.exe" ps svn:eol-style native "%%a"

)
Place this bat file and RegExRun.exe to Program Files / Sliksvn / bin directory, go with command prompt (or FAR for example) to your SVN repository directory and run something like this:
"C:\Program Files\SlikSvn\bin\svnps.bat" *.cs
This will fix all non DOS (non Windows) EOLs in .CS files in current path (recursively) and set svn:eol-style=native for each of them. Together with autoproperties that should resolve all your (and our) problems :)

No comments:

Post a Comment