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