History

refactor (#279)

Sebastian Thiel committed 4 years ago

Commit 501b85b1 parent 8b8b4c51 file · +50 −53Browse files

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

Unified · Split

git-commitgraph/src/file/init.rs+50−53
@@ -114,8 +114,8 @@ impl TryFrom<&Path> for File { · 113 unmodified lines
114 114
115 let chunks = git_chunk::file::Index::from_bytes(&data, ofs, chunk_count as u32)?;115 let chunks = git_chunk::file::Index::from_bytes(&data, ofs, chunk_count as u32)?;
116 116
117 let base_graphs_list_offset = match chunks.usize_offset_by_id(BASE_GRAPHS_LIST_CHUNK_ID).ok() {117 let base_graphs_list_offset = chunks
118 Some(chunk_range) => {118 .validated_usize_offset_by_id(BASE_GRAPHS_LIST_CHUNK_ID, |chunk_range| {
119 let chunk_size = chunk_range.len();119 let chunk_size = chunk_range.len();
120 if chunk_size % SHA1_SIZE != 0 {120 if chunk_size % SHA1_SIZE != 0 {
121 return Err(Error::InvalidChunkSize {121 return Err(Error::InvalidChunkSize {
@@ -132,38 +132,33 @@ impl TryFrom<&Path> for File { · 10 unmodified lines
132 from_header: base_graph_count,132 from_header: base_graph_count,
133 });133 });
134 }134 }
135 Some(chunk_range.start)135 Ok(chunk_range.start)
136 }136 })
137 None => None,
138 };
139 let (commit_data_offset, commit_data_count) = {
140 let chunk_range = chunks.usize_offset_by_id(COMMIT_DATA_CHUNK_ID)?;
141 let chunk_size = chunk_range.len();
142
143 if chunk_size % COMMIT_DATA_ENTRY_SIZE != 0 {
144 return Err(Error::InvalidChunkSize {
145 id: COMMIT_DATA_CHUNK_ID,
146 msg: format!(
147 "chunk size {} is not a multiple of {}",
148 chunk_size, COMMIT_DATA_ENTRY_SIZE
149 ),
150 });
151 }
152 (
153 chunk_range.start,
154 (chunk_size / COMMIT_DATA_ENTRY_SIZE)
155 .try_into()
156 .expect("number of commits in CDAT chunk to fit in 32 bits"),
157 )
158 };
159
160 let extra_edges_list_range = chunks
161 .offset_by_id(EXTENDED_EDGES_LIST_CHUNK_ID)
162 .ok()137 .ok()
163 .and_then(git_chunk::range::into_usize);138 .transpose()?;
164 139
165 let fan_offset = {140 let (commit_data_offset, commit_data_count) =
166 let chunk_range = chunks.usize_offset_by_id(OID_FAN_CHUNK_ID)?;141 chunks.validated_usize_offset_by_id(COMMIT_DATA_CHUNK_ID, |chunk_range| {
142 let chunk_size = chunk_range.len();
143
144 if chunk_size % COMMIT_DATA_ENTRY_SIZE != 0 {
145 return Err(Error::InvalidChunkSize {
146 id: COMMIT_DATA_CHUNK_ID,
147 msg: format!(
148 "chunk size {} is not a multiple of {}",
149 chunk_size, COMMIT_DATA_ENTRY_SIZE
150 ),
151 });
152 }
153 Ok((
154 chunk_range.start,
155 (chunk_size / COMMIT_DATA_ENTRY_SIZE)
156 .try_into()
157 .expect("number of commits in CDAT chunk to fit in 32 bits"),
158 ))
159 })??;
160
161 let fan_offset = chunks.validated_usize_offset_by_id(OID_FAN_CHUNK_ID, |chunk_range| {
167 let chunk_size = chunk_range.len();162 let chunk_size = chunk_range.len();
168 163
169 let expected_size = 4 * FAN_LEN;164 let expected_size = 4 * FAN_LEN;
@@ -173,29 +168,31 @@ impl TryFrom<&Path> for File { · 3 unmodified lines
173 msg: format!("expected chunk length {}, got {}", expected_size, chunk_size),168 msg: format!("expected chunk length {}, got {}", expected_size, chunk_size),
174 });169 });
175 }170 }
176 chunk_range.start171 Ok(chunk_range.start)
177 };172 })??;
178 173
179 let (oid_lookup_offset, oid_lookup_count) = {174 let (oid_lookup_offset, oid_lookup_count) =
180 let chunk_range = chunks.usize_offset_by_id(OID_LOOKUP_CHUNK_ID)?;175 chunks.validated_usize_offset_by_id(OID_LOOKUP_CHUNK_ID, |chunk_range| {
181 let chunk_size = chunk_range.len();176 let chunk_size = chunk_range.len();
182 177
183 if chunk_size % OID_LOOKUP_ENTRY_SIZE != 0 {178 if chunk_size % OID_LOOKUP_ENTRY_SIZE != 0 {
184 return Err(Error::InvalidChunkSize {179 return Err(Error::InvalidChunkSize {
185 id: OID_LOOKUP_CHUNK_ID,180 id: OID_LOOKUP_CHUNK_ID,
186 msg: format!(181 msg: format!(
187 "chunk size {} is not a multiple of {}",182 "chunk size {} is not a multiple of {}",
188 chunk_size, OID_LOOKUP_ENTRY_SIZE183 chunk_size, OID_LOOKUP_ENTRY_SIZE
189 ),184 ),
190 });185 });
191 }186 }
192 (187 Ok((
193 chunk_range.start,188 chunk_range.start,
194 (chunk_size / OID_LOOKUP_ENTRY_SIZE)189 (chunk_size / OID_LOOKUP_ENTRY_SIZE)
195 .try_into()190 .try_into()
196 .expect("number of commits in OIDL chunk to fit in 32 bits"),191 .expect("number of commits in OIDL chunk to fit in 32 bits"),
197 )192 ))
198 };193 })??;
194
195 let extra_edges_list_range = chunks.usize_offset_by_id(EXTENDED_EDGES_LIST_CHUNK_ID).ok();
199 196
200 let trailer = &data[chunks.highest_offset() as usize..];197 let trailer = &data[chunks.highest_offset() as usize..];
201 if trailer.len() != TRAILER_LEN {198 if trailer.len() != TRAILER_LEN {