Debian Codegraph @

cli/src/commands/tag/set.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 clap_complete::ArgValueCompleter;
17 use itertools::Itertools as _;
18 use jj_lib::op_store::RefTarget;
19 use jj_lib::ref_name::RefNameBuf;
20
21 use crate::cli_util::CommandHelper;
22 use crate::cli_util::RevisionArg;
23 use crate::command_error::CommandError;
24 use crate::command_error::user_error;
25 use crate::complete;
26 use crate::revset_util;
27 use crate::ui::Ui;
28
29 /// Create or update tags
30 #[derive(clap::Args, Clone, Debug)]
31 pub struct TagSetArgs {
32 /// Target revision to point to
33 #[arg(
34 long,
35 short,
36 default_value = "@",
37 visible_alias = "to",
38 value_name = "REVSET"
39 )]
40 #[arg(add = ArgValueCompleter::new(complete::revset_expression_all))]
41 revision: RevisionArg,
42
43 /// Allow moving existing tags
44 #[arg(long)]
45 allow_move: bool,
46
47 /// Tag names to create or update
48 #[arg(required = true, value_parser = revset_util::parse_tag_name)]
49 #[arg(add = ArgValueCandidates::new(complete::local_tags))]
50 names: Vec<RefNameBuf>,
51 }
52
53 pub async fn cmd_tag_set(
54 ui: &mut Ui,
55 command: &CommandHelper,
56 args: &TagSetArgs,
57 ) -> Result<(), CommandError> {
58 let mut workspace_command = command.workspace_helper(ui).await?;
59 let target_commit = workspace_command
60 .resolve_single_rev(ui, &args.revision)
61 .await?;
62 let repo = workspace_command.repo().as_ref();
63
64 let mut new_count = 0;
65 let mut moved_count = 0;
66 for name in &args.names {
67 let old_target = repo.view().get_local_tag(name);
68 // TODO: If we add support for tracked remote tags, deleted tags should
69 // be considered present until they get pushed. See cmd_bookmark_set().
70 if old_target.is_present() && !args.allow_move {
71 return Err(user_error(format!(
72 "Refusing to move tag: {name}",
73 name = name.as_symbol()
74 ))
75 .hinted("Use --allow-move to update existing tags."));
76 }
77 if old_target.is_absent() {
78 new_count += 1;
79 } else if old_target.as_normal() != Some(target_commit.id()) {
80 moved_count += 1;
81 }
82 }
83 if target_commit.is_discardable(repo).await? {
84 writeln!(ui.warning_default(), "Target revision is empty.")?;
85 }
86
87 let mut tx = workspace_command.start_transaction();
88 for name in &args.names {
89 tx.repo_mut()
90 .set_local_tag_target(name, RefTarget::normal(target_commit.id().clone()));
91 }
92
93 if let Some(mut formatter) = ui.status_formatter() {
94 if new_count > 0 {
95 write!(formatter, "Created {new_count} tags pointing to ")?;
96 tx.write_commit_summary(formatter.as_mut(), &target_commit)?;
97 writeln!(formatter)?;
98 }
99 if moved_count > 0 {
100 write!(formatter, "Moved {moved_count} tags to ")?;
101 tx.write_commit_summary(formatter.as_mut(), &target_commit)?;
102 writeln!(formatter)?;
103 }
104 }
105
106 tx.finish(
107 ui,
108 format!(
109 "set tag {names} to commit {id}",
110 names = args.names.iter().map(|n| n.as_symbol()).join(", "),
111 id = target_commit.id()
112 ),
113 )
114 .await?;
115 Ok(())
116 }