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 hg diff or cvs diff command.
This article was initially written about CVS; and after Mozilla 1.9 (which corresponds to Firefox 3), we switched to another source control system - Mercurial. While the ideas are the same, the specific commands and tips do not apply anymore. See these instructions for the basics of how to create a patch when using Mercurial.
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
cvs diff from the top-level
mozilla/ directory. (In this case they can apply it by running
patch -p0 < your_patch 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:
$ hg diff -p -U 8 FILENAME
$ cvs diff -u8p FILENAME
$ svn diff -x -u FILENAME
This creates a diff in the so called 'unified' format (
-u), with 8 lines of context in cvs and mercurial , but with svn it creates a 3 lines of context which is default . The diff is printed to stdout by default. To redirect the output to a file, use something like:
$ hg diff -p -U 8 FILENAME > OUT_FILE
$ cvs diff -u8p FILENAME > OUT_FILE
$ svn diff -x -u 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
$ hg diff -p -U 8 mozilla/<restOfPath>/ > OUT_FILE
$ cvs diff -u8p mozilla/<restOfPath>/ > OUT_FILE
$ svn diff -x -u 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
$ 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
CVS/Entries 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
diff 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
-w 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
-w 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.