Last updated April 18, 2013.

You can use a patch program to apply a patch, but you don't need to. Doing it manually is perfectly ok for smaller patches, and gives you a better idea of what's going on in your code.

As said in the main article, don't patch your live site directly! Instead, patch your test site, test it, then upload the changes.

Example

The given patch could begin with something like this,

diff --git a/modules/system/system.admin.inc b/modules/system/system.admin.inc
index fb0664a..193c475 100644
--- a/modules/system/system.admin.inc
+++ b/modules/system/system.admin.inc

This tells you which file you need to edit: modules/system/system.admin.inc.

@@ -911,12 +911,6 @@
  * Array sorting callback; sorts modules or themes by their name.
  */
function system_sort_themes($a, $b) {
-  if ($a->is_default) {
-    return -1;
-  }
-  if ($b->is_default) {
-    return 1;
-  }
+ // added a comment
   return strcasecmp($a->info['name'], $b->info['name']);
}

Here is the first snippet that you need to change.
911,12 is the line number and number of lines of the snippet before the patch is applied.
911,6 is the line number and number of lines of the snippet after the patch is applied.
  • Lines beginning with "-" are deleted.
  • Lines beginning with "+" are added (without the "+", of course).
  • Lines beginning with nothing remain as they are. Their only purpose is to give you an idea where you are in the code.

Please note: If you want to be able to revert the patch with an automated tool at a later time, you should be careful that you don't insert additional linebreaks or comments.

On the other hand, you might want to mark the modifications with comments, to find them later on.

Finding modified code

If your code is checked out from git, you can use git to view the modifications.

Otherwise, you can download a clean copy of Drupal + contributed modules into a separate folder, and use a diff tool like WinMerge to compare the clean download with your modified code.

Comments

I am completely new to looking at code, so I'm still a little puzzled.

@@ -618,10 +618,6 @@ function _system_is_incompatible(&$incom

So this says to delete line 618 and replace it with something. What do you replace it with? What do the ,10 and ,6 mean?

What does it mean when the lines in the patch look like:
233c221

Where can I find more detailed instructions to read?

Thanks!

If you are new to looking at code, applying a patch manually is more work than you need to take on. I imagine you are likely to do more harm than good. There is an excellent beginners guide to patching written up here:

http://drupal.org/node/620014

These numbers are just pre-patch and post-patch line numbers. The minus and plus signs mean pre-patch and post-patch respectively. They don't have anything to do with deleting or adding lines, as they do outside of the @@ symbols.

The first number, -618,10, is the initial line number before the patch is applied. So the code referenced just after the 2nd @@ would start on line #618. 10 refers to the number of lines in the section prior to patching.

The 2nd number, +618,6 is the initial line number after the patch is applied. So in this case, function_system_is_incompatible would start on line 618 after the patch is applied. There would be only 6 lines of code in this section after patched as opposed to the original 10. As more functions are changed in a single patch, the gap between the pre-patch line # and the post-patch line # gets bigger.

added an explanation in the article.

Thank you for these clear instructions with example! I successfully applied a patch manually for the first time!