| 1 |
// Copyright 2020 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::io::Write as _; |
| 17 |
|
| 18 |
use bstr::ByteVec as _; |
| 19 |
use clap_complete::ArgValueCompleter; |
| 20 |
use futures::future::try_join_all; |
| 21 |
use indexmap::IndexSet; |
| 22 |
use itertools::Itertools as _; |
| 23 |
use jj_lib::backend::CommitId; |
| 24 |
use jj_lib::repo::Repo as _; |
| 25 |
use jj_lib::rewrite::merge_commit_trees; |
| 26 |
use jj_lib::rewrite::rebase_commit; |
| 27 |
use tracing::instrument; |
| 28 |
|
| 29 |
use crate::cli_util::CommandHelper; |
| 30 |
use crate::cli_util::RevisionArg; |
| 31 |
use crate::cli_util::compute_commit_location; |
| 32 |
use crate::cli_util::merge_args_with; |
| 33 |
use crate::command_error::CommandError; |
| 34 |
use crate::complete; |
| 35 |
use crate::description_util::add_trailers; |
| 36 |
use crate::description_util::join_message_paragraphs; |
| 37 |
use crate::text_util; |
| 38 |
use crate::ui::Ui; |
| 39 |
|
| 40 |
/// Create a new, empty change and (by default) edit it in the working copy |
| 41 |
/// |
| 42 |
/// By default, `jj` will edit the new change, making the [working copy] |
| 43 |
/// represent the new commit. This can be avoided with `--no-edit`. |
| 44 |
/// |
| 45 |
/// Note that you can create a merge commit by specifying multiple revisions as |
| 46 |
/// argument. For example, `jj new @ main` will create a new commit with the |
| 47 |
/// working copy and the `main` bookmark as parents. |
| 48 |
/// |
| 49 |
/// [working copy]: |
| 50 |
/// https://docs.jj-vcs.dev/latest/working-copy/ |
| 51 |
#[derive(clap::Args, Clone, Debug)] |
| 52 |
#[command(group(clap::ArgGroup::new("revisions").multiple(true)))] |
| 53 |
pub(crate) struct NewArgs { |
| 54 |
/// Parent(s) of the new change [default: @] [aliases: -o, -r] |
| 55 |
#[arg(group = "revisions", value_name = "REVSETS")] |
| 56 |
#[arg(add = ArgValueCompleter::new(complete::revset_expression_all))] |
| 57 |
revisions_pos: Option<Vec<RevisionArg>>, |
| 58 |
|
| 59 |
#[arg( |
| 60 |
short = 'o', |
| 61 |
group = "revisions", |
| 62 |
hide = true, |
| 63 |
short_aliases = ['d', 'r'], |
| 64 |
value_name = "REVSETS", |
| 65 |
|
| 66 |
)] |
| 67 |
#[arg(add = ArgValueCompleter::new(complete::revset_expression_all))] |
| 68 |
revisions_opt: Option<Vec<RevisionArg>>, |
| 69 |
|
| 70 |
/// The change description to use |
| 71 |
#[arg(long = "message", short, value_name = "MESSAGE")] |
| 72 |
message_paragraphs: Vec<String>, |
| 73 |
|
| 74 |
/// Do not edit the newly created change |
| 75 |
#[arg(long, conflicts_with = "_edit")] |
| 76 |
no_edit: bool, |
| 77 |
|
| 78 |
/// No-op flag to pair with --no-edit |
| 79 |
#[arg(long, hide = true)] |
| 80 |
_edit: bool, |
| 81 |
|
| 82 |
/// Insert the new change after the given commit(s) |
| 83 |
/// |
| 84 |
/// Example: `jj new --insert-after A` creates a new change between `A` and |
| 85 |
/// its children: |
| 86 |
/// |
| 87 |
/// ```text |
| 88 |
/// B C |
| 89 |
/// \ / |
| 90 |
/// B C => @ |
| 91 |
/// \ / | |
| 92 |
/// A A |
| 93 |
/// ``` |
| 94 |
/// |
| 95 |
/// Specifying `--insert-after` multiple times will relocate all children of |
| 96 |
/// the given commits. |
| 97 |
/// |
| 98 |
/// Example: `jj new --insert-after A --insert-after X` creates a change |
| 99 |
/// with `A` and `X` as parents, and rebases all children on top of the new |
| 100 |
/// change: |
| 101 |
/// |
| 102 |
/// ```text |
| 103 |
/// B Y |
| 104 |
/// \ / |
| 105 |
/// B Y => @ |
| 106 |
/// | | / \ |
| 107 |
/// A X A X |
| 108 |
/// ``` |
| 109 |
#[arg( |
| 110 |
long, |
| 111 |
short = 'A', |
| 112 |
visible_alias = "after", |
| 113 |
conflicts_with = "revisions", |
| 114 |
value_name = "REVSETS", |
| 115 |
verbatim_doc_comment |
| 116 |
)] |
| 117 |
#[arg(add = ArgValueCompleter::new(complete::revset_expression_all))] |
| 118 |
insert_after: Option<Vec<RevisionArg>>, |
| 119 |
|
| 120 |
/// Insert the new change before the given commit(s) |
| 121 |
/// |
| 122 |
/// Example: `jj new --insert-before C` creates a new change between `C` and |
| 123 |
/// its parents: |
| 124 |
/// |
| 125 |
/// ```text |
| 126 |
/// C |
| 127 |
/// | |
| 128 |
/// C => @ |
| 129 |
/// / \ / \ |
| 130 |
/// A B A B |
| 131 |
/// ``` |
| 132 |
/// |
| 133 |
/// `--insert-after` and `--insert-before` can be combined. |
| 134 |
/// |
| 135 |
/// Example: `jj new --insert-after A --insert-before D`: |
| 136 |
/// |
| 137 |
/// ```text |
| 138 |
/// D D |
| 139 |
/// | / \ |
| 140 |
/// C | C |
| 141 |
/// | => @ | |
| 142 |
/// B | B |
| 143 |
/// | \ / |
| 144 |
/// A A |
| 145 |
/// ``` |
| 146 |
/// |
| 147 |
/// Similar to `--insert-after`, you can specify `--insert-before` multiple |
| 148 |
/// times. |
| 149 |
#[arg( |
| 150 |
long, |
| 151 |
short = 'B', |
| 152 |
visible_alias = "before", |
| 153 |
conflicts_with = "revisions", |
| 154 |
value_name = "REVSETS", |
| 155 |
verbatim_doc_comment |
| 156 |
)] |
| 157 |
#[arg(add = ArgValueCompleter::new(complete::revset_expression_mutable))] |
| 158 |
insert_before: Option<Vec<RevisionArg>>, |
| 159 |
} |
| 160 |
|
| 161 |
#[instrument(skip_all)] |
| 162 |
pub(crate) async fn cmd_new( |
| 163 |
ui: &mut Ui, |
| 164 |
command: &CommandHelper, |
| 165 |
args: &NewArgs, |
| 166 |
) -> Result<(), CommandError> { |
| 167 |
let mut workspace_command = command.workspace_helper(ui).await?; |
| 168 |
|
| 169 |
let revision_args = match (&args.revisions_pos, &args.revisions_opt) { |
| 170 |
(None, None) => (args.insert_before.is_none() && args.insert_after.is_none()) |
| 171 |
.then(|| vec![RevisionArg::AT]), |
| 172 |
(None, Some(args)) | (Some(args), None) => Some(args.clone()), |
| 173 |
(Some(pos), Some(opt)) => Some(merge_args_with( |
| 174 |
command.matches().subcommand_matches("new").unwrap(), |
| 175 |
&[("revisions_pos", pos), ("revisions_opt", opt)], |
| 176 |
|_id, value| value.clone(), |
| 177 |
)), |
| 178 |
}; |
| 179 |
let (parent_commit_ids, child_commit_ids) = compute_commit_location( |
| 180 |
ui, |
| 181 |
&workspace_command, |
| 182 |
revision_args.as_deref(), |
| 183 |
args.insert_after.as_deref(), |
| 184 |
args.insert_before.as_deref(), |
| 185 |
"new commit", |
| 186 |
) |
| 187 |
.await?; |
| 188 |
let parent_commits = try_join_all( |
| 189 |
parent_commit_ids |
| 190 |
.iter() |
| 191 |
.map(|commit_id| workspace_command.repo().store().get_commit_async(commit_id)), |
| 192 |
) |
| 193 |
.await?; |
| 194 |
let mut advance_bookmarks_target = None; |
| 195 |
let mut advanceable_bookmarks = vec![]; |
| 196 |
|
| 197 |
if args.insert_before.is_none() && args.insert_after.is_none() { |
| 198 |
let should_advance_bookmarks = parent_commits.len() == 1; |
| 199 |
if should_advance_bookmarks { |
| 200 |
advance_bookmarks_target = Some(parent_commit_ids[0].clone()); |
| 201 |
advanceable_bookmarks = |
| 202 |
workspace_command.get_advanceable_bookmarks(ui, parent_commits[0].parent_ids())?; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
let parent_commit_ids_set: HashSet<CommitId> = parent_commit_ids.iter().cloned().collect(); |
| 207 |
|
| 208 |
let mut tx = workspace_command.start_transaction(); |
| 209 |
let merged_tree = merge_commit_trees(tx.repo(), &parent_commits).await?; |
| 210 |
let mut commit_builder = tx |
| 211 |
.repo_mut() |
| 212 |
.new_commit(parent_commit_ids, merged_tree) |
| 213 |
.detach(); |
| 214 |
let description = if !args.message_paragraphs.is_empty() { |
| 215 |
join_message_paragraphs(&args.message_paragraphs) |
| 216 |
} else { |
| 217 |
let template_text = tx.settings().get_string("templates.new_description")?; |
| 218 |
if template_text.is_empty() { |
| 219 |
String::new() |
| 220 |
} else { |
| 221 |
let template = tx.parse_commit_template(ui, &template_text)?; |
| 222 |
let temp_commit = commit_builder.write_hidden().await?; |
| 223 |
text_util::complete_newline( |
| 224 |
template.format_plain_text(&temp_commit).into_string_lossy(), |
| 225 |
) |
| 226 |
} |
| 227 |
}; |
| 228 |
if !description.is_empty() { |
| 229 |
// The first trailer would become the first line of the description. |
| 230 |
// Also, a commit with no description is treated in a special way in jujutsu: it |
| 231 |
// can be discarded as soon as it's no longer the working copy. Adding a |
| 232 |
// trailer to an empty description would break that logic. |
| 233 |
commit_builder.set_description(&description); |
| 234 |
let description = add_trailers(ui, &tx, &commit_builder).await?; |
| 235 |
commit_builder.set_description(&description); |
| 236 |
} |
| 237 |
let new_commit = commit_builder.write(tx.repo_mut()).await?; |
| 238 |
|
| 239 |
let child_commits = try_join_all( |
| 240 |
child_commit_ids |
| 241 |
.iter() |
| 242 |
.map(|commit_id| tx.repo().store().get_commit_async(commit_id)), |
| 243 |
) |
| 244 |
.await?; |
| 245 |
let mut num_rebased = 0; |
| 246 |
for child_commit in child_commits { |
| 247 |
let mut new_parent_ids = IndexSet::new(); |
| 248 |
// If the original parents of the children commits are the parents of the new |
| 249 |
// commit, replace them with the new commit since we are "inserting" the new |
| 250 |
// commit in between the parents and the children. |
| 251 |
for old_parent_id in child_commit.parent_ids() { |
| 252 |
if parent_commit_ids_set.contains(old_parent_id) { |
| 253 |
new_parent_ids.insert(new_commit.id().clone()); |
| 254 |
} else { |
| 255 |
new_parent_ids.insert(old_parent_id.clone()); |
| 256 |
} |
| 257 |
} |
| 258 |
new_parent_ids.insert(new_commit.id().clone()); |
| 259 |
let new_parent_ids = new_parent_ids.into_iter().collect_vec(); |
| 260 |
rebase_commit(tx.repo_mut(), child_commit, new_parent_ids).await?; |
| 261 |
num_rebased += 1; |
| 262 |
} |
| 263 |
num_rebased += tx.repo_mut().rebase_descendants().await?; |
| 264 |
|
| 265 |
if args.no_edit { |
| 266 |
if let Some(mut formatter) = ui.status_formatter() { |
| 267 |
write!(formatter, "Created new commit ")?; |
| 268 |
tx.write_commit_summary(formatter.as_mut(), &new_commit)?; |
| 269 |
writeln!(formatter)?; |
| 270 |
} |
| 271 |
} else { |
| 272 |
tx.edit(&new_commit)?; |
| 273 |
// The description of the new commit will be printed by tx.finish() |
| 274 |
} |
| 275 |
if num_rebased > 0 { |
| 276 |
writeln!(ui.status(), "Rebased {num_rebased} descendant commits.")?; |
| 277 |
} |
| 278 |
|
| 279 |
// Does nothing if there's no bookmarks to advance. |
| 280 |
if let Some(target) = advance_bookmarks_target { |
| 281 |
tx.advance_bookmarks(advanceable_bookmarks, &target).await?; |
| 282 |
} |
| 283 |
|
| 284 |
tx.finish(ui, "new empty commit").await?; |
| 285 |
Ok(()) |
| 286 |
} |