| 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::LocalRemoteRefTarget; |
| 18 |
use jj_lib::op_store::RefTarget; |
| 19 |
use jj_lib::op_store::RemoteRef; |
| 20 |
use jj_lib::ref_name::RefName; |
| 21 |
use jj_lib::repo::Repo as _; |
| 22 |
use jj_lib::view::View; |
| 23 |
|
| 24 |
use super::warn_unmatched_local_or_remote_bookmarks; |
| 25 |
use crate::cli_util::CommandHelper; |
| 26 |
use crate::cli_util::default_ignored_remote_name; |
| 27 |
use crate::command_error::CommandError; |
| 28 |
use crate::complete; |
| 29 |
use crate::revset_util::parse_union_name_patterns; |
| 30 |
use crate::ui::Ui; |
| 31 |
|
| 32 |
/// Forget a bookmark without marking it as a deletion to be pushed |
| 33 |
/// |
| 34 |
/// If a local bookmark is forgotten, any corresponding remote bookmarks will |
| 35 |
/// become untracked to ensure that the forgotten bookmark will not impact |
| 36 |
/// remotes on future pushes. |
| 37 |
#[derive(clap::Args, Clone, Debug)] |
| 38 |
pub struct BookmarkForgetArgs { |
| 39 |
/// When forgetting a local bookmark, also forget any corresponding remote |
| 40 |
/// bookmarks |
| 41 |
/// |
| 42 |
/// A forgotten remote bookmark will not impact remotes on future pushes. It |
| 43 |
/// will be recreated on future fetches if it still exists on the remote. If |
| 44 |
/// there is a corresponding Git-tracking remote bookmark, it will also be |
| 45 |
/// forgotten. |
| 46 |
#[arg(long)] |
| 47 |
include_remotes: bool, |
| 48 |
|
| 49 |
/// The bookmarks to forget |
| 50 |
/// |
| 51 |
/// By default, the specified pattern matches bookmark names with glob |
| 52 |
/// syntax. You can also use other [string pattern syntax]. |
| 53 |
/// |
| 54 |
/// [string pattern syntax]: |
| 55 |
/// https://docs.jj-vcs.dev/latest/revsets/#string-patterns |
| 56 |
#[arg(required = true)] |
| 57 |
#[arg(add = ArgValueCandidates::new(complete::bookmarks))] |
| 58 |
names: Vec<String>, |
| 59 |
} |
| 60 |
|
| 61 |
pub async fn cmd_bookmark_forget( |
| 62 |
ui: &mut Ui, |
| 63 |
command: &CommandHelper, |
| 64 |
args: &BookmarkForgetArgs, |
| 65 |
) -> Result<(), CommandError> { |
| 66 |
let mut workspace_command = command.workspace_helper(ui).await?; |
| 67 |
let repo = workspace_command.repo().clone(); |
| 68 |
let ignored_remote = default_ignored_remote_name(repo.store()); |
| 69 |
let matched_bookmarks = find_forgettable_bookmarks(ui, repo.view(), &args.names)?; |
| 70 |
if matched_bookmarks.is_empty() { |
| 71 |
writeln!(ui.status(), "No bookmarks to forget.")?; |
| 72 |
return Ok(()); |
| 73 |
} |
| 74 |
|
| 75 |
let mut tx = workspace_command.start_transaction(); |
| 76 |
let mut forgotten_local: usize = 0; |
| 77 |
let mut forgotten_remote: usize = 0; |
| 78 |
for (name, bookmark_target) in &matched_bookmarks { |
| 79 |
if bookmark_target.local_target.is_present() { |
| 80 |
forgotten_local += 1; |
| 81 |
} |
| 82 |
tx.repo_mut() |
| 83 |
.set_local_bookmark_target(name, RefTarget::absent()); |
| 84 |
for (remote, _) in &bookmark_target.remote_refs { |
| 85 |
let symbol = name.to_remote_symbol(remote); |
| 86 |
// If `--include-remotes` is specified, we forget the corresponding remote |
| 87 |
// bookmarks instead of untracking them |
| 88 |
if args.include_remotes { |
| 89 |
tx.repo_mut() |
| 90 |
.set_remote_bookmark(symbol, RemoteRef::absent()); |
| 91 |
forgotten_remote += 1; |
| 92 |
continue; |
| 93 |
} |
| 94 |
// Git-tracking remote bookmarks cannot be untracked currently, so skip them |
| 95 |
if ignored_remote.is_some_and(|ignored| symbol.remote == ignored) { |
| 96 |
continue; |
| 97 |
} |
| 98 |
tx.repo_mut().untrack_remote_bookmark(symbol); |
| 99 |
} |
| 100 |
} |
| 101 |
if forgotten_local != 0 { |
| 102 |
writeln!(ui.status(), "Forgot {forgotten_local} local bookmarks.")?; |
| 103 |
} |
| 104 |
if forgotten_remote != 0 { |
| 105 |
writeln!(ui.status(), "Forgot {forgotten_remote} remote bookmarks.")?; |
| 106 |
} |
| 107 |
let forgotten_bookmarks = matched_bookmarks |
| 108 |
.iter() |
| 109 |
.map(|(name, _)| name.as_symbol()) |
| 110 |
.join(", "); |
| 111 |
tx.finish(ui, format!("forget bookmark {forgotten_bookmarks}")) |
| 112 |
.await?; |
| 113 |
Ok(()) |
| 114 |
} |
| 115 |
|
| 116 |
fn find_forgettable_bookmarks<'a>( |
| 117 |
ui: &Ui, |
| 118 |
view: &'a View, |
| 119 |
name_patterns: &[String], |
| 120 |
) -> Result<Vec<(&'a RefName, LocalRemoteRefTarget<'a>)>, CommandError> { |
| 121 |
let name_expr = parse_union_name_patterns(ui, name_patterns)?; |
| 122 |
let name_matcher = name_expr.to_matcher(); |
| 123 |
let matched_bookmarks = view |
| 124 |
.bookmarks() |
| 125 |
.filter(|(name, _)| name_matcher.is_match(name.as_str())) |
| 126 |
.collect(); |
| 127 |
warn_unmatched_local_or_remote_bookmarks(ui, view, &name_expr)?; |
| 128 |
Ok(matched_bookmarks) |
| 129 |
} |