Gerrit/git-review

From MediaWiki.org
Jump to: navigation, search

git-review is a command-line tool for Git / Gerrit. It makes it easier to configure your git clone, to submit a change or to fetch an existing one.

The upstream project is led by Openstack. Not to be confused with the unrelated Facebook project.

Installation[edit | edit source]

See also Gerrit/Tutorial#Installing git-review.

Linux[edit | edit source]

Debian[edit | edit source]

Users of Debian or related distributions (e.g., Ubuntu, Mint) on a shared host usually do not have root access. If you cannot get you hosting administrator to install git-review for you, you can try to install it locally. Go to the appropriate section below

Having root access[edit | edit source]

You can do

 sudo apt-get install python-pip
 sudo easy_install pip
 sudo pip install git-review

If you do not have easy_install set up, try

 sudo apt-get install python-setuptools

Then run:

 git review -s

in your cloned copy to setup to work with Gerrit. It will probably ask you for your commit username. Then it will automatically install the commit-msg hook.

Without root access[edit | edit source]

If you are on a shared host and your system does not have git-review, you can install it locally in your user directory.

virtualenv --python=/usr/bin/python2.6 virtualenv
virtualenv/bin/pip install git-review

If this is your first local install, or you have not done that already, extend your PATH to include the the local binaries. There are two ways to do that. The more convervative way

PATH=$PATH:~/virtualenv/bin/

lets the system look through all binary directories first, and only looks into the local binary directory if a command was not found elsewhere. If you want software installed locally in your home directory to override system programs, you need the inverse the sort order.

PATH=~/virtualenv/bin/:$PATH

You probably did that earlier already. You may want to add either command to your login sequence to have it run automatically.

If you do not want to alter your PATH variable, you don't have to. In this case, you cannot use the command git review but must remember to use ~/virtualenv/bin/git-review instead.

Finally run:

 git review -s

or

 ~/virtualenv/bin/git-review -s

respectively, in your cloned copy to set it up to work with Gerrit. It will probably ask you for your commit username. Then it will automatically install the commit-msg hook.

Fedora/CentOS[edit | edit source]

 sudo yum install git-review

You may find that yum cannot find the git-review package -- in this case you can enable the REMI repository and try again.

OpenSUSE[edit | edit source]

Install (YaST) the python-setuptools, then

 easy_install pip
 pip install git-review

As of OpenSUSE 13.1, git-review is included in the main repository, so you can use,

sudo zypper in git-review

Gentoo[edit | edit source]

 emerge dev-python/pip
 pip install git-review

Or use ebuild http://data.gpo.zugaina.org/openstack/dev-vcs/git-review/git-review-9999.ebuild

FreeBSD[edit | edit source]

You can directly install git-review through ports.

 cd /usr/ports/devel/git-review
 make install

Windows[edit | edit source]

Python is needed for git-review to function and pip is used for its installation:

  • Install Python or upgrade to most current version (either 3.3.x or 2.7.x depending on your python installation).
  • Install pip by following the instructions here.
  • Add your python scripts directory to the system path (e.g. C:\Python27\Scripts\). Different directories in path are delimited by a semicolon ";" only. Do not add any whitespace to path list.
  • Run Git Bash as Administrator (right click on icon for this option) and install git-review with the following command:
$ pip install git-review

Mac OS X[edit | edit source]

Mac OS X comes with Python (for now) but not the installation programs supported by git and git-review.

  1. Open Terminal and change to a directory you're comfortable downloading test Git packages to (such as Downloads or Sites)
  2. Download and install the OSX Installer for Git (optional: Mac OS X 10.9 comes with Git installed)
  3. Install pip (Note: Already included in some older versions of Mac OS X):
sudo easy_install pip
  1. Install git-review:
sudo pip install git-review

Setting up git-review[edit | edit source]

To avoid confusion and conflicts between remotes named "gerrit" and "origin", the Gerrit tutorial recommends adding a git-review config file (.gitreview) telling git review to use "origin" as its default remote.

After cloning a repository, you need to set it up for using git-review. This will automatically happen the first time you try to submit a commit, but it's generally better to do it right after cloning.

 $ cd MyNewProj
 $ git review -s

This may ask you for your git username, if it's different from the shell username you're using.

Internally, this does the following:

  • checks whether accessing the remote repository works
  • if it doesn't, asks for a username and tries again
  • creates a remote called 'gerrit' that points to gerrit
  • installs the commit-msg hook

Troubleshooting for Windows[edit | edit source]

If this fails, you have a couple of options. The first is the use scp (secure copy) from within your local repository's directory, for each repository, before committing edits to it.

$ scp -P 29418 <USERNAME>@gerrit.wikimedia.org:hooks/commit-msg .git/hooks/commit-msg

The second is to try patching "c:\Python27\Scripts\git-review" to make git-review work properly:

--- /c/Python27/Scripts/git-review-old  Tue May 28 20:51:50 2013
+++ /c/Python27/Scripts/git-review      Tue May 28 20:48:03 2013
@@ -952,6 +952,9 @@
     config = get_config(os.path.join(top_dir, ".gitreview"))
     hook_file = os.path.join(git_dir, "hooks", "commit-msg")
 
