Monday, May 20, 2013

Trouble with Windows patch files and projects on Linux

I was having a rough time patching a project in Debian Linux using a patch file generated in Windows using svn diff. There were many errors of the type Hunk #1 FAILED.

From this forum thread, I got the idea that it might be the windows style line endings that were causing the errors. I proceeded to run dos2unix on my patch file but the errors persisted when running patch -p0 < path/to/patchfile.

On further investigation, it emerged that the project I was patching was also using Windows style line endings. The following command changed all the files that are listed in the patch file to Unix line endings.

cat file.patch | grep ^Index | cut -d: -f2 | xargs dos2unix

New files in the patch are gracefully skipped by dos2unix. After this, the patch succeeded.