| 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 |
mod advance; |
| 16 |
mod create; |
| 17 |
mod delete; |
| 18 |
mod forget; |
| 19 |
mod list; |
| 20 |
mod r#move; |
| 21 |
mod rename; |
| 22 |
mod set; |
| 23 |
mod track; |
| 24 |
mod untrack; |
| 25 |
|
| 26 |
use std::io; |
| 27 |
|
| 28 |
use itertools::Itertools as _; |
| 29 |
use jj_lib::backend::CommitId; |
| 30 |
use jj_lib::iter_util::fallible_any; |
| 31 |
use jj_lib::op_store::RefTarget; |
| 32 |
use jj_lib::op_store::RemoteRef; |
| 33 |
use jj_lib::ref_name::RefName; |
| 34 |
use jj_lib::ref_name::RemoteName; |
| 35 |
use jj_lib::ref_name::RemoteRefSymbol; |
| 36 |
use jj_lib::ref_name::RemoteRefSymbolBuf; |
| 37 |
use jj_lib::repo::Repo; |
| 38 |
use jj_lib::str_util::StringExpression; |
| 39 |
use jj_lib::str_util::StringMatcher; |
| 40 |
use jj_lib::view::View; |
| 41 |
|
| 42 |
use self::advance::BookmarkAdvanceArgs; |
| 43 |
use self::advance::cmd_bookmark_advance; |
| 44 |
use self::create::BookmarkCreateArgs; |
| 45 |
use self::create::cmd_bookmark_create; |
| 46 |
use self::delete::BookmarkDeleteArgs; |
| 47 |
use self::delete::cmd_bookmark_delete; |
| 48 |
use self::forget::BookmarkForgetArgs; |
| 49 |
use self::forget::cmd_bookmark_forget; |
| 50 |
use self::list::BookmarkListArgs; |
| 51 |
use self::list::cmd_bookmark_list; |
| 52 |
use self::r#move::BookmarkMoveArgs; |
| 53 |
use self::r#move::cmd_bookmark_move; |
| 54 |
use self::rename::BookmarkRenameArgs; |
| 55 |
use self::rename::cmd_bookmark_rename; |
| 56 |
use self::set::BookmarkSetArgs; |
| 57 |
use self::set::cmd_bookmark_set; |
| 58 |
use self::track::BookmarkTrackArgs; |
| 59 |
use self::track::cmd_bookmark_track; |
| 60 |
use self::untrack::BookmarkUntrackArgs; |
| 61 |
use self::untrack::cmd_bookmark_untrack; |
| 62 |
use crate::cli_util::CommandHelper; |
| 63 |
use crate::command_error::CommandError; |
| 64 |
use crate::ui::Ui; |
| 65 |
|
| 66 |
// Unlike most other aliases, `b` is defined in the config and can be overridden |
| 67 |
// by the user. |
| 68 |
|
| 69 |
/// Manage bookmarks [default alias: b] |
| 70 |
/// |
| 71 |
/// See [`jj help -k bookmarks`] for more information. |
| 72 |
/// |
| 73 |
/// [`jj help -k bookmarks`]: |
| 74 |
/// https://docs.jj-vcs.dev/latest/bookmarks |
| 75 |
#[derive(clap::Subcommand, Clone, Debug)] |
| 76 |
pub enum BookmarkCommand { |
| 77 |
#[command(visible_alias("a"))] |
| 78 |
Advance(BookmarkAdvanceArgs), |
| 79 |
#[command(visible_alias("c"))] |
| 80 |
Create(BookmarkCreateArgs), |
| 81 |
#[command(visible_alias("d"))] |
| 82 |
Delete(BookmarkDeleteArgs), |
| 83 |
#[command(visible_alias("f"))] |
| 84 |
Forget(BookmarkForgetArgs), |
| 85 |
#[command(visible_alias("l"))] |
| 86 |
List(BookmarkListArgs), |
| 87 |
#[command(visible_alias("m"))] |
| 88 |
Move(BookmarkMoveArgs), |
| 89 |
#[command(visible_alias("r"))] |
| 90 |
Rename(BookmarkRenameArgs), |
| 91 |
#[command(visible_alias("s"))] |
| 92 |
Set(BookmarkSetArgs), |
| 93 |
#[command(visible_alias("t"))] |
| 94 |
Track(BookmarkTrackArgs), |
| 95 |
Untrack(BookmarkUntrackArgs), |
| 96 |
} |
| 97 |
|
| 98 |
pub async fn cmd_bookmark( |
| 99 |
ui: &mut Ui, |
| 100 |
command: &CommandHelper, |
| 101 |
subcommand: &BookmarkCommand, |
| 102 |
) -> Result<(), CommandError> { |
| 103 |
match subcommand { |
| 104 |
BookmarkCommand::Advance(args) => cmd_bookmark_advance(ui, command, args).await, |
| 105 |
BookmarkCommand::Create(args) => cmd_bookmark_create(ui, command, args).await, |
| 106 |
BookmarkCommand::Delete(args) => cmd_bookmark_delete(ui, command, args).await, |
| 107 |
BookmarkCommand::Forget(args) => cmd_bookmark_forget(ui, command, args).await, |
| 108 |
BookmarkCommand::List(args) => cmd_bookmark_list(ui, command, args).await, |
| 109 |
BookmarkCommand::Move(args) => cmd_bookmark_move(ui, command, args).await, |
| 110 |
BookmarkCommand::Rename(args) => cmd_bookmark_rename(ui, command, args).await, |
| 111 |
BookmarkCommand::Set(args) => cmd_bookmark_set(ui, command, args).await, |
| 112 |
BookmarkCommand::Track(args) => cmd_bookmark_track(ui, command, args).await, |
| 113 |
BookmarkCommand::Untrack(args) => cmd_bookmark_untrack(ui, command, args).await, |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
fn resolve_trackable_remote_bookmarks<'a>( |
| 118 |
ui: &Ui, |
| 119 |
view: &'a View, |
| 120 |
symbols: &'a [RemoteRefSymbolBuf], |
| 121 |
) -> Result<Vec<(RemoteRefSymbol<'a>, &'a RemoteRef)>, CommandError> { |
| 122 |
let mut trackable_refs = vec![]; |
| 123 |
let mut unmatched_symbols = vec![]; |
| 124 |
for symbol in symbols { |
| 125 |
let symbol = symbol.as_ref(); |
| 126 |
let remote_ref = view.get_remote_bookmark(symbol); |
| 127 |
if remote_ref.is_present() |
| 128 |
|| view.get_local_bookmark(symbol.name).is_present() |
| 129 |
&& view.get_remote_view(symbol.remote).is_some() |
| 130 |
{ |
| 131 |
trackable_refs.push((symbol, remote_ref)); |
| 132 |
} else { |
| 133 |
unmatched_symbols.push(symbol); |
| 134 |
} |
| 135 |
} |
| 136 |
trackable_refs.sort_unstable_by_key(|(sym, _)| *sym); |
| 137 |
trackable_refs.dedup_by(|(sym1, _), (sym2, _)| sym1 == sym2); |
| 138 |
if !unmatched_symbols.is_empty() { |
| 139 |
writeln!( |
| 140 |
ui.warning_default(), |
| 141 |
"No matching remote bookmarks for names: {}", |
| 142 |
unmatched_symbols.iter().join(", ") |
| 143 |
)?; |
| 144 |
} |
| 145 |
Ok(trackable_refs) |
| 146 |
} |
| 147 |
|
| 148 |
fn trackable_remote_bookmarks_matching<'a>( |
| 149 |
view: &'a View, |
| 150 |
bookmark_matcher: &StringMatcher, |
| 151 |
remote_matcher: &StringMatcher, |
| 152 |
) -> impl Iterator<Item = (RemoteRefSymbol<'a>, &'a RemoteRef)> { |
| 153 |
let present_or_tracked_matches = |
| 154 |
view.remote_bookmarks_matching(bookmark_matcher, remote_matcher); |
| 155 |
let absent_matches = |
| 156 |
view.remote_views_matching(remote_matcher) |
| 157 |
.flat_map(move |(remote, remote_view)| { |
| 158 |
view.local_bookmarks_matching(bookmark_matcher) |
| 159 |
.filter(|&(name, _)| !remote_view.bookmarks.contains_key(name)) |
| 160 |
.map(|(name, _)| (name.to_remote_symbol(remote), RemoteRef::absent_ref())) |
| 161 |
}); |
| 162 |
itertools::chain(present_or_tracked_matches, absent_matches) |
| 163 |
} |
| 164 |
|
| 165 |
async fn is_fast_forward( |
| 166 |
repo: &dyn Repo, |
| 167 |
old_target: &RefTarget, |
| 168 |
new_target_id: &CommitId, |
| 169 |
) -> Result<bool, CommandError> { |
| 170 |
if old_target.is_present() { |
| 171 |
// Strictly speaking, "all" old targets should be ancestors, but we allow |
| 172 |
// conflict resolution by setting bookmark to "any" of the old target |
| 173 |
// descendants. |
| 174 |
let found = fallible_any(old_target.added_ids(), async |old| { |
| 175 |
repo.index().is_ancestor(old, new_target_id).await |
| 176 |
}) |
| 177 |
.await?; |
| 178 |
Ok(found) |
| 179 |
} else { |
| 180 |
Ok(true) |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
/// Warns about exact patterns that don't match local bookmarks. |
| 185 |
fn warn_unmatched_local_bookmarks( |
| 186 |
ui: &Ui, |
| 187 |
view: &View, |
| 188 |
name_expr: &StringExpression, |
| 189 |
) -> io::Result<()> { |
| 190 |
let mut names = name_expr |
| 191 |
.exact_strings() |
| 192 |
.map(RefName::new) |
| 193 |
.filter(|name| view.get_local_bookmark(name).is_absent()) |
| 194 |
.peekable(); |
| 195 |
if names.peek().is_none() { |
| 196 |
return Ok(()); |
| 197 |
} |
| 198 |
writeln!( |
| 199 |
ui.warning_default(), |
| 200 |
"No matching bookmarks for names: {}", |
| 201 |
names.map(|name| name.as_symbol()).join(", ") |
| 202 |
) |
| 203 |
} |
| 204 |
|
| 205 |
/// Warns about exact patterns that don't match local or remote bookmarks. |
| 206 |
fn warn_unmatched_local_or_remote_bookmarks( |
| 207 |
ui: &Ui, |
| 208 |
view: &View, |
| 209 |
name_expr: &StringExpression, |
| 210 |
) -> io::Result<()> { |
| 211 |
let mut names = name_expr |
| 212 |
.exact_strings() |
| 213 |
.map(RefName::new) |
| 214 |
.filter(|&name| { |
| 215 |
view.get_local_bookmark(name).is_absent() |
| 216 |
&& view |
| 217 |
.remote_views() |
| 218 |
.all(|(_, remote_view)| !remote_view.bookmarks.contains_key(name)) |
| 219 |
}) |
| 220 |
.peekable(); |
| 221 |
if names.peek().is_none() { |
| 222 |
return Ok(()); |
| 223 |
} |
| 224 |
writeln!( |
| 225 |
ui.warning_default(), |
| 226 |
"No matching bookmarks for names: {}", |
| 227 |
names.map(|name| name.as_symbol()).join(", ") |
| 228 |
) |
| 229 |
} |
| 230 |
|
| 231 |
/// Warns about exact patterns that don't match remotes. |
| 232 |
fn warn_unmatched_remotes(ui: &Ui, view: &View, name_expr: &StringExpression) -> io::Result<()> { |
| 233 |
let mut names = name_expr |
| 234 |
.exact_strings() |
| 235 |
.map(RemoteName::new) |
| 236 |
.filter(|name| view.get_remote_view(name).is_none()) |
| 237 |
.peekable(); |
| 238 |
if names.peek().is_none() { |
| 239 |
return Ok(()); |
| 240 |
} |
| 241 |
writeln!( |
| 242 |
ui.warning_default(), |
| 243 |
"No matching remotes for names: {}", |
| 244 |
names.map(|name| name.as_symbol()).join(", ") |
| 245 |
) |
| 246 |
} |