use std::collections::HashSet;
use itertools::Itertools as _;
use jj_lib::repo_path::RepoPathBuf;
use tracing::instrument;
use super::update_sparse_patterns_with;
use crate::cli_util::CommandHelper;
use crate::command_error::CommandError;
use crate::ui::Ui;
#[derive(clap::Args, Clone, Debug)]
pub struct SparseSetArgs {
#[arg(
long,
value_hint = clap::ValueHint::AnyPath,
value_parser = |s: &str| RepoPathBuf::from_relative_path(s),
)]
add: Vec<RepoPathBuf>,
#[arg(
long,
conflicts_with = "clear",
value_hint = clap::ValueHint::AnyPath,
value_parser = |s: &str| RepoPathBuf::from_relative_path(s),
)]
remove: Vec<RepoPathBuf>,
#[arg(long)]
clear: bool,
}
#[instrument(skip_all)]
pub async fn cmd_sparse_set(
ui: &mut Ui,
command: &CommandHelper,
args: &SparseSetArgs,
) -> Result<(), CommandError> {
let mut workspace_command = command.workspace_helper(ui)?;
update_sparse_patterns_with(ui, &mut workspace_command, |_ui, old_patterns| {
let mut new_patterns = HashSet::new();
if !args.clear {
new_patterns.extend(old_patterns.iter().cloned());
for path in &args.remove {
new_patterns.remove(path);
}
}
for path in &args.add {
new_patterns.insert(path.to_owned());
}
Ok(new_patterns.into_iter().sorted_unstable().collect())
})
.await
}