[go: up one dir, main page]

gix/object/
peel.rs

1//!
2#![allow(clippy::empty_docs)]
3use crate::{
4    object,
5    object::{peel, Kind},
6    Commit, Object, Tree,
7};
8
9///
10pub mod to_kind {
11    mod error {
12
13        use crate::object;
14
15        /// The error returned by [`Object::peel_to_kind()`][crate::Object::peel_to_kind()].
16        #[derive(Debug, thiserror::Error)]
17        #[allow(missing_docs)]
18        pub enum Error {
19            #[error(transparent)]
20            FindExistingObject(#[from] object::find::existing::Error),
21            #[error("Last encountered object {oid} was {actual} while trying to peel to {expected}")]
22            NotFound {
23                oid: gix_hash::Prefix,
24                actual: object::Kind,
25                expected: object::Kind,
26            },
27        }
28    }
29    pub use error::Error;
30}
31
32impl<'repo> Object<'repo> {
33    // TODO: tests
34    /// Follow tags to their target and commits to trees until the given `kind` of object is encountered.
35    ///
36    /// Note that this object doesn't necessarily have to be the end of the chain.
37    /// Typical values are [`Kind::Commit`] or [`Kind::Tree`].
38    pub fn peel_to_kind(mut self, kind: Kind) -> Result<Self, peel::to_kind::Error> {
39        loop {
40            match self.kind {
41                our_kind if kind == our_kind => {
42                    return Ok(self);
43                }
44                Kind::Commit => {
45                    let tree_id = self
46                        .try_to_commit_ref_iter()
47                        .expect("commit")
48                        .tree_id()
49                        .expect("valid commit");
50                    let repo = self.repo;
51                    drop(self);
52                    self = repo.find_object(tree_id)?;
53                }
54                Kind::Tag => {
55                    let target_id = self.to_tag_ref_iter().target_id().expect("valid tag");
56                    let repo = self.repo;
57                    drop(self);
58                    self = repo.find_object(target_id)?;
59                }
60                Kind::Tree | Kind::Blob => {
61                    return Err(peel::to_kind::Error::NotFound {
62                        oid: self.id().shorten().unwrap_or_else(|_| self.id.into()),
63                        actual: self.kind,
64                        expected: kind,
65                    })
66                }
67            }
68        }
69    }
70
71    /// Peel this object into a tree and return it, if this is possible.
72    ///
73    /// This will follow tag objects and commits until their tree is reached.
74    pub fn peel_to_tree(self) -> Result<Tree<'repo>, peel::to_kind::Error> {
75        Ok(self.peel_to_kind(gix_object::Kind::Tree)?.into_tree())
76    }
77
78    /// Peel this object into a commit and return it, if this is possible.
79    ///
80    /// This will follow tag objects until a commit is reached.
81    pub fn peel_to_commit(self) -> Result<Commit<'repo>, peel::to_kind::Error> {
82        Ok(self.peel_to_kind(gix_object::Kind::Commit)?.into_commit())
83    }
84
85    // TODO: tests
86    /// Follow all tag object targets until a commit, tree or blob is reached.
87    ///
88    /// Note that this method is different from [`peel_to_kind(…)`][Object::peel_to_kind()] as it won't
89    /// peel commits to their tree, but handles tags only.
90    pub fn peel_tags_to_end(mut self) -> Result<Self, object::find::existing::Error> {
91        loop {
92            match self.kind {
93                Kind::Commit | Kind::Tree | Kind::Blob => break Ok(self),
94                Kind::Tag => {
95                    let target_id = self.to_tag_ref_iter().target_id().expect("valid tag");
96                    let repo = self.repo;
97                    drop(self);
98                    self = repo.find_object(target_id)?;
99                }
100            }
101        }
102    }
103}