## Introduction

This document explains how to contribute changes to the Go project. It assumes you have installed Go from source:

    $ git clone https://go.googlesource.com/go
    $ cd go/src
    $ ./all.bash

(Note that the `gccgo` frontend lives elsewhere; see Contributing to gccgo.)

## Discuss your design

The project welcomes submissions but please let everyone know what you're working on if you want it to become part of the main repository.

Before undertaking to write something new for the Go project, send mail to the mailing list to discuss what you plan to do. This gives everyone a chance to validate the design, helps prevent duplication of effort, and ensures that the idea fits inside the goals for the language and tools. It also guarantees that the design is sound before code is written; the code review tool is not the place for high-level discussions.

In short, send mail before you code. And don't start the discussion by mailing a change list!

## Testing redux

You've written and tested your code, but before sending code out for review, run all the tests for the whole tree to make sure the changes don't break other packages or programs:

    $ cd go/src
    $ ./all.bash

(To build under Windows use `all.bat`.)

After running for a while, the command should print "`ALL` `TESTS` `PASSED`".

## Code review

Changes to Go must be reviewed before they are submitted, no matter who makes the change. (In exceptional cases, such as fixing a build, the review can follow shortly after submitting.) A custom git command called `git-review`, discussed below, helps manage the code review process through a Google-hosted instance of the code review system called Gerrit.

### Set up authentication for code review

The Git code hosting server and Gerrit code review server both use a Google Account to authenticate. You therefore need a Google Account to proceed. (If you can use the account to sign in at google.com, you can use it to sign in to the code review server.) The email address you use with the code review system will be recorded in the change log and in the `CONTRIBUTORS` file. You can create a Google Account associated with any address where you receive email.

Visit the site go.googlesource.com and log in using your Google Account. Click on the "Generate Password" link that appears at the top of the page.

Click the radio button that says "Only `go.googlesource.com`" to use this authentication token only for the Go project.

Further down the page is a box containing commands to install the authentication cookie in file called `.gitcookies` in your home directory. Copy the text for the commands into a Unix shell window to execute it. That will install the authentication token.

(If you are on a Windows computer, you should instead follow the instructions in the yellow box to run the command.)

### Register with Gerrit

Now that you have a Google account and the authentication token, you need to register your account with Gerrit, the code review system. To do this, visit golang.org/cl and log in using the same Google Account you used above. That is all that is required.

### Install the git-review command

Now install the `git-review` command by running,

    go get -u golang.org/x/review/git-review

Make sure `git-review` is installed in your shell path, so that the `git` command can find it. Check that

    $ git review help

prints help text, not an error.

Note to Git aficionados: The `git-review` command is not required to upload and manage Gerrit code reviews. For those who prefer plain Git, the text below gives the Git equivalent of each git-review command. If you do use plain Git, note that you still need the commit hooks that the git-review command configures; those hooks add a Gerrit `Change-Id` line to the commit message and check that all Go source files have been formatted with gofmt. Even if you intend to use plain Git for daily work, install the hooks in a new Git checkout by running `git-review` `hooks`).

### Set up git aliases

The `git-review` command can be run directly from the shell by typing, for instance,

    $ git review sync

but it is more convenient to set up aliases for `git-review`'s own subcommands, so that the above becomes,

    $ git sync

The `git-review` subcommands have been chosen to be distinct from Git's own, so it's safe to do so.

The aliases are optional, but in the rest of this document we will assume they are installed. To install them, copy this text into your Git configuration file (usually `.gitconfig` in your home directory):

    [alias]
    	change = review change
    	gofmt = review gofmt
    	mail = review mail
    	pending = review pending
    	sync = review sync

### Understanding the git-review command

After installing the `git-review` command, you can run

    $ git review help

to learn more about its commands. You can also read the command documentation.

### Switch to the master branch

