Applying patches

Last modified: January 16, 2010 - 19:14

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 IDEs (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 root directory of the concerned project, navigate to the that directory (using cd). For a patch on Drupal, that will be the Drupal directory; for a contrib module or theme, that is the root directory of the project. Once there, issue the command:

patch -p0 < path/file.patch

If the patch was not made relative to the project's 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.

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

Thanks

teodor.sandu - July 16, 2010 - 08:11

Thanks GA, very helpful :)

Regards,
Teodor Sandu

Patching and SELINUX

rpanna - July 22, 2009 - 22:58

When I run patches I usually run into a problem where the patch seems to mess up my site. However the problem comes from the fact that I have SELINUX enabled. When I run the patch it changes the security context for the patched file and it need to be reset.

I usually get an error message something like this:

PHP Fatal error:  require_once() [<a href='function.require'>function.require</a>]: Failed opening required 'modules/node/node.pages.inc'

To change the context of the patch file go to that folder and run "ls -Z" to view the context of the files in the folder. Next copy the context from a file that was not patched to the patched files like this:

chcon --reference=filewithpropercontext.inc newlypatchedfile.inc

baby steps version

john_kenney - February 20, 2010 - 16:16

took me ages to understand the process described in these pages. many important details are not well described.

i wrote a baby steps version here: http://drupal.org/node/620014

Mac OS X command to apply patch with spaces rather than tabs

EvanDonovan - April 2, 2010 - 17:32

pwolanin in #drupal IRC said that on Mac OS X, you can apply a patch using tab2space -unix -t2 foo.patch | patch -p0, and the code style of the patch will be fixed to use 2 spaces (as per the Drupal coding standards) rather than tabs. It will also have Unix-style line endings.

 
 

Drupal is a registered trademark of Dries Buytaert.