blob: 5a54c5f2c84ac6a762a15c31f1970dbb8227bae0 [file] [log] [blame]
Marian Harbachebeb1542019-12-13 10:42:46 +01001:linkattrs:
Edwin Kempin34be4102019-04-24 09:44:22 +02002= Gerrit Code Review - Crafting Changes
3
4Here are some hints as to what approvers may be looking for
5before approving or submitting changes to the Gerrit project.
6Let's start with the simple nit picky stuff. You are likely
7excited that your code works; help us share your excitement
8by not distracting us with the simple stuff. Thanks to Gerrit,
9problems are often highlighted and we find it hard to look
10beyond simple spacing issues. Blame it on our short attention
11spans, we really do want your code.
12
13[[commit-message]]
14== Commit Message
15
16It is essential to have a good commit message if you want your
17change 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
32Git uses Vim as the default commit message editor. Put this into your
33`$HOME/.vimrc` file to configure Vim for Git commit message formatting
34and 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
64The `Change-Id` line is, as usual, created by a local git hook. To install it,
65simply 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
72If you are working on core plugins, you will also need to install the
73same 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
81To 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
87The HTTPS access requires proper username and password; this can be obtained
88by clicking the 'Obtain Password' link on the
89link:https://gerrit-review.googlesource.com/#/settings/http-password[HTTP
Marian Harbach34253372019-12-10 18:01:31 +010090Password tab of the user settings page,role=external,window=_blank].
Edwin Kempin34be4102019-04-24 09:44:22 +020091
David Pursehouse923318b2019-08-30 10:55:05 +090092Alternately, you may use the
Marian Harbach34253372019-12-10 18:01:31 +010093link:https://pypi.org/project/git-review/[git-review,role=external,window=_blank] tool to submit changes
David Pursehouse923318b2019-08-30 10:55:05 +090094to Gerrit. If you do, it will set up the Change-Id hook and `gerrit` remote
95for you. You will still need to do the HTTP access step.
96
Edwin Kempin34be4102019-04-24 09:44:22 +020097[[style]]
98== Style
99
100This project has a policy of Eclipse's warning free code. Eclipse
101configuration is added to git and we expect the changes to be
102warnings free.
103
104We do not ask you to use Eclipse for editing, obviously. We do ask you
105to provide Eclipse's warning free patches only. If for some reasons, you
106are not able to set up Eclipse and verify, that your patch hasn't
107introduced any new Eclipse warnings, mention this in a comment to your
108change, so that reviewers will do it for you. Yes, the way to go is to
109extend gerrit CI to take care of this, but it's not yet implemented.
110
David Pursehouseb8a03992020-05-14 08:55:16 +0900111Gerrit follows the
Edwin Kempin34be4102019-04-24 09:44:22 +0200112link:https://google.github.io/styleguide/javaguide.html[Google Java Style
Marian Harbach34253372019-12-10 18:01:31 +0100113Guide,role=external,window=_blank].
Edwin Kempin34be4102019-04-24 09:44:22 +0200114
115To format Java source code, Gerrit uses the
Marian Harbach34253372019-12-10 18:01:31 +0100116link:https://github.com/google/google-java-format[`google-java-format`,role=external,window=_blank]
Edwin Kempin34be4102019-04-24 09:44:22 +0200117tool (version 1.7), and to format Bazel BUILD, WORKSPACE and .bzl files the
Marian Harbach34253372019-12-10 18:01:31 +0100118link:https://github.com/bazelbuild/buildtools/tree/master/buildifier[`buildifier`,role=external,window=_blank]
Marco Millerce85bda2020-04-29 13:40:25 -0400119tool (version 3.0.0). Unused dependencies are found and removed using the
David Pursehousebfe2d142020-01-29 18:02:53 +0900120link:https://github.com/bazelbuild/buildtools/tree/master/unused_deps[`unused_deps`,role=external,window=_blank]
David Pursehouse68668102020-01-29 15:52:06 +0900121build tool, a sibling of `buildifier`.
122
Edwin Kempin34be4102019-04-24 09:44:22 +0200123These tools automatically apply format according to the style guides; this
124streamlines code review by reducing the need for time-consuming, tedious,
125and contentious discussions about trivial issues like whitespace.
126
127You may download and run `google-java-format` on your own, or you may
128run `./tools/setup_gjf.sh` to download a local copy and set up a
129wrapper script. If you run your own copy, please use the same version,
130as there may be slight differences between versions.
131
Edwin Kempin34be4102019-04-24 09:44:22 +0200132When to use `final` modifier and when not (in new code):
133
134Always:
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
141Optional:
142
143 * final classes: use when appropriate, e.g. API restriction
144 * final methods: similar to final classes
145
146Never:
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
156Do your best to organize classes and methods in a logical way.
157Here 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
182Wow that's a lot! But don't worry, you'll get the habit and most
183of the code is organized this way already; so if you pay attention
184to the class you are editing you will likely pick up on it.
185Naturally new classes are a little harder; you may want to come
186back and consult this section when creating them.
187
188[[design]]
189== Design
190
191Here are some design level objectives that you should keep in mind
192when 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 Kempin9d4cc9e2019-06-12 17:03:39 +0200209 * 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 Kempin34be4102019-04-24 09:44:22 +0200212 * 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 Kempin34be4102019-04-24 09:44:22 +0200218
219[[tests]]
220== Tests
221
222 * Tests for new code will greatly help your change get approved.
223
Edwin Kempin6f3879d2019-11-19 12:24:51 +0100224[[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 Kempin34be4102019-04-24 09:44:22 +0200231[[change-size]]
232== Change Size/Number of Files Touched
233
234And finally, I probably cannot say enough about change sizes.
235Generally, smaller is better, hopefully within reason. Do try to
236keep things which will be confusing on their own together,
237especially 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
269GERRIT
270------
271Part of link:index.html[Gerrit Code Review]
272
273SEARCHBOX
274---------