Debian Codegraph @

cli/src/commands/bookmark/untrack.rs

raw
1 // Copyright 2020-2023 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::repo::Repo as _;
18 use jj_lib::str_util::StringExpression;
19
20 use super::resolve_trackable_remote_bookmarks;
21 use super::trackable_remote_bookmarks_matching;
22 use super::warn_unmatched_local_or_remote_bookmarks;
23 use super::warn_unmatched_remotes;
24 use crate::cli_util::CommandHelper;
25 use crate::cli_util::default_ignored_remote_name;
26 use crate::command_error::CommandError;
27 use crate::command_error::cli_error;
28 use crate::complete;
29 use crate::revset_util::parse_name_patterns_or_remote_symbols;
30 use crate::revset_util::parse_union_name_patterns;
31 use crate::ui::Ui;
32
33 /// Stop tracking given remote bookmarks
34 ///
35 /// An untracked remote bookmark is just a pointer to the last-fetched remote
36 /// bookmark. It won't be imported as a local bookmark on future pulls.
37 ///
38 /// If you want to forget a local bookmark while also untracking the
39 /// corresponding remote bookmarks, use `jj bookmark forget` instead.
40 #[derive(clap::Args, Clone, Debug)]
41 pub struct BookmarkUntrackArgs {
42 /// Bookmark name patterns or remote bookmark symbols to untrack
43 ///
44 /// `BOOKMARK` matches bookmark names using glob syntax by default. You can
45 /// also use other [string pattern syntax].
46 ///
47 /// `BOOKMARK@REMOTE` resolves to a remote bookmark exactly.
48 ///
49 /// [string pattern syntax]:
50 /// https://docs.jj-vcs.dev/latest/revsets/#string-patterns
51 #[arg(required = true, value_name = "BOOKMARK[@REMOTE]")]
52 #[arg(add = ArgValueCandidates::new(complete::tracked_bookmarks))]
53 names: Vec<String>,
54
55 /// Remote names to untrack
56 ///
57 /// By default, the specified pattern matches remote names with glob syntax.
58 /// You can also use other [string pattern syntax].
59 ///
60 /// If no remote names are given, all remote bookmarks matching the bookmark
61 /// names will be untracked.
62 ///
63 /// [string pattern syntax]:
64 /// https://docs.jj-vcs.dev/latest/revsets/#string-patterns
65 #[arg(long = "remote", value_name = "REMOTE")]
66 // TODO: Make this skip untracked remotes
67 #[arg(add = ArgValueCandidates::new(complete::git_remotes))]
68 remotes: Option<Vec<String>>,
69 }
70
71 pub async fn cmd_bookmark_untrack(
72 ui: &mut Ui,
73 command: &CommandHelper,
74 args: &BookmarkUntrackArgs,
75 ) -> Result<(), CommandError> {
76 let mut workspace_command = command.workspace_helper(ui).await?;
77 let repo = workspace_command.repo().clone();
78 let view = repo.view();
79 let ignored_remote = default_ignored_remote_name(repo.store())
80 // suppress unmatched remotes warning for default-ignored remote
81 .filter(|name| view.get_remote_view(name).is_some());
82
83 let (bookmark_exprs, remote_symbols) = parse_name_patterns_or_remote_symbols(ui, &args.names)?;
84 // Reject mixed syntax. It is confusing if the default @<remote> or
85 // user-specified --remote flag applies only to <bookmark> patterns.
86 if !bookmark_exprs.is_empty() && !remote_symbols.is_empty() {
87 return Err(cli_error(
88 "Cannot specify both <bookmark> patterns and <bookmark>@<remote> symbols",
89 ));
90 } else if args.remotes.is_some() && !remote_symbols.is_empty() {
91 return Err(cli_error(
92 "--remote cannot be used with <bookmark>@<remote> symbols",
93 ));
94 }
95 let matched_refs = if !remote_symbols.is_empty() {
96 resolve_trackable_remote_bookmarks(ui, view, &remote_symbols)?
97 } else {
98 let bookmark_expr = StringExpression::union_all(bookmark_exprs);
99 let remote_expr = match (&args.remotes, ignored_remote) {
100 (Some(text), _) => parse_union_name_patterns(ui, text)?,
101 (None, Some(ignored)) => StringExpression::exact(ignored).negated(),
102 (None, None) => StringExpression::all(),
103 };
104 let bookmark_matcher = bookmark_expr.to_matcher();
105 let remote_matcher = remote_expr.to_matcher();
106 let matched_refs =
107 trackable_remote_bookmarks_matching(view, &bookmark_matcher, &remote_matcher).collect();
108 warn_unmatched_local_or_remote_bookmarks(ui, view, &bookmark_expr)?;
109 warn_unmatched_remotes(ui, view, &remote_expr)?;
110 matched_refs
111 };
112
113 let mut symbols = Vec::new();
114 for (symbol, remote_ref) in matched_refs {
115 if ignored_remote.is_some_and(|ignored| symbol.remote == ignored) {
116 // This restriction can be lifted if we want to support untracked @git
117 // bookmarks.
118 writeln!(
119 ui.warning_default(),
120 "Git-tracking bookmark cannot be untracked: {symbol}"
121 )?;
122 } else if !remote_ref.is_tracked() {
123 writeln!(
124 ui.warning_default(),
125 "Remote bookmark not tracked yet: {symbol}"
126 )?;
127 } else {
128 symbols.push(symbol);
129 }
130 }
131 let mut tx = workspace_command.start_transaction();
132 for &symbol in &symbols {
133 tx.repo_mut().untrack_remote_bookmark(symbol);
134 }
135 if !symbols.is_empty() {
136 writeln!(
137 ui.status(),
138 "Stopped tracking {} remote bookmarks.",
139 symbols.len()
140 )?;
141 }
142 tx.finish(
143 ui,
144 format!("untrack remote bookmark {}", symbols.iter().join(", ")),
145 )
146 .await?;
147 Ok(())
148 }