How do I apply a diff patch on Windows?
There are plenty of programs out there that can create a diff patch, but I'm having a heck of a time trying to apply one. I'm trying to distribute a patch, and I got a question from a user about how to apply it. So I tried to figure it out on my own and found out that I have no clue, and most of the tools I can find are command-line. (I can handle a command line, but a lot of people would be lost without a nice, friendly GUI. So those are no good for this purpose.)
I tried using TortoiseSVN. I have the patch I'd like to apply. I right-click on the patch, and there's an option under the TortoiseSVN submenu that says "Apply patch." All it does is pull up an empty window.
So I tried hitting Open. It has two options: merge and apply unified diff. (The patch is in unified diff format, luckily.) But the apply option just plain doesn't work: It asks for the patch and a folder. Somehow it forgot to ask for the file to apply the patch to! So TortoiseSVN just plain doesn't work. Is there a Windows GUI-based utility that will take a patch and a file and apply it properly?
EDIT: Looking at the replies so far, it seems that Tortoise will only do it right if it's a file that's already versioned. That's not the case here. I need to be able to apply a patch to a file that did not come out of an SVN repository. I just tried using Tortoise, because I happen to know that SVN uses diffs and has to know how to both create them and apply them.
Solution 1:
Apply Patch
With TortoiseMerge:
- Find and open an existing SVN repo directory
- Create a new directory named "merges", if it does not exist already
- Copy the file onto which you want to apply the .patch file
- ADD and COMMIT to the svn repository before you continue to the next step
- Right click on merges and choose Apply patch...
- Double click the file from list
- The patched file with diff is displayed on the right pane
- Click on that pane and hit Save or export with File->Save As...
Alternative screeny if you Open from TortoiseMerge. In the screeny below, directory refers to the "merges" directory mentioned at step 2 above:
Screenshot of WinMerge GUI:
Solution 2:
I made pure Python tool just for that. It has predictable cross-platform behavior. Although it doesn't create new files (at the time of writing this) and lacks a GUI, it can be used as a library to create graphic tool.
UPDATE: It should be more convenient to use it if you have Python installed.
pip install patch
python -m patch
Solution 3:
TortoiseMerge is a separate utility that comes bundled with TortoiseSVN.
It can also be can be downloaded separately in the TortoiseDiff.zip archive. This will allow you to apply unified diffs to non-versioned files.
Solution 4:
I know you said you would prefer a GUI, but the commandline tools will do the work nicely. See GnuWin for a port of unix tools to Windows. You'd need the patch command, obviously ;-)
You might run into a problem with the line termination, though. The GnuWin port will assume that the patchfile has DOS style line termination (CR/LF). Try to open the patchfile in a reasonably smart editor and it will convert it for you.