Debian Codegraph @

cli/src/commands/simplify_parents.rs

raw
1 use std::collections::HashSet;
2
3 use clap_complete::ArgValueCompleter;
4 use futures::TryStreamExt as _;
5 use jj_lib::backend::BackendError;
6
7 use crate::cli_util::CommandHelper;
8 use crate::cli_util::RevisionArg;
9 use crate::command_error::CommandError;
10 use crate::complete;
11 use crate::ui::Ui;
12
13 /// Simplify parent edges for the specified revision(s).
14 ///
15 /// Removes all parents of each of the specified revisions that are also
16 /// indirect ancestors of the same revisions through other parents. This has no
17 /// effect on any revision's contents, including the working copy.
18 ///
19 /// In other words, for all (A, B, C) where A has (B, C) as parents and C is an
20 /// ancestor of B, A will be rewritten to have only B as a parent instead of
21 /// B+C.
22 #[derive(clap::Args, Clone, Debug)]
23 pub(crate) struct SimplifyParentsArgs {
24 /// Simplify specified revision(s) together with their trees of descendants
25 /// (can be repeated)
26 #[arg(long, short, value_name = "REVSETS")]
27 #[arg(add = ArgValueCompleter::new(complete::revset_expression_mutable))]
28 source: Vec<RevisionArg>,
29
30 /// Simplify specified revision(s) (can be repeated)
31 ///
32 /// If both `--source` and `--revisions` are not provided, this defaults to
33 /// the `revsets.simplify-parents` setting, or `reachable(@, mutable())`
34 /// if it is not set.
35 #[arg(long = "revision", short, value_name = "REVSETS", alias = "revisions")]
36 #[arg(add = ArgValueCompleter::new(complete::revset_expression_mutable))]
37 revisions: Vec<RevisionArg>,
38 }
39
40 pub(crate) async fn cmd_simplify_parents(
41 ui: &mut Ui,
42 command: &CommandHelper,
43 args: &SimplifyParentsArgs,
44 ) -> Result<(), CommandError> {
45 let mut workspace_command = command.workspace_helper(ui).await?;
46 let revs = if args.source.is_empty() && args.revisions.is_empty() {
47 let revs = workspace_command
48 .settings()
49 .get_string("revsets.simplify-parents")?;
50 workspace_command
51 .parse_revset(ui, &RevisionArg::from(revs))?
52 .resolve()?
53 } else {
54 workspace_command
55 .parse_union_revsets(ui, &args.source)?
56 .resolve()?
57 .descendants()
58 .union(
59 &workspace_command
60 .parse_union_revsets(ui, &args.revisions)?
61 .resolve()?,
62 )
63 };
64 workspace_command.check_rewritable_expr(&revs).await?;
65 let commit_ids: Vec<_> = revs
66 .evaluate(workspace_command.repo().as_ref())?
67 .stream()
68 .try_collect()
69 .await?;
70 let commit_ids_set: HashSet<_> = commit_ids.iter().cloned().collect();
71 let num_orig_commits = commit_ids.len();
72
73 let mut tx = workspace_command.start_transaction();
74 let mut simplified_commits = 0;
75 let mut edges = 0;
76 let mut reparented_descendants = 0;
77
78 tx.repo_mut()
79 .transform_descendants(commit_ids, async |mut rewriter| {
80 let num_old_heads = rewriter.new_parents().len();
81 if commit_ids_set.contains(rewriter.old_commit().id()) && num_old_heads > 1 {
82 // TODO: BackendError is not the right error here because
83 // the error does not come from `Backend`, but `Index`.
84 rewriter
85 .simplify_ancestor_merge()
86 .await
87 .map_err(|err| BackendError::Other(err.into()))?;
88 }
89 let num_new_heads = rewriter.new_parents().len();
90
91 if rewriter.parents_changed() {
92 rewriter.reparent().write().await?;
93
94 if num_new_heads < num_old_heads {
95 simplified_commits += 1;
96 edges += num_old_heads - num_new_heads;
97 } else {
98 reparented_descendants += 1;
99 }
100 }
101 Ok(())
102 })
103 .await?;
104
105 if let Some(mut formatter) = ui.status_formatter()
106 && simplified_commits > 0
107 {
108 writeln!(
109 formatter,
110 "Removed {edges} edges from {simplified_commits} out of {num_orig_commits} commits.",
111 )?;
112 if reparented_descendants > 0 {
113 writeln!(
114 formatter,
115 "Rebased {reparented_descendants} descendant commits.",
116 )?;
117 }
118 }
119 tx.finish(ui, format!("simplify {num_orig_commits} commits"))
120 .await?;
121
122 Ok(())
123 }