1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use crate::{bstr, bstr::BStr, revision, Commit, ObjectDetached, Tree};
mod error {
use crate::object;
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
#[error(transparent)]
FindExistingObject(#[from] object::find::existing::Error),
#[error("The commit could not be decoded fully or partially")]
Decode(#[from] git_object::decode::Error),
#[error("Expected object of type {}, but got {}", .expected, .actual)]
ObjectKind {
expected: git_object::Kind,
actual: git_object::Kind,
},
}
}
pub use error::Error;
impl<'repo> Commit<'repo> {
pub fn detached(&self) -> ObjectDetached {
ObjectDetached {
id: self.id,
kind: git_object::Kind::Commit,
data: self.data.clone(),
}
}
pub fn detach(self) -> ObjectDetached {
self.into()
}
}
impl<'repo> Commit<'repo> {
pub fn short_id(&self) -> Result<git_hash::Prefix, crate::id::shorten::Error> {
use crate::ext::ObjectIdExt;
self.id.attach(self.repo).shorten()
}
pub fn message(&self) -> Result<git_object::commit::MessageRef<'_>, git_object::decode::Error> {
Ok(git_object::commit::MessageRef::from_bytes(self.message_raw()?))
}
pub fn message_raw(&self) -> Result<&'_ BStr, git_object::decode::Error> {
git_object::CommitRefIter::from_bytes(&self.data).message()
}
pub fn message_raw_sloppy(&self) -> &BStr {
use bstr::ByteSlice;
self.data
.find(b"\n\n")
.map(|pos| &self.data[pos + 2..])
.unwrap_or_default()
.as_bstr()
}
pub fn time(&self) -> Result<git_actor::Time, Error> {
Ok(self.committer()?.time)
}
pub fn decode(&self) -> Result<git_object::CommitRef<'_>, git_object::decode::Error> {
git_object::CommitRef::from_bytes(&self.data)
}
pub fn iter(&self) -> git_object::CommitRefIter<'_> {
git_object::CommitRefIter::from_bytes(&self.data)
}
pub fn author(&self) -> Result<git_actor::SignatureRef<'_>, git_object::decode::Error> {
git_object::CommitRefIter::from_bytes(&self.data)
.author()
.map(|s| s.trim())
}
pub fn committer(&self) -> Result<git_actor::SignatureRef<'_>, git_object::decode::Error> {
git_object::CommitRefIter::from_bytes(&self.data)
.committer()
.map(|s| s.trim())
}
pub fn parent_ids(&self) -> impl Iterator<Item = crate::Id<'repo>> + '_ {
use crate::ext::ObjectIdExt;
let repo = self.repo;
git_object::CommitRefIter::from_bytes(&self.data)
.parent_ids()
.map(move |id| id.attach(repo))
}
pub fn tree(&self) -> Result<Tree<'repo>, Error> {
match self.tree_id()?.object()?.try_into_tree() {
Ok(tree) => Ok(tree),
Err(crate::object::try_into::Error { actual, expected, .. }) => Err(Error::ObjectKind { actual, expected }),
}
}
pub fn tree_id(&self) -> Result<crate::Id<'repo>, git_object::decode::Error> {
git_object::CommitRefIter::from_bytes(&self.data)
.tree_id()
.map(|id| crate::Id::from_id(id, self.repo))
}
pub fn id(&self) -> crate::Id<'repo> {
use crate::ext::ObjectIdExt;
self.id.attach(self.repo)
}
pub fn ancestors(&self) -> revision::walk::Platform<'repo> {
self.id().ancestors()
}
pub fn describe(&self) -> crate::commit::describe::Platform<'repo> {
crate::commit::describe::Platform {
id: self.id,
repo: self.repo,
select: Default::default(),
first_parent: false,
id_as_fallback: false,
max_candidates: 10,
}
}
}
impl<'r> std::fmt::Debug for Commit<'r> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Commit({})", self.id)
}
}