Home | History | Annotate | Download | only in doc
      1 <!--{
      2 	"Title": "Contribution Guidelines"
      3 }-->
      4 
      5 <h2 id="Introduction">Introduction</h2>
      6 
      7 <p>
      8 This document explains how to contribute changes to the Go project.
      9 It assumes you have followed the
     10 <a href="/doc/install/source">installation instructions</a> and
     11 have <a href="code.html">written and tested your code</a>.
     12 </p>
     13 
     14 <p>
     15 (Note that the <code>gccgo</code> frontend lives elsewhere;
     16 see <a href="gccgo_contribute.html">Contributing to gccgo</a>.)
     17 </p>
     18 
     19 <h2 id="Design">Discuss your design</h2>
     20 
     21 <p>
     22 The project welcomes submissions but please let everyone know what
     23 you're working on if you want to change or add to the Go repositories.
     24 </p>
     25 
     26 <p>
     27 Before undertaking to write something new for the Go project,
     28 please <a href="https://golang.org/issue/new">file an issue</a>
     29 (or claim an <a href="https://golang.org/issues">existing issue</a>).
     30 Significant changes must go through the
     31 <a href="https://golang.org/s/proposal-process">change proposal process</a>
     32 before they can be accepted.
     33 </p>
     34 
     35 <p>
     36 This process gives everyone a chance to validate the design,
     37 helps prevent duplication of effort,
     38 and ensures that the idea fits inside the goals for the language and tools.
     39 It also checks that the design is sound before code is written;
     40 the code review tool is not the place for high-level discussions.
     41 </p>
     42 
     43 <p>
     44 When planning work, please note that the Go project follows a
     45 <a href="https://golang.org/wiki/Go-Release-Cycle">six-month
     46 development cycle</a>. The latter half of each cycle is a three-month
     47 feature freeze during which only bug fixes and doc updates are accepted.
     48 New work cannot be submitted during a feature freeze.
     49 </p>
     50 
     51 <h2 id="Testing">Testing redux</h2>
     52 
     53 <p>
     54 You've <a href="code.html">written and tested your code</a>, but
     55 before sending code out for review, run all the tests for the whole
     56 tree to make sure the changes don't break other packages or programs:
     57 </p>
     58 
     59 <pre>
     60 $ cd go/src
     61 $ ./all.bash
     62 </pre>
     63 
     64 <p>
     65 (To build under Windows use <code>all.bat</code>.)
     66 </p>
     67 
     68 <p>
     69 After running for a while, the command should print
     70 "<code>ALL</code> <code>TESTS</code> <code>PASSED</code>".
     71 </p>
     72 
     73 <h2 id="Code_review">Code review</h2>
     74 
     75 <p>
     76 Changes to Go must be reviewed before they are accepted,
     77 no matter who makes the change.
     78 A custom git command called <code>git-codereview</code>,
     79 discussed below, helps manage the code review process through a Google-hosted
     80 <a href="https://go-review.googlesource.com/">instance</a> of the code review
     81 system called <a href="https://code.google.com/p/gerrit/">Gerrit</a>.
     82 </p>
     83 
     84 <h3>Set up authentication for code review</h3>
     85 
     86 <p>
     87 Gerrit uses Google Accounts for authentication. If you don't have
     88 a Google Account, you can create an account which
     89 <a href="https://www.google.com/accounts/NewAccount">includes
     90 a new Gmail email account</a> or create an account associated
     91 <a href="https://accounts.google.com/SignUpWithoutGmail">with your existing
     92 email address</a>.
     93 </p>
     94 
     95 <p>
     96 The email address associated with the Google Account you use will be recorded in
     97 the <a href="https://go.googlesource.com/go/+log/">change log</a>
     98 and in the <a href="/CONTRIBUTORS">contributors file</a>.
     99 </p>
    100 
    101 <p>
    102 To set up your account in Gerrit, visit
    103 <a href="https://go.googlesource.com">go.googlesource.com</a>
    104 and click on "Generate Password" in the page's top right menu bar.
    105 </p>
    106 
    107 <p>
    108 You will be redirected to accounts.google.com to sign in.
    109 </p>
    110 
    111 <p>
    112 Once signed in, you are returned back to go.googlesource.com to "Configure Git".
    113 Follow the instructions on the page.
    114 (If you are on a Windows computer, you should instead follow the instructions
    115 in the yellow box to run the command.)
    116 </p>
    117 
    118 <p>
    119 Your secret authentication token is now in a <code>.gitcookie</code> file
    120 and Git is configured to use this file.
    121 </p>
    122 
    123 <h3>Register with Gerrit</h3>
    124 
    125 <p>
    126 Now that you have your authentication token,
    127 you need to register your account with Gerrit.
    128 To do this, visit
    129 <a href="https://go-review.googlesource.com/login/">
    130 go-review.googlesource.com/login/</a>. You will immediately be redirected
    131 to Google Accounts. Sign in using the same Google Account you used above.
    132 That is all that is required.
    133 </p>
    134 
    135 <h3>Contributor License Agreement</h3>
    136 
    137 <p>Gerrit serves as the gatekeeper and uses your e-mail address as the key.
    138 To send your first change to the Go project from a given address,
    139 you must have completed one of the contributor license agreements:
    140 <ul>
    141 <li>
    142 If you are the copyright holder, you will need to agree to the
    143 <a href="https://developers.google.com/open-source/cla/individual">individual
    144 contributor license agreement</a>, which can be completed online.
    145 </li>
    146 <li>
    147 If your organization is the copyright holder, the organization
    148 will need to agree to the
    149 <a href="https://developers.google.com/open-source/cla/corporate">corporate
    150 contributor license agreement</a>.
    151 (If the copyright holder for your code has already completed the
    152 agreement in connection with another Google open source project,
    153 it does not need to be completed again.)
    154 </li>
    155 </ul>
    156 
    157 <p>
    158 You can use the links above to create and sign the contributor license agreement
    159 or you can show your current agreements and create new ones through the Gerrit
    160 interface.  <a href="https://go-review.googlesource.com/login/">Log into Gerrit</a>,
    161 click your name in the upper-right, choose "Settings", then select "Agreements"
    162 from the topics on the left. If you do not have a signed agreement listed here,
    163 you can create one by clicking "New Contributor Agreement" and following the steps.
    164 </p>
    165 
    166 <p>
    167 This rigmarole only needs to be done for your first submission for each email address.
    168 </p>
    169 
    170 <h3>Install the git-codereview command</h3>
    171 
    172 <p>
    173 Now install the <code>git-codereview</code> command by running,
    174 </p>
    175 
    176 <pre>
    177 $ go get -u golang.org/x/review/git-codereview
    178 </pre>
    179 
    180 <p>
    181 Make sure <code>git-codereview</code> is installed in your shell path, so that the
    182 <code>git</code> command can find it. Check that
    183 </p>
    184 
    185 <pre>
    186 $ git codereview help
    187 </pre>
    188 
    189 <p>
    190 prints help text, not an error.
    191 </p>
    192 
    193 <p>
    194 Note to Git aficionados: The <code>git-codereview</code> command is not required to
    195 upload and manage Gerrit code reviews. For those who prefer plain Git, the text
    196 below gives the Git equivalent of each git-codereview command. If you do use plain
    197 Git, note that you still need the commit hooks that the git-codereview command
    198 configures; those hooks add a Gerrit <code>Change-Id</code> line to the commit
    199 message and check that all Go source files have been formatted with gofmt. Even
    200 if you intend to use plain Git for daily work, install the hooks in a new Git
    201 checkout by running <code>git-codereview</code> <code>hooks</code>.
    202 </p>
    203 
    204 <h3>Set up git aliases</h3>
    205 
    206 <p>
    207 The <code>git-codereview</code> command can be run directly from the shell
    208 by typing, for instance,
    209 </p>
    210 
    211 <pre>
    212 $ git codereview sync
    213 </pre>
    214 
    215 <p>
    216 but it is more convenient to set up aliases for <code>git-codereview</code>'s own
    217 subcommands, so that the above becomes,
    218 </p>
    219 
    220 <pre>
    221 $ git sync
    222 </pre>
    223 
    224 <p>
    225 The <code>git-codereview</code> subcommands have been chosen to be distinct from
    226 Git's own, so it's safe to do so.
    227 </p>
    228 
    229 <p>
    230 The aliases are optional, but in the rest of this document we will assume
    231 they are installed.
    232 To install them, copy this text into your Git configuration file
    233 (usually <code>.gitconfig</code> in your home directory):
    234 </p>
    235 
    236 <pre>
    237 [alias]
    238 	change = codereview change
    239 	gofmt = codereview gofmt
    240 	mail = codereview mail
    241 	pending = codereview pending
    242 	submit = codereview submit
    243 	sync = codereview sync
    244 </pre>
    245 
    246 <h3>Understanding the git-codereview command</h3>
    247 
    248 <p>After installing the <code>git-codereview</code> command, you can run</p>
    249 
    250 <pre>
    251 $ git codereview help
    252 </pre>
    253 
    254 <p>
    255 to learn more about its commands.
    256 You can also read the <a href="https://godoc.org/golang.org/x/review/git-codereview">command documentation</a>.
    257 </p>
    258 
    259 <h3>Switch to the master branch</h3>
    260 
    261 <p>
    262 Most Go installations use a release branch, but new changes should
    263 only be made based on the master branch.
    264 (They may be applied later to a release branch as part of the release process,
    265 but most contributors won't do this themselves.)
    266 Before making a change, make sure you start on the master branch:
    267 </p>
    268 
    269 <pre>
    270 $ git checkout master
    271 $ git sync
    272 </pre>
    273 
    274 <p>
    275 (In Git terms, <code>git</code> <code>sync</code> runs
    276 <code>git</code> <code>pull</code> <code>-r</code>.)
    277 </p>
    278 
    279 <h3>Make a change</h3>
    280 
    281 <p>
    282 The entire checked-out tree is writable.
    283 Once you have edited files, you must tell Git that they have been modified.
    284 You must also tell Git about any files that are added, removed, or renamed files.
    285 These operations are done with the usual Git commands,
    286 <code>git</code> <code>add</code>,
    287 <code>git</code> <code>rm</code>,
    288 and
    289 <code>git</code> <code>mv</code>.
    290 </p>
    291 
    292 <p>
    293 If you wish to checkpoint your work, or are ready to send the code out for review, run</p>
    294 
    295 <pre>
    296 $ git change <i>&lt;branch&gt;</i>
    297 </pre>
    298 
    299 <p>
    300 from any directory in your Go repository to commit the changes so far.
    301 The name <i>&lt;branch&gt;</i> is an arbitrary one you choose to identify the
    302 local branch containing your changes.
    303 </p>
    304 
    305 <p>
    306 (In Git terms, <code>git</code> <code>change</code> <code>&lt;branch&gt;</code>
    307 runs <code>git</code> <code>checkout</code> <code>-b</code> <code>branch</code>,
    308 then <code>git</code> <code>branch</code> <code>--set-upstream-to</code> <code>origin/master</code>,
    309 then <code>git</code> <code>commit</code>.)
    310 </p>
    311 
    312 <p>
    313 Git will open a change description file in your editor.
    314 (It uses the editor named by the <code>$EDITOR</code> environment variable,
    315 <code>vi</code> by default.)
    316 The file will look like:
    317 </p>
    318 
    319 <pre>
    320 
    321 # Please enter the commit message for your changes. Lines starting
    322 # with '#' will be ignored, and an empty message aborts the commit.
    323 # On branch foo
    324 # Changes not staged for commit:
    325 #	modified:   editedfile.go
    326 #
    327 </pre>
    328 
    329 <p>
    330 At the beginning of this file is a blank line; replace it
    331 with a thorough description of your change.
    332 The first line of the change description is conventionally a one-line
    333 summary of the change, prefixed by the primary affected package,
    334 and is used as the subject for code review mail.
    335 The rest of the
    336 description elaborates and should provide context for the
    337 change and explain what it does.
    338 If there is a helpful reference, mention it here.
    339 </p>
    340 
    341 <p>
    342 After editing, the template might now read:
    343 </p>
    344 
    345 <pre>
    346 math: improved Sin, Cos and Tan precision for very large arguments
    347 
    348 The existing implementation has poor numerical properties for
    349 large arguments, so use the McGillicutty algorithm to improve
    350 accuracy above 1e10.
    351 
    352 The algorithm is described at http://wikipedia.org/wiki/McGillicutty_Algorithm
    353 
    354 Fixes #159
    355 
    356 # Please enter the commit message for your changes. Lines starting
    357 # with '#' will be ignored, and an empty message aborts the commit.
    358 # On branch foo
    359 # Changes not staged for commit:
    360 #	modified:   editedfile.go
    361 #
    362 </pre>
    363 
    364 <p>
    365 The commented section of the file lists all the modified files in your client.
    366 It is best to keep unrelated changes in different change lists,
    367 so if you see a file listed that should not be included, abort
    368 the command and move that file to a different branch.
    369 </p>
    370 
    371 <p>
    372 The special notation "Fixes #159" associates the change with issue 159 in the
    373 <a href="https://golang.org/issue/159">Go issue tracker</a>.
    374 When this change is eventually submitted, the issue
    375 tracker will automatically mark the issue as fixed.
    376 (There are several such conventions, described in detail in the
    377 <a href="https://help.github.com/articles/closing-issues-via-commit-messages/">GitHub Issue Tracker documentation</a>.)
    378 </p>
    379 
    380 <p>
    381 Once you have finished writing the commit message,
    382 save the file and exit the editor.
    383 </p>
    384 
    385 <p>
    386 If you wish to do more editing, re-stage your changes using
    387 <code>git</code> <code>add</code>, and then run
    388 </p>
    389 
    390 <pre>
    391 $ git change
    392 </pre>
    393 
    394 <p>
    395 to update the change description and incorporate the staged changes.  The
    396 change description contains a <code>Change-Id</code> line near the bottom,
    397 added by a Git commit hook during the initial
    398 <code>git</code> <code>change</code>.
    399 That line is used by Gerrit to match successive uploads of the same change.
    400 Do not edit or delete it.
    401 </p>
    402 
    403 <p>
    404 (In Git terms, <code>git</code> <code>change</code> with no branch name
    405 runs <code>git</code> <code>commit</code> <code>--amend</code>.)
    406 </p>
    407 
    408 <h3>Mail the change for review</h3>
    409 
    410 <p>
    411 Once the change is ready, mail it out for review:
    412 </p>
    413 
    414 <pre>
    415 $ git mail
    416 </pre>
    417 
    418 <p>
    419 You can specify a reviewer or CC interested parties
    420 using the <code>-r</code> or <code>-cc</code> options.
    421 Both accept a comma-separated list of email addresses:
    422 </p>
    423 
    424 <pre>
    425 $ git mail -r joe (a] golang.org -cc mabel (a] example.com,math-nuts (a] swtch.com
    426 </pre>
    427 
    428 <p>
    429 Unless explicitly told otherwise, such as in the discussion leading
    430 up to sending in the change list, it's better not to specify a reviewer.
    431 All changes are automatically CC'ed to the
    432 <a href="https://groups.google.com/group/golang-codereviews">golang-codereviews (a] googlegroups.com</a>
    433 mailing list. If this is your first ever change, there may be a moderation
    434 delay before it appears on the mailing list, to prevent spam.
    435 </p>
    436 
    437 <p>
    438 (In Git terms, <code>git</code> <code>mail</code> pushes the local committed
    439 changes to Gerrit using <code>git</code> <code>push</code> <code>origin</code>
    440 <code>HEAD:refs/for/master</code>.)
    441 </p>
    442 
    443 <p>
    444 If your change relates to an open issue, please add a comment to the issue
    445 announcing your proposed fix, including a link to your CL.
    446 </p>
    447 
    448 <p>
    449 The code review server assigns your change an issue number and URL,
    450 which <code>git</code> <code>mail</code> will print, something like:
    451 </p>
    452 
    453 <pre>
    454 remote: New Changes:
    455 remote:   https://go-review.googlesource.com/99999 math: improved Sin, Cos and Tan precision for very large arguments
    456 </pre>
    457 
    458 <h3>Reviewing code</h3>
    459 
    460 <p>
    461 Running <code>git</code> <code>mail</code> will send an email to you and the
    462 reviewers asking them to visit the issue's URL and make comments on the change.
    463 When done, the reviewer adds comments through the Gerrit user interface
    464 and clicks "Reply" to send comments back.
    465 You will receive a mail notification when this happens.
    466 You must reply through the web interface.
    467 (Unlike with the old Rietveld review system, replying by mail has no effect.)
    468 </p>
    469 
    470 <h3>Revise and upload</h3>
    471 
    472 <p>
    473 You must respond to review comments through the web interface.
    474 (Unlike with the old Rietveld review system, responding by mail has no effect.)
    475 </p>
    476 
    477 <p>
    478 When you have revised the code and are ready for another round of review,
    479 stage those changes and use <code>git</code> <code>change</code> to update the
    480 commit.
    481 To send the update change list for another round of review,
    482 run <code>git</code> <code>mail</code> again.
    483 </p>
    484 
    485 <p>
    486 The reviewer can comment on the new copy, and the process repeats.
    487 The reviewer approves the change by giving it a positive score
    488 (+1 or +2) and replying <code>LGTM</code>: looks good to me.
    489 </p>
    490 
    491 <p>
    492 You can see a list of your pending changes by running <code>git</code>
    493 <code>pending</code>, and switch between change branches with <code>git</code>
    494 <code>change</code> <code><i>&lt;branch&gt;</i></code>.
    495 </p>
    496 
    497 <h3>Synchronize your client</h3>
    498 
    499 <p>
    500 While you were working, others might have submitted changes to the repository.
    501 To update your local branch, run
    502 </p>
    503 
    504 <pre>
    505 $ git sync
    506 </pre>
    507 
    508 <p>
    509 (In git terms, <code>git</code> <code>sync</code> runs
    510 <code>git</code> <code>pull</code> <code>-r</code>.)
    511 </p>
    512 
    513 <p>
    514 If files you were editing have changed, Git does its best to merge the
    515 remote changes into your local changes.
    516 It may leave some files to merge by hand.
    517 </p>
    518 
    519 <p>
    520 For example, suppose you have edited <code>sin.go</code> but
    521 someone else has committed an independent change.
    522 When you run <code>git</code> <code>sync</code>,
    523 you will get the (scary-looking) output:
    524 
    525 <pre>
    526 $ git sync
    527 Failed to merge in the changes.
    528 Patch failed at 0023 math: improved Sin, Cos and Tan precision for very large arguments
    529 The copy of the patch that failed is found in:
    530    /home/you/repo/.git/rebase-apply/patch
    531 
    532 When you have resolved this problem, run "git rebase --continue".
    533 If you prefer to skip this patch, run "git rebase --skip" instead.
    534 To check out the original branch and stop rebasing, run "git rebase --abort".
    535 </pre>
    536 
    537 <p>
    538 If this happens, run
    539 </p>
    540 
    541 <pre>
    542 $ git status
    543 </pre>
    544 
    545 <p>
    546 to see which files failed to merge.
    547 The output will look something like this:
    548 </p>
    549 
    550 <pre>
    551 rebase in progress; onto a24c3eb
    552 You are currently rebasing branch 'mcgillicutty' on 'a24c3eb'.
    553   (fix conflicts and then run "git rebase --continue")
    554   (use "git rebase --skip" to skip this patch)
    555   (use "git rebase --abort" to check out the original branch)
    556 
    557 Unmerged paths:
    558   (use "git reset HEAD &lt;file&gt;..." to unstage)
    559   (use "git add &lt;file&gt;..." to mark resolution)
    560 
    561 	<i>both modified: sin.go</i>
    562 </pre>
    563 
    564 <p>
    565 The only important part in that transcript is the italicized "both modified"
    566 line: Git failed to merge your changes with the conflicting change.
    567 When this happens, Git leaves both sets of edits in the file,
    568 with conflicts marked by <code>&lt;&lt;&lt;&lt;&lt;&lt;&lt;</code> and
    569 <code>&gt;&gt;&gt;&gt;&gt;&gt;&gt;</code>.
    570 It is now your job to edit the file to combine them.
    571 Continuing the example, searching for those strings in <code>sin.go</code>
    572 might turn up:
    573 </p>
    574 
    575 <pre>
    576 	arg = scale(arg)
    577 &lt;&lt;&lt;&lt;&lt;&lt;&lt; HEAD
    578 	if arg &lt; 1e9 {
    579 =======
    580 	if arg &lh; 1e10 {
    581 &gt;&gt;&gt;&gt;&gt;&gt;&gt; mcgillicutty
    582 		largeReduce(arg)
    583 </pre>
    584 
    585 <p>
    586 Git doesn't show it, but suppose the original text that both edits
    587 started with was 1e8; you changed it to 1e10 and the other change to 1e9,
    588 so the correct answer might now be 1e10.  First, edit the section
    589 to remove the markers and leave the correct code:
    590 </p>
    591 
    592 <pre>
    593 	arg = scale(arg)
    594 	if arg &lt; 1e10 {
    595 		largeReduce(arg)
    596 </pre>
    597 
    598 <p>
    599 Then tell Git that the conflict is resolved by running
    600 </p>
    601 
    602 <pre>
    603 $ git add sin.go
    604 </pre>
    605 
    606 <p>
    607 If you had been editing the file, say for debugging, but do not
    608 care to preserve your changes, you can run
    609 <code>git</code> <code>reset</code> <code>HEAD</code> <code>sin.go</code>
    610 to abandon your changes.
    611 Then run <code>git</code> <code>rebase</code> <code>--continue</code> to
    612 restore the change commit.
    613 </p>
    614 
    615 <h3>Reviewing code by others</h3>
    616 
    617 <p>
    618 You can import a change proposed by someone else into your local Git repository.
    619 On the Gerrit review page, click the "Download " link in the upper right
    620 corner, copy the "Checkout" command and run it from your local Git repo.
    621 It should look something like this:
    622 </p>
    623 
    624 <pre>
    625 $ git fetch https://go.googlesource.com/review refs/changes/21/1221/1 &amp;&amp; git checkout FETCH_HEAD
    626 </pre>
    627 
    628 <p>
    629 To revert, change back to the branch you were working in.
    630 </p>
    631 
    632 <h3>Submit the change after the review</h3>
    633 
    634 <p>
    635 After the code has been <code>LGTM</code>'ed, an approver may
    636 submit it to the master branch using the Gerrit UI.
    637 There is a "Submit" button on the web page for the change
    638 that appears once the change is approved (marked +2).
    639 </p>
    640 
    641 <p>
    642 This checks the change into the repository.
    643 The change description will include a link to the code review,
    644 and the code review will be updated with a link to the change
    645 in the repository.
    646 Since the method used to integrate the changes is "Cherry Pick",
    647 the commit hashes in the repository will be changed by
    648 the submit operation.
    649 </p>
    650 
    651 <h3>More information</h3>
    652 
    653 <p>
    654 In addition to the information here, the Go community maintains a <a href="https://golang.org/wiki/CodeReview">CodeReview</a> wiki page.
    655 Feel free to contribute to this page as you learn the review process.
    656 </p>
    657 
    658 <h2 id="copyright">Copyright</h2>
    659 
    660 <p>Files in the Go repository don't list author names,
    661 both to avoid clutter and to avoid having to keep the lists up to date.
    662 Instead, your name will appear in the
    663 <a href="https://golang.org/change">change log</a>
    664 and in the <a href="/CONTRIBUTORS"><code>CONTRIBUTORS</code></a> file
    665 and perhaps the <a href="/AUTHORS"><code>AUTHORS</code></a> file.
    666 </p>
    667 
    668 <p>The <a href="/CONTRIBUTORS"><code>CONTRIBUTORS</code></a> file
    669 defines who the Go contributors&mdash;the people&mdash;are;
    670 the <a href="/AUTHORS"><code>AUTHORS</code></a> file defines
    671 who &ldquo;The Go Authors&rdquo;&mdash;the copyright holders&mdash;are.
    672 These files will be periodically updated based on the commit logs.
    673 
    674 <p>Code that you contribute should use the standard copyright header:</p>
    675 
    676 <pre>
    677 // Copyright 2015 The Go Authors. All rights reserved.
    678 // Use of this source code is governed by a BSD-style
    679 // license that can be found in the LICENSE file.
    680 </pre>
    681 
    682 <p>
    683 Files in the repository are copyright the year they are added. It is not
    684 necessary to update the copyright year on files that you change.
    685 </p>
    686