Friday, January 30, 2015

Gerrit git review fails for initial empty repository

Gerrit is a code review tool built on and for git - a distributed version control system. When creating a project with gerrit, the user has an option to either create a completely empty repository (where an existing project could be imported) or to create a repository with an initial empty commit, as follows:

# gerrit create-project --empty-commit --name myproject
Without the initial empty commit, the underlying git repository is left empty without any revisions or branches.

Another tool used in conjunction with gerrit is git-review. This tool simplifies creation and management of reviews from the command line.

If a user attempts to run git-review on a repository while using the completely empty gerrit repository as the remote, the command may fail since there is no master branch yet created in the remote repository.

# git review
Errors running git rebase -i remotes/origin/master
fatal: Needed a single revision
invalid upstream remotes/origin/master

# git review -R
Had trouble running git log  --decorate --oneline HEAD --not remotes/origin/master --
fatal: bad revision 'remotes/origin/master'

For the first commit, the user can circumvent git-review and instead push directly to gerrit using git.

# git push origin HEAD:refs/for/master
As a result, a new review will be created in gerrit which can then be merged into the project's master branch. After an initial commit is merged into the master branch, the git-review command can be used as usual.

If the above command fails, complaining about the commit message "missing Change-Id in commit message footer" then simply amend the suggested Change-Id to the the commit message.

# git commit --amend