| 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 |
use std::collections::HashSet; |
| 16 |
|
| 17 |
use itertools::Itertools as _; |
| 18 |
use jj_lib::repo_path::RepoPathBuf; |
| 19 |
use tracing::instrument; |
| 20 |
|
| 21 |
use super::update_sparse_patterns_with; |
| 22 |
use crate::cli_util::CommandHelper; |
| 23 |
use crate::command_error::CommandError; |
| 24 |
use crate::ui::Ui; |
| 25 |
|
| 26 |
/// Update the patterns that are present in the working copy |
| 27 |
/// |
| 28 |
/// For example, if all you need is the `README.md` and the `lib/` |
| 29 |
/// directory, use `jj sparse set --clear --add README.md --add lib`. |
| 30 |
/// If you no longer need the `lib` directory, use `jj sparse set --remove lib`. |
| 31 |
#[derive(clap::Args, Clone, Debug)] |
| 32 |
pub struct SparseSetArgs { |
| 33 |
/// Patterns to add to the working copy |
| 34 |
#[arg( |
| 35 |
long, |
| 36 |
value_hint = clap::ValueHint::AnyPath, |
| 37 |
value_parser = |s: &str| RepoPathBuf::from_relative_path(s), |
| 38 |
)] |
| 39 |
add: Vec<RepoPathBuf>, |
| 40 |
|
| 41 |
/// Patterns to remove from the working copy |
| 42 |
#[arg( |
| 43 |
long, |
| 44 |
conflicts_with = "clear", |
| 45 |
value_hint = clap::ValueHint::AnyPath, |
| 46 |
value_parser = |s: &str| RepoPathBuf::from_relative_path(s), |
| 47 |
)] |
| 48 |
remove: Vec<RepoPathBuf>, |
| 49 |
|
| 50 |
/// Include no files in the working copy (combine with --add) |
| 51 |
#[arg(long)] |
| 52 |
clear: bool, |
| 53 |
} |
| 54 |
|
| 55 |
#[instrument(skip_all)] |
| 56 |
pub async fn cmd_sparse_set( |
| 57 |
ui: &mut Ui, |
| 58 |
command: &CommandHelper, |
| 59 |
args: &SparseSetArgs, |
| 60 |
) -> Result<(), CommandError> { |
| 61 |
let mut workspace_command = command.workspace_helper(ui).await?; |
| 62 |
update_sparse_patterns_with(ui, &mut workspace_command, |_ui, old_patterns| { |
| 63 |
let mut new_patterns = HashSet::new(); |
| 64 |
if !args.clear { |
| 65 |
new_patterns.extend(old_patterns.iter().cloned()); |
| 66 |
for path in &args.remove { |
| 67 |
new_patterns.remove(path); |
| 68 |
} |
| 69 |
} |
| 70 |
for path in &args.add { |
| 71 |
new_patterns.insert(path.to_owned()); |
| 72 |
} |
| 73 |
Ok(new_patterns.into_iter().sorted_unstable().collect()) |
| 74 |
}) |
| 75 |
.await |
| 76 |
} |