Skip to content

Commit

Permalink
cli: proxy rewrite_commit in WorkspaceCommandTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
scott2000 committed Jun 21, 2024
1 parent 3db183b commit 385a066
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 33 deletions.
3 changes: 1 addition & 2 deletions cli/examples/custom-command/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ fn run_custom_command(
let commit = workspace_command.resolve_single_rev(&args.revision)?;
let mut tx = workspace_command.start_transaction();
let new_commit = tx
.mut_repo()
.rewrite_commit(command_helper.settings(), &commit)
.rewrite_edited_commit(&commit)?
.set_description("Frobnicated!")
.write()?;
tx.finish(ui, "Frobnicate")?;
Expand Down
9 changes: 9 additions & 0 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use indexmap::{IndexMap, IndexSet};
use itertools::Itertools;
use jj_lib::backend::{ChangeId, CommitId, MergedTreeId, TreeValue};
use jj_lib::commit::Commit;
use jj_lib::commit_builder::CommitBuilder;
use jj_lib::fileset::FilesetExpression;
use jj_lib::git_backend::GitBackend;
use jj_lib::gitignore::{GitIgnoreError, GitIgnoreFile};
Expand Down Expand Up @@ -1625,6 +1626,14 @@ impl WorkspaceCommandTransaction<'_> {
self.tx.mut_repo().edit(workspace_id, commit)
}

pub fn rewrite_edited_commit(
&mut self,
commit: &Commit,
) -> Result<CommitBuilder, CommandError> {
let settings = &self.helper.settings;
Ok(self.tx.mut_repo().rewrite_commit(settings, commit))
}

pub fn format_commit_summary(&self, commit: &Commit) -> String {
let mut output = Vec::new();
self.write_commit_summary(&mut PlainTextFormatter::new(&mut output), commit)
Expand Down
3 changes: 1 addition & 2 deletions cli/src/commands/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ new working-copy commit.
};

