| 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::io::Write as _; |
| 16 |
|
| 17 |
use bstr::ByteVec as _; |
| 18 |
use clap_complete::ArgValueCompleter; |
| 19 |
use futures::TryStreamExt as _; |
| 20 |
use itertools::Itertools as _; |
| 21 |
use jj_lib::backend::BackendResult; |
| 22 |
use jj_lib::backend::CommitId; |
| 23 |
use jj_lib::repo::Repo as _; |
| 24 |
use jj_lib::rewrite::DuplicateCommitsStats; |
| 25 |
use jj_lib::rewrite::duplicate_commits; |
| 26 |
use jj_lib::rewrite::duplicate_commits_onto_parents; |
| 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::short_commit_hash; |
| 33 |
use crate::command_error::CommandError; |
| 34 |
use crate::command_error::user_error; |
| 35 |
use crate::complete; |
| 36 |
use crate::text_util; |
| 37 |
use crate::ui::Ui; |
| 38 |
|
| 39 |
/// Create new changes with the same content as existing ones |
| 40 |
/// |
| 41 |
/// When none of the `--onto`, `--insert-after`, or `--insert-before` arguments |
| 42 |
/// are provided, commits will be duplicated onto their existing parents or onto |
| 43 |
/// other newly duplicated commits. |
| 44 |
/// |
| 45 |
/// When any of the `--onto`, `--insert-after`, or `--insert-before` arguments |
| 46 |
/// are provided, the roots of the specified commits will be duplicated onto the |
| 47 |
/// destination indicated by the arguments. Other specified commits will be |
| 48 |
/// duplicated onto these newly duplicated commits. If the `--insert-after` or |
| 49 |
/// `--insert-before` arguments are provided, the new children indicated by the |
| 50 |
/// arguments will be rebased onto the heads of the specified commits. |
| 51 |
/// |
| 52 |
/// By default, the duplicated commits retain the descriptions of the originals. |
| 53 |
/// This can be customized with the `templates.duplicate_description` setting. |
| 54 |
#[derive(clap::Args, Clone, Debug)] |
| 55 |
pub(crate) struct DuplicateArgs { |
| 56 |
/// The revision(s) to duplicate (default: @) [aliases: -r] |
| 57 |
#[arg(value_name = "REVSETS")] |
| 58 |
#[arg(add = ArgValueCompleter::new(complete::revset_expression_all))] |
| 59 |
revisions_pos: Vec<RevisionArg>, |
| 60 |
|
| 61 |
#[arg(short = 'r', hide = true, value_name = "REVSETS")] |
| 62 |
#[arg(add = ArgValueCompleter::new(complete::revset_expression_all))] |
| 63 |
revisions_opt: Vec<RevisionArg>, |
| 64 |
|
| 65 |
/// The revision(s) to duplicate onto (can be repeated to create a merge |
| 66 |
/// commit) |
| 67 |
#[arg( |
| 68 |
long, |
| 69 |
visible_alias = "destination", |
| 70 |
short, |
| 71 |
visible_short_alias = 'd', |
| 72 |
value_name = "REVSETS" |
| 73 |
)] |
| 74 |
#[arg(add = ArgValueCompleter::new(complete::revset_expression_all))] |
| 75 |
onto: Option<Vec<RevisionArg>>, |
| 76 |
|
| 77 |
/// The revision(s) to insert after (can be repeated to create a merge |
| 78 |
/// commit) |
| 79 |
#[arg( |
| 80 |
long, |
| 81 |
short = 'A', |
| 82 |
visible_alias = "after", |
| 83 |
conflicts_with = "onto", |
| 84 |
value_name = "REVSETS" |
| 85 |
)] |
| 86 |
#[arg(add = ArgValueCompleter::new(complete::revset_expression_all))] |
| 87 |
insert_after: Option<Vec<RevisionArg>>, |
| 88 |
|
| 89 |
/// The revision(s) to insert before (can be repeated to create a merge |
| 90 |
/// commit) |
| 91 |
#[arg( |
| 92 |
long, |
| 93 |
short = 'B', |
| 94 |
visible_alias = "before", |
| 95 |
conflicts_with = "onto", |
| 96 |
value_name = "REVSETS" |
| 97 |
)] |
| 98 |
#[arg(add = ArgValueCompleter::new(complete::revset_expression_mutable))] |
| 99 |
insert_before: Option<Vec<RevisionArg>>, |
| 100 |
} |
| 101 |
|
| 102 |
#[instrument(skip_all)] |
| 103 |
pub(crate) async fn cmd_duplicate( |
| 104 |
ui: &mut Ui, |
| 105 |
command: &CommandHelper, |
| 106 |
args: &DuplicateArgs, |
| 107 |
) -> Result<(), CommandError> { |
| 108 |
let mut workspace_command = command.workspace_helper(ui).await?; |
| 109 |
let to_duplicate: Vec<CommitId> = |
| 110 |
if !args.revisions_pos.is_empty() || !args.revisions_opt.is_empty() { |
| 111 |
workspace_command |
| 112 |
.parse_union_revsets(ui, &[&*args.revisions_pos, &*args.revisions_opt].concat())? |
| 113 |
} else { |
| 114 |
workspace_command.parse_revset(ui, &RevisionArg::AT)? |
| 115 |
} |
| 116 |
.evaluate_to_commit_ids()? |
| 117 |
.try_collect() |
| 118 |
.await?; // in reverse topological order |
| 119 |
if to_duplicate.is_empty() { |
| 120 |
writeln!(ui.status(), "No revisions to duplicate.")?; |
| 121 |
return Ok(()); |
| 122 |
} |
| 123 |
if to_duplicate.last() == Some(workspace_command.repo().store().root_commit_id()) { |
| 124 |
return Err(user_error("Cannot duplicate the root commit")); |
| 125 |
} |
| 126 |
|
| 127 |
let location = |
| 128 |
if args.onto.is_none() && args.insert_after.is_none() && args.insert_before.is_none() { |
| 129 |
None |
| 130 |
} else { |
| 131 |
Some( |
| 132 |
compute_commit_location( |
| 133 |
ui, |
| 134 |
&workspace_command, |
| 135 |
args.onto.as_deref(), |
| 136 |
args.insert_after.as_deref(), |
| 137 |
args.insert_before.as_deref(), |
| 138 |
"duplicated commits", |
| 139 |
) |
| 140 |
.await?, |
| 141 |
) |
| 142 |
}; |
| 143 |
|
| 144 |
let mut tx = workspace_command.start_transaction(); |
| 145 |
|
| 146 |
if let Some((parent_commit_ids, children_commit_ids)) = &location |
| 147 |
&& !parent_commit_ids.is_empty() |
| 148 |
{ |
| 149 |
let index = tx.repo().index(); |
| 150 |
for commit_id in &to_duplicate { |
| 151 |
for parent_commit_id in parent_commit_ids { |
| 152 |
if index.is_ancestor(commit_id, parent_commit_id).await? { |
| 153 |
writeln!( |
| 154 |
ui.warning_default(), |
| 155 |
"Duplicating commit {} as a descendant of itself.", |
| 156 |
short_commit_hash(commit_id) |
| 157 |
)?; |
| 158 |
break; |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
for commit_id in &to_duplicate { |
| 164 |
for child_commit_id in children_commit_ids { |
| 165 |
if index.is_ancestor(child_commit_id, commit_id).await? { |
| 166 |
writeln!( |
| 167 |
ui.warning_default(), |
| 168 |
"Duplicating commit {} as an ancestor of itself.", |
| 169 |
short_commit_hash(commit_id) |
| 170 |
)?; |
| 171 |
break; |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
let new_descs = { |
| 178 |
let template = tx |
| 179 |
.settings() |
| 180 |
.get_string("templates.duplicate_description")?; |
| 181 |
let parsed = tx.parse_commit_template(ui, &template)?; |
| 182 |
|
| 183 |
to_duplicate |
| 184 |
.iter() |
| 185 |
.map(|commit_id| -> BackendResult<_> { |
| 186 |
let commit = tx.repo().store().get_commit(commit_id)?; |
| 187 |
let output = parsed.format_plain_text(&commit); |
| 188 |
let output = text_util::complete_newline(output.into_string_lossy()); |
| 189 |
Ok((commit_id.clone(), output)) |
| 190 |
}) |
| 191 |
.try_collect()? |
| 192 |
}; |
| 193 |
|
| 194 |
let num_to_duplicate = to_duplicate.len(); |
| 195 |
let DuplicateCommitsStats { |
| 196 |
duplicated_commits, |
| 197 |
num_rebased, |
| 198 |
} = if let Some((parent_commit_ids, children_commit_ids)) = location { |
| 199 |
duplicate_commits( |
| 200 |
tx.repo_mut(), |
| 201 |
&to_duplicate, |
| 202 |
&new_descs, |
| 203 |
&parent_commit_ids, |
| 204 |
&children_commit_ids, |
| 205 |
) |
| 206 |
.await? |
| 207 |
} else { |
| 208 |
duplicate_commits_onto_parents(tx.repo_mut(), &to_duplicate, &new_descs).await? |
| 209 |
}; |
| 210 |
|
| 211 |
if let Some(mut formatter) = ui.status_formatter() { |
| 212 |
for (old_id, new_commit) in &duplicated_commits { |
| 213 |
write!(formatter, "Duplicated {} as ", short_commit_hash(old_id))?; |
| 214 |
tx.write_commit_summary(formatter.as_mut(), new_commit)?; |
| 215 |
writeln!(formatter)?; |
| 216 |
} |
| 217 |
if num_rebased > 0 { |
| 218 |
writeln!( |
| 219 |
ui.status(), |
| 220 |
"Rebased {num_rebased} commits onto duplicated commits." |
| 221 |
)?; |
| 222 |
} |
| 223 |
} |
| 224 |
tx.finish(ui, format!("duplicate {num_to_duplicate} commit(s)")) |
| 225 |
.await?; |
| 226 |
Ok(()) |
| 227 |
} |