| 1 |
# Architecture |
| 2 |
|
| 3 |
## Data model |
| 4 |
|
| 5 |
The commit data model is similar |
| 6 |
to [Git's object model](https://git-scm.com/book/en/v2/Git-Internals-Git-Objects) |
| 7 |
, but with some differences. |
| 8 |
|
| 9 |
## Separation of library from UI |
| 10 |
|
| 11 |
The `jj` binary consists of two Rust crates: the library crate (`jj-lib`) and |
| 12 |
the CLI crate (`jj-cli`). The library crate is currently only used by the CLI |
| 13 |
crate, but it is meant to also be usable from a GUI or TUI, or in a server |
| 14 |
serving requests from multiple users. As a result, the library should avoid |
| 15 |
interacting directly with the user via the terminal or by other means; all |
| 16 |
input/output is handled by the CLI crate [^1]. Since the library crate is meant |
| 17 |
to usable in a server, it also cannot read configuration from the user's home |
| 18 |
directory, or from user-specific environment variables. |
| 19 |
|
| 20 |
[^1]: There are a few exceptions, such as for messages printed during automatic |
| 21 |
upgrades of the repo format |
| 22 |
|
| 23 |
A lot of thought has gone into making the library crate's API easy to use, but |
| 24 |
not much has gone into "details" such as which collection types are used, or |
| 25 |
which symbols are exposed in the API. |
| 26 |
|
| 27 |
## Storage-independent APIs |
| 28 |
|
| 29 |
One overarching principle in the design is that it should be easy to change |
| 30 |
where data is stored. The goal was to be able to put storage on local-disk by |
| 31 |
default but also be able to move storage to the cloud at Google |
| 32 |
(and for anyone). To that end, commits (and trees, files, etc.) are stored by |
| 33 |
the commit backend, operations (and views) are stored by the operation backend, |
| 34 |
the heads of the operation log are stored by the "op heads" backend, the commit |
| 35 |
index is stored by the index backend, and the working copy is stored by the |
| 36 |
working copy backend. The interfaces are defined in terms of plain Rust data |
| 37 |
types, not tied to a specific format. The working copy doesn't have its own |
| 38 |
trait defined yet, but its interface is small and easy to create traits for when |
| 39 |
needed. |
| 40 |
|
| 41 |
The commit backend to use when loading a repo is specified in |
| 42 |
the `.jj/repo/store/type` file. There are similar files for the other backends |
| 43 |
(`.jj/repo/index/type`, `.jj/repo/op_store/type`, `.jj/repo/op_heads/type`). |
| 44 |
|
| 45 |
## Design of the library crate |
| 46 |
|
| 47 |
### Overview |
| 48 |
|
| 49 |
Here's a diagram showing some important types in the library crate, and how they |
| 50 |
relate. For example, given a `Workspace`, you can use it to get a `WorkingCopy` |
| 51 |
or a `RepoLoader`. A `Transaction` is required to acquire a `MutableRepo`, etc. |
| 52 |
The following sections describe each component. |
| 53 |
|
| 54 |
 |
| 55 |
|
| 56 |
_This diagram was created with [Excalidraw]. You can get a copy of it [at this |
| 57 |
location][types-drawing], and Right Click > "Copy to Clipboard as SVG"._ |
| 58 |
|
| 59 |
[Excalidraw]: https://excalidraw.com/ |
| 60 |
[types-drawing]: https://excalidraw.com/#json=X081PJVRW2h4iW12qZxc1,H2Yg3qnycVG0Vpq5Z90Miw |
| 61 |
|
| 62 |
### Backend |
| 63 |
|
| 64 |
The `Backend` trait defines the interface each |
| 65 |
commit backend needs to implement. The current in-tree commit backends |
| 66 |
are `GitBackend` |
| 67 |
and `SimpleBackend`. |
| 68 |
|
| 69 |
Since there are non-commit backends, the `Backend` trait should probably be |
| 70 |
renamed to `CommitBackend`. |
| 71 |
|
| 72 |
### GitBackend |
| 73 |
|
| 74 |
The `GitBackend` stores commits in a Git repository. It uses |
| 75 |
[`gitoxide`](https://github.com/GitoxideLabs/gitoxide) to read and write commits |
| 76 |
and refs. |
| 77 |
|
| 78 |
To prevent GC from deleting commits that are still reachable from the operation |
| 79 |
log, the `GitBackend` stores a ref for each commit in the operation log in |
| 80 |
the `refs/jj/keep/` namespace. |
| 81 |
|
| 82 |
Commit data that is available in Jujutsu's model but not in Git's model is |
| 83 |
stored in a `StackedTable` in `.jj/repo/store/extra/`. That is currently the |
| 84 |
change ID and the list of predecessors. For commits that don't have any data in |
| 85 |
that table, which is any commit created by `git`, we use an empty list as |
| 86 |
predecessors, and the bit-reversed commit ID as change ID. |
| 87 |
|
| 88 |
Because we use the Git Object ID as commit ID, two commits that differ only in |
| 89 |
their change ID, for example, will get the same commit ID, so we error out when |
| 90 |
trying to write the second one of them. |
| 91 |
|
| 92 |
### SimpleBackend |
| 93 |
|
| 94 |
The `SimpleBackend` is just a proof of concept. It stores objects addressed by |
| 95 |
their hash, with one file per object. |
| 96 |
|
| 97 |
### Store |
| 98 |
|
| 99 |
The `Store` type wraps the `Backend` and returns wrapped types for commits and |
| 100 |
trees to make them easier to use. The wrapped objects have a reference to |
| 101 |
the `Store` itself, so you can do e.g. `commit.parents()` without having to |
| 102 |
provide the `Store` as an argument. |
| 103 |
|
| 104 |
The `Store` type also provides caching of commits and trees. |
| 105 |
|
| 106 |
### ReadonlyRepo |
| 107 |
|
| 108 |
A `ReadonlyRepo` represents the state of a repo at a specific operation. It |
| 109 |
keeps the view object associated with that operation. |
| 110 |
|
| 111 |
The repository doesn't know where on disk any working copies live. It knows, via |
| 112 |
the view object, which commit is supposed to be the current working-copy commit |
| 113 |
in each workspace. |
| 114 |
|
| 115 |
### MutableRepo |
| 116 |
|
| 117 |
A `MutableRepo` is a mutable version of `ReadonlyRepo`. It has a reference to |
| 118 |
its base `ReadonlyRepo`, but it has its own copy of the view object and lets the |
| 119 |
caller modify it. |
| 120 |
|
| 121 |
### Transaction |
| 122 |
|
| 123 |
The `Transaction` object has a `MutableRepo` and metadata that will go into the |
| 124 |
operation log. When the transaction commits, the `MutableRepo` becomes a view |
| 125 |
object in the operation log on disk, and the `Transaction` object becomes an |
| 126 |
operation object. In memory, `Transaction::commit()` returns a |
| 127 |
new `ReadonlyRepo`. |
| 128 |
|
| 129 |
### RepoLoader |
| 130 |
|
| 131 |
The `RepoLoader` represents a repository at an unspecified operation. You can |
| 132 |
think of as a pointer to the `.jj/repo/` directory. It can create |
| 133 |
a `ReadonlyRepo` given an operation ID. |
| 134 |
|
| 135 |
### TreeState |
| 136 |
|
| 137 |
The `TreeState` type represents the state of the files in a working copy. It |
| 138 |
keep track of the mtime and size for each tracked file. It knows the `TreeId` |
| 139 |
that the working copy represents. It has a `snapshot()` method that will use the |
| 140 |
recorded mtimes and sizes and detect changes in the working copy. If anything |
| 141 |
changed, it will return a new `TreeId`. It also has `checkout()` for updating |
| 142 |
the files on disk to match a requested `TreeId`. |
| 143 |
|
| 144 |
The `TreeState` type supports sparse checkouts. In fact, all working copies are |
| 145 |
sparse; they simply track the full repo in most cases. |
| 146 |
|
| 147 |
### WorkingCopy |
| 148 |
|
| 149 |
The `WorkingCopy` type has a `TreeState` but also knows which `WorkspaceName` it |
| 150 |
has and at which operation it was most recently updated. |
| 151 |
|
| 152 |
### Workspace |
| 153 |
|
| 154 |
The `Workspace` type represents the combination of a repo and a working copy |
| 155 |
(like Git's *worktree* concept). |
| 156 |
|
| 157 |
The repo view at the current operation determines the desired working-copy |
| 158 |
commit in each workspace. The `WorkingCopy` determines what is actually in the |
| 159 |
working copy. The working copy can become stale if the working-copy commit was |
| 160 |
changed from another workspace (or if the process updating the working copy |
| 161 |
crashed, for example). |
| 162 |
|
| 163 |
### Git |
| 164 |
|
| 165 |
The `git` module contains functionality for interoperating with a Git repo, at a |
| 166 |
higher level than the `GitBackend`. The `GitBackend` is restricted by |
| 167 |
the `Backend` trait; the `git` module is specifically for Git-backed repos. It |
| 168 |
has functionality for importing refs from the Git repo and for exporting to refs |
| 169 |
in the Git repo. It also has functionality for pushing and pulling to/from Git |
| 170 |
remotes. |
| 171 |
|
| 172 |
### Revsets |
| 173 |
|
| 174 |
A user-provided revset expression string goes through a few different stages to |
| 175 |
be evaluated: |
| 176 |
|
| 177 |
1. Parse the expression into a `RevsetExpression`, which is close to an AST. |
| 178 |
Aliases are also expanded during this stage. |
| 179 |
2. Resolve symbols and functions like `tags()` into specific commits. After |
| 180 |
this stage, the expression is still a `RevsetExpression`, but it won't have |
| 181 |
any `CommitRef` variants in it. |
| 182 |
3. Optimize the expression so that it can be evaluated more efficiently. |
| 183 |
4. Resolve visibility. This stage resolves `visible_heads()` and `all()` and |
| 184 |
produces a `ResolvedExpression`. |
| 185 |
5. Evaluate the `ResolvedExpression` into a `Revset`. |
| 186 |
|
| 187 |
This evaluation step is performed by `Index::evaluate_revset()`, allowing |
| 188 |
the `Revset` implementation to leverage the specifics of a custom index |
| 189 |
implementation. The first four steps are independent of the index |
| 190 |
implementation. |
| 191 |
|
| 192 |
For more details, see the [revset evaluation](revset-evaluation.md) |
| 193 |
documentation. |
| 194 |
|
| 195 |
### StackedTable |
| 196 |
|
| 197 |
`StackedTable` (actually `ReadonlyTable` and `MutableTable`) is a simple disk |
| 198 |
format for storing key-value pairs sorted by key. The keys have to have the same |
| 199 |
size but the values can have different sizes. We use our own format because we |
| 200 |
want [lock-free concurrency](concurrency.md) and there doesn't seem to be an |
| 201 |
existing key-value store we could use. |
| 202 |
|
| 203 |
The file format contains a lookup table followed by concatenated values. The |
| 204 |
lookup table is a sorted list of keys, where each key is followed by the |
| 205 |
associated value's offset in the concatenated values. |
| 206 |
|
| 207 |
A table can have a parent table. When looking up a key, if it's not found in the |
| 208 |
current table, the parent table is searched. We never update a table in place. |
| 209 |
If the number of new entries to write is less than half the number of entries in |
| 210 |
the parent table, we create a new table with the new entries and a pointer to |
| 211 |
the parent. Otherwise, we copy the entries from the parent table and the new |
| 212 |
entries into a new table with the grandparent as the parent. We do that |
| 213 |
recursively so parent tables are at least 2 times as large as child tables. This |
| 214 |
results in O(log N) amortized insertion time and lookup time. |
| 215 |
|
| 216 |
There's no garbage collection of unreachable tables yet. |
| 217 |
|
| 218 |
The tables are named by their hash. We keep a separate directory of pointers to |
| 219 |
the current leaf tables, in the same way as we |
| 220 |
do [for the operation log](concurrency.md#storage). |
| 221 |
|
| 222 |
## Design of the CLI crate |
| 223 |
|
| 224 |
### Templates |
| 225 |
|
| 226 |
The concept is copied from Mercurial, but the syntax is different. The main |
| 227 |
difference is that the top-level expression is a template expression, not a |
| 228 |
string like in Mercurial. There is also no string interpolation (e.g. |
| 229 |
`"Commit ID: {node}"` in Mercurial). |
| 230 |
|
| 231 |
### Diff-editing |
| 232 |
|
| 233 |
Diff-editing works by creating two very sparse working copies, containing only |
| 234 |
the files we want the user to edit. We then let the user edit the right-hand |
| 235 |
side of the diff. Then we simply snapshot that working copy to create the new |
| 236 |
tree. |