blob: bb8013458dbb5b8727a96c976bab01c735071daf [file] [log] [blame]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001= Gerrit Code Review - A Quick Introduction
Ed Costello7407a922011-07-23 21:38:42 +12002
3Gerrit is a web-based code review tool built on top of the git version
4control system, but if you've got as far as reading this guide then
5you probably already know that. The purpose of this introduction is to
6allow you to answer the question, is Gerrit the right tool for me?
7Will it fit in my work flow and in my organization?
8
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08009== What is Gerrit?
Ed Costello7407a922011-07-23 21:38:42 +120010
David Pursehouse98fef392014-05-13 16:59:29 +090011It is assumed that if you're reading this then you're already convinced
12of the benefits of code review in general but want some technical support
13to make it easy.
Ed Costello7407a922011-07-23 21:38:42 +120014
David Pursehouse98fef392014-05-13 16:59:29 +090015Code reviews mean different things to different people. To some it's a
16formal meeting with a projector and an entire team going through the code
17line by line. To others it's getting someone to glance over the code before
18it is committed.
19
20Gerrit is intended to provide a lightweight framework for reviewing
Ed Costello7407a922011-07-23 21:38:42 +120021every commit before it is accepted into the code base. Changes are
22uploaded to Gerrit but don't actually become a part of the project
23until they've been reviewed and accepted. In many ways this is simply
24tooling to support the standard open source process of submitting
25patches which are then reviewed by the project members before being
26applied to the code base. However Gerrit goes a step further making it
27simple for all committers on a project to ensure that changes are
28checked over before they're actually applied. Because of this Gerrit
29is equally useful where all users are trusted committers such as may
David Pursehouse221d4f62012-06-08 17:38:08 +090030be the case with closed-source commercial development. Either way it's
Ed Costello7407a922011-07-23 21:38:42 +120031still desirable to have code reviewed to improve the quality and
32maintainability of the code. After all, if only one person has seen
33the code it may be a little difficult to maintain when that person
34leaves.
35
36Gerrit is firstly a staging area where changes can be checked over
37before becoming a part of the code base. It is also an enabler for
38this review process, capturing notes and comments about the changes to
39enable discussion of the change. This is particularly useful with
40distributed teams where this conversation can't happen face to face.
41Even with a co-located team having a review tool as an option is
42beneficial because reviews can be done at a time that is convenient
43for the reviewer. This allows the developer to create the review and
44explain the change while it is fresh in their mind. Without such a
45tool they either need to interrupt someone to review the code or
46switch context to explain the change when they've already moved on to
47the next task.
48
49This also creates a lasting record of the conversation which can be
50useful for answering the inevitable "I know we changed this for a
51reason" questions.
52
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080053== Where does Gerrit fit in?
Ed Costello7407a922011-07-23 21:38:42 +120054
55Any team with more than one member has a central source repository of
56some kind (or they should). Git can theoretically work without such a
57central location but in practice there is usually a central
58repository. This serves as the authoritative copy of what is actually in
59the project. This is what everyone fetches from and pushes to and is
60generally where build servers and other such tools get the source
61from.
62
63.Central Source Repository
64image::images/intro-quick-central-repo.png[Authoritative Source Repository]
65
66Gerrit is deployed in place of this central repository and adds an
67additional concept, a store of pending changes. Everyone still fetches
68from the authoritative repository but instead of pushing back to it,
69they push to this pending changes location. A change can only be submitted
70into the authoritative repository and become an accepted part of the project
71once the change has been reviewed and approved.
72
73.Gerrit in place of Central Repository
74image::images/intro-quick-central-gerrit.png[Gerrit in place of Central Repository]
75
76Like any repository hosting solution, Gerrit has a powerful
77link:access-control.html[access control model.]
78Users can even be granted access to push directly into the central
79repository, bypassing code review entirely. Gerrit can even be used
80without code review, used simply to host the repositories and
81controlling access. But generally it's just simpler and safer to go
82through the review process even for users who are allowed to directly
83push.
84
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080085== The Life and Times of a Change
Ed Costello7407a922011-07-23 21:38:42 +120086
87The easiest way to get a feel for how Gerrit works is to follow a
88change through its entire life cycle. For the purpose of this example
89we'll assume that the Gerrit Server is running on a server called
90+gerrithost+ with the HTTP interface on port +8080+ and the SSH
91interface on port +29418+. The project we'll be working on is called
92+RecipeBook+ and we'll be developing a change for the +master+ branch.
93
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080094=== Cloning the Repository
Ed Costello7407a922011-07-23 21:38:42 +120095
96Obviously the first thing we need to do is get the source that we're
97going to be modifying. As with any git project you do this by cloning
98the central repository that Gerrit is hosting. e.g.
99
100----
101$ git clone ssh://gerrithost:29418/RecipeBook.git RecipeBook
102Cloning into RecipeBook...
103----
104
105Then we need to make our actual change and commit it locally. Gerrit
106doesn't really change anything here, this is just the standard editing
107and git. While not strictly required, it's best to include a Change-Id
108in your commit message so that Gerrit can link together different
109versions of the same change being reviewed. Gerrit contains a standard
110link:user-changeid.html[Change-Id commit-msg hook]
111that will generate a unique Change-Id when you commit. If you don't do
112this then Gerrit will generate a Change-Id when you push your change
113for review. But because you don't have the Change-Id in your commit
114message you'll need to manually copy it in if you need to upload
115another version of your change. Because of this it's best to just
116install the hook and forget about it.
117
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800118=== Creating the Review
Ed Costello7407a922011-07-23 21:38:42 +1200119
120Once you've made your change and committed it locally it's time to
121push it to Gerrit so that it can be reviewed. This is done with a git
122push to the Gerrit server. Since we cloned our local repository
123directly from Gerrit it is the origin so we don't have to redefine the
124remote.
125
126----
127$ <work>
128$ git commit
129[master 9651f22] Change to a proper, yeast based pizza dough.
130 1 files changed, 3 insertions(+), 2 deletions(-)
131$ git push origin HEAD:refs/for/master
132Counting objects: 5, done.
133Delta compression using up to 8 threads.
134Compressing objects: 100% (2/2), done.
135Writing objects: 100% (3/3), 542 bytes, done.
136Total 3 (delta 0), reused 0 (delta 0)
Robin Rosenberg524a3032012-10-14 14:24:36 +0200137remote:
Ed Costello7407a922011-07-23 21:38:42 +1200138remote: New Changes:
139remote: http://gerrithost:8080/68
Robin Rosenberg524a3032012-10-14 14:24:36 +0200140remote:
Ed Costello7407a922011-07-23 21:38:42 +1200141To ssh://gerrithost:29418/RecipeBook.git
142 * [new branch] HEAD -> refs/for/master
143----
144
145The only different thing about this is the +refs/for/master+ branch.
146This is a magic branch that creates reviews that target the master
147branch. For every branch Gerrit tracks there is a magic
148+refs/for/<branch_name>+ that you push to to create reviews.
149
150In the output of this command you'll notice that there is a link to
151the HTTP interface of the Gerrit server we just pushed to. This is the
152web interface where we will review this commit. Let's follow that link
153and see what we get.
154
155.Gerrit Code Review Screen
156image::images/intro-quick-new-review.jpg[Gerrit Review Screen]
157
158This is the Gerrit code review screen where someone will come to
159review the change. There isn't too much to see here yet, you can look
160at the diff of your change, add some comments explaining what you did
161and why, you may even add a list of people that should review the change.
162
163Reviewers can find changes that they want to review in any number of
David Pursehouse98fef392014-05-13 16:59:29 +0900164ways. Gerrit has a capable link:user-search.html[search]
Ed Costello7407a922011-07-23 21:38:42 +1200165that allows project leaders (or anyone else) to find changes that need
166to be reviewed. Users can also setup watches on Gerrit projects with a
167search expression, this causes Gerrit to notify them of matching
168changes. So adding a reviewer when creating a review is just a
169recommendation.
170
171At this point the change is available for review and we need to switch
172roles to continue following the change. Now let's pretend we're the
173reviewer.
174
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800175=== Reviewing the Change
Ed Costello7407a922011-07-23 21:38:42 +1200176
177The reviewer's life starts at the code review screen shown above. He
178can get here in a number of ways, but for some reason they've decided
179to review this change. Of particular note on this screen are the two
180"Need" lines:
181
182----
183* Need Verified
184* Need Code-Review
185----
186
187Gerrit's default work-flow requires two checks before a change is
188accepted. Code-Review is someone looking at the code, ensuring it
189meets the project guidelines, intent etc. Verifying is checking that
190the code actually compiles, unit tests pass etc. Verification is
191usually done by an automated build server rather than a person. There
192is even a
193link:https://wiki.jenkins-ci.org/display/JENKINS/Gerrit+Trigger[Gerrit Trigger Jenkins Plugin]
194that will automatically build each uploaded change and update the
195verified score accordingly.
196
197It is important to note that Code-Review and Verification are
198different permissions in Gerrit, allowing these tasks to be separated.
199For example, an automated process would have rights to verify but not
200to code-review.
201
202Since we are the code reviewer, we're going to review the code. To do
203this we can view it within the Gerrit web interface as either a
204unified or side-by-side diff by selecting the appropriate option. In
205the example below we've selected the side-by-side view. In either of
Bruce Zu6b0fd762012-10-25 16:52:00 +0800206these views you can add inline comments by double clicking on the line
207(or single click the line number) that you want to comment on. Also you
208can add file comment by double clicking anywhere (not just on the
209"Patch Set" words) in the table header or single clicking on the icon
210in the line-number column header. Once published these comments are
211viewable to all, allowing discussion of the change to take place.
Ed Costello7407a922011-07-23 21:38:42 +1200212
213.Side By Side Patch View
214image::images/intro-quick-review-line-comment.jpg[Adding a Comment]
215
216Code reviewers end up spending a lot of time navigating these screens,
217looking at and commenting on these changes. To make this as efficient
218as possible Gerrit has keyboard shortcuts for most operations (and
219even some operations that are only accessible via the hot-keys). At
220any time you can hit the +?+ key to see the keyboard shortcuts.
221
222.Gerrit Hot Key Help
223image::images/intro-quick-hot-key-help.jpg[Hot Key Help]
224
225Once we've looked over the changes we need to complete reviewing the
226submission. To do this we click the _Review_ button on the change
227screen where we started. This allows us to enter a Code Review label
228and message.
229
230.Reviewing the Change
231image::images/intro-quick-reviewing-the-change.jpg[Reviewing the Change]
232
233The label that the reviewer selects determines what can happen next.
234The +1 and -1 level are just an opinion where as the +2 and -2 levels
235are allowing or blocking the change. In order for a change to be
236accepted it must have at least one +2 and no -2 votes.
237Although these are numeric values, they in no way accumulate;
238two +1s do not equate to a +2.
239
240Regardless of what label is selected, once the _Publish Comments_
241button has been clicked, the cover message and any comments on the
242files become visible to all users.
243
244In this case the change was not accepted so the creator needs to
245rework it. So let's switch roles back to the creator where we
246started.
247
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800248=== Reworking the Change
Ed Costello7407a922011-07-23 21:38:42 +1200249
250As long as we set up the
251link:user-changeid.html[Change-Id commit-msg hook]
252before we uploaded the change, re-working it is easy. All we need
253to do to upload a re-worked change is to push another commit that has
David Pursehouse2c6f6382014-09-04 13:06:19 +0900254the same Change-Id in the message. Since the hook added a Change-Id in
Ed Costello7407a922011-07-23 21:38:42 +1200255our initial commit we can simply checkout and then amend that commit.
256Then push it to Gerrit in the same way as we did to create the review. E.g.
257
258----
259$ <checkout first commit>
260$ <rework>
261$ git commit --amend
262$ git push origin HEAD:refs/for/master
263Counting objects: 5, done.
264Delta compression using up to 8 threads.
265Compressing objects: 100% (2/2), done.
266Writing objects: 100% (3/3), 546 bytes, done.
267Total 3 (delta 0), reused 0 (delta 0)
Edwin Kempinef126582014-05-13 12:31:38 +0200268remote: Processing changes: updated: 1, done
269remote:
270remote: Updated Changes:
271remote: http://gerrithost:8080/68
272remote:
Ed Costello7407a922011-07-23 21:38:42 +1200273To ssh://gerrithost:29418/RecipeBook.git
274 * [new branch] HEAD -> refs/for/master
275----
276
Edwin Kempinef126582014-05-13 12:31:38 +0200277Note that the output is slightly different this time around. Since
278we're adding to an existing review it tells us that the change was
279updated.
280
281Having uploaded the reworked commit we can go back into the Gerrit web
282interface and look at our change.
Ed Costello7407a922011-07-23 21:38:42 +1200283
284.Reviewing the Rework
285image::images/intro-quick-review-2-patches.jpg[Reviewing the Rework]
286
287If you look closely you'll notice that there are now two patch sets
288associated with this change, the initial submission and the rework.
289Rather than repeating ourselves lets assume that this time around the
290patch is given a +2 score by the code reviewer.
291
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800292=== Trying out the Change
Ed Costello7407a922011-07-23 21:38:42 +1200293
294With Gerrit's default work-flow there are two sign-offs, code review
295and verify. Verifying means checking that the change actually works.
296This would typically be checking that the code compiles, unit tests
297pass and similar checks. Really a project can decide how much or
298little they want to do here. It's also worth noting that this is only
299Gerrit's default work-flow, the verify check can actually be removed
300or others added.
301
302As mentioned in the code review section, verification is typically an
303automated process using the
304link:https://wiki.jenkins-ci.org/display/JENKINS/Gerrit+Trigger[Gerrit Trigger Jenkins Plugin]
305or similar. But there are times when the code needs to be manually
306verified, or the reviewer needs to check that something actually works
307or how it works. Sometimes it's just nice to work through the code in a
308development environment rather than the web interface. All of these
309involve someone needing to get the change into their development
310environment. Gerrit makes this process easy by exposing each change as
311a git branch. So all the reviewers need to do is fetch and checkout that
312branch from Gerrit and they will have the change.
313
314We don't even need to think about it that hard, if you look at the
David Pursehouse98fef392014-05-13 16:59:29 +0900315earlier screenshots of the Gerrit Code Review Screen you'll notice a
Ed Costello7407a922011-07-23 21:38:42 +1200316_download_ command. All we need to do to get the change is copy
317paste this command and run it in our Gerrit checkout.
318
319----
320$ git fetch http://gerrithost:8080/p/RecipeBook refs/changes/68/68/2
321From http://gerrithost:8080/p/RecipeBook
322 * branch refs/changes/68/68/2 -> FETCH_HEAD
323$ git checkout FETCH_HEAD
324Note: checking out 'FETCH_HEAD'.
325
326You are in 'detached HEAD' state. You can look around, make experimental
327changes and commit them, and you can discard any commits you make in this
328state without impacting any branches by performing another checkout.
329
330If you want to create a new branch to retain commits you create, you may
331do so (now or later) by using -b with the checkout command again. Example:
332
333 git checkout -b new_branch_name
334
335HEAD is now at d5dacdb... Change to a proper, yeast based pizza dough.
336----
337
338Easy as that, we now have the change in our working copy to play with.
339You might be interested in what the numbers of the refspec mean.
340
David Pursehouse221d4f62012-06-08 17:38:08 +0900341* The first *68* is the id of the change +mod 100+. The only reason
Ed Costello7407a922011-07-23 21:38:42 +1200342for this initial number is to reduce the number of files in any given
343directory within the git repository.
344* The second *68* is the full id of the change. You'll notice this in
345the URL of the Gerrit review screen.
346* The *2* is the patch-set within the change. In this example we
347uploaded some fixes so we want the second patch set rather than the
348initial one which the reviewer rejected.
349
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800350=== Manually Verifying the Change
Ed Costello7407a922011-07-23 21:38:42 +1200351
352For simplicity we're just going to manually verify the change.
353The Verifier may be the same person as the code reviewer or a
354different person entirely. It really depends on the size of the
355project and what works. If you have Verify permission then when you
356click the _Review_ button in the Gerrit web interface you'll be
357presented with a verify score.
358
359.Verifying the Change
360image::images/intro-quick-verifying.jpg[Verifying the Change]
361
362Unlike the code review the verify check doesn't have a +2 or -2 level,
363it's either a pass or fail so all we need for the change to be
364submitted is a +1 score (and no -1's).
365
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800366=== Submitting the Change
Ed Costello7407a922011-07-23 21:38:42 +1200367
368You might have noticed that in the verify screen shot there are two
369buttons for submitting the score _Publish Comments_ and _Publish
370and Submit_. The publish and submit button is always visible, but will
371only work if the change meets the criteria for being submitted (I.e.
372has been both verified and code reviewed). So it's a convenience to be
373able to post review scores as well as submitting the change by clicking
374a single button. If you choose just to publish comments at this point then
375the score will be stored but the change won't yet be accepted into the code
376base. In this case there will be a _Submit Patch Set X_ button on the
Dave Borowitz01c1b1f2013-02-27 13:49:04 -0800377main screen. Just as Code-Review and Verify are different operations
Ed Costello7407a922011-07-23 21:38:42 +1200378that can be done by different users, Submission is a third operation
379that can be limited down to another group of users.
380
David Pursehouse221d4f62012-06-08 17:38:08 +0900381Clicking the _Publish and Submit_ or _Submit Patch Set X_ button
Ed Costello7407a922011-07-23 21:38:42 +1200382will merge the change into the main part of the repository so that it
383becomes an accepted part of the project. After this anyone fetching
384the git repository will receive this change as a part of the master
385branch.
386
387GERRIT
388------
389Part of link:index.html[Gerrit Code Review]
Yuxuan 'fishy' Wang99cb68d2013-10-31 17:26:00 -0700390
391SEARCHBOX
392---------