Add reviewers via the commit message
gerrit, git
Solution
It is not possible to set per-commit reviewers, these are applied per push (see gerrit's git-receive-pack manual). Instead of executing `git push origin HEAD` or `git review` (assuming `origin` to be the gerrit remote, and `HEAD` the branch you want to push), you can run the following to add two reviewers for all new commits:
git push origin HEAD:refs/for/master%r=alice@example.com,r=bob@example.com
That gets applied to all commits which is not what you want. Due to the above limitations, let's change the workflow to push changes first and then set some reviewers.
Since Gerrit distinguishes between `Cc` (just send a mail notification) and reviewers (send a mail, but also mark the user as reviewer), I'll modify the commit message as follows:
component: make foo more bar
Foo was not bar enough, this change adds more bar to make foo fit better
in baz.
R=alice@example.com
R=bob@example.net
Change-Id: I4724e283214e0bfbb85a8e3d8db4971618e2609a
Given a number of commits, one can follow the following steps to add separate reviewers for each commit:
- Gather a list of commits IDs (or `Change-Id`s). Example that assumes the master branch as base: `git rev-list --reverse origin/master..`
- For each commit ID, scan for `R=...` (reviewers) in the commit message. The commit message for a given commit can be found with `git show --no-patch --format=%b COMMIT_ID`
- If reviewers exist for a commit message, add them with the command `ssh -p 29418 user@host 'gerrit set-reviewers -a bob@example.net COMMIT_ID'` (instead of `COMMIT_ID`, you can also use the `Change-Id` which is `I4724e283214e0bfbb85a8e3d8db4971618e2609a` for the example).
To perform the above steps (together with auto-detecting the user, host and port settings), I wrote a Bash script: https://git.lekensteyn.nl/scripts/tree/git/gerrit-add-reviewers
It is recommended to have a `.gitreview` file in your repo with a remote that points to a Gerrit instance. Then execute `~/scripts/gerrit-add-reviews origin/master..` from within that git repo to scan commit messages and add reviewers.
Problem
Is it possible to add reviewers in Gerrit via a commit message? Consider this commit message: ``` component: make foo more bar Foo was not bar enough, this change adds more bar to make foo fit better in baz. Change-Id: I4724e283214e0bfbb85a8e3d8db4971618e2609a Cc: alice@example.com Cc: bob@example.net ``` Here, `alice@example.com` and `bob@example.net` must be added as reviewers when pushing to gerrit. I am aware of a special branch specifier to add reviewers, but would like to have something more automated as I create commits. The changes are independent, though it would be nice if I could group them on a topic branch because they are related.