ssh-keygen -t rsa
Code Review with Gerrit, a mostly visual guideUpdate: Some of this information is out of date. Instead of pushing to the
A while ago, when Paul, Jason and I worked together, I became a big fan of code reviews before merging code. It was no surprise really, we were the first to adopt Git at the company and our workflow was quite ad-hoc, the need to federate knowledge within the group meant code reviews were a pretty big deal. At the time, we mostly did code reviews in person by way of "hey, what's this you're doing here?" or by literally sending patch emails with git-format-patch(1) to the team mailing list so all could participate in the discussion about what merits "good code" exhibited versus "less good code." Now that I've left that company and joined another one, I've found myself in another small-team situation, where my teammates place high value on code review. Fortunately this time around better tools exist, namely: Gerrit. The history behind Gerrit I'm a bit hazy on, what I do know is that it's primary developer Shawn Pearce (spearce) is one of the Git "inner circle" who contributes heavily to Git itself as well as JGit, a Git implementation in Java which sits underneath Gerrit's internals. What makes Gerrit unique in the land of code review systems is how tightly coupled Gerrit is with Git itself, so much so that you submit changes by pushing as if the Gerrit server were "just another Git repo." I recommend building Gerrit from source for now, spearce is planning a proper release of the recent Gerrit developments shortly before Christmas, but who has that kind of patience! To build Gerrit you will need Maven and the Sun JDK 1.6. Setting up the Gerrit daemonFirst you should clone one of Gerrit's dependencies, followed by Gerrit itself:
Once both clones are complete, you can start by building one and then the other (which might take a while, go grab yourself a coffee, you've earned it):
After Gerrit has finished building, you'll have a
After running through Gerrit's brief wizard, you'll be ready to start Gerrit itself (note: this command will not detach from the terminal, so you might want to start it within screen for now):
Now that you've reached this point you'll have Gerrit running a web application on port 8080, and listening for SSH connections on port 29418, congratulations! You're most of the way there :) Creating users and groupsWelcome to Gerrit First thing you should do after starting Gerrit up is log in to make sure your user is the administrator, you can do so by clicking the "Register" link in the top right corner which should present you with an openID login dialog After logging in with your favorite openID provider, Gerrit will allow you to enter in information about you (SSH key, email address, etc). It's worth noting that the email address is very important as Gerrit uses the email address to match your commits to your Gerrit account When you create your SSH key for Gerrit, it's recommended that you give it a custom entry in
After you click "Continue" at the bottom of the user information page, you will be taken to your dashboard which is where your changes waiting to be reviewed as well as changes waiting to be reviewed by you will be waiting Now that your account is all set up, let's create a group for "integrators", integrators in Git parlance are those that are responsible for reviewing code and integrating it into the "official" repository (typically integrators are project maintainers or core developers). Be sure to add yourself to the "Integrators" group, we'll use this "Integrators" group later to create more granular permissions on a particular project: Projects in GerritCreating a new project in Gerrit is fairly easy but a little different insofar that there isn't a web UI for doing so but there is a command line one:
For the purposes of my examples moving forward, we'll use a project created in Gerrit for one of the Python modules I maintain, py-yajl. After creating the "py-yajl" project with the command line, I can visit Admin > Projects and select "py-yajl" and edited some of its permissions. Here we'll give "Integrators" the ability to Verify changes as well as Push Branch. With the py-yajl project all set up in Gerrit, I can return to my Git repository and add a "remote" for Gerrit, and push my master branch to it
This will give Gerrit a baseline for reviewing changes against and allow it to determine when a change has been merged down. Before getting down to business and starting to commit changes, it's recommended that you install the Gerrit Change-Id commit-msg hook documented here which will help Gerrit track changes through rebasing; once that's taken care of, have at it!
The last command will push my commit to Gerrit, the command is kind of weird looking so feel free to put it behind a git-alias(1). After the push is complete however, my changes will be awaiting review in Gerrit At this point, you'd likely wait for another reviewer to come along and either comment your code inline in the side-by-side viewer or otherwise approve the commit bu clicking "Publish Comments" After comments have been published, the view in My Dashboard has changed to indicate that the change has not only been reviewed but also verified: Upon seeing this, I can return back to my Git repository and feel comfortable merging my code to the master branch:
The last command is significant again, by pushing the updated master branch to Gerrit, we indicate that the change has been merged, which is also reflected in My Dashboard Tada! You've just had your code reviewed and subsequently integrated into the upstream tree, pat yourself on the back. It's worth noting that while Gerrit is under steady development it is being used by the likes of the Android team, JGit/EGit team and countless others. Gerrit contains a number of nice subtle features, like double-clicking a line inside the side-by-side diff to add a comment to that line specifically, the ability to "star" changes (similar to bookmarking) and a too many others to go into detail in this post. While it may seem like this was a fair amount of set-up to get code reviews going, the payoff can be tremendous, Gerrit facilitates a solid Git-oriented code review process that scales very well with the number of committers and changes. I hope you enjoy it :) note: Gerrit supports three methods of uploading changes:
All three methods rely on SSH public key authentication, which must first be configured by the uploading user. SSHEach user uploading changes to Gerrit must configure one or more SSH
public keys. The per-user SSH key list can be accessed over the web
within Gerrit by ConfigurationTo register a new SSH key for use with Gerrit, paste the contents of
your Typically SSH keys are stored in your home directory, under
Then copy the content of the public key file onto your clipboard, and paste it into Gerrit’s web interface:
Testing ConnectionsTo verify your SSH key is working correctly, try using an SSH client to connect to Gerrit’s SSHD port. By default Gerrit runs on port 29418, using the same hostname as the web server:
In the command above, To determine the port number Gerrit is running on, visit the special
information URL
If you are developing an automated tool to perform uploads to Gerrit,
let the user supply the hostname or the web address for Gerrit,
and obtain the port number on the fly from the git pushCreate ChangesTo create new changes for review, simply push to the project’s
magical
E.g.
Each new commit uploaded by the Other users (e.g. project owners) who have configured Gerrit to notify them of new changes will be automatically sent an email message when the push is completed. To include a short tag associated with all of the changes in the same group, such as the local topic branch name, append it after the destination branch name. In this example the short topic tag driver/i42 will be saved on each change this push creates or updates:
If you are frequently uploading changes to the same Gerrit server,
consider adding an SSH host block in
Specific reviewers can be requested and/or additional carbon
copies of the notification message may be sent by including these
as arguments to
The If you are frequently sending changes to the same parties and/or
branches, consider adding a custom remote block to your project’s
Replace ChangesTo add an additional patch set to a change, ensure Change-Id
lines were created in the original commit messages, and just use
If Change-Id lines are not present in the commit messages, consider
amending the message and copying the line from the change’s page
on the web, and then using If Change-Id lines are not available, then the user must use the manual mapping technique described below. For more about Change-Ids, see Change-Id Lines. Manual Replacement MappingTo add an additional patch set to a change, replacing it with an
updated version of the same logical modification, send the new
commit to the change’s ref. For example, to add the commit whose
SHA-1 starts with
This form can be combined together with For example, consider the following sequence of events:
At the final step during the push Gerrit will attach A' as a new patch set on change 1500; B' as a new patch set on change 1501; C' as a new patch set on 1502; and D will be created as a new change. Ensuring D is created as a new change requires passing the refspec
Bypass ReviewChanges (and annotated tags) can be pushed directly into a repository, bypassing the review process. This is primarily useful for a project owner to create new branches, create annotated tags for releases, or to force-update a branch whose history needed to be rewritten. Gerrit restricts direct pushes that bypass review to:
To push branches, the proper access rights must be configured first. Here follows a few examples of how to configure this in Gerrit:
To push annotated tags, the Project owners may wish to grant themselves repo uploadrepo is a multiple repository management tool, most commonly used by the Android Open Source Project. For more details, see using repo. Create ChangesTo upload changes to a project using Each new commit uploaded by For more details on using Replace ChangesTo replace changes, ensure Change-Id lines were created in the
commit messages, and just use If Change-Id lines are not present in the commit messages, consider amending the message and copying the line from the change’s page on the web. If Change-Id lines are not available, then the user must use the much
more manual mapping technique offered
by using For more about Change-Ids, see Change-Id Lines. |
|
来自: langhuayipian > 《git》