Patches

Introduction
Generating patches, files containing the difference between files, is the domain of diff programs. There are many different programs with this functionality; some stand-alone (diff), some integrated in IDE's (Eclipse, Xcode) or version control systems (CVS).

This page only deals with some basic principles using the command line utility diff.

Diff can be found on most UNIX systems and is included in the packages UnxUtils and Cygwin for use on Windows.

Standard diff
Although diff can output in diverse formats, the easiest to read is the unified format; simply use the -u switch to generate unified output.

Because patch readability is important for the review process it's best to add another switch: -p. This switch shows the function closest to the difference, making it easy to see what function changed. (alternately you can use the flag -F^function or in abbreviated form -F^f)

When you've modified multiple files in the source tree, use diffs ability to compare directories. Add the -r switch to instruct diff to recurse (sub)directories and add the -N switch to account for new or deleted files:

Applying the patch
You can apply the patch to a single file with the command:

Or to the whole subdirectory without defining the file:

The headers in the diff must match the files you are patching. Creating a patch to include newly added files against cvs

CVS
When executing the command:

in your cvsroot with files that you have newly added your .patch file will have entries like this: modules/user/user.meta modules/watchdog/watchdog.meta

This is because these files cannot be compared with files in the repository because CVS doesn't know they exist. For this reason you will have to tell CVS that these files are added by editing the ./CVS/Entries file in the directory where the new files are. /menu.css/1.3/Wed Aug 16 07:32:16 2006// /menu.module/1.81/Sun Aug 20 08:03:58 2006// /menu.meta/0/Initial menu.meta// /menu.install/0/Initial menu.install//

In this example we added the two new files menu.meta and menu.install giving them a version 0 and a comment that they are new. If you run cvs diff again you will see the contents of the new files in your patch file which can be patched by others who want to review your contribution.