| 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::op_store::RefTarget; |
| 18 |
|
| 19 |
use super::warn_unmatched_local_bookmarks; |
| 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 an existing bookmark and propagate the deletion to remotes on the |
| 27 |
/// next push |
| 28 |
/// |
| 29 |
/// Revisions referred to by the deleted bookmarks are not abandoned. To delete |
| 30 |
/// revisions as well as bookmarks, use `jj abandon`. For example, `jj abandon |
| 31 |
/// main..<bookmark>` will abandon revisions belonging to the `<bookmark>` |
| 32 |
/// branch (relative to the `main` branch.) |
| 33 |
/// |
| 34 |
/// If you don't want the deletion of the local bookmark to propagate to any |
| 35 |
/// tracked remote bookmarks, use `jj bookmark forget` instead. |
| 36 |
#[derive(clap::Args, Clone, Debug)] |
| 37 |
pub struct BookmarkDeleteArgs { |
| 38 |
/// The bookmarks to delete |
| 39 |
/// |
| 40 |
/// By default, the specified pattern matches bookmark names with glob |
| 41 |
/// syntax. You can also use other [string pattern syntax]. |
| 42 |
/// |
| 43 |
/// [string pattern syntax]: |
| 44 |
/// https://docs.jj-vcs.dev/latest/revsets/#string-patterns |
| 45 |
#[arg(required = true)] |
| 46 |
#[arg(add = ArgValueCandidates::new(complete::local_bookmarks))] |
| 47 |
names: Vec<String>, |
| 48 |
} |
| 49 |
|
| 50 |
pub async fn cmd_bookmark_delete( |
| 51 |
ui: &mut Ui, |
| 52 |
command: &CommandHelper, |
| 53 |
args: &BookmarkDeleteArgs, |
| 54 |
) -> Result<(), CommandError> { |
| 55 |
let mut workspace_command = command.workspace_helper(ui).await?; |
| 56 |
let repo = workspace_command.repo().clone(); |
| 57 |
let name_expr = parse_union_name_patterns(ui, &args.names)?; |
| 58 |
let name_matcher = name_expr.to_matcher(); |
| 59 |
let matched_bookmarks = repo |
| 60 |
.view() |
| 61 |
.local_bookmarks_matching(&name_matcher) |
| 62 |
.collect_vec(); |
| 63 |
warn_unmatched_local_bookmarks(ui, repo.view(), &name_expr)?; |
| 64 |
if matched_bookmarks.is_empty() { |
| 65 |
writeln!(ui.status(), "No bookmarks to delete.")?; |
| 66 |
return Ok(()); |
| 67 |
} |
| 68 |
|
| 69 |
let mut tx = workspace_command.start_transaction(); |
| 70 |
for (name, _) in &matched_bookmarks { |
| 71 |
tx.repo_mut() |
| 72 |
.set_local_bookmark_target(name, RefTarget::absent()); |
| 73 |
} |
| 74 |
writeln!( |
| 75 |
ui.status(), |
| 76 |
"Deleted {} bookmarks.", |
| 77 |
matched_bookmarks.len() |
| 78 |
)?; |
| 79 |
tx.finish( |
| 80 |
ui, |
| 81 |
format!( |
| 82 |
"delete bookmark {}", |
| 83 |
matched_bookmarks |
| 84 |
.iter() |
| 85 |
.map(|(name, _)| name.as_symbol()) |
| 86 |
.join(", ") |
| 87 |
), |
| 88 |
) |
| 89 |
.await?; |
| 90 |
Ok(()) |
| 91 |
} |