Debian Codegraph @

cli/src/commands/tag/mod.rs

raw
1 // Copyright 2020-2024 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 mod delete;
16 mod list;
17 mod set;
18 mod track;
19 mod untrack;
20
21 use std::io;
22
23 use itertools::Itertools as _;
24 use jj_lib::op_store::RemoteRef;
25 use jj_lib::ref_name::RefName;
26 use jj_lib::ref_name::RemoteName;
27 use jj_lib::ref_name::RemoteRefSymbol;
28 use jj_lib::ref_name::RemoteRefSymbolBuf;
29 use jj_lib::str_util::StringExpression;
30 use jj_lib::str_util::StringMatcher;
31 use jj_lib::view::View;
32
33 use self::delete::TagDeleteArgs;
34 use self::delete::cmd_tag_delete;
35 use self::list::TagListArgs;
36 use self::list::cmd_tag_list;
37 use self::set::TagSetArgs;
38 use self::set::cmd_tag_set;
39 use self::track::TagTrackArgs;
40 use self::track::cmd_tag_track;
41 use self::untrack::TagUntrackArgs;
42 use self::untrack::cmd_tag_untrack;
43 use crate::cli_util::CommandHelper;
44 use crate::command_error::CommandError;
45 use crate::ui::Ui;
46
47 /// Manage tags.
48 #[derive(clap::Subcommand, Clone, Debug)]
49 pub enum TagCommand {
50 #[command(visible_alias("d"))]
51 Delete(TagDeleteArgs),
52 #[command(visible_alias("l"))]
53 List(TagListArgs),
54 #[command(visible_alias("s"))]
55 Set(TagSetArgs),
56 Track(TagTrackArgs),
57 Untrack(TagUntrackArgs),
58 }
59
60 pub async fn cmd_tag(
61 ui: &mut Ui,
62 command: &CommandHelper,
63 subcommand: &TagCommand,
64 ) -> Result<(), CommandError> {
65 match subcommand {
66 TagCommand::Delete(args) => cmd_tag_delete(ui, command, args).await,
67 TagCommand::List(args) => cmd_tag_list(ui, command, args).await,
68 TagCommand::Set(args) => cmd_tag_set(ui, command, args).await,
69 TagCommand::Track(args) => cmd_tag_track(ui, command, args).await,
70 TagCommand::Untrack(args) => cmd_tag_untrack(ui, command, args).await,
71 }
72 }
73
74 fn resolve_trackable_remote_tags<'a>(
75 ui: &Ui,
76 view: &'a View,
77 symbols: &'a [RemoteRefSymbolBuf],
78 ) -> Result<Vec<(RemoteRefSymbol<'a>, &'a RemoteRef)>, CommandError> {
79 let mut trackable_refs = vec![];
80 let mut unmatched_symbols = vec![];
81 for symbol in symbols {
82 let symbol = symbol.as_ref();
83 let remote_ref = view.get_remote_tag(symbol);
84 if remote_ref.is_present()
85 || view.get_local_tag(symbol.name).is_present()
86 && view.get_remote_view(symbol.remote).is_some()
87 {
88 trackable_refs.push((symbol, remote_ref));
89 } else {
90 unmatched_symbols.push(symbol);
91 }
92 }
93 trackable_refs.sort_unstable_by_key(|(sym, _)| *sym);
94 trackable_refs.dedup_by(|(sym1, _), (sym2, _)| sym1 == sym2);
95 if !unmatched_symbols.is_empty() {
96 writeln!(
97 ui.warning_default(),
98 "No matching remote tags for names: {}",
99 unmatched_symbols.iter().join(", ")
100 )?;
101 }
102 Ok(trackable_refs)
103 }
104
105 fn trackable_remote_tags_matching<'a>(
106 view: &'a View,
107 tag_matcher: &StringMatcher,
108 remote_matcher: &StringMatcher,
109 ) -> impl Iterator<Item = (RemoteRefSymbol<'a>, &'a RemoteRef)> {
110 let present_or_tracked_matches = view.remote_tags_matching(tag_matcher, remote_matcher);
111 let absent_matches =
112 view.remote_views_matching(remote_matcher)
113 .flat_map(move |(remote, remote_view)| {
114 view.local_tags_matching(tag_matcher)
115 .filter(|&(name, _)| !remote_view.tags.contains_key(name))
116 .map(|(name, _)| (name.to_remote_symbol(remote), RemoteRef::absent_ref()))
117 });
118 itertools::chain(present_or_tracked_matches, absent_matches)
119 }
120
121 /// Warns about exact patterns that don't match local tags.
122 fn warn_unmatched_local_tags(ui: &Ui, view: &View, name_expr: &StringExpression) -> io::Result<()> {
123 let mut names = name_expr
124 .exact_strings()
125 .map(RefName::new)
126 .filter(|name| view.get_local_tag(name).is_absent())
127 .peekable();
128 if names.peek().is_none() {
129 return Ok(());
130 }
131 writeln!(
132 ui.warning_default(),
133 "No matching tags for names: {}",
134 names.map(|name| name.as_symbol()).join(", ")
135 )
136 }
137
138 /// Warns about exact patterns that don't match local or remote tags.
139 fn warn_unmatched_local_or_remote_tags(
140 ui: &Ui,
141 view: &View,
142 name_expr: &StringExpression,
143 ) -> io::Result<()> {
144 let mut names = name_expr
145 .exact_strings()
146 .map(RefName::new)
147 .filter(|&name| {
148 view.get_local_tag(name).is_absent()
149 && view
150 .remote_views()
151 .all(|(_, remote_view)| !remote_view.tags.contains_key(name))
152 })
153 .peekable();
154 if names.peek().is_none() {
155 return Ok(());
156 }
157 writeln!(
158 ui.warning_default(),
159 "No matching tags for names: {}",
160 names.map(|name| name.as_symbol()).join(", ")
161 )
162 }
163
164 /// Warns about exact patterns that don't match remotes.
165 fn warn_unmatched_remotes(ui: &Ui, view: &View, name_expr: &StringExpression) -> io::Result<()> {
166 let mut names = name_expr
167 .exact_strings()
168 .map(RemoteName::new)
169 .filter(|name| view.get_remote_view(name).is_none())
170 .peekable();
171 if names.peek().is_none() {
172 return Ok(());
173 }
174 writeln!(
175 ui.warning_default(),
176 "No matching remotes for names: {}",
177 names.map(|name| name.as_symbol()).join(", ")
178 )
179 }