Skip to main content

thin_fetch/repo/
get.rs

1use crate::object::{Blob, Object};
2use crate::{Checkout, Error, HashKind, WithError as _, bail, error};
3
4use super::Repo;
5
6impl Repo {
7    #[must_use]
8    pub fn get_hash_kind(&self) -> HashKind {
9        self.hash_kind
10    }
11
12    #[must_use]
13    pub fn get_blob(&self, oid: &str) -> Option<&Blob> {
14        self.object_cache.get(oid).and_then(|a| match a {
15            Object::Blob(blob) => Some(blob),
16            _ => None,
17        })
18    }
19
20    /// Gets linear history of commits between `start` and `end`.
21    ///
22    /// # Errors
23    ///
24    /// Returns an error when the history is not linear.
25    pub fn get_history_linear<'a>(
26        &'a self,
27        start: &str,
28        end: &str,
29    ) -> Result<Vec<Checkout<'a>>, Error> {
30        let mut new_head = end.to_owned();
31        let mut checkouts = vec![];
32
33        loop {
34            let checkout = Checkout::from_pack(&self.object_cache, &new_head)
35                .with_err(|| error!("could not create a checkout for commit {new_head}"))?;
36
37            let parents = &checkout.commit.commit.parents;
38            if parents.len() > 1 {
39                bail!("commit history non-linear");
40            }
41            checkouts.push(checkout);
42            if new_head == start {
43                break;
44            }
45            new_head = parents.first().with_err_msg("no parent")?.to_string();
46        }
47
48        Ok(checkouts)
49    }
50}