Most Go installations use a release branch, but new changes should only be made based on the master branch. (They may be applied later to a release branch as part of the release process, but most contributors won't do this themselves.) Before making a change, make sure you start on the master branch:

    $ git checkout master
    $ git sync

(In Git terms, `git` `sync` runs `git` `pull` `-r`.)

### Make a change

The entire checked-out tree is writable. Once you have edited files, you must tell Git that they have been modified. You must also tell Git about any files that are added, removed, or renamed files. These operations are done with the usual Git commands, `git` `add`, `git` `rm`, and `git` `mv`.

If you wish to checkpoint your work, or are ready to send the code out for review, run

    $ git change <branch>

from any directory in your Go repository to commit the changes so far. The name <branch> is an arbitrary one you choose to identify the local branch containing your changes.

(In Git terms, `git` `change` `<branch>` runs `git` `checkout` `-b` `branch`, then `git` `branch` `--set-upstream-to` `origin/master`, then `git` `commit`.)

Git will open a change description file in your editor. (It uses the editor named by the `$EDITOR` environment variable, `vi` by default.) The file will look like:

    # Please enter the commit message for your changes. Lines starting
    # with '#' will be ignored, and an empty message aborts the commit.
    # On branch foo
    # Changes not staged for commit:
    #	modified:   editedfile.go
    #

At the beginning of this file is a blank line; replace it with a thorough description of your change. The first line of the change description is conventionally a one-line summary of the change, prefixed by the primary affected package, and is used as the subject for code review mail. The rest of the description elaborates and should provide context for the change and explain what it does. If there is a helpful reference, mention it here.

After editing, the template might now read:

    math: improved Sin, Cos and Tan precision for very large arguments

    The existing implementation has poor numerical properties for
    large arguments, so use the McGillicutty algorithm to improve
    accuracy above 1e10.

    The algorithm is described at http://wikipedia.org/wiki/McGillicutty_Algorithm

    Fixes #159

    # Please enter the commit message for your changes. Lines starting
    # with '#' will be ignored, and an empty message aborts the commit.
    # On branch foo
    # Changes not staged for commit:
    #	modified:   editedfile.go
    #

The commented section of the file lists all the modified files in your client. It is best to keep unrelated changes in different change lists, so if you see a file listed that should not be included, abort the command and move that file to a different branch.

The special notation "Fixes #159" associates the change with issue 159 in the Go issue tracker. When this change is eventually submitted, the issue tracker will automatically mark the issue as fixed. (There are several such conventions, described in detail in the GitHub Issue Tracker documentation.)

Once you have finished writing the commit message, save the file and exit the editor.

If you wish to do more editing, re-stage your changes using `git` `add`, and then run

    $ git change

to update the change description and incorporate the staged changes. The change description contains a `Change-Id` line near the bottom, added by a Git commit hook during the initial `git` `change`. That line is used by Gerrit to match successive uploads of the same change. Do not edit or delete it.

(In Git terms, `git` `change` with no branch name runs `git` `commit` `--amend`.)

### Mail the change for review

Once the change is ready, mail it out for review:

    $ git mail

You can specify a reviewer or CC interested parties using the `-r` or `-cc` options. Both accept a comma-separated list of email addresses:

    $ git mail -r joe@golang.org -cc mabel@example.com,math-nuts@swtch.com

Unless explicitly told otherwise, such as in the discussion leading up to sending in the change list, it's better not to specify a reviewer. All changes are automatically CC'ed to the golang-codereviews@googlegroups.com mailing list.

(In Git terms, `git` `mail` pushes the local committed changes to Gerrit using `git` `push` `origin` `HEAD:refs/for/master`.)

If your change relates to an open issue, please add a comment to the issue announcing your proposed fix, including a link to your CL.

The code review server assigns your change an issue number and URL, which `git` `mail` will print, something like:

    remote: New Changes:
    remote:   https://go-review.googlesource.com/99999 math: improved Sin, Cos and Tan precision for very large arguments

### Reviewing code

Running `git` `mail` will send an email to you and the reviewers asking them to visit the issue's URL and make comments on the change. When done, the reviewer adds comments through the Gerrit user interface and clicks "Reply" to send comments back. You will receive a mail notification when this happens. You must reply through the web interface. (Unlike with the old Rietveld review system, replying by mail has no effect.)

### Revise and upload

You must respond to review comments through the web interface. (Unlike with the old Rietveld review system, responding by mail has no effect.)

When you have revised the code and are ready for another round of review, stage those changes and use `git` `change` to update the commit. To send the update change list for another round of review, run `git` `mail` again.

The reviewer can comment on the new copy, and the process repeats. The reviewer approves the change by giving it a positive score (+1 or +2) and replying `LGTM`: looks good to me.

You can see a list of your pending changes by running `git` `pending`, and switch between change branches with `git` `change` `<branch>`.

### Synchronize your client

While you were working, others might have submitted changes to the repository. To update your local branch, run

    $ git sync

(In git terms, git sync runs `git` `pull` `-r`.)

If files you were editing have changed, Git does its best to merge the remote changes into your local changes. It may leave some files to merge by hand.

For example, suppose you have edited `sin.go` but someone else has committed an independent change. When you run `git` `sync`, you will get the (scary-looking) output:

    $ git sync
    Failed to merge in the changes.
    Patch failed at 0023 math: improved Sin, Cos and Tan precision for very large arguments
    The copy of the patch that failed is found in:
       /home/you/repo/.git/rebase-apply/patch

    When you have resolved this problem, run "git rebase --continue".
    If you prefer to skip this patch, run "git rebase --skip" instead.
    To check out the original branch and stop rebasing, run "git rebase --abort".

If this happens, run

    $ git status

to see which files failed to merge. The output will look something like this:

    rebase in progress; onto a24c3eb
    You are currently rebasing branch 'mcgillicutty' on 'a24c3eb'.
      (fix conflicts and then run "git rebase --continue")
      (use "git rebase --skip" to skip this patch)
      (use "git rebase --abort" to check out the original branch)

    Unmerged paths:
      (use "git reset HEAD <file>..." to unstage)
      (use "git add <file>..." to mark resolution)

    	both modified:   sin.go

The only important part in that transcript is the italicized "both modified" line: Git failed to merge your changes with the conflicting change. When this happens, Git leaves both sets of edits in the file, with conflicts marked by `<<<<<<<` and `>>>>>>>`. It is now your job to edit the file to combine them. Continuing the example, searching for those strings in `sin.go` might turn up:

    	arg = scale(arg)
    <<<<<<< HEAD
    	if arg > 1e9 {
    =======
    	if arg > 1e10 {
    >>>>>>> mcgillicutty
    		largeReduce(arg)

Git doesn't show it, but suppose the original text that both edits started with was 1e8; you changed it to 1e10 and the other change to 1e9, so the correct answer might now be 1e10. First, edit the section to remove the markers and leave the correct code:

    	arg = scale(arg)
    	if arg > 1e10 {
    		largeReduce(arg)

Then tell Git that the conflict is resolved by running

    $ git add sin.go

If you had been editing the file, say for debugging, but do not care to preserve your changes, you can run `git` `reset` `HEAD` `sin.go` to abandon your changes. Then run `git` `rebase` `--continue` to restore the change commit.

### Reviewing code by others

You can import a change proposed by someone else into your local Git repository. On the Gerrit review page, click the "Download ▼" link in the upper right corner, copy the "Checkout" command and run it from your local Git repo. It should look something like this:

    $ git fetch https://go.googlesource.com/review refs/changes/21/1221/1 && git checkout FETCH_HEAD

To revert, change back to the branch you were working in.

### Submit the change after the review

After the code has been `LGTM`'ed, an approver may submit it to the master branch using the Gerrit UI. There is a "Submit" button on the web page for the change that appears once the change is approved (marked +2).

This checks the change into the repository. The change description will include a link to the code review, and the code review will be updated with a link to the change in the repository. Since the method used to integrate the changes is "Cherry Pick", the commit hashes in the repository will be changed by the submit operation.

### More information

In addition to the information here, the Go community maintains a CodeReview wiki page. Feel free to contribute to this page as you learn the review process.

## Copyright

Files in the Go repository don't list author names, both to avoid clutter and to avoid having to keep the lists up to date. Instead, your name will appear in the change log and in the `CONTRIBUTORS` file and perhaps the `AUTHORS` file.

The `CONTRIBUTORS` file defines who the Go contributors—the people—are; the `AUTHORS` file defines who “The Go Authors”—the copyright holders—are. The Go developers at Google will update these files when submitting your first change. In order for them to do that, you need to have completed one of the contributor license agreements:

- If you are the copyright holder, you will need to agree to the individual contributor license agreement, which can be completed online.
- If your organization is the copyright holder, the organization will need to agree to the corporate contributor license agreement. (If the copyright holder for your code has already completed the agreement in connection with another Google open source project, it does not need to be completed again.)

This rigmarole needs to be done only for your first submission.

Code that you contribute should use the standard copyright header:

    // Copyright 2014 The Go Authors. All rights reserved.
    // Use of this source code is governed by a BSD-style
    // license that can be found in the LICENSE file.

Files in the repository are copyright the year they are added. It is not necessary to update the copyright year on files that you change.