| 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 |
mod edit; |
| 16 |
mod list; |
| 17 |
mod reset; |
| 18 |
mod set; |
| 19 |
|
| 20 |
use clap::Subcommand; |
| 21 |
use jj_lib::repo_path::RepoPathBuf; |
| 22 |
use tracing::instrument; |
| 23 |
|
| 24 |
use self::edit::SparseEditArgs; |
| 25 |
use self::edit::cmd_sparse_edit; |
| 26 |
use self::list::SparseListArgs; |
| 27 |
use self::list::cmd_sparse_list; |
| 28 |
use self::reset::SparseResetArgs; |
| 29 |
use self::reset::cmd_sparse_reset; |
| 30 |
use self::set::SparseSetArgs; |
| 31 |
use self::set::cmd_sparse_set; |
| 32 |
use crate::cli_util::CommandHelper; |
| 33 |
use crate::cli_util::WorkspaceCommandHelper; |
| 34 |
use crate::cli_util::print_checkout_stats; |
| 35 |
use crate::command_error::CommandError; |
| 36 |
use crate::command_error::internal_error_with_message; |
| 37 |
use crate::ui::Ui; |
| 38 |
|
| 39 |
/// Manage which paths from the working-copy commit are present in the working |
| 40 |
/// copy |
| 41 |
#[derive(Subcommand, Clone, Debug)] |
| 42 |
pub(crate) enum SparseCommand { |
| 43 |
Edit(SparseEditArgs), |
| 44 |
List(SparseListArgs), |
| 45 |
Reset(SparseResetArgs), |
| 46 |
Set(SparseSetArgs), |
| 47 |
} |
| 48 |
|
| 49 |
#[instrument(skip_all)] |
| 50 |
pub(crate) async fn cmd_sparse( |
| 51 |
ui: &mut Ui, |
| 52 |
command: &CommandHelper, |
| 53 |
subcommand: &SparseCommand, |
| 54 |
) -> Result<(), CommandError> { |
| 55 |
match subcommand { |
| 56 |
SparseCommand::Edit(args) => cmd_sparse_edit(ui, command, args).await, |
| 57 |
SparseCommand::List(args) => cmd_sparse_list(ui, command, args).await, |
| 58 |
SparseCommand::Reset(args) => cmd_sparse_reset(ui, command, args).await, |
| 59 |
SparseCommand::Set(args) => cmd_sparse_set(ui, command, args).await, |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
async fn update_sparse_patterns_with( |
| 64 |
ui: &mut Ui, |
| 65 |
workspace_command: &mut WorkspaceCommandHelper, |
| 66 |
f: impl FnOnce(&mut Ui, &[RepoPathBuf]) -> Result<Vec<RepoPathBuf>, CommandError>, |
| 67 |
) -> Result<(), CommandError> { |
| 68 |
let (mut locked_ws, wc_commit) = workspace_command.start_working_copy_mutation().await?; |
| 69 |
let new_patterns = f(ui, locked_ws.locked_wc().sparse_patterns()?)?; |
| 70 |
let stats = locked_ws |
| 71 |
.locked_wc() |
| 72 |
.set_sparse_patterns(new_patterns) |
| 73 |
.await |
| 74 |
.map_err(|err| internal_error_with_message("Failed to update working copy paths", err))?; |
| 75 |
let operation_id = locked_ws.locked_wc().old_operation_id().clone(); |
| 76 |
locked_ws.finish(operation_id).await?; |
| 77 |
print_checkout_stats(ui, &stats, &wc_commit)?; |
| 78 |
Ok(()) |
| 79 |
} |