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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use std::{convert::TryInto, ops::DerefMut};
use git_hash::{oid, ObjectId};
use git_odb::{Find, FindExt};
use git_pack::cache::Object;
use git_ref::{
transaction::{LogChange, PreviousValue, RefLog},
FullName,
};
use crate::{
easy,
easy::{commit, object, tag, ObjectRef, Oid, Reference},
ext::ObjectIdExt,
};
pub trait ObjectAccessExt: easy::Access + Sized {
fn find_object(&self, id: impl Into<ObjectId>) -> Result<ObjectRef<'_, Self>, object::find::existing::Error> {
let state = self.state();
let id = id.into();
let kind = {
let mut buf = self.state().try_borrow_mut_buf()?;
let mut object_cache = state.try_borrow_mut_object_cache()?;
if let Some(c) = object_cache.deref_mut() {
if let Some(kind) = c.get(&id, &mut buf) {
drop(buf);
return ObjectRef::from_current_buf(id, kind, self).map_err(Into::into);
}
}
let kind = self
.repo()?
.odb
.find(&id, &mut buf, state.try_borrow_mut_pack_cache()?.deref_mut())?
.kind;
if let Some(c) = object_cache.deref_mut() {
c.put(id, kind, &buf);
}
kind
};
ObjectRef::from_current_buf(id, kind, self).map_err(Into::into)
}
fn try_find_object(&self, id: impl Into<ObjectId>) -> Result<Option<ObjectRef<'_, Self>>, object::find::Error> {
let state = self.state();
let id = id.into();
let mut object_cache = state.try_borrow_mut_object_cache()?;
let mut buf = state.try_borrow_mut_buf()?;
if let Some(c) = object_cache.deref_mut() {
if let Some(kind) = c.get(&id, &mut buf) {
drop(buf);
return Ok(Some(ObjectRef::from_current_buf(id, kind, self)?));
}
}
match self
.repo()?
.odb
.try_find(&id, &mut buf, state.try_borrow_mut_pack_cache()?.deref_mut())?
{
Some(obj) => {
let kind = obj.kind;
drop(obj);
if let Some(c) = object_cache.deref_mut() {
c.put(id, kind, &buf);
}
drop(buf);
Ok(Some(ObjectRef::from_current_buf(id, kind, self)?))
}
None => Ok(None),
}
}
fn write_object(&self, object: impl git_object::WriteTo) -> Result<Oid<'_, Self>, object::write::Error> {
use git_odb::Write;
let repo = self.repo()?;
repo.odb
.write(object, repo.hash_kind)
.map(|oid| oid.attach(self))
.map_err(Into::into)
}
fn tag(
&self,
name: impl AsRef<str>,
target: impl AsRef<oid>,
target_kind: git_object::Kind,
tagger: Option<&git_actor::SignatureRef<'_>>,
message: impl AsRef<str>,
constraint: PreviousValue,
) -> Result<Reference<'_, Self>, tag::Error> {
let tag = git_object::Tag {
target: target.as_ref().into(),
target_kind,
name: name.as_ref().into(),
tagger: tagger.map(|t| t.to_owned()),
message: message.as_ref().into(),
pgp_signature: None,
};
let tag_id = self.write_object(&tag)?;
super::ReferenceAccessExt::tag_reference(self, name, tag_id, constraint).map_err(Into::into)
}
fn commit<Name, E>(
&self,
reference: Name,
author: &git_actor::SignatureRef<'_>,
committer: &git_actor::SignatureRef<'_>,
message: impl AsRef<str>,
tree: impl Into<ObjectId>,
parents: impl IntoIterator<Item = impl Into<ObjectId>>,
) -> Result<Oid<'_, Self>, commit::Error>
where
Name: TryInto<FullName, Error = E>,
commit::Error: From<E>,
{
use git_ref::{
transaction::{Change, RefEdit},
Target,
};
use crate::easy::ext::ReferenceAccessExt;
let reference = reference.try_into()?;
let commit = git_object::Commit {
message: message.as_ref().into(),
tree: tree.into(),
author: author.to_owned(),
committer: committer.to_owned(),
encoding: None,
parents: parents.into_iter().map(|id| id.into()).collect(),
extra_headers: Default::default(),
};
let commit_id = self.write_object(&commit)?;
self.edit_reference(
RefEdit {
change: Change::Update {
log: LogChange {
mode: RefLog::AndReference,
force_create_reflog: false,
message: crate::reference::log::message(
"commit",
commit.message.as_ref(),
commit.parents.len(),
),
},
expected: match commit.parents.get(0).map(|p| Target::Peeled(*p)) {
Some(previous) => {
if reference.as_bstr() == "HEAD" {
PreviousValue::MustExistAndMatch(previous)
} else {
PreviousValue::ExistingMustMatch(previous)
}
}
None => PreviousValue::MustNotExist,
},
new: Target::Peeled(commit_id.inner),
},
name: reference,
deref: true,
},
git_lock::acquire::Fail::Immediately,
Some(&commit.committer),
)?;
Ok(commit_id)
}
}
impl<A> ObjectAccessExt for A where A: easy::Access + Sized {}