What is a patch?

Last modified: October 10, 2009 - 07:48

A patch is a file that consists of a list of differences between one set of files and another. All code changes, additions, or deletions to Drupal core and contributed modules/themes between developers are done through patches.

The differences are presented in a structured, standard way, which means that a program (also named patch) can be used to apply the changes to another copy of the original file.

All changes to Drupal, big and small, can be represented by patches. Patches make development easier - instead of saying "I have this replacement file" which may have thousands of lines of code making it difficult to see what the actual differences are, the patch highlights the exact changes that were made for easy review.

In effect, a patch is a list of all the changes made to a file, which can be used to re-created those changes on another copy of that file.

Here is an example of what a patch looks like:

Index: includes/example.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/example.inc
diff -u -p -r1.445 theme.inc
--- includes/example.inc 24 Oct 2008 07:14:51 -0000 1.445
+++ includes/example.inc 25 Oct 2008 17:57:21 -0000
@@ -373,7 +373,7 @@ function example_function(&$parameter
-  // This is a comment in the original file. It will be removed when the patch is applied.
+ // And here's a line we added when we were editing the file. It will replace the line above when the patch is applied.

Here is an overview of what this patch is saying:

Index: includes/example.inc
===================================================================

  • This line explains which file is being modified; in this case, includes/example.inc.

@@ -373,7 +373,7 @@ function example_function(&$parameter

  • This line shows that the change is inside function example_function() around line 373.

-  // This is a comment in the original file. It will be removed when the patch is applied.

  • Remove this line.

+ // And here's a line we added when we were editing the file. It will replace the line above when the patch is applied.

  • Add this line. In this case, we're replacing the line of text with a whole new line. Sometimes the change might simply be the addition of a period, or a grammatical correction.

The rest of the patch file merely provides context as to where the change lies, to help when applying patches.

 
 

Drupal is a registered trademark of Dries Buytaert.