just playing with tangled
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
15use std::collections::HashSet;
16
17use itertools::Itertools as _;
18use jj_lib::repo_path::RepoPathBuf;
19use tracing::instrument;
20
21use super::update_sparse_patterns_with;
22use crate::cli_util::CommandHelper;
23use crate::command_error::CommandError;
24use 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)]
32pub 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 /// Patterns to remove from the working copy
41 #[arg(
42 long,
43 conflicts_with = "clear",
44 value_hint = clap::ValueHint::AnyPath,
45 value_parser = |s: &str| RepoPathBuf::from_relative_path(s),
46 )]
47 remove: Vec<RepoPathBuf>,
48 /// Include no files in the working copy (combine with --add)
49 #[arg(long)]
50 clear: bool,
51}
52
53#[instrument(skip_all)]
54pub fn cmd_sparse_set(
55 ui: &mut Ui,
56 command: &CommandHelper,
57 args: &SparseSetArgs,
58) -> Result<(), CommandError> {
59 let mut workspace_command = command.workspace_helper(ui)?;
60 update_sparse_patterns_with(ui, &mut workspace_command, |_ui, old_patterns| {
61 let mut new_patterns = HashSet::new();
62 if !args.clear {
63 new_patterns.extend(old_patterns.iter().cloned());
64 for path in &args.remove {
65 new_patterns.remove(path);
66 }
67 }
68 for path in &args.add {
69 new_patterns.insert(path.to_owned());
70 }
71 Ok(new_patterns.into_iter().sorted_unstable().collect())
72 })
73}