rust: make RevlogError AmbiguousPrefix case contain the actual prefix
This brings the work started in `652149ed64f0` to its logical conclusion and
makes the RevlogError self-sufficient so it can be directly converted to
CommandError, without an extra rev text annotation.
Without this change, it's confusing that the extra annotation is ignored in
most-but-not-all cases.
--- a/rust/hg-core/src/revlog/index.rs Mon Dec 02 09:45:56 2024 -0500
+++ b/rust/hg-core/src/revlog/index.rs Tue Nov 26 15:45:11 2024 +0000
@@ -447,7 +447,10 @@
}
if node.is_prefix_of(&candidate_node) {
if found_by_prefix.is_some() {
- return Err(RevlogError::AmbiguousPrefix);
+ return Err(RevlogError::AmbiguousPrefix(format!(
+ "{:x}",
+ node
+ )));
}
found_by_prefix = Some(rev)
}
--- a/rust/hg-core/src/revlog/mod.rs Mon Dec 02 09:45:56 2024 -0500
+++ b/rust/hg-core/src/revlog/mod.rs Tue Nov 26 15:45:11 2024 +0000
@@ -208,15 +208,15 @@
/// Working directory is not supported
WDirUnsupported,
/// Found more than one entry whose ID match the requested prefix
- AmbiguousPrefix,
+ AmbiguousPrefix(String),
#[from]
Other(HgError),
}
-impl From<NodeMapError> for RevlogError {
- fn from(error: NodeMapError) -> Self {
+impl From<(NodeMapError, String)> for RevlogError {
+ fn from((error, rev): (NodeMapError, String)) -> Self {
match error {
- NodeMapError::MultipleResults => RevlogError::AmbiguousPrefix,
+ NodeMapError::MultipleResults => RevlogError::AmbiguousPrefix(rev),
NodeMapError::RevisionNotInIndex(rev) => RevlogError::corrupted(
format!("nodemap point to revision {} not in index", rev),
),
@@ -359,7 +359,8 @@
) -> Result<Revision, RevlogError> {
if let Some(nodemap) = &self.nodemap {
nodemap
- .find_bin(self.index(), node)?
+ .find_bin(self.index(), node)
+ .map_err(|err| (err, format!("{:x}", node)))?
.ok_or(RevlogError::InvalidRevision(format!("{:x}", node)))
} else {
self.index().rev_from_node_no_persistent_nodemap(node)
@@ -887,7 +888,7 @@
.rev_from_node(NodePrefix::from_hex("00").unwrap())
.expect_err("Expected to give AmbiguousPrefix error")
{
- RevlogError::AmbiguousPrefix => (),
+ RevlogError::AmbiguousPrefix(_) => (),
e => {
panic!("Got another error than AmbiguousPrefix: {:?}", e);
}
--- a/rust/rhg/src/commands/cat.rs Mon Dec 02 09:45:56 2024 -0500
+++ b/rust/rhg/src/commands/cat.rs Tue Nov 26 15:45:11 2024 +0000
@@ -94,7 +94,7 @@
None => format!("{:x}", repo.dirstate_parents()?.p1),
};
- let output = cat(repo, &rev, files).map_err(|e| (e, rev.as_str()))?;
+ let output = cat(repo, &rev, files)?;
for (_file, contents) in output.results {
invocation.ui.write_stdout(&contents)?;
}
--- a/rust/rhg/src/commands/debugdata.rs Mon Dec 02 09:45:56 2024 -0500
+++ b/rust/rhg/src/commands/debugdata.rs Tue Nov 26 15:45:11 2024 +0000
@@ -62,7 +62,7 @@
"support for ellipsis nodes is missing and repo has narrow enabled",
));
}
- let data = debug_data(repo, rev, kind).map_err(|e| (e, rev.as_ref()))?;
+ let data = debug_data(repo, rev, kind)?;
let mut stdout = invocation.ui.stdout_buffer();
stdout.write_all(&data)?;
--- a/rust/rhg/src/commands/files.rs Mon Dec 02 09:45:56 2024 -0500
+++ b/rust/rhg/src/commands/files.rs Tue Nov 26 15:45:11 2024 +0000
@@ -88,8 +88,7 @@
};
if let Some(rev) = rev {
- let files = list_revset_tracked_files(repo, rev, matcher)
- .map_err(|e| (e, rev.as_ref()))?;
+ let files = list_revset_tracked_files(repo, rev, matcher)?;
display_files(
invocation.ui,
repo,
--- a/rust/rhg/src/commands/status.rs Mon Dec 02 09:45:56 2024 -0500
+++ b/rust/rhg/src/commands/status.rs Tue Nov 26 15:45:11 2024 +0000
@@ -175,10 +175,8 @@
let rev1 = &revs[0];
let rev2 = &revs[1];
- let rev1 = hg::revset::resolve_single(rev1, repo)
- .map_err(|e| (e, rev1.as_str()))?;
- let rev2 = hg::revset::resolve_single(rev2, repo)
- .map_err(|e| (e, rev2.as_str()))?;
+ let rev1 = hg::revset::resolve_single(rev1, repo)?;
+ let rev2 = hg::revset::resolve_single(rev2, repo)?;
Ok(Some((rev1, rev2)))
}
@@ -388,9 +386,7 @@
&& (display_states.modified || display_states.clean)
{
let p1 = repo.dirstate_parents()?.p1;
- let manifest = repo.manifest_for_node(p1).map_err(|e| {
- CommandError::from((e, &*format!("{:x}", p1.short())))
- })?;
+ let manifest = repo.manifest_for_node(p1)?;
let working_directory_vfs = repo.working_directory_vfs();
let store_vfs = repo.store_vfs();
let filelog_open_options = default_revlog_options(
--- a/rust/rhg/src/error.rs Mon Dec 02 09:45:56 2024 -0500
+++ b/rust/rhg/src/error.rs Tue Nov 26 15:45:11 2024 +0000
@@ -204,8 +204,8 @@
}
}
-impl From<(RevlogError, &str)> for CommandError {
- fn from((err, rev): (RevlogError, &str)) -> CommandError {
+impl From<RevlogError> for CommandError {
+ fn from(err: RevlogError) -> CommandError {
match err {
RevlogError::WDirUnsupported => CommandError::abort(
"abort: working directory revision cannot be specified",
@@ -214,9 +214,9 @@
"abort: invalid revision identifier: {}",
r
)),
- RevlogError::AmbiguousPrefix => CommandError::abort(format!(
+ RevlogError::AmbiguousPrefix(r) => CommandError::abort(format!(
"abort: ambiguous revision identifier: {}",
- rev
+ r
)),
RevlogError::Other(error) => error.into(),
}