Debian Codegraph @

cli/src/commands/tag/delete.rs

raw
1 // Copyright 2025 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 clap_complete::ArgValueCandidates;
16 use itertools::Itertools as _;
17 use jj_lib::op_store::RefTarget;
18
19 use super::warn_unmatched_local_tags;
20 use crate::cli_util::CommandHelper;
21 use crate::command_error::CommandError;
22 use crate::complete;
23 use crate::revset_util::parse_union_name_patterns;
24 use crate::ui::Ui;
25
26 /// Delete existing tags
27 ///
28 /// Revisions referred to by the deleted tags are not abandoned.
29 #[derive(clap::Args, Clone, Debug)]
30 pub struct TagDeleteArgs {
31 /// Tag names to delete
32 ///
33 /// By default, the specified pattern matches tag names with glob syntax.
34 /// You can also use other [string pattern syntax].
35 ///
36 /// [string pattern syntax]:
37 /// https://docs.jj-vcs.dev/latest/revsets/#string-patterns
38 #[arg(required = true)]
39 #[arg(add = ArgValueCandidates::new(complete::local_tags))]
40 names: Vec<String>,
41 }
42
43 pub async fn cmd_tag_delete(
44 ui: &mut Ui,
45 command: &CommandHelper,
46 args: &TagDeleteArgs,
47 ) -> Result<(), CommandError> {
48 let mut workspace_command = command.workspace_helper(ui).await?;
49 let repo = workspace_command.repo().clone();
50 let name_expr = parse_union_name_patterns(ui, &args.names)?;
51 let name_matcher = name_expr.to_matcher();
52 let matched_tags = repo.view().local_tags_matching(&name_matcher).collect_vec();
53 warn_unmatched_local_tags(ui, repo.view(), &name_expr)?;
54 if matched_tags.is_empty() {
55 writeln!(ui.status(), "No tags to delete.")?;
56 return Ok(());
57 }
58
59 let mut tx = workspace_command.start_transaction();
60 for (name, _) in &matched_tags {
61 tx.repo_mut()
62 .set_local_tag_target(name, RefTarget::absent());
63 }
64 writeln!(ui.status(), "Deleted {} tags.", matched_tags.len())?;
65 tx.finish(
66 ui,
67 format!(
68 "delete tag {names}",
69 names = matched_tags.iter().map(|(n, _)| n.as_symbol()).join(", ")
70 ),
71 )
72 .await?;
73 Ok(())
74 }