ok/jj
1
0
Fork 0
forked from mirrors/jj

errors: fix some easy error propagation in commands.rs

This is actually enough to fix #248, but I'll continue to work on
error handling for a while. I'd like to at least include the bad
object ID in this type of error messages.

Closes #248.
This commit is contained in:
Martin von Zweigbergk 2022-04-28 22:48:29 -07:00 committed by Martin von Zweigbergk
parent 0719dae975
commit d42c71f48d

View file

@ -400,7 +400,7 @@ impl WorkspaceCommandHelper {
fn start_working_copy_mutation(&mut self) -> Result<(LockedWorkingCopy, Commit), CommandError> { fn start_working_copy_mutation(&mut self) -> Result<(LockedWorkingCopy, Commit), CommandError> {
let current_checkout_id = self.repo.view().get_checkout(&self.workspace_id()); let current_checkout_id = self.repo.view().get_checkout(&self.workspace_id());
let current_checkout = if let Some(current_checkout_id) = current_checkout_id { let current_checkout = if let Some(current_checkout_id) = current_checkout_id {
self.repo.store().get_commit(current_checkout_id).unwrap() self.repo.store().get_commit(current_checkout_id)?
} else { } else {
return Err(CommandError::UserError( return Err(CommandError::UserError(
"Nothing checked out in this workspace".to_string(), "Nothing checked out in this workspace".to_string(),
@ -586,7 +586,7 @@ impl WorkspaceCommandHelper {
// doesn't, but we'll need to reload the repo so the new commit is // doesn't, but we'll need to reload the repo so the new commit is
// in the index and view, and so we don't cause unnecessary // in the index and view, and so we don't cause unnecessary
// divergence. // divergence.
let checkout_commit = repo.store().get_commit(&checkout_id).unwrap(); let checkout_commit = repo.store().get_commit(&checkout_id)?;
let wc_tree_id = locked_wc.old_tree_id().clone(); let wc_tree_id = locked_wc.old_tree_id().clone();
if *checkout_commit.tree_id() != wc_tree_id { if *checkout_commit.tree_id() != wc_tree_id {
let wc_operation_data = self let wc_operation_data = self
@ -769,7 +769,8 @@ impl WorkspaceCommandHelper {
.base_repo() .base_repo()
.view() .view()
.get_checkout(&self.workspace_id()) .get_checkout(&self.workspace_id())
.map(|commit_id| store.get_commit(commit_id).unwrap()); .map(|commit_id| store.get_commit(commit_id))
.transpose()?;
self.repo = tx.commit(); self.repo = tx.commit();
if self.may_update_working_copy { if self.may_update_working_copy {
let stats = update_working_copy( let stats = update_working_copy(
@ -969,7 +970,7 @@ fn update_working_copy(
return Ok(None); return Ok(None);
} }
}; };
let new_commit = repo.store().get_commit(new_commit_id).unwrap(); let new_commit = repo.store().get_commit(new_commit_id)?;
let old_tree_id = old_commit.map(|commit| commit.tree_id().clone()); let old_tree_id = old_commit.map(|commit| commit.tree_id().clone());
let stats = if Some(new_commit.tree_id()) != old_tree_id.as_ref() { let stats = if Some(new_commit.tree_id()) != old_tree_id.as_ref() {
// TODO: CheckoutError::ConcurrentCheckout should probably just result in a // TODO: CheckoutError::ConcurrentCheckout should probably just result in a
@ -2803,7 +2804,9 @@ fn cmd_status(
workspace_command.maybe_commit_working_copy(ui)?; workspace_command.maybe_commit_working_copy(ui)?;
let repo = workspace_command.repo(); let repo = workspace_command.repo();
let maybe_checkout_id = repo.view().get_checkout(&workspace_command.workspace_id()); let maybe_checkout_id = repo.view().get_checkout(&workspace_command.workspace_id());
let maybe_checkout = maybe_checkout_id.map(|id| repo.store().get_commit(id).unwrap()); let maybe_checkout = maybe_checkout_id
.map(|id| repo.store().get_commit(id))
.transpose()?;
if let Some(checkout_commit) = &maybe_checkout { if let Some(checkout_commit) = &maybe_checkout {
ui.write("Parent commit: ")?; ui.write("Parent commit: ")?;
let workspace_id = workspace_command.workspace_id(); let workspace_id = workspace_command.workspace_id();
@ -2973,7 +2976,7 @@ fn cmd_log(ui: &mut Ui, command: &CommandHelper, args: &LogArgs) -> Result<(), C
} }
let mut buffer = vec![]; let mut buffer = vec![];
let commit_id = index_entry.commit_id(); let commit_id = index_entry.commit_id();
let commit = store.get_commit(&commit_id).unwrap(); let commit = store.get_commit(&commit_id)?;
let is_checkout = Some(&commit_id) == checkout_id; let is_checkout = Some(&commit_id) == checkout_id;
{ {
let writer = Box::new(&mut buffer); let writer = Box::new(&mut buffer);
@ -3004,7 +3007,7 @@ fn cmd_log(ui: &mut Ui, command: &CommandHelper, args: &LogArgs) -> Result<(), C
} }
} else { } else {
for index_entry in revset.iter() { for index_entry in revset.iter() {
let commit = store.get_commit(&index_entry.commit_id()).unwrap(); let commit = store.get_commit(&index_entry.commit_id())?;
template.format(&commit, formatter)?; template.format(&commit, formatter)?;
// TODO: should --summary (without --patch) show diff summary as in hg log // TODO: should --summary (without --patch) show diff summary as in hg log
// --stat? // --stat?
@ -3368,10 +3371,7 @@ from the source will be moved into the destination.
let mut rebaser = mut_repo.create_descendant_rebaser(ui.settings()); let mut rebaser = mut_repo.create_descendant_rebaser(ui.settings());
rebaser.rebase_all(); rebaser.rebase_all();
let rebased_destination_id = rebaser.rebased().get(destination.id()).unwrap().clone(); let rebased_destination_id = rebaser.rebased().get(destination.id()).unwrap().clone();
destination = mut_repo destination = mut_repo.store().get_commit(&rebased_destination_id)?;
.store()
.get_commit(&rebased_destination_id)
.unwrap();
} }
// Apply the selected changes onto the destination // Apply the selected changes onto the destination
let new_destination_tree_id = merge_trees(&destination.tree(), &parent_tree, &new_parent_tree)?; let new_destination_tree_id = merge_trees(&destination.tree(), &parent_tree, &new_parent_tree)?;
@ -4463,8 +4463,7 @@ fn cmd_workspace_add(
new_workspace_command new_workspace_command
.repo() .repo()
.store() .store()
.get_commit(old_checkout_id) .get_commit(old_checkout_id)?
.unwrap()
.parents()[0] .parents()[0]
.clone() .clone()
} else { } else {