This article discusses applying patches in eclipse, including a feature introduced in the Eclipse Galileo. To take advantage of the examples in this article, you need to install Eclipse and have an available source code library, such as Subversion (SVN) or Concurrent versions System (CVS).
Problem
The Eclipse integrated Development Environment (IDE) facilitates work in a team environment by providing some functionality. These features allow you to integrate directly into the IDE with your source control management system. They not only enable you to obtain source code, view and commit changes, but also provide the ability to handle changes by applying patches to code.
Patches can be exchanged as files, which contain changes between versions of code that use the standard diff format. When created correctly, the patch file contains only the differences between the modified file and the files in your workspace. Not only does this make the patch file smaller, but it also makes it easier and more selective to use patches.
In a team development environment, it is sometimes necessary to share changes to the code base directly between developers. Different scenarios for using patch files include:
Changes from outside the team-for example, in open source code, changes may come from someone in the community.
For some reason, changes cannot be committed to the current source tree, such as significant changes that will affect compilation.
Changes are complex and need to be consolidated with other changes before being submitted to the source control system.
One advantage of a patch file is that it can be submitted as an attachment to an e-mail message or as a bug report. You can then apply the patch file to the source code to consolidate the modified code.
Patch Format Overview
Patches are written in a uniform diff format when patches are created in Eclipse. This means you can create a diff from CVS or SVN and apply them to your Eclipse project. It also means that you can rely on a standard format for patch files, so they can be shared easily. diff files have several formats.
Understanding the format of patch files is not important to understand how to apply them in eclipse, but having some knowledge of the diff file format used by Eclipse can help you troubleshoot problems and help you understand what happens when you apply patches.
For example, see the Simple Motorcycle class in Listing 1. In this article, it is a benchmark example. You will modify it and, with this example, you will see how the patch file appears in the following example.
Listing 1. Example Motorcycle class
package com.nathangood.examples;
public class Motorcycle {
private int cc;
private String model;
private String make;
private String year;
public String getModel() {
return model;
}
public int getCc() {
return cc;
}
public void setCc(int cc) {
this.cc = cc;
}
public void setModel(String model) {
this.model = model;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
}