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