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    /// The revisions to change.
43    #[arg(long, short, default_value = "@", value_name = "REVSETS")]
44    revisions: Vec<RevisionArg>,
45    /// A no-op option to match the interface of `git rebase -x`.
46    #[arg(short = 'x', hide = true)]
47    unused_command: bool,
48    /// How many processes should run in parallel, uses by default all cores.
49    #[arg(long, short)]
50    jobs: Option<usize>,
51}
52
53pub fn cmd_run(ui: &mut Ui, command: &CommandHelper, args: &RunArgs) -> Result<(), CommandError> {
54    let workspace_command = command.workspace_helper(ui)?;
55    let _resolved_commits: Vec<_> = workspace_command
56        .parse_union_revsets(ui, &args.revisions)?
57        .evaluate_to_commits()?
58        .try_collect()?;
59    // Jobs are resolved in this order:
60    // 1. Commandline argument iff > 0.
61    // 2. the amount of cores available.
62    // 3. a single job, if all of the above fails.
63    let _jobs = match args.jobs {
64        Some(0) | None => std::thread::available_parallelism().map(|t| t.into()).ok(),
65        Some(jobs) => Some(jobs),
66    }
67    // Fallback to a single user-visible job.
68    .unwrap_or(1usize);
69    Err(user_error("This is a stub, do not use"))
70}