Drupal Patches Guide Highlights
source
Drupal » Developing for Drupal » Patches
Check your directory
One thing to keep in mind with patching contributed modules/themes vs. core patches is that not everyone puts contribs in the same location. So you should to do the diff from within the module's/theme's root directory rather than Drupal root.
Readability
Patch readability is very important for the review process and a patch not created with that in mind is likely to be rejected by reviewers. The two best options to accomplish this are the -u option for unified formatting and -p to show the function closest to the difference in the code, making it easy to see what function changed.
Separating your changes and whitespace
Separate each logical change into its own patch.
Line endings and directory separators
Use Unix line endings (LF) and directory separators (/). Line endings can be converted manually with many text editors or by piping diff output through a dos2unix utility.
The command
So the basic command to use is:
cvs diff -up original.php > filename.patch
or
diff -up original.php new.php > filename.patch
Note that the symbol > will redirect the output to the file filename.patch.
Applying patches
Provided that the patch was made relative to the Drupal root directory, navigate to the Drupal directory (using cd) and issue the command:
patch -p0 < path/file.patch
If the patch was not made relative to the Drupal 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.patch
Patch 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.
- An option so the origin file won't have the patch apply to itself:
- -o outfile
- --output=outfile
- Send output to outfile instead of patching files in place. Do not use this option if outfile is one of the files to be patched.
patch unexpectedly ends in middle of line
Applying some patches generates the error:
patch unexpectedly ends in middle of line
This error seems to occur when the patch file ends on a line that includes nothing but whitespace characters.
Patch can't find the target file
This usually means that you are either executing patch in the wrong directory or that the patch was made relative to another directory. Open the patchfile and locate the lines describing the changed files to determine the path:
--- modules/codefilter/codefilter.module Tue Mar 28 15:04:48 2006
+++ modules/codefilter/codefilter.module Sun Apr 23 15:51:32 2006
The above patch appears to be made relative to the drupal root directory so you can execute the following command from the Drupal root:
patch -p0 < file.patch
The number after -p determines the number of path elements that should be ignored. This is very useful when you need to apply a patch that was made against an old and new directory:
--- olddir/modules/codefilter/codefilter.module Tue Mar 28 15:04:48 2006
+++ newdir/modules/codefilter/codefilter.module Sun Apr 23 15:51:32 2006
You can apply this patch from the Drupal root by stripping the first path element; 'olddir' / 'newdir':
patch -p1 < file.patch
Reversing patches
You should also reverse a patch prior to adding a newer, updated version of the same patch. To reverse the patch, use the patch command with the -R option:
patch -p0 -R < path/file.patch
Coding style and security
If your code deviates too much from the Coding standards, it will be rejected without further review. You may want to check your code with the code-style.pl helper script or the Coder module to help you find style errors. Well commented/documented code will also be looked upon favorably. Make sure your code follows the security guidelines! Obviously code that introduces security issues will be rejected.
Verify your patch
- Your code works! Test your code.
- If your patch is just a quick hack, then don't set your issue to "patch (code needs review)" status, instead set it to "active" or "patch (code needs work)".
Submit your patch
Set the issue's status to "patch (code needs review)" or "patch (code needs work)". Setting the status to patch is important as it adds the issue to the patch queue.
