github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/doc/contribute.html (about)

     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 installed Go using the
    10  <a href="/doc/install/source">installation instructions</a> and
    11  have <a href="code.html">written and tested your code</a>.
    12  (Note that the <code>gccgo</code> frontend lives elsewhere;
    13  see <a href="gccgo_contribute.html">Contributing to gccgo</a>.)
    14  </p>
    15  
    16  <h2 id="Design">Discuss your design</h2>
    17  
    18  <p>
    19  The project welcomes submissions but please let everyone know what
    20  you're working on if you want it to become part of the main repository.
    21  </p>
    22  
    23  <p>
    24  Before undertaking to write something new for the Go project, send
    25  mail to the <a href="http://groups.google.com/group/golang-nuts">mailing
    26  list</a> to discuss what you plan to do.  This gives everyone a
    27  chance to validate the design, helps prevent duplication of effort,
    28  and ensures that the idea fits inside the goals for the language
    29  and tools.  It also guarantees that the design is sound before code
    30  is written; the code review tool is not the place for high-level
    31  discussions.
    32  </p>
    33  
    34  <p>
    35  In short, send mail before you code.
    36  And don't start the discussion by mailing a change list!
    37  </p>
    38  
    39  <h2 id="Testing">Testing redux</h2>
    40  
    41  <p>
    42  You've <a href="code.html">written and tested your code</a>, but
    43  before sending code out for review, run all the tests for the whole
    44  tree to make sure the changes don't break other packages or programs:
    45  </p>
    46  
    47  <pre>
    48  cd $GOROOT/src
    49  ./all.bash    # On Windows, run all.bat
    50  </pre>
    51  
    52  <p>
    53  After running for a while, the command should print "<code>ALL TESTS PASSED</code>".
    54  </p>
    55  
    56  <h2 id="Code_review">Code review</h2>
    57  
    58  <p>
    59  Changes to Go must be reviewed before they are submitted,
    60  no matter who makes the change.
    61  (In exceptional cases, such as fixing a build, the review can
    62  follow shortly after submitting.)
    63  A Mercurial extension helps manage the code review process.
    64  The extension is included in the Go source tree but needs
    65  to be added to your Mercurial configuration.
    66  </p>
    67  
    68  <h3>Caveat for Mercurial aficionados</h3>
    69  
    70  <p>
    71  <i>Using Mercurial with the code review extension is not the same
    72  as using standard Mercurial.</i>
    73  </p>
    74  
    75  <p>
    76  The Go repository is maintained as a single line of reviewed changes;
    77  we prefer to avoid the complexity of Mercurial's arbitrary change graph.
    78  The code review extension helps here: its <code>hg submit</code> command
    79  automatically checks for and warns about the local repository
    80  being out of date compared to the remote one.
    81  The <code>hg submit</code> command also verifies other
    82  properties about the Go repository.
    83  For example,
    84  it checks that Go code being checked in is formatted in the standard style,
    85  as defined by <a href="/cmd/gofmt">gofmt</a>,
    86  and it checks that the author of the code is properly recorded for
    87  <a href="#copyright">copyright purposes</a>.
    88  </p>
    89  
    90  <p>
    91  To help ensure changes are only created by <code>hg submit</code>,
    92  the code review extension disables the standard <code>hg commit</code>
    93  command.
    94  </p>
    95  
    96  <h3>Configure the extension</h3>
    97  
    98  <p>Edit <code>$GOROOT/.hg/hgrc</code> to add:</p>
    99  
   100  <pre>
   101  [extensions]
   102  codereview = $GOROOT/lib/codereview/codereview.py
   103  
   104  [ui]
   105  username = Your Name &lt;you@server.dom&gt;
   106  </pre>
   107  
   108  <p>
   109  The <code>username</code> information will not be used unless
   110  you are a committer (see below), but Mercurial complains if it is missing.
   111  </p>
   112  
   113  <p>
   114  After adding the extension, <code>hg help codereview</code>
   115  will show documentation for its commands. As the codereview extension is only
   116  enabled for your checkout in <code>$GOROOT</code>, the remainder of this
   117  document assumes you are inside <code>$GOROOT</code> when issuing commands.
   118  </p>
   119  
   120  <p>
   121  Windows users may need to perform extra steps to get the code review 
   122  extension working. See the
   123  <a href="https://code.google.com/p/go-wiki/wiki/CodeReview">CodeReview page</a> 
   124  on the <a href="http://code.google.com/p/go-wiki/wiki">Go Wiki</a> for details.
   125  </p>
   126  
   127  <h3>Log in to the code review site.</h3>
   128  
   129  <p>
   130  The code review server uses a Google Account to authenticate.
   131  (If you can use the account to
   132  <a href="https://www.google.com/accounts/Login?hl=en&amp;continue=http://www.google.com/">sign in at google.com</a>,
   133  you can use it to sign in to the code review server.)
   134  The email address you use on the Code Review site
   135  will be recorded in the <a href="http://code.google.com/p/go/source/list">Mercurial change log</a>
   136  and in the <a href="/CONTRIBUTORS"><code>CONTRIBUTORS</code></a> file.
   137  You can <a href="https://www.google.com/accounts/NewAccount">create a Google Account</a>
   138  associated with any address where you receive email.
   139  If you've enabled the two-step verification feature, don't forget to generate an
   140  application-specific password and use that when prompted for a password.
   141  </p>
   142  
   143  <pre>
   144  $ cd $GOROOT
   145  $ hg code-login
   146  Email (login for uploading to codereview.appspot.com): rsc@golang.org
   147  Password for rsc@golang.org:
   148  
   149  Saving authentication cookies to /Users/rsc/.codereview_upload_cookies_codereview.appspot.com
   150  </pre>
   151  
   152  <h3>Configure your account settings.</h3>
   153  
   154  <p>Edit your <a href="http://codereview.appspot.com/settings">code review settings</a>.
   155  Grab a nickname.
   156  Many people prefer to set the Context option to
   157  &ldquo;Whole file&rdquo; to see more context when reviewing changes.
   158  </p>
   159  
   160  <p>Once you have chosen a nickname in the settings page, others
   161  can use that nickname as a shorthand for naming reviewers and the CC list.
   162  For example, <code>rsc</code> is an alias for <code>rsc@golang.org</code>.
   163  </p>
   164  
   165  <h3>Switch to the default branch</h3>
   166  
   167  <p>
   168  Most Go installations use a release branch, but new changes should
   169  only be made to the default branch. (They may be applied later to a release
   170  branch as part of the release process.)
   171  Before making a change, make sure you use the default branch:
   172  </p>
   173  
   174  <pre>
   175  $ hg update default
   176  </pre>
   177  
   178  <h3>Make a change</h3>
   179  
   180  <p>
   181  The entire checked-out tree is writable.
   182  If you need to edit files, just edit them: Mercurial will figure out which ones changed.
   183  You do need to inform Mercurial of added, removed, copied, or renamed files,
   184  by running
   185  <code>hg add</code>,
   186  <code>hg rm</code>,
   187  <code>hg cp</code>,
   188  or
   189  <code>hg mv</code>.
   190  </p>
   191  
   192  <p>When you are ready to send a change out for review, run</p>
   193  
   194  <pre>
   195  $ hg change
   196  </pre>
   197  
   198  <p>from any directory in your Go repository.
   199  Mercurial will open a change description file in your editor.
   200  (It uses the editor named by the <code>$EDITOR</code> environment variable, <code>vi</code> by default.)
   201  The file will look like:
   202  </p>
   203  
   204  <pre>
   205  # Change list.
   206  # Lines beginning with # are ignored.
   207  # Multi-line values should be indented.
   208  
   209  Reviewer:
   210  CC:
   211  
   212  Description:
   213  	&lt;enter description here&gt;
   214  
   215  Files:
   216  	src/pkg/math/sin.go
   217  	src/pkg/math/tan.go
   218  	src/pkg/regexp/regexp.go
   219  </pre>
   220  
   221  <p>
   222  The <code>Reviewer</code> line lists the reviewers assigned
   223  to this change, and the <code>CC</code> line lists people to
   224  notify about the change.
   225  These can be code review nicknames or arbitrary email addresses.
   226  Unless explicitly told otherwise, such as in the discussion leading
   227  up to sending in the change list, leave the reviewer field blank.
   228  This means that the
   229  <a href="http://groups.google.com/group/golang-dev">golang-dev@googlegroups.com</a>
   230  mailing list will be used as the reviewer.
   231  </p>
   232  
   233  <p>
   234  Replace &ldquo;<code>&lt;enter description here&gt;</code>&rdquo;
   235  with a description of your change.
   236  The first line of the change description is conventionally a one-line
   237  summary of the change, prefixed by the primary affected package,
   238  and is used as the subject for code review mail; the rest of the
   239  description elaborates.
   240  </p>
   241  
   242  <p>
   243  The <code>Files</code> section lists all the modified files
   244  in your client.
   245  It is best to keep unrelated changes in different change lists.
   246  In this example, we can include just the changes to package <code>math</code>
   247  by deleting the line mentioning <code>regexp.go</code>.
   248  </p>
   249  
   250  <p>
   251  After editing, the template might now read:
   252  </p>
   253  
   254  <pre>
   255  # Change list.
   256  # Lines beginning with # are ignored.
   257  # Multi-line values should be indented.
   258  
   259  Reviewer: golang-dev@googlegroups.com
   260  CC: math-nuts@swtch.com
   261  
   262  Description:
   263  	math: improved Sin, Cos and Tan precision for very large arguments.
   264  
   265  	See Bimmler and Shaney, ``Extreme sinusoids,'' J. Math 3(14).
   266  	Fixes issue 159.
   267  
   268  Files:
   269  	src/pkg/math/sin.go
   270  	src/pkg/math/tan.go
   271  </pre>
   272  
   273  <p>
   274  The special sentence &ldquo;Fixes issue 159.&rdquo; associates
   275  the change with issue 159 in the <a href="http://code.google.com/p/go/issues/list">Go issue tracker</a>.
   276  When this change is eventually submitted, the issue
   277  tracker will automatically mark the issue as fixed.
   278  (These conventions are described in detail by the
   279  <a href="http://code.google.com/p/support/wiki/IssueTracker#Integration_with_version_control">Google Project Hosting Issue Tracker documentation</a>.)
   280  </p>
   281  
   282  <p>
   283  Save the file and exit the editor.</p>
   284  
   285  <p>
   286  The code review server assigns your change an issue number and URL,
   287  which <code>hg change</code> will print, something like:
   288  </p>
   289  
   290  <pre>
   291  CL created: http://codereview.appspot.com/99999
   292  </pre>
   293  
   294  <h3>Adding or removing files from an existing change</h3>
   295  
   296  <p>
   297  If you need to re-edit the change description, or change the files included in the CL,
   298  run <code>hg change 99999</code>.
   299  </p>
   300  
   301  <p>
   302  Alternatively, you can use
   303  </p>
   304  
   305  <pre>
   306  $ hg file 99999 somefile
   307  </pre>
   308  
   309  <p>
   310  to add <code>somefile</code> to CL 99999, and
   311  </p>
   312  
   313  <pre>
   314  $ hg file -d 99999 somefile
   315  </pre>
   316  
   317  <p>
   318  to remove <code>somefile</code> from the CL.
   319  </p>
   320  
   321  <p>
   322  A file may only belong to a single active CL at a time. <code>hg file</code>
   323  will issue a warning if a file is moved between changes.
   324  </p>
   325  
   326  <h3>Synchronize your client</h3>
   327  
   328  <p>While you were working, others might have submitted changes
   329  to the repository.  To update your client, run</p>
   330  
   331  <pre>
   332  $ hg sync
   333  </pre>
   334  
   335  <p>(For Mercurial fans, <code>hg sync</code> runs <code>hg pull -u</code>
   336  but then also synchronizes the local change list state against the new data.)</p>
   337  
   338  <p>
   339  If files you were editing have changed, Mercurial does its best to merge the
   340  remote changes into your local changes.  It may leave some files to merge by hand.
   341  </p>
   342  
   343  <p>
   344  For example, suppose you have edited <code>flag_test.go</code> but
   345  someone else has committed an independent change.
   346  When you run <code>hg sync</code>, you will get the (scary-looking) output
   347  (emphasis added):
   348  
   349  <pre>
   350  $ hg sync
   351  adding changesets
   352  adding manifests
   353  adding file changes
   354  added 1 changeset with 2 changes to 2 files
   355  getting src/pkg/flag/flag.go
   356  couldn't find merge tool hgmerge
   357  merging src/pkg/flag/flag_test.go
   358  warning: conflicts during merge.
   359  <i>merging src/pkg/flag/flag_test.go failed!</i>
   360  1 file updated, 0 files merged, 0 files removed, 1 file unresolved
   361  use 'hg resolve' to retry unresolved file merges
   362  $
   363  </pre>
   364  
   365  <p>
   366  The only important part in that transcript is the italicized line:
   367  Mercurial failed to merge your changes with the independent change.
   368  When this happens, Mercurial leaves both edits in the file,
   369  marked by <code>&lt;&lt;&lt;&lt;&lt;&lt;&lt;</code> and
   370  <code>&gt;&gt;&gt;&gt;&gt;&gt;&gt;</code>.
   371  it is now your job to edit the file to combine them.
   372  Continuing the example, searching for those strings in <code>flag_test.go</code>
   373  might turn up:
   374  </p>
   375  
   376  <pre>
   377  	VisitAll(visitor);
   378  &lt;&lt;&lt;&lt;&lt;&lt;&lt; local
   379  	if len(m) != 7 {
   380  =======
   381  	if len(m) != 8 {
   382  &gt;&gt;&gt;&gt;&gt;&gt;&gt; other
   383  		t.Error("VisitAll misses some flags");
   384  </pre>
   385  
   386  <p>
   387  Mercurial doesn't show it, but suppose the original text that both edits
   388  started with was 6; you added 1 and the other change added 2,
   389  so the correct answer might now be 9.  First, edit the section
   390  to remove the markers and leave the correct code:
   391  </p>
   392  
   393  <pre>
   394  	VisitAll(visitor);
   395  	if len(m) != 9 {
   396  		t.Error("VisitAll misses some flags");
   397  </pre>
   398  
   399  <p>
   400  Then ask Mercurial to mark the conflict as resolved:
   401  </p>
   402  
   403  <pre>
   404  $ hg resolve -m flag_test.go
   405  </pre>
   406  
   407  <p>
   408  If you had been editing the file, say for debugging, but do not
   409  care to preserve your changes, you can run
   410  <code>hg revert flag_test.go</code> to abandon your
   411  changes, but you may still need to run
   412  <code>hg resolve -m</code> to mark the conflict resolved.
   413  </p>
   414  
   415  <h3>Mail the change for review</h3>
   416  
   417  <p>
   418  Creating or uploading the change uploads a copy of the diff to the code review server,
   419  but it does not notify anyone about it. To do that, you need to run <code>hg mail</code>
   420  (see below).
   421  </p>
   422  
   423  <p>To send out a change for review, run <code>hg mail</code> using the change list number
   424  assigned during <code>hg change</code>:</p>
   425  
   426  <pre>
   427  $ hg mail 99999
   428  </pre>
   429  
   430  <p>You can add to the <code>Reviewer:</code> and <code>CC:</code> lines
   431  using the <code>-r</code> or <code>--cc</code> options.
   432  In the above example, we could have left the <code>Reviewer</code> and <code>CC</code>
   433  lines blank and then run:
   434  </p>
   435  
   436  <pre>
   437  $ hg mail -r golang-dev@googlegroups.com --cc math-nuts@swtch.com 99999
   438  </pre>
   439  
   440  <p>to achieve the same effect.</p>
   441  
   442  <p>Note that <code>-r</code> and <code>--cc</code> cannot be spelled <code>--r</code> or <code>-cc</code>.</p>
   443  
   444  <p>
   445  If your change relates to an open issue, please add a comment to the issue
   446  announcing your proposed fix, including a link to your CL.
   447  </p>
   448  
   449  <h3>Reviewing code</h3>
   450  
   451  <p>
   452  Running <code>hg mail</code> will send an email to you and the reviewers
   453  asking them to visit the issue's URL and make comments on the change.
   454  When done, the reviewer clicks &ldquo;Publish and Mail comments&rdquo;
   455  to send comments back.
   456  </p>
   457  
   458  
   459  <h3>Revise and upload</h3>
   460  
   461  <p>
   462  You will probably revise your code in response to the reviewer comments. When
   463  you have done this, you can upload your change to the code review server
   464  without sending a notification by running <code>hg upload</code> using the change
   465  list number assigned during <code>hg change</code>
   466  </p>
   467  
   468  <pre>
   469  $ hg upload 99999
   470  </pre>
   471  
   472  <p>
   473  When you have revised the code and are ready for another round of review, run
   474  </p>
   475  
   476  <pre>
   477  $ hg mail 99999
   478  </pre>
   479  
   480  <p>again to upload the latest copy and send mail asking the reviewers to please take another look
   481  (<code>PTAL</code>).
   482  You might also visit the code review web page and reply to the comments,
   483  letting the reviewer know that you've addressed them or explain why you
   484  haven't.  When you're done replying, click &ldquo;Publish and Mail comments&rdquo;
   485  to send the line-by-line replies and any other comments.
   486  </p>
   487  <p>
   488  The reviewer can comment on the new copy, and the process repeats.
   489  The reviewer approves the change by replying with a mail that says
   490  <code>LGTM</code>: looks good to me.
   491  </p>
   492  
   493  <p>
   494  You can see a list of your pending changes by running <code>hg pending</code> (<code>hg p</code> for short).
   495  </p>
   496  
   497  <h3>Reviewing code by others</h3>
   498  
   499  <p>
   500  You can import a CL proposed by someone else into your local Mercurial client
   501  by using the <code>hg clpatch</code> command. Running
   502  </p>
   503  
   504  <pre>
   505  $ hg clpatch 99999
   506  </pre>
   507  
   508  <p>
   509  will apply the latest diff for CL 99999 to your working copy. If any of the
   510  files referenced in CL 99999 have local modifications, <code>clpatch</code>
   511  will refuse to apply the whole diff. Once applied, CL 99999 will show up in
   512  the output of <code>hg pending</code> and others.
   513  </p>
   514  
   515  <p>
   516  To revert a CL you have applied locally, use the <code>hg revert</code>
   517  command. Running
   518  </p>
   519  
   520  <pre>
   521  $ hg revert @99999
   522  </pre>
   523  
   524  <p>
   525  will revert any files mentioned on CL 99999 to their original state. This can
   526  be an effective way of reverting one CL revision and applying another.
   527  </p>
   528  
   529  <p>
   530  Once the CL has been submitted, the next time you run <code>hg sync</code>
   531  it will be removed from your local pending list. Occasionally the pending list
   532  can get out of sync leaving stale references to closed or abandoned CLs.
   533  You can use <code>hg change -D 99999</code> to remove the reference to CL 99999.
   534  </p>
   535  
   536  <h3>Submit the change after the review</h3>
   537  
   538  <p>
   539  After the code has been <code>LGTM</code>'ed, it is time to submit
   540  it to the Mercurial repository.
   541  </p>
   542  
   543  <p>
   544  If you are not a committer, you cannot submit the change directly.
   545  Instead a committer, usually the reviewer who said <code>LGTM</code>,
   546  will run:
   547  </p>
   548  
   549  <pre>
   550  $ hg clpatch 99999
   551  $ hg submit 99999
   552  </pre>
   553  
   554  <p>
   555  The <code>submit</code> command submits the code.  You will be listed as the
   556  author, but the change message will also indicate who the committer was.
   557  Your local client will notice that the change has been submitted
   558  when you next run <code>hg sync</code>.
   559  </p>
   560  
   561  <p>
   562  If you are a committer, you can run:
   563  </p>
   564  
   565  <pre>
   566  $ hg submit 99999
   567  </pre>
   568  
   569  <p>
   570  This checks the change into the repository.
   571  The change description will include a link to the code review,
   572  and the code review will be updated with a link to the change
   573  in the repository.
   574  </p>
   575  
   576  <p>
   577  If your local copy of the repository is out of date,
   578  <code>hg submit</code> will refuse the change:
   579  </p>
   580  
   581  <pre>
   582  $ hg submit 99999
   583  local repository out of date; must sync before submit
   584  </pre>
   585  
   586  <h2 id="copyright">Copyright</h2>
   587  
   588  <p>Files in the Go repository don't list author names,
   589  both to avoid clutter and to avoid having to keep the lists up to date.
   590  Instead, your name will appear in the <a href="http://code.google.com/p/go/source/list">Mercurial change log</a>
   591  and in the <a href="/CONTRIBUTORS"><code>CONTRIBUTORS</code></a> file
   592  and perhaps the <a href="/AUTHORS"><code>AUTHORS</code></a> file.
   593  </p>
   594  
   595  <p>The <a href="/CONTRIBUTORS"><code>CONTRIBUTORS</code></a> file
   596  defines who the Go contributors&mdash;the people&mdash;are;
   597  the <a href="/AUTHORS"><code>AUTHORS</code></a> file defines
   598  who &ldquo;The Go Authors&rdquo;&mdash;the copyright holders&mdash;are.
   599  The Go developers at Google will update these files when submitting
   600  your first change.
   601  In order for them to do that, you need to have completed one of the
   602  contributor license agreements:
   603  <ul>
   604  <li>
   605  If you are the copyright holder, you will need to agree to
   606  the <a href="http://code.google.com/legal/individual-cla-v1.0.html">individual
   607  contributor license agreement</a>, which can be completed online.
   608  </li>
   609  <li>
   610  If your organization is the copyright holder, the organization
   611  will need to agree to the <a href="http://code.google.com/legal/corporate-cla-v1.0.html">corporate contributor license agreement</a>.
   612  (If the copyright holder for your code has already completed the
   613  agreement in connection with another Google open source project,
   614  it does not need to be completed again.)
   615  </li>
   616  </ul>
   617  
   618  <p>
   619  This rigmarole needs to be done only for your first submission.
   620  </p>
   621  
   622  <p>Code that you contribute should use the standard copyright header:</p>
   623  
   624  <pre>
   625  // Copyright 2013 The Go Authors. All rights reserved.
   626  // Use of this source code is governed by a BSD-style
   627  // license that can be found in the LICENSE file.
   628  </pre>
   629  
   630  <p>
   631  Files in the repository are copyright the year they are added. It is not
   632  necessary to update the copyright year on files that you change.
   633  </p>