Debian Codegraph @

cli/src/commands/bookmark/create.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 clap_complete::ArgValueCompleter;
16 use itertools::Itertools as _;
17 use jj_lib::object_id::ObjectId as _;
18 use jj_lib::op_store::RefTarget;
19 use jj_lib::ref_name::RefNameBuf;
20
21 use crate::cli_util::CommandHelper;
22 use crate::cli_util::RevisionArg;
23 use crate::cli_util::has_tracked_remote_bookmarks;
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 /// Create a new bookmark
31 #[derive(clap::Args, Clone, Debug)]
32 pub struct BookmarkCreateArgs {
33 /// The bookmark's target revision
34 //
35 // The `--to` alias exists for making it easier for the user to switch
36 // between `bookmark create`, `bookmark move`, and `bookmark set`.
37 #[arg(
38 long,
39 short,
40 default_value = "@",
41 visible_alias = "to",
42 value_name = "REVSET"
43 )]
44 #[arg(add = ArgValueCompleter::new(complete::revset_expression_all))]
45 revision: RevisionArg,
46
47 /// The bookmarks to create
48 #[arg(required = true, value_parser = revset_util::parse_bookmark_name)]
49 names: Vec<RefNameBuf>,
50 }
51
52 pub async fn cmd_bookmark_create(
53 ui: &mut Ui,
54 command: &CommandHelper,
55 args: &BookmarkCreateArgs,
56 ) -> Result<(), CommandError> {
57 let mut workspace_command = command.workspace_helper(ui).await?;
58 let target_commit = workspace_command
59 .resolve_single_rev(ui, &args.revision)
60 .await?;
61 let repo = workspace_command.repo().as_ref();
62 let view = repo.view();
63 let bookmark_names = &args.names;
64 for name in bookmark_names {
65 if view.get_local_bookmark(name).is_present() {
66 return Err(user_error(format!(
67 "Bookmark already exists: {name}",
68 name = name.as_symbol()
69 ))
70 .hinted("Use `jj bookmark set` to update it."));
71 }
72 if has_tracked_remote_bookmarks(repo, name) {
73 return Err(user_error(format!(
74 "Tracked remote bookmarks exist for deleted bookmark: {name}",
75 name = name.as_symbol()
76 ))
77 .hinted(format!(
78 "Use `jj bookmark set` to recreate the local bookmark. Run `jj bookmark untrack \
79 {name}` to disassociate them.",
80 name = name.as_symbol()
81 )));
82 }
83 }
84 if target_commit.is_discardable(repo).await? {
85 writeln!(ui.warning_default(), "Target revision is empty.")?;
86 }
87
88 let mut tx = workspace_command.start_transaction();
89 let remote_settings = tx.settings().remote_settings()?;
90 let remote_auto_track_matchers =
91 revset_util::parse_remote_auto_track_bookmarks_map_for_new_bookmarks(ui, &remote_settings)?;
92 let readonly_repo = tx.base_repo().clone();
93 for name in bookmark_names {
94 tx.repo_mut()
95 .set_local_bookmark_target(name, RefTarget::normal(target_commit.id().clone()));
96 for (remote_name, matcher) in &remote_auto_track_matchers {
97 if !matcher.is_match(name.as_str()) {
98 continue;
99 }
100 let Some(view) = readonly_repo.view().get_remote_view(remote_name) else {
101 continue;
102 };
103 let symbol = name.to_remote_symbol(remote_name);
104 if view.bookmarks.contains_key(name) {
105 writeln!(
106 ui.warning_default(),
107 "Auto-tracking bookmark that exists on the remote: {symbol}"
108 )?;
109 }
110 tx.repo_mut().track_remote_bookmark(symbol).await?;
111 }
112 }
113
114 if let Some(mut formatter) = ui.status_formatter() {
115 write!(
116 formatter,
117 "Created {} bookmarks pointing to ",
118 bookmark_names.len()
119 )?;
120 tx.write_commit_summary(formatter.as_mut(), &target_commit)?;
121 writeln!(formatter)?;
122 }
123 tx.finish(
124 ui,
125 format!(
126 "create bookmark {names} pointing to commit {id}",
127 names = bookmark_names.iter().map(|n| n.as_symbol()).join(", "),
128 id = target_commit.id().hex()
129 ),
130 )
131 .await?;
132 Ok(())
133 }