After you have obtained the source code, made changes to it, built and tested it (and included your tests in your patch, if possible), you'll want to get your changes reviewed and checked in. In order to do that, you need to create a file listing the changes you made, called a patch or a diff file. You can generate it using cvs diff or hg diff command.
This article is written about CVS; for Mozilla 2 (ie everthing after 1.9.0), we switched to another source control system - Mercurial. While the ideas are the same, the specific commands and tips may not apply anymore. See Mercurial FAQ#How can I diff and patch files? for the complete list of hg commands and tips.
Note that if you want to contribute your change, you will be asked to generate the diff against the latest version of the code (CVS HEAD, also known as "trunk", or the tip of an active branch for some patches).
People who need to apply your patch will be grateful if you generate it by running <tt>cvs diff</tt> from the top-level <tt>mozilla/</tt> directory. (In this case they can apply it by running <tt>patch -p0 < your_patch</tt> from the top-level directory without looking at the patch.)
Creating a diff of a single file
In order to create a diff of a single local file against the current file in the repository, use:
$ cvs diff -u8p FILENAME
$ hg diff -p -U 8 FILENAME
This creates a diff in the so called 'unified' format (<tt>-u</tt>), with 8 lines of context. The diff is printed to stdout by default. To redirect the output to a file, use something like:
$ cvs diff -u8p FILENAME > OUT_FILE
$ hg diff -p -U 8 FILENAME > OUT_FILE
Creating a diff for multiple files
If, instead of using a regular file for FILENAME, you provide a directory, then this directory as well as all subdirectories will be searched recursively. For example
$ cvs diff -u8p mozilla/<restOfPath>/ > OUT_FILE
$ hg diff -p -U 8 mozilla/<restOfPath>/ > OUT_FILE
will compare all files in the current directory and all its subdirectories against the versions in the repository, and write the combined unified diffs to a file named OUT_FILE, using 8 lines of context.
There should be enough context in the patch for it to be understood without opening the source file. The default guideline is to use 8 lines of context; if more context is required to make the patch understandable, replace 8 with a higher number. Also note that the more context you include, the greater the chance to be able to also apply the patch to a file which differs heavily from the original source against which the diff was prepared.
Including new files in a patch
To include a new file in your patch, use the <tt>-N</tt> option:
$ cvs diff -u8pN mozilla/<restOfPath>/ > OUT_FILE
A common problem here is that cvs diff will not include the new files that were not cvs added, and cvs add requires write access to repository.
The solution is to use the cvsdo utility, which edits <tt>CVS/Entries</tt> to make cvs think the file is added to repository:
$ cvsdo add NEWFILE $ cvs diff -u8pN NEWFILE > OUT_FILE
Creating a patch that contains files in new directories is a bit trickier. First, create a diff of everything NOT in a new directory:
$ cvsdo add newfiles $ cvs diff -u8pN NEWFILES > OUT_FILE
Now, you will have to
cvsdo add, each new directory, and then the files in that directory.
$ cvsdo add mozilla/<restOfPath>/newdir/ $ cvsdo add mozilla/<restOfPath>/newdir/newfile
Then you can
cvsdo diff on each new directory, appending the changes to your diff.
$ cvsdo diff mozilla/<restOfPath>/newdir/ >> OUT_FILE
Patches with lots of whitespace changes
When generating a patch you can ask <tt>diff</tt> to ignore whitespace changes. This is especially useful if you are doing a lot of indentation changes, such as when wrapping or unwrapping parts of the code inside
if statements. To create a patch without whitespace changes use the <tt>-w</tt> flag. So if you use:
$ cvs diff -u8pN mozilla/<restOfPath>/ > OUTFILE
for your original patch, then do another patch with
$ cvs diff -u8pNw mozilla/<restOfPath>/ > OUTFILE-w
When doing so, please make sure that both patches are attached to the bug (the patch without <tt>-w</tt> is needed for the reviewer to check that whitespace changes are done correctly and for the person doing the check-in for you to apply your changes).
Automated review tools
There are some tools available that may help to catch some errors within your patch thus making your and your reviewer's job a little easier, here are some that may be useful: JST Review Simulacrum.