Drupal, Linux and Other Observations

Applying and Creating Patches in Drupal

From time to time it is necessary to supplement your Drupal installation with a patch to a contributed module or Drupal core itself.  While it may seem intimidating to the novice user, the entire process is very simple if you stay organized, make notes and follow the available instructions.  If you are a developer you should know that creating and submitting patches on d.o is equally painless and can be a huge help to the community.

The real trick is not breaking your production site because you just didn't feel like testing a patch in a development environment first.  It's really not that tricky...always backup your files and database before making any significant changes.  At least you can get things back up and running in the event that you break your site.

Since I recently submitted my first patch for a contributed module I thought it would be worthwhile to provide a starting point for folks on either side of the equation.  It's actually pretty cool to see the wheels of progress in motion. 

The Gist

Applying and creating patches in Linux is accomplished by using two simple commands.  In their simplest form, the diff command will generate a file containing the differences between two files or a group of files while the patch command will read a file generated by diff and transfer those differences to the target file(s).

To apply a patch to the current working directory use the following command (assumes that the patch was created from a folder with the same relative position to patched files as your current working directory):

> patch -p0 < path/file.patch

To create a patch navigate to the root folder of your project and use the following command:

> diff -up path/to/file/orignal path/to/file/new > mypatchname.patch

Fortunately, there are already some fantastic Drupal related resources that we can reference:

Applying Patches

If you have an issue with your website and have found a patch on d.o that provides a potential solution, don't be afraid to download it and follow the guidelines found here: http://drupal.org/patch/apply.  If you've properly done your backup tasks then you have nothing to worry about, right?  It is important to pay particular attention to which folder you use for saving and applying the patch. 

You may also wish to take a look at this page for a slightly different take (targeted to Windows users but informative): http://drupal.org/node/620014.

Creating Patches

If you are a developer who has fixed an issue and you see other people in the community who could also benefit from your effort, feel free to share it with the community by following the instructions found here: http://drupal.org/patch/create.  DO NOT underestimate how valuable a simple fix may be to someone out there having a technical issue.  Plus, contributing to the community can only help your Karma points.

Study Up