# Contributing Patches

## Using Gerrit at Eclipse

EGit and JGit projects are using Gerrit Code Review for Git based patch submission and review.

Parts of this chapter are also available in the Eclipse Gerrit wiki.

### User Account

- In order to contribute you need an Eclipse user account on eclipse.org, on creation of a new account you must agree to the Contributor Agreement.

### Legal Paperwork

Before your first contribution can be accepted, you need to electronically sign the Eclipse Foundation Contributor License Agreement (CLA). You only have to do this once, and it covers all Eclipse projects. The new process was discussed in bug 381105 and introduced on June 27, 2013.

Minimally, all Git commits you contribute must have the following:

- A single line summary in the comment field, followed by a more detailed descriptive paragraph;
- Your credentials (email address) captured in the "Author" field; and
- A "Signed-off-by" entry with matching credentials in the comment.
- The "Signed-off-by" entry is required. By including this, you confirm that you are in compliance with the Certificate of Origin.

In addition ensure

- that the contributed code is licensed under the project license (EPL for EGit and EDL for JGit). This is done by putting a copyright and license header into every new java file. See other existing project source files for the correct content.

With a valid CLA on file, the signed-off commit and the copyright and license header in place, we will be able to accept small patches (<1000 LoC) immediately. For larger patches, we will also have to create a contribution questionnaire for review by the Eclipse IP team, but this usually doesn't require additional actions from you.

To verify whether a contribution requires a CQ, use one of the following git commands to check:

- If it's committed: git log --shortstat
- If not committed: git diff --stat

These commands tell you the number of insertions(+), and deletions(-). If the total number of lines inserted (e.g. added) in a contribution is greater than 1000 (yes, this includes comments) then a CQ is required.

Find more details about how to contribute in Contributing via Git (for contributors) and Handling Git Contributions (for committers).

### Logon

#### Gerrit Web UI

Logon to the Gerrit Web UI at `https://git.eclipse.org/r/` using the email address you registered with your Eclipse (and Bugzilla) account and your Eclipse password.

#### Git over SSH

When accessing Gerrit over SSH from git or EGit use the username displayed here and upload your public SSH key to Gerrit here.

Gerrit SSH URl: `ssh://username@git.eclipse.org:29418/egit/egit.git`

#### Git over HTTPS

When accessing Gerrit over HTTPS from git or EGit use username and HTTP password displayed here

Gerrit HTTPS URl: `https://git.eclipse.org/r/p/egit/egit.git`

### SSH Keys

- Add one or more public SSH keys to Gerrit here.
- If you are absolutely certain you do not have keys already, you must create a public and private pair of SSH keys. It is strongly recommended that you use a passphrase.
- Generating SSH key pair on command line

    ssh-keygen -t rsa -C "your_email@youremail.com"

- Execute SSH once to accept the host key (or copy it from the registration web page)

    ssh -p 29418 username@git.eclipse.org

- Generating SSH key pair in Eclipse

### Doing Code Reviews with Gerrit

- Visit the Eclipse Gerrit Code Review instance to start reviewing,
- Register to watch projects if you want to be notified by email on new or updated changes pushed for review
- Adjust your Gerrit preferences to customize it to your needs
- See the Gerrit user guide for more information about using Gerrit.
- The EGit tutorial walks you through the basic steps of working with Gerrit and EGit.
- Use Gerrit queries to filter the review list for changes you are interested in:
  - EGit changes pending in review
  - JGit changes pending in review

### Using Gerrit with git command line:

- Upload your patch from Git to the target project:

JGit

    git push ssh://username@git.eclipse.org:29418/jgit/jgit.git HEAD:refs/for/master

EGit

    git push ssh://username@git.eclipse.org:29418/egit/egit.git HEAD:refs/for/master

- Visit the Eclipse Gerrit Code Review server to start reviewing

#### Adding a dedicated remote

Since git can have multiple remotes, you can define one to be used to refer to Gerrit to save typing. Inside a previously checked-out repository you can run:

    cd path/to/jgit
    git config remote.review.url ssh://username@git.eclipse.org:29418/jgit/jgit.git
    git config remote.review.push HEAD:refs/for/master

    cd path/to/egit
    git config remote.review.url ssh://username@git.eclipse.org:29418/egit/egit.git
    git config remote.review.push HEAD:refs/for/master

You can now submit review requests from either repository using:

    git push review

### Using Gerrit with EGit:

Eclipse will look for your private key in the SSH2 Home location specified in the General>Network Connections>SSH2 Preference Page. If your `id_rsa` private key makes use of the AES-128-CBC algorithm (view the file as text to confirm), Eclipse will need at least `com.jcraft.jsch 0.1.44` to make use of it.

