Marian Harbach | ebeb154 | 2019-12-13 10:42:46 +0100 | [diff] [blame] | 1 | :linkattrs: |
Edwin Kempin | 34be410 | 2019-04-24 09:44:22 +0200 | [diff] [blame] | 2 | = Gerrit Code Review - Crafting Changes |
| 3 | |
| 4 | Here are some hints as to what approvers may be looking for |
| 5 | before approving or submitting changes to the Gerrit project. |
| 6 | Let's start with the simple nit picky stuff. You are likely |
| 7 | excited that your code works; help us share your excitement |
| 8 | by not distracting us with the simple stuff. Thanks to Gerrit, |
| 9 | problems are often highlighted and we find it hard to look |
| 10 | beyond simple spacing issues. Blame it on our short attention |
| 11 | spans, we really do want your code. |
| 12 | |
| 13 | [[commit-message]] |
| 14 | == Commit Message |
| 15 | |
| 16 | It is essential to have a good commit message if you want your |
| 17 | change to be reviewed. |
| 18 | |
| 19 | * Keep lines no longer than 72 chars |
| 20 | * Start with a short one line summary |
| 21 | * Followed by a blank line |
| 22 | * Followed by one or more explanatory paragraphs |
| 23 | * Use the present tense (fix instead of fixed) |
| 24 | * Use the past tense when describing the status before this commit |
| 25 | * Include a `Bug: Issue <#>` line if fixing a Gerrit issue, or a |
| 26 | `Feature: Issue <#>` line if implementing a feature request. |
| 27 | * Include a `Change-Id` line |
| 28 | |
| 29 | [[vim-setup]] |
| 30 | === Setting up Vim for Git commit message |
| 31 | |
| 32 | Git uses Vim as the default commit message editor. Put this into your |
| 33 | `$HOME/.vimrc` file to configure Vim for Git commit message formatting |
| 34 | and writing: |
| 35 | |
| 36 | ==== |
| 37 | " Enable spell checking, which is not on by default for commit messages. |
| 38 | au FileType gitcommit setlocal spell |
| 39 | |
| 40 | " Reset textwidth if you've previously overridden it. |
| 41 | au FileType gitcommit setlocal textwidth=72 |
| 42 | ==== |
| 43 | |
| 44 | |
| 45 | [[git-commit-settings]] |
| 46 | === A sample good Gerrit commit message: |
| 47 | ==== |
| 48 | Add sample commit message to guidelines doc |
| 49 | |
| 50 | The original patch set for the contributing guidelines doc did not |
| 51 | include a sample commit message, this new patchset does. Hopefully this |
| 52 | makes things a bit clearer since examples can sometimes help when |
| 53 | explanations don't. |
| 54 | |
| 55 | Note that the body of this commit message can be several paragraphs, and |
| 56 | that I word wrap it at 72 characters. Also note that I keep the summary |
| 57 | line under 50 characters since it is often truncated by tools which |
| 58 | display just the git summary. |
| 59 | |
| 60 | Bug: Issue 98765605 |
| 61 | Change-Id: Ic4a7c07eeb98cdeaf44e9d231a65a51f3fceae52 |
| 62 | ==== |
| 63 | |
| 64 | The `Change-Id` line is, as usual, created by a local git hook. To install it, |
| 65 | simply copy it from the checkout and make it executable: |
| 66 | |
| 67 | ==== |
| 68 | cp ./gerrit-server/src/main/resources/com/google/gerrit/server/tools/root/hooks/commit-msg .git/hooks/ |
| 69 | chmod +x .git/hooks/commit-msg |
| 70 | ==== |
| 71 | |
| 72 | If you are working on core plugins, you will also need to install the |
| 73 | same hook in the submodules: |
| 74 | |
| 75 | ==== |
| 76 | export hook=$(pwd)/.git/hooks/commit-msg |
| 77 | git submodule foreach 'cp -p "$hook" "$(git rev-parse --git-dir)/hooks/"' |
| 78 | ==== |
| 79 | |
| 80 | |
| 81 | To set up git's remote for easy pushing, run the following: |
| 82 | |
| 83 | ==== |
| 84 | git remote add gerrit https://gerrit.googlesource.com/gerrit |
| 85 | ==== |
| 86 | |
| 87 | The HTTPS access requires proper username and password; this can be obtained |
| 88 | by clicking the 'Obtain Password' link on the |
| 89 | link:https://gerrit-review.googlesource.com/#/settings/http-password[HTTP |
Marian Harbach | 3425337 | 2019-12-10 18:01:31 +0100 | [diff] [blame] | 90 | Password tab of the user settings page,role=external,window=_blank]. |
Edwin Kempin | 34be410 | 2019-04-24 09:44:22 +0200 | [diff] [blame] | 91 | |
David Pursehouse | 923318b | 2019-08-30 10:55:05 +0900 | [diff] [blame] | 92 | Alternately, you may use the |
Marian Harbach | 3425337 | 2019-12-10 18:01:31 +0100 | [diff] [blame] | 93 | link:https://pypi.org/project/git-review/[git-review,role=external,window=_blank] tool to submit changes |
David Pursehouse | 923318b | 2019-08-30 10:55:05 +0900 | [diff] [blame] | 94 | to Gerrit. If you do, it will set up the Change-Id hook and `gerrit` remote |
| 95 | for you. You will still need to do the HTTP access step. |
| 96 | |
Edwin Kempin | 34be410 | 2019-04-24 09:44:22 +0200 | [diff] [blame] | 97 | [[style]] |
| 98 | == Style |
| 99 | |
| 100 | This project has a policy of Eclipse's warning free code. Eclipse |
| 101 | configuration is added to git and we expect the changes to be |
| 102 | warnings free. |
| 103 | |
| 104 | We do not ask you to use Eclipse for editing, obviously. We do ask you |
| 105 | to provide Eclipse's warning free patches only. If for some reasons, you |
| 106 | are not able to set up Eclipse and verify, that your patch hasn't |
| 107 | introduced any new Eclipse warnings, mention this in a comment to your |
| 108 | change, so that reviewers will do it for you. Yes, the way to go is to |
| 109 | extend gerrit CI to take care of this, but it's not yet implemented. |
| 110 | |
David Pursehouse | b8a0399 | 2020-05-14 08:55:16 +0900 | [diff] [blame] | 111 | Gerrit follows the |
Edwin Kempin | 34be410 | 2019-04-24 09:44:22 +0200 | [diff] [blame] | 112 | link:https://google.github.io/styleguide/javaguide.html[Google Java Style |
Marian Harbach | 3425337 | 2019-12-10 18:01:31 +0100 | [diff] [blame] | 113 | Guide,role=external,window=_blank]. |
Edwin Kempin | 34be410 | 2019-04-24 09:44:22 +0200 | [diff] [blame] | 114 | |
| 115 | To format Java source code, Gerrit uses the |
Marian Harbach | 3425337 | 2019-12-10 18:01:31 +0100 | [diff] [blame] | 116 | link:https://github.com/google/google-java-format[`google-java-format`,role=external,window=_blank] |
Edwin Kempin | 34be410 | 2019-04-24 09:44:22 +0200 | [diff] [blame] | 117 | tool (version 1.7), and to format Bazel BUILD, WORKSPACE and .bzl files the |
Marian Harbach | 3425337 | 2019-12-10 18:01:31 +0100 | [diff] [blame] | 118 | link:https://github.com/bazelbuild/buildtools/tree/master/buildifier[`buildifier`,role=external,window=_blank] |
Marco Miller | ce85bda | 2020-04-29 13:40:25 -0400 | [diff] [blame] | 119 | tool (version 3.0.0). Unused dependencies are found and removed using the |
David Pursehouse | bfe2d14 | 2020-01-29 18:02:53 +0900 | [diff] [blame] | 120 | link:https://github.com/bazelbuild/buildtools/tree/master/unused_deps[`unused_deps`,role=external,window=_blank] |
David Pursehouse | 6866810 | 2020-01-29 15:52:06 +0900 | [diff] [blame] | 121 | build tool, a sibling of `buildifier`. |
| 122 | |
Edwin Kempin | 34be410 | 2019-04-24 09:44:22 +0200 | [diff] [blame] | 123 | These tools automatically apply format according to the style guides; this |
| 124 | streamlines code review by reducing the need for time-consuming, tedious, |
| 125 | and contentious discussions about trivial issues like whitespace. |
| 126 | |
| 127 | You may download and run `google-java-format` on your own, or you may |
| 128 | run `./tools/setup_gjf.sh` to download a local copy and set up a |
| 129 | wrapper script. If you run your own copy, please use the same version, |
| 130 | as there may be slight differences between versions. |
| 131 | |
Edwin Kempin | 34be410 | 2019-04-24 09:44:22 +0200 | [diff] [blame] | 132 | When to use `final` modifier and when not (in new code): |
| 133 | |
| 134 | Always: |
| 135 | |
| 136 | * final fields: marking fields as final forces them to be |
| 137 | initialized in the constructor or at declaration |
| 138 | * final static fields: clearly communicates the intent |
| 139 | * to use final variables in inner anonymous classes |
| 140 | |
| 141 | Optional: |
| 142 | |
| 143 | * final classes: use when appropriate, e.g. API restriction |
| 144 | * final methods: similar to final classes |
| 145 | |
| 146 | Never: |
| 147 | |
| 148 | * local variables: it clutters the code, and makes the code less |
| 149 | readable. When copying old code to new location, finals should |
| 150 | be removed |
| 151 | * method parameters: similar to local variables |
| 152 | |
| 153 | [[code-organization]] |
| 154 | == Code Organization |
| 155 | |
| 156 | Do your best to organize classes and methods in a logical way. |
| 157 | Here are some guidelines that Gerrit uses: |
| 158 | |
| 159 | * Ensure a standard copyright header is included at the top |
| 160 | of any new files (copy it from another file, update the year). |
| 161 | * Always place loggers first in your class! |
| 162 | * Define any static interfaces next in your class. |
| 163 | * Define non static interfaces after static interfaces in your |
| 164 | class. |
| 165 | * Next you should define static types, static members, and |
| 166 | static methods, in decreasing order of visibility (public to private). |
| 167 | * Finally instance types, instance members, then constructors, |
| 168 | and then instance methods. |
| 169 | * Some common exceptions are private helper static methods, which |
| 170 | might appear near the instance methods which they help (but may |
| 171 | also appear at the top). |
| 172 | * Getters and setters for the same instance field should usually |
| 173 | be near each other barring a good reason not to. |
| 174 | * If you are using assisted injection, the factory for your class |
| 175 | should be before the instance members. |
| 176 | * Annotations should go before language keywords (`final`, `private`, etc) + |
| 177 | Example: `@Assisted @Nullable final type varName` |
| 178 | * Prefer to open multiple AutoCloseable resources in the same |
| 179 | try-with-resources block instead of nesting the try-with-resources |
| 180 | blocks and increasing the indentation level more than necessary. |
| 181 | |
| 182 | Wow that's a lot! But don't worry, you'll get the habit and most |
| 183 | of the code is organized this way already; so if you pay attention |
| 184 | to the class you are editing you will likely pick up on it. |
| 185 | Naturally new classes are a little harder; you may want to come |
| 186 | back and consult this section when creating them. |
| 187 | |
| 188 | [[design]] |
| 189 | == Design |
| 190 | |
| 191 | Here are some design level objectives that you should keep in mind |
| 192 | when coding: |
| 193 | |
| 194 | * Most client pages should perform only one RPC to load so as to |
| 195 | keep latencies down. Exceptions would apply to RPCs which need |
| 196 | to load large data sets if splitting them out will help the |
| 197 | page load faster. Generally page loads are expected to complete |
| 198 | in under 100ms. This will be the case for most operations, |
| 199 | unless the data being fetched is not using Gerrit's caching |
| 200 | infrastructure. In these slower cases, it is worth considering |
| 201 | mitigating this longer load by using a second RPC to fill in |
| 202 | this data after the page is displayed (or alternatively it might |
| 203 | be worth proposing caching this data). |
| 204 | * `@Inject` should be used on constructors, not on fields. The |
| 205 | current exceptions are the ssh commands, these were implemented |
| 206 | earlier in Gerrit's development. To stay consistent, new ssh |
| 207 | commands should follow this older pattern; but eventually these |
| 208 | should get converted to eliminate this exception. |
Edwin Kempin | 9d4cc9e | 2019-06-12 17:03:39 +0200 | [diff] [blame] | 209 | * Don't leave repository objects (git or schema) open. Use a |
| 210 | try-with-resources statement to ensure that repository objects get |
| 211 | closed after use. |
Edwin Kempin | 34be410 | 2019-04-24 09:44:22 +0200 | [diff] [blame] | 212 | * Don't leave UI components, which can cause new actions to occur, |
| 213 | enabled during RPCs which update Git repositories, including NoteDb. |
| 214 | This is to prevent people from submitting actions more than once |
| 215 | when operating on slow links. If the action buttons are disabled, |
| 216 | they cannot be resubmitted and the user can see that Gerrit is still |
| 217 | busy. |
Edwin Kempin | 34be410 | 2019-04-24 09:44:22 +0200 | [diff] [blame] | 218 | |
| 219 | [[tests]] |
| 220 | == Tests |
| 221 | |
| 222 | * Tests for new code will greatly help your change get approved. |
| 223 | |
Edwin Kempin | 6f3879d | 2019-11-19 12:24:51 +0100 | [diff] [blame] | 224 | [[javadoc]] |
| 225 | == Javadoc |
| 226 | |
| 227 | * Javadocs for new code (especially public classes and |
| 228 | public/protected methods) will greatly help your change get |
| 229 | approved. |
| 230 | |
Edwin Kempin | 34be410 | 2019-04-24 09:44:22 +0200 | [diff] [blame] | 231 | [[change-size]] |
| 232 | == Change Size/Number of Files Touched |
| 233 | |
| 234 | And finally, I probably cannot say enough about change sizes. |
| 235 | Generally, smaller is better, hopefully within reason. Do try to |
| 236 | keep things which will be confusing on their own together, |
| 237 | especially if changing one without the other will break something! |
| 238 | |
| 239 | * If a new feature is implemented and it is a larger one, try to |
| 240 | identify if it can be split into smaller logical features; when |
| 241 | in doubt, err on the smaller side. |
| 242 | * Separate bug fixes from feature improvements. The bug fix may |
| 243 | be an easy candidate for approval and should not need to wait |
| 244 | for new features to be approved. Also, combining the two makes |
| 245 | reviewing harder since then there is no clear line between the |
| 246 | fix and the feature. |
| 247 | * Separate supporting refactoring from feature changes. If your |
| 248 | new feature requires some refactoring, it helps to make the |
| 249 | refactoring a separate change which your feature change |
| 250 | depends on. This way, reviewers can easily review the refactor |
| 251 | change as a something that should not alter the current |
| 252 | functionality, and feel more confident they can more easily |
| 253 | spot errors this way. Of course, it also makes it easier to |
| 254 | test and locate later on if an unfortunate error does slip in. |
| 255 | Lastly, by not having to see refactoring changes at the same |
| 256 | time, it helps reviewers understand how your feature changes |
| 257 | the current functionality. |
| 258 | * Separate logical features into separate changes. This |
| 259 | is often the hardest part. Here is an example: when adding a |
| 260 | new ability, make separate changes for the UI and the ssh |
| 261 | commands if possible. |
| 262 | * Do only what the commit message describes. In other words, things which |
| 263 | are not strictly related to the commit message shouldn't be part of |
| 264 | a change, even trivial things like externalizing a string somewhere |
| 265 | or fixing a typo. This helps keep `git blame` more useful in the future |
| 266 | and it also makes `git revert` more useful. |
| 267 | * Use topics to link your separate changes together. |
| 268 | |
| 269 | GERRIT |
| 270 | ------ |
| 271 | Part of link:index.html[Gerrit Code Review] |
| 272 | |
| 273 | SEARCHBOX |
| 274 | --------- |