Skip to content

Commit

Permalink
fix: instead of erroring if loose iteration is performed on missing b…
Browse files Browse the repository at this point in the history
…ase, correctly yield zero references. (#595)

Previously it reported an error, now it does not and instead performs no
iteration, which is more helpful to the user of the API I believe as
they won't randomly fail just because somebody deleted the `refs`
folder.
  • Loading branch information
Byron committed Nov 16, 2022
1 parent 27386a9 commit e9853dd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 28 deletions.
25 changes: 8 additions & 17 deletions git-ref/src/store/file/loose/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,27 @@ use crate::{file::iter::LooseThenPacked, store_impl::file, BString, FullName};
pub(in crate::store_impl::file) struct SortedLoosePaths {
pub(crate) base: PathBuf,
filename_prefix: Option<BString>,
file_walk: DirEntryIter,
file_walk: Option<DirEntryIter>,
}

impl SortedLoosePaths {
pub fn at(
path: impl AsRef<Path>,
base: impl Into<PathBuf>,
filename_prefix: Option<BString>,
) -> std::io::Result<Self> {
pub fn at(path: impl AsRef<Path>, base: impl Into<PathBuf>, filename_prefix: Option<BString>) -> Self {
let path = path.as_ref();
if !path.is_dir() {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
format!("loose reference iteration path does not exist: \"{}\"", path.display()),
));
}
let file_walk = git_features::fs::walkdir_sorted_new(path).into_iter();
Ok(SortedLoosePaths {
SortedLoosePaths {
base: base.into(),
filename_prefix,
file_walk,
})
file_walk: path
.is_dir()
.then(|| git_features::fs::walkdir_sorted_new(path).into_iter()),
}
}
}

impl Iterator for SortedLoosePaths {
type Item = std::io::Result<(PathBuf, FullName)>;

fn next(&mut self) -> Option<Self::Item> {
for entry in self.file_walk.by_ref() {
for entry in self.file_walk.as_mut()?.by_ref() {
match entry {
Ok(entry) => {
if !entry.file_type().is_file() {
Expand Down
18 changes: 9 additions & 9 deletions git-ref/src/store/file/overlay_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,23 +253,23 @@ impl<'a> IterInfo<'a> {
}
}

fn into_iter(self) -> std::io::Result<Peekable<SortedLoosePaths>> {
Ok(match self {
IterInfo::Base { base } => SortedLoosePaths::at(base.join("refs"), base, None)?,
fn into_iter(self) -> Peekable<SortedLoosePaths> {
match self {
IterInfo::Base { base } => SortedLoosePaths::at(base.join("refs"), base, None),
IterInfo::BaseAndIterRoot {
base,
iter_root,
prefix: _,
} => SortedLoosePaths::at(iter_root, base, None)?,
IterInfo::PrefixAndBase { base, prefix } => SortedLoosePaths::at(base.join(prefix), base, None)?,
} => SortedLoosePaths::at(iter_root, base, None),
IterInfo::PrefixAndBase { base, prefix } => SortedLoosePaths::at(base.join(prefix), base, None),
IterInfo::ComputedIterationRoot {
iter_root,
base,
prefix: _,
remainder,
} => SortedLoosePaths::at(iter_root, base, remainder)?,
} => SortedLoosePaths::at(iter_root, base, remainder),
}
.peekable())
.peekable()
}

fn from_prefix(base: &'a Path, prefix: Cow<'a, Path>) -> std::io::Result<Self> {
Expand Down Expand Up @@ -397,8 +397,8 @@ impl file::Store {
),
None => None,
},
iter_git_dir: git_dir_info.into_iter()?,
iter_common_dir: common_dir_info.map(IterInfo::into_iter).transpose()?,
iter_git_dir: git_dir_info.into_iter(),
iter_common_dir: common_dir_info.map(IterInfo::into_iter),
buf: Vec::new(),
namespace: self.namespace.as_ref(),
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ mod collisions {
"packed-refs can store everything in case-insensitive manner"
);

assert!(
store.loose_iter().is_err(),
assert_eq!(
store.loose_iter()?.count(),
0,
"refs/ directory isn't present as there is no loose ref - it removed every up to the base dir"
);

Expand Down

0 comments on commit e9853dd

Please sign in to comment.