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
use std::ops::Deref;
use git_hash::{oid, ObjectId};
use crate::{object::find, revision, Id, Object};
impl<'repo> Id<'repo> {
pub fn object(&self) -> Result<Object<'repo>, find::existing::Error> {
self.repo.find_object(self.inner)
}
pub fn try_object(&self) -> Result<Option<Object<'repo>>, find::Error> {
self.repo.try_find_object(self.inner)
}
pub fn shorten(&self) -> Result<git_hash::Prefix, shorten::Error> {
let hex_len = self.repo.config.hex_len.map_or_else(
|| self.repo.objects.packed_object_count().map(calculate_auto_hex_len),
Ok,
)?;
let prefix = git_odb::store::prefix::disambiguate::Candidate::new(self.inner, hex_len)
.expect("BUG: internal hex-len must always be valid");
self.repo
.objects
.disambiguate_prefix(prefix)?
.ok_or(shorten::Error::NotFound { oid: self.inner })
}
pub fn shorten_or_id(&self) -> git_hash::Prefix {
self.shorten().unwrap_or_else(|_| self.inner.into())
}
}
fn calculate_auto_hex_len(num_packed_objects: u64) -> usize {
let mut len = 64 - num_packed_objects.leading_zeros();
len = (len + 1) / 2;
len.max(7) as usize
}
pub mod shorten {
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
#[error(transparent)]
PackedObjectsCount(#[from] git_odb::store::load_index::Error),
#[error(transparent)]
DisambiguatePrefix(#[from] git_odb::store::prefix::disambiguate::Error),
#[error("Id could not be shortened as the object with id {} could not be found", .oid)]
NotFound { oid: git_hash::ObjectId },
}
}
impl<'repo> Deref for Id<'repo> {
type Target = oid;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<'repo> Id<'repo> {
pub(crate) fn from_id(id: impl Into<ObjectId>, repo: &'repo crate::Repository) -> Self {
Id { inner: id.into(), repo }
}
pub fn detach(self) -> ObjectId {
self.inner
}
}
impl<'repo> Id<'repo> {
pub fn ancestors(&self) -> revision::walk::Platform<'repo> {
revision::walk::Platform::new(Some(self.inner), self.repo)
}
}
mod impls {
use std::{cmp::Ordering, hash::Hasher};
use git_hash::{oid, ObjectId};
use crate::{Id, Object, ObjectDetached};
impl<'a> std::hash::Hash for Id<'a> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.inner.hash(state)
}
}
impl<'a> PartialOrd<Id<'a>> for Id<'a> {
fn partial_cmp(&self, other: &Id<'a>) -> Option<Ordering> {
self.inner.partial_cmp(&other.inner)
}
}
impl<'repo> PartialEq<Id<'repo>> for Id<'repo> {
fn eq(&self, other: &Id<'repo>) -> bool {
self.inner == other.inner
}
}
impl<'repo> PartialEq<ObjectId> for Id<'repo> {
fn eq(&self, other: &ObjectId) -> bool {
&self.inner == other
}
}
impl<'repo> PartialEq<Id<'repo>> for ObjectId {
fn eq(&self, other: &Id<'repo>) -> bool {
self == &other.inner
}
}
impl<'repo> PartialEq<oid> for Id<'repo> {
fn eq(&self, other: &oid) -> bool {
self.inner == other
}
}
impl<'repo> PartialEq<Object<'repo>> for Id<'repo> {
fn eq(&self, other: &Object<'repo>) -> bool {
self.inner == other.id
}
}
impl<'repo> PartialEq<ObjectDetached> for Id<'repo> {
fn eq(&self, other: &ObjectDetached) -> bool {
self.inner == other.id
}
}
impl<'repo> std::fmt::Debug for Id<'repo> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.inner.fmt(f)
}
}
impl<'repo> std::fmt::Display for Id<'repo> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.inner.fmt(f)
}
}
impl<'repo> AsRef<oid> for Id<'repo> {
fn as_ref(&self) -> &oid {
&self.inner
}
}
impl<'repo> From<Id<'repo>> for ObjectId {
fn from(v: Id<'repo>) -> Self {
v.inner
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn size_of_oid() {
assert_eq!(
std::mem::size_of::<Id<'_>>(),
32,
"size of oid shouldn't change without notice"
)
}
}