Skip to content

Commit

Permalink
git_backend: classify "merge with root" as user error
Browse files Browse the repository at this point in the history
Perhaps, there will be more error types that hold BackendError internally, but
this change is good enough to handle a merge error.
  • Loading branch information
yuja committed Mar 30, 2024
1 parent 91ff1fd commit f20004f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
11 changes: 9 additions & 2 deletions cli/src/command_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,10 @@ impl From<CheckOutCommitError> for CommandError {

impl From<BackendError> for CommandError {
fn from(err: BackendError) -> Self {
internal_error_with_message("Unexpected error from backend", err)
match &err {
BackendError::Unsupported(_) => user_error(err),
_ => internal_error_with_message("Unexpected error from backend", err),
}
}
}

Expand Down Expand Up @@ -305,7 +308,11 @@ want this file to be snapshotted. Otherwise add it to your `.gitignore` file."#,

impl From<TreeMergeError> for CommandError {
fn from(err: TreeMergeError) -> Self {
internal_error_with_message("Merge failed", err)
let kind = match &err {
TreeMergeError::BackendError(BackendError::Unsupported(_)) => CommandErrorKind::User,
_ => CommandErrorKind::Internal,
};
CommandError::with_message(kind, "Merge failed", err)
}
}

Expand Down
4 changes: 2 additions & 2 deletions cli/tests/test_abandon_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,9 @@ fn test_bug_2600_rootcommit_special_case() {
"###);

// Now, the test
let stderr = test_env.jj_cmd_internal_error(&repo_path, &["abandon", "base"]);
let stderr = test_env.jj_cmd_failure(&repo_path, &["abandon", "base"]);
insta::assert_snapshot!(stderr, @r###"
Internal error: Merge failed
Error: Merge failed
Caused by: The Git backend does not support creating merge commits with the root commit as one of the parents.
"###);
}
Expand Down
4 changes: 4 additions & 0 deletions lib/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ pub enum BackendError {
},
#[error(transparent)]
Other(Box<dyn std::error::Error + Send + Sync>),
/// A valid operation attempted, but failed because it isn't supported by
/// the particular backend.
#[error("{0}")]
Unsupported(String),
}

pub type BackendResult<T> = Result<T, BackendError>;
Expand Down
6 changes: 3 additions & 3 deletions lib/src/git_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1121,10 +1121,10 @@ impl Backend for GitBackend {
// there are no other parents since Git cannot represent a merge between a root
// commit and another commit.
if contents.parents.len() > 1 {
return Err(BackendError::Other(
return Err(BackendError::Unsupported(
"The Git backend does not support creating merge commits with the root \
commit as one of the parents."
.into(),
.to_owned(),
));
}
} else {
Expand Down Expand Up @@ -1702,7 +1702,7 @@ mod tests {
commit.parents = vec![first_id, backend.root_commit_id().clone()];
assert_matches!(
backend.write_commit(commit, None),
Err(BackendError::Other(err)) if err.to_string().contains("root commit")
Err(BackendError::Unsupported(message)) if message.contains("root commit")
);
}

Expand Down

0 comments on commit f20004f

Please sign in to comment.