History

Use `git-chunk` crate for all chunk-related operations (#279)

Sebastian Thiel committed 4 years ago

Commit 0cd7f3b1 parent e14096e1 file · +47 −70Browse files

Showing the changes to git-commitgraph/src (1 of 1 file). Show all

Unified · Split

git-commitgraph/src/file/init.rs+47−70
@@ -44,6 +44,8 @@ pub enum Error { · 43 unmodified lines
4444 },
4545 #[error("Missing required chunk {:?}", .0.as_bstr())]
4646 MissingChunk(ChunkId),
47+ #[error(transparent)]
48+ MissingChunk2(#[from] git_chunk::file::index::offset_by_kind::Error),
4749 #[error("{0}")]
4850 Trailer(String),
4951 #[error("Commit-graph file uses unsupported hash version: {0}")]
@@ -116,41 +118,30 @@ impl TryFrom<&Path> for File { · 66 unmodified lines
116118
117119 let chunks = git_chunk::file::Index::from_bytes(&data, ofs, chunk_count as u32)?;
118120
119− let mut base_graphs_list_offset: Option<usize> = None;
120− let mut commit_data_offset: Option<usize> = None;
121− let mut commit_data_count = 0u32;
122− let mut fan_offset: Option<usize> = None;
123− let mut oid_lookup_offset: Option<usize> = None;
124− let mut oid_lookup_count = 0u32;
125−
126− if let Some(chunk_range) = chunks
127− .offset_by_id(BASE_GRAPHS_LIST_CHUNK_ID)
128− .ok()
129− .and_then(git_chunk::range::into_usize)
130− {
131− let chunk_size = chunk_range.len();
132− if chunk_size % SHA1_SIZE != 0 {
133− return Err(Error::InvalidChunkSize {
134− id: BASE_GRAPHS_LIST_CHUNK_ID,
135− msg: format!("chunk size {} is not a multiple of {}", chunk_size, SHA1_SIZE),
136− });
137− }
138− let chunk_base_graph_count: u32 = (chunk_size / SHA1_SIZE)
139− .try_into()
140− .expect("base graph count to fit in 32-bits");
141− if chunk_base_graph_count != u32::from(base_graph_count) {
142− return Err(Error::BaseGraphMismatch {
143− from_chunk: chunk_base_graph_count,
144− from_header: base_graph_count,
145− });
121+ let base_graphs_list_offset = match chunks.usize_offset_by_id(BASE_GRAPHS_LIST_CHUNK_ID).ok() {
122+ Some(chunk_range) => {
123+ let chunk_size = chunk_range.len();
124+ if chunk_size % SHA1_SIZE != 0 {
125+ return Err(Error::InvalidChunkSize {
126+ id: BASE_GRAPHS_LIST_CHUNK_ID,
127+ msg: format!("chunk size {} is not a multiple of {}", chunk_size, SHA1_SIZE),
128+ });
129+ }
130+ let chunk_base_graph_count: u32 = (chunk_size / SHA1_SIZE)
131+ .try_into()
132+ .expect("base graph count to fit in 32-bits");
133+ if chunk_base_graph_count != u32::from(base_graph_count) {
134+ return Err(Error::BaseGraphMismatch {
135+ from_chunk: chunk_base_graph_count,
136+ from_header: base_graph_count,
137+ });
138+ }
139+ Some(chunk_range.start)
146140 }
147− base_graphs_list_offset = Some(chunk_range.start);
148− }
149− if let Some(chunk_range) = chunks
150− .offset_by_id(COMMIT_DATA_CHUNK_ID)
151− .ok()
152− .and_then(git_chunk::range::into_usize)
153− {
141+ None => None,
142+ };
143+ let (commit_data_offset, commit_data_count) = {
144+ let chunk_range = chunks.usize_offset_by_id(COMMIT_DATA_CHUNK_ID)?;
154145 let chunk_size = chunk_range.len();
155146
156147 if chunk_size % COMMIT_DATA_ENTRY_SIZE != 0 {
@@ -162,22 +153,21 @@ impl TryFrom<&Path> for File { · 5 unmodified lines
162153 ),
163154 });
164155 }
165− commit_data_offset = Some(chunk_range.start);
166− commit_data_count = (chunk_size / COMMIT_DATA_ENTRY_SIZE)
167− .try_into()
168− .expect("number of commits in CDAT chunk to fit in 32 bits");
169− }
156+ (
157+ chunk_range.start,
158+ (chunk_size / COMMIT_DATA_ENTRY_SIZE)
159+ .try_into()
160+ .expect("number of commits in CDAT chunk to fit in 32 bits"),
161+ )
162+ };
170163
171164 let extra_edges_list_range = chunks
172165 .offset_by_id(EXTENDED_EDGES_LIST_CHUNK_ID)
173166 .ok()
174167 .and_then(git_chunk::range::into_usize);
175168
176− if let Some(chunk_range) = chunks
177− .offset_by_id(OID_FAN_CHUNK_ID)
178− .ok()
179− .and_then(git_chunk::range::into_usize)
180− {
169+ let fan_offset = {
170+ let chunk_range = chunks.usize_offset_by_id(OID_FAN_CHUNK_ID)?;
181171 let chunk_size = chunk_range.len();
182172
183173 let expected_size = 4 * FAN_LEN;
@@ -187,39 +177,29 @@ impl TryFrom<&Path> for File { · 3 unmodified lines
187177 msg: format!("expected chunk length {}, got {}", expected_size, chunk_size),
188178 });
189179 }
190− fan_offset = Some(chunk_range.start);
191− }
180+ chunk_range.start
181+ };
192182
193− if let Some(chunk_range) = chunks
194− .offset_by_id(OID_LOOKUP_CHUNK_ID)
195− .ok()
196− .and_then(git_chunk::range::into_usize)
197− {
183+ let (oid_lookup_offset, oid_lookup_count) = {
184+ let chunk_range = chunks.usize_offset_by_id(OID_LOOKUP_CHUNK_ID)?;
198185 let chunk_size = chunk_range.len();
199186
200187 if chunk_size % OID_LOOKUP_ENTRY_SIZE != 0 {
201188 return Err(Error::InvalidChunkSize {
202− id: OID_FAN_CHUNK_ID,
189+ id: OID_LOOKUP_CHUNK_ID,
203190 msg: format!(
204191 "chunk size {} is not a multiple of {}",
205192 chunk_size, OID_LOOKUP_ENTRY_SIZE
206193 ),
207194 });
208195 }
209− oid_lookup_offset = Some(chunk_range.start);
210− oid_lookup_count = (chunk_size / OID_LOOKUP_ENTRY_SIZE)
211− .try_into()
212− .expect("number of commits in OIDL chunk to fit in 32 bits");
213− // TODO(ST): Figure out how to handle this. Don't know what to do with the commented code.
214− // git allows extra garbage in the extra edges list chunk?
215− // if oid_lookup_count > 0 {
216− // let last_edge = &data[next_chunk_offset - 4..next_chunk_offset];
217− // let last_edge = BigEndian::read_u32(last_edge);
218− // if let ExtraEdge::Internal(_) = ExtraEdge::from_raw(last_edge) {
219− // return Err(Error::ExtraEdgesListOverflow);
220− // }
221− // }
222− }
196+ (
197+ chunk_range.start,
198+ (chunk_size / OID_LOOKUP_ENTRY_SIZE)
199+ .try_into()
200+ .expect("number of commits in OIDL chunk to fit in 32 bits"),
201+ )
202+ };
223203
224204 let trailer = &data[chunks.highest_offset() as usize..];
225205 if trailer.len() != TRAILER_LEN {
@@ -230,10 +210,7 @@ impl TryFrom<&Path> for File { · 4 unmodified lines
230210 )));
231211 }
232212
233− let fan_offset = fan_offset.ok_or(Error::MissingChunk(OID_FAN_CHUNK_ID))?;
234− let oid_lookup_offset = oid_lookup_offset.ok_or(Error::MissingChunk(OID_LOOKUP_CHUNK_ID))?;
235− let commit_data_offset = commit_data_offset.ok_or(Error::MissingChunk(COMMIT_DATA_CHUNK_ID))?;
236− if base_graph_count > 0 && base_graphs_list_offset == None {
213+ if base_graph_count > 0 && base_graphs_list_offset.is_none() {
237214 return Err(Error::MissingChunk(BASE_GRAPHS_LIST_CHUNK_ID));
238215 }
239216