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
15mod edit;
16mod list;
17mod reset;
18mod set;
19
20use clap::Subcommand;
21use jj_lib::repo_path::RepoPathBuf;
22use tracing::instrument;
23
24use self::edit::cmd_sparse_edit;
25use self::edit::SparseEditArgs;
26use self::list::cmd_sparse_list;
27use self::list::SparseListArgs;
28use self::reset::cmd_sparse_reset;
29use self::reset::SparseResetArgs;
30use self::set::cmd_sparse_set;
31use self::set::SparseSetArgs;
32use crate::cli_util::print_checkout_stats;
33use crate::cli_util::CommandHelper;
34use crate::cli_util::WorkspaceCommandHelper;
35use crate::command_error::internal_error_with_message;
36use crate::command_error::CommandError;
37use 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)]
42pub(crate) enum SparseCommand {
43 Edit(SparseEditArgs),
44 List(SparseListArgs),
45 Reset(SparseResetArgs),
46 Set(SparseSetArgs),
47}
48
49#[instrument(skip_all)]
50pub(crate) 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),
57 SparseCommand::List(args) => cmd_sparse_list(ui, command, args),
58 SparseCommand::Reset(args) => cmd_sparse_reset(ui, command, args),
59 SparseCommand::Set(args) => cmd_sparse_set(ui, command, args),
60 }
61}
62
63fn 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 checkout_options = workspace_command.checkout_options();
69 let (mut locked_ws, wc_commit) = workspace_command.start_working_copy_mutation()?;
70 let new_patterns = f(ui, locked_ws.locked_wc().sparse_patterns()?)?;
71 let stats = locked_ws
72 .locked_wc()
73 .set_sparse_patterns(new_patterns, &checkout_options)
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)?;
77 print_checkout_stats(ui, &stats, &wc_commit)?;
78 Ok(())
79}