contribute.html 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  1. <!--{
  2. "Title": "Contribution Guide"
  3. }-->
  4. <p>
  5. The Go project welcomes all contributors.
  6. </p>
  7. <p>
  8. This document is a guide to help you through the process
  9. of contributing to the Go project, which is a little different
  10. from that used by other open source projects.
  11. We assume you have a basic understanding of Git and Go.
  12. </p>
  13. <p>
  14. In addition to the information here, the Go community maintains a
  15. <a href="https://golang.org/wiki/CodeReview">CodeReview</a> wiki page.
  16. Feel free to contribute to the wiki as you learn the review process.
  17. </p>
  18. <p>
  19. Note that the <code>gccgo</code> front end lives elsewhere;
  20. see <a href="gccgo_contribute.html">Contributing to gccgo</a>.
  21. </p>
  22. <h2 id="contributor">Becoming a contributor</h2>
  23. <h3>Overview</h3>
  24. <p>
  25. The first step is registering as a Go contributor and configuring your environment.
  26. Here is a checklist of the required steps to follow:
  27. </p>
  28. <ul>
  29. <li>
  30. <b>Step 0</b>: Decide on a single Google Account you will be using to contribute to Go.
  31. Use that account for all the following steps and make sure that <code>git</code>
  32. is configured to create commits with that account's e-mail address.
  33. </li>
  34. <li>
  35. <b>Step 1</b>: <a href="https://cla.developers.google.com/clas">Sign and submit</a> a
  36. CLA (Contributor License Agreement).
  37. </li>
  38. <li>
  39. <b>Step 2</b>: Configure authentication credentials for the Go Git repository.
  40. Visit <a href="https://go.googlesource.com/">go.googlesource.com</a>, click
  41. on the gear icon (top right), then on "Obtain password", and follow the
  42. instructions.
  43. </li>
  44. <li>
  45. <b>Step 3</b>: Register for Gerrit, the code review tool used by the Go team,
  46. by <a href="https://go-review.googlesource.com/login/">visiting this page</a>.
  47. The CLA and the registration need to be done only once for your account.
  48. </li>
  49. <li>
  50. <b>Step 4</b>: Install <code>git-codereview</code> by running
  51. <code>go get -u golang.org/x/review/git-codereview</code>
  52. </li>
  53. </ul>
  54. <p>
  55. If you prefer, there is an automated tool that walks through these steps.
  56. Just run:
  57. </p>
  58. <pre>
  59. $ go get -u golang.org/x/tools/cmd/go-contrib-init
  60. $ cd /code/to/edit
  61. $ go-contrib-init
  62. </pre>
  63. <p>
  64. The rest of this chapter elaborates on these instructions.
  65. If you have completed the steps above (either manually or through the tool), jump to
  66. <a href="#before_contributing">Before contributing code</a>.
  67. </p>
  68. <h3 id="google_account">Step 0: Select a Google Account</h3>
  69. <p>
  70. A contribution to Go is made through a Google account with a specific
  71. e-mail address.
  72. Make sure to use the same account throughout the process and
  73. for all your subsequent contributions.
  74. You may need to decide whether to use a personal address or a corporate address.
  75. The choice will depend on who
  76. will own the copyright for the code that you will be writing
  77. and submitting.
  78. You might want to discuss this topic with your employer before deciding which
  79. account to use.
  80. </p>
  81. <p>
  82. Google accounts can either be Gmail e-mail accounts, G Suite organization accounts, or
  83. accounts associated with an external e-mail address.
  84. For instance, if you need to use
  85. an existing corporate e-mail that is not managed through G Suite, you can create
  86. an account associated
  87. <a href="https://accounts.google.com/SignUpWithoutGmail">with your existing
  88. e-mail address</a>.
  89. </p>
  90. <p>
  91. You also need to make sure that your Git tool is configured to create commits
  92. using your chosen e-mail address.
  93. You can either configure Git globally
  94. (as a default for all projects), or locally (for a single specific project).
  95. You can check the current configuration with this command:
  96. </p>
  97. <pre>
  98. $ git config --global user.email # check current global config
  99. $ git config user.email # check current local config
  100. </pre>
  101. <p>
  102. To change the configured address:
  103. </p>
  104. <pre>
  105. $ git config --global user.email name@example.com # change global config
  106. $ git config user.email name@example.com # change local config
  107. </pre>
  108. <h3 id="cla">Step 1: Contributor License Agreement</h3>
  109. <p>
  110. Before sending your first change to the Go project
  111. you must have completed one of the following two CLAs.
  112. Which CLA you should sign depends on who owns the copyright to your work.
  113. </p>
  114. <ul>
  115. <li>
  116. If you are the copyright holder, you will need to agree to the
  117. <a href="https://developers.google.com/open-source/cla/individual">individual
  118. contributor license agreement</a>, which can be completed online.
  119. </li>
  120. <li>
  121. If your organization is the copyright holder, the organization
  122. will need to agree to the
  123. <a href="https://developers.google.com/open-source/cla/corporate">corporate
  124. contributor license agreement</a>.<br>
  125. </li>
  126. </ul>
  127. <p>
  128. You can check your currently signed agreements and sign new ones at
  129. the <a href="https://cla.developers.google.com/clas?pli=1&amp;authuser=1">Google Developers
  130. Contributor License Agreements</a> website.
  131. If the copyright holder for your contribution has already completed the
  132. agreement in connection with another Google open source project,
  133. it does not need to be completed again.
  134. </p>
  135. <p>
  136. If the copyright holder for the code you are submitting changes&mdash;for example,
  137. if you start contributing code on behalf of a new company&mdash;please send mail
  138. to the <a href="mailto:golang-dev@googlegroups.com"><code>golang-dev</code>
  139. mailing list</a>.
  140. This will let us know the situation so we can make sure an appropriate agreement is
  141. completed and update the <code>AUTHORS</code> file.
  142. </p>
  143. <h3 id="config_git_auth">Step 2: Configure git authentication</h3>
  144. <p>
  145. The main Go repository is located at
  146. <a href="https://go.googlesource.com">go.googlesource.com</a>,
  147. a Git server hosted by Google.
  148. Authentication on the web server is made through your Google account, but
  149. you also need to configure <code>git</code> on your computer to access it.
  150. Follow this steps:
  151. </p>
  152. <ol>
  153. <li>
  154. Visit <a href="https://go.googlesource.com">go.googlesource.com</a>
  155. and click on "Generate Password" in the page's top right menu bar.
  156. You will be redirected to accounts.google.com to sign in.
  157. </li>
  158. <li>
  159. After signing in, you will be taken to a page with the title "Configure Git".
  160. This page contains a personalized script that when run locally will configure Git
  161. to hold your unique authentication key.
  162. This key is paired with one that is generated and stored on the server,
  163. analogous to how SSH keys work.
  164. </li>
  165. <li>
  166. Copy and run this script locally in your terminal to store your secret
  167. authentication token in a <code>.gitcookies</code> file.
  168. If you are using a Windows computer and running <code>cmd</code>,
  169. you should instead follow the instructions in the yellow box to run the command;
  170. otherwise run the regular script.
  171. </li>
  172. </ol>
  173. <h3 id="auth">Step 3: Create a Gerrit account </h3>
  174. <p>
  175. Gerrit is an open-source tool used by Go maintainers to discuss and review
  176. code submissions.
  177. </p>
  178. <p>
  179. To register your account, visit <a href="https://go-review.googlesource.com/login/">
  180. go-review.googlesource.com/login/</a> and sign in once using the same Google Account you used above.
  181. </p>
  182. <h3 id="git-codereview_install">Step 4: Install the git-codereview command</h3>
  183. <p>
  184. Changes to Go must be reviewed before they are accepted, no matter who makes the change.
  185. A custom <code>git</code> command called <code>git-codereview</code>
  186. simplifies sending changes to Gerrit.
  187. </p>
  188. <p>
  189. Install the <code>git-codereview</code> command by running,
  190. </p>
  191. <pre>
  192. $ go get -u golang.org/x/review/git-codereview
  193. </pre>
  194. <p>
  195. Make sure <code>git-codereview</code> is installed in your shell path, so that the
  196. <code>git</code> command can find it.
  197. Check that
  198. </p>
  199. <pre>
  200. $ git codereview help
  201. </pre>
  202. <p>
  203. prints help text, not an error.
  204. </p>
  205. <p>
  206. On Windows, when using git-bash you must make sure that
  207. <code>git-codereview.exe</code> is in your <code>git</code> exec-path.
  208. Run <code>git --exec-path</code> to discover the right location then create a
  209. symbolic link or just copy the executable from $GOPATH/bin to this directory.
  210. </p>
  211. <h2 id="before_contributing">Before contributing code</h2>
  212. <p>
  213. The project welcomes code patches, but to make sure things are well
  214. coordinated you should discuss any significant change before starting
  215. the work.
  216. It's recommended that you signal your intention to contribute in the
  217. issue tracker, either by <a href="https://golang.org/issue/new">filing
  218. a new issue</a> or by claiming
  219. an <a href="https://golang.org/issues">existing one</a>.
  220. </p>
  221. <h3>Check the issue tracker</h3>
  222. <p>
  223. Whether you already know what contribution to make, or you are searching for
  224. an idea, the <a href="https://github.com/golang/go/issues">issue tracker</a> is
  225. always the first place to go.
  226. Issues are triaged to categorize them and manage the workflow.
  227. </p>
  228. <p>
  229. Most issues will be marked with one of the following workflow labels:
  230. </p>
  231. <ul>
  232. <li>
  233. <b>NeedsInvestigation</b>: The issue is not fully understood
  234. and requires analysis to understand the root cause.
  235. </li>
  236. <li>
  237. <b>NeedsDecision</b>: the issue is relatively well understood, but the
  238. Go team hasn't yet decided the best way to address it.
  239. It would be better to wait for a decision before writing code.
  240. If you are interested on working on an issue in this state,
  241. feel free to "ping" maintainers in the issue's comments
  242. if some time has passed without a decision.
  243. </li>
  244. <li>
  245. <b>NeedsFix</b>: the issue is fully understood and code can be written
  246. to fix it.
  247. </li>
  248. </ul>
  249. <p>
  250. You can use GitHub's search functionality to find issues to help out with. Examples:
  251. </p>
  252. <ul>
  253. <li>
  254. Issues that need investigation: <a href="https://github.com/golang/go/issues?q=is%3Aissue+is%3Aopen+label%3ANeedsInvestigation"><code>is:issue is:open label:NeedsInvestigation</code></a>
  255. </li>
  256. <li>
  257. Issues that need a fix: <a href="https://github.com/golang/go/issues?q=is%3Aissue+is%3Aopen+label%3ANeedsFix"><code>is:issue is:open label:NeedsFix</code></a>
  258. </li>
  259. <li>
  260. Issues that need a fix and have a CL: <a href="https://github.com/golang/go/issues?q=is%3Aissue+is%3Aopen+label%3ANeedsFix+%22golang.org%2Fcl%22"><code>is:issue is:open label:NeedsFix "golang.org/cl"</code></a>
  261. </li>
  262. <li>
  263. Issues that need a fix and do not have a CL: <a href="https://github.com/golang/go/issues?q=is%3Aissue+is%3Aopen+label%3ANeedsFix+NOT+%22golang.org%2Fcl%22"><code>is:issue is:open label:NeedsFix NOT "golang.org/cl"</code></a>
  264. </li>
  265. </ul>
  266. <h3 id="design">Open an issue for any new problem</h3>
  267. <p>
  268. Excluding very trivial changes, all contributions should be connected
  269. to an existing issue.
  270. Feel free to open one and discuss your plans.
  271. This process gives everyone a chance to validate the design,
  272. helps prevent duplication of effort,
  273. and ensures that the idea fits inside the goals for the language and tools.
  274. It also checks that the design is sound before code is written;
  275. the code review tool is not the place for high-level discussions.
  276. </p>
  277. <p>
  278. When planning work, please note that the Go project follows a <a
  279. href="https://golang.org/wiki/Go-Release-Cycle">six-month development cycle</a>.
  280. The latter half of each cycle is a three-month feature freeze during
  281. which only bug fixes and documentation updates are accepted.
  282. New contributions can be sent during a feature freeze, but they will
  283. not be merged until the freeze is over.
  284. </p>
  285. <p>
  286. Significant changes to the language, libraries, or tools must go
  287. through the
  288. <a href="https://golang.org/s/proposal-process">change proposal process</a>
  289. before they can be accepted.
  290. </p>
  291. <p>
  292. Sensitive security-related issues (only!) should be reported to <a href="mailto:security@golang.org">security@golang.org</a>.
  293. </p>
  294. <h2 id="sending_a_change_github">Sending a change via GitHub</h2>
  295. <p>
  296. First-time contributors that are already familiar with the
  297. <a href="https://guides.github.com/introduction/flow/">GitHub flow</a>
  298. are encouraged to use the same process for Go contributions.
  299. Even though Go
  300. maintainers use Gerrit for code review, a bot called Gopherbot has been created to sync
  301. GitHub pull requests to Gerrit.
  302. </p>
  303. <p>
  304. Open a pull request as you normally would.
  305. Gopherbot will create a corresponding Gerrit change and post a link to
  306. it on your GitHub pull request; updates to the pull request will also
  307. get reflected in the Gerrit change.
  308. When somebody comments on the change, their comment will be also
  309. posted in your pull request, so you will get a notification.
  310. </p>
  311. <p>
  312. Some things to keep in mind:
  313. </p>
  314. <ul>
  315. <li>
  316. To update the pull request with new code, just push it to the branch; you can either
  317. add more commits, or rebase and force-push (both styles are accepted).
  318. </li>
  319. <li>
  320. If the request is accepted, all commits will be squashed, and the final
  321. commit description will be composed by concatenating the pull request's
  322. title and description.
  323. The individual commits' descriptions will be discarded.
  324. See <a href="#commit_messages">Writing good commit messages</a> for some
  325. suggestions.
  326. </li>
  327. <li>
  328. Gopherbot is unable to sync line-by-line codereview into GitHub: only the
  329. contents of the overall comment on the request will be synced.
  330. Remember you can always visit Gerrit to see the fine-grained review.
  331. </li>
  332. </ul>
  333. <h2 id="sending_a_change_gerrit">Sending a change via Gerrit</h2>
  334. <p>
  335. It is not possible to fully sync Gerrit and GitHub, at least at the moment,
  336. so we recommend learning Gerrit.
  337. It's different but powerful and familiarity with it will help you understand
  338. the flow.
  339. </p>
  340. <h3>Overview</h3>
  341. <p>
  342. This is an overview of the overall process:
  343. </p>
  344. <ul>
  345. <li>
  346. <b>Step 1:</b> Clone the Go source code from <code>go.googlesource.com</code>
  347. and make sure it's stable by compiling and testing it once:
  348. <pre>
  349. $ git clone https://go.googlesource.com/go
  350. $ cd go/src
  351. $ ./all.bash # compile and test
  352. </pre>
  353. </li>
  354. <li>
  355. <b>Step 2:</b> Prepare changes in a new branch, created from the master branch.
  356. To commit the changes, use <code>git</code> <code>codereview</code> <code>change</code>; that
  357. will create or amend a single commit in the branch.
  358. <pre>
  359. $ git checkout -b mybranch
  360. $ [edit files...]
  361. $ git add [files...]
  362. $ git codereview change # create commit in the branch
  363. $ [edit again...]
  364. $ git add [files...]
  365. $ git codereview change # amend the existing commit with new changes
  366. $ [etc.]
  367. </pre>
  368. </li>
  369. <li>
  370. <b>Step 3:</b> Test your changes, re-running <code>all.bash</code>.
  371. <pre>
  372. $ ./all.bash # recompile and test
  373. </pre>
  374. </li>
  375. <li>
  376. <b>Step 4:</b> Send the changes for review to Gerrit using <code>git</code>
  377. <code>codereview</code> <code>mail</code> (which doesn't use e-mail, despite the name).
  378. <pre>
  379. $ git codereview mail # send changes to Gerrit
  380. </pre>
  381. </li>
  382. <li>
  383. <b>Step 5:</b> After a review, apply changes to the same single commit
  384. and mail them to Gerrit again:
  385. <pre>
  386. $ [edit files...]
  387. $ git add [files...]
  388. $ git codereview change # update same commit
  389. $ git codereview mail # send to Gerrit again
  390. </pre>
  391. </li>
  392. </ul>
  393. <p>
  394. The rest of this section describes these steps in more detail.
  395. </p>
  396. <h3 id="checkout_go">Step 1: Clone the Go source code</h3>
  397. <p>
  398. In addition to a recent Go installation, you need to have a local copy of the source
  399. checked out from the correct repository.
  400. You can check out the Go source repo onto your local file system anywhere
  401. you want as long as it's outside your <code>GOPATH</code>.
  402. Clone from <code>go.googlesource.com</code> (not GitHub):
  403. </p>
  404. <pre>
  405. $ git clone https://go.googlesource.com/go
  406. $ cd go
  407. </pre>
  408. <h3 id="make_branch">Step 2: Prepare changes in a new branch</h3>
  409. <p>
  410. Each Go change must be made in a separate branch, created from the master branch.
  411. You can use
  412. the normal <code>git</code> commands to create a branch and add changes to the
  413. staging area:
  414. </p>
  415. <pre>
  416. $ git checkout -b mybranch
  417. $ [edit files...]
  418. $ git add [files...]
  419. </pre>
  420. <p>
  421. To commit changes, instead of <code>git commit</code>, use <code>git codereview change</code>.
  422. </p>
  423. <pre>
  424. $ git codereview change
  425. (open $EDITOR)
  426. </pre>
  427. <p>
  428. You can edit the commit description in your favorite editor as usual.
  429. The <code>git</code> <code>codereview</code> <code>change</code> command
  430. will automatically add a unique Change-Id line near the bottom.
  431. That line is used by Gerrit to match successive uploads of the same change.
  432. Do not edit or delete it.
  433. A Change-Id looks like this:
  434. </p>
  435. <pre>
  436. Change-Id: I2fbdbffb3aab626c4b6f56348861b7909e3e8990
  437. </pre>
  438. <p>
  439. The tool also checks that you've
  440. run <code>go</code> <code>fmt</code> over the source code, and that
  441. the commit message follows the <a href="#commit_messages">suggested format</a>.
  442. </p>
  443. <p>
  444. If you need to edit the files again, you can stage the new changes and
  445. re-run <code>git</code> <code>codereview</code> <code>change</code>: each subsequent
  446. run will amend the existing commit while preserving the Change-Id.
  447. </p>
  448. <p>
  449. Make sure that you always keep a single commit in each branch.
  450. If you add more
  451. commits by mistake, you can use <code>git</code> <code>rebase</code> to
  452. <a href="https://stackoverflow.com/questions/31668794/squash-all-your-commits-in-one-before-a-pull-request-in-github">squash them together</a>
  453. into a single one.
  454. </p>
  455. <h3 id="testing">Step 3: Test your changes</h3>
  456. <p>
  457. You've <a href="code.html">written and tested your code</a>, but
  458. before sending code out for review, run <i>all the tests for the whole
  459. tree</i> to make sure the changes don't break other packages or programs:
  460. </p>
  461. <pre>
  462. $ cd go/src
  463. $ ./all.bash
  464. </pre>
  465. <p>
  466. (To build under Windows use <code>all.bat</code>; this also requires
  467. setting the environment variable <code>GOROOT_BOOTSTRAP</code> to the
  468. directory holding the Go tree for the bootstrap compiler.)
  469. </p>
  470. <p>
  471. After running for a while and printing a lot of testing output, the command should finish
  472. by printing,
  473. </p>
  474. <pre>
  475. ALL TESTS PASSED
  476. </pre>
  477. <p>
  478. You can use <code>make.bash</code> instead of <code>all.bash</code>
  479. to just build the compiler and the standard library without running the test suite.
  480. Once the <code>go</code> tool is built, it will be installed as <code>bin/go</code>
  481. under the directory in which you cloned the Go repository, and you can
  482. run it directly from there.
  483. See also
  484. the section on how to <a href="#quick_test">test your changes quickly</a>.
  485. </p>
  486. <h3 id="mail">Step 4: Send changes for review</h3>
  487. <p>
  488. Once the change is ready and tested over the whole tree, send it for review.
  489. This is done with the <code>mail</code> sub-command which, despite its name, doesn't
  490. directly mail anything; it just sends the change to Gerrit:
  491. </p>
  492. <pre>
  493. $ git codereview mail
  494. </pre>
  495. <p>
  496. Gerrit assigns your change a number and URL, which <code>git</code> <code>codereview</code> <code>mail</code> will print, something like:
  497. </p>
  498. <pre>
  499. remote: New Changes:
  500. remote: https://go-review.googlesource.com/99999 math: improved Sin, Cos and Tan precision for very large arguments
  501. </pre>
  502. <p>
  503. If you get an error instead, check the
  504. <a href="#troubleshooting_mail">Troubleshooting mail errors</a> section.
  505. </p>
  506. <p>
  507. If your change relates to an open GitHub issue and you have followed the <a href="#commit_messages">
  508. suggested commit message format</a>, the issue will be updated in a few minutes by a bot,
  509. linking your Gerrit change to it in the comments.
  510. </p>
  511. <h3 id="revise">Step 5: Revise changes after a review</h3>
  512. <p>
  513. Go maintainers will review your code on Gerrit, and you will get notifications via e-mail.
  514. You can see the review on Gerrit and comment on them there.
  515. You can also reply
  516. <a href="https://gerrit-review.googlesource.com/Documentation/intro-user.html#reply-by-email">using e-mail</a>
  517. if you prefer.
  518. </p>
  519. <p>
  520. If you need to revise your change after the review, edit the files in
  521. the same branch you previously created, add them to the Git staging
  522. area, and then amend the commit with
  523. <code>git</code> <code>codereview</code> <code>change</code>:
  524. </p>
  525. <pre>
  526. $ git codereview change # amend current commit
  527. (open $EDITOR)
  528. $ git codereview mail # send new changes to Gerrit
  529. </pre>
  530. <p>
  531. If you don't need to change the commit description, just save and exit from the editor.
  532. Remember not to touch the special Change-Id line.
  533. </p>
  534. <p>
  535. Again, make sure that you always keep a single commit in each branch.
  536. If you add more
  537. commits by mistake, you can use <code>git rebase</code> to
  538. <a href="https://stackoverflow.com/questions/31668794/squash-all-your-commits-in-one-before-a-pull-request-in-github">squash them together</a>
  539. into a single one.
  540. </p>
  541. <h2 id="commit_messages">Good commit messages</h2>
  542. <p>
  543. Commit messages in Go follow a specific set of conventions,
  544. which we discuss in this section.
  545. </p>
  546. <p>
  547. Here is an example of a good one:
  548. </p>
  549. <pre>
  550. math: improve Sin, Cos and Tan precision for very large arguments
  551. The existing implementation has poor numerical properties for
  552. large arguments, so use the McGillicutty algorithm to improve
  553. accuracy above 1e10.
  554. The algorithm is described at https://wikipedia.org/wiki/McGillicutty_Algorithm
  555. Fixes #159
  556. </pre>
  557. <h3>First line</h3>
  558. <p>
  559. The first line of the change description is conventionally a short one-line
  560. summary of the change, prefixed by the primary affected package.
  561. </p>
  562. <p>
  563. A rule of thumb is that it should be written so to complete the sentence
  564. "This change modifies Go to _____."
  565. That means it does not start with a capital letter, is not a complete sentence,
  566. and actually summarizes the result of the change.
  567. </p>
  568. <p>
  569. Follow the first line by a blank line.
  570. </p>
  571. <h3>Main content</h3>
  572. <p>
  573. The rest of the description elaborates and should provide context for the
  574. change and explain what it does.
  575. Write in complete sentences with correct punctuation, just like
  576. for your comments in Go.
  577. Don't use HTML, Markdown, or any other markup language.
  578. </p>
  579. <p>
  580. Add any relevant information, such as benchmark data if the change
  581. affects performance.
  582. The <a href="https://godoc.org/golang.org/x/perf/cmd/benchstat">benchstat</a>
  583. tool is conventionally used to format
  584. benchmark data for change descriptions.
  585. </p>
  586. <h3>Referencing issues</h3>
  587. <p>
  588. The special notation "Fixes #12345" associates the change with issue 12345 in the
  589. <a href="https://golang.org/issue/12345">Go issue tracker</a>.
  590. When this change is eventually applied, the issue
  591. tracker will automatically mark the issue as fixed.
  592. </p>
  593. <p>
  594. If the change is a partial step towards the resolution of the issue,
  595. uses the notation "Updates #12345".
  596. This will leave a comment in the issue
  597. linking back to the change in Gerrit, but it will not close the issue
  598. when the change is applied.
  599. </p>
  600. <p>
  601. If you are sending a change against a subrepository, you must use
  602. the fully-qualified syntax supported by GitHub to make sure the change is
  603. linked to the issue in the main repository, not the subrepository.
  604. All issues are tracked in the main repository's issue tracker.
  605. The correct form is "Fixes golang/go#159".
  606. </p>
  607. <h2 id="review">The review process</h2>
  608. <p>
  609. This section explains the review process in detail and how to approach
  610. reviews after a change has been mailed.
  611. </p>
  612. <h3 id="mistakes">Common beginner mistakes</h3>
  613. <p>
  614. When a change is sent to Gerrit, it is usually triaged within a few days.
  615. A maintainer will have a look and provide some initial review that for first-time
  616. contributors usually focuses on basic cosmetics and common mistakes.
  617. These include things like:
  618. </p>
  619. <ul>
  620. <li>
  621. Commit message not following the <a href="#commit_messages">suggested
  622. format</a>.
  623. </li>
  624. <li>
  625. The lack of a linked GitHub issue.
  626. The vast majority of changes
  627. require a linked issue that describes the bug or the feature that the change
  628. fixes or implements, and consensus should have been reached on the tracker
  629. before proceeding with it.
  630. Gerrit reviews do not discuss the merit of the change,
  631. just its implementation.
  632. <br>
  633. Only trivial or cosmetic changes will be accepted without an associated issue.
  634. </li>
  635. <li>
  636. Change sent during the freeze phase of the development cycle, when the tree
  637. is closed for general changes.
  638. In this case,
  639. a maintainer might review the code with a line such as <code>R=go1.12</code>,
  640. which means that it will be reviewed later when the tree opens for a new
  641. development window.
  642. You can add <code>R=go1.XX</code> as a comment yourself
  643. if you know that it's not the correct time frame for the change.
  644. </li>
  645. </ul>
  646. <h3 id="trybots">Trybots</h3>
  647. <p>
  648. After an initial reading of your change, maintainers will trigger trybots,
  649. a cluster of servers that will run the full test suite on several different
  650. architectures.
  651. Most trybots complete in a few minutes, at which point a link will
  652. be posted in Gerrit where you can see the results.
  653. </p>
  654. <p>
  655. If the trybot run fails, follow the link and check the full logs of the
  656. platforms on which the tests failed.
  657. Try to understand what broke, update your patch to fix it, and upload again.
  658. Maintainers will trigger a new trybot run to see
  659. if the problem was fixed.
  660. </p>
  661. <p>
  662. Sometimes, the tree can be broken on some platforms for a few hours; if
  663. the failure reported by the trybot doesn't seem related to your patch, go to the
  664. <a href="https://build.golang.org">Build Dashboard</a> and check if the same
  665. failure appears in other recent commits on the same platform.
  666. In this case,
  667. feel free to write a comment in Gerrit to mention that the failure is
  668. unrelated to your change, to help maintainers understand the situation.
  669. </p>
  670. <h3 id="reviews">Reviews</h3>
  671. <p>
  672. The Go community values very thorough reviews.
  673. Think of each review comment like a ticket: you are expected to somehow "close" it
  674. by acting on it, either by implementing the suggestion or convincing the
  675. reviewer otherwise.
  676. </p>
  677. <p>
  678. After you update the change, go through the review comments and make sure
  679. to reply to every one.
  680. You can click the "Done" button to reply
  681. indicating that you've implemented the reviewer's suggestion; otherwise,
  682. click on "Reply" and explain why you have not, or what you have done instead.
  683. </p>
  684. <p>
  685. It is perfectly normal for changes to go through several round of reviews,
  686. with one or more reviewers making new comments every time
  687. and then waiting for an updated change before reviewing again.
  688. This cycle happens even for experienced contributors, so
  689. don't be discouraged by it.
  690. </p>
  691. <h3 id="votes">Voting conventions</h3>
  692. <p>
  693. As they near a decision, reviewers will make a "vote" on your change.
  694. The Gerrit voting system involves an integer in the range -2 to +2:
  695. </p>
  696. <ul>
  697. <li>
  698. <b>+2</b> The change is approved for being merged.
  699. Only Go maintainers can cast a +2 vote.
  700. </li>
  701. <li>
  702. <b>+1</b> The change looks good, but either the reviewer is requesting
  703. minor changes before approving it, or they are not a maintainer and cannot
  704. approve it, but would like to encourage an approval.
  705. </li>
  706. <li>
  707. <b>-1</b> The change is not good the way it is but might be fixable.
  708. A -1 vote will always have a comment explaining why the change is unacceptable.
  709. </li>
  710. <li>
  711. <b>-2</b> The change is blocked by a maintainer and cannot be approved.
  712. Again, there will be a comment explaining the decision.
  713. </li>
  714. </ul>
  715. <h3 id="submit">Submitting an approved change</h3>
  716. <p>
  717. After the code has been +2'ed, an approver will
  718. apply it to the master branch using the Gerrit user interface.
  719. This is called "submitting the change".
  720. </p>
  721. <p>
  722. The two steps (approving and submitting) are separate because in some cases maintainers
  723. may want to approve it but not to submit it right away (for instance,
  724. the tree could be temporarily frozen).
  725. </p>
  726. <p>
  727. Submitting a change checks it into the repository.
  728. The change description will include a link to the code review,
  729. which will be updated with a link to the change
  730. in the repository.
  731. Since the method used to integrate the changes is Git's "Cherry Pick",
  732. the commit hashes in the repository will be changed by
  733. the submit operation.
  734. </p>
  735. <p>
  736. If your change has been approved for a few days without being
  737. submitted, feel free to write a comment in Gerrit requesting
  738. submission.
  739. </p>
  740. <h3 id="more_information">More information</h3>
  741. <p>
  742. In addition to the information here, the Go community maintains a <a
  743. href="https://golang.org/wiki/CodeReview">CodeReview</a> wiki page.
  744. Feel free to contribute to this page as you learn more about the review process.
  745. </p>
  746. <h2 id="advanced_topics">Miscellaneous topics</h2>
  747. <p>
  748. This section collects a number of other comments that are
  749. outside the issue/edit/code review/submit process itself.
  750. </p>
  751. <h3 id="copyright">Copyright headers</h3>
  752. <p>
  753. Files in the Go repository don't list author names, both to avoid clutter
  754. and to avoid having to keep the lists up to date.
  755. Instead, your name will appear in the
  756. <a href="https://golang.org/change">change log</a> and in the <a
  757. href="/CONTRIBUTORS"><code>CONTRIBUTORS</code></a> file and perhaps the <a
  758. href="/AUTHORS"><code>AUTHORS</code></a> file.
  759. These files are automatically generated from the commit logs periodically.
  760. The <a href="/AUTHORS"><code>AUTHORS</code></a> file defines who &ldquo;The Go
  761. Authors&rdquo;&mdash;the copyright holders&mdash;are.
  762. </p>
  763. <p>
  764. New files that you contribute should use the standard copyright header:
  765. </p>
  766. <pre>
  767. // Copyright 2019 The Go Authors. All rights reserved.
  768. // Use of this source code is governed by a BSD-style
  769. // license that can be found in the LICENSE file.
  770. </pre>
  771. <p>
  772. (Use the current year if you're reading this in 2020 or beyond.)
  773. Files in the repository are copyrighted the year they are added.
  774. Do not update the copyright year on files that you change.
  775. </p>
  776. <h3 id="troubleshooting_mail">Troubleshooting mail errors</h3>
  777. <p>
  778. The most common way that the <code>git</code> <code>codereview</code> <code>mail</code>
  779. command fails is because the e-mail address in the commit does not match the one
  780. that you used during <a href="#google_account">the registration process</a>.
  781. <br>
  782. If you see something like...
  783. </p>
  784. <pre>
  785. remote: Processing changes: refs: 1, done
  786. remote:
  787. remote: ERROR: In commit ab13517fa29487dcf8b0d48916c51639426c5ee9
  788. remote: ERROR: author email address XXXXXXXXXXXXXXXXXXX
  789. remote: ERROR: does not match your user account.
  790. </pre>
  791. <p>
  792. you need to configure Git for this repository to use the
  793. e-mail address that you registered with.
  794. To change the e-mail address to ensure this doesn't happen again, run:
  795. </p>
  796. <pre>
  797. $ git config user.email email@address.com
  798. </pre>
  799. <p>
  800. Then change the commit to use this alternative e-mail address with this command:
  801. </p>
  802. <pre>
  803. $ git commit --amend --author="Author Name &lt;email@address.com&gt;"
  804. </pre>
  805. <p>
  806. Then retry by running:
  807. </p>
  808. <pre>
  809. $ git codereview mail
  810. </pre>
  811. <h3 id="quick_test">Quickly testing your changes</h3>
  812. <p>
  813. Running <code>all.bash</code> for every single change to the code tree
  814. is burdensome.
  815. Even though it is strongly suggested to run it before
  816. sending a change, during the normal development cycle you may want
  817. to compile and test only the package you are developing.
  818. </p>
  819. <ul>
  820. <li>
  821. In general, you can run <code>make.bash</code> instead of <code>all.bash</code>
  822. to only rebuild the Go tool chain without running the whole test suite.
  823. Or you
  824. can run <code>run.bash</code> to only run the whole test suite without rebuilding
  825. the tool chain.
  826. You can think of <code>all.bash</code> as <code>make.bash</code>
  827. followed by <code>run.bash</code>.
  828. </li>
  829. <li>
  830. In this section, we'll call the directory into which you cloned the Go repository <code>$GODIR</code>.
  831. The <code>go</code> tool built by <code>$GODIR/make.bash</code> will be installed
  832. in <code>$GODIR/bin/go</code> and you
  833. can invoke it to test your code.
  834. For instance, if you
  835. have modified the compiler and you want to test how it affects the
  836. test suite of your own project, just run <code>go</code> <code>test</code>
  837. using it:
  838. <pre>
  839. $ cd &lt;MYPROJECTDIR&gt;
  840. $ $GODIR/bin/go test
  841. </pre>
  842. </li>
  843. <li>
  844. If you're changing the standard library, you probably don't need to rebuild
  845. the compiler: you can just run the tests for the package you've changed.
  846. You can do that either with the Go version you normally use, or
  847. with the Go compiler built from your clone (which is
  848. sometimes required because the standard library code you're modifying
  849. might require a newer version than the stable one you have installed).
  850. <pre>
  851. $ cd $GODIR/src/hash/sha1
  852. $ [make changes...]
  853. $ $GODIR/bin/go test .
  854. </pre>
  855. </li>
  856. <li>
  857. If you're modifying the compiler itself, you can just recompile
  858. the <code>compile</code> tool (which is the internal binary invoked
  859. by <code>go</code> <code>build</code> to compile each single package).
  860. After that, you will want to test it by compiling or running something.
  861. <pre>
  862. $ cd $GODIR/src
  863. $ [make changes...]
  864. $ $GODIR/bin/go install cmd/compile
  865. $ $GODIR/bin/go build [something...] # test the new compiler
  866. $ $GODIR/bin/go run [something...] # test the new compiler
  867. $ $GODIR/bin/go test [something...] # test the new compiler
  868. </pre>
  869. The same applies to other internal tools of the Go tool chain,
  870. such as <code>asm</code>, <code>cover</code>, <code>link</code>, and so on.
  871. Just recompile and install the tool using <code>go</code>
  872. <code>install</code> <code>cmd/&lt;TOOL&gt;</code> and then use
  873. the built Go binary to test it.
  874. </li>
  875. <li>
  876. In addition to the standard per-package tests, there is a top-level
  877. test suite in <code>$GODIR/test</code> that contains
  878. several black-box and regression tests.
  879. The test suite is run
  880. by <code>all.bash</code> but you can also run it manually:
  881. <pre>
  882. $ cd $GODIR/test
  883. $ $GODIR/bin/go run run.go
  884. </pre>
  885. </ul>
  886. <h3 id="subrepos">Contributing to subrepositories (golang.org/x/...)</h3>
  887. <p>
  888. If you are contributing a change to a subrepository, obtain the
  889. Go package using <code>go get</code>.
  890. For example, to contribute
  891. to <code>golang.org/x/oauth2</code>, check out the code by running:
  892. </p>
  893. <pre>
  894. $ go get -d golang.org/x/oauth2/...
  895. </pre>
  896. <p>
  897. Then, change your directory to the package's source directory
  898. (<code>$GOPATH/src/golang.org/x/oauth2</code>), and follow the
  899. normal contribution flow.
  900. </p>
  901. <h3 id="cc">Specifying a reviewer / CCing others</h3>
  902. <p>
  903. Unless explicitly told otherwise, such as in the discussion leading
  904. up to sending in the change, it's better not to specify a reviewer.
  905. All changes are automatically CC'ed to the
  906. <a href="https://groups.google.com/group/golang-codereviews">golang-codereviews@googlegroups.com</a>
  907. mailing list.
  908. If this is your first ever change, there may be a moderation
  909. delay before it appears on the mailing list, to prevent spam.
  910. </p>
  911. <p>
  912. You can specify a reviewer or CC interested parties
  913. using the <code>-r</code> or <code>-cc</code> options.
  914. Both accept a comma-separated list of e-mail addresses:
  915. </p>
  916. <pre>
  917. $ git codereview mail -r joe@golang.org -cc mabel@example.com,math-nuts@swtch.com
  918. </pre>
  919. <h3 id="sync">Synchronize your client</h3>
  920. <p>
  921. While you were working, others might have submitted changes to the repository.
  922. To update your local branch, run
  923. </p>
  924. <pre>
  925. $ git codereview sync
  926. </pre>
  927. <p>
  928. (Under the covers this runs
  929. <code>git</code> <code>pull</code> <code>-r</code>.)
  930. </p>
  931. <h3 id="download">Reviewing code by others</h3>
  932. <p>
  933. As part of the review process reviewers can propose changes directly (in the
  934. GitHub workflow this would be someone else attaching commits to a pull request).
  935. You can import these changes proposed by someone else into your local Git repository.
  936. On the Gerrit review page, click the "Download ▼" link in the upper right
  937. corner, copy the "Checkout" command and run it from your local Git repo.
  938. It will look something like this:
  939. </p>
  940. <pre>
  941. $ git fetch https://go.googlesource.com/review refs/changes/21/13245/1 &amp;&amp; git checkout FETCH_HEAD
  942. </pre>
  943. <p>
  944. To revert, change back to the branch you were working in.
  945. </p>
  946. <h3 id="git-config">Set up git aliases</h3>
  947. <p>
  948. The <code>git-codereview</code> command can be run directly from the shell
  949. by typing, for instance,
  950. </p>
  951. <pre>
  952. $ git codereview sync
  953. </pre>
  954. <p>
  955. but it is more convenient to set up aliases for <code>git-codereview</code>'s own
  956. subcommands, so that the above becomes,
  957. </p>
  958. <pre>
  959. $ git sync
  960. </pre>
  961. <p>
  962. The <code>git-codereview</code> subcommands have been chosen to be distinct from
  963. Git's own, so it's safe to define these aliases.
  964. To install them, copy this text into your
  965. Git configuration file (usually <code>.gitconfig</code> in your home directory):
  966. </p>
  967. <pre>
  968. [alias]
  969. change = codereview change
  970. gofmt = codereview gofmt
  971. mail = codereview mail
  972. pending = codereview pending
  973. submit = codereview submit
  974. sync = codereview sync
  975. </pre>
  976. <h3 id="multiple_changes">Sending multiple dependent changes</h3>
  977. <p>
  978. Advanced users may want to stack up related commits in a single branch.
  979. Gerrit allows for changes to be dependent on each other, forming such a dependency chain.
  980. Each change will need to be approved and submitted separately but the dependency
  981. will be visible to reviewers.
  982. </p>
  983. <p>
  984. To send out a group of dependent changes, keep each change as a different commit under
  985. the same branch, and then run:
  986. </p>
  987. <pre>
  988. $ git codereview mail HEAD
  989. </pre>
  990. <p>
  991. Make sure to explicitly specify <code>HEAD</code>, which is usually not required when sending
  992. single changes.
  993. </p>