Debian Codegraph @

cli/src/commands/bookmark/set.rs

raw
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
17 use clap_complete::ArgValueCandidates;
18 use clap_complete::ArgValueCompleter;
19 use itertools::Itertools as _;
20 use jj_lib::object_id::ObjectId as _;
21 use jj_lib::op_store::RefTarget;
22 use jj_lib::ref_name::RefNameBuf;
23
24 use super::is_fast_forward;
25 use crate::cli_util::CommandHelper;
26 use crate::cli_util::RevisionArg;
27 use crate::cli_util::has_tracked_remote_bookmarks;
28 use crate::command_error::CommandError;
29 use crate::command_error::user_error;
30 use crate::complete;
31 use crate::revset_util;
32 use crate::ui::Ui;
33
34 /// Create a new bookmark, or update an existing one by name
35 ///
36 /// If you want to move bookmarks based on their current location rather than
37 /// by name, use `jj bookmark move --from <REVSETS>`.
38 #[derive(clap::Args, Clone, Debug)]
39 pub struct BookmarkSetArgs {
40 /// The bookmark's target revision
41 #[arg(
42 long,
43 short,
44 default_value = "@",
45 visible_alias = "to",
46 value_name = "REVSET"
47 )]
48 #[arg(add = ArgValueCompleter::new(complete::revset_expression_all))]
49 revision: RevisionArg,
50
51 /// Allow moving the bookmark backwards or sideways
52 #[arg(long, short = 'B')]
53 allow_backwards: bool,
54
55 /// The bookmarks to update
56 #[arg(required = true, value_parser = revset_util::parse_bookmark_name)]
57 #[arg(add = ArgValueCandidates::new(complete::local_bookmarks))]
58 names: Vec<RefNameBuf>,
59 }
60
61 pub async fn cmd_bookmark_set(
62 ui: &mut Ui,
63 command: &CommandHelper,
64 args: &BookmarkSetArgs,
65 ) -> Result<(), CommandError> {
66 let mut workspace_command = command.workspace_helper(ui).await?;
67 let target_commit = workspace_command
68 .resolve_single_rev(ui, &args.revision)
69 .await?;
70 let repo = workspace_command.repo().as_ref();
71 let bookmark_names = &args.names;
72 let mut new_bookmarks = HashSet::new();
73 let mut moved_bookmark_count = 0;
74 for name in bookmark_names {
75 let old_target = repo.view().get_local_bookmark(name);
76 // If a bookmark is absent locally but is still tracking remote bookmarks,
77 // we are resurrecting the local bookmark, not "creating" a new bookmark.
78 if old_target.is_absent() && !has_tracked_remote_bookmarks(repo, name) {
79 new_bookmarks.insert(name);
80 } else if old_target.as_normal() != Some(target_commit.id()) {
81 moved_bookmark_count += 1;
82 }
83 if !args.allow_backwards && !is_fast_forward(repo, old_target, target_commit.id()).await? {
84 return Err(user_error(format!(
85 "Refusing to move bookmark backwards or sideways: {name}",
86 name = name.as_symbol()
87 ))
88 .hinted("Use --allow-backwards to allow it."));
89 }
90 }
91 if target_commit.is_discardable(repo).await? {
92 writeln!(ui.warning_default(), "Target revision is empty.")?;
93 }
94
95 let mut tx = workspace_command.start_transaction();
96 let remote_settings = tx.settings().remote_settings()?;
97 let remote_auto_track_matchers =
98 revset_util::parse_remote_auto_track_bookmarks_map_for_new_bookmarks(ui, &remote_settings)?;
99 let readonly_repo = tx.base_repo().clone();
100 for name in bookmark_names {
101 tx.repo_mut()
102 .set_local_bookmark_target(name, RefTarget::normal(target_commit.id().clone()));
103 if new_bookmarks.contains(name) {
104 for (remote_name, matcher) in &remote_auto_track_matchers {
105 if !matcher.is_match(name.as_str()) {
106 continue;
107 }
108 let Some(view) = readonly_repo.view().get_remote_view(remote_name) else {
109 continue;
110 };
111 let symbol = name.to_remote_symbol(remote_name);
112 if view.bookmarks.contains_key(name) {
113 writeln!(
114 ui.warning_default(),
115 "Auto-tracking bookmark that exists on the remote: {symbol}"
116 )?;
117 }
118 tx.repo_mut().track_remote_bookmark(symbol).await?;
119 }
120 }
121 }
122
123 if let Some(mut formatter) = ui.status_formatter() {
124 let new_bookmark_count = new_bookmarks.len();
125 if new_bookmark_count > 0 {
126 write!(
127 formatter,
128 "Created {new_bookmark_count} bookmarks pointing to "
129 )?;
130 tx.write_commit_summary(formatter.as_mut(), &target_commit)?;
131 writeln!(formatter)?;
132 }
133 if moved_bookmark_count > 0 {
134 write!(formatter, "Moved {moved_bookmark_count} bookmarks to ")?;
135 tx.write_commit_summary(formatter.as_mut(), &target_commit)?;
136 writeln!(formatter)?;
137 }
138 }
139
140 tx.finish(
141 ui,
142 format!(
143 "point bookmark {names} to commit {id}",
144 names = bookmark_names.iter().map(|n| n.as_symbol()).join(", "),
145 id = target_commit.id().hex()
146 ),
147 )
148 .await?;
149 Ok(())
150 }