]> git.proxmox.com Git - rustc.git/blob - vendor/gix-ref/src/store/file/loose/reference/logiter.rs
New upstream version 1.70.0+dfsg2
[rustc.git] / vendor / gix-ref / src / store / file / loose / reference / logiter.rs
1 use crate::store_impl::{
2 file,
3 file::{log, loose, loose::Reference},
4 };
5
6 pub(crate) fn must_be_io_err(err: loose::reflog::Error) -> std::io::Error {
7 match err {
8 loose::reflog::Error::Io(err) => err,
9 loose::reflog::Error::RefnameValidation(_) => unreachable!("we are called from a valid ref"),
10 }
11 }
12
13 impl Reference {
14 /// Returns true if a reflog exists in the given `store`.
15 ///
16 /// Please note that this method shouldn't be used to check if a log exists before trying to read it, but instead
17 /// is meant to be the fastest possible way to determine if a log exists or not.
18 /// If the caller needs to know if it's readable, try to read the log instead with a reverse or forward iterator.
19 pub fn log_exists(&self, store: &file::Store) -> bool {
20 store
21 .reflog_exists(self.name.as_ref())
22 .expect("name conversion infallible")
23 }
24 /// Return a reflog reverse iterator for this ref, reading chunks from the back into the fixed buffer `buf`, in the given `store`.
25 ///
26 /// The iterator will traverse log entries from most recent to oldest, reading the underlying file in chunks from the back.
27 /// Return `Ok(None)` if no reflog exists.
28 pub fn log_iter_rev<'b>(
29 &self,
30 store: &file::Store,
31 buf: &'b mut [u8],
32 ) -> std::io::Result<Option<log::iter::Reverse<'b, std::fs::File>>> {
33 store.reflog_iter_rev(self.name.as_ref(), buf).map_err(must_be_io_err)
34 }
35
36 /// Return a reflog forward iterator for this ref and write its file contents into `buf`, in the given `store`.
37 ///
38 /// The iterator will traverse log entries from oldest to newest.
39 /// Return `Ok(None)` if no reflog exists.
40 pub fn log_iter<'a, 'b: 'a>(
41 &'a self,
42 store: &file::Store,
43 buf: &'b mut Vec<u8>,
44 ) -> std::io::Result<Option<impl Iterator<Item = Result<log::LineRef<'b>, log::iter::decode::Error>> + 'a>> {
45 store.reflog_iter(self.name.as_ref(), buf).map_err(must_be_io_err)
46 }
47 }