Friday, January 14, 2011

Merging in git while ignoring newlines (or, Convert newline style to match file-by-file)

Basically, I want to merge two commits that are mostly the same, except some of the files have different newline endings.
Basically my problem looked like this.
      
     First commit with strange line endings
       | |
       V
A-B-C-D-E-F-G

        H
        ^
        ||
   Second commit with strange line endings.

I wanted to rebase H on top of C, and then merge with G. but C and H had differing newline endings, and there was no order to which files had which type.  So my solution was to hack together an absolutely horrible script that went through each file in H and converted it to UNIX or DOS newline styles, depending on which one would cause it to have a smaller diff with the same file in C.


Here it is:

    find . \! -type d -exec sh -c "unix2dos -q '{}';DOS=\$(git diff COMMITC -- '{}' | wc -c); dos2unix -q '{}'; UNIX=\$(git diff COMMITC -- '{}' |wc -c); if \[ \$DOS -lt \$UNIX ]; then unix2dos '{}'; fi" \;

You execute it with commit H checked out, and COMMITC replaced with the name of your "commit C" in my diagram.

No comments:

Post a Comment