Tuesday, July 14, 2009

Applying code patches in Windows Vista

(I'm speaking of .patch files, BTW) =)

In Vista it was particularly frustrating figuring out how to do this in Windows:
   patch -p0 <patchfile
...something that's pretty simple in the UNIX shell of your choice.

I started by installing GNU Patch for Windows... but Vista didn't want to run it because of Windows UAC. Also, to me it seemed too cumbersome to have to open up a Windows commmand prompt JUST to apply patches, when I was doing everything else from the Windows Explorer UI thanks to the awesome TortoiseCVS.

So, here's what I did:
  1. Install GNU Patch for windows, using the Binaries ZIP file; I installed it in C:\Program Files
  2. Make sure patch.exe now lives in c:\program files\patch\bin\patch.exe
  3. Create the .bat file c:\program files\patch\bin\patch.bat with these commands:
    @echo off
    "c:\program files\patch\bin\patch" -p0 < %1
    pause
    
  4. Create two .manifest files to keep UAC happy:

    c:\program files\patch\bin\patch.exe.manifest
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
    <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> 
    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2"> 
    <security> 
    <requestedPrivileges> 
    <requestedExecutionLevel level="asInvoker"/> 
    </requestedPrivileges> 
    </security> 
    </trustInfo> 
    </assembly> 
    

    c:\program files\patch\bin\patch.bat.manifest
    With the exact above contents of patch.exe.manifest

  5. Associate .batch files with the new patch.bat file. You can do this by creating an empty file and renaming it something.patch. On Windows Explorer, right-click this new file and use the "Open with" option, and tell Windows to always use c:\program files\patch\bin\patch.bat to open this file type.
There! Now, when you double-click on .patch files from Windows Explorer, a command line will open up with the Patch results, and wait for a keystroke before closing.

Tip: Always remember to place your patches in the correct directory!