+    # msysgit hacks to work around scp.exe (--aaron)
+    hook_file = unicode.replace( ".\%s" % os.path.relpath( hook_file ), "\\", "/" )
+
     defaultrebase = convert_bool(
         git_config_get_value("gitreview", "rebase",
                              default=str(config['defaultrebase'])))

If using Python 2.x and git-review 1.17 or earlier, use "str.replace" instead of "unicode.replace".

Another error encountered on Windows is:

    ...
    (top_dir, git_dir) = git_directories()
ValueError: too many values to unpack (expected 2)

One solution to this is to change the return line of git_directories() to use splitlines rather than it's normal split function. Split looks for any whitespace and will produce this error when the path to your source code directory contains a space. Splitlines looks for newline characters and will not produce this same error.

     return out.splitlines()

Submitting changes with git-review[edit | edit source]

Submitting changes with git review does not involve a lot of commands:

$ git checkout -b mycoolfeature
change files
$ git commit -a
$ git review

What happens when you submit a change[edit | edit source]

When you submit a change, git review does the following things:

  • it looks up which branch to push to (production or whatever) in the .gitreview file. If it can't find this information, it pushes to master
  • it figures out what "topic" to put on the revision (you can set the topic manually with -t)
    • if you're resubmitting a downloaded change, it will reuse the tag of the original change
    • if your commit summary contains a bug number like bug 12345, the tag will be bug/12345
    • otherwise, the tag will be the name of your local branch
  • it rebases your change against the HEAD of the branch you're pushing to (use -R to skip this)
  • if you are submitting more than one change at once, or submitting a change that's based on another unmerged change, it will ask you whether you really meant to do that (use -y to skip this)
  • it pushes the change for review

Downloading a change with git-review[edit | edit source]

When downloading a change from gerrit to review it or amend it, git-review offers an easier alternative to copypasting a magic incantation from the Gerrit web UI. All you need is the sequence number of the change in Gerrit, which you can find in the URL: https://gerrit.wikimedia.org/r/#change,2033.

$ git review -d 2033

This will download the change, put it in a branch called review/AUTHOR/TAG (if the change has no tag, the sequence number will be used instead), and switch to that branch.

Full feature branch workflow with git-review[edit | edit source]

you@yourmachine:~/puppet (production)$ git checkout -b mycoolfeature
you@yourmachine:~/puppet (mycoolfeature)$ vi foobar
you@yourmachine:~/puppet (mycoolfeature)$ git commit -a -m "Committing my cool feature"
you@yourmachine:~/puppet (mycoolfeature)$ git review -f
you@yourmachine:~/puppet (production)$

If the -f flag is passed to git-review, it will try to submit the change, and if it succeeds it will switch back to the master branch (production in this case) and delete the feature branch.

Setting up a repository for git-remote[edit | edit source]

To tell git-remote where your repository is and what the name of the master branch is, you need to create a .gitreview file in the root of the repository, and commit it. The format is the following:

[gerrit]
host=gerrit.wikimedia.org
port=29418
project=operations/puppet.git
defaultbranch=production

The host and project fields are mandatory. The other fields are optional: port defaults to 29418 and defaultbranch defaults to master .

Troubleshooting[edit | edit source]

"Cannot query patchset information"[edit | edit source]

git-review does not work correctly if git generates non-English output. You will see an error like this:

$ git review -d 62474
Cannot query patchset information
The following command failed with exit code 255
    "ssh -x -p None gerrit query --format=JSON --current-patch-set change:62474"
-----------------------
Bad port ' None'
-----------------------

This is due to a bug in git-review.

To work around this on a Linux system, either apply the patch from the bug report above, or set up an alias that forces git to use English output. To do so, put this into your bashrc or similar setup file:

alias git="LANG=C git"

"Could not parse json query response: u'Verified'"[edit | edit source]

git-review version 1.18 has been reported to have issues when trying to review a change from Gerrit. You will see an error like this:

$ git review -d 76352
Could not parse json query response: u'Verified'

This seams to be due to a bug in git-review v1.18 since v1.12 and v1.21 work correct.

To work around this on a Linux system, use another version like v1.12 on fedora or v1.12, v1.21 on ubuntu (by downgrading or removing v1.18 and installing the suitable rpm). Version 1.22 under fedora works also.

"ConfigParser.NoSectionError: No section: 'updates'"[edit | edit source]

If this happens (bugzilla:55732, [1]):

$ git review -s
Traceback (most recent call last):
  File "/usr/local/bin/git-review", line 1196, in <module>
    main()
  File "/usr/local/bin/git-review", line 1108, in main
    needs_update = latest_is_newer()
  File "/usr/local/bin/git-review", line 179, in latest_is_newer
    if not configParser.getboolean("updates", "check"):
  File "/usr/lib/python2.7/ConfigParser.py", line 368, in getboolean
    v = self.get(section, option)
  File "/usr/lib/python2.7/ConfigParser.py", line 607, in get
    raise NoSectionError(section)
ConfigParser.NoSectionError: No section: 'updates'

Adding something like this to ".gitreview" fixes the problem:

[updates]
check=off

See also[edit | edit source]