views:

390

answers:

2

I have a Git repository (originally CVS, then SVN, now Git) containing a Rails project that has been deployed on Linux for a while now. Everything seems to run fine.

Now that I've converted to git, I see that many of my files in the repository contain CRLF line endings. I'd love for it to all be consistent (LF), but not at the expense of loosing the edit history of every file that has CRLF line endings.

Can you think of any reason I can't leave the files as they are? I seem to remember there being a problem with shell scripts or cron files or something that didn't respond to CRLF very well.

Also, I know all about the Git options core.autocrlf and core.safecrlf, But is there some way to have it convert all text files from CRLF to LF on checkout (for the linux side) ... i.e. a core.autolf option or something similar?

+4  A: 

If it is ok for you to rewrite your repository's history (see problems with rewriting history) you could use git filter-branch to convert CRLF to LF:

git filter-branch --tree-filter 'find . -path './.git' -prune -o -type f -exec dos2unix \{} \;' HEAD

Note that if you have binary files in your repository you will have to refine the find command to exclude them.

davitenio
Does that command know to leave binary files alone?
Baxissimo
Just tried it with dos2unix from Ubuntu's tofrodos package (version 1.7.6-2) and it does not leave binary files alone. So the find command should be refined to exclude binary files if you have any in your repository.
davitenio
Yeah, this would work if I can only find a good way to make sure and leave binary files alone.
Daniel Beardsley
A: 

A comment to davitenio's answer and Daniel Beardsley's comment; I believe you could use this little program as a wrapper around dos2unix:

#!/bin/sh
for f in $@; do
    if [ $(file -b -n -i -m /dev/null $f | grep -c "text") -gt 0 ]; then
        dos2unix $f
    fi
done

although there is probably still some corner case that will fail.

RolKau