From 0c441d9558a1fd48756b8fd8f05f10f2743ab0a1 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Wed, 1 Dec 2021 08:45:09 -0800 Subject: [PATCH] cli: don't commit no-op transaction If nothing changed in a transaction, it's rarely useful to commit it, so let's avoid that. For example, if you run `jj git import` without changing the anything in the Git repo, we now just print "Nothing changed.". --- lib/src/repo.rs | 4 ++++ lib/src/view.rs | 1 + src/commands.rs | 11 ++++++++++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/src/repo.rs b/lib/src/repo.rs index 8de255b8b..7a4b7c58b 100644 --- a/lib/src/repo.rs +++ b/lib/src/repo.rs @@ -456,6 +456,10 @@ impl MutableRepo { &self.view } + pub fn has_changes(&self) -> bool { + self.view != self.base_repo.view + } + pub fn consume(self) -> (MutableIndex, View) { (self.index, self.view) } diff --git a/lib/src/view.rs b/lib/src/view.rs index a613202a2..5798f02d4 100644 --- a/lib/src/view.rs +++ b/lib/src/view.rs @@ -28,6 +28,7 @@ pub enum RefName { GitRef(String), } +#[derive(PartialEq, Eq, Debug)] pub struct View { data: op_store::View, } diff --git a/src/commands.rs b/src/commands.rs index 1c0c9221d..2255ac59b 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -441,6 +441,11 @@ impl WorkspaceCommandHelper { fn finish_transaction(&mut self, ui: &mut Ui, mut tx: Transaction) -> Result<(), CommandError> { let mut_repo = tx.mut_repo(); + if !mut_repo.has_changes() { + tx.discard(); + writeln!(ui, "Nothing changed.")?; + return Ok(()); + } if self.rebase_descendants { let num_rebased = rebase_descendants(ui.settings(), mut_repo); if num_rebased > 0 { @@ -1390,7 +1395,11 @@ fn cmd_init(ui: &mut Ui, command: &CommandHelper, args: &ArgMatches) -> Result<( git::import_refs(tx.mut_repo(), &git_repo).unwrap(); // TODO: Check out a recent commit. Maybe one with the highest generation // number. - workspace_command.finish_transaction(ui, tx)?; + if tx.mut_repo().has_changes() { + workspace_command.finish_transaction(ui, tx)?; + } else { + tx.discard(); + } } else if args.is_present("git") { Workspace::init_internal_git(ui.settings(), wc_path.clone())?; } else {