- Clone the JGit and EGit repositories and select Gerrit Configuration... in the context menu of the remote "origin" in the Git Repositories view to configure pushing to the code review queue.
- Alternative approach: Add a new review remote in the Git Repositories view and select Gerrit Configuration... in the context menu of the remote
  - From the appropriate Remotes node, create a New Remote and choose to Configure for Push. A unique name should be chosen, review is suggested.
  - Changes committed to your local clone can now be pushed to Gerrit using the review Remote. You will be prompted for your private key's passphrase if Eclipse is looking for it in the right place.
- Instead of using the Gerrit Configuration... wizard you can do the configuration steps manually:
  - Change the main URI or Add a Push URI (your Gerrit user name must be used here)
    - `ssh://username@git.eclipse.org:29418/(project).git`
  - In the Ref mapping section, add a RefSpec specification of `HEAD:refs/for/master`

- Visit our Gerrit Code Review instance to start reviewing

### Using the Mylyn Gerrit Connector

The Mylyn Gerrit Connector can be installed from the Mylyn p2 repository, e.g. for juno from http://download.eclipse.org/mylyn/releases/juno.

It contains several useful features:

- Cloning from Gerrit and automatic configuration
  - The wizards "Import Projects from Git" and "Clone Git Repository" will offer the possibility to browse the list of repositories on Gerrit servers and to clone selected repositories. After cloning the Gerrit configuration will be done automatically.
- Importing Gerrit changes as Mylyn tasks
- Fetching patch sets directly from the task editor
- Reviewing changes in the task editor
- Submitting changes from the task editor

## Granularity of Changes

- Make small commits, as small as reasonable. This makes them easy to review.
- Each commit should have a commit message that explains very clearly what the commit sets out to achieve (unless this is abundantly clear from the code itself, which is basically only the case for trivial patches). Also, when you fix a bug then report which bug you fix. When there are deeper reasons for doing things the way the commit does, then explain these as well. This all is for the reviewers and yourself: the context of the commit is completely clear.
- Do not mix concerns in commits: have a commit do a single thing. This makes them reviewable 'in isolation'. The purpose of the commit is clear and can be understood easily by the reviewers and yourself.
- Do not break the build and tests for any commit: this is very important for bug hunting.
- Split your work into multiple smaller pieces of work (when possible) and implement each of these pieces in a series of commits.
- A series of commits should work towards a 'feature' in a clear way and only 'enable' the feature in the last commit of the series.
- In a series of commits first lay the groundwork and then build on that towards the feature.

### Branches

When working with Gerrit, you can create local branches as you wish. When you are ready to push your changes, only the commits from your branch are pushed and are converted to reviews on Gerrit. The branch name itself is not visible on Gerrit.

Do not mix unrelated changes in branches: When you encounter a bug while working on something then create a new branch to fix the bug. Make sure you base it on the state of the remote branch that you want your fix to go to, e.g. origin/master. If you have other changes that depend on the bug being fixed then rebase your work on that new branch.

Merge/Rebase: If you want your branch to include new commits from the remote repository, rebase your local branch. The reason for this is that in Gerrit, changes are reviewed one commit at a time, and modified until all review feedback has been addressed. This is different from a pull request workflow, where the combined changes are reviewed and feedback is addressed with additional commits.

## Coding standards

Eclipse has standards for how to write code.

Coding conventions

Use interface guidelines

These documents have links to other document. Browse through them without expecting to learn everything, just so you know roughly what areas and types of details they covert. When you are not sure about how to write a piece of code or design the user interface, these are the two first places to look at.

In addition there is all the worlds collective knowledge on how to write programs that shine. When there is a conflict, the Eclipse guide lines and conventions take precedence.

Breaking the rules is ok if there is a very good reason and you can tell us what that reason is.

In addition to these general rules, we regard performance high. If the EGit plugin is slow in any way, that is a bug and should be reported and fixed. Java isn't slow, but there is a lot of slow Java code.

### Braces for one-line statements

Before 3.7.0 both in JGit and EGit, the preferred coding style was to leave off braces around statements with one line (with some exceptions to this rule), e.g.:

    if (condition)
        doSomething();

Starting with 3.7.0 braces are mandatory independently on the number of lines, without exceptions. The old code will remain as is, but the new changes should use the style below:

    if (condition) {
        doSomething();
    }

The main reason to the change was to simplify the review process, coding guidelines and to make them more consistent with Eclipse code formatter, see bug 457592.

