gix_pack/data/output/entry/
mod.rs1use std::io::Write;
2
3use gix_hash::ObjectId;
4
5use crate::{data, data::output, find};
6
7pub mod iter_from_counts;
9pub use iter_from_counts::function::iter_from_counts;
10
11#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
13#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
14pub enum Kind {
15 Base(gix_object::Kind),
17 DeltaRef {
20 object_index: usize,
23 },
24 DeltaOid {
29 id: ObjectId,
31 },
32}
33
34#[allow(missing_docs)]
36#[derive(Debug, thiserror::Error)]
37pub enum Error {
38 #[error("{0}")]
39 ZlibDeflate(#[from] std::io::Error),
40 #[error(transparent)]
41 EntryType(#[from] crate::data::entry::decode::Error),
42}
43
44impl output::Entry {
45 pub fn invalid() -> output::Entry {
47 output::Entry {
48 id: gix_hash::Kind::Sha1.null(), kind: Kind::Base(gix_object::Kind::Blob),
50 decompressed_size: 0,
51 compressed_data: vec![],
52 }
53 }
54
55 pub fn is_invalid(&self) -> bool {
60 self.id.is_null()
61 }
62
63 pub fn from_pack_entry(
66 mut entry: find::Entry,
67 count: &output::Count,
68 potential_bases: &[output::Count],
69 bases_index_offset: usize,
70 pack_offset_to_oid: Option<impl FnMut(u32, u64) -> Option<ObjectId>>,
71 target_version: data::Version,
72 ) -> Option<Result<Self, Error>> {
73 if entry.version != target_version {
74 return None;
75 };
76
77 let pack_offset_must_be_zero = 0;
78 let pack_entry = match data::Entry::from_bytes(&entry.data, pack_offset_must_be_zero, count.id.as_slice().len())
79 {
80 Ok(e) => e,
81 Err(err) => return Some(Err(err.into())),
82 };
83
84 use crate::data::entry::Header::*;
85 match pack_entry.header {
86 Commit => Some(output::entry::Kind::Base(gix_object::Kind::Commit)),
87 Tree => Some(output::entry::Kind::Base(gix_object::Kind::Tree)),
88 Blob => Some(output::entry::Kind::Base(gix_object::Kind::Blob)),
89 Tag => Some(output::entry::Kind::Base(gix_object::Kind::Tag)),
90 OfsDelta { base_distance } => {
91 let pack_location = count.entry_pack_location.as_ref().expect("packed");
92 let base_offset = pack_location
93 .pack_offset
94 .checked_sub(base_distance)
95 .expect("pack-offset - distance is firmly within the pack");
96 potential_bases
97 .binary_search_by(|e| {
98 e.entry_pack_location
99 .as_ref()
100 .expect("packed")
101 .pack_offset
102 .cmp(&base_offset)
103 })
104 .ok()
105 .map(|idx| output::entry::Kind::DeltaRef {
106 object_index: idx + bases_index_offset,
107 })
108 .or_else(|| {
109 pack_offset_to_oid
110 .and_then(|mut f| f(pack_location.pack_id, base_offset))
111 .map(|id| output::entry::Kind::DeltaOid { id })
112 })
113 }
114 RefDelta { base_id: _ } => None, }
116 .map(|kind| {
117 Ok(output::Entry {
118 id: count.id.to_owned(),
119 kind,
120 decompressed_size: pack_entry.decompressed_size as usize,
121 compressed_data: {
122 entry.data.copy_within(pack_entry.data_offset as usize.., 0);
123 entry.data.resize(
124 entry.data.len()
125 - usize::try_from(pack_entry.data_offset).expect("offset representable as usize"),
126 0,
127 );
128 entry.data
129 },
130 })
131 })
132 }
133
134 pub fn from_data(count: &output::Count, obj: &gix_object::Data<'_>) -> Result<Self, Error> {
136 Ok(output::Entry {
137 id: count.id.to_owned(),
138 kind: Kind::Base(obj.kind),
139 decompressed_size: obj.data.len(),
140 compressed_data: {
141 let mut out = gix_features::zlib::stream::deflate::Write::new(Vec::new());
142 if let Err(err) = std::io::copy(&mut &*obj.data, &mut out) {
143 match err.kind() {
144 std::io::ErrorKind::Other => return Err(Error::ZlibDeflate(err)),
145 err => unreachable!("Should never see other errors than zlib, but got {:?}", err,),
146 }
147 };
148 out.flush()?;
149 out.into_inner()
150 },
151 })
152 }
153
154 pub fn to_entry_header(
160 &self,
161 version: data::Version,
162 index_to_base_distance: impl FnOnce(usize) -> u64,
163 ) -> data::entry::Header {
164 assert!(
165 matches!(version, data::Version::V2),
166 "we can only write V2 pack entries for now"
167 );
168
169 use Kind::*;
170 match self.kind {
171 Base(kind) => {
172 use gix_object::Kind::*;
173 match kind {
174 Tree => data::entry::Header::Tree,
175 Blob => data::entry::Header::Blob,
176 Commit => data::entry::Header::Commit,
177 Tag => data::entry::Header::Tag,
178 }
179 }
180 DeltaOid { id } => data::entry::Header::RefDelta { base_id: id.to_owned() },
181 DeltaRef { object_index } => data::entry::Header::OfsDelta {
182 base_distance: index_to_base_distance(object_index),
183 },
184 }
185 }
186}