| 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 jj_lib::op_store::RefTarget; |
| 17 |
use jj_lib::ref_name::RefNameBuf; |
| 18 |
use jj_lib::repo::Repo as _; |
| 19 |
use jj_lib::str_util::StringExpression; |
| 20 |
use jj_lib::str_util::StringMatcher; |
| 21 |
|
| 22 |
use crate::cli_util::CommandHelper; |
| 23 |
use crate::cli_util::default_ignored_remote_name; |
| 24 |
use crate::command_error::CommandError; |
| 25 |
use crate::command_error::user_error; |
| 26 |
use crate::complete; |
| 27 |
use crate::revset_util; |
| 28 |
use crate::ui::Ui; |
| 29 |
|
| 30 |
/// Rename `old` bookmark name to `new` bookmark name |
| 31 |
/// |
| 32 |
/// The new bookmark name points at the same commit as the old bookmark name. |
| 33 |
#[derive(clap::Args, Clone, Debug)] |
| 34 |
pub struct BookmarkRenameArgs { |
| 35 |
/// The old name of the bookmark |
| 36 |
#[arg(value_parser = revset_util::parse_bookmark_name)] |
| 37 |
#[arg(add = ArgValueCandidates::new(complete::local_bookmarks))] |
| 38 |
old: RefNameBuf, |
| 39 |
|
| 40 |
/// The new name of the bookmark |
| 41 |
#[arg(value_parser = revset_util::parse_bookmark_name)] |
| 42 |
new: RefNameBuf, |
| 43 |
|
| 44 |
/// Allow renaming even if the new bookmark name already exists |
| 45 |
#[arg(long)] |
| 46 |
overwrite_existing: bool, |
| 47 |
} |
| 48 |
|
| 49 |
pub async fn cmd_bookmark_rename( |
| 50 |
ui: &mut Ui, |
| 51 |
command: &CommandHelper, |
| 52 |
args: &BookmarkRenameArgs, |
| 53 |
) -> Result<(), CommandError> { |
| 54 |
let mut workspace_command = command.workspace_helper(ui).await?; |
| 55 |
let base_repo = workspace_command.repo().clone(); |
| 56 |
let base_view = base_repo.view(); |
| 57 |
let old_bookmark = &args.old; |
| 58 |
let ref_target = base_view.get_local_bookmark(old_bookmark).clone(); |
| 59 |
if ref_target.is_absent() { |
| 60 |
return Err(user_error(format!( |
| 61 |
"No such bookmark: {old_bookmark}", |
| 62 |
old_bookmark = old_bookmark.as_symbol() |
| 63 |
))); |
| 64 |
} |
| 65 |
|
| 66 |
let new_bookmark = &args.new; |
| 67 |
if !args.overwrite_existing && base_view.get_local_bookmark(new_bookmark).is_present() { |
| 68 |
return Err(user_error(format!( |
| 69 |
"Bookmark already exists: {new_bookmark}", |
| 70 |
new_bookmark = new_bookmark.as_symbol() |
| 71 |
))); |
| 72 |
} |
| 73 |
if old_bookmark == new_bookmark { |
| 74 |
// Noop if renaming to the same name. |
| 75 |
return Ok(()); |
| 76 |
} |
| 77 |
|
| 78 |
let mut tx = workspace_command.start_transaction(); |
| 79 |
|
| 80 |
let remote_matcher = match default_ignored_remote_name(tx.repo().store()) { |
| 81 |
Some(remote) => StringExpression::exact(remote).negated().to_matcher(), |
| 82 |
None => StringMatcher::all(), |
| 83 |
}; |
| 84 |
|
| 85 |
if args.overwrite_existing { |
| 86 |
// Untrack all tracked remotes of the overwritten bookmark so that |
| 87 |
// tracking state transfer from the old bookmark starts clean. |
| 88 |
for (symbol, remote_ref) in base_view |
| 89 |
.remote_bookmarks_matching(&StringMatcher::exact(new_bookmark), &remote_matcher) |
| 90 |
{ |
| 91 |
if remote_ref.is_tracked() { |
| 92 |
tx.repo_mut() |
| 93 |
.untrack_remote_bookmark(new_bookmark.to_remote_symbol(symbol.remote)); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
tx.repo_mut() |
| 98 |
.set_local_bookmark_target(new_bookmark, ref_target); |
| 99 |
tx.repo_mut() |
| 100 |
.set_local_bookmark_target(old_bookmark, RefTarget::absent()); |
| 101 |
|
| 102 |
// Preserve tracking state of old bookmark |
| 103 |
let mut tracked_present_old_remotes_exist = false; |
| 104 |
for (symbol, remote_ref) in |
| 105 |
base_view.remote_bookmarks_matching(&StringMatcher::exact(old_bookmark), &remote_matcher) |
| 106 |
{ |
| 107 |
if !remote_ref.is_tracked() { |
| 108 |
continue; |
| 109 |
} |
| 110 |
if remote_ref.is_present() { |
| 111 |
tracked_present_old_remotes_exist = true; |
| 112 |
} |
| 113 |
let new_remote_bookmark = new_bookmark.to_remote_symbol(symbol.remote); |
| 114 |
let existing_ref = tx.repo().view().get_remote_bookmark(new_remote_bookmark); |
| 115 |
if existing_ref.is_present() && !existing_ref.is_tracked() { |
| 116 |
writeln!( |
| 117 |
ui.warning_default(), |
| 118 |
"The renamed bookmark already exists on the remote '{remote}', tracking state was \ |
| 119 |
dropped.", |
| 120 |
remote = new_remote_bookmark.remote.as_symbol(), |
| 121 |
)?; |
| 122 |
writeln!( |
| 123 |
ui.hint_default(), |
| 124 |
"To track the existing remote bookmark, run `jj bookmark track \ |
| 125 |
{new_remote_bookmark}`.", |
| 126 |
)?; |
| 127 |
continue; |
| 128 |
} |
| 129 |
tx.repo_mut() |
| 130 |
.track_remote_bookmark(new_remote_bookmark) |
| 131 |
.await?; |
| 132 |
} |
| 133 |
|
| 134 |
// Warn about present+tracked remotes of the overwritten bookmark where the |
| 135 |
// old bookmark was untracked on the same remote. |
| 136 |
if args.overwrite_existing { |
| 137 |
for (symbol, remote_ref) in base_view |
| 138 |
.remote_bookmarks_matching(&StringMatcher::exact(new_bookmark), &remote_matcher) |
| 139 |
{ |
| 140 |
if !remote_ref.is_tracked() || !remote_ref.is_present() { |
| 141 |
continue; |
| 142 |
} |
| 143 |
let old_ref = |
| 144 |
base_view.get_remote_bookmark(old_bookmark.to_remote_symbol(symbol.remote)); |
| 145 |
if old_ref.is_tracked() { |
| 146 |
continue; |
| 147 |
} |
| 148 |
writeln!( |
| 149 |
ui.warning_default(), |
| 150 |
"Tracking of remote bookmark {new_bookmark}@{remote} was dropped.", |
| 151 |
new_bookmark = new_bookmark.as_symbol(), |
| 152 |
remote = symbol.remote.as_symbol(), |
| 153 |
)?; |
| 154 |
writeln!( |
| 155 |
ui.hint_default(), |
| 156 |
"Use `jj bookmark track` to re-track if needed.", |
| 157 |
)?; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
tx.finish( |
| 162 |
ui, |
| 163 |
format!( |
| 164 |
"rename bookmark {old_bookmark} to {new_bookmark}", |
| 165 |
old_bookmark = old_bookmark.as_symbol(), |
| 166 |
new_bookmark = new_bookmark.as_symbol() |
| 167 |
), |
| 168 |
) |
| 169 |
.await?; |
| 170 |
|
| 171 |
if tracked_present_old_remotes_exist { |
| 172 |
writeln!( |
| 173 |
ui.warning_default(), |
| 174 |
"Tracked remote bookmarks for bookmark {old_bookmark} were not renamed.", |
| 175 |
old_bookmark = old_bookmark.as_symbol(), |
| 176 |
)?; |
| 177 |
writeln!( |
| 178 |
ui.hint_default(), |
| 179 |
"To rename the bookmark on the remote, you can `jj git push --bookmark \ |
| 180 |
{old_bookmark}` first (to delete it on the remote), and then `jj git push --bookmark \ |
| 181 |
{new_bookmark}`. `jj git push --all --deleted` would also be sufficient.", |
| 182 |
old_bookmark = old_bookmark.as_symbol(), |
| 183 |
new_bookmark = new_bookmark.as_symbol() |
| 184 |
)?; |
| 185 |
} |
| 186 |
if !args.overwrite_existing { |
| 187 |
let has_existing_present_tracked = base_view |
| 188 |
.remote_bookmarks_matching(&StringMatcher::exact(new_bookmark), &remote_matcher) |
| 189 |
.any(|(_, remote_ref)| remote_ref.is_tracked() && remote_ref.is_present()); |
| 190 |
if has_existing_present_tracked { |
| 191 |
// This isn't an error because bookmark renaming can't be propagated |
| 192 |
// to the remote immediately. "rename old new && rename new old" |
| 193 |
// should be allowed even if the original old bookmark had tracked |
| 194 |
// remotes. |
| 195 |
writeln!( |
| 196 |
ui.warning_default(), |
| 197 |
"Tracked remote bookmarks for bookmark {new_bookmark} exist.", |
| 198 |
new_bookmark = new_bookmark.as_symbol() |
| 199 |
)?; |
| 200 |
writeln!( |
| 201 |
ui.hint_default(), |
| 202 |
"Run `jj bookmark untrack {new_bookmark}` to disassociate them.", |
| 203 |
new_bookmark = new_bookmark.as_symbol() |
| 204 |
)?; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
Ok(()) |
| 209 |
} |