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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use std::{cell::Ref, convert::TryInto};
use git_hash::ObjectId;
pub use git_object::Kind;
use crate::{
easy,
easy::{Object, ObjectRef, TreeRef},
};
mod errors;
pub(crate) mod cache {
pub use git_pack::cache::object::MemoryCappedHashmap;
}
pub use errors::{conversion, find, write};
mod impls;
pub mod peel;
mod tree;
impl Object {
pub fn attach<A>(self, access: &A) -> easy::borrow::state::Result<ObjectRef<'_, A>>
where
A: easy::Access + Sized,
{
*access.state().try_borrow_mut_buf()? = self.data;
Ok(ObjectRef {
id: self.id,
kind: self.kind,
data: Ref::map(access.state().try_borrow_buf()?, |v| v.as_slice()),
access,
})
}
}
impl<'repo, A> ObjectRef<'repo, A>
where
A: easy::Access + Sized,
{
pub(crate) fn from_current_buf(
id: impl Into<ObjectId>,
kind: Kind,
access: &'repo A,
) -> easy::borrow::state::Result<Self> {
Ok(ObjectRef {
id: id.into(),
kind,
data: Ref::map(access.state().try_borrow_buf()?, |v| v.as_slice()),
access,
})
}
pub fn into_tree(self) -> TreeRef<'repo, A> {
match self.try_into() {
Ok(tree) => tree,
Err(this) => panic!("Tried to use {} as tree, but was {}", this.id, this.kind),
}
}
pub fn try_into_tree(self) -> Result<TreeRef<'repo, A>, Self> {
self.try_into()
}
}
impl<'repo, A> ObjectRef<'repo, A> {
pub fn to_owned(&self) -> Object {
Object {
id: self.id,
kind: self.kind,
data: self.data.to_owned(),
}
}
pub fn into_owned(self) -> Object {
Object {
id: self.id,
kind: self.kind,
data: self.data.to_owned(),
}
}
pub fn detach(self) -> Object {
self.into()
}
}
impl<'repo, A> ObjectRef<'repo, A>
where
A: easy::Access + Sized,
{
pub fn to_commit(&self) -> git_object::CommitRef<'_> {
self.try_to_commit().expect("BUG: need a commit")
}
pub fn try_to_commit(&self) -> Result<git_object::CommitRef<'_>, conversion::Error> {
git_odb::data::Object::new(self.kind, &self.data)
.decode()?
.into_commit()
.ok_or(conversion::Error::UnexpectedType {
expected: git_object::Kind::Commit,
actual: self.kind,
})
}
pub fn to_commit_iter(&self) -> git_object::CommitRefIter<'_> {
git_odb::data::Object::new(self.kind, &self.data)
.try_into_commit_iter()
.expect("BUG: This object must be a commit")
}
pub fn try_to_commit_iter(&self) -> Option<git_object::CommitRefIter<'_>> {
git_odb::data::Object::new(self.kind, &self.data).try_into_commit_iter()
}
pub fn to_tag_iter(&self) -> git_object::TagRefIter<'_> {
git_odb::data::Object::new(self.kind, &self.data)
.try_into_tag_iter()
.expect("BUG: this object must be a tag")
}
pub fn try_to_tag_iter(&self) -> Option<git_object::TagRefIter<'_>> {
git_odb::data::Object::new(self.kind, &self.data).try_into_tag_iter()
}
pub fn to_tag(&self) -> git_object::TagRef<'_> {
self.try_to_tag().expect("BUG: need tag")
}
pub fn try_to_tag(&self) -> Result<git_object::TagRef<'_>, conversion::Error> {
git_odb::data::Object::new(self.kind, &self.data)
.decode()?
.into_tag()
.ok_or(conversion::Error::UnexpectedType {
expected: git_object::Kind::Tag,
actual: self.kind,
})
}
}