| 1 |
# Using Jujutsu with Gerrit Code Review |
| 2 |
|
| 3 |
JJ and Gerrit share the same mental model, which makes Gerrit feel like a |
| 4 |
natural collaboration tool for JJ. JJ tracks a "change identity" across |
| 5 |
rewrites, and Gerrit's `Change-Id` tracks the same logical change across patch |
| 6 |
sets. JJ and Gerrit's `Change-Id`s aren't natively compatible yet, but they're |
| 7 |
philosophically aligned. `jj gerrit upload` bridges the gap today by adding a |
| 8 |
Gerrit-style `Change-Id` trailer while JJ keeps its own notion of change |
| 9 |
identity on the client. In practice, that means small, clean commits that evolve |
| 10 |
over time, exactly how Gerrit wants you to work. |
| 11 |
|
| 12 |
This guide assumes a basic understanding of Git, Gerrit, and Jujutsu. |
| 13 |
|
| 14 |
## Set up a Gerrit remote |
| 15 |
|
| 16 |
Jujutsu communicates with Gerrit by pushing commits to a Git remote. If you're |
| 17 |
starting from an existing Git repository with Gerrit remotes already configured, |
| 18 |
you can use `jj git init` to start using JJ in that repo. Otherwise, set up your |
| 19 |
Gerrit remote: |
| 20 |
|
| 21 |
```shell |
| 22 |
# Option 1: Start JJ in an existing Git repo with Gerrit remotes |
| 23 |
$ jj git init |
| 24 |
|
| 25 |
# Option 2: Add a Gerrit remote to a JJ repo |
| 26 |
$ jj git remote add gerrit https://review.gerrithub.io/yourname/yourproject |
| 27 |
|
| 28 |
# Option 3: Clone the repo via jj |
| 29 |
$ jj git clone https://review.gerrithub.io/your/project |
| 30 |
``` |
| 31 |
|
| 32 |
If you used option 2, you can configure default values in your repository config |
| 33 |
by appending the following lines to your config file, like so (to do this for |
| 34 |
a specific repo, run `jj config edit --repo`): |
| 35 |
|
| 36 |
```toml |
| 37 |
[gerrit] |
| 38 |
default-remote = "gerrit" # name of the Git remote to push to |
| 39 |
default-remote-branch = "main" # target branch in Gerrit |
| 40 |
``` |
| 41 |
|
| 42 |
## Basic workflow |
| 43 |
|
| 44 |
`jj gerrit upload` takes one or more revsets and uploads the stack of commits |
| 45 |
and their ancestors to Gerrit. Each JJ change will map to a single Gerrit change |
| 46 |
by generating a `Change-Id` based on the JJ change ID (or using the existing |
| 47 |
`Change-Id` trailer if already present). This should be what you want most of |
| 48 |
the time, but if you want to associate a JJ change with a specific change |
| 49 |
already uploaded to Gerrit, you can copy the `Change-Id` trailer from Gerrit to |
| 50 |
the bottom of the commit description in JJ. |
| 51 |
|
| 52 |
> Note: Gerrit identifies and updates changes by the `Change-Id` trailer. When |
| 53 |
> you re-upload a commit with the same `Change-Id`, Gerrit creates a new patch |
| 54 |
> set. |
| 55 |
|
| 56 |
### Upload a single change |
| 57 |
|
| 58 |
```shell |
| 59 |
# Uploads `@` if it has a description, otherwise uploads `@-`. |
| 60 |
$ jj gerrit upload |
| 61 |
|
| 62 |
# Or explicitly specify a revision to upload. |
| 63 |
$ jj gerrit upload -r @- |
| 64 |
``` |
| 65 |
|
| 66 |
## Selecting revisions (revsets) |
| 67 |
|
| 68 |
`jj gerrit upload` accepts one or more `-r`/`--revisions` arguments. Each |
| 69 |
argument may expand to multiple commits. Common patterns: |
| 70 |
|
| 71 |
- `-r @-`: the commit previous to the one you're currently working on |
| 72 |
- `-r A..B`: commits that are ancestors of B but not of A |
| 73 |
|
| 74 |
See the [revsets](revsets.md) guide for more information. |
| 75 |
|
| 76 |
### Preview without pushing |
| 77 |
|
| 78 |
Use `--dry-run` to see which commits would be modified and pushed, and where, |
| 79 |
without changing anything or contacting the remote. |
| 80 |
|
| 81 |
```shell |
| 82 |
$ jj gerrit upload -r '@-' --remote-branch main --dry-run |
| 83 |
``` |
| 84 |
|
| 85 |
## Target branch and remote selection |
| 86 |
|
| 87 |
There are a few way of specifying the target remote for your projects: |
| 88 |
|
| 89 |
- Run `jj config set --user gerrit.default-remote <remote name>` to set your |
| 90 |
default remote across all repos. |
| 91 |
- Run `jj config set --repo gerrit.default-remote <remote name>` to set your |
| 92 |
default remote for this specific repo. |
| 93 |
- Use `--remote <remote name>` to use a specific remote in one invocation of |
| 94 |
`jj gerrit upload`. |
| 95 |
- Otherwise, a remote named `gerrit` will be used. If that doesn't exist, the |
| 96 |
command will fail, and you will need to use one of the above methods to |
| 97 |
specify the remote to upload to. |
| 98 |
|
| 99 |
Additionally, you can specify the target remote branch: |
| 100 |
|
| 101 |
- Run `jj config set --user gerrit.default-remote-branch <branch name>` to set |
| 102 |
your default branch across all repos. |
| 103 |
- Run `jj config set --repo gerrit.default-remote-branch <branch name>` to set |
| 104 |
your default branch for this specific repo. |
| 105 |
- Use `--remote-branch <branch name>` to use a specific branch in one invocation |
| 106 |
of `jj gerrit upload`. |
| 107 |
|
| 108 |
## Updating changes after review |
| 109 |
|
| 110 |
To address review feedback, update your revisions, then run `jj gerrit |
| 111 |
upload` again with the same revsets. Gerrit will add new patch sets to the |
| 112 |
existing changes instead of creating new ones. |
| 113 |
|
| 114 |
Examples: |
| 115 |
|
| 116 |
```shell |
| 117 |
# Edit an earlier commit in the stack |
| 118 |
$ jj edit xyz # position on the stack to edit |
| 119 |
--- Apply needed edits --- |
| 120 |
$ jj gerrit upload -r xyz |
| 121 |
``` |
| 122 |
|
| 123 |
## `Change-Id` management |
| 124 |
|
| 125 |
If you do not provide an explicit `Change-Id` trailer in your commits, |
| 126 |
`jj gerrit upload` will generate a transient one for you based on your JJ |
| 127 |
change ID. This means that as long as the JJ change ID remains the same (and |
| 128 |
you don't add an explicit `Change-Id` trailer), it will upload as a new patch |
| 129 |
set on the existing change. |
| 130 |
|
| 131 |
Keep this association in mind when splitting or squashing changes. For example, |
| 132 |
when splitting a change, the portion that you want associated with the |
| 133 |
original Gerrit change should remain in the original JJ change (the first half |
| 134 |
of the split). Similarly, when squashing new changes, you typically want to |
| 135 |
squash into the change that was previously uploaded to Gerrit. |
| 136 |
|
| 137 |
If your JJ changes no longer align with the desired mapping to Gerrit changes, |
| 138 |
you can manually copy a Gerrit `Change-Id` trailer into your JJ change |
| 139 |
description to directly assign a JJ change to an exist Gerrit change. |
| 140 |
|
| 141 |
As an alternative to `jj gerrit upload`'s automatic `Change-Id` mapping, you |
| 142 |
can configure JJ to automatically add `Change-Id` trailer to all change |
| 143 |
descriptions: |
| 144 |
|
| 145 |
```toml |
| 146 |
[templates] |
| 147 |
commit_trailers = ''' |
| 148 |
if( |
| 149 |
!trailers.contains_key("Change-Id"), |
| 150 |
format_gerrit_change_id_trailer(self) |
| 151 |
) |
| 152 |
''' |
| 153 |
``` |
| 154 |
|
| 155 |
In this case, the Gerrit change mapping is defined entirely by the `Change-Id` |
| 156 |
trailer. When splitting or squashing changes, be sure to keep the `Change-Id` |
| 157 |
trailer associated with the desired changes. Be sure not to duplicate the same |
| 158 |
`Change-Id` across different changes. Gerrit will reject pushes that contain |
| 159 |
duplicate `Change-Id`s, but if the uploads are done separately, you may |
| 160 |
unintentionally overwrite an existing change. |
| 161 |
|
| 162 |
Note that when `jj gerrit upload` automatically adds a `Change-Id` trailer |
| 163 |
before pushing to Gerrit, this addition to the commit description will only be |
| 164 |
reflected on the uploaded commit, not your local commit. This means the server |
| 165 |
will have a different commit ID than you do. As a result, you may encounter |
| 166 |
divergence when you fetch a merged change into your local repo. To address this, |
| 167 |
you can abandon your local change or rebase it on top of trunk with |
| 168 |
`jj rebase --skip-emptied ...`, which will resolve the divergence. Alternatively, |
| 169 |
add the above config so that the `Change-Id` trailer is automatically added to |
| 170 |
your local commits before `jj gerrit upload` does. |
| 171 |
|
| 172 |
## Alternative `Link` trailer |
| 173 |
|
| 174 |
Since version 3.3.1 Gerrit supports an alternative to the `Change-Id` trailer, |
| 175 |
using a `Link` trailer in the format of `<review-url>/id/I<change-id>`. It is |
| 176 |
only documented in the [commit-msg hook documentation]. Jujutsu's |
| 177 |
`jj gerrit upload` will do the same if you set |
| 178 |
`jj config set --repo gerrit.review-url <review-url>`. |
| 179 |
|
| 180 |
[commit-msg hook documentation]: https://gerrit-documentation.storage.googleapis.com/Documentation/3.3.1/cmd-hook-commit-msg.html |