This page describes how to create patch files from your repository, how to load them into Crucible so they can be reviewed, and how to use Crucible's Patch Anchoring to retrieve more context from the original file.

A patch file is a portion of the source code file that contains the code changes that you have made – it's a diff that shows the differences between your working copy and the base revision.

Crucible allows you to review a change before it has been committed. To do this, you upload a patch file to the Patch tab (or paste it in as text) when creating a review. You must first generate this patch file from your repository, using either commands built into your IDE, or with the repository command-line tools.

To create a patch in Perforce, you must ensure you have set P4DIFF to point to a GNU-compatible diff program.


By default, patch files will only show a few lines of code surrounding each change, rather than the entire file and its changes. Crucible's patch anchoring feature overcomes this limitation.

On this page:

 

Creating a patch file from your IDE

Creating a patch file in IntelliJ IDEA 7.0

To create a patch file under IntelliJ IDEA, do the following:

Select a parent folder, sub-folder or file that you have altered in the Project tool window. Select Version Control > Create Patch. The following window appears:

Screenshot: The IDEA Create Patch window

Click Create Patch. Choose a location to save the patch file and click OK.

If you do not have the Create Patch command available in IDEA

If you have not configured version control in IDEA, you may not have the Create Patch option available. If so, use the following steps to create a patch file in IDEA:

1. Select a parent folder, sub-folder or file that you have altered in the Project tool window, right-click it and choose Local History > Show History.

Screenshot: The IDEA Show History dialog

2. In the Local History view, right-click the revision number, and choose Create Patch.

Screenshot: The IDEA Create Patch dialog

3. In the Create Patch dialog, choose a location for the patch file and a file name, then click OK.

Creating a patch file in Eclipse 3.3.1.1

To create a patch file under Eclipse, do the following:

Find the parent folder, sub-folder or file that you have altered, right-click it and choose Team > Create Patch.

Screenshot: Creating a patch in Eclipse

In the Create Patch window, choose a location on your computer and type an appropriate file name (the file format is plain text):

Creating a basic patch file from the command line

CVS patch creation with the command line

To create a patch in CVS, use the cvs diff -Nu command from your workspace. For example:

cvs diff -Nu > patch.txt

Note that patch files created with this command will only include around three lines of code, before and after each change. It will include revision information by default, so if you have FishEye, you may be able to anchor the patch to get full context.

Subversion patch creation with the command line

To create a patch in Subversion, use the svn diff command from your workspace. For example:

svn diff > patch.txt

(warning) svn diff does not print any information about files copied in the workspace.

Note that patch files created with this command will only include around three lines of code, before and after each change. It will include revision information by default, so if you have FishEye, you may be able to anchor the patch to get full context.

Perforce patch creation with the command line

Use p4 diff -dcu to generate a patch for changed files. For example:

p4 diff -dcu > patch.txt

The "-dcu" option provides a combination of "context format" and "unified format". It provides the diff in a standard unified diff format (which we need to parse the diff) as well as revision information (which we need to anchor to FishEye).

Since Perforce diffs do not include added and deleted files so you should then do a p4 opened to find such files. For added files, in UNIX you can append them individually to the patch using GNU diff:

diff -u /dev/null path_to_added_file >> patch.txt

In the example above, replace path_to_added_file with the actual path of your added file. You can follow a similar procedure with deleted files using p4 print to extract the previous version of the file.

Note that patch files created with this command will only include around three lines of code, before and after each change. It will include revision information by default, so if you have FishEye, you may be able to anchor the patch to get full context.

Mercurial patch creation with the command line

Use hg diff to generate a patch for changed files. For example:

hg diff > patch.txt

This will include 3 lines of context. It will also include revision information, so if you have FishEye, you may be able to anchor the patch to get full context.

Note: if you use git style diffs (--git), the revision information will not be provided. This means that we cannot anchor the patch to a FishEye repository.

Git patch creation with the command line

Use git diff to generate a patch for changed files. For example:

git diff > patch.txt

This will include 3 lines of context. It will also include revision information, so if you have FishEye, you may be able to anchor the patch to get full context.

Creating patches that include all the code

To create a patch file that shows all lines of code in the file, as well as the changes, use the following commands from your repository command-line tools.

Creating a patch in CVS with all lines of code

To create a CVS patch that shows all code (not just the changes and surrounding code), use this command:

cvs diff -N -U 10000 > patch.txt

(info) The '10000' number refers to the number of code lines included in the patch, before and after each change. patch.txt represents your desired name for the new patch file.

Creating a patch in Subversion with all lines of code

To create a patch in Subversion that shows all code (not just the changes and surrounding code), use this command:

svn diff --diff-cmd diff -x "-U 10000" > patch.txt

Creating a patch in Perforce with all lines of code

(warning) Unfortunately, Perforce does not directly support creating patches that include all lines of code. A workaround is to checkout 'before' and 'after' versions of the file, and use GNU Diff to create a patch between the two files. That file could then be loaded into a Crucible review.

Using Crucible patch anchoring to automatically add full context

Crucible's Patch Anchoring feature allows you to add a regular patch (showing only a few lines of context) to a review. Then, Crucible will automatically search for the relevant file content in the connected repositories. When it finds the files, it will seamlessly add in more context from the file so that you can view all of the lines of code (greatly enhancing the review process).

To use patch anchoring:

  1. Create a new review. From the Tools menu in Crucible, select Create Review.
  2. Click Pre-Commit - Upload a patch file to be reviewed. In the dialog that appears, click Browse, locate your file, then click Upload. Crucible will now search for matches in the files in its database. Crucible will analyse all the paths in the patch, find the branches containing all those paths, then anchor the patch to the trunk or the branch with the most recent commit activity.

    (info) Crucible makes a 'best guess' in its processing – you should check that it has anchored the patch to the correct location in your repository.


    Screenshot: Crucible Patch Anchoring 

  3. You can click Edit to change the anchoring, either by choosing a new match or removing the anchor. You can also change the anchoring later, after the review is live.
  4. Start the review. When viewing the diffs, you will be able to choose more than three lines of context from the View menu.



Screenshot: Editing the settings for Patch Anchoring
 


Screenshot: Viewing more than three lines with Patch Anchoring