Skip to content

improve find-reference error messages #1637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gix-pack/src/bundle/write/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ fn resolve_entry(range: data::EntryRange, mapped_file: &memmap2::Mmap) -> Option
mapped_file.get(range.start as usize..range.end as usize)
}

#[allow(clippy::type_complexity)] // cannot typedef impl Fn
fn new_pack_file_resolver(
data_file: SharedTempFile,
) -> io::Result<(
Expand Down
8 changes: 4 additions & 4 deletions gix/src/reference/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,16 @@ pub mod head_tree_id {
pub mod find {
///
pub mod existing {
use gix_ref::PartialName;

/// The error returned by [`find_reference(…)`][crate::Repository::find_reference()], and others.
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
#[error(transparent)]
Find(#[from] crate::reference::find::Error),
#[error("The reference did not exist")]
NotFound,
#[error("The reference '{}' did not exist", name.as_ref().as_bstr())]
NotFound { name: PartialName },
}
}

Expand All @@ -127,7 +129,5 @@ pub mod find {
pub enum Error {
#[error(transparent)]
Find(#[from] gix_ref::file::find::Error),
#[error(transparent)]
PackedRefsOpen(#[from] gix_ref::packed::buffer::open::Error),
}
}
17 changes: 12 additions & 5 deletions gix/src/repository/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl crate::Repository {
Ok(match head.inner.target {
Target::Symbolic(branch) => match self.find_reference(&branch) {
Ok(r) => crate::head::Kind::Symbolic(r.detach()),
Err(reference::find::existing::Error::NotFound) => crate::head::Kind::Unborn(branch),
Err(reference::find::existing::Error::NotFound { .. }) => crate::head::Kind::Unborn(branch),
Err(err) => return Err(err),
},
Target::Object(target) => crate::head::Kind::Detached {
Expand Down Expand Up @@ -222,11 +222,19 @@ impl crate::Repository {
/// without that being considered an error.
pub fn find_reference<'a, Name, E>(&self, name: Name) -> Result<Reference<'_>, reference::find::existing::Error>
where
Name: TryInto<&'a PartialNameRef, Error = E>,
Name: TryInto<&'a PartialNameRef, Error = E> + Clone,
gix_ref::file::find::Error: From<E>,
{
// TODO: is there a way to just pass `partial_name` to `try_find_reference()`? Compiler freaks out then
// as it still wants to see `E` there, not `Infallible`.
let partial_name = name
.clone()
.try_into()
.map_err(|err| reference::find::Error::Find(gix_ref::file::find::Error::from(err)))?;
self.try_find_reference(name)?
.ok_or(reference::find::existing::Error::NotFound)
.ok_or_else(|| reference::find::existing::Error::NotFound {
name: partial_name.to_owned(),
})
}

/// Return a platform for iterating references.
Expand All @@ -249,8 +257,7 @@ impl crate::Repository {
Name: TryInto<&'a PartialNameRef, Error = E>,
gix_ref::file::find::Error: From<E>,
{
let state = self;
match state.refs.try_find(name) {
match self.refs.try_find(name) {
Ok(r) => match r {
Some(r) => Ok(Some(Reference::from_ref(r, self))),
None => Ok(None),
Expand Down
Loading