| 1 |
// Copyright 2026 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 clap_complete::ArgValueCompleter; |
| 17 |
use itertools::Itertools as _; |
| 18 |
use jj_lib::dsl_util::ExpressionNode; |
| 19 |
use jj_lib::iter_util::fallible_any; |
| 20 |
use jj_lib::iter_util::fallible_find; |
| 21 |
use jj_lib::object_id::ObjectId as _; |
| 22 |
use jj_lib::op_store::RefTarget; |
| 23 |
use jj_lib::revset; |
| 24 |
use jj_lib::revset::ExpressionKind; |
| 25 |
use jj_lib::revset::RevsetDiagnostics; |
| 26 |
|
| 27 |
use super::is_fast_forward; |
| 28 |
use super::warn_unmatched_local_bookmarks; |
| 29 |
use crate::cli_util::CommandHelper; |
| 30 |
use crate::cli_util::RevisionArg; |
| 31 |
use crate::command_error::CommandError; |
| 32 |
use crate::command_error::print_parse_diagnostics; |
| 33 |
use crate::command_error::user_error; |
| 34 |
use crate::complete; |
| 35 |
use crate::revset_util::parse_union_name_patterns; |
| 36 |
use crate::ui::Ui; |
| 37 |
|
| 38 |
/// Advance the closest bookmarks to a target revision |
| 39 |
/// |
| 40 |
/// The target `--to` defaults to `revsets.bookmark-advance-to` |
| 41 |
/// (which defaults to `@`). |
| 42 |
/// |
| 43 |
/// The bookmarks to advance are determined by `revsets.bookmark-advance-from` |
| 44 |
/// (which defaults to `heads(::to & bookmarks())`). |
| 45 |
/// |
| 46 |
/// Note that the from revset has access to `to`. |
| 47 |
/// |
| 48 |
/// Positional bookmark name arguments can target specific bookmarks to advance |
| 49 |
/// to the target, in this case the default from revset is ignored. |
| 50 |
/// |
| 51 |
/// Example: |
| 52 |
/// |
| 53 |
/// `jj bookmark advance --to x` - Does the equivalent of |
| 54 |
/// `jj bookmark move --from 'heads(::x & bookmarks())' --to x`. |
| 55 |
#[derive(clap::Args, Clone, Debug)] |
| 56 |
pub struct BookmarkAdvanceArgs { |
| 57 |
/// Move bookmarks matching the given name patterns |
| 58 |
/// |
| 59 |
/// By default, the specified pattern matches bookmark names with glob |
| 60 |
/// syntax. You can also use other [string pattern syntax]. |
| 61 |
/// |
| 62 |
/// [string pattern syntax]: |
| 63 |
/// https://docs.jj-vcs.dev/latest/revsets/#string-patterns |
| 64 |
#[arg(add = ArgValueCandidates::new(complete::local_bookmarks))] |
| 65 |
names: Option<Vec<String>>, |
| 66 |
|
| 67 |
/// Move bookmarks to this revision |
| 68 |
/// |
| 69 |
/// Defaults to `revsets.bookmark-advance-to`. |
| 70 |
#[arg(long, short, value_name = "REVSET")] |
| 71 |
#[arg(add = ArgValueCompleter::new(complete::revset_expression_all))] |
| 72 |
to: Option<RevisionArg>, |
| 73 |
} |
| 74 |
|
| 75 |
pub async fn cmd_bookmark_advance( |
| 76 |
ui: &mut Ui, |
| 77 |
command: &CommandHelper, |
| 78 |
args: &BookmarkAdvanceArgs, |
| 79 |
) -> Result<(), CommandError> { |
| 80 |
let mut workspace_command = command.workspace_helper(ui).await?; |
| 81 |
|
| 82 |
let to = if let Some(to) = &args.to { |
| 83 |
to.clone() |
| 84 |
} else { |
| 85 |
RevisionArg::from( |
| 86 |
workspace_command |
| 87 |
.settings() |
| 88 |
.get_string("revsets.bookmark-advance-to")?, |
| 89 |
) |
| 90 |
}; |
| 91 |
|
| 92 |
let target_commit = workspace_command |
| 93 |
.resolve_single_rev(ui, &to) |
| 94 |
.await |
| 95 |
.map_err(|error| { |
| 96 |
if args.to.is_none() { |
| 97 |
error.hinted( |
| 98 |
"`revsets.bookmark-advance-to` controls the default target. You can also \ |
| 99 |
specify a specific target with `--to`.", |
| 100 |
) |
| 101 |
} else { |
| 102 |
error |
| 103 |
} |
| 104 |
})?; |
| 105 |
|
| 106 |
let repo = workspace_command.repo().clone(); |
| 107 |
|
| 108 |
let matched_bookmarks = { |
| 109 |
let mut bookmarks: Vec<_> = match &args.names { |
| 110 |
Some(texts) => { |
| 111 |
let name_expr = parse_union_name_patterns(ui, texts)?; |
| 112 |
let name_matcher = name_expr.to_matcher(); |
| 113 |
let result = repo |
| 114 |
.view() |
| 115 |
.local_bookmarks_matching(&name_matcher) |
| 116 |
.collect(); |
| 117 |
warn_unmatched_local_bookmarks(ui, repo.view(), &name_expr)?; |
| 118 |
result |
| 119 |
} |
| 120 |
None => { |
| 121 |
// Get the default-from revset config, and provide `to`. |
| 122 |
let from_revset_str = workspace_command |
| 123 |
.settings() |
| 124 |
.get_string("revsets.bookmark-advance-from")?; |
| 125 |
let mut context = workspace_command.env().revset_parse_context(); |
| 126 |
let commit_hex = target_commit.id().hex(); |
| 127 |
context.local_variables.insert( |
| 128 |
"to", |
| 129 |
ExpressionNode { |
| 130 |
kind: ExpressionKind::String(commit_hex.clone()), |
| 131 |
span: pest::Span::new(commit_hex.as_str(), 0, commit_hex.len()) |
| 132 |
.expect("programmatic span shouldn't fail"), |
| 133 |
}, |
| 134 |
); |
| 135 |
|
| 136 |
let mut diags = RevsetDiagnostics::default(); |
| 137 |
let expression = revset::parse(&mut diags, &from_revset_str, &context)?; |
| 138 |
print_parse_diagnostics(ui, "In revsets.bookmark-advance-from", &diags)?; |
| 139 |
|
| 140 |
let is_source_commit = workspace_command |
| 141 |
.attach_revset_evaluator(expression) |
| 142 |
.evaluate()? |
| 143 |
.containing_fn(); |
| 144 |
let is_source_ref = async |target: &RefTarget| -> Result<bool, CommandError> { |
| 145 |
Ok( |
| 146 |
fallible_any(target.added_ids(), async |old| is_source_commit(old).await) |
| 147 |
.await?, |
| 148 |
) |
| 149 |
}; |
| 150 |
|
| 151 |
let mut bookmarks = vec![]; |
| 152 |
for (name, target) in repo.view().local_bookmarks() { |
| 153 |
if is_source_ref(target).await? { |
| 154 |
bookmarks.push((name, target)); |
| 155 |
} |
| 156 |
} |
| 157 |
bookmarks |
| 158 |
} |
| 159 |
}; |
| 160 |
// Noop matches aren't errors, but should be excluded from stats. |
| 161 |
bookmarks.retain(|(_, old_target)| old_target.as_normal() != Some(target_commit.id())); |
| 162 |
bookmarks |
| 163 |
}; |
| 164 |
|
| 165 |
if matched_bookmarks.is_empty() { |
| 166 |
writeln!(ui.status(), "No bookmarks to update.")?; |
| 167 |
return Ok(()); |
| 168 |
} |
| 169 |
|
| 170 |
if let Some((name, _)) = fallible_find( |
| 171 |
matched_bookmarks.iter(), |
| 172 |
async |(_, old_target)| -> Result<_, CommandError> { |
| 173 |
let is_ff = is_fast_forward(repo.as_ref(), old_target, target_commit.id()).await?; |
| 174 |
Ok(!is_ff) |
| 175 |
}, |
| 176 |
) |
| 177 |
.await? |
| 178 |
{ |
| 179 |
return Err(user_error(format!( |
| 180 |
"Refusing to advance bookmark backwards or sideways: {name}", |
| 181 |
name = name.as_symbol() |
| 182 |
))); |
| 183 |
} |
| 184 |
if target_commit.is_discardable(repo.as_ref()).await? { |
| 185 |
writeln!(ui.warning_default(), "Target revision is empty.")?; |
| 186 |
} |
| 187 |
|
| 188 |
let mut tx = workspace_command.start_transaction(); |
| 189 |
for (name, _) in &matched_bookmarks { |
| 190 |
tx.repo_mut() |
| 191 |
.set_local_bookmark_target(name, RefTarget::normal(target_commit.id().clone())); |
| 192 |
} |
| 193 |
|
| 194 |
if let Some(mut formatter) = ui.status_formatter() { |
| 195 |
write!( |
| 196 |
formatter, |
| 197 |
"Advanced {} bookmarks to ", |
| 198 |
matched_bookmarks.len() |
| 199 |
)?; |
| 200 |
tx.write_commit_summary(formatter.as_mut(), &target_commit)?; |
| 201 |
writeln!(formatter)?; |
| 202 |
} |
| 203 |
if matched_bookmarks.len() > 1 && args.names.is_none() { |
| 204 |
writeln!( |
| 205 |
ui.hint_default(), |
| 206 |
"Specify bookmark by name to update just one of the bookmarks." |
| 207 |
)?; |
| 208 |
} |
| 209 |
|
| 210 |
tx.finish( |
| 211 |
ui, |
| 212 |
format!( |
| 213 |
"point bookmark {names} to commit {id}", |
| 214 |
names = matched_bookmarks |
| 215 |
.iter() |
| 216 |
.map(|(name, _)| name.as_symbol()) |
| 217 |
.join(", "), |
| 218 |
id = target_commit.id().hex() |
| 219 |
), |
| 220 |
) |
| 221 |
.await?; |
| 222 |
Ok(()) |
| 223 |
} |