just playing with tangled
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
15use std::io::Write;
16
17use jj_lib::matchers::EverythingMatcher;
18use jj_lib::object_id::ObjectId;
19use jj_lib::rewrite::merge_commit_trees;
20use tracing::instrument;
21
22use crate::cli_util::{CommandHelper, RevisionArg};
23use crate::command_error::CommandError;
24use crate::ui::Ui;
25
26/// Touch up the content changes in a revision with a diff editor
27///
28/// With the `-r` option, which is the default, starts a [diff editor] on the
29/// changes in the revision.
30///
31/// With the `--from` and/or `--to` options, starts a [diff editor] comparing
32/// the "from" revision to the "to" revision.
33///
34/// [diff editor]:
35/// https://martinvonz.github.io/jj/latest/config/#editing-diffs
36///
37/// Edit the right side of the diff until it looks the way you want. Once you
38/// close the editor, the revision specified with `-r` or `--to` will be
39/// updated. Descendants will be rebased on top as usual, which may result in
40/// conflicts.
41///
42/// See `jj restore` if you want to move entire files from one revision to
43/// another. See `jj squash -i` or `jj unsquash -i` if you instead want to move
44/// changes into or out of the parent revision.
45#[derive(clap::Args, Clone, Debug)]
46pub(crate) struct DiffeditArgs {
47 /// The revision to touch up. Defaults to @ if neither --to nor --from are
48 /// specified.
49 #[arg(long, short)]
50 revision: Option<RevisionArg>,
51 /// Show changes from this revision. Defaults to @ if --to is specified.
52 #[arg(long, conflicts_with = "revision")]
53 from: Option<RevisionArg>,
54 /// Edit changes in this revision. Defaults to @ if --from is specified.
55 #[arg(long, conflicts_with = "revision")]
56 to: Option<RevisionArg>,
57 /// Specify diff editor to be used
58 #[arg(long, value_name = "NAME")]
59 tool: Option<String>,
60}
61
62#[instrument(skip_all)]
63pub(crate) fn cmd_diffedit(
64 ui: &mut Ui,
65 command: &CommandHelper,
66 args: &DiffeditArgs,
67) -> Result<(), CommandError> {
68 let mut workspace_command = command.workspace_helper(ui)?;
69
70 let (target_commit, base_commits, diff_description);
71 if args.from.is_some() || args.to.is_some() {
72 target_commit =
73 workspace_command.resolve_single_rev(args.to.as_ref().unwrap_or(&RevisionArg::AT))?;
74 base_commits =
75 vec![workspace_command
76 .resolve_single_rev(args.from.as_ref().unwrap_or(&RevisionArg::AT))?];
77 diff_description = format!(
78 "The diff initially shows the commit's changes relative to:\n{}",
79 workspace_command.format_commit_summary(&base_commits[0])
80 );
81 } else {
82 target_commit = workspace_command
83 .resolve_single_rev(args.revision.as_ref().unwrap_or(&RevisionArg::AT))?;
84 base_commits = target_commit.parents();
85 diff_description = "The diff initially shows the commit's changes.".to_string();
86 };
87 workspace_command.check_rewritable([target_commit.id()])?;
88
89 let diff_editor = workspace_command.diff_editor(ui, args.tool.as_deref())?;
90 let mut tx = workspace_command.start_transaction();
91 let instructions = format!(
92 "\
93You are editing changes in: {}
94
95{diff_description}
96
97Adjust the right side until it shows the contents you want. If you
98don't make any changes, then the operation will be aborted.",
99 tx.format_commit_summary(&target_commit),
100 );
101 let base_tree = merge_commit_trees(tx.repo(), base_commits.as_slice())?;
102 let tree = target_commit.tree()?;
103 let tree_id = diff_editor.edit(&base_tree, &tree, &EverythingMatcher, Some(&instructions))?;
104 if tree_id == *target_commit.tree_id() {
105 writeln!(ui.status(), "Nothing changed.")?;
106 } else {
107 let mut_repo = tx.mut_repo();
108 let new_commit = mut_repo
109 .rewrite_commit(command.settings(), &target_commit)
110 .set_tree_id(tree_id)
111 .write()?;
112 // rebase_descendants early; otherwise `new_commit` would always have
113 // a conflicted change id at this point.
114 let num_rebased = tx.mut_repo().rebase_descendants(command.settings())?;
115 if let Some(mut formatter) = ui.status_formatter() {
116 write!(formatter, "Created ")?;
117 tx.write_commit_summary(formatter.as_mut(), &new_commit)?;
118 writeln!(formatter)?;
119 if num_rebased > 0 {
120 writeln!(formatter, "Rebased {num_rebased} descendant commits")?;
121 }
122 }
123 tx.finish(ui, format!("edit commit {}", target_commit.id().hex()))?;
124 }
125 Ok(())
126}