Creating a patch

  • Revision slug: Creating_a_patch
  • Revision title: Creating a patch
  • Revision id: 37792
  • Created:
  • Creator: Vlad.sukhoy
  • Is current revision? No
  • Comment Added -w flag to the patch creation routines (this improves patch readability).

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

{{ 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 -u8pw 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 -u8pw 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 -u8pw . &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 -u8pNw . &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 -u8pNw 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>{{ wiki.languages( { "fr": "fr/Cr\u00e9ation_d\'un_patch", "it": "it/Creare_patch", "ja": "ja/Creating_a_patch" } ) }}</a>
Revert to this revision