| 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 std::collections::HashSet; |
| 16 |
use std::rc::Rc; |
| 17 |
|
| 18 |
use clap_complete::ArgValueCandidates; |
| 19 |
use futures::TryStreamExt as _; |
| 20 |
use jj_lib::repo::Repo as _; |
| 21 |
use jj_lib::revset::RevsetExpression; |
| 22 |
use jj_lib::str_util::StringExpression; |
| 23 |
|
| 24 |
use super::warn_unmatched_local_or_remote_bookmarks; |
| 25 |
use crate::cli_util::CommandHelper; |
| 26 |
use crate::cli_util::RevisionArg; |
| 27 |
use crate::cli_util::default_ignored_remote_name; |
| 28 |
use crate::command_error::CommandError; |
| 29 |
use crate::commit_ref_list; |
| 30 |
use crate::commit_ref_list::RefFilterPredicates; |
| 31 |
use crate::commit_ref_list::SortKey; |
| 32 |
use crate::commit_templater::CommitRef; |
| 33 |
use crate::complete; |
| 34 |
use crate::revset_util::parse_union_name_patterns; |
| 35 |
use crate::templater::TemplateRenderer; |
| 36 |
use crate::ui::Ui; |
| 37 |
|
| 38 |
/// List bookmarks and their targets |
| 39 |
/// |
| 40 |
/// By default, a tracked remote bookmark will be included only if its target is |
| 41 |
/// different from the local target. An untracked remote bookmark won't be |
| 42 |
/// listed. For a conflicted bookmark (both local and remote), old target |
| 43 |
/// revisions are preceded by a "-" and new target revisions are preceded by a |
| 44 |
/// "+". |
| 45 |
/// |
| 46 |
/// See [`jj help -k bookmarks`] for more information. |
| 47 |
/// |
| 48 |
/// [`jj help -k bookmarks`]: |
| 49 |
/// https://docs.jj-vcs.dev/latest/bookmarks |
| 50 |
#[derive(clap::Args, Clone, Debug)] |
| 51 |
pub struct BookmarkListArgs { |
| 52 |
/// Show all tracked and untracked remote bookmarks including the ones |
| 53 |
/// whose targets are synchronized with the local bookmarks |
| 54 |
#[arg(long, short, alias = "all")] |
| 55 |
all_remotes: bool, |
| 56 |
|
| 57 |
/// Show all tracked and untracked remote bookmarks belonging to this remote |
| 58 |
/// |
| 59 |
/// Can be combined with `--tracked` or `--conflicted` to filter the |
| 60 |
/// bookmarks shown (can be repeated.) |
| 61 |
/// |
| 62 |
/// By default, the specified pattern matches remote names with glob syntax. |
| 63 |
/// You can also use other [string pattern syntax]. |
| 64 |
/// |
| 65 |
/// [string pattern syntax]: |
| 66 |
/// https://docs.jj-vcs.dev/latest/revsets/#string-patterns |
| 67 |
#[arg(long = "remote", value_name = "REMOTE", conflicts_with = "all_remotes")] |
| 68 |
#[arg(add = ArgValueCandidates::new(complete::git_remotes))] |
| 69 |
remotes: Option<Vec<String>>, |
| 70 |
|
| 71 |
/// Show tracked remote bookmarks only |
| 72 |
/// |
| 73 |
/// This omits local Git-tracking bookmarks by default. |
| 74 |
#[arg(long, short, conflicts_with = "all_remotes")] |
| 75 |
tracked: bool, |
| 76 |
|
| 77 |
/// Show conflicted bookmarks only |
| 78 |
#[arg(long, short, conflicts_with = "all_remotes")] |
| 79 |
conflicted: bool, |
| 80 |
|
| 81 |
/// Show bookmarks whose local name matches |
| 82 |
/// |
| 83 |
/// By default, the specified pattern matches bookmark names with glob |
| 84 |
/// syntax. You can also use other [string pattern syntax]. |
| 85 |
/// |
| 86 |
/// [string pattern syntax]: |
| 87 |
/// https://docs.jj-vcs.dev/latest/revsets/#string-patterns |
| 88 |
#[arg(add = ArgValueCandidates::new(complete::bookmarks))] |
| 89 |
names: Option<Vec<String>>, |
| 90 |
|
| 91 |
/// Show bookmarks whose local targets are in the given revisions |
| 92 |
/// |
| 93 |
/// Note that `-r deleted_bookmark` will not work since `deleted_bookmark` |
| 94 |
/// wouldn't have a local target. |
| 95 |
#[arg(long = "revision", short, value_name = "REVSETS", alias = "revisions")] |
| 96 |
revisions: Option<Vec<RevisionArg>>, |
| 97 |
|
| 98 |
/// Render each bookmark using the given template |
| 99 |
/// |
| 100 |
/// All 0-argument methods of the [`CommitRef` type] are available as |
| 101 |
/// keywords in the template expression. See [`jj help -k templates`] |
| 102 |
/// for more information. |
| 103 |
/// |
| 104 |
/// The default template can be set by the `templates.bookmark_list` |
| 105 |
/// setting. |
| 106 |
/// |
| 107 |
/// [`CommitRef` type]: |
| 108 |
/// https://docs.jj-vcs.dev/latest/templates/#commitref-type |
| 109 |
/// |
| 110 |
/// [`jj help -k templates`]: |
| 111 |
/// https://docs.jj-vcs.dev/latest/templates/ |
| 112 |
#[arg(long, short = 'T')] |
| 113 |
#[arg(add = ArgValueCandidates::new(complete::template_aliases))] |
| 114 |
template: Option<String>, |
| 115 |
|
| 116 |
/// Sort bookmarks based on the given key (or multiple keys) |
| 117 |
/// |
| 118 |
/// Suffix the key with `-` to sort in descending order of the value (e.g. |
| 119 |
/// `--sort name-`). Note that when using multiple keys, the first key is |
| 120 |
/// the most significant. |
| 121 |
/// |
| 122 |
/// This defaults to the `ui.bookmark-list-sort-keys` setting. |
| 123 |
#[arg(long, value_name = "SORT_KEY", value_enum, value_delimiter = ',')] |
| 124 |
sort: Vec<SortKey>, |
| 125 |
} |
| 126 |
|
| 127 |
pub async fn cmd_bookmark_list( |
| 128 |
ui: &mut Ui, |
| 129 |
command: &CommandHelper, |
| 130 |
args: &BookmarkListArgs, |
| 131 |
) -> Result<(), CommandError> { |
| 132 |
let workspace_command = command.workspace_helper(ui).await?; |
| 133 |
let repo = workspace_command.repo(); |
| 134 |
let view = repo.view(); |
| 135 |
|
| 136 |
// Like cmd_git_push(), names and revisions are OR-ed. |
| 137 |
let name_expr = match (&args.names, &args.revisions) { |
| 138 |
(Some(texts), _) => parse_union_name_patterns(ui, texts)?, |
| 139 |
(None, Some(_)) => StringExpression::none(), |
| 140 |
(None, None) => StringExpression::all(), |
| 141 |
}; |
| 142 |
let matched_local_targets: HashSet<_> = if let Some(revisions) = &args.revisions { |
| 143 |
// Match against local targets only, which is consistent with "jj git push". |
| 144 |
let mut expression = workspace_command.parse_union_revsets(ui, revisions)?; |
| 145 |
// Intersects with the set of local bookmark targets to minimize the lookup |
| 146 |
// space. |
| 147 |
expression.intersect_with(&RevsetExpression::bookmarks(StringExpression::all())); |
| 148 |
expression.evaluate_to_commit_ids()?.try_collect().await? |
| 149 |
} else { |
| 150 |
HashSet::new() |
| 151 |
}; |
| 152 |
|
| 153 |
let template: TemplateRenderer<Rc<CommitRef>> = { |
| 154 |
let language = workspace_command.commit_template_language(); |
| 155 |
let text = match &args.template { |
| 156 |
Some(value) => value.to_owned(), |
| 157 |
None => workspace_command |
| 158 |
.settings() |
| 159 |
.get("templates.bookmark_list")?, |
| 160 |
}; |
| 161 |
workspace_command |
| 162 |
.parse_template(ui, &language, &text)? |
| 163 |
.labeled(["bookmark_list"]) |
| 164 |
}; |
| 165 |
|
| 166 |
let ignored_tracked_remote = default_ignored_remote_name(repo.store()); |
| 167 |
// --tracked implies --remote=~git by default |
| 168 |
let remote_expr = match ( |
| 169 |
&args.remotes, |
| 170 |
args.tracked.then_some(ignored_tracked_remote).flatten(), |
| 171 |
) { |
| 172 |
(Some(texts), _) => parse_union_name_patterns(ui, texts)?, |
| 173 |
(None, Some(ignored)) => StringExpression::exact(ignored).negated(), |
| 174 |
(None, None) => StringExpression::all(), |
| 175 |
}; |
| 176 |
|
| 177 |
let predicates = RefFilterPredicates { |
| 178 |
name_matcher: name_expr.to_matcher(), |
| 179 |
remote_matcher: remote_expr.to_matcher(), |
| 180 |
matched_local_targets, |
| 181 |
conflicted: args.conflicted, |
| 182 |
include_local_only: !args.tracked && args.remotes.is_none(), |
| 183 |
include_synced_remotes: args.tracked || args.all_remotes || args.remotes.is_some(), |
| 184 |
include_untracked_remotes: !args.tracked && (args.all_remotes || args.remotes.is_some()), |
| 185 |
}; |
| 186 |
let mut bookmark_list_items = commit_ref_list::collect_items(view.bookmarks(), &predicates); |
| 187 |
let sort_keys = if args.sort.is_empty() { |
| 188 |
workspace_command.settings().get_value_with( |
| 189 |
"ui.bookmark-list-sort-keys", |
| 190 |
commit_ref_list::parse_sort_keys, |
| 191 |
)? |
| 192 |
} else { |
| 193 |
args.sort.clone() |
| 194 |
}; |
| 195 |
commit_ref_list::sort(repo.store(), &mut bookmark_list_items, &sort_keys)?; |
| 196 |
|
| 197 |
ui.request_pager(); |
| 198 |
let mut formatter = ui.stdout_formatter(); |
| 199 |
bookmark_list_items |
| 200 |
.iter() |
| 201 |
.flat_map(|item| itertools::chain([&item.primary], &item.tracked)) |
| 202 |
.try_for_each(|commit_ref| template.format(commit_ref, formatter.as_mut()))?; |
| 203 |
drop(formatter); |
| 204 |
|
| 205 |
warn_unmatched_local_or_remote_bookmarks(ui, view, &name_expr)?; |
| 206 |
|
| 207 |
if bookmark_list_items |
| 208 |
.iter() |
| 209 |
.any(|item| item.primary.is_local() && item.primary.has_conflict()) |
| 210 |
{ |
| 211 |
writeln!( |
| 212 |
ui.hint_default(), |
| 213 |
"Some bookmarks have conflicts. Use `jj bookmark set <name> -r <rev>` to resolve." |
| 214 |
)?; |
| 215 |
} |
| 216 |
|
| 217 |
#[cfg(feature = "git")] |
| 218 |
if jj_lib::git::get_git_backend(repo.store()).is_ok() { |
| 219 |
// Print only one of these hints. It's not important to mention unexported |
| 220 |
// bookmarks, but user might wonder why deleted bookmarks are still listed. |
| 221 |
let deleted_tracking = bookmark_list_items |
| 222 |
.iter() |
| 223 |
.filter(|item| item.primary.is_local() && item.primary.is_absent()) |
| 224 |
.map(|item| { |
| 225 |
item.tracked.iter().any(|r| { |
| 226 |
let remote = r.remote_name().expect("tracked ref should be remote"); |
| 227 |
ignored_tracked_remote.is_none_or(|ignored| remote != ignored) |
| 228 |
}) |
| 229 |
}) |
| 230 |
.max(); |
| 231 |
match deleted_tracking { |
| 232 |
Some(true) => { |
| 233 |
writeln!( |
| 234 |
ui.hint_default(), |
| 235 |
"Bookmarks marked as deleted can be *deleted permanently* on the remote by \ |
| 236 |
running `jj git push --deleted`. Use `jj bookmark forget` if you don't want \ |
| 237 |
that." |
| 238 |
)?; |
| 239 |
} |
| 240 |
Some(false) => { |
| 241 |
writeln!( |
| 242 |
ui.hint_default(), |
| 243 |
"Bookmarks marked as deleted will be deleted from the underlying Git repo on \ |
| 244 |
the next `jj git export`." |
| 245 |
)?; |
| 246 |
} |
| 247 |
None => {} |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
Ok(()) |
| 252 |
} |