mediatribe.net -- Drupal and Web Development

Notice: this post was last updated 6 years 27 weeks ago so it might be outdated. Please be cautious before implementing any of suggestions herein.

Using diff and patch for directories, and managing patches during Drupal upgrades.

Background: I was recently making a Drupal site where I needed (as is often the case) a visual text editor along with image- and file-upload capabilities. I tried using the wysiwyg module in conjunction FCKEditor 2.6.x, which is a good combination for that purpose.

Since this does not work out of the box, I followed the instructions here.

This procedure requires modifying files in the Wysiwyg module (residing in sites/all/modules) as well as the FCKEditor library itself (in sites/all/libraries).

The causes potential problems when the module and library are upgraded. My solution is to use patches. Here is how.

  • Put the original file in its place (e.g. site/all/modules/wysiwyg or sites/all/libraries/fckeditor)
  • Copy the entire directory (e.g. cp sites/all/modules/wysiwyg sites/all/modules/wysiwyg.patched)
  • Make the required changes files in the patched (e.g. wysiwyg.patched/) directory
  • I like to make a directory for all my patches on the system (e.g. mkdir patches)
  • For each of your patched directories, execute the diff command. Here is an example for the wysiwyg directory. the options "c" and "r" tell the diff program to provide context and apply recursively to subdirectories. Providing context is not required, although the idea here is to be able to apply the patch to upgrades of the code, which might be different than the original, in which case context is very helpful.
        diff -cr sites/all/modules/wysiwyg/ sites/all/modules/wysiwyg.patched/ > patches/wysiwyg_module_allow_file_uploads.diff
     
  • Test your patch by executing the patch command, which applies it to the original, in this case "sites/all/modules/wysiwyg", directory. Here is an example with the wysiwyg directory. The "p0" option tells the patch command to apply the patch to the same directory structure as when the patch was created (in this case sites/all/modules/wysiwyg), which makes sense. The default value for "p" (if you don't specify it) tells patch to ignore the directory structure and apply the patch, in this case, to "wysiwyg".
        patch -p0 < patches/wysiwyg_module_allow_file_uploads.diff
     
  • You can now get rid of the patched directory
  • Next time an upgrade is released for a directory you patched (say, wysiwyg), just back up your old directory, put the new one in its place, and apply the patch with the "patch -p0" command.

Resources

(note: I sometimes use the IMCE module to integrate image uploading with visual editors, which does not require any patching. In this case it was decided to use upload capabilities already integrated into FCKEditor because it is easier for the end user.)