gix_ref/store/file/loose/
iter.rs

1use std::path::{Path, PathBuf};
2
3use gix_features::fs::walkdir::DirEntryIter;
4use gix_object::bstr::ByteSlice;
5use gix_path::RelativePath;
6
7use crate::{file::iter::LooseThenPacked, store_impl::file, BString, FullName};
8
9/// An iterator over all valid loose reference paths as seen from a particular base directory.
10pub(in crate::store_impl::file) struct SortedLoosePaths {
11    pub(crate) base: PathBuf,
12    /// An prefix like `refs/heads/foo/` or `refs/heads/prefix` that a returned reference must match against..
13    prefix: Option<BString>,
14    /// A suffix like `HEAD` that a returned reference must match against..
15    suffix: Option<BString>,
16    file_walk: Option<DirEntryIter>,
17}
18
19impl SortedLoosePaths {
20    pub fn at(
21        path: &Path,
22        base: PathBuf,
23        prefix: Option<BString>,
24        suffix: Option<BString>,
25        precompose_unicode: bool,
26    ) -> Self {
27        let depth = if suffix.is_some() { 1 } else { usize::MAX };
28        SortedLoosePaths {
29            base,
30            prefix,
31            suffix,
32            file_walk: path.is_dir().then(|| {
33                // serial iteration as we expect most refs in packed-refs anyway.
34                gix_features::fs::walkdir_sorted_new(
35                    path,
36                    gix_features::fs::walkdir::Parallelism::Serial,
37                    depth,
38                    precompose_unicode,
39                )
40                .into_iter()
41            }),
42        }
43    }
44}
45
46impl Iterator for SortedLoosePaths {
47    type Item = std::io::Result<(PathBuf, FullName)>;
48
49    fn next(&mut self) -> Option<Self::Item> {
50        for entry in self.file_walk.as_mut()?.by_ref() {
51            match entry {
52                Ok(entry) => {
53                    if !entry.file_type().is_ok_and(|ft| ft.is_file()) {
54                        continue;
55                    }
56                    let full_path = entry.path().into_owned();
57                    let full_name = full_path
58                        .strip_prefix(&self.base)
59                        .expect("prefix-stripping cannot fail as base is within our root");
60                    let Ok(full_name) = gix_path::try_into_bstr(full_name)
61                        .map(|name| gix_path::to_unix_separators_on_windows(name).into_owned())
62                    else {
63                        continue;
64                    };
65                    if let Some(prefix) = &self.prefix {
66                        if !full_name.starts_with(prefix) {
67                            continue;
68                        }
69                    }
70                    if let Some(suffix) = &self.suffix {
71                        if !full_name.ends_with(suffix) {
72                            continue;
73                        }
74                    }
75                    if gix_validate::reference::name_partial(full_name.as_bstr()).is_ok() {
76                        let name = FullName(full_name);
77                        return Some(Ok((full_path, name)));
78                    } else {
79                        continue;
80                    }
81                }
82                Err(err) => return Some(Err(err.into_io_error().expect("no symlink related errors"))),
83            }
84        }
85        None
86    }
87}
88
89impl file::Store {
90    /// Return an iterator over all loose references, notably not including any packed ones, in lexical order.
91    /// Each of the references may fail to parse and the iterator will not stop if parsing fails, allowing the caller
92    /// to see all files that look like references whether valid or not.
93    ///
94    /// Reference files that do not constitute valid names will be silently ignored.
95    pub fn loose_iter(&self) -> std::io::Result<LooseThenPacked<'_, '_>> {
96        self.iter_packed(None)
97    }
98
99    /// Return an iterator over all loose references that start with the given `prefix`.
100    ///
101    /// Otherwise, it's similar to [`loose_iter()`](file::Store::loose_iter()).
102    ///
103    /// Note that if a prefix isn't using a trailing `/`, like in `refs/heads/foo`, it will effectively
104    /// start the traversal in the parent directory, e.g. `refs/heads/` and list everything inside that
105    /// starts with `foo`, like `refs/heads/foo` and `refs/heads/foobar`.
106    ///
107    /// Prefixes are relative paths with slash-separated components.
108    pub fn loose_iter_prefixed(&self, prefix: &RelativePath) -> std::io::Result<LooseThenPacked<'_, '_>> {
109        self.iter_prefixed_packed(prefix, None)
110    }
111}