Skip to main content

jj_cli/commands/
run.rs

1// Copyright 2020 The Jujutsu Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! This file contains the internal implementation of `run`.
16
17use itertools::Itertools as _;
18
19use crate::cli_util::CommandHelper;
20use crate::cli_util::RevisionArg;
21use crate::command_error::CommandError;
22use crate::command_error::user_error;
23use crate::ui::Ui;
24
25/// (**Stub**, does not work yet) Run a command across a set of revisions.
26///
27///
28/// All recorded state will be persisted in the `.jj` directory, so occasionally
29/// a `jj run --clean` is needed to clean up disk space.
30///
31/// # Example
32///
33/// # Run pre-commit on your local work
34/// $ jj run 'pre-commit run .github/pre-commit.yaml' -r (trunk()..@) -j 4
35///
36/// This allows pre-commit integration and other funny stuff.
37#[derive(clap::Args, Clone, Debug)]
38#[command(verbatim_doc_comment)]
39pub struct RunArgs {
40    /// The command to run across all selected revisions.
41    shell_command: String,
42
43    /// The revisions to change.
44    #[arg(long, short, default_value = "@", value_name = "REVSETS")]
45    revisions: Vec<RevisionArg>,
46
47    /// A no-op option to match the interface of `git rebase -x`.
48    #[arg(short = 'x', hide = true)]
49    unused_command: bool,
50
51    /// How many processes should run in parallel, uses by default all cores.
52    #[arg(long, short)]
53    jobs: Option<usize>,
54}
55
56pub async fn cmd_run(
57    ui: &mut Ui,
58    command: &CommandHelper,
59    args: &RunArgs,
60) -> Result<(), CommandError> {
61    let workspace_command = command.workspace_helper(ui)?;
62    let _resolved_commits: Vec<_> = workspace_command
63        .parse_union_revsets(ui, &args.revisions)?
64        .evaluate_to_commits()?
65        .try_collect()?;
66    // Jobs are resolved in this order:
67    // 1. Commandline argument iff > 0.
68    // 2. the amount of cores available.
69    // 3. a single job, if all of the above fails.
70    let _jobs = match args.jobs {
71        Some(0) | None => std::thread::available_parallelism().map(|t| t.into()).ok(),
72        Some(jobs) => Some(jobs),
73    }
74    // Fallback to a single user-visible job.
75    .unwrap_or(1usize);
76    Err(user_error("This is a stub, do not use"))
77}