| 1 |
# Remote/`@git` tracking branches |
| 2 |
|
| 3 |
This is a plan to implement more Git-like remote tracking branch UX. |
| 4 |
|
| 5 |
## Objective |
| 6 |
|
| 7 |
`jj` imports all remote branches to local branches by default. As described in |
| 8 |
[#1136], this doesn't interact nicely with Git if we have multiple Git remotes |
| 9 |
with a number of branches. The `auto-track-bookmarks` config can mitigate this |
| 10 |
problem, but we'll get locally-deleted branches instead. |
| 11 |
|
| 12 |
The goal of this plan is to implement |
| 13 |
* proper support for tracking/non-tracking remote branches |
| 14 |
* logically consistent data model for importing/exporting Git refs |
| 15 |
|
| 16 |
[#1136]: https://github.com/jj-vcs/jj/issues/1136 |
| 17 |
|
| 18 |
## Current data model (as of jj 0.8.0) |
| 19 |
|
| 20 |
Under the current model, all remote branches are "tracking" branches, and |
| 21 |
remote changes are merged into the local counterparts. |
| 22 |
|
| 23 |
``` |
| 24 |
branches |
| 25 |
[name]: |
| 26 |
local_target? |
| 27 |
remote_targets[remote]: target |
| 28 |
tags |
| 29 |
[name]: target |
| 30 |
git_refs |
| 31 |
["refs/heads/{name}"]: target # last-known local branches |
| 32 |
["refs/remotes/{remote}/{name}"]: target # last-known remote branches |
| 33 |
# (copied to remote_targets) |
| 34 |
["refs/tags/{name}"]: target # last-known tags |
| 35 |
git_head: target? |
| 36 |
``` |
| 37 |
|
| 38 |
* Remote branches are stored in both `branches[name].remote_targets` and |
| 39 |
`git_refs["refs/remotes"]`. These two are mostly kept in sync, but there |
| 40 |
are two scenarios where remote-tracking branches and git refs can diverge: |
| 41 |
1. `jj branch forget` |
| 42 |
2. `jj op revert`/`restore` in colocated workspace |
| 43 |
* Pseudo `@git` tracking branches are stored in `git_refs["refs/heads"]`. We |
| 44 |
need special case to resolve `@git` branches, and their behavior is slightly |
| 45 |
different from the other remote-tracking branches. |
| 46 |
|
| 47 |
## Proposed data model |
| 48 |
|
| 49 |
We'll add a per-remote-branch `state` to distinguish non-tracking branches |
| 50 |
from tracking ones. |
| 51 |
|
| 52 |
``` |
| 53 |
state = new # not merged in the local branch or tag |
| 54 |
| tracking # merged in the local branch or tag |
| 55 |
# `ignored` state could be added if we want to manage it by view, not by |
| 56 |
# config file. target of ignored remote branch would be absent. |
| 57 |
``` |
| 58 |
|
| 59 |
We'll add a per-remote view-like object to record the last known remote |
| 60 |
branches. It will replace `branches[name].remote_targets` in the current model. |
| 61 |
`@git` branches will be stored in `remotes["git"]`. |
| 62 |
|
| 63 |
``` |
| 64 |
branches |
| 65 |
[name]: target |
| 66 |
tags |
| 67 |
[name]: target |
| 68 |
remotes |
| 69 |
["git"]: |
| 70 |
branches |
| 71 |
[name]: target, state # refs/heads/{name} |
| 72 |
tags |
| 73 |
[name]: target, state = tracking # refs/tags/{name} |
| 74 |
head: target?, state = TBD # refs/HEAD |
| 75 |
[remote]: |
| 76 |
branches |
| 77 |
[name]: target, state # refs/remotes/{remote}/{name} |
| 78 |
tags: (empty) |
| 79 |
head: (empty) |
| 80 |
git_refs # last imported/exported refs |
| 81 |
["refs/heads/{name}"]: target |
| 82 |
["refs/remotes/{remote}/{name}"]: target |
| 83 |
["refs/tags/{name}"]: target |
| 84 |
``` |
| 85 |
|
| 86 |
With the proposed data model, we can |
| 87 |
* naturally support remote branches which have no local counterparts |
| 88 |
* deduplicate `branches[name].remote_targets` and `git_refs["refs/remotes"]` |
| 89 |
|
| 90 |
### Import/export data flow |
| 91 |
|
| 92 |
``` |
| 93 |
export flow import flow |
| 94 |
----------- ----------- |
| 95 |
+----------------+ --. |
| 96 |
+------------------->|backing Git repo|---+ : |
| 97 |
| +----------------+ | : unchanged |
| 98 |
|[update] |[copy] : on "op restore" |
| 99 |
| +----------+ | : |
| 100 |
| +-------------->| git_refs |<------+ : |
| 101 |
| | +----------+ | --' |
| 102 |
+--[compare] [diff]--+ |
| 103 |
| .-- +---------------+ | | --. |
| 104 |
| : +--->|remotes["git"] | | | : |
| 105 |
+---: | | |<---+ | : |
| 106 |
: | |remotes[remote]| | : restored |
| 107 |
'-- | +---------------+ |[merge] : on "op restore" |
| 108 |
| | : by default |
| 109 |
[copy]| +---------------+ | : |
| 110 |
+----| (local) |<---------+ : |
| 111 |
| branches/tags | : |
| 112 |
+---------------+ --' |
| 113 |
``` |
| 114 |
|
| 115 |
* `jj git import` applies diff between `git_refs` and `remotes[]`. `git_refs` is |
| 116 |
always copied from the backing Git repo. |
| 117 |
* `jj git export` copies jj's `remotes` view back to the Git repo. If a ref in |
| 118 |
the Git repo has been updated since the last import, the ref isn't exported. |
| 119 |
* `jj op restore` never rolls back `git_refs`. |
| 120 |
|
| 121 |
### Tracking state |
| 122 |
|
| 123 |
The `auto-track-bookmarks` config knob is applied when importing new remote |
| 124 |
branch. `jj branch` sub commands will be added to change the tracking state. |
| 125 |
|
| 126 |
```rust |
| 127 |
fn default_state_for_newly_imported_branch(config, remote) { |
| 128 |
if remote == "git" { |
| 129 |
State::Tracked |
| 130 |
} else if matches_auto_track_bookmarks { |
| 131 |
State::Tracked |
| 132 |
} else { |
| 133 |
State::New |
| 134 |
} |
| 135 |
} |
| 136 |
``` |
| 137 |
|
| 138 |
A branch target to be merged is calculated based on the `state`. |
| 139 |
|
| 140 |
```rust |
| 141 |
fn target_in_merge_context(known_target, state) { |
| 142 |
match state { |
| 143 |
State::New => RefTarget::absent(), |
| 144 |
State::Tracked => known_target, |
| 145 |
} |
| 146 |
} |
| 147 |
``` |
| 148 |
|
| 149 |
### Mapping to the current data model |
| 150 |
|
| 151 |
* New `remotes["git"].branches` corresponds to `git_refs["refs/heads"]`, but |
| 152 |
forgotten branches are removed from `remotes["git"].branches`. |
| 153 |
* New `remotes["git"].tags` corresponds to `git_refs["refs/tags"]`. |
| 154 |
* New `remotes["git"].head` corresponds to `git_head`. |
| 155 |
* New `remotes[remote].branches` corresponds to |
| 156 |
`branches[].remote_targets[remote]`. |
| 157 |
* `state = new|tracking` doesn't exist in the current model. It's determined |
| 158 |
by `auto-track-bookmarks` config. |
| 159 |
|
| 160 |
## Common command behaviors |
| 161 |
|
| 162 |
In the following sections, a merge is expressed as `adds - removes`. |
| 163 |
In particular, a merge of local and remote targets is |
| 164 |
`[local, remote] - [known_remote]`. |
| 165 |
|
| 166 |
### fetch/import |
| 167 |
|
| 168 |
* `jj git fetch` |
| 169 |
1. Fetches remote changes to the backing Git repo. |
| 170 |
2. Import changes only for `remotes[remote].branches[glob]` (see below) |
| 171 |
* TODO: how about fetched `.tags`? |
| 172 |
|
| 173 |
* `jj git import` |
| 174 |
1. Copies `git_refs` from the backing Git repo. |
| 175 |
2. Calculates diff from the known `remotes` to the new `git_refs`. |
| 176 |
* `git_refs["refs/heads"] - remotes["git"].branches` |
| 177 |
* `git_refs["refs/tags"] - remotes["git"].tags` |
| 178 |
* TBD: `"HEAD" - remotes["git"].head` (unused) |
| 179 |
* `git_refs["refs/remotes/{remote}"] - remotes[remote]` |
| 180 |
3. Merges diff in local `branches` and `tags` if `state` is `tracking`. |
| 181 |
* If the known `target` is `absent`, the default `state` should be |
| 182 |
calculated. This also applies to previously-forgotten branches. |
| 183 |
4. Updates `remotes` reflecting the import. |
| 184 |
5. Abandons commits that are no longer referenced. |
| 185 |
|
| 186 |
### push/export |
| 187 |
|
| 188 |
* `jj git push` |
| 189 |
1. Calculates diff from the known `remotes[remote]` to the local changes. |
| 190 |
* `branches - remotes[remote].branches` |
| 191 |
* If `state` is `new` (i.e. untracked), the known remote branch `target` |
| 192 |
is considered `absent`. |
| 193 |
* If `state` is `new`, and if the local branch `target` is `absent`, the |
| 194 |
diff `[absent, remote] - absent` is noop. So it's not allowed to push |
| 195 |
deleted branch to untracked remote. |
| 196 |
* TODO: Copy Git's `--force-with-lease` behavior? |
| 197 |
* ~`tags`~ (not implemented, but should be the same as `branches`) |
| 198 |
2. Pushes diff to the remote Git repo (as well as remote tracking branches |
| 199 |
in the backing Git repo.) |
| 200 |
3. Updates `remotes[remote]` and `git_refs` reflecting the push. |
| 201 |
|
| 202 |
* `jj git export` |
| 203 |
1. Copies local `branches`/`tags` back to `remotes["git"]`. |
| 204 |
* Conceptually, `remotes["git"].branches[name].state` can be set to |
| 205 |
untracked. Untracked local branches won't be exported to Git. |
| 206 |
* If `remotes["git"].branches[name]` is `absent`, the default |
| 207 |
`state = tracking` applies. This also applies to forgotten branches. |
| 208 |
* ~`tags`~ (not implemented, but should be the same as `branches`) |
| 209 |
2. Calculates diff from the known `git_refs` to the new `remotes[remote]`. |
| 210 |
3. Applies diff to the backing Git repo. |
| 211 |
4. Updates `git_refs` reflecting the export. |
| 212 |
|
| 213 |
If a ref failed to export at the step 3, the preceding steps should also be |
| 214 |
rolled back for that ref. |
| 215 |
|
| 216 |
### init/clone |
| 217 |
|
| 218 |
* `jj init` |
| 219 |
* Import, track, and merge per `git.auto_local_branch` config. |
| 220 |
* If `!git.auto_local_branch`, no `tracking` state will be set. |
| 221 |
|
| 222 |
* `jj git clone` |
| 223 |
* Import, track, and merge per `git.auto_local_branch` config. |
| 224 |
* The default branch will be tracked regardless of `git.auto_local_branch` |
| 225 |
config. This isn't technically needed, but will help users coming from Git. |
| 226 |
|
| 227 |
### branch |
| 228 |
|
| 229 |
* `jj branch set {name}` |
| 230 |
1. Sets local `branches[name]` entry. |
| 231 |
* `jj branch delete {name}` |
| 232 |
1. Removes local `branches[name]` entry. |
| 233 |
* `jj branch forget {name}` |
| 234 |
1. Removes local `branches[name]` entry if exists. |
| 235 |
2. Removes `remotes[remote].branches[name]` entries if exist. |
| 236 |
TODO: maybe better to not remove non-tracking remote branches? |
| 237 |
* `jj branch track {name}@{remote}` (new command) |
| 238 |
1. Merges `[local, remote] - [absent]` in local branch. |
| 239 |
* Same as "fetching/importing existing branch from untracked remote". |
| 240 |
2. Sets `remotes[remote].branches[name].state = tracking`. |
| 241 |
* `jj branch untrack {name}@{remote}` (new command) |
| 242 |
1. Sets `remotes[remote].branches[name].state = new`. |
| 243 |
* `jj branch list` |
| 244 |
* TODO: hide non-tracking branches by default? ... |
| 245 |
|
| 246 |
Note: desired behavior of `jj branch forget` is to |
| 247 |
* discard both local and remote branches (without actually removing branches |
| 248 |
at remotes) |
| 249 |
* not abandon commits which belongs to those branches (even if the branch is |
| 250 |
removed at a remote) |
| 251 |
|
| 252 |
## Command behavior examples |
| 253 |
|
| 254 |
### fetch/import |
| 255 |
|
| 256 |
* Fetching/importing new branch |
| 257 |
1. Decides new `state = new|tracking` based on `git.auto_local_branch` |
| 258 |
2. If new `state` is `tracking`, merges `[absent, new_remote] - [absent]` |
| 259 |
(i.e. creates local branch with `new_remote` target) |
| 260 |
3. Sets `remotes[remote].branches[name].state` |
| 261 |
* Fetching/importing existing branch from tracking remote |
| 262 |
1. Merges `[local, new_remote] - [known_remote]` |
| 263 |
* Fetching/importing existing branch from untracked remote |
| 264 |
1. Decides new `state = new|tracking` based on `git.auto_local_branch` |
| 265 |
2. If new `state` is `tracking`, merges `[local, new_remote] - [absent]` |
| 266 |
3. Sets `remotes[remote].branches[name].state` |
| 267 |
* Fetching/importing remotely-deleted branch from tracking remote |
| 268 |
1. Merges `[local, absent] - [known_remote]` |
| 269 |
2. Removes `remotes[remote].branches[name]` (`target` becomes `absent`) |
| 270 |
(i.e. the remote branch is no longer tracked) |
| 271 |
3. Abandons commits in the deleted branch |
| 272 |
* Fetching/importing remotely-deleted branch from untracked remote |
| 273 |
1. Decides new `state = new|tracking` based on `git.auto_local_branch` |
| 274 |
2. Noop anyway since `[local, absent] - [absent]` -> `local` |
| 275 |
* Fetching previously-forgotten branch from remote |
| 276 |
1. Decides new `state = new|tracking` based on `git.auto_local_branch` |
| 277 |
2. If new `state` is `tracking`, merges |
| 278 |
`[absent, new_remote] - [absent]` -> `new_remote` |
| 279 |
3. Sets `remotes[remote].branches[name].state` |
| 280 |
* Fetching forgotten and remotely-deleted branch |
| 281 |
* Same as "remotely-deleted branch from untracked remote" since forgotten |
| 282 |
remote branch should be `state = new` |
| 283 |
* Therefore, no local commits should be abandoned |
| 284 |
|
| 285 |
### push |
| 286 |
|
| 287 |
* Pushing new branch, remote doesn't exist |
| 288 |
1. Pushes `[local, absent] - [absent]` -> `local` |
| 289 |
2. Sets `remotes[remote].branches[name].target = local`, `.state = tracking` |
| 290 |
* Pushing new branch, untracked remote exists |
| 291 |
1. Pushes `[local, remote] - [absent]` |
| 292 |
* Fails if `local` moved backwards or sideways |
| 293 |
2. Sets `remotes[remote].branches[name].target = local`, `.state = tracking` |
| 294 |
* Pushing existing branch to tracking remote |
| 295 |
1. Pushes `[local, remote] - [remote]` -> `local` |
| 296 |
* Fails if `local` moved backwards or sideways, and if `remote` is out of |
| 297 |
sync |
| 298 |
2. Sets `remotes[remote].branches[name].target = local` |
| 299 |
* Pushing existing branch to untracked remote |
| 300 |
* Same as "new branch" |
| 301 |
* Pushing deleted branch to tracking remote |
| 302 |
1. Pushes `[absent, remote] - [remote]` -> `absent` |
| 303 |
* TODO: Fails if `remote` is out of sync? |
| 304 |
2. Removes `remotes[remote].branches[name]` (`target` becomes `absent`) |
| 305 |
* Pushing deleted branch to untracked remote |
| 306 |
* Noop since `[absent, remote] - [absent]` -> `remote` |
| 307 |
* Perhaps, UI will report error |
| 308 |
* Pushing forgotten branch to untracked remote |
| 309 |
* Same as "deleted branch to untracked remote" |
| 310 |
* Pushing previously-forgotten branch to remote |
| 311 |
* Same as "new branch, untracked remote exists" |
| 312 |
* The `target` of forgotten remote branch is `absent` |
| 313 |
|
| 314 |
### export |
| 315 |
|
| 316 |
* Exporting new local branch, git branch doesn't exist |
| 317 |
1. Sets `remotes["git"].branches[name].target = local`, `.state = tracking` |
| 318 |
2. Exports `[local, absent] - [absent]` -> `local` |
| 319 |
* Exporting new local branch, git branch is out of sync |
| 320 |
1. Exports `[local, git] - [absent]` -> fail |
| 321 |
* Exporting existing local branch, git branch is synced |
| 322 |
1. Sets `remotes["git"].branches[name].target = local` |
| 323 |
2. Exports `[local, git] - [git]` -> `local` |
| 324 |
* Exporting deleted local branch, git branch is synced |
| 325 |
1. Removes `remotes["git"].branches[name]` |
| 326 |
2. Exports `[absent, git] - [git]` -> `absent` |
| 327 |
* Exporting forgotten branches, git branches are synced |
| 328 |
1. Exports `[absent, git] - [git]` -> `absent` for forgotten local/remote |
| 329 |
branches |
| 330 |
|
| 331 |
### undo fetch |
| 332 |
|
| 333 |
* Exporting undone fetch, git branches are synced |
| 334 |
1. Exports `[old, git] - [git]` -> `old` for undone local/remote branches |
| 335 |
* Redoing undone fetch without exporting |
| 336 |
* Same as plain fetch since the known `git_refs` isn't diffed against the |
| 337 |
refs in the backing Git repo. |
| 338 |
|
| 339 |
### `@git` remote |
| 340 |
|
| 341 |
* `jj branch untrack {name}@git` |
| 342 |
* Maybe rejected (to avoid confusion)? |
| 343 |
* Allowing this would mean different local branches of the same name coexist |
| 344 |
in jj and git. |
| 345 |
* `jj git fetch --remote git` |
| 346 |
* Rejected. The implementation is different. |
| 347 |
* Conceptually, it's `git::import_refs()` only for local branches. |
| 348 |
* `jj git push --remote git` |
| 349 |
* Rejected. The implementation is different. |
| 350 |
* Conceptually, it's `jj branch track` and `git::export_refs()` only for |
| 351 |
local branches. |
| 352 |
|
| 353 |
## Remaining issues |
| 354 |
|
| 355 |
* <https://github.com/jj-vcs/jj/issues/1278> pushing to tracked remote |
| 356 |
* Option could be added to push to all `tracking` remotes? |
| 357 |
* Track remote branch locally with different name |
| 358 |
* Local branch name could be stored per remote branch |
| 359 |
* Consider UI complexity |
| 360 |
* "private" state (suggested by @ilyagr) |
| 361 |
* "private" branches can be pushed to their own remote, but not to the |
| 362 |
upstream repo |
| 363 |
* This might be a state attached to a local branch (similar to Mercurial's |
| 364 |
"secret" phase) |
| 365 |
|
| 366 |
## References |
| 367 |
|
| 368 |
* <https://github.com/jj-vcs/jj/issues/1136> |
| 369 |
* <https://github.com/jj-vcs/jj/issues/1666> |
| 370 |
* <https://github.com/jj-vcs/jj/issues/1690> |
| 371 |
* <https://github.com/jj-vcs/jj/issues/1734> |
| 372 |
* <https://github.com/jj-vcs/jj/pull/1739> |