Last updated February 7, 2012. Created by ztyx on April 24, 2006.
Edited by jhodgdon, eliza411, mlncn, aspilicious. Log in to edit this page.
// These documents are out-of-date with current best practices
This information deals with applying patches via the unix "patch" command. For information on using git to apply patches, please see the git patch contributor guide.
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 -p1 < path/file.patchor
git apply path/file.patchNote: git apply will fail to do anything when used within a local checkout of a git repository (other than the one for the project the patch is made for), such as if you are patching a module that is within a site that is in Git version control. Use
patch -p1 < path/file.patch instead.
You can find more info about applying patches with Git in the Git handbook
The -p option tells patch how many leading prefixes to strip. For patches created using git, -p1 is normally the right option, and is the default for git apply. If that doesn't work, try either of the above commands with -p0 instead.
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.patchPatch 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.
Comments
Additional details on patch command and parameters
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
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
Thanks GA, very helpful :)
Regards,
Teodor Sandu
Patching and SELINUX
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.incThx
I'm running drupal on centos and ran into the exact same problem. Been banging my head against the wall for hours... Patch works fine now. Thanks a lot for the hint!
baby steps version
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
Thanks John.Kenney - Very
Thanks John.Kenney - Very usefull! :o)
Regards
Kenneth Nielsen, www.submit.dk/drupal
Kenneth, Submit.dk
...
pleasure. thanks for the acknowledgment.
Mac OS X command to apply patch with spaces rather than tabs
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 Mentor
http://drupal.org/user/168664
patch for signup
Apparently signup needs a patch to work but all the help I have seen has to do with applying the patch in a local environment. How do I aply the patch on by hosting server? FTP the patch to where? And then run update.php?
Running patch without shell access
I didn't have shell access and wanted to run a patch directly on a new install on my production site. (SimpleScripts install, easy to redo, at BH)
I used the run.php from this tutorial and it worked like a charm...
http://php-html.net/tutorials/how-to-write-a-php-script-to-run-shell-com...
I was even able to apply a diff to the patch first using patch because the patch was wrong.
(How much patch could a diff patch patch if a diff patch could patch patch?)
Hint: Make sure you delete the run.php when you are done. ;)
git and patches
Now that Drupal is using git, there are some different methods to apply patches:
If the patch was made using 'git diff' and the patch is to be made on a git clone of the module concerned, then the patch should be applied with the command
'git apply [patchname]'
If the patch was made using 'git diff' and the patch is to be made on an unpacked tarball, then the patch should be applied with the command
'patch -p1 < [patchname]'
In both cases the patch should be placed in the module directory.
If you are not sure how a patch was made, open it and if it has paths beginning with a/ and b/ then it's a git style patch.
Old CVS style patches (and patches made with diff) would be applied with 'patch -p0 < [patchname]' but they should no longer appear.
Hope this helps
-----------------
Bob Hutchinson
Midwales dot com
-----------------
Alternate form
People who get confused about the "
<" character and don't mind a little more typing may prefer this form:CVS-style patches:
patch -dPROJECT_DIR-p0 -iPATCHFILEGit-style patches:
patch -dPROJECT_DIR-p1 -iPATCHFILEThe directory for the project you are patching. For example,/var/www(for Drupal core) or/var/www/sites/all/modules/mymodule(for the mymodule module).The filename of the patch you just downloaded or copied.Good. — Fast. — Cheap.
(Pick any two.)
error applying patch
I'm attempting to apply the patch for the new Reference module for Drupal 7, and get this output:
patching file node_ref_views_3_d7.patch
Hunk #1 FAILED at 786.
1 out of 1 hunk FAILED -- saving rejects to file node_ref_views_3_d7.patch.rej
Anyone? Thanks!
Drupal 7 Core with Chaos Tools running on Linux.
error applying patch
Well as you can see the patch process failed, it looks like you are trying to patch a patchfile, something wrong there....
-----------------
Bob Hutchinson
Midwales dot com
-----------------
so...
What should the command be? I'm trying to apply this patch: http://drupal.org/node/962694
Cheers!
Dr.E
How to patch
Well the command would be
patch -p2 < node_ref_views_3_d7.patch
with the patch in the node_reference folder. However the current D7 dev version of the references module is dated 2011-03-30 23:47 whereas the patch was made on a file dated 21 Jun 2010 21:38:23 and node_reference.module has changed considerably since then so the patch will fail anyway.
You could add the two functions in the patch manually but I'd be surprised if they work and views support appears to be there already.
-----------------
Bob Hutchinson
Midwales dot com
-----------------