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.".
This commit is contained in:
Martin von Zweigbergk 2021-12-01 08:45:09 -08:00
parent a27e77205a
commit 0c441d9558
3 changed files with 15 additions and 1 deletions

View File

@ -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)
}

View File

@ -28,6 +28,7 @@ pub enum RefName {
GitRef(String),
}
#[derive(PartialEq, Eq, Debug)]
pub struct View {
data: op_store::View,
}

View File

@ -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 {