Applying patches

Last modified: October 26, 2008 - 12:04

Applying patches, modifying files according to instructions in the patch file, is the domain of patch programs. There are many different programs with this functionality. some stand-alone (patch), some integrated in IDE's (Eclipse, XCode).

Warning: Patching is something that should never be done on your production site unless you have sufficient backup and testing performed. While patching itself is relatively easy, understanding the implications of a patch is not. Patching your system can lead to loss of data and/or site instabilities.

This page only deals with some basic principles using the command line utility patch. Patch can be found on most UNIX systems and is included in the packages UnxUtils and Cygwin for use on Windows. There is also a video on Applying patches to Drupal core in the videocasts section.

Provided that the patch was made relative to the Drupal root directory, navigate to the Drupal directory (using cd) and issue the command:

patch -p0 < path/file.patch

If the patch was not made relative to the Drupal root directory you can place the patch in the same directory as the file being patched and run the patch command without the -p option. To do so, cd to the directory and type:

patch < file.patch

Patch will usually auto-detect the format. You can also use the -u command line option to indicate a unified patch, and the -b option creates a backup copy of the file before modifying it. In case of problems, you can then easily restore the backup file.

You can also reverse the patch if you want to.

Additional details on patch command and parameters

allisterbeharry - June 6, 2007 - 01:13

Wikipedia has a comprehensive entry on patch including history and usage: http://en.wikipedia.org/wiki/Patch_%28Unix%29
If you're wondering what the p parameter is for; from the man pages for patch at http://www.die.net/doc/linux/man/man1/patch.1.html :

-pnum or --strip=num
Strip the smallest prefix containing num leading slashes from each file name found in the patch file. A sequence of one or more adjacent slashes is counted as a single slash. This controls how file names found in the patch file are treated, in case you keep your files in a different directory than the person who sent out the patch. For example, supposing the file name in the patch file was

/u/howard/src/blurfl/blurfl.c

setting -p0 gives the entire file name unmodified, -p1 gives

u/howard/src/blurfl/blurfl.c

without the leading slash, -p4 gives

blurfl/blurfl.c

and not specifying -p at all just gives you blurfl.c. Whatever you end up with is looked for either in the current directory, or the directory specified by the -d option.

http://www.die.net/doc/linux/man/man1/patch.1.html contains a list of all parameters for patch.

Patch for Win32

farzan - March 27, 2008 - 11:16

You can acquire Patch for Win32 via this URL:

http://gnuwin32.sourceforge.net/packages.html

Applying patches using Eclipse

GA - August 1, 2008 - 15:41

you can easily apply patches to your code using eclipse, here are the steps:

1) menu>windows>open perspective>others>team synchronizing
2) open the patch file, select all text and copy it to clipboard (Ctrl+ C)
3) menu>project>apply patch...
4) select clipboard, click next, select the file you want to patch, click finish or next to setup patching options.

to learn how to install and setup Eclipse for Drupal visit:
http://drupal.org/node/157609

 
 

Drupal is a registered trademark of Dries Buytaert.