Fixing CRLF issues when applying patches in a multi-platform environment

In my role as a Habari developer, I have been increasing butting heads with the same issue. I do some development on my windows machine at home, create a patch file with TortoiseSVN and upload it to trac. Then, I arrive at my iBook at work and download my patch. When I go to apply it though, I get messages such as this:

physics-2004-19:~/Sites/habari bjohnson$ patch -p0 --dry-run < timelineSearch.patch (Stripping trailing CRs from patch.) patching file system/admin/comments.php (Stripping trailing CRs from patch.) patching file system/admin/entries.php (Stripping trailing CRs from patch.) patching file system/admin/js/admin.js Hunk #1 FAILED at 190. Hunk #2 FAILED at 230. Hunk #3 FAILED at 277. Hunk #4 FAILED at 385. 4 out of 4 hunks FAILED -- saving rejects to file system/admin/js/admin.js.rej

Patch is failing because it is not appropriately dealing with the way that Windows machines and Unix-based machines (including OS X) signify an end of line. On Windows, a newline is specified by a carriage return (CR) followed by a line feed (LF). On Unix, however, a newline is just a LF. The ‘patch’ command is somewhat intelligent in dealing with patch files created on a Windows machine, in that it converts all CRLFs in the diff file to LFs before applying the patch. The problem, however, is that the target file might also have CRLFs in it. This is frequently the case when working in a multi-platform development environment like so many open source projects. The fix: remove all CRLFs from the target files before applying the patch. An easy way to do so is with the ‘tr’ command. For example:

tr -d '\r' < admin.js > temp; rm admin.js; mv temp admin.js

Now apply your patch. Problem solved!


1 Responses to Fixing CRLF issues when applying patches in a multi-platform environment

  1. 73 Ali May 18, 2008 8:43pm

    Neat find Blake! I’ll fav this to remember it when I get my first mac ;)

Leave a Reply