From 2888f1b10a2baf40155544e667ddd461f3ddc938 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Mon, 13 Jul 2020 20:57:05 +0800 Subject: [PATCH] refactor --- demos/git-count.rs | 4 +- git-odb/src/loose/db/mod.rs | 6 +- git-odb/src/loose/object.rs | 2 +- git-odb/src/pack/bundle.rs | 16 +++--- git-odb/src/pack/{file => data}/decode.rs | 10 ++-- git-odb/src/pack/{file => data}/decoded.rs | 0 .../src/pack/{file/mod.rs => data/file.rs} | 55 ++++--------------- git-odb/src/pack/data/mod.rs | 37 +++++++++++++ git-odb/src/pack/{file => data}/verify.rs | 4 +- git-odb/src/pack/index/mod.rs | 2 +- git-odb/src/pack/index/verify.rs | 20 +++---- git-odb/src/pack/mod.rs | 3 +- git-odb/tests/pack/file.rs | 6 +- git-odb/tests/pack/index.rs | 6 +- git-repository/src/init.rs | 4 +- gitoxide-core/src/lib.rs | 12 ++-- src/plumbing/lean.rs | 2 +- src/plumbing/pretty.rs | 4 +- 18 files changed, 99 insertions(+), 94 deletions(-) rename git-odb/src/pack/{file => data}/decode.rs (98%) rename git-odb/src/pack/{file => data}/decoded.rs (100%) rename git-odb/src/pack/{file/mod.rs => data/file.rs} (59%) create mode 100644 git-odb/src/pack/data/mod.rs rename git-odb/src/pack/{file => data}/verify.rs (95%) diff --git a/demos/git-count.rs b/demos/git-count.rs index 3422f96b291..26fcb01eced 100644 --- a/demos/git-count.rs +++ b/demos/git-count.rs @@ -19,8 +19,8 @@ fn run() -> Result<()> { } }; let index = odb::pack::index::File::at(index)?; - let pack = odb::pack::File::at(pack)?; - use odb::pack::decoded::Header::*; + let pack = odb::pack::data::File::at(pack)?; + use odb::pack::data::decoded::Header::*; writeln!( stdout(), diff --git a/git-odb/src/loose/db/mod.rs b/git-odb/src/loose/db/mod.rs index 70e8f7ee2b1..079b3e03398 100644 --- a/git-odb/src/loose/db/mod.rs +++ b/git-odb/src/loose/db/mod.rs @@ -39,7 +39,7 @@ quick_error! { display("{}", msg) } Io(err: std::io::Error, action: &'static str, path: PathBuf) { - display("Could not {} file at '{}'", action, path.display()) + display("Could not {} data at '{}'", action, path.display()) cause(err) } } @@ -149,7 +149,7 @@ impl Db { object::Kind::Tag | object::Kind::Commit | object::Kind::Tree => { let mut compressed = SmallVec::from_buf(compressed); // Read small objects right away and store them in memory while we - // have a file handle available and 'hot'. Note that we don't decompress yet! + // have a data handle available and 'hot'. Note that we don't decompress yet! let file_size = input_stream .metadata() .map_err(|e| Error::Io(e, "read metadata", path.to_owned()))? @@ -172,7 +172,7 @@ impl Db { (compressed, None) } } - object::Kind::Blob => (SmallVec::default(), Some(path)), // we will open the file again when needed. Maybe we can load small sized objects anyway + object::Kind::Blob => (SmallVec::default(), Some(path)), // we will open the data again when needed. Maybe we can load small sized objects anyway } }; diff --git a/git-odb/src/loose/object.rs b/git-odb/src/loose/object.rs index 1b79ce037b3..acc7317695a 100644 --- a/git-odb/src/loose/object.rs +++ b/git-odb/src/loose/object.rs @@ -24,7 +24,7 @@ quick_error! { cause(err) } Io(err: std::io::Error, action: &'static str, path: PathBuf) { - display("Could not {} file at '{}'", action, path.display()) + display("Could not {} data at '{}'", action, path.display()) cause(err) } } diff --git a/git-odb/src/pack/bundle.rs b/git-odb/src/pack/bundle.rs index 88f41395a08..ba6e7e56d19 100644 --- a/git-odb/src/pack/bundle.rs +++ b/git-odb/src/pack/bundle.rs @@ -10,9 +10,9 @@ quick_error! { #[derive(Debug)] pub enum Error { InvalidPath(path: PathBuf) { - display("An 'idx' extension is expected of an index file: '{}'", path.display()) + display("An 'idx' extension is expected of an index data: '{}'", path.display()) } - Pack(err: pack::Error) { + Pack(err: pack::data::Error) { display("Could not instantiate pack") from() cause(err) @@ -22,7 +22,7 @@ quick_error! { from() cause(err) } - Decode(err: pack::decode::Error) { + Decode(err: pack::data::decode::Error) { display("Could not decode object") } } @@ -30,12 +30,12 @@ quick_error! { /// A packfile with an index pub struct Bundle { - pack: pack::File, + pack: pack::data::File, index: pack::index::File, } impl Bundle { - /// `path` is either a pack file or an index file + /// `path` is either a pack data or an index data pub fn at(path: impl AsRef) -> Result { Self::try_from(path.as_ref()) } @@ -60,7 +60,7 @@ impl Bundle { out, |id, _out| { self.index.lookup_index(id).map(|idx| { - pack::decode::ResolvedBase::InPack(self.pack.entry(self.index.pack_offset_at_index(idx))) + pack::data::decode::ResolvedBase::InPack(self.pack.entry(self.index.pack_offset_at_index(idx))) }) }, cache, @@ -85,10 +85,10 @@ impl TryFrom<&Path> for Bundle { Ok(match ext { "idx" => Self { index: pack::index::File::at(path)?, - pack: pack::File::at(path.with_extension("pack"))?, + pack: pack::data::File::at(path.with_extension("pack"))?, }, "pack" => Self { - pack: pack::File::at(path)?, + pack: pack::data::File::at(path)?, index: pack::index::File::at(path.with_extension("idx"))?, }, _ => return Err(Error::InvalidPath(path.to_owned())), diff --git a/git-odb/src/pack/file/decode.rs b/git-odb/src/pack/data/decode.rs similarity index 98% rename from git-odb/src/pack/file/decode.rs rename to git-odb/src/pack/data/decode.rs index ef582c205af..1e8c3096b24 100644 --- a/git-odb/src/pack/file/decode.rs +++ b/git-odb/src/pack/data/decode.rs @@ -1,6 +1,6 @@ use crate::{ pack::cache, - pack::{decoded, File}, + pack::data::{decoded, File}, zlib::Inflate, }; use git_object as object; @@ -85,7 +85,7 @@ impl File { } /// Currently only done during pack verification - finding the right size is only possible by decompressing - /// the pack entry beforehand, or by using the (to be sorted) offsets stored in an index file. + /// the pack entry beforehand, or by using the (to be sorted) offsets stored in an index data. pub fn entry_crc32(&self, pack_offset: u64, size: usize) -> u32 { let pack_offset: usize = pack_offset.try_into().expect("pack_size fits into usize"); git_features::hash::crc32(&self.data[pack_offset..pack_offset + size]) @@ -93,7 +93,7 @@ impl File { fn assure_v2(&self) { assert!( - if let crate::pack::Kind::V2 = self.kind.clone() { + if let crate::pack::data::Kind::V2 = self.kind.clone() { true } else { false @@ -142,7 +142,7 @@ impl File { resolve: impl Fn(&object::Id, &mut Vec) -> Option, cache: &mut impl cache::DecodeEntry, ) -> Result { - use crate::pack::decoded::Header::*; + use crate::pack::data::decoded::Header::*; match entry.header { Tree | Blob | Commit | Tag => { out.resize( @@ -174,7 +174,7 @@ impl File { out: &mut Vec, cache: &mut impl cache::DecodeEntry, ) -> Result { - use crate::pack::decoded::Header; + use crate::pack::data::decoded::Header; // all deltas, from the one that produces the desired object (first) to the oldest at the end of the chain let mut chain = SmallVec::<[Delta; 10]>::default(); let first_entry = last.clone(); diff --git a/git-odb/src/pack/file/decoded.rs b/git-odb/src/pack/data/decoded.rs similarity index 100% rename from git-odb/src/pack/file/decoded.rs rename to git-odb/src/pack/data/decoded.rs diff --git a/git-odb/src/pack/file/mod.rs b/git-odb/src/pack/data/file.rs similarity index 59% rename from git-odb/src/pack/file/mod.rs rename to git-odb/src/pack/data/file.rs index eafce9b651a..443a215b51b 100644 --- a/git-odb/src/pack/file/mod.rs +++ b/git-odb/src/pack/data/file.rs @@ -1,20 +1,15 @@ +use crate::pack::data; use byteorder::{BigEndian, ByteOrder}; use filebuffer::FileBuffer; use git_object::SHA1_SIZE; use quick_error::quick_error; use std::{convert::TryFrom, mem::size_of, path::Path}; -pub mod decode; - -pub mod decoded; - -pub mod verify; - quick_error! { #[derive(Debug)] pub enum Error { Io(err: std::io::Error, path: std::path::PathBuf) { - display("Could not open pack file at '{}'", path.display()) + display("Could not open pack data at '{}'", path.display()) cause(err) } Corrupt(msg: String) { @@ -28,40 +23,14 @@ quick_error! { const N32_SIZE: usize = size_of::(); -#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)] -pub enum Kind { - V2, - V3, -} - -pub struct File { - data: FileBuffer, - path: std::path::PathBuf, - kind: Kind, - num_objects: u32, -} - -/// Instantiation and basic file information -impl File { - pub fn kind(&self) -> Kind { - self.kind.clone() - } - pub fn num_objects(&self) -> u32 { - self.num_objects - } - pub fn data_len(&self) -> usize { - self.data.len() - } - pub fn path(&self) -> &Path { - &self.path - } - - pub fn at(path: impl AsRef) -> Result { - File::try_from(path.as_ref()) +/// Instantiation +impl data::File { + pub fn at(path: impl AsRef) -> Result { + data::File::try_from(path.as_ref()) } } -impl TryFrom<&Path> for File { +impl TryFrom<&Path> for data::File { type Error = Error; fn try_from(path: &Path) -> Result { @@ -69,24 +38,24 @@ impl TryFrom<&Path> for File { let pack_len = data.len(); if pack_len < N32_SIZE * 3 + SHA1_SIZE { return Err(Error::Corrupt(format!( - "Pack file of size {} is too small for even an empty pack", + "Pack data of size {} is too small for even an empty pack", pack_len ))); } let mut ofs = 0; if &data[ofs..ofs + b"PACK".len()] != b"PACK" { - return Err(Error::Corrupt("Pack file type not recognized".into())); + return Err(Error::Corrupt("Pack data type not recognized".into())); } ofs += N32_SIZE; let kind = match BigEndian::read_u32(&data[ofs..ofs + N32_SIZE]) { - 2 => Kind::V2, - 3 => Kind::V3, + 2 => data::Kind::V2, + 3 => data::Kind::V3, v => return Err(Error::UnsupportedVersion(v)), }; ofs += N32_SIZE; let num_objects = BigEndian::read_u32(&data[ofs..ofs + N32_SIZE]); - Ok(File { + Ok(data::File { data, path: path.to_owned(), kind, diff --git a/git-odb/src/pack/data/mod.rs b/git-odb/src/pack/data/mod.rs new file mode 100644 index 00000000000..f7c5ea008da --- /dev/null +++ b/git-odb/src/pack/data/mod.rs @@ -0,0 +1,37 @@ +use filebuffer::FileBuffer; +use std::path::Path; + +pub mod decode; +pub mod decoded; +pub mod verify; + +mod file; +pub use file::*; + +#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)] +pub enum Kind { + V2, + V3, +} + +pub struct File { + data: FileBuffer, + path: std::path::PathBuf, + kind: Kind, + num_objects: u32, +} + +impl File { + pub fn kind(&self) -> Kind { + self.kind.clone() + } + pub fn num_objects(&self) -> u32 { + self.num_objects + } + pub fn data_len(&self) -> usize { + self.data.len() + } + pub fn path(&self) -> &Path { + &self.path + } +} diff --git a/git-odb/src/pack/file/verify.rs b/git-odb/src/pack/data/verify.rs similarity index 95% rename from git-odb/src/pack/file/verify.rs rename to git-odb/src/pack/data/verify.rs index 0b9bd904446..e8fa0d191ac 100644 --- a/git-odb/src/pack/file/verify.rs +++ b/git-odb/src/pack/data/verify.rs @@ -1,4 +1,4 @@ -use crate::pack::File; +use crate::pack::data::File; use git_object::{self as object, SHA1_SIZE}; use quick_error::quick_error; @@ -9,7 +9,7 @@ quick_error! { display("pack checksum mismatch: expected {}, got {}", expected, actual) } Io(err: std::io::Error) { - display("could not read pack file") + display("could not read pack data") from() cause(err) } diff --git a/git-odb/src/pack/index/mod.rs b/git-odb/src/pack/index/mod.rs index cf5d64a54d1..5564e71deb5 100644 --- a/git-odb/src/pack/index/mod.rs +++ b/git-odb/src/pack/index/mod.rs @@ -78,7 +78,7 @@ quick_error! { #[derive(Debug)] pub enum Error { Io(err: std::io::Error, path: std::path::PathBuf) { - display("Could not open pack index file at '{}'", path.display()) + display("Could not open pack index data at '{}'", path.display()) cause(err) } Corrupt(msg: String) { diff --git a/git-odb/src/pack/index/verify.rs b/git-odb/src/pack/index/verify.rs index c01daeed5d1..fab96e25469 100644 --- a/git-odb/src/pack/index/verify.rs +++ b/git-odb/src/pack/index/verify.rs @@ -1,7 +1,7 @@ use crate::{ pack, pack::index, - pack::{cache, decode::DecodeEntryOutcome}, + pack::{cache, data::decode::DecodeEntryOutcome}, }; use git_features::progress::{self, Progress}; use git_object::SHA1_SIZE; @@ -15,23 +15,23 @@ quick_error! { Mismatch { expected: git_object::Id, actual: git_object::Id } { display("index checksum mismatch: expected {}, got {}", expected, actual) } - PackChecksum(err: pack::verify::Error) { - display("The pack of this index file failed to verify its checksums") + PackChecksum(err: pack::data::verify::Error) { + display("The pack of this index data failed to verify its checksums") from() cause(err) } - PackDecode(err: pack::decode::Error, id: git_object::Id, offset: u64) { + PackDecode(err: pack::data::decode::Error, id: git_object::Id, offset: u64) { display("Object {} at offset {} could not be decoded", id, offset) cause(err) } PackMismatch { expected: git_object::Id, actual: git_object::Id } { - display("The packfiles checksum didn't match the index file checksum: expected {}, got {}", expected, actual) + display("The packfiles checksum didn't match the index data checksum: expected {}, got {}", expected, actual) } PackObjectMismatch { expected: git_object::Id, actual: git_object::Id, offset: u64, kind: git_object::Kind} { - display("The SHA1 of {} object at offset {} didn't match the checksum in the index file: expected {}, got {}", kind, offset, expected, actual) + display("The SHA1 of {} object at offset {} didn't match the checksum in the index data: expected {}, got {}", kind, offset, expected, actual) } Crc32Mismatch { expected: u32, actual: u32, offset: u64, kind: git_object::Kind} { - display("The CRC32 of {} object at offset {} didn't match the checksum in the index file: expected {}, got {}", kind, offset, expected, actual) + display("The CRC32 of {} object at offset {} didn't match the checksum in the index data: expected {}, got {}", kind, offset, expected, actual) } } } @@ -76,7 +76,7 @@ pub struct Outcome { pub pack_size: u64, } -/// Verify and validate the content of the index file +/// Verify and validate the content of the index data impl index::File { pub fn checksum_of_index(&self) -> git_object::Id { git_object::Id::from_20_bytes(&self.data[self.data.len() - SHA1_SIZE..]) @@ -92,7 +92,7 @@ impl index::File { /// is indeed as advertised via its SHA1 as stored in this index, as well as the CRC32 hash. pub fn verify_checksum_of_index( &self, - pack: Option<&pack::File>, + pack: Option<&pack::data::File>, thread_limit: Option, progress: Option

, make_cache: impl Fn() -> C + Send + Sync, @@ -102,7 +102,7 @@ impl index::File {

::SubProgress: Send, C: cache::DecodeEntry, { - use crate::pack::decode::ResolvedBase; + use crate::pack::data::decode::ResolvedBase; use git_features::parallel::{self, in_parallel_if}; let mut root = progress::DoOrDiscard::from(progress); diff --git a/git-odb/src/pack/mod.rs b/git-odb/src/pack/mod.rs index 99cafb45200..d91b16e4d20 100644 --- a/git-odb/src/pack/mod.rs +++ b/git-odb/src/pack/mod.rs @@ -2,8 +2,7 @@ pub mod index; pub mod cache; -mod file; -pub use self::file::*; +pub mod data; mod bundle; pub use bundle::{Bundle, Object}; diff --git a/git-odb/tests/pack/file.rs b/git-odb/tests/pack/file.rs index 72dc95ff168..03bd0336f10 100644 --- a/git-odb/tests/pack/file.rs +++ b/git-odb/tests/pack/file.rs @@ -2,8 +2,8 @@ use crate::fixture_path; use git_odb::pack; use std::convert::TryFrom; -fn pack_at(at: &str) -> pack::File { - pack::File::try_from(fixture_path(at).as_path()).unwrap() +fn pack_at(at: &str) -> pack::data::File { + pack::data::File::try_from(fixture_path(at).as_path()).unwrap() } mod method { @@ -27,7 +27,7 @@ mod method { mod decode_entry { use crate::{fixture_path, fixup, pack::file::pack_at, pack::SMALL_PACK}; use git_object::bstr::ByteSlice; - use git_odb::pack::{cache, decode::ResolvedBase}; + use git_odb::pack::{cache, data::decode::ResolvedBase}; fn content_of(path: &str) -> Vec { fixup(std::fs::read(fixture_path(path)).unwrap()) diff --git a/git-odb/tests/pack/index.rs b/git-odb/tests/pack/index.rs index 0e9bc1811b5..2c96c071c82 100644 --- a/git-odb/tests/pack/index.rs +++ b/git-odb/tests/pack/index.rs @@ -2,7 +2,7 @@ use crate::{ fixture_path, hex_to_id, pack::{SMALL_PACK, SMALL_PACK_INDEX}, }; -use git_odb::pack::{self, decode::DecodeEntryOutcome, index}; +use git_odb::pack::{self, data::decode::DecodeEntryOutcome, index}; use pretty_assertions::assert_eq; const INDEX_V2: &str = "packs/pack-11fdfa9e156ab73caae3b6da867192221f2089c2.idx"; @@ -148,9 +148,9 @@ fn pack_lookup() { ), ] { let idx = index::File::at(&fixture_path(index_path)).unwrap(); - let pack = pack::File::at(&fixture_path(pack_path)).unwrap(); + let pack = pack::data::File::at(&fixture_path(pack_path)).unwrap(); - assert_eq!(pack.kind(), pack::Kind::V2); + assert_eq!(pack.kind(), pack::data::Kind::V2); assert_eq!(pack.num_objects(), idx.num_objects()); assert_eq!( idx.verify_checksum_of_index(Some(&pack), None, Discard.into(), || DecodeEntryNoop) diff --git a/git-repository/src/init.rs b/git-repository/src/init.rs index f66e12d31d6..97b8bb04ccb 100644 --- a/git-repository/src/init.rs +++ b/git-repository/src/init.rs @@ -10,11 +10,11 @@ quick_error! { #[derive(Debug)] pub enum Error { IoOpen(err: std::io::Error, path: PathBuf) { - display("Could not open file at '{}'", path.display()) + display("Could not open data at '{}'", path.display()) cause(err) } IoWrite(err: std::io::Error, path: PathBuf) { - display("Could not write file at '{}'", path.display()) + display("Could not write data at '{}'", path.display()) cause(err) } DirectoryExists(path: PathBuf) { diff --git a/gitoxide-core/src/lib.rs b/gitoxide-core/src/lib.rs index e2abd5acce0..31df4dbc55f 100644 --- a/gitoxide-core/src/lib.rs +++ b/gitoxide-core/src/lib.rs @@ -108,23 +108,23 @@ where let path = path.as_ref(); let ext = path.extension().and_then(|ext| ext.to_str()).ok_or_else(|| { anyhow!( - "Cannot determine file type on path without extension '{}', expecting default extensions 'idx' and 'pack'", + "Cannot determine data type on path without extension '{}', expecting default extensions 'idx' and 'pack'", path.display() ) })?; let res = match ext { "pack" => { - let pack = git_odb::pack::File::at(path).with_context(|| "Could not open pack file")?; + let pack = git_odb::pack::data::File::at(path).with_context(|| "Could not open pack data")?; pack.verify_checksum().map(|id| (id, None))? } "idx" => { - let idx = git_odb::pack::index::File::at(path).with_context(|| "Could not open pack index file")?; + let idx = git_odb::pack::index::File::at(path).with_context(|| "Could not open pack index data")?; let packfile_path = path.with_extension("pack"); - let pack = git_odb::pack::File::at(&packfile_path) + let pack = git_odb::pack::data::File::at(&packfile_path) .or_else(|e| { writeln!( err, - "Could not find matching pack file at '{}' - only index file will be verified, error was: {}", + "Could not find matching pack data at '{}' - only index data will be verified, error was: {}", packfile_path.display(), e ) @@ -165,7 +165,7 @@ fn print_statistics(out: &mut impl io::Write, stats: &index::verify::Outcome) -> } writeln!(out, "\t->: {}", total_object_count)?; - let pack::decode::DecodeEntryOutcome { + let pack::data::decode::DecodeEntryOutcome { kind: _, num_deltas, decompressed_size, diff --git a/src/plumbing/lean.rs b/src/plumbing/lean.rs index 64d5bb46018..535cc645cd7 100644 --- a/src/plumbing/lean.rs +++ b/src/plumbing/lean.rs @@ -36,7 +36,7 @@ mod options { /// verbose progress messages are printed line by line #[argh(switch, short = 'v')] pub verbose: bool, - /// the '.pack' or '.idx' file whose checksum to validate. + /// the '.pack' or '.idx' data whose checksum to validate. #[argh(positional)] pub path: PathBuf, } diff --git a/src/plumbing/pretty.rs b/src/plumbing/pretty.rs index cae91e08e2c..a43f326ef21 100644 --- a/src/plumbing/pretty.rs +++ b/src/plumbing/pretty.rs @@ -26,7 +26,7 @@ mod options { #[derive(Debug, StructOpt)] pub enum Subcommands { - /// Verify the integrity of a pack or index file + /// Verify the integrity of a pack or index data #[structopt(setting = AppSettings::ColoredHelp)] VerifyPack { /// output statistical information about the pack @@ -55,7 +55,7 @@ mod options { #[structopt(long, conflicts_with("verbose"), requires("progress"))] progress_keep_open: bool, - /// The '.pack' or '.idx' file whose checksum to validate. + /// The '.pack' or '.idx' data whose checksum to validate. #[structopt(parse(from_os_str))] path: PathBuf, },