-
Notifications
You must be signed in to change notification settings - Fork 966
Expand file tree
/
Copy pathmove.rs
More file actions
166 lines (154 loc) · 5.97 KB
/
move.rs
File metadata and controls
166 lines (154 loc) · 5.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright 2020-2023 The Jujutsu Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use clap_complete::ArgValueCandidates;
use clap_complete::ArgValueCompleter;
use itertools::Itertools as _;
use jj_lib::iter_util::fallible_any;
use jj_lib::iter_util::fallible_find;
use jj_lib::object_id::ObjectId as _;
use jj_lib::op_store::RefTarget;
use jj_lib::str_util::StringExpression;
use super::is_fast_forward;
use super::warn_unmatched_local_bookmarks;
use crate::cli_util::CommandHelper;
use crate::cli_util::RevisionArg;
use crate::command_error::CommandError;
use crate::command_error::user_error;
use crate::complete;
use crate::revset_util::parse_union_name_patterns;
use crate::ui::Ui;
/// Move existing bookmarks to target revision
///
/// Unlike `jj bookmark set`, this command cannot create new bookmarks.
///
/// If bookmark names are given, the specified bookmarks will be updated to
/// point to the target revision.
///
/// If `--from` options are given, bookmarks currently pointing to the
/// specified revisions will be updated. The bookmarks can also be filtered by
/// names.
#[derive(clap::Args, Clone, Debug)]
#[command(group(clap::ArgGroup::new("source").multiple(true).required(true)))]
pub struct BookmarkMoveArgs {
/// Move bookmarks matching the given name patterns
///
/// By default, the specified pattern matches bookmark names with glob
/// syntax. You can also use other [string pattern syntax].
///
/// [string pattern syntax]:
/// https://docs.jj-vcs.dev/latest/revsets/#string-patterns
#[arg(group = "source")]
#[arg(add = ArgValueCandidates::new(complete::local_bookmarks))]
names: Option<Vec<String>>,
/// Move bookmarks from the given revisions
#[arg(long, short, group = "source", value_name = "REVSETS")]
#[arg(add = ArgValueCompleter::new(complete::revset_expression_all))]
from: Vec<RevisionArg>,
/// Move bookmarks to this revision
#[arg(long, short, default_value = "@", value_name = "REVSET")]
#[arg(add = ArgValueCompleter::new(complete::revset_expression_all))]
to: RevisionArg,
/// Allow moving bookmarks backwards or sideways
#[arg(long, short = 'B')]
allow_backwards: bool,
}
pub async fn cmd_bookmark_move(
ui: &mut Ui,
command: &CommandHelper,
args: &BookmarkMoveArgs,
) -> Result<(), CommandError> {
let mut workspace_command = command.workspace_helper(ui)?;
let repo = workspace_command.repo().clone();
let target_commit = workspace_command.resolve_single_rev(ui, &args.to)?;
let matched_bookmarks = {
let is_source_ref: Box<dyn Fn(&RefTarget) -> _> = if !args.from.is_empty() {
let is_source_commit = workspace_command
.parse_union_revsets(ui, &args.from)?
.evaluate()?
.containing_fn();
Box::new(move |target| fallible_any(target.added_ids(), &is_source_commit))
} else {
Box::new(|_| Ok(true))
};
let name_expr = match &args.names {
Some(texts) => parse_union_name_patterns(ui, texts)?,
None => StringExpression::all(),
};
let name_matcher = name_expr.to_matcher();
let mut bookmarks: Vec<_> = repo
.view()
.local_bookmarks_matching(&name_matcher)
.filter_map(|(name, target)| {
is_source_ref(target)
.map(|matched| matched.then_some((name, target)))
.transpose()
})
.try_collect()?;
warn_unmatched_local_bookmarks(ui, repo.view(), &name_expr)?;
// Noop matches aren't errors, but should be excluded from stats.
bookmarks.retain(|(_, old_target)| old_target.as_normal() != Some(target_commit.id()));
bookmarks
};
if matched_bookmarks.is_empty() {
writeln!(ui.status(), "No bookmarks to update.")?;
return Ok(());
}
if !args.allow_backwards
&& let Some((name, _)) = fallible_find(
matched_bookmarks.iter(),
|(_, old_target)| -> Result<_, CommandError> {
let is_ff = is_fast_forward(repo.as_ref(), old_target, target_commit.id())?;
Ok(!is_ff)
},
)?
{
return Err(user_error(format!(
"Refusing to move bookmark backwards or sideways: {name}",
name = name.as_symbol()
))
.hinted("Use --allow-backwards to allow it."));
}
if target_commit.is_discardable(repo.as_ref()).await? {
writeln!(ui.warning_default(), "Target revision is empty.")?;
}
let mut tx = workspace_command.start_transaction();
for (name, _) in &matched_bookmarks {
tx.repo_mut()
.set_local_bookmark_target(name, RefTarget::normal(target_commit.id().clone()));
}
if let Some(mut formatter) = ui.status_formatter() {
write!(formatter, "Moved {} bookmarks to ", matched_bookmarks.len())?;
tx.write_commit_summary(formatter.as_mut(), &target_commit)?;
writeln!(formatter)?;
}
if matched_bookmarks.len() > 1 && args.names.is_none() {
writeln!(
ui.hint_default(),
"Specify bookmark by name to update just one of the bookmarks."
)?;
}
tx.finish(
ui,
format!(
"point bookmark {names} to commit {id}",
names = matched_bookmarks
.iter()
.map(|(name, _)| name.as_symbol())
.join(", "),
id = target_commit.id().hex()
),
)
.await?;
Ok(())
}