Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
2 What is Gerrit?
Gerrit is a free, web-based collaborative code review tool
that integrates with Git. It has been developed at Google
by Shawn Pearce (co-author of Git, founder of JGit) for
the development of the Android project.
Git and Gerrit lets us deploy your code faster and makes collaboration easier!
installing git-review
submitting a patch
understanding the MediaWiki code review process
1. To encourage participation: Since Git is distributed, it allows people to contribute with a much
lower barrier to entry. Anyone will be able to clone
the repository and make their own changes to keep
track of them. And if youve got an account in
our code review tool (Gerrit), youll be able to push
changes for the wider community to review.
What is Git?
Git is a distributed version control system (dvcs) written in C originally developed by Linus Torvalds and others to manage the Linux kernel. In the past couple of
years, it has taken o as a very robust and well-supported
code repository. Distributed means that there is no central copy of the repository. With Subversion, Wikimedias servers host the repository and users commit their
changes to it. In contrast, with Git, once youve cloned
the repository, you have a fully functioning copy of the
source code, with all the branches and tagged releases at
your disposal.
2. To x our technical process: Subversion has technical aws that make life dicult for developers.
Notably, the implementation of branching is not
very easy to use, and makes it hard to use feature
branches. Our community is very distributed, with
many parallel eorts and needs to integrate many
dierent feature eorts, so wed like to use feature branches more. Git branches are very easy to
work with and merge between, which should make
things easier for our development community. (Several other large projects, such as Drupal and PostgreSQL, have made the same switch for similar rea-
4
sons, and weve done our best to learn from their
experiences.)
3. To get improvements to users faster: With better
branching and a more granular code review workow that suits our needs better, plus our ongoing
improvements to our automated testing infrastructure, we wont have to wait months before deploying
already-written features and bugxes to Wikimedia
sites.
SETTING UP GIT
Setting up Git
4.1
4.1.1
Installation
pkg
install
devel-
Mac OS X
5.2
3
down that random password in a
le on your computer. Anyone who
gains access to your drive has gained
access to every system you use that
key with. This is also a Very Bad
Thing. The solution is obvious:
add a passphrase.
But I dont want to enter a long
passphrase every time I use the
key!
We use SSH keys to establish a secure connection between your computer and Gerrit. Setting them up is fairly
easy, but does involve a number of steps. Run the follow- Which should give you something like this:
ing commands in a terminal. For alternate instructions
look here, and then here.
To make sure whether you need to generate a brand new
key, you need to check if one already exists. List the les
in your .ssh directory (if you have one):
$ ls ~/.ssh
If you see a le called id_rsa.pub here, you may skip right
to #Add your SSH key.
5.1
(private)
key
Run ssh. (Do be paranoid and check for the matching SSH ngerprint for gerrit.wikimedia.org:29418
when logging in for the rst time.)
<USER-
5.2.1
OSX
You can download MediaWiki core using Git, as well
$ pbcopy < ~/.ssh/id_rsa.pub # Copies the contents of the
as the source code of any project repository hosted at
id_rsa.pub le to your clipboard
gerrit.wikimedia.org (the Wikimedia Foundation server
Windows
cluster).
You can open Git GUI, go to Help > Show Key, and then
Lets practice downloading the Examples extension. Simpress Copy To Clipboard to copy your public key to your
ply run the following on the git bash command line:
clipboard.
$ git clone ssh://<USERNAME>@gerrit.wikimedia.org:
Linux
29418/mediawiki/extensions/examples
$ sudo apt-get install xclip # Downloads and installs xclip
$ xclip -sel clip < ~/.ssh/id_rsa.pub
5.3
5.4
This will copy the entire history of the Examples extension repository. You will have a working directory of the
extensions main branch, so you can look at the code and
start editing it. If you change into the new directory, you
can see the .git subdirectory. That is where all the project
data is.
By default, Git will create a directory that has the same
name as the project in the URL you give it - basically
whatever is after the last slash of the URL. If you want
something dierent, you can just put it at the end of the
command, after the URL. So, in this example you will
have a examples directory.
$ eval `ssh-agent`
Be sure to use the accent " ` " located under the
tilde " ~ " (or above tab for UK keyboards), not
the single quote " ' ".
6.2
Conguring git-review
Theres a git add-on called git-review that adds a ChangeID line to your commits and manages other aspects of
using Gerrit.
6.1
Installing git-review
Generally, the easiest way to get the latest version of gitreview is to install it using the python package installer
pip:
$ sudo pip install git-review
If that worked, skip to the next section. If you need to
install pip, then:
In Debian/Ubuntu, to install
$ sudo apt-get install python-pip
On OpenSuse, install via YaST python-setuptools, then On FreeBSD, you directly have a kept up to date
devel/git-review port:
run
$ cd /usr/ports/devel/git-review $ make install
$ sudo easy_install pip
Note: if you don't have apt-get but have python installed,
you can use this:
$ sudo easy_install pip $ sudo pip install git-review
Or, if you already have git-review installed, you can upgrade it using
$ sudo pip install --upgrade git-review
Note: Older versions of git-review used a separate con- git pull origin master
g le:.cong/git-review/git-review.conf (in Windows,
the le is %USERPROFILE%\.cong\git-review\gitreview.conf) and add these two lines:
## Deprecated! [gerrit] defaultremote = origin
6.3
Setting up git-review
After cloning a repository, you need to set it up for gitreview. This will automatically happen the rst time you 7.2 Create a branch
try to submit a commit, but its generally better to do it
right after cloning. In your projects directory (examFirst, create a local branch for your new change. Give
ples), enter
the branch a short but reasonably descriptive name (e.g.
$ git review -s
T1234, bug/1234, cleanup/some-thing, badtitle-error, ..).
which should give you this:
7.1
Update master
7.3
Without any extra arguments, a simple git di will display in unied di format (a patch) what code or content
you've changed in your project since the last commit that
are not yet staged for the next commit snapshot.
Then check the changes you've made, within the le(s)
and within the directory:
git status
The git di --cached command will show you what conYou run git status to see if anything has been modied tents have been staged. That is, this will show you the
and/or staged since your last commit so you can decide changes that will currently go into the next commit snapif you want to commit a new snapshot and what will be shot.
recorded in it.
Once you are happy with the change list, you can add
This will show all modied les. To prepare submitting a them to your local repository by using
le, you should add your changes to the index (the staging git commit
area between your working copy and your local repository), which is done by using the git add command.
git add Example/Example.body.php
You pass a le to git add when you want the changes you
You can repeat this step over and over until you have a
made to it to be included in your next commit.
set of changes that you want to have pushed to the masAny les you've changed that are not staged by you doing ter branch. One of the cool things about git is that when
git add will be left alone - this means you can craft your you git commit, you are committing to your local copy.
commits with a bit more precision.
This means you can commit as often as you like without
At any time you can always review the changes already potentially screwing things up for another developer on
added to the staging area by running git status, and look the project, unlike in SVN where you would want to be
very careful that the changes you commit would not cause
at the di with git di --cached:
things to break.
Hence the workow is something like:
# Add change: $ git add <some le> # Verify list of
les added to the staging area $ git status # Review di
of changes staged: $ git di --cached # repeat until you some code squash be33007 Fixed my bug in that le
are happy with your changes $ git commit <edit commit Save the le. Another le will open in your text editor
message>
which will allow you to edit the combined commit message. Be careful to only keep one of the Change-Id lines
and have it be at bottom of the message after one empty
line.
7.4
7.5
But it will also suggest a commit message with a ChangeId: INNNXXXNNN... line.
Either:
If you make several related commits to your local repository prior to wanting to submit for review, you should
squash those commits into a single commit. If you've followed everything above, you can perform this action by
doing:
git rebase -i origin/master
This will bring up your text editor with text like:
pick 749a62a Added a le pick ec9295b Changed some
code pick be33007 Fixed my bug in that le
7.9
didn't work for me), then repeat git review -R and it 7.9
should complete.
7.7
BEWARE: git review -d performs a hard reset that destroys all local changes. Stash or commit changes you
wish to preserve rst.
Sometimes, you might need to amend a submitted
change. You can amend your own changes as well as
changes submitted by someone else, as long as the change
hasn't been merged yet.
Rebase to bring your local branch up to date with the remote. Its best to make rebase updates a separate patch, so
that your code reviewers have an easy time seeing what
changes you've made. Assuming you are using Gerrit,
you can do this by clicking the Rebase Change button
when viewing your patch in Gerrits web interface.
If you have git-review, hard reset and checkout the change
with this command:
git review -d <change number>
Note, if you already have the change in a branch on your
local repository, you can just check it out instead:
7.8
Push to a branch
As you can see in the screenshot above, the commit was Should accomplish the same thing.
pushed to master. The branch name only appeared as
the topic of the commit. If you really want to push to a Next, make some changes.
branch, you have to push via git review <branch name>. vim Example/Example.body.php
10
git add the les as needed, then commit the change (ensuring you are amending the commit):
git add Example/Example.body.php git commit --amend
--all
7.10
11
Now copy the change ID and amend your commit. Always add Change-ID as the last line of your commit message.
7.9.2
This method is helpful for submitting to changes to Gerrit git commit --amend
when SSH is not functional.
This will open an editor to change the commit message.
For this the user needs the HTTP Password which can be Paste the Change-Id as the last line of the message and
generated in the Account Settings of Gerrit under the tab save it. See example:
of HTTP Password. After generating the password the Your commit summary
developer should commit all the changes for one patch Your commit message
under one single commit and use
Bug: Txxxxx
git push https://gerrit.wikimedia.org/r/p/mediawiki/core Change-Id: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
HEAD:refs/for/master
Now you can successfully push to Gerrit.
The authentication credentials need to be entered to successfully submit the changes. The syntax given above
Automatically adding commit Hook To obtain the
is for Mediawiki core and will vary for extensions accommit-msg script:
cordingly. For example if one is pushing to Extention:
scp -p -P 29418 USERNAME@gerrit.wikimedia.org:
LiquidThreads then
hooks/commit-msg <local path to your git>/.git/hooks/
git push https://gerrit.wikimedia.org/r/p/mediawiki/
See
https://gerrit.wikimedia.org/r/Documentation/
extensions/LiquidThreads HEAD:refs/for/master
cmd-hook-commit-msg.html for details.
7.10.1
A major problem that arises when using HTTPS for submitting changes is that commit hook is not automatically This section is untested; please update this section with
attached. A hack for this approach is to make one fail updated information if issues arise
attempt to push. On doing so, the error message will automatically highlight the Change-Id, see below example: If you are behind a proxy server, you also need to
12
clone over HTTPS. Make sure the environment variable 8.2 Who can review? Gerrit project ownHTTPS_PROXY is set correctly. To debug issues, try
ers
running with GIT_CURL_VERBOSE=1, e.g.
GIT_CURL_VERBOSE=1
git
clone
https: Who has the ability to do code review?
//gerrit.wikimedia.org/r/pywikibot/core.git
We use gerrit.wikimedia.org to manage code review.
Anyone can ask for a Gerrit account get one!. Within
In case of issues, try providing HTTPS_PROXY directly, Gerrit, anyone can comment on commits and signal their
criticisms and approvals. Anyone can give a nonbinding
e.g.
"+1 to any commit. However, for any given repository
HTTPS_PROXY=proxy_server
(Gerrit project), only a small group of people will have
GIT_CURL_VERBOSE=1
git
clone
https: the ability to approve code within Gerrit and merge it into
//gerrit.wikimedia.org/r/mediawiki/core.git
the repository. This superapproval is a "+2" even though
thats a misleading name, because two +1 approvals DO
NOT add up to a +2. These people are Gerrit project
or use gits options directly:
owners. Learn about becoming a Gerrit project owner.
GIT_CURL_VERBOSE=1
git
-c
http.proxy="proxy_server
clone
https://gerrit. Even within a Gerrit project, we can also specify particular branches that only specic people can pull into.
wikimedia.org/r/mediawiki/core.git
This last option should also work for SOCKS proxy
8.3
servers, using
GIT_CURL_VERBOSE=1 git -c http.proxy="socks5:
//socks_server" clone https://gerrit.wikimedia.org/r/
mediawiki/core.git
7.11 Di algorithm
Gerrit uses jGits di , which uses a slightly dierent di
algorithm from gits default di algorithm.
To see roughly what a patch will look like in Gerrit, run
jgit show -w.
If jgit isnt available in your packaging system, download jgit.sh from https://eclipse.org/jgit/download/ and
A sample changeset, with annotations
use jgit.sh instead of jgit.
Code review is an essential part of our contribution workow. The principle is basic: any patch must be reviewed
by others before being merged.
This means that your code will need reviewers. Check
our advice for getting reviews.
8.1
Its important to us to have a review-before-merge workow for MediaWiki core and also for any extension we
deploy. We will also oer that option to any extension author who wants it for their extension. The one exception is
localisation and internationalisation commits, which will
be able to be pushed without review.
Side-by-side di
8.3
13
nonbinding, won't cause merges or rejections, and have no formal eect on the
code review.
Abandon change (you'll see this if you wrote
this di). This action removes the di from
the merge queue, but leaves it in Gerrit for
archival purposes.
8.3.2 Comparing patch sets
Every time you amend your commit and submit it for review, a new patch set is created. You can compare the
dierent patch sets like this:
Review screen
preserving.
8.3.1
Select the older patch set in the Old Version History list.
Submit Patch Set 1 button (merge -only useful if you or someone else has already given a +2 approval to the di, but
not merged it)
14
10
//gerrit.wikimedia.org/r/gitweb?p=test/mediawiki/
extensions/examples.git;a=summary .
If you merge a commit that references a ticket in Phabricator, please go to that ticket and mark it RESOLVED
and reference the merge ID.
SEE ALSO
Troubleshooting
9.1
Problems
encountered
commit-msg hook
installing
9.2
This message will appear if you are using openssh >= 7.0.
For more information, see task T112025.
10
See also
15
11
11.1
Gerrit/Tutorial Source: https://www.mediawiki.org/wiki/Gerrit/Tutorial?oldid=2057060 Contributors: Hashar, Wikinaut, Elvey, Skierpage, Purodha, Rogerhc, Turnstep, John Vandenberg, Mattaschen, Amire80, Cacycle, FunPika, Waldir, ^demon, Saper, Dereckson,
Wargo, GrafZahl, Peachey88, Valhallasw, Kaldari, He7d3r, Multichill, Matj Grabovsk, Aude, DrTrigon, Nemo bis, Whym, Xqt, GregRundlett, Giftpanze, Krinkle, Wizardist, *devunt, Ijon, Varnent, Awjrichards, RobLa-WMF, Steven (WMF), Niedzielski, Daniel Kinzler
(WMDE), Petrb, Sumanah, Yuvipanda, Qgil-WMF, Preilly, Van de Bugger, Wctaiwan, Base, , Shirayuki, Jamesmontalvo3, Krenair, MarkTraceur, Danielle Benoit, Leucosticte, KevinCole, SPage (WMF), UltrasonicNXT, Malyacko, Mattsmith321, Sharihareswara
(WMF), Florianschmidtwelzow, Bcharles, Cblair91, AKlapper (WMF), Suriyaa Kudo, Ckoerner, Glaisher, Wmdv, 01tonythomas, 5xbe,
Xue Fuqiao, Adi.iiita, Guymontag~mediawikiwiki, Tedsa, Firebus, Nuwanga82, Snailtsunami, Jonas.keutel, VEckl (WMF), MHolloway
(WMF), TJones (WMF), Dwlocks, JadeMaveric, Dhshzj and Anonymous: 27
11.2
Images
16
11
https://upload.wikimedia.org/wikipedia/mediawiki/2/28/Vim_example.body_file.png Li-
11.3
Content license