Project

General

Profile

Gerrit » History » Version 86

neels, 09/17/2018 11:21 AM

1 1 zecke
h1. Contributing using Gerrit
2
3 11 laforge
{{>toc}}
4
5 10 laforge
At [[OpenBSC:OsmoDevCon2016]] we discussed problems with our past contribution / patch submission process using mails on the mailing list as well as patchwork.  The result is that we want to give Gerrit a try for some time and see if it helps us to have a better process
6 1 zecke
7 10 laforge
Gerrit is a review tool that integrates nicely with git and ssh. You can find general information about Gerrit at https://www.gerritcodereview.com/
8 1 zecke
9 10 laforge
The advantages of Gerrit are:
10
* patch submission status is automatically tracked, also with several revisions for a patch set.
11
* patches are build-tested (and possibly even further tested) by jenkins before they are applied
12
* developers + maintainers can formally vote on a patch (developer: -1/0/+1, maintainer: -2/0/+2)
13
* once a patch has +2 score, it can be (automatically) merged into master
14
* patch sumissions not via git send-email but direcly from git
15
16
h2. Osmocom Subprojects using Gerrit
17
18 73 laforge
The majority of Osmocom sub-projects have chosen to use Gerrit for patch review.  In order to check if a given program uses Gerrit, please check the auto-generated list at https://gerrit.osmocom.org/#/admin/projects/
19 1 zecke
20 75 laforge
If the project is listed there, then it uses Gerrit.   If the project is not listed there, please send patches by e-mail to the respective project [[Mailing_Lists]] instead.
21 30 neels
22 1 zecke
h2. Configuring Gerrit/Account
23
24 54 neels
You will need to sign-up at https://gerrit.osmocom.org/login/. If you have an Osmocom Redmine account you can use https://osmocom.org/openid as OpenID provider.
25 1 zecke
26 68 neels
* first sign in on https://osmocom.org. Do this before logging in on gerrit (the redmine login process loses the gerrit login data and you'd have to do the same thing twice if not logged in on osmocom.org already).
27 55 neels
* go to https://gerrit.osmocom.org and click the "Sign in" link.
28 68 neels
* click the "Sign in with Osmocom":https://gerrit.osmocom.org/login/%23%2Fq%2Fstatus%3Aopen?id=https://osmocom.org/openid link (can be bookmarked). -- This is the same as entering https://osmocom.org/openid as OpenID provider and hitting the "Sign in" button.
29 61 neels
30
*careful:* enter 'https' to ensure that your openid credentials are passed on encryptedly.
31 68 neels
*pitfall:* if you're logged in on 'projects.osmocom.org' (including the 'projects.' part), you should also use the openid provider: https://projects.osmocom.org/openid; the 'projects.' part may be omitted, what's important is that redmine login and OpenID URLs match. Also, decide for one of those URLs once, because when picking a different OpenID URL next time, you will create a new user instead of logging in as yourself.
32 61 neels
*note:* gerrit will create a distinct user for each openid URL you pass. If you logged in successfully but your user seems to have lost permissions, you may have created an evil twin user: contact us on the mailing list so we can fix it in the user database.
33 54 neels
34
If you have no Osmocom redmine account, you can simply create one online at the "Register" link in the upper right corner.
35 10 laforge
Even without an existing or new redmine account, you should also be able to use any other OpenID provider to authenticate against gerrit (untested).
36
37
After the initial sign-up you will need to:
38 1 zecke
39
* Pick a username (can not be changed)
40
* Add your public ssh key(s)
41
* Add email addresses you intend to use as author/comitter
42 30 neels
43
If you would like to push private branches to the Gerrit repository, you also need to be added to the "known users" group.
44
Please send a short requesting email to openbsc@lists.osmocom.org.
45 1 zecke
46
h2. Setting up Gerrit for commits and pushing
47
48 33 neels
*Note:* it is easiest to work with gerrit when gerrit is the only remote in your git clone.
49
When you clone from git.osmocom.org and add the gerrit remote, git will have two remotes,
50 36 neels
so when you first checkout a branch you have to supply the remote explicitly (cumbersome).
51 34 neels
The gerrit repositories and git.osmocom.org are constantly synced, so it is sufficient
52
to clone from gerrit only.
53 33 neels
54
h3. Simplest: new clone
55
56 35 neels
* Create a new clone from gerrit
57
* Fetch the commit hook that adds Change-Id to each commit to uniquely identify a commit
58 42 neels
59 33 neels
<pre>
60
git clone ssh://$USERNAME@gerrit.osmocom.org:29418/$PROJECT.git
61
scp -P 29418 $USERNAME@gerrit.osmocom.org:hooks/commit-msg $PROJECT/.git/hooks/
62
</pre>
63
64
h3. SSH config
65
66
In '~/.ssh/config', add these lines:
67
<pre>
68 52 neels
Host go
69 33 neels
Hostname gerrit.osmocom.org
70
Port 29418
71
User $USERNAME
72
</pre>
73 52 neels
('go' means gerrit.osmocom, replace with your favorite shortcut name,
74 33 neels
replace '$USERNAME' with your user name as used on the gerrit website)
75
76
Then you can shorten above commands to
77 1 zecke
<pre>
78 52 neels
git clone ssh://go/$PROJECT.git
79 51 neels
cd $PROJECT
80
scp go:hooks/commit-msg .git/hooks/
81 33 neels
</pre>
82
83 46 neels
h3. Committer must match
84
85 47 neels
Your email address on gerrit and the email address git places in your
86 46 neels
commits must match, or you will get rejected with an error message like
87
"invalid commiter". You can add email addresses on the gerrit web UI.
88
89 33 neels
h3. Add gerrit to an existing clone
90
91 7 neels
* Add the remote to be able to fetch and push to gerrit
92
* Fetch the commit hook that adds Change-Id to each commit to uniquely identify a commit
93
94
<pre>
95
USERNAME=gerrit_user_name
96
PROJECT=$(basename $PWD)
97 1 zecke
git remote add gerrit ssh://$USERNAME@gerrit.osmocom.org:29418/$PROJECT.git
98
scp -P 29418 $USERNAME@gerrit.osmocom.org:hooks/commit-msg .git/hooks/
99 44 neels
</pre>
100
101 33 neels
h2. Push for review
102 1 zecke
103 81 neels
Prerequisites:
104
105
* your user on gerrit has an SSH public key
106
* your patch is committed in your local clone
107 82 neels
* the commit log message has a Change-Id (see 'commit-msg' hook above, and 'Tips and Tricks' below to add a Change-Id to a commit that lacks one.)
108 81 neels
109 77 neels
<pre>
110
git push $REMOTE $GITHASH:refs/for/$BRANCH/$TOPIC
111
</pre>
112
113
$REMOTE: from above instructions, that's either 'origin' (cloned from gerrit) or 'gerrit' (if you added a second remote).
114
$GITHASH: the committed patch to push, typically you're on your branch and simply push 'HEAD'.
115
$BRANCH: you will typically intend a patch to go to 'master'.
116
$TOPIC: an optional name you may choose.
117
118
For example, checkout the revision or branch that you want to submit for review,
119
i.e. the one where your patch or several patches are committed on top of the current master, then:
120 1 zecke
121 76 neels
If you cloned directly from gerrit:
122
123 1 zecke
<pre>
124 76 neels
git push origin HEAD:refs/for/master
125
</pre>
126
127
If you added 'gerrit' as a second remote to an existing clone:
128
129
<pre>
130 1 zecke
git push gerrit HEAD:refs/for/master
131 38 neels
</pre>
132 1 zecke
133 40 neels
You can optionally add a topic name with
134 1 zecke
135 40 neels
<pre>
136 76 neels
git push origin HEAD:refs/for/master/my_topic
137 38 neels
</pre>
138
139 57 neels
h2. Merge patch to master
140
141
A patch can be merged when it has CR+2 and V+1 votes, and if, in case of a
142
series of patches pushed from a branch, when its ancestor patches can also be
143
merged.
144
145
Sometimes the reviewer that gives CR+2 also hits the "Submit" button right away 
146
to merge the patch to master. Sometimes it is left up to the owner of the patch
147 59 neels
to decide when to hit "Submit" (who needs to be in the "Known Users" group).
148 57 neels
149
The V+1 vote means "build is verified" and is usually given by our jenkins
150 59 neels
gerrit builds: https://jenkins.osmocom.org/jenkins/view/Jenkins-Gerrit/
151 57 neels
152
The CR+2 vote means "code reviewed and ready for merge to master branch".
153
Accounts with the "Reviewer" role for a given project are allowed to give CR+2
154
votes. Others are allowed to give CR+1 (and CR-1). CR votes _don't_ add up.
155
156 67 neels
_Fixed by gerrit 2.12.6, see https://bugs.chromium.org/p/gerrit/issues/detail?id=4158:_
157
-Sometimes hitting the "Submit" button results in an error message saying
158 57 neels
"Change is New", which is a bug related to a private branch with the same
159 65 neels
patches being present. Can be fixed e.g. by an admin's manual push to master.-
160 1 zecke
161 84 neels
h2. Manage private branches
162 1 zecke
163 84 neels
Use a sub-directory with your name to group your own branches, please.
164 1 zecke
*Note* that you must be a member of the "known users" group, see above.
165
166 85 neels
To share / backup local branches to git.osmocom.org, without starting a code review process on gerrit.osmocom.org, just push them as usual to gerrit.
167 84 neels
168
* Note that the git repository must be cloned from the gerrit SSH URL -- all pushing goes to gerrit ("pushurl" as described below also works).
169
* All private branches are automatically synced to git.osmocom.org in a matter of minutes.
170 86 neels
* Private branches do not kick off patch sets for review, they are just branches. To kick off review for all patches on your branch, you'd use a 'refs/for/master' URL, as shown in the example below.
171 84 neels
172
The typical transcript for "Fred" developing feature "Kazoo" looks like this:
173
174 1 zecke
<pre>
175 84 neels
# Set up ~/.ssh/config so that 'go' points at gerrit.osmocom.org
176
git clone ssh://go/libosmocore
177
178
cd libosmocore
179
git checkout -b fred/kazoo   # create local branch
180
181
$EDITOR
182
git add kazoo.c
183
git commit -m "implement kazoo"
184
185
# just invoke 'git push' and git tells you how to create the branch once-off:
186 1 zecke
git push
187 84 neels
|fatal: The current branch asdf has no upstream branch.
188
|To push the current branch and set the remote as upstream, use
189
|
190
|    git push --set-upstream origin fred/kazoo
191 1 zecke
192 84 neels
git push --set-upstream origin fred/kazoo
193
# Now the branch exists on gerrit, very soon will also exist on git.osmocom.org
194
195
# Further tweaks
196
$EDITOR
197
git add kazoo.h
198
git commit --amend
199
200
# You are free to force-push private branches
201
git push -f
202
203
# origin/master has changed? Rebase onto the latest.
204
git fetch
205
git rebase -i origin/master
206
# resolve conflicts...
207
git push -f
208
209
# Feature is ready
210
git push origin HEAD:refs/for/master/kazoo
211 39 neels
</pre>
212 7 neels
213 48 ahuemer
h2. List changesets in gerrit
214 2 zecke
215 12 msuraev
<pre>
216 17 neels
git ls-remote gerrit changes/*
217 1 zecke
</pre>
218 80 neels
219
h1. Tips and Tricks
220
221
h2. A commit lacks a Change-Id
222
223
once you added the commit hook as above, just re-edit the commit log message, e.g. with
224
225
<pre>
226
git commit --amend
227
</pre>
228
229
or by
230
231
<pre>
232
git rebase -i
233
</pre>
234
235
and in the upcoming editor replacing 'pick' with 'r' in front of the commit to edit.
236 74 neels
237
No need to change the commit log if you don't want to, just exit the editor and the commit hook will add a Change-Id.
238 79 neels
239
h2. Fetch fast from git.osmocom.org, push to gerrit
240
241 1 zecke
Gerrit has moved to a faster host, so this should no longer be necessary. Anyway...
242 74 neels
243
Adding a second remote forces you to often pass the remote on the command line ("origin").
244 79 neels
It is possible to have only one remote for cmdline convenience, with differing push and pull URLs:
245
246 74 neels
<pre>
247 1 zecke
git remote set-url origin git://git.osmocom.org/$PROJECT
248 74 neels
git remote set-url --push origin ssh://$USERNAME@gerrit.osmocom.org:29418/$PROJECT
249
</pre>
250 79 neels
251 74 neels
With above .ssh config you can also use the shorter ssh:// URL:
252
<pre>
253 79 neels
git remote set-url --push origin ssh://go/$PROJECT
254 74 neels
</pre>
255
256
The resulting .git/config in libosmocore would look something like:
257
<pre>
258
[remote "origin"]
259
        url = git://git.osmocom.org/libosmocore
260
        pushurl = ssh://go/libosmocore
261
        fetch = +refs/heads/*:refs/remotes/origin/*
262
</pre>
263 17 neels
264
Now you're fetching from git.osmocom.org, which is lightning fast, while pushing patches will still go to gerrit as usual.
265
266 45 neels
h2. Throw-away branch
267 13 neels
268 56 neels
If you need to adjust and re-submit patches, it may be handy to create a throw-away branch ("R D" in magit-gerrit in emacs for example),
269
make your changes/amendments and then send patch(es) back to gerrit while removing temporary branch automatically with "git review -f".
270
271
h2. Fetch a patch from gerrit
272
273
This script (I called it @P@) makes fetching a patch set from gerrit a breeze:
274
<pre>
275
#!/bin/sh
276
# fetch gerrit patch into new branch named like the patch number.
277
#
278
# Usage: go to a git clone and pass a patch number:
279
#
280
#   cd openbsc
281
#   P 973
282
# or
283
#   P 973/2
284
#
285
# Will create new local branches '973_4' (if 4 is the latest patch set)
286
# or '973_2', respectively.
287
288
patch="$1"
289
290
if [ -z "$patch" ]; then
291
  echo "Usage: P 1234[/5]"
292
  exit 1
293
fi
294
295
if [ -z "$(echo "$patch" | grep '/')" ]; then
296
  patch="/$patch/"
297
fi
298
299
if [ -z "$(echo "$patch" | grep '^/')" ]; then
300
  patch="/$patch"
301
fi
302
303
last_set="$(git ls-remote origin "changes/*" | grep "$patch" | sed 's#.*/\([^/]*\)$#\1 &#' | sort -n | tail -n 1)"
304
if [ -z "$last_set" ]; then
305
  echo "Not found: $patch"
