Creating a patch

  • Revision slug: Creating_a_patch
  • Revision title: Creating a patch
  • Revision id: 37798
  • Created:
  • Creator: Vlad.sukhoy
  • Is current revision? No
  • Comment

Revision Content

After you obtained the source code, made changes to it, built and tested it, you might 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 command.

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

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

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 . > 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 . > 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 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

Note that this trick does not work for new directories; for those, cvs add actually has to modify the repository immediately, which does require write access.

Patches with lots of whitespace changes

When diffing 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 <tt>if</tt> statements. To create a patch without whitespace changes add the <tt>-w</tt> flag. So if you use:

$ cvs diff -u8pN . > OUTFILE

for your original patch, then do another patch with

$ cvs diff -u8pNw . > OUTFILE

When doing so, please make sure that both patches are attached onto the bug (the patch without <tt>-w</tt> is needed for the reviewer to check that whitespace changes are done correctly).

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.

{{ wiki.languages( { "fr": "fr/Cr\u00e9ation_d\'un_patch", "it": "it/Creare_patch", "ja": "ja/Creating_a_patch" } ) }}

Revision Source

<p>
</p><p>After you <a href="en/Mozilla_Source_Code_Via_CVS"> obtained the source code</a>, made changes to it, <a href="en/Build_and_Install">built</a> and tested it, you might want to get your changes <a class="external" href="http://www.mozilla.org/hacking/life-cycle.html">reviewed and checked in</a>. In order to do that, you need to create a file listing the changes you made, called a <i>patch</i> or a <i>diff file</i>. You can generate it using <b>cvs diff</b> command.
</p>
<h3 name="Creating_a_diff_of_a_single_file"> Creating a diff of a single file </h3>
<p>In order to create a diff of a single local file against the current file in the repository, use:
</p>
<pre class="eval">$ cvs diff -u8p FILENAME
</pre>
<p>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:
</p>
<pre class="eval">$ cvs diff -u8p FILENAME &gt; OUT_FILE
</pre>
<h3 name="Creating_a_diff_for_multiple_files"> Creating a diff for multiple files </h3>
<p>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
</p>
<pre class="eval">$ cvs diff -u8p . &gt; OUT_FILE
</pre>
<p>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.
</p><p>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.
</p>
<h3 name="Including_new_files_in_a_patch"> Including new files in a patch </h3>
<p>To include a new file in your patch, use the <tt>-N</tt> option:
</p>
<pre class="eval">$ cvs diff -u8pN . &gt; OUT_FILE
</pre>
<p>A common problem here is that <b>cvs diff</b> will not include the new files that were not <b>cvs add</b>ed, and cvs add requires write access to repository.
</p><p>The solution is to use <b>cvsdo</b> utility <a class="external" href="http://viper.haque.net/~timeless/redbean/">, which edits <tt>CVS/Entries</tt> to make cvs think the file is added to repository:
</a></p><a class="external" href="http://viper.haque.net/~timeless/redbean/">
<pre class="eval">$ cvsdo add NEWFILE
$ cvs diff -u8pN NEWFILE &gt; OUT_FILE
</pre>
<p>Note that this trick does not work for new directories; for those, <code>cvs add</code> actually has to modify the repository immediately, which does require write access.
</p>
<h3 name="Patches_with_lots_of_whitespace_changes"> Patches with lots of whitespace changes </h3>
<p>When diffing 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 <tt>if</tt> statements. To create a patch without whitespace changes add the <tt>-w</tt> flag. So if you use:
</p>
<pre class="eval">$ cvs diff -u8pN . &gt; OUTFILE
</pre>
<p>for your original patch, then do another patch with
</p>
<pre class="eval">$ cvs diff -u8pNw . &gt; OUTFILE
</pre>
<p>When doing so, please make sure that <b>both</b> patches are attached onto the bug (the patch without <tt>-w</tt> is needed for the reviewer to check that whitespace changes are done correctly).
</p>
<h3 name="Automated_review_tools"> Automated review tools </h3>
</a><p><a class="external" href="http://viper.haque.net/~timeless/redbean/">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: 
</a><a class="external" href="http://www.johnkeiser.com/cgi-bin/jst-review-cgi.pl">JST Review Simulacrum</a>.
</p>{{ wiki.languages( { "fr": "fr/Cr\u00e9ation_d\'un_patch", "it": "it/Creare_patch", "ja": "ja/Creating_a_patch" } ) }}
Revert to this revision