| 1 |
# Introducing JJ run |
| 2 |
|
| 3 |
Authors: [Philip Metzger](mailto:[email protected]), [Martin von Zweigberk](mailto:[email protected]), [Danny Hooper](mailto:[email protected]), [Waleed Khan](mailto:[email protected]) |
| 4 |
|
| 5 |
Initial Version, 10.12.2022 (view full history [here](https://docs.google.com/document/d/14BiAoEEy_e-BRPHYpXRFjvHMfgYVKh-pKWzzTDi-v-g/edit)) |
| 6 |
|
| 7 |
**Summary:** This Document documents the design of a new `run` command for |
| 8 |
Jujutsu which will be used to seamlessly integrate with build systems, linters |
| 9 |
and formatters. This is achieved by running a user-provided command or script |
| 10 |
across multiple revisions. For more details, read the |
| 11 |
[Use-Cases of jj run](#use-cases-of-jj-run). |
| 12 |
|
| 13 |
## Preface |
| 14 |
|
| 15 |
The goal of this Design Document is to specify the correct behavior of `jj run`. |
| 16 |
The points we decide on here I (Philip Metzger) will try to implement. There |
| 17 |
exists some prior work in other DVCS: |
| 18 |
* `git test`: part of [git-branchless]. Similar to this proposal for `jj run`. |
| 19 |
* `hg run`: Google's internal Mercurial extension. Similar to this proposal for |
| 20 |
`jj run`. |
| 21 |
Details not available. |
| 22 |
* `hg fix`: Google's open source Mercurial extension: [source code][fix-src]. A |
| 23 |
more specialized approach to rewriting file content without full context of the |
| 24 |
working directory. |
| 25 |
* `git rebase -x`: runs commands opportunistically as part of rebase. |
| 26 |
* `git bisect run`: run a command to determine which commit introduced a bug. |
| 27 |
|
| 28 |
## Context and Scope |
| 29 |
|
| 30 |
The initial need for some kind of command runner integrated in the VCS, surfaced |
| 31 |
in a [github discussion][pre-commit]. In a [discussion on discord][hooks] about |
| 32 |
the git-hook model, there was consensus about not repeating their mistakes. |
| 33 |
|
| 34 |
For `jj run` there is prior art in Mercurial, git branchless and Google's |
| 35 |
internal Mercurial. Currently git-branchless `git test` and `hg fix` implement |
| 36 |
some kind of command runner. The Google internal `hg run` works in conjunction |
| 37 |
with CitC (Clients in the Cloud) which allows it to lazily apply the current |
| 38 |
command to any affected file. Currently no open-source Jujutsu backend (Git, |
| 39 |
Simple) has a fancy virtual filesystem supporting it, so we can't apply this |
| 40 |
optimization. We could do the same once we have an implementation of the working |
| 41 |
copy based on a virtual file system. Until then, we have to run the commands in |
| 42 |
regular local-disk working copies. |
| 43 |
|
| 44 |
## Goals and Non-Goals |
| 45 |
|
| 46 |
### Goals |
| 47 |
|
| 48 |
* We should be able to apply the command to any revision, published or unpublished. |
| 49 |
* We should be able to parallelize running the actual command, while preserving a |
| 50 |
good console output. |
| 51 |
* The run command should be able to work in any commit, the working-copy commit |
| 52 |
itself or any other commit. |
| 53 |
* There should exist some way to signal hard failure. |
| 54 |
* The command should build enough infrastructure for `jj test`, `jj fix` and |
| 55 |
`jj format`. |
| 56 |
* The main goal is to be good enough, as we can always expand the functionality |
| 57 |
in the future. |
| 58 |
|
| 59 |
### Non-Goals |
| 60 |
|
| 61 |
* While we should build a base for `jj test`, `jj format` and `jj fix`, we |
| 62 |
shouldn't mash their use-cases into `jj run`. |
| 63 |
* The command shouldn't be too smart, as too many assumptions about workflows |
| 64 |
makes the command confusing for users. |
| 65 |
* The smart caching of outputs, as user input commands can be unpredictable. |
| 66 |
makes the command confusing for users. |
| 67 |
* Avoid the smart caching of outputs, as user input commands can be |
| 68 |
unpredictable. |
| 69 |
* Fine grained user facing configuration, as it's unwarranted complexity. |
| 70 |
* A `fix` subcommand as it cuts too much design space. |
| 71 |
|
| 72 |
## Use-Cases of jj run |
| 73 |
|
| 74 |
**Linting and Formatting:** |
| 75 |
|
| 76 |
- `jj run 'pre-commit run' -r $revset` |
| 77 |
- `jj run 'cargo clippy' -r $revset` |
| 78 |
- `jj run 'cargo +nightly fmt'` |
| 79 |
|
| 80 |
**Large scale changes across repositories, local and remote:** |
| 81 |
|
| 82 |
- `jj run 'sed /some/test/' -r 'mine() & ~remote_bookmarks("origin")'` |
| 83 |
- `jj run '$rewrite-tool' -r '$revset'` |
| 84 |
|
| 85 |
**Build systems:** |
| 86 |
|
| 87 |
- `jj run 'bazel build //some/target:somewhere'` |
| 88 |
- `jj run 'ninja check-lld'` |
| 89 |
|
| 90 |
Some of these use-cases should get a specialized command, as this allows |
| 91 |
further optimization. A command could be `jj format`, which runs a list of |
| 92 |
formatters over a subset of the files in a revision. Another command could be |
| 93 |
`jj fix`, which runs a command like `rustfmt --fix` or `cargo clippy --fix` over |
| 94 |
a subset of the files in a revision. |
| 95 |
|
| 96 |
## Design |
| 97 |
|
| 98 |
### Base Design |
| 99 |
|
| 100 |
All the work will be done in the `.jj/` directory. This allows us to hide all |
| 101 |
complexity from the users, while preserving the user's current workspace. |
| 102 |
|
| 103 |
We will copy the approach from git-branchless's `git test` of creating a |
| 104 |
temporary working copy for each parallel command. The working copies will be |
| 105 |
reused between `jj run` invocations. They will also be reused within `jj run` |
| 106 |
invocation if there are more commits to run on than there are parallel jobs. |
| 107 |
|
| 108 |
We will leave ignored files in the temporary directory between runs. That |
| 109 |
enables incremental builds (e.g. by letting cargo reuse its `target/` |
| 110 |
directory). However, it also means that runs potentially become less |
| 111 |
reproducible. We will provide a flag for removing ignored files from the |
| 112 |
temporary working copies to address that. |
| 113 |
|
| 114 |
Another problem with leaving ignored files in the temporary directories is that |
| 115 |
they take up space. That is especially problematic in the case of cargo (the |
| 116 |
`target/` directory often takes up tens of GBs). The same flag for cleaning up |
| 117 |
ignored files can be used to address that. We may want to also have a flag for |
| 118 |
cleaning up temporary working copies *after* running the command. |
| 119 |
|
| 120 |
An early version of the command will directly use [Treestate] to |
| 121 |
manage the temporary working copies. That means that running `jj` inside the |
| 122 |
temporary working copies will not work . We can later extend that to use a full |
| 123 |
[Workspace]. To prevent operations in the working copies from |
| 124 |
impacting the repo, we can use a separate [OpHeadsStore] for it. |
| 125 |
|
| 126 |
### Modifying the Working Copy |
| 127 |
|
| 128 |
Since the subprocesses will run in temporary working copies, they |
| 129 |
won't interfere with the user's working copy. The user can therefore continue |
| 130 |
to work in it while `jj run` is running. |
| 131 |
|
| 132 |
We want subprocesses to be able to make changes to the repo by updating their |
| 133 |
assigned working copy. Let's say the user runs `jj run` on just commits A and |
| 134 |
B, where B's parent is A. Any changes made on top of A would be squashed into |
| 135 |
A, forming A'. Similarly B' would be formed by squasing it into B. We can then |
| 136 |
either do a normal rebase of B' onto A', or we can simply update its parent to |
| 137 |
A'. The former is useful, e.g. when the subprocess only makes a partial update |
| 138 |
of the tree based on the parent commit. In addition to these two modes, we may |
| 139 |
want to have an option to ignore any changes made in the subprocess's working |
| 140 |
copy. |
| 141 |
|
| 142 |
### Modifying the Repo |
| 143 |
|
| 144 |
Once we give the subprocess access to a fork of the repo via separate |
| 145 |
[OpHeadsStore], it will be able to create new operations in its fork. |
| 146 |
If the user runs `jj run -r foo` and the subprocess checks out another commit, |
| 147 |
it's not clear what that should do. We should probably just verify that the |
| 148 |
working-copy commit's parents are unchanged after the subprocess returns. Any |
| 149 |
operations created by the subprocess will be ignored. |
| 150 |
|
| 151 |
### Rewriting the revisions |
| 152 |
|
| 153 |
Like all commands, `jj run` will refuse to rewrite public/immutable commits. |
| 154 |
For private/unpublished revisions, we either amend or reparent the changes, |
| 155 |
which are available as command options. |
| 156 |
|
| 157 |
## Execution order/parallelism |
| 158 |
|
| 159 |
It may be useful to execute commands in topological order. For example, |
| 160 |
commands with costs proportional to incremental changes, like build systems. |
| 161 |
There may also be other relevant heuristics, but topological order is an easy |
| 162 |
and effective way to start. |
| 163 |
|
| 164 |
Parallel execution of commands on different commits may choose to schedule |
| 165 |
commits to still reduce incremental changes in the working copy used by each |
| 166 |
execution slot/"thread". However, running the command on all commits |
| 167 |
concurrently should be possible if desired. |
| 168 |
|
| 169 |
Executing commands in topological order allows for more meaningful use of any |
| 170 |
potential features that stop execution "at the first failure". For example, |
| 171 |
when running tests on a chain of commits, it might be useful to proceed in |
| 172 |
topological/chronological order, and stop on the first failure, because it |
| 173 |
might imply that the remaining executions will be undesirable because they will |
| 174 |
also fail. |
| 175 |
|
| 176 |
## Dealing with failure |
| 177 |
|
| 178 |
It will be useful to have multiple strategies to deal with failures on a single |
| 179 |
or multiple revisions. The reason for these strategies is to allow customized |
| 180 |
conflict handling. These strategies then can be exposed in the ui with a |
| 181 |
matching option. |
| 182 |
|
| 183 |
**Continue:** If any subprocess fails, we will continue the work on child |
| 184 |
revisions. Notify the user on exit about the failed revisions. |
| 185 |
|
| 186 |
**Stop:** Signal a fatal failure and cancel any scheduled work that has not |
| 187 |
yet started running, but let any already started subprocess finish. Notify the |
| 188 |
user about the failed command and display the generated error from the |
| 189 |
subprocess. |
| 190 |
|
| 191 |
**Fatal:** Signal a fatal failure and immediately stop processing and kill any |
| 192 |
running processes. Notify the user that we failed to apply the command to the |
| 193 |
specific revision. |
| 194 |
|
| 195 |
We will leave any affected commit in its current state, if any subprocess fails. |
| 196 |
This allows us to provide a better user experience, as leaving revisions in an |
| 197 |
undesirable state, e.g partially formatted, may confuse users. |
| 198 |
|
| 199 |
## Resource constraints |
| 200 |
|
| 201 |
It will be useful to constrain the execution to prevent resource exhaustion. |
| 202 |
Relevant resources could include: |
| 203 |
- CPU and memory available on the machine running the commands. `jj run` can |
| 204 |
provide some simple mitigations like limiting parallelism to "number of CPUs" |
| 205 |
by default, and limiting parallelism by dividing "available memory" by some |
| 206 |
estimate or measurement of per-invocation memory use of the commands. |
| 207 |
- External resources that are not immediately known to jj. For example, |
| 208 |
commands run in parallel may wish to limit the total number of connections |
| 209 |
to a server. We might choose to defer any handling of this to the |
| 210 |
implementation of the command being invoked, instead of trying to |
| 211 |
communicate that information to jj. |
| 212 |
|
| 213 |
## Command Options |
| 214 |
|
| 215 |
The base command of any jj command should be usable. By default `jj run` works |
| 216 |
on `@`, the current working copy. |
| 217 |
- --command, explicit name of the first argument |
| 218 |
- -x, for git compatibility (may alias another command) |
| 219 |
- -j, --jobs, the amount of parallelism to use |
| 220 |
- -k, --keep-going, continue on failure (may alias another command) |
| 221 |
- --show, display the diff for an affected revision |
| 222 |
- --dry-run, do the command execution without doing any work, logging all |
| 223 |
intended files and arguments |
| 224 |
- --rebase, rebase all parents on the consulitng diff (may alias another |
| 225 |
command) |
| 226 |
- --reparent, change the parent of an effected revision to the new change |
| 227 |
(may alias another command) |
| 228 |
- --clean, remove existing workspaces and remove the ignored files |
| 229 |
- --readonly, ignore changes across multiple run invocations |
| 230 |
- --error-strategy=`continue|stop|fatal`, see [Dealing with failure](#dealing-with-failure) |
| 231 |
|
| 232 |
### Integrating with other commands |
| 233 |
|
| 234 |
- `jj log`: No special handling needed |
| 235 |
- `jj diff`: No special handling needed |
| 236 |
- `jj st`: For now reprint the final output of `jj run` |
| 237 |
- `jj op log`: No special handling needed, but awaits further discussion in |
| 238 |
[#963][issue] |
| 239 |
- `jj undo/jj op revert`: No special handling needed |
| 240 |
|
| 241 |
## Open Points |
| 242 |
|
| 243 |
- Should the command be working copy backend specific? |
| 244 |
- How do we manage the Processes which the command will spawn? |
| 245 |
- Configuration options, User and Repository Wide? |
| 246 |
|
| 247 |
## Future possibilities |
| 248 |
|
| 249 |
- We could rewrite the file in memory, which is a neat optimization |
| 250 |
- Exposing some internal state, to allow preciser resource constraints |
| 251 |
- Integration options for virtual filesystems, which allow them to cache the |
| 252 |
needed working copies. |
| 253 |
- A Jujutsu wide concept for a cached working copy, as they could be expensive |
| 254 |
to materialize. |
| 255 |
- Customized failure messages, this may be useful for bots, it could be similar |
| 256 |
to Bazel's `select(..., message = "arch not supported for $project")`. |
| 257 |
- Make `jj run` asynchronous by spawning a `main` process, directly return to the |
| 258 |
user and incrementally updating the output of `jj st`. |
| 259 |
|
| 260 |
[git-branchless]: https://github.com/arxanas/git-branchless |
| 261 |
[issue]: https://github.com/jj-vcs/jj/issues/963 |
| 262 |
[fix-src]: https://repo.mercurial-scm.org/hg/file/tip/hgext/fix.py |
| 263 |
[hooks]: https://discord.com/channels/968932220549103686/969829516539228222/1047958933161119795 |
| 264 |
[OpHeadsStore]: https://github.com/jj-vcs/jj/blob/main/lib/src/op_heads_store.rs |
| 265 |
[pre-commit]: https://github.com/jj-vcs/jj/issues/405 |
| 266 |
[Treestate]: https://github.com/jj-vcs/jj/blob/af85f552b676d66ed0e9ae0d401cd0c4ffbbeb21/lib/src/working_copy.rs#L117 |
| 267 |
[Workspace]: https://github.com/jj-vcs/jj/blob/af85f552b676d66ed0e9ae0d401cd0c4ffbbeb21/lib/src/workspace.rs#L54 |