306
  exit 1
307
fi
308
309
change_name="$(echo "$last_set" | sed 's/.*\(refs.*\)/\1/')"
310
branch_name="$(echo "$change_name" | sed 's#refs/changes/../\([0-9]*\)/\([0-9]*\)#\1_\2#')"
311
312
set -x
313
git fetch origin "$change_name"
314 25 neels
git co -b "$branch_name" FETCH_HEAD
315 13 neels
</pre>
316 1 zecke
317 58 neels
h2. Re-submit a Branch with Amended Commits
318
319 22 neels
On a feature branch, one typically has numerous commits that depend on their preceding commits.
320 58 neels
Often, some of the branch commits need to be amended for fixes. You can re-submit changes to
321
patches on your branch by pushing in the same way that you first submitted the branch.
322
323 29 neels
Note: if you modify the Change-Ids in the commit logs, your push would open entirely new
324 58 neels
review entries and you would have to abandon your previous submission. Comments on the first
325
submission are "lost" and you cannot diff between patch sets.
326 29 neels
327
(There used to be a bug in gerrit that required editing the first patch to be able to
328
re-submit a branch, but that's fixed.)
329 26 neels
330 1 zecke
331
332
h2. Re-submit Previously Abandoned Changes
333
334
You have to edit the Change-Ids, on a branch that would be every single commit log message.
335
336
<pre>
337
cd openbsc
338
git co my-branch
339
git rebase -i master
340
# replace all 'pick' with 'r' (or 'reword'), exit your editor
341
# git presents each commit log message for editing
342
</pre>
343
344 78 neels
h2. 502 Bad Gateway
345
346
When getting a "Bad Gateway" error message upon trying to login on gerrit, you probably just need to restart your web browser. The reason is not clear.
347
348
349
h2. Commit hook: Always put Change-Id at the bottom of the log message
350
351
The commit-msg hook places a Change-Id tag in the footer, often above other tags like 'Depends:' or 'Related:'. Since the Change-Id is an implementation detail for Gerrit, I personally prefer it always placed right at the bottom. This simple edit changes the commit-msg hook to add Change-Id at the bottom unconditionally:
352
353
<pre>
354
cd $PROJECT
355
sed -i 's/if (unprinted /if (0 \&\& unprinted /' .git/hooks/commit-msg
356
</pre>
357
358
The goal is to disable the condition in line 163 with an 'if (0...':
359
360
<pre>
361
                        if (0 && unprinted && match(tolower(footer[line]), changeIdAfter) != 1) {
362
                                unprinted = 0
363
                                print "Change-Id: I'"$id"'"
364
                        }
365 60 neels
</pre>
366 16 neels
367 13 neels
Then the Change-Id will be placed by line 170 instead.
368 16 neels
369
h1. Reasons for Particular Configuration
370
371 13 neels
h2. Rebase if necessary
372
373
There are different merge strategies that Gerrit performs to accept patches.
374
Each project can be configured to a specific merge strategy, but unfortunately you can't
375
decide on a strategy per patch submission.
376
377
It seems that the "Merge if Necessary" strategy is best supported, but it creates non-linear
378
history with numerous merge commits that are usually not at all necessary.
379
380
Instead, the "Cherry Pick" strategy puts each patch onto current master's HEAD to create
381 1 zecke
linear history. However, this will cause merge failures as soon as one patch depends on
382 13 neels
another submitted patch, as typical for a feature branch submission.
383
384 1 zecke
So we prefer the "Rebase if Necessary" strategy, which always tries to apply your patches to
385 13 neels
the current master HEAD, in sequence with the previous patches on the same branch.
386
However, some problems still remain, including some bugs in "Rebase if Necessary".
387
388 1 zecke
There's a problem with "Rebase if Necessary": If your branch sits at master's HEAD, Gerrit
389
refuses to accept the submission, because it thinks that no new changes are submitted.
390
This is a bug in Gerrit, which holger has fixed manually in our Gerrit installation:
391
392 16 neels
https://bugs.chromium.org/p/gerrit/issues/detail?id=4158
393 1 zecke
394 13 neels
395
h2. Private Branches: Create a new change for every commit...
396 16 neels
397 13 neels
Say you have an extensive feature in development, and you want to keep it on the
398
upstream git repository to a) keep it safe and b) collaborate with other devs on it.
399
So, of course, you have regularly pushed to refs/heads/yoyodyne/feature.
400
401
Since you have the gerrit commit hook installed, your feature branch already has
402
Change-Id tags in all commit log messages.
403
404
Now your feature is complete and you would like to submit it to master.
405 16 neels
Alas, Gerrit refuses to accept your patch submission for master, because it
406
knows the Change-Ids are also on a different branch.
407
408
Gerrit by default enforces that a Change-Id must be unique across all branches,
409
so that each submission for review is separate for each branch. Instead, we
410 13 neels
want to handle Change-Ids per-branch, so that you can have the same change
411 16 neels
submitted to different branches, as separate patch submissions, without having
412
to cosmetically adjust the Change-Id.
413 13 neels
414 20 neels
Solution: set the option 
415 14 neels
_Create a new change for every commit not in the target branch_ to _TRUE_
416
417
h2. Allow content merges
418
419
By default, gerrit compares patches only by the files' paths. If two paths are the same,
420
it immediately shows them as conflicts (path conflicts).
421
422 23 neels
In software development, a conflict usually means an actual content conflict, so if the
423 14 neels
edits are in two entirely separate places in the file, we don't consider this a conflict.
424
425 32 neels
By setting _Allow content merges_ to _TRUE_ in the git project config, we tell Gerrit to
426
perform text merges of the submitted patches and only complain about actual content
427
conflicts, in the usual software engineering sense.
428 72 laforge
429
h1. Admin
430
431
h2. Adding a new repository
432
433
* create the repository in the Gerrit Ui, inherit from "All-Projects"
434
* create an empty git repository using gitosis on git.osmcoom.org
435
* configure a jenkins build testing job for this project, cloning/copying from any osmo-*-gerrit projects
436
437 32 neels
git replication to gerrit.osmocom.org is enabled automatically, nothing to be done here.  In case of doubt, try
438
@ssh -p 29418 gerrit.osmocom.org replication start --all --wait@
439
440
h2. Adding users to groups
441
442
Normally, the gerrit UI auto-completes a user name in the edit field. It has happened
443
though that an existing user is not auto-completed, as if it didn't exist. In that case,
444
find out the user ID (seven digit number like 1000123) and just enter that.
445
446
The user ID can be found on the user's "Settings" page, or in the database (s.b.).
447
448
h2. Querying the database directly
449
450
If your user has permission to access the database, you can place SQL queries using the
451 71 neels
'gerrit gsql' commands over ssh:
452
453 1 zecke
<pre>
454 53 neels
ssh go "gerrit gsql -c \"show tables\""
455
ssh go "gerrit gsql -c \"select full_name,account_id from accounts\""
456 1 zecke
</pre>
457
458 71 neels
(see ~/.ssh/config above for the 'go' shortcut)
459
460 62 neels
This seems to be the MySQL dialect.
461
462
The "...\"...\"" quoting allows including single-quotes in the SQL statements.
463
464
h2. Fix evil twin users
465
466 64 neels
If differing openid URLs have lead to evil twin users shadowing the same email address just without the permissions, you can fix it like this:
467 62 neels
468
<pre>
469
ssh go "gerrit gsql -c \"select * from account_external_ids where email_address like '%foo%'\""
470
# ACCOUNT_ID | EMAIL_ADDRESS   | PASSWORD | EXTERNAL_ID
471
# -----------+-----------------+----------+----------------------------------
472 64 neels
# 100004     | foo@example.com | NULL     | https://osmocom.org/openid/user/777
473 62 neels
# 100021     | foo@example.com | NULL     | https://projects.osmocom.org/openid/user/777
474 64 neels
475 62 neels
ssh go "gerrit gsql -c \"update account_external_ids set account_id = 100004 where email_address like '%foo%'\""
476
477
ssh go "gerrit gsql -c \"select * from account_external_ids where email_address like '%foo%'\""
478
# ACCOUNT_ID | EMAIL_ADDRESS   | PASSWORD | EXTERNAL_ID
479
# -----------+-----------------+----------+----------------------------------
480 1 zecke
# 100004     | foo@example.com | NULL     | https://osmocom.org/openid/user/777
481
# 100004     | foo@example.com | NULL     | https://projects.osmocom.org/openid/user/777
482
</pre>
Add picture from clipboard (Maximum size: 48.8 MB)