### Removing trailing whitespace

In JGit and EGit we have enabled the save action "Remove trailing white spaces on all lines" for Java sources. This works except for empty comment lines, see bug 414421.

As a workaround, use the following sequence of commands in the Java editor to trick the save action:

- remove the offending trailing whitespace
- the save action re-adds the trailing whitespace
- CTRL-Z (CMD-Z on Mac) removes the re-added whitespace without triggering the save action again

Another workaround is to use this little script from the command line to edit away trailing whitespace from changed lines.

## Commit message guidelines

- The commit message header should fit on one line and should start with an uppercase letter. A blank line separates it from the body of the message.
- The first line should be a clear and concise description about the change and should not end with a dot.
- Enter a newline before providing a more detailed description about the change.
- Format the commit message to have newline characters after every 60-70 characters.
- Find more reasoning about commit message formatting in "A Note About Git Commit Messages"
- Commit message footers (everything following the last blank line in the commit message) in Key: value format are used for additional commit meta data. Some tools especially Gerrit parse this meta data to provide additional functionality.
  - If there is an associated bug number in Bugzilla about it, it should come as a Bug: footer right before Gerrit's Change-Id entry (if available) or towards the end.
  - If a Contribution Questionnaire has been issued to initiate and track the review of contributed changes by the Eclipse Foundation's IP team the IPZilla bug number should be added as CQ: footer in the format shown below
  - A Gerrit Change-Id footer is required for all changes pushed to Gerrit (to enable pushing new patchsets for the same change), it should be added in the format shown below. Use the Gerrit commit message hook or EGit to add the Change-Id.
  - A "Signed-off-by" can be added at the end of the commit message (see example below). Note: at the moment this is not required but may be used to list all who modified (amended, rebased, cherry-picked) this change.

    Fix the commit dialog to respect the workbench's selection

    Originally, the commit dialog would automatically check off all
    files in the dialog. This behaviour contradicts a user's expectation
    because their selection in the workbench is completely ignored. The
    code has been corrected to only preselect what the user has actually
    selected.

    Bug: 12345
    CQ: 6031
    Change-Id: I71ac4844ab9d2f848352eba9252090c586b4146a
    Signed-off-by: Your Name <your.email@example.org>

## Copyright

When contributing patches, you have to update the copyright section at the beginning of the file if there is one. Please follow the style that is already present in the file. Some examples follow.

When there is only one copyright present (from a person or a company), like this:

    Copyright (C) 2010, 2011 Some Name <some@example.org>

Change it like this (notice the updated year):

    Copyright (C) 2010, YEAR Some Name <some@example.org> and others.

If there is a section `Contributors:` below the legal text and your change is more than a few lines, you can add your name there and optionally describe the change and link to a bug number. You can also start such a section if you contributed a significant change.

When there are multiple copyright entries there, add yours as a separate line. So, given this:

    Copyright (C) 2010 Some Name <some@example.org>
    Copyright (C) 2011 Other Name <other@example.org>

Add another line:

    Copyright (C) 2010 Some Name <some@example.org>
    Copyright (C) 2011 Other Name <other@example.org>
    Copyright (C) YEAR Your Name <you@example.org>

For new files, copy one of the existing headers and start the copyright section with your name.

Also see http://www.eclipse.org/legal/copyrightandlicensenotice.php for more information.

## Test before submitting

See the Manual alpha testing section for some advice about how to test you work yourself.

- Run all existing tests. It does not take very long.
- Pay attention to the Java and Eclipse SDK baselines. EGit requires only Java5 and Eclipse 3.5. You cannot use API's that are newer. We often see breakages because Java 6 API's are used.

Note: In order to test in Eclipse 3.5 (Galileo), consider building EGit with the `platform-galileo` maven profile (see the Maven Build Sequence for more details).

## Sending patches by mail

Although sending patches by mail is the approved way of interacting with, and asking feedback from, the Git project, please don't send patches via git send-email. Instead, please use git format-patch to generate the `mbox`, and then attach that to an item in bugzilla as per the above SUBMITTING_PATCHES guides.

If you're sending a work-in-progress for a review, be aware that you can also attach work-in-progress (or RFC) items to Bugzilla; it's not just for finished patches.

However, it's generally preferred that you send items which you want comments on via Gerrit as per Contributing_Patches, since Gerrit allows comments to be added in-line and allows the opportunity to send multiple versions of a patch after changes are made. Once a change has been submitted to Gerrit, you can then mail the developer mailing list with a request to review your change via URL or get Gerrit to send the mail on your behalf.