let new_commit = tx
.mut_repo()
.rewrite_commit(command.settings(), &commit)
.rewrite_edited_commit(&commit)?
.set_tree_id(tree_id)
.set_description(description)
.write()?;
Expand Down
3 changes: 1 addition & 2 deletions cli/src/commands/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ pub(crate) fn cmd_describe(
} else {
let mut tx = workspace_command.start_transaction();
let mut commit_builder = tx
.mut_repo()
.rewrite_commit(command.settings(), &commit)
.rewrite_edited_commit(&commit)?
.set_description(description);
if args.reset_author {
let new_author = commit_builder.committer().clone();
Expand Down
5 changes: 2 additions & 3 deletions cli/src/commands/diffedit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,8 @@ don't make any changes, then the operation will be aborted.",
if tree_id == *target_commit.tree_id() {
writeln!(ui.status(), "Nothing changed.")?;
} else {
let mut_repo = tx.mut_repo();
let new_commit = mut_repo
.rewrite_commit(command.settings(), &target_commit)
let new_commit = tx
.rewrite_edited_commit(&target_commit)?
.set_tree_id(tree_id)
.write()?;
// rebase_descendants early; otherwise `new_commit` would always have
Expand Down
5 changes: 2 additions & 3 deletions cli/src/commands/duplicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ pub(crate) fn cmd_duplicate(
let mut tx = workspace_command.start_transaction();
let base_repo = tx.base_repo().clone();
let store = base_repo.store();
let mut_repo = tx.mut_repo();

for original_commit_id in to_duplicate.iter().rev() {
// Topological order ensures that any parents of `original_commit` are
Expand All @@ -69,8 +68,8 @@ pub(crate) fn cmd_duplicate(
.iter()
.map(|id| duplicated_old_to_new.get(id).map_or(id, |c| c.id()).clone())
.collect();
let new_commit = mut_repo
.rewrite_commit(command.settings(), &original_commit)
let new_commit = tx
.rewrite_edited_commit(&original_commit)?
.generate_new_change_id()
.set_parents(new_parents)
.write()?;
Expand Down
3 changes: 1 addition & 2 deletions cli/src/commands/file/chmod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ pub(crate) fn cmd_chmod(
}

let new_tree_id = tree_builder.write_tree(store)?;
tx.mut_repo()
.rewrite_commit(command.settings(), &commit)
tx.rewrite_edited_commit(&commit)?
.set_tree_id(new_tree_id)
.write()?;
tx.finish(
Expand Down
3 changes: 1 addition & 2 deletions cli/src/commands/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ pub(crate) fn cmd_resolve(
let mut tx = workspace_command.start_transaction();
let new_tree_id = merge_editor.edit_file(&tree, repo_path)?;
let new_commit = tx
.mut_repo()
.rewrite_commit(command.settings(), &commit)
.rewrite_edited_commit(&commit)?
.set_tree_id(new_tree_id)
.write()?;
tx.finish(
Expand Down
5 changes: 2 additions & 3 deletions cli/src/commands/restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,8 @@ pub(crate) fn cmd_restore(
writeln!(ui.status(), "Nothing changed.")?;
} else {
let mut tx = workspace_command.start_transaction();
let mut_repo = tx.mut_repo();
let new_commit = mut_repo
.rewrite_commit(command.settings(), &to_commit)
let new_commit = tx
.rewrite_edited_commit(&to_commit)?
.set_tree_id(new_tree_id)
.write()?;
// rebase_descendants early; otherwise `new_commit` would always have
Expand Down
6 changes: 2 additions & 4 deletions cli/src/commands/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ the operation will be aborted.
)?;
let first_description = edit_description(tx.base_repo(), &first_template, command.settings())?;
let first_commit = tx
.mut_repo()
.rewrite_commit(command.settings(), &commit)
.rewrite_edited_commit(&commit)?
.set_tree_id(selected_tree_id)
.set_description(first_description)
.write()?;
Expand Down Expand Up @@ -168,8 +167,7 @@ the operation will be aborted.
edit_description(tx.base_repo(), &second_template, command.settings())?
};
let second_commit = tx
.mut_repo()
.rewrite_commit(command.settings(), &commit)
.rewrite_edited_commit(&commit)?
.set_parents(second_commit_parents)
.set_tree_id(second_tree.id())
// Generate a new change id so that the commit being split doesn't
Expand Down
6 changes: 2 additions & 4 deletions cli/src/commands/squash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,7 @@ from the source will be moved into the destination.
let source_tree = source.commit.tree()?;
// Apply the reverse of the selected changes onto the source
let new_source_tree = source_tree.merge(&source.selected_tree, &source.parent_tree)?;
tx.mut_repo()
.rewrite_commit(settings, source.commit)
tx.rewrite_edited_commit(source.commit)?
.set_tree_id(new_source_tree.id().clone())
.write()?;
}
Expand Down Expand Up @@ -299,8 +298,7 @@ from the source will be moved into the destination.
.iter()
.map(|source| source.commit.id().clone()),
);
tx.mut_repo()
.rewrite_commit(settings, &rewritten_destination)
tx.rewrite_edited_commit(&rewritten_destination)?
.set_tree_id(destination_tree.id().clone())
.set_predecessors(predecessors)
.set_description(description)
Expand Down
9 changes: 3 additions & 6 deletions cli/src/commands/unsquash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,18 @@ aborted.
let description =
combine_messages(tx.base_repo(), &[&parent], &commit, command.settings())?;
// Commit the new child on top of the parent's parents.
tx.mut_repo()
.rewrite_commit(command.settings(), &commit)
tx.rewrite_edited_commit(&commit)?
.set_parents(parent.parent_ids().to_vec())
.set_description(description)
.write()?;
} else {
let new_parent = tx
.mut_repo()
.rewrite_commit(command.settings(), &parent)
.rewrite_edited_commit(&parent)?
.set_tree_id(new_parent_tree_id)
.set_predecessors(vec![parent.id().clone(), commit.id().clone()])
.write()?;
// Commit the new child on top of the new parent.
tx.mut_repo()
.rewrite_commit(command.settings(), &commit)
tx.rewrite_edited_commit(&commit)?
.set_parents(vec![new_parent.id().clone()])
.write()?;
}
Expand Down

0 comments on commit 385a066

Please sign in to comment.