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