[go: up one dir, main page]

gix/object/
commit.rs

1use crate::{bstr, bstr::BStr, Commit, ObjectDetached, Tree};
2
3mod error {
4    use crate::object;
5
6    #[derive(Debug, thiserror::Error)]
7    #[allow(missing_docs)]
8    pub enum Error {
9        #[error(transparent)]
10        FindExistingObject(#[from] object::find::existing::Error),
11        #[error("The commit could not be decoded fully or partially")]
12        Decode(#[from] gix_object::decode::Error),
13        #[error("The commit date could not be parsed")]
14        ParseDate(#[from] gix_date::parse::Error),
15        #[error("Expected object of type {}, but got {}", .expected, .actual)]
16        ObjectKind {
17            expected: gix_object::Kind,
18            actual: gix_object::Kind,
19        },
20    }
21}
22
23pub use error::Error;
24
25/// Remove Lifetime
26impl Commit<'_> {
27    /// Create an owned instance of this object, copying our data in the process.
28    pub fn detached(&self) -> ObjectDetached {
29        ObjectDetached {
30            id: self.id,
31            kind: gix_object::Kind::Commit,
32            data: self.data.clone(),
33        }
34    }
35
36    /// Sever the connection to the `Repository` and turn this instance into a standalone object.
37    pub fn detach(self) -> ObjectDetached {
38        self.into()
39    }
40
41    /// Retrieve this instance's encoded data, leaving its own data empty.
42    ///
43    /// This method works around the immovability of members of this type.
44    pub fn take_data(&mut self) -> Vec<u8> {
45        std::mem::take(&mut self.data)
46    }
47}
48
49impl<'repo> Commit<'repo> {
50    /// Turn this objects id into a shortened id with a length in hex as configured by `core.abbrev`.
51    pub fn short_id(&self) -> Result<gix_hash::Prefix, crate::id::shorten::Error> {
52        use crate::ext::ObjectIdExt;
53        self.id.attach(self.repo).shorten()
54    }
55
56    /// Parse the commits message into a [`MessageRef`][gix_object::commit::MessageRef]
57    pub fn message(&self) -> Result<gix_object::commit::MessageRef<'_>, gix_object::decode::Error> {
58        Ok(gix_object::commit::MessageRef::from_bytes(self.message_raw()?))
59    }
60    /// Decode the commit object until the message and return it.
61    pub fn message_raw(&self) -> Result<&'_ BStr, gix_object::decode::Error> {
62        gix_object::CommitRefIter::from_bytes(&self.data).message()
63    }
64    /// Obtain the message by using intricate knowledge about the encoding, which is fastest and
65    /// can't fail at the expense of error handling.
66    pub fn message_raw_sloppy(&self) -> &BStr {
67        use bstr::ByteSlice;
68        self.data
69            .find(b"\n\n")
70            .map(|pos| &self.data[pos + 2..])
71            .unwrap_or_default()
72            .as_bstr()
73    }
74
75    /// Decode the commit and obtain the time at which the commit was created.
76    ///
77    /// For the time at which it was authored, refer to `.decode()?.author.time`.
78    pub fn time(&self) -> Result<gix_date::Time, Error> {
79        Ok(self.committer()?.time()?)
80    }
81
82    /// Decode the entire commit object and return it for accessing all commit information.
83    ///
84    /// It will allocate only if there are more than 2 parents.
85    ///
86    /// Note that the returned commit object does make lookup easy and should be
87    /// used for successive calls to string-ish information to avoid decoding the object
88    /// more than once.
89    pub fn decode(&self) -> Result<gix_object::CommitRef<'_>, gix_object::decode::Error> {
90        gix_object::CommitRef::from_bytes(&self.data)
91    }
92
93    /// Return an iterator over tokens, representing this commit piece by piece.
94    pub fn iter(&self) -> gix_object::CommitRefIter<'_> {
95        gix_object::CommitRefIter::from_bytes(&self.data)
96    }
97
98    /// Return the commits author, with surrounding whitespace trimmed.
99    pub fn author(&self) -> Result<gix_actor::SignatureRef<'_>, gix_object::decode::Error> {
100        gix_object::CommitRefIter::from_bytes(&self.data)
101            .author()
102            .map(|s| s.trim())
103    }
104
105    /// Return the commits committer. with surrounding whitespace trimmed.
106    pub fn committer(&self) -> Result<gix_actor::SignatureRef<'_>, gix_object::decode::Error> {
107        gix_object::CommitRefIter::from_bytes(&self.data)
108            .committer()
109            .map(|s| s.trim())
110    }
111
112    /// Decode this commits parent ids on the fly without allocating.
113    // TODO: tests
114    pub fn parent_ids(&self) -> impl Iterator<Item = crate::Id<'repo>> + '_ {
115        use crate::ext::ObjectIdExt;
116        let repo = self.repo;
117        gix_object::CommitRefIter::from_bytes(&self.data)
118            .parent_ids()
119            .map(move |id| id.attach(repo))
120    }
121
122    /// Parse the commit and return the tree object it points to.
123    pub fn tree(&self) -> Result<Tree<'repo>, Error> {
124        match self.tree_id()?.object()?.try_into_tree() {
125            Ok(tree) => Ok(tree),
126            Err(crate::object::try_into::Error { actual, expected, .. }) => Err(Error::ObjectKind { actual, expected }),
127        }
128    }
129
130    /// Parse the commit and return the tree id it points to.
131    pub fn tree_id(&self) -> Result<crate::Id<'repo>, gix_object::decode::Error> {
132        gix_object::CommitRefIter::from_bytes(&self.data)
133            .tree_id()
134            .map(|id| crate::Id::from_id(id, self.repo))
135    }
136
137    /// Return our id own id with connection to this repository.
138    pub fn id(&self) -> crate::Id<'repo> {
139        use crate::ext::ObjectIdExt;
140        self.id.attach(self.repo)
141    }
142
143    /// Obtain a platform for traversing ancestors of this commit.
144    pub fn ancestors(&self) -> crate::revision::walk::Platform<'repo> {
145        self.id().ancestors()
146    }
147
148    /// Create a platform to further configure a `git describe` operation to find a name for this commit by looking
149    /// at the closest annotated tags (by default) in its past.
150    #[cfg(feature = "revision")]
151    pub fn describe(&self) -> crate::commit::describe::Platform<'repo> {
152        crate::commit::describe::Platform {
153            id: self.id,
154            repo: self.repo,
155            select: Default::default(),
156            first_parent: false,
157            id_as_fallback: false,
158            max_candidates: 10,
159        }
160    }
161
162    /// Extracts the PGP signature and the data that was used to create the signature, or `None` if it wasn't signed.
163    // TODO: make it possible to verify the signature, probably by wrapping `SignedData`. It's quite some work to do it properly.
164    pub fn signature(
165        &self,
166    ) -> Result<Option<(std::borrow::Cow<'_, BStr>, gix_object::commit::SignedData<'_>)>, gix_object::decode::Error>
167    {
168        gix_object::CommitRefIter::signature(&self.data)
169    }
170}
171
172impl std::fmt::Debug for Commit<'_> {
173    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
174        write!(f, "Commit({})", self.id)
